-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
267 lines (235 loc) · 9.7 KB
/
Program.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Threading;
namespace WinpCapInstaller
{
class Program
{
static string DLL_PACKET_NT4 = "Packet_nt4_x86.dll";
static string DLL_PACKET_NT5_x86 = "Packet_nt5_x86.dll";
static string DLL_PACKET_NT5_x64 = "Packet_nt5_x64.dll";
static string DLL_PACKET_Vista_x86 = "Packet_Vista_x86.dll";
static string DLL_PACKET_Vista_x64 = "Packet_Vista_x64.dll";
static string DLL_PACKET = "Packet.dll";
static string DLL_WPCAP_x86 = "wpcap_x86.dll";
static string DLL_WPCAP_x64 = "wpcap_x64.dll";
static string DLL_WPCAP = "wpcap.dll";
static string DLL_PTHREAD_VC = "pthreadVC.dll";
static string DRIVER_NPF_NT4 = "npf_nt4_x86.sys";
static string DRIVER_NPF_NT5_NT6_x86 = "npf_nt5_nt6_x86.sys";
static string DRIVER_NPF_NT5_NT6_x64 = "npf_nt5_nt6_x64.sys";
static string DRIVER_NPF = "npf.sys";
static string DLL_DST_PATH = "C:\\windows\\System32";
static string DLL_DST_PATH_x86_64 = "C:\\Windows\\sysWOW64";
static string DRIVER_DST_PATH = "C:\\Windows\\System32\\Drivers";
static void Main(string[] args)
{
installWinpCap();
//isInstalledWinPcap();
//installWinpCap();
//Console.WriteLine(a.ToString());
//Console.ReadLine();
//uninstallWinpCap();
}
public static bool isInstalledWinPcap()
{
bool result = File.Exists(Path.Combine(DLL_DST_PATH, DLL_PTHREAD_VC));
result = result && File.Exists(Path.Combine(DLL_DST_PATH, DLL_WPCAP));
result = result && File.Exists(Path.Combine(DLL_DST_PATH, DLL_PACKET));
result = result && File.Exists(Path.Combine(DRIVER_DST_PATH, DRIVER_NPF));
if (Environment.Is64BitOperatingSystem)
{
result = result && File.Exists(Path.Combine(DLL_DST_PATH_x86_64, DLL_WPCAP));
result = result && File.Exists(Path.Combine(DLL_DST_PATH_x86_64, DLL_PACKET));
}
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/C sc query npf",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
}
};
proc.Start();
List<string> outputProcessList = new List<string>();
while (!proc.StandardOutput.EndOfStream)
{
outputProcessList.Add(proc.StandardOutput.ReadLine());
}
for (int i = 0; i < outputProcessList.Count; i++)
{
if (outputProcessList[i].Contains("STATE") && outputProcessList[i].Contains("RUNNING"))
{
result = true;
break;
}
else
{
result = false;
}
}
return result;
}
public static void installWinpCap()
{
string winpcap_path = "";
string pthread_dll = Path.Combine(winpcap_path, DLL_PTHREAD_VC);
string wpcap_dll_x86 = Path.Combine(winpcap_path, DLL_WPCAP_x86);
string wpcap_dll_x64 = "";
if (Environment.Is64BitOperatingSystem)
{
wpcap_dll_x64 = Path.Combine(winpcap_path, DLL_WPCAP_x64);
}
int version = System.Environment.OSVersion.Version.Major;
string packet_dll_x86 = "";
string packet_dll_x64 = "";
string driver_sys = "";
if (version < 5)
{
// NT 4.0 Windows or older
packet_dll_x86 = Path.Combine(winpcap_path, DLL_PACKET_NT4);
packet_dll_x64 = "";
driver_sys = Path.Combine(winpcap_path, DRIVER_NPF_NT4);
}
else if (version < 6)
{
// NT 5.0 Windows or older
packet_dll_x86 = Path.Combine(winpcap_path, DLL_PACKET_NT5_x86);
if (Environment.Is64BitOperatingSystem)
{
packet_dll_x64 = Path.Combine(winpcap_path, DLL_PACKET_NT5_x64);
driver_sys = Path.Combine(winpcap_path, DRIVER_NPF_NT5_NT6_x64);
}
else
{
packet_dll_x64 = "";
driver_sys = Path.Combine(winpcap_path, DRIVER_NPF_NT5_NT6_x86);
}
}
else
{
// NT 6.0 Windows or newer
packet_dll_x86 = Path.Combine(winpcap_path, DLL_PACKET_Vista_x86);
if (Environment.Is64BitOperatingSystem)
{
packet_dll_x64 = Path.Combine(winpcap_path, DLL_PACKET_Vista_x64);
driver_sys = Path.Combine(winpcap_path, DRIVER_NPF_NT5_NT6_x64);
}
else
{
packet_dll_x64 = "";
driver_sys = Path.Combine(winpcap_path, DRIVER_NPF_NT5_NT6_x86);
}
}
try
{
File.Copy(pthread_dll, Path.Combine(DLL_DST_PATH, DLL_PTHREAD_VC), true);
if (wpcap_dll_x64 == "")
{
File.Copy(wpcap_dll_x86, Path.Combine(DLL_DST_PATH, DLL_WPCAP), true);
}
else
{
File.Copy(wpcap_dll_x86, Path.Combine(DLL_DST_PATH_x86_64, DLL_WPCAP), true);
File.Copy(wpcap_dll_x64, Path.Combine(DLL_DST_PATH, DLL_WPCAP), true);
}
if (packet_dll_x64 == "")
{
File.Copy(packet_dll_x86, Path.Combine(DLL_DST_PATH, DLL_PACKET), true);
}
else
{
File.Copy(packet_dll_x86, Path.Combine(DLL_DST_PATH_x86_64, DLL_PACKET), true);
File.Copy(packet_dll_x64, Path.Combine(DLL_DST_PATH, DLL_PACKET), true);
}
File.Copy(driver_sys, Path.Combine(DRIVER_DST_PATH, DRIVER_NPF), true);
}
catch (IOException iox)
{
}
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/C sc create npf binPath=system32\\drivers\\npf.sys type=kernel start=auto error=normal tag=no DisplayName=\"NetGroup Packet Filter Driver\"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
var a = proc.StandardOutput.ReadLine();
Console.WriteLine(a);
}
var proc1 = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/C sc start npf",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc1.Start();
while (!proc1.StandardOutput.EndOfStream)
{
var a = proc1.StandardOutput.ReadLine();
Console.WriteLine(a);
}
//Process.Start("sc create npf binPath=system32\\drivers\\npf.sys type=kernel start=auto error=normal tag=no DisplayName=\"NetGroup Packet Filter Driver\"");
//Process.Start("sc start npf");
}
public static void uninstallWinpCap()
{
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/C sc stop npf",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
var proc1 = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/C sc delete npf",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc1.Start();
if (File.Exists(Path.Combine(DLL_DST_PATH, DLL_PTHREAD_VC)))
File.Delete(Path.Combine(DLL_DST_PATH, DLL_PTHREAD_VC));
if (File.Exists(Path.Combine(DLL_DST_PATH, DLL_WPCAP)))
File.Delete(Path.Combine(DLL_DST_PATH, DLL_WPCAP));
if (File.Exists(Path.Combine(DLL_DST_PATH, DLL_PACKET)))
File.Delete(Path.Combine(DLL_DST_PATH, DLL_PACKET));
if (File.Exists(Path.Combine(DRIVER_DST_PATH, DRIVER_NPF)))
File.Delete(Path.Combine(DRIVER_DST_PATH, DRIVER_NPF));
if (Environment.Is64BitOperatingSystem)
if (File.Exists(Path.Combine(DLL_DST_PATH_x86_64, DLL_WPCAP)))
File.Delete(Path.Combine(DLL_DST_PATH_x86_64, DLL_WPCAP));
if (File.Exists(Path.Combine(DLL_DST_PATH_x86_64, DLL_PACKET)))
File.Delete(Path.Combine(DLL_DST_PATH_x86_64, DLL_PACKET));
}
}
}