/// <summary> /// 菜单项“退出”单击时的事件。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { // 创建载入界面 var task = new Thread(stop => { StopModels(); }) { Name = RegisteredThreadName.ExitStopAllModels.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("正在退出,请稍候", task); loading.ShowDialog(); // 等待结果 if (!new WaitTimeoutChecker(30000).ThreadSleep(500, () => { var msg = MessagePipe.GetNextOutMessage(task); switch (msg) { case Message.NoAvailableMessage: return(true); case Message.TaskOut: return(false); default: throw new Exception($"无效的消息类型:{msg}"); } })) { Environment.Exit(-1); } // 模块已停止 Environment.Exit(0); }
/// <summary> /// 菜单项“启动所有模块”单击时的事件。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void 启动所有模块ToolStripMenuItem_Click(object sender, EventArgs e) { // 判断是否为重启 if (启动所有模块ToolStripMenuItem.Text == "重启所有模块") { var result = false; // 创建载入界面 var task = new Thread(stop => { result = StopModels(); }) { Name = RegisteredThreadName.RestartStopAllModels.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("正在停止,请稍候", task); loading.ShowDialog(); // 等待结果 new WaitTimeoutChecker(30000).ThreadSleep(500, () => { var msg = MessagePipe.GetNextOutMessage(task); switch (msg) { case Message.NoAvailableMessage: return(true); case Message.TaskOut: return(false); default: throw new Exception($"无效的消息类型:{msg}"); } }); // 模块已停止 MessagePipe.ClearAllMessage(task); if (!result) { MessageBox.Show("一个或多个模块未能成功停止,请检查。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } DisableAllItems(); } // 启动模块 if (StartupModels(Models)) { if (Models.Any(item => item.CurDevName == "")) { return; } MessageBox.Show("所有模块均已成功设置。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); EnableAllItems(); } else { MessageBox.Show("一个或多个模块未能成功初始化,请检查。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <inheritdoc /> /// <summary> /// 初始化 <see cref="MainPart" /> 窗口。 /// </summary> public MainPart() { // 初始化模块 TaskHandler.Init(); var task = new Thread(load => { try { // TODO:新增模块时请更新此处的代码 var threads = new[] { new Thread(init => { Scanner = new Scanner(); }), new Thread(init => { Poisoner = new Poisoner(); }), new Thread(init => { Watcher = new Watcher(); }) }; foreach (var thread in threads) { thread.Start(); } new WaitTimeoutChecker(30000).ThreadSleep(500, () => threads.Any(item => item.IsAlive)); } catch (ThreadAbortException) { Environment.Exit(-1); } }) { Name = RegisteredThreadName.ProgramInit.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("初始化中,请稍候", task); loading.ShowDialog(); // 用户取消 if (MessagePipe.GetNextOutMessage(task) == Message.UserCancel) { Environment.Exit(-1); } // 初始化完成(由loading判断得到) MessagePipe.ClearAllMessage(task); TopMost = true; InitializeComponent(); }
/// <summary> /// 菜单项“启动模块”(监视)单击时的事件。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void 启动监视模块ToolStripMenuItem_Click(object sender, EventArgs e) { // 判断工作模式 if (启动监视模块ToolStripMenuItem.Text == "启动模块") { if (!StartupModels(new[] { Watcher })) { MessageBox.Show("模块未能成功初始化,请检查。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (Watcher.CurDevName != "") { MessageBox.Show("模块已成功设置。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); 启动所有模块ToolStripMenuItem.Text = "重启所有模块"; 启动监视模块ToolStripMenuItem.Text = "停止模块"; 开始监视ToolStripMenuItem.Enabled = true; } } else { // 创建载入界面 var task = new Thread(stop => { Watcher.Stop(); Watcher.Reset(); Watcher.CurDevName = ""; }) { Name = RegisteredThreadName.StopWatcher.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("正在停止,请稍候", task); loading.ShowDialog(); // 等待结果 new WaitTimeoutChecker(30000).ThreadSleep(500, () => { var msg = MessagePipe.GetNextOutMessage(task); switch (msg) { case Message.NoAvailableMessage: return(true); case Message.TaskOut: return(false); default: throw new Exception($"无效的消息类型:{msg}"); } }); // 模块已停止 MessagePipe.ClearAllMessage(task); MessageBox.Show("模块已停止。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); 启动监视模块ToolStripMenuItem.Text = "启动模块"; 开始监视ToolStripMenuItem.Enabled = false; ConnectionListUpdateTimer.Stop(); new WaitTimeoutChecker(30000).ThreadSleep(100, () => ConnectionListUpdateTimer.Enabled); ConnectionList.Rows.Clear(); // TODO:新增模块时请更新此处的代码 if (启动扫描模块ToolStripMenuItem.Text == "启动模块" && 启动毒化模块ToolStripMenuItem.Text == "启动模块") { 启动所有模块ToolStripMenuItem.Text = "启动所有模块"; } } }
/// <summary> /// 菜单项“开始毒化”单击时的事件。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void 开始监视ToolStripMenuItem_Click(object sender, EventArgs e) { if (开始监视ToolStripMenuItem.Text == "开始监视") { // 创建载入界面 var task = new Thread(watching => { try { Watcher.StartWatching(); } catch (ThreadAbortException) { // 用户中止了操作 Watcher.Stop(); } }) { Name = RegisteredThreadName.StartWatching.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("正在启动监视,请稍候", task); loading.ShowDialog(); // 等待结果 var result = Message.NoAvailableMessage; new WaitTimeoutChecker(30000).ThreadSleep(500, () => (result = MessagePipe.GetNextOutMessage(task)) == Message.NoAvailableMessage); // 用户取消 if (result == Message.UserCancel) { MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskCancel, task)); new WaitTimeoutChecker(30000).ThreadSleep(500, () => (result = MessagePipe.GetNextOutMessage(task)) == Message.NoAvailableMessage); switch (result) { case Message.TaskAborted: return; case Message.TaskOut: break; case Message.TaskNotFound: MessageBox.Show("未能找到指定名称的工作线程。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; default: throw new Exception($"无效的消息类型:{result}"); } } MessagePipe.ClearAllMessage(task); MessageBox.Show("监视工作已启动。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); ConnectionListUpdateTimer.Start(); 开始监视ToolStripMenuItem.Text = "停止监视"; } else { // 创建载入界面 var task = new Thread(stop => { Watcher.Stop(); }) { Name = RegisteredThreadName.StopWatching.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("正在停止,请稍候", task); loading.ShowDialog(); // 等待结果 new WaitTimeoutChecker(30000).ThreadSleep(500, () => { var msg = MessagePipe.GetNextOutMessage(task); switch (msg) { case Message.NoAvailableMessage: return(true); case Message.TaskOut: return(false); default: throw new Exception($"无效的消息类型:{msg}"); } }); // 模块已停止 MessagePipe.ClearAllMessage(task); ConnectionListUpdateTimer.Stop(); new WaitTimeoutChecker(30000).ThreadSleep(100, () => ConnectionListUpdateTimer.Enabled); ConnectionList.Rows.Clear(); MessageBox.Show("监视工作已停止。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); 开始监视ToolStripMenuItem.Text = "开始监视"; } }
/// <summary> /// 启动指定的模块。 /// </summary> /// <param name="models">需要启动的模块列表。</param> /// <returns>成功返回true,错误返回false。</returns> private static bool StartupModels(IEnumerable <BasicClass> models) { // 判断模块是否初始化 var modelsList = models.ToList(); if (modelsList.Contains(null)) { return(false); } // 选择设备 var index = new PointerPacker(""); var chooseDevice = new ChooseDevice(ref index); while (true) { chooseDevice.ShowDialog(); // 用户点击了取消,中止本次操作 var value = (string)index.Item; if (value.Length == 0) { return(true); } // 检查设备网络是否有效 var device = (WinPcapDevice)CaptureDeviceList.Instance[value]; var isValid = false; foreach (var address in device.Addresses) { if (address.Addr.sa_family != 2) { continue; } isValid = true; break; } if (isValid) { break; } var dialogResult = MessageBox.Show("无法确定指定设备的IPv4网络环境,仍然继续?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (dialogResult == DialogResult.OK) { break; } } // 创建载入界面 var task = new Thread(init => { try { foreach (var model in modelsList) { model.CurDevName = index.Item.ToString(); } } catch (ThreadAbortException) { foreach (var model in modelsList) { model.CurDevName = ""; } } }) { Name = RegisteredThreadName.StartupModels.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("设置模块中,请稍候", task); loading.ShowDialog(); // 等待结果 var result = Message.NoAvailableMessage; new WaitTimeoutChecker(30000).ThreadSleep(500, () => (result = MessagePipe.GetNextOutMessage(task)) == Message.NoAvailableMessage); // 用户取消 if (result == Message.UserCancel) { MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskCancel, task)); new WaitTimeoutChecker(30000).ThreadSleep(500, () => (result = MessagePipe.GetNextOutMessage(task)) == Message.NoAvailableMessage); switch (result) { case Message.TaskAborted: return(false); case Message.TaskOut: break; case Message.TaskNotFound: MessageBox.Show("未能找到指定名称的工作线程。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); default: throw new Exception($"无效的消息类型:{result}"); } } // 任务完成 MessagePipe.ClearAllMessage(task); return(true); }
/// <summary> /// 菜单项“侦测主机”单击时的事件。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void 侦测主机ToolStripMenuItem_Click(object sender, EventArgs e) { // 清空历史纪录 HostList.Rows.Clear(); // 创建载入界面 var task = new Thread(spy => { try { Scanner.SpyForTarget(); } catch (ThreadAbortException) { } }) { Name = RegisteredThreadName.SpyForTarget.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("正在侦测,取消以停止", task); loading.ShowDialog(); // 等待结果 new WaitTimeoutChecker(30000).ThreadSleep(500, () => MessagePipe.GetNextOutMessage(task) != Message.UserCancel); // 用户取消 MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskCancel, task)); var result = Message.NoAvailableMessage; new WaitTimeoutChecker(30000).ThreadSleep(500, () => (result = MessagePipe.GetNextOutMessage(task)) == Message.NoAvailableMessage); MessagePipe.ClearAllMessage(task); // 检查是否正确结束 if (result != Message.TaskAborted) { MessageBox.Show("线程异常结束。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 输出侦测结果 foreach (var host in Scanner.HostList) { // 格式化MAC地址 var temp = new StringBuilder(host.PhysicalAddress.ToString()); if (temp.Length == 12) { temp = temp.Insert(2, '-').Insert(5, '-').Insert(8, '-').Insert(11, '-').Insert(14, '-'); } if (Scanner.GatewayAddresses.Contains(host.IPAddress)) { HostList.Rows.Add(host.IPAddress.ToString(), temp.ToString(), "网关地址"); Poisoner.Gateway = host; } else if (Equals(host.IPAddress, Scanner.Ipv4Address)) { HostList.Rows.Add(host.IPAddress.ToString(), temp.ToString(), "当前设备地址"); } else { HostList.Rows.Add(host.IPAddress.ToString(), temp.ToString(), ""); } HostList.Rows[HostList.Rows.Count - 1].ContextMenuStrip = HostListMenuStrip; } }
/// <summary> /// 菜单项“扫描主机”单击时的事件。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void 扫描主机ToolStripMenuItem_Click(object sender, EventArgs e) { // 清空历史纪录 HostList.Rows.Clear(); // 创建载入界面 var task = new Thread(scan => { try { Scanner.ScanForTarget(); } catch (ThreadAbortException) { Scanner.Reset(); } }) { Name = RegisteredThreadName.ScanForTarget.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("正在扫描,请稍候", task); loading.ShowDialog(); // 等待结果 var result = Message.NoAvailableMessage; new WaitTimeoutChecker(30000).ThreadSleep(500, () => (result = MessagePipe.GetNextOutMessage(task)) == Message.NoAvailableMessage); // 用户取消 if (result == Message.UserCancel) { MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskCancel, task)); new WaitTimeoutChecker(30000).ThreadSleep(500, () => (result = MessagePipe.GetNextOutMessage(task)) == Message.NoAvailableMessage); switch (result) { case Message.TaskAborted: return; case Message.TaskOut: break; case Message.TaskNotFound: MessageBox.Show("未能找到指定名称的工作线程。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; default: throw new Exception($"无效的消息类型:{result}"); } } MessagePipe.ClearAllMessage(task); // 输出扫描结果 foreach (var host in Scanner.HostList) { // 格式化MAC地址 var temp = new StringBuilder(host.PhysicalAddress.ToString()); if (temp.Length == 12) { temp = temp.Insert(2, '-').Insert(5, '-').Insert(8, '-').Insert(11, '-').Insert(14, '-'); } if (Scanner.GatewayAddresses.Contains(host.IPAddress)) { HostList.Rows.Add(host.IPAddress.ToString(), temp.ToString(), "网关地址"); Poisoner.Gateway = host; } else if (Equals(host.IPAddress, Scanner.Ipv4Address)) { HostList.Rows.Add(host.IPAddress.ToString(), temp.ToString(), "当前设备地址"); } else { HostList.Rows.Add(host.IPAddress.ToString(), temp.ToString(), ""); } HostList.Rows[HostList.Rows.Count - 1].ContextMenuStrip = HostListMenuStrip; } }
/// <summary> /// 菜单项“启动模块”(毒化)单击时的事件。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void 启动毒化模块ToolStripMenuItem_Click(object sender, EventArgs e) { // 判断工作模式 if (启动毒化模块ToolStripMenuItem.Text == "启动模块") { if (!StartupModels(new[] { Poisoner })) { MessageBox.Show("模块未能成功初始化,请检查。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (Poisoner.CurDevName != "") { // 查找网关 if (Poisoner.Gateway is null) { foreach (var host in Scanner.HostList) { if (Scanner.GatewayAddresses.Contains(host.IPAddress)) { Poisoner.Gateway = host; } } } MessageBox.Show("模块已成功设置。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); 启动所有模块ToolStripMenuItem.Text = "重启所有模块"; 启动毒化模块ToolStripMenuItem.Text = "停止模块"; 开始毒化ToolStripMenuItem.Enabled = true; 添加到目标组1ToolStripMenuItem.Enabled = true; 添加到目标组2ToolStripMenuItem.Enabled = true; 阻止此连接ToolStripMenuItem.Enabled = true; } } else { // 创建载入界面 var task = new Thread(stop => { Poisoner.Stop(); Poisoner.Reset(); Poisoner.CurDevName = ""; }) { Name = RegisteredThreadName.StopPoisoner.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("正在停止,请稍候", task); loading.ShowDialog(); // 等待结果 new WaitTimeoutChecker(30000).ThreadSleep(500, () => { var msg = MessagePipe.GetNextOutMessage(task); switch (msg) { case Message.NoAvailableMessage: return(true); case Message.TaskOut: return(false); default: throw new Exception($"无效的消息类型:{msg}"); } }); // 模块已停止 MessagePipe.ClearAllMessage(task); MessageBox.Show("模块已停止。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); 启动毒化模块ToolStripMenuItem.Text = "启动模块"; 开始毒化ToolStripMenuItem.Enabled = false; 添加到目标组1ToolStripMenuItem.Enabled = false; 添加到目标组2ToolStripMenuItem.Enabled = false; 阻止此连接ToolStripMenuItem.Enabled = false; Target1List.Rows.Clear(); Target2List.Rows.Clear(); BlockList.Rows.Clear(); // TODO:新增模块时请更新此处的代码 if (启动扫描模块ToolStripMenuItem.Text == "启动模块" && 启动监视模块ToolStripMenuItem.Text == "启动模块") { 启动所有模块ToolStripMenuItem.Text = "启动所有模块"; } } }
/// <summary> /// 菜单项“开始毒化”单击时的事件。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void 开始毒化ToolStripMenuItem_Click(object sender, EventArgs e) { if (开始毒化ToolStripMenuItem.Text == "开始毒化") { if (MessageBox.Show("注意:在重新启动毒化工作前,目标无法被更改,仍然启动?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel) { return; } if (Poisoner.Gateway is null && MessageBox.Show("未能找到默认网关,被毒化设备将不能接入外部网络,仍然继续?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel) { return; } // 创建载入界面 var task = new Thread(poisoning => { try { Poisoner.Target1.Clear(); Poisoner.Target2.Clear(); foreach (DataGridViewRow target in Target1List.Rows) { Poisoner.Target1.Add(new Host(IPAddress.Parse(target.Cells["Target1IP"].Value.ToString()), PhysicalAddress.Parse(target.Cells["Target1MAC"].Value.ToString()))); } foreach (DataGridViewRow target in Target2List.Rows) { Poisoner.Target2.Add(new Host(IPAddress.Parse(target.Cells["Target2IP"].Value.ToString()), PhysicalAddress.Parse(target.Cells["Target2MAC"].Value.ToString()))); } Poisoner.StartPoisoning(); } catch (ThreadAbortException) { // 用户中止了操作 Poisoner.Stop(); } }) { Name = RegisteredThreadName.StartPoisoning.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("正在毒化,请稍候", task); loading.ShowDialog(); // 等待结果 var result = Message.NoAvailableMessage; new WaitTimeoutChecker(30000).ThreadSleep(500, () => (result = MessagePipe.GetNextOutMessage(task)) == Message.NoAvailableMessage); // 用户取消 if (result == Message.UserCancel) { MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskCancel, task)); new WaitTimeoutChecker(30000).ThreadSleep(500, () => (result = MessagePipe.GetNextOutMessage(task)) == Message.NoAvailableMessage); switch (result) { case Message.TaskAborted: return; case Message.TaskOut: break; case Message.TaskNotFound: MessageBox.Show("未能找到指定名称的工作线程。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; default: throw new Exception($"无效的消息类型:{result}"); } } MessagePipe.ClearAllMessage(task); MessageBox.Show("毒化工作已启动。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); 开始毒化ToolStripMenuItem.Text = "停止毒化"; } else { // 创建载入界面 var task = new Thread(stop => { Poisoner.Stop(); }) { Name = RegisteredThreadName.StopPoisoning.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("正在停止,请稍候", task); loading.ShowDialog(); // 等待结果 new WaitTimeoutChecker(30000).ThreadSleep(500, () => { var msg = MessagePipe.GetNextOutMessage(task); switch (msg) { case Message.NoAvailableMessage: return(true); case Message.TaskOut: return(false); default: throw new Exception($"无效的消息类型:{msg}"); } }); // 模块已停止 MessagePipe.ClearAllMessage(task); MessageBox.Show("毒化工作已停止。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); 开始毒化ToolStripMenuItem.Text = "开始毒化"; } }