-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
170 lines (138 loc) · 5.63 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TrojanWrapper
{
public delegate void DelReadStdOutput(string result);
public delegate void DelReadErrOutput(string result);
public partial class Form1 : Form
{
public event DelReadStdOutput ReadStdOutput;
public event DelReadErrOutput ReadErrOutput;
public Form1()
{
InitializeComponent();
ReadStdOutput += new DelReadStdOutput(ReadStdOutputAction);
ReadErrOutput += new DelReadErrOutput(ReadErrOutputAction);
this.Opacity = 0;
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
// KillTrojan();
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
Hide();
RegistryKey reg = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
string exePath = Process.GetCurrentProcess().MainModule.FileName;
string exeName = Process.GetCurrentProcess().MainModule.ModuleName;
var value = reg.GetValue(exeName);
if (value != null)
{
autoStratupMenu.Checked = true;
if (!value.ToString().Equals(exePath))
{
reg.SetValue(exeName, exePath);
}
}
System.IO.Directory.SetCurrentDirectory(Application.StartupPath);
// MessageBox.Show(System.IO.Directory.GetCurrentDirectory());
// MessageBox.Show(Application.StartupPath);
Process cmdProcess = new Process();
cmdProcess.StartInfo.FileName = "trojan.exe"; // 命令
cmdProcess.StartInfo.CreateNoWindow = true; // 不创建新窗口
cmdProcess.StartInfo.UseShellExecute = false;
cmdProcess.StartInfo.RedirectStandardInput = true; // 重定向输入
cmdProcess.StartInfo.RedirectStandardOutput = true; // 重定向标准输出
cmdProcess.StartInfo.RedirectStandardError = true; // 重定向错误输出
//CmdProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmdProcess.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
cmdProcess.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
cmdProcess.EnableRaisingEvents = true; // 启用Exited事件
cmdProcess.Exited += new EventHandler(CmdProcess_Exited); // 注册进程结束事件
cmdProcess.Start();
cmdProcess.BeginOutputReadLine();
cmdProcess.BeginErrorReadLine();
Process privoxyProcess = new Process
{
// Configure the process using the StartInfo properties.
StartInfo =
{
FileName = "privoxy.exe",
Arguments = "config.txt",
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
CreateNoWindow = true
}
};
privoxyProcess.Start();
}
private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
// 4. 异步调用,需要invoke
this.Invoke(ReadStdOutput, new object[] { e.Data });
}
}
private void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
this.Invoke(ReadErrOutput, new object[] { e.Data });
}
}
private void ReadStdOutputAction(string result)
{
}
private void ReadErrOutputAction(string result)
{
}
private void CmdProcess_Exited(object sender, EventArgs e)
{
// 执行结束后触发
}
private void autoStratupMenu_Click(object sender, EventArgs e)
{
RegistryKey reg = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
string exePath = Process.GetCurrentProcess().MainModule.FileName;
string exeName = Process.GetCurrentProcess().MainModule.ModuleName;
if (autoStratupMenu.Checked)
{
reg.DeleteValue(exeName);
autoStratupMenu.Checked = false;
}
else
{
reg.SetValue(exeName, exePath);
autoStratupMenu.Checked = true;
}
}
private void Form1_Shown(object sender, EventArgs e)
{
Hide();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Kill();
}
private void Kill()
{
string[] names = new string[] { "trojan", "privoxy" };
var pro = Process.GetProcesses().Where(p=>names.Contains(p.ProcessName.ToLower()));
foreach (Process p in pro)
{
p.Kill();
}
}
}
}