-
Notifications
You must be signed in to change notification settings - Fork 1
/
RawSocket.cs
296 lines (258 loc) · 9.55 KB
/
RawSocket.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
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace PacketSnifferNET
{
[StructLayout(LayoutKind.Explicit)]
public struct IPHeader
{
[FieldOffset(0)] public byte ip_verlen;
[FieldOffset(1)] public byte ip_tos;
[FieldOffset(2)] public ushort ip_totallength;
[FieldOffset(4)] public ushort ip_id;
[FieldOffset(6)] public ushort ip_offset;
[FieldOffset(8)] public byte ip_ttl;
[FieldOffset(9)] public byte ip_protocol;
[FieldOffset(10)] public ushort ip_checksum;
[FieldOffset(12)] public uint ip_srcaddr;
[FieldOffset(16)] public uint ip_destaddr;
}
public static class OperatingSystem
{
public static bool IsWindows() =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public static bool IsMacOS() =>
RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
public static bool IsLinux() =>
RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
}
public class RawSocket
{
private bool error_occurred;
public bool KeepRunning;
private static int len_receive_buf;
byte[] receive_buf_bytes;
private Socket socket = null;
const int SIO_RCVALL = unchecked((int)0x98000001);
const int ETH_P_ALL = 0x0003;
public class PacketArrivedEventArgs : EventArgs
{
public PacketArrivedEventArgs()
{
this.protocol = "";
this.destination_port = "";
this.origination_port = "";
this.destination_address = "";
this.origination_address = "";
this.ip_version = "";
this.total_packet_length = 0;
this.message_length = 0;
this.header_length = 0;
this.receive_buf_bytes = new byte[len_receive_buf];
this.ip_header_bytes = new byte[len_receive_buf];
this.message_bytes = new byte[len_receive_buf];
}
public string Protocol
{
get { return protocol; }
set { protocol = value; }
}
public string DestinationPort
{
get { return destination_port; }
set { destination_port = value; }
}
public string OriginationPort
{
get { return origination_port; }
set { origination_port = value; }
}
public string DestinationAddress
{
get { return destination_address; }
set { destination_address = value; }
}
public string OriginationAddress
{
get { return origination_address; }
set { origination_address = value; }
}
public string IPVersion
{
get { return ip_version; }
set { ip_version = value; }
}
public uint PacketLength
{
get { return total_packet_length; }
set { total_packet_length = value; }
}
public uint MessageLength
{
get { return message_length; }
set { message_length = value; }
}
public uint HeaderLength
{
get { return header_length; }
set { header_length = value; }
}
public byte[] ReceiveBuffer
{
get { return receive_buf_bytes; }
set { receive_buf_bytes = value; }
}
public byte[] IPHeaderBuffer
{
get { return ip_header_bytes; }
set { ip_header_bytes = value; }
}
public byte[] MessageBuffer
{
get { return message_bytes; }
set { message_bytes = value; }
}
private string protocol;
private string destination_port;
private string origination_port;
private string destination_address;
private string origination_address;
private string ip_version;
private uint total_packet_length;
private uint message_length;
private uint header_length;
private byte[] receive_buf_bytes = null;
private byte[] ip_header_bytes = null;
private byte[] message_bytes = null;
}
public event PacketArrivedEventHandler PacketArrival;
public delegate void PacketArrivedEventHandler(Object sender, PacketArrivedEventArgs args);
protected virtual void OnPacketArrival(PacketArrivedEventArgs e)
{
PacketArrival?.Invoke(this, e);
}
/// <summary>
/// Raw Socket Constructor
/// </summary>
public RawSocket()
{
error_occurred = false;
len_receive_buf = 4096;
receive_buf_bytes = new byte[len_receive_buf];
}
/// <summary>
///
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <param name="protocal"></param>
public void CreateAndBindSocket(string IP, int port = 0, ProtocolType protocal = ProtocolType.IP)
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, protocal);
socket.Blocking = false;
socket.Bind(new IPEndPoint(IPAddress.Parse(IP), port));
if (SetSocketOption() == false) error_occurred = true;
}
private bool SetSocketOption()
{
bool ret_value = true;
try
{
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1);
byte[] IN = new byte[4] { 1, 0, 0, 0 };
byte[] OUT = new byte[4];
int ret_code = socket.IOControl(SIO_RCVALL, IN, OUT);
ret_code = OUT[0] + OUT[1] + OUT[2] + OUT[3];
if (ret_code != 0) ret_value = false;
}
catch (SocketException)
{
ret_value = false;
}
return ret_value;
}
public bool ErrorOccurred
{
get
{
return error_occurred;
}
}
unsafe private void Receive(byte[] buf, int len)
{
byte temp_protocol = 0;
uint temp_version = 0;
uint temp_ip_srcaddr = 0;
uint temp_ip_destaddr = 0;
short temp_srcport = 0;
short temp_dstport = 0;
IPAddress temp_ip;
PacketArrivedEventArgs e = new PacketArrivedEventArgs();
fixed (byte* fixed_buf = buf)
{
IPHeader* head = (IPHeader*)fixed_buf;
e.HeaderLength = (uint)(head->ip_verlen & 0x0F) << 2;
//Extract Network Protocal Type
temp_protocol = head->ip_protocol;
switch (temp_protocol)
{
case 1: e.Protocol = "ICMP"; break;
case 2: e.Protocol = "IGMP"; break;
case 6: e.Protocol = "TCP"; break;
case 17: e.Protocol = "UDP"; break;
default: e.Protocol = "UNKNOWN"; break;
}
//Extract Network Protocal Version
temp_version = (uint)(head->ip_verlen & 0xF0) >> 4;
e.IPVersion = temp_version.ToString();
temp_ip_srcaddr = head->ip_srcaddr;
temp_ip_destaddr = head->ip_destaddr;
temp_ip = new IPAddress(temp_ip_srcaddr);
e.OriginationAddress = temp_ip.ToString();
temp_ip = new IPAddress(temp_ip_destaddr);
e.DestinationAddress = temp_ip.ToString();
temp_srcport = *(short*)&fixed_buf[e.HeaderLength];
temp_dstport = *(short*)&fixed_buf[e.HeaderLength + 2];
e.OriginationPort = IPAddress.NetworkToHostOrder(temp_srcport).ToString();
e.DestinationPort = IPAddress.NetworkToHostOrder(temp_dstport).ToString();
e.PacketLength = (uint)len;
e.MessageLength = (uint)len - e.HeaderLength;
e.ReceiveBuffer = buf;
Array.Copy(buf, 0, e.IPHeaderBuffer, 0, (int)e.HeaderLength);
Array.Copy(buf, (int)e.HeaderLength, e.MessageBuffer, 0, (int)e.MessageLength);
}
OnPacketArrival(e);
}
//Start sniffering
public void Run()
{
IAsyncResult ar = socket.BeginReceive(receive_buf_bytes, 0, len_receive_buf, SocketFlags.None, new AsyncCallback(CallReceive), this);
}
//Async Callback
private void CallReceive(IAsyncResult ar)
{
int received_bytes;
try
{
received_bytes = socket.EndReceive(ar);
}
catch (Exception ex)
{
received_bytes = receive_buf_bytes.Length;
}
Receive(receive_buf_bytes, received_bytes);
if (KeepRunning) Run();
}
//Shutdown Raw Socket
public void Shutdown()
{
if (socket != null)
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
}
}