-
Notifications
You must be signed in to change notification settings - Fork 0
/
macnat.c
422 lines (340 loc) · 11.1 KB
/
macnat.c
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
/*
* A simple tool to re-write the MAC address of packets that go through this
* daemon. Maintains a dictionary that contains a mapping of the source ip
* address of a packet to a randomize mac address and rewrites the source
* mac address of the packet for outgoing (egress?) packets. Does the reverse
* for incoming (ingress?) packets.
*
* EXAMPLE:
* Egress (all source):
* <00:30:1b:b9:97:d7>/192.168.10.3 --> <00:00:5e:00:04:44>/192.168.10.3
* <00:30:1b:b9:97:d7>/192.168.10.4 --> <00:00:5e:00:01:44>/192.168.10.4
* <00:30:1b:b9:97:d7>/192.168.10.5 --> <00:00:5e:00:01:44>/192.168.10.4
*
* Ingress (all destination):
* <00:00:5e:00:04:44>/192.168.10.3 --> <00:30:1b:b9:97:d7>/192.168.10.3
* <00:00:5e:00:01:44>/192.168.10.4 --> <00:30:1b:b9:97:d7>/192.168.10.4
* <00:00:5e:00:01:44>/192.168.10.4 --> <00:30:1b:b9:97:d7>/192.168.10.5
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <event2/event.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netpacket/packet.h>
#include <sys/socket.h>
#include "macnat.h"
#define PROTO_TO_USE ETH_P_ALL
#define BIG 65535
char * random_chars(char *dst, int size);
void print_mac_address(unsigned char *addr);
int client_sock;
int server_sock;
/*
* Structure to hold mac address that we will use in our lookup table;
*/
struct ip_mac_map {
struct ether_addr *mac;
#if 0
/* or */
char mac[18];
#endif
};
/*
* Our mapping 'table'. The last 3 (max) digits in an IP address
* will be used to determine the key where our mac address resides.
*/
struct ip_mac_map mac_map[BIG];
char * random_chars(char *dst, int size)
{
static const char allowable_chars[] = "1234567890abcdef";
static const len = 2;
int i, r;
for (i=0; i<len; ++i)
{
r = (int)((double)rand() / ((double)RAND_MAX + 1) * (sizeof(allowable_chars) -1 ));
dst[i] = allowable_chars[r];
}
dst[i] = '\0';
return dst;
}
void print_mac_address(unsigned char *addr)
{
int i;
printf("%02x", addr[0]);
for (i=1; i<ETH_ALEN; ++i)
printf(":%02x", addr[i]);
}
int extract_significant_key_from_ip(struct in_addr ip_addr, unsigned char *ip_packet)
{
int key = 1;
char * ip = inet_ntoa(ip_addr);
printf("IP: %s\n", ip);
/* Super hack */
char *num, *prev;
num = strtok(ip, ".");
while (num != NULL) {
prev = num;
num = strtok(NULL, ".");
if (!num)
break;
}
key = atoi(prev);
return key; /* Doh! */
}
void get_spoofed_mac_address(void *ip_packet, char *spoofed_mac_address, int protocol)
{
const char pattern[] = "00:00:5e";
char dst1[3], dst2[3], dst3[3];
char spoofed_mac_addr[18];
int key;
struct ether_addr mac;
struct in_addr ip_addr;
struct iphdr *ip = (struct iphdr *)ip_packet;
ip_addr.s_addr = ip->saddr;
printf("Extracting ip info\n");
if (protocol == 0x806)
key = 512;
else if (protocol == 0x800)
key = extract_significant_key_from_ip(ip_addr, (unsigned char *)ip_packet);
else
key == 512;
printf("Key: %d\n", key);
if (mac_map[key].mac != NULL) {
char * temp = ether_ntoa(mac_map[key].mac);
printf("Found key %s\n", temp);
strncpy(spoofed_mac_address, temp, strlen(temp));
}
else {
/*
* We cannot find a mac address in our table generate
* a new one add it to our table/dictionary and return
* the generated mac.
*/
sprintf(spoofed_mac_addr, "%s:%s:%s:%s", pattern,
random_chars(dst1, sizeof(dst1)),
random_chars(dst2, sizeof(dst2)),
random_chars(dst3, sizeof(dst3)));
printf("Generated spoofed first time %s\n", spoofed_mac_addr);
mac_map[key].mac = (struct ether_addr *)malloc(sizeof(struct ether_addr *));
memcpy(mac_map[key].mac, ether_aton(spoofed_mac_addr), sizeof(struct ether_addr *));
printf("Added to table\n");
strncpy(spoofed_mac_address, spoofed_mac_addr, strlen(spoofed_mac_addr));
printf("Copied\n");
}
}
int macnat_create_and_bind_socket(int ifindex, struct sockaddr_ll sdl)
{
struct packet_mreq mreq;
int sock;
int protocol = htons(PROTO_TO_USE);
if ((sock = socket(PF_PACKET, SOCK_RAW, protocol)) == -1) {
perror("socket");
return -1;
}
/* Make the interface promisuous */
mreq.mr_ifindex = ifindex;
mreq.mr_type = PACKET_MR_PROMISC;
if (setsockopt(sock, SOL_SOCKET, PACKET_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) == -1) {
perror("setsockopt");
return -1;
}
#if 0
/* TODO: Nice value for buf. */
if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF , NULL, 0) == -1) {
perror("setsockopt");
exit(1);
}
#endif
if (bind(sock, (struct sockaddr *)&sdl, sizeof(sdl)) == -1) {
perror("bind");
return -1;
}
return sock;
}
void initialize_map()
{
int i;
for (i=0; i<BIG; ++i)
mac_map[i].mac = NULL;
}
void initialize_server_socket(const char *ifname)
{
struct sockaddr_ll sdl;
int ifindex, protocol;
ifindex = if_nametoindex(ifname);
protocol = htons(PROTO_TO_USE);
sdl.sll_family = AF_PACKET;
sdl.sll_halen = ETH_ALEN;
memcpy(sdl.sll_addr, (ether_aton("00:0c:29:c6:37:13"))->ether_addr_octet, ETHER_ADDR_LEN);
sdl.sll_ifindex = ifindex;
sdl.sll_protocol = protocol; /* Not required for sending */
sdl.sll_hatype = ARPHRD_ETHER; /* Not required for sending */
server_sock = macnat_create_and_bind_socket(ifindex, sdl);
if (server_sock == -1) {
printf("Error when creating server socket..\n");
exit(1);
}
}
void initialize_client_socket(const char *ifname)
{
struct sockaddr_ll sdl;
int ifindex, protocol;
ifindex = if_nametoindex(ifname);
protocol = htons(PROTO_TO_USE);
sdl.sll_family = AF_PACKET;
sdl.sll_protocol = protocol;
sdl.sll_ifindex = ifindex;
sdl.sll_hatype = 0; /* Will be filled for us */
sdl.sll_pkttype = 0; /* Will be filled for us */
/* sdl.sll_halen and sdl.sll_addr not required for receiveing */
client_sock = macnat_create_and_bind_socket(ifindex, sdl);
if (client_sock == -1) {
printf("Error when creating client socket..\n");
exit(1);
}
}
void cleanup_on_exit()
{
close(server_sock);
close(client_sock);
}
void replace_with_original_and_send_packet(void *packet, int framelen)
{
/* We can make a blind replace here */
char original_mac_addr[] = "00:0b:5d:8d:6f:c7";
struct ethhdr *hdr = (struct ethhdr *)packet;
int protocol = ntohs(hdr->h_proto);
if (protocol < 0x05DC) {
//printf("INFO: Skipping unknown proto %d %x\n", protocol, hdr->h_proto);
return;
}
print_mac_address(hdr->h_source);
printf(" --> ");
print_mac_address(hdr->h_dest);
printf("\n");
printf("Replacing with original mac %s and sending packet\n", original_mac_addr);
/* Construct ethernet header */
memcpy(hdr->h_dest, (ether_aton(original_mac_addr))->ether_addr_octet, ETH_ALEN);
if (send(client_sock, packet, framelen, MSG_CONFIRM) == -1) {
perror("send");
exit(1);
}
printf("Sending Data:\n");
print_mac_address(hdr->h_source);
printf(" --> ");
print_mac_address(hdr->h_dest);
printf("\n");
}
void modify_and_send_packet(void *packet, int framelen)
{
char spoofed_mac_addr[18];
struct ethhdr *hdr = (struct ethhdr *)packet;
int protocol = ntohs(hdr->h_proto);
if (protocol < 0x05DC) {
//printf("INFO: Skipping unknown proto %d %x\n", protocol, hdr->h_proto);
return;
}
print_mac_address(hdr->h_source);
printf(" --> ");
print_mac_address(hdr->h_dest);
printf("\n");
printf("Getting spoofed mac\n");
get_spoofed_mac_address((packet + sizeof(struct ethhdr)), spoofed_mac_addr, protocol);
printf("Modifying and sending packet using MAC: %s\n", spoofed_mac_addr);
/* Replace ethernet header */
memcpy(hdr->h_source, (ether_aton(spoofed_mac_addr))->ether_addr_octet, ETH_ALEN);
if (send(server_sock, packet, framelen, MSG_CONFIRM) == -1) {
perror("send");
exit(1);
}
printf("Sending Data:\n");
print_mac_address(hdr->h_source);
printf(" --> ");
print_mac_address(hdr->h_dest);
printf("\n");
}
void read_callback_from_client(evutil_socket_t sock, short what, void *arg)
{
unsigned char buffer[ETH_FRAME_LEN];
struct ethhdr *hdr;
int rv;
rv = read((int)sock, buffer, ETH_FRAME_LEN);
if (rv == -1) {
perror("read");
exit(1);
}
modify_and_send_packet(buffer, rv);
}
void read_callback_from_server(evutil_socket_t sock, short what, void *arg)
{
unsigned char buffer[ETH_FRAME_LEN];
struct ethhdr *hdr;
int rv;
rv = read((int)sock, buffer, ETH_FRAME_LEN);
if (rv == -1) {
perror("read");
exit(1);
}
replace_with_original_and_send_packet(buffer, rv);
}
void usage()
{
printf("Usage: macnat <xx:yy:zz>\n"
"where:\n"
" xx:yy:zz will be used to generate mac addresses like so:"
" xx:yy:zz:00:01:ab\n"
" xx:yy:zz:01:0f:12\n");
}
int main(int argc, char *argv[])
{
const char server_facing_ifname[] = "eth0";
const char client_facing_ifname[] = "eth1";
struct event_config *cfg;
struct event_base *base;
struct event *client_facing_read, *server_facing_read;
srand(time(NULL));
cfg = event_config_new();
event_config_avoid_method(cfg, "select");
base = event_base_new_with_config(cfg);
event_config_free(cfg);
if (!base) {
printf("ERROR: could not initialize base event\n");
exit(1);
}
initialize_map();
/*
* We use 2 sockets because we will (probably) receive packets via one
* interface and send via another interface.
*/
initialize_server_socket(server_facing_ifname);
initialize_client_socket(client_facing_ifname);
/*
* We not have 2 sockets, one server-facing and one client-facing.
* Register for appropriate events. For now because of the way the
* sockets are configured, they are unidirectional. This will change
* soon.
* XXX Do not register for write callback, receive_packet will send it out...
* FIXME Ugly duplicated code for distinguising between client facing and
* server facing interfaces... Ughhh... I am ashamed... :(
*/
client_facing_read = event_new(base, client_sock, EV_TIMEOUT|EV_READ|EV_PERSIST,
read_callback_from_client, (char *)"Client facing reading event");
server_facing_read = event_new(base, server_sock, EV_TIMEOUT|EV_READ|EV_PERSIST,
read_callback_from_server, (char *)"Server facing reading event");
/* Add event and wait forever to event to happen */
event_add(client_facing_read, NULL);
event_add(server_facing_read, NULL);
atexit(cleanup_on_exit);
/* Enter event loop */
event_base_dispatch(base);
return 0;
}