-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProgram.cs
566 lines (441 loc) · 19.2 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpPcap.WinPcap;
using SharpPcap;
using System.Data;
using PacketDotNet;
using System.Net.NetworkInformation;
using System.Net;
using Ng_IDS.Model;
using System.Threading;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.IO;
using System.Collections;
namespace Open_HIDS
{
class Program
{
static int devIndex;
static public string gat { get; set; }
static public string Hip { get; set; }
static public string Hmac { get; set; }
static public string Hinter { get; set; }
static public string Hdefullt { get; set; }
static public string Rip { get; set; }
static public string Rmac { get; set; }
static public bool RunArp { get; set; }
static public bool RunDhcp { get; set; }
static public bool RunAll { get; set; }
static public bool scan { get; set; }
static string commend;
public static int num { get; set; }
static void Main(string[] args)
{
Console.WriteLine(Environment.NewLine);
Console.Title = "入侵检测系统";
Console.WriteLine(@"");
Console.WriteLine(@"计算机1621 ");
Console.WriteLine(@"1630107137 陈灿婷");
Console.WriteLine(@"入侵检测系统的设计与实现");
Console.WriteLine(Environment.NewLine);
if (!File.Exists("data.sqlite"))
{
ado o = new ado();
o.creatDB();
}
if (!File.Exists("ports.port"))
{
create_file();
}
try
{
var devic = WinPcapDeviceList.Instance;
}
catch (Exception)
{
Console.WriteLine("没有发现接口!确保在本地机器上正确安装libpcap /WinPcap.");
Thread.Sleep(5000);
return;
}
var devices = WinPcapDeviceList.Instance;
if (devices.Count < 1)
{
Console.WriteLine("在这台机器上没有发现任何设备,请确认你已经安装了winpcap");
return;
}
Console.WriteLine("请选择一个选项*** :");
Console.WriteLine("********************************");
Console.WriteLine();
int i = 0;
foreach (var dev in devices)
{
Console.WriteLine("{0}) {1}", i, dev.Description);
Console.WriteLine(Environment.NewLine);
i++;
}
Console.WriteLine();
Console.Write("***请选择一个选项***: ");
i = int.Parse(Console.ReadLine());
devIndex = i;
if (devices.Count < i)
{
Console.WriteLine("***{0} 是不正确的*** : ", i.ToString());
Console.WriteLine("***请选择一个选项*** : ");
i = int.Parse(Console.ReadLine());
if (devices.Count < i)
{
return;
}
// ;
}
var device = devices[i];
device.Open(DeviceMode.Promiscuous, 1000);
num = 0;
foreach (var item in device.Addresses)
{
if (item.Addr.ipAddress != null)
{
if (item.Addr.ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
int inf = Array.IndexOf(device.Addresses.ToArray(), item);
num = inf;
}
}
}
Console.WriteLine(Environment.NewLine);
Console.WriteLine("界面 : {0}", device.Description);
Hip = device.Addresses[num].Addr.ToString();
Hmac = device.MacAddress.ToString();
if (device.Interface.GatewayAddress == null)
{
Console.WriteLine("你没有GatewayAddress");
Console.ReadKey();
return;
}
gat = device.Interface.GatewayAddress.ToString();
Hdefullt = device.Interface.GatewayAddress.ToString();
Hinter = device.Description.Replace("'", "");
Console.WriteLine(Environment.NewLine);
Console.WriteLine("IP Address : {0}",device.Addresses[num].Addr);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("MAC Address : {0}",device.MacAddress.ToString());
if (IPAddress.Parse(gat).AddressFamily == AddressFamily.InterNetworkV6)
{
Console.WriteLine("Catch不能找到你的网关地址,请输入你的网关地址 : ");
var g = Console.ReadLine();
gat = g;
}
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Defult Gatway : {0}",gat);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("__________________________Router_______________________________");
Console.WriteLine("Router IP Address : {0}", gat);
string myip = device.Addresses[num].Addr.ToString();
try
{
IPAddress address = IPAddress.Parse(gat);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + " " + gat);
Thread.Sleep(3000);
return;
}
if (string.IsNullOrEmpty(gat)!= null)
{
EthernetPacket eth = Protect_Arp(device.MacAddress.ToString(), "FFFFFFFFFFFF", myip, gat);
device.SendPacket(eth);
}
Thread th = new Thread(() => {
device.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival);
});
th.Start();
Rmac = getGAtWatWayMac(device,gat);
if (Rmac == "")
{
Console.WriteLine("您没有连接到路由器,重新尝试");
Console.ReadKey();
return;
}
Console.WriteLine("Router MAC Address : {0}", Rmac);
Rip = gat;
device.StartCapture();
Console.WriteLine(Environment.NewLine);
ado a = new ado();
if (a.checkpc(Hip,Hmac,Hinter).Rows.Count > 0)
{
}
else
{
Data d = new Data() { date = DateTime.Now.ToString(), inter = Hinter, name="Pc", ip= Hip, mac=Hmac };
a.insert(d);
}
if (a.selectname("Router", Hinter).Rows.Count > 0)
{
int id = 0;
DataTable dt = a.selectname("Router", Hinter);
foreach (DataRow item in dt.Rows)
{
string ip = item[4].ToString();
string mac = item[3].ToString();
string Time = item[5].ToString();
id = Convert.ToInt32(item[0]);
if (mac == Rmac)
{
}
else
{
cheeck(new ado(),ip, mac, Time, id);
}
}
}
else
{
Data d = new Data() { inter = Hinter, date= DateTime.Now.ToString(), ip = Rip, mac = Rmac, name = "Router"};
a.insert(d);
}
Runcmd();
}
static void device_OnPacketArrival(object sender, CaptureEventArgs e)
{
scan s = new scan();
if (RunArp == true)
{
s.ScanAttack(e, Hinter,Rip);
}
if (RunDhcp == true)
{
s.ScanDhcp(e, Hinter);
}
if (s.Attack == true)
{
for (int i = 0; i < 3; i++)
{
System.Media.SystemSounds.Hand.Play();
Console.WriteLine("********************* You Have Been Attacked **************************");
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Attack Name : {0} , Time : {1} , Attacker HardwareAddress : {2} , Attacker ip address : {3} ", s.Attack_data[0], s.Attack_data[3], s.Attack_data[2], s.Attack_data[1]);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Old data {0} {1} {2} {3} ", s.Attack_data[4], s.Attack_data[5], s.Attack_data[6], s.Attack_data[7]);
System.Media.SystemSounds.Hand.Play();
}
log.attackLog(s);
}
var txt = File.ReadAllLines("ports.port");
var tcp = (from t in txt where t.Contains("tcp") select t).ToArray();
if (scan == true)
{
foreach (var item in txt)
{
if (item.Contains("tcp"))
{
int inte = Array.IndexOf(txt, item);
string cm = txt[inte + 1];
string r = item.Replace("tcp", "");
int p = Convert.ToInt16(r);
if (cm.StartsWith("#"))
{
scanTCP(e, p, cm);
}
else
{
string commend = "您正在使用非加密的明文协议请求使用安全协议";
scanTCP(e, p, commend);
}
}
if (item.Contains("udp"))
{
int inte = Array.IndexOf(txt, item);
string cm = txt[inte + 1];
string r = item.Replace("udp", "");
int p = Convert.ToInt16(r);
if (cm.StartsWith("#"))
{
scanUDP(e, p, cm);
}
else
{
string commend = "您正在使用非加密的明文协议请求使用安全协议";
scanUDP(e, p, commend);
}
}
}
}
}
public static void Runcmd()
{
Cmd();
Console.WriteLine("有关特定命令的更多信息,键入- help命令名");
commend = Console.ReadLine();
if (commend.Equals("--start arp"))
{
Console.WriteLine(" 成功启动Arp欺骗检测工具 ");
RunArp = true;
}
else if (commend == "--start dhcp")
{
Console.WriteLine(" 成功启动dhcp欺骗检测工具");
RunDhcp = true;
}
else if (commend == "--start all")
{
Console.WriteLine(" 成功启动所有工具");
RunArp = true;
RunDhcp = true;
scan = true;
}
else if (commend == "--attacks")
{
if (!File.Exists("AttacksDB.txt"))
{
Console.WriteLine("数据库中没有攻击");
Thread.Sleep(1000);
Runcmd();
}
else
{
var txt = File.ReadAllLines("AttacksDB.txt");
foreach (var item in txt)
{
Console.WriteLine(item);
}
Console.WriteLine("按回车键返回 ");
if (Console.ReadKey().Key == ConsoleKey.Enter)
{
Thread.Sleep(500);
Runcmd();
}
}
}
else if (commend == "--start scan")
{
scan = true;
Console.WriteLine("Start Scan");
}
else if (commend == "-help")
{
Runcmd();
}
else
{
Console.WriteLine(commend + " 不是命令吗,");
Thread.Sleep(1000);
Runcmd();
}
}
public static string getGAtWatWayMac(WinPcapDevice dev,string GatewayAddress)
{
RawCapture packet;
while ((packet = dev.GetNextPacket()) != null)
{
var mypacket = Packet.ParsePacket(packet.LinkLayerType, packet.Data);
var arp = (ARPPacket)mypacket.Extract(typeof(ARPPacket));
if (arp != null)
{
if (arp.Operation == ARPOperation.Response)
{
if (arp.SenderProtocolAddress.Address == IPAddress.Parse(GatewayAddress).Address)
{
return arp.SenderHardwareAddress.ToString();
}
}
}
}
return "";
}
public static EthernetPacket Protect_Arp(string Router_mac, string My_pc_mac, string Router_ip, string My_pc_ip)
{
var eth = new EthernetPacket(PhysicalAddress.Parse(Router_mac), PhysicalAddress.Parse(My_pc_mac), EthernetPacketType.Arp);
var arp = new ARPPacket(ARPOperation.Request, PhysicalAddress.Parse(My_pc_mac), IPAddress.Parse(My_pc_ip), PhysicalAddress.Parse(Router_mac), IPAddress.Parse(Router_ip));
eth.PayloadPacket = arp;
return eth;
}
static void scanTCP(CaptureEventArgs e, int port, string cm)
{
var _packet = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
var tcp = (TcpPacket)_packet.Extract(typeof(TcpPacket));
if (tcp != null)
{
if (tcp.DestinationPort == port)
{
var dst_ip = IpPacket.GetEncapsulated(_packet).DestinationAddress.ToString();
var src_ip = IpPacket.GetEncapsulated(_packet).SourceAddress.ToString();
Console.WriteLine("******************************************************************************");
Console.WriteLine(cm+" {0}", port.ToString());
Console.WriteLine("Source: {0} " + "Destination: {1}",src_ip,dst_ip);
}
}
}
static void scanUDP(CaptureEventArgs e, int port, string cm)
{
var _packet = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
var udp = (UdpPacket)_packet.Extract(typeof(UdpPacket));
if (udp != null)
{
if (udp.DestinationPort == port)
{
var dst_ip = IpPacket.GetEncapsulated(_packet).DestinationAddress.ToString();
var src_ip = IpPacket.GetEncapsulated(_packet).SourceAddress.ToString();
Console.WriteLine("********************************************************************");
Console.WriteLine(cm + " {0}", port.ToString());
Console.WriteLine("Source: {0} " + "Destination: {1}", src_ip, dst_ip);
}
}
}
static void create_file()
{
using (StreamWriter write = new StreamWriter("ports.port", true))
{
write.WriteLine("80 tcp");
write.WriteLine("#You Are using Clear Text Protocol http Pleas Use secure Protocol Https");
write.WriteLine("21 tcp");
write.WriteLine("#You Are using Clear Text Protocol FTP Pleas Use secure Protocol like SFTP or FTPS");
write.WriteLine("143 tcp");
write.WriteLine("#You Are using Clear Text Protocol IMAP Pleas Use secure Protocol IMAP with ssl");
write.WriteLine("20 tcp");
write.WriteLine("#You Are using Clear Text Protocol FTP Pleas Use secure Protocol like SFTP or FTPS");
write.WriteLine("110 tcp");
write.WriteLine("#You Are using Clear Text Protocol POP3 Pleas Use secure Protocol POP3 with ssl");
write.WriteLine("23 tcp");
write.WriteLine("#You Are using Clear Text Protocol Telnet Pleas Use secure Protocol like SSH");
write.WriteLine("25 tcp");
write.WriteLine("#You Are using Clear Text Protocol SMTP Pleas Use secure Protocol SMTP with ssl");
}
}
public static void Cmd()
{
Console.WriteLine(@"
Catch 规则
--start arp 用于检测Arp攻击检测 (Arp spoofing MITM)
--start dhcp 用于检测Dhcp攻击检测 (Dhcp spoofing MITM )
--start scan 这是在使用明文协议时通知您
Like (Http) or (Telent)
--Start All 这是开始所有的功能
--attacks 看到以前所有的攻击记录
");
}
public static void cheeck(ado a, string ip, string mac, string Time, int id)
{
Console.WriteLine("你在处理路由器吗 {0}", Environment.MachineName);
Console.WriteLine("The old Data IP address: {0}, Mac address {1}, Time : {2} , And Interface : {3} , and ID = {4}", ip, mac, Time, Hinter,id);
Console.Write(Environment.NewLine);
Console.WriteLine("Yas 如果你选择了Yas,Catch会考虑这是你的路由器");
Console.WriteLine("No 如果你没有选择,Catch会认为这是攻击,所以请小心你选择的 ");
string ch = Console.ReadLine();
if (ch == "Yas")
{
a.Delete(id);
Console.WriteLine("Delete Old Data {0}", id.ToString());
Data d = new Data() { inter = Hinter, date = DateTime.Now.ToString(), ip = Rip, mac = Rmac, name = "Router" };
a.insert(d);
}
if (ch == "no")
{
Console.WriteLine("********************* You Have Been Attacked **************************");
}
}
}
}