File: cutter.c

package info (click to toggle)
cutter 1.02-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 84 kB
  • ctags: 36
  • sloc: ansic: 410; makefile: 53
file content (548 lines) | stat: -rw-r--r-- 14,394 bytes parent folder | download
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
/*
 * cutter.c	Cut a NATted TCP/IP connection passing through
 * 		an IPTABLES firewall or router.
 *
 * Version	1.02 (experimental)
 * 
 * Date		June 2003
 *
 * Author	Chris Lowth <chris@lowth.com>
 * 
 * Copyright	GNU GENERAL PUBLIC LICENSE Version 2, June 1991
 * 		(see "COPYING" file for details).
 *
 * Docs	& updates
 * 		Can be found at http://www.lowth.com/cutter
 *
 *
 *
 * Help?	This software is free - please consider supporting it's
 * 		development by making your Book, Music or IT purchases
 * 		with Amazon.com or Amazon.co.uk by using the links on
 * 		the www.lowth.com website as your entry to the Amazon
 * 		site. It costs you nothing extra, but Amazon pays the
 * 		author a small commission on any purchases made in this
 * 		manner. Please use one of the following URLs:
 *
 *		  http://www.lowth.com/cutter/uk-shop (UK and europe)
 *		  http://www.lowth.com/cutter/us-shop (America)
 *
 *		If you dont live in the UK or USA, just choose the one
 *		nearest to you.
 *
 * 		I am also seeking input in terms of news of the use of
 * 		this software on new platforms, or ideas for it's
 * 		improvment.
 */

#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <linux/socket.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/if_ether.h>
#include <fcntl.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>     /* the L2 protocols */
#include <linux/sockios.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <errno.h>

#define ETHHDR	 sizeof(struct ethhdr)
#define TCPHDR	 sizeof(struct tcphdr)
#define IPHDR	 sizeof(struct iphdr)
#define PACKETSIZE  TCPHDR + IPHDR

#ifndef EXIT_FAILURE
#define EXIT_FAILURE 2
#define EXIT_WARNING 1
#define EXIT_SUCCESS 0
#endif

typedef unsigned char uchar;
#define TRUE 1
#define FALSE 0

struct rpack{
	struct iphdr ip;
	struct tcphdr tcp;
	char space[8192];
};

struct tpack{
	struct iphdr ip;
	struct tcphdr tcp;
};

struct pseudo_header{		// pseudo header 4 the checksum
	unsigned source_address;
	unsigned dest_address;
	unsigned char placeholder;
	unsigned char protocol;
	unsigned short tcp_length;
	struct tcphdr tcp;
};

struct ifreq ifreq[32];
struct ifconf ifconf;

/****************************************************************************
 * Get the IP address of the next hop device for the specified IP (or
 * 0.0.0.0 if there isnt one - ie we are directly connected). And the local
 * interface that we should use to get there.
 */

int getnexthop(in_addr_t ip, char *intf, in_addr_t *gateway)
{
	FILE *id = fopen( "/proc/net/route", "r" );
	char buff[500];
	char iface[32];
	in_addr_t dest_ip, gateway_ip, mask;
	int flags, refcnt, use, metric, mtu, window, irtt;

	if ( id == NULL ) return FALSE;

	while (fgets(buff, sizeof(buff), id) != NULL) {

		memset(iface, 0, sizeof(iface));
		dest_ip = gateway_ip = flags = refcnt = use
			= metric = mask = mtu = window = irtt = -1;

		if (sscanf(buff, "%s %8lx %8lx %4x %d %d %d %8lx %d %d %d",
			iface, &dest_ip, &gateway_ip, &flags, &refcnt,
			&use, &metric, &mask, &mtu, &window, &irtt
		) == 11) {
			dest_ip = dest_ip;
			gateway_ip = gateway_ip;
			mask = mask;
			if (
				iface[0] != '*' &&		// not a rejected interface
				(flags & 0x0001) &&		// route is UP
				(flags & 0x0200) == 0 &&	// not a "reject"
				(ip & mask) == dest_ip		// IP match
			) {
				strcpy(intf, iface);
				*gateway = gateway_ip;
				fclose(id);
				return TRUE;
			}
		}
	}
	fclose(id);
	return FALSE;
}

/****************************************************************************
 * Get the MAC address (in binary format) for a neighbouring IP
 */

int getmac(in_addr_t ip, uchar *mac)
{
	FILE *id = fopen( "/proc/net/arp", "r" );
	union { uchar c[4]; in_addr_t n; } ipu;
	in_addr_t ipn;
	int mac0, mac1, mac2, mac3, mac4, mac5;
	int hwtype, flags;
	char dev[32], mask[32], arpip[32];
	char buff[200];

	if (id == NULL) return FALSE;
	while (fgets(buff, sizeof(buff), id) != NULL) {
		if (sscanf(buff, "%s 0x%x 0x%x %x:%x:%x:%x:%x:%x %s %s",
			arpip,
			&hwtype, &flags,
			&mac0,&mac1,&mac2,&mac3,&mac4,&mac5,
			mask,dev
		) == 11 ) {
			ipn = inet_addr(arpip);
			if (ipn == ip) {
				mac[0]=mac0; mac[1]=mac1; mac[2]=mac2;
				mac[3]=mac3; mac[4]=mac4; mac[5]=mac5;
				fclose(id);
				return TRUE;
			}
		}
	}
	fclose(id);
	return FALSE;
}

unsigned short in_cksum(unsigned short *ptr,int nbytes){

	register long		sum;		// assumes long == 32 bits
	u_short 		oddbyte;
	register u_short	answer;		// assumes u_short == 16 bits

	sum = 0;
	while (nbytes > 1)  {
		sum += *ptr++;
		nbytes -= 2;
	}

	if (nbytes == 1) {
		oddbyte = 0;				 // make sure top half is zero
		*((u_char *) &oddbyte) = *(u_char *)ptr; // one byte only
		sum += oddbyte;
	}

	sum  = (sum >> 16) + (sum & 0xffff);	// add high-16 to low-16
	sum += (sum >> 16);			// add carry
	answer = ~sum;				// ones-complement, then truncate to 16 bits
	return(answer);
}

int getifconfig()
{
	int rtn;
	int raw_sock;

	if ((raw_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) == -1) {
		perror("open packet socket");
		return FALSE;
	}
	memset(ifreq, 0, sizeof(ifreq));
	ifconf.ifc_len = sizeof(ifreq);
	ifconf.ifc_req = ifreq;

	rtn = ioctl(raw_sock, SIOCGIFCONF, &ifconf);

	close(raw_sock);
	return TRUE;
}

int localip(in_addr_t ip)
{
	int i;
	struct sockaddr_in *sa;

	for (i=0; i<ifconf.ifc_len / sizeof(struct ifreq); i++) {
		sa = (struct sockaddr_in *)&(ifreq[i].ifr_addr);
		if ( sa->sin_addr.s_addr == ip)
			return TRUE;
	}
	return FALSE;
}

int send_rst(
	char *from_ip_str,
	u_short fromport,
	char *to_ip_str,
	u_short toport
)
{
	int i_result, raw_sock, rtn;
	in_addr_t gateway_ip;
	struct sockaddr_ll myaddr, hisaddr;
	struct tpack tpack;
	struct rpack rpack;
	struct pseudo_header pheader;
	struct ifreq ifr;
	int ifindex;
	unsigned char mac[6];
	char interf[80];
	int from_ip = inet_addr(from_ip_str);
	int to_ip = inet_addr(to_ip_str);
	time_t tstart;

	printf( "\tsending RST from %s:%d to %s:%d\n", from_ip_str, fromport, to_ip_str, toport);

	memset( &tpack, 0, sizeof(tpack) );

	// TCP header
	tpack.tcp.source=htons(fromport);		// 16-bit Source port number
	tpack.tcp.dest=htons(toport);			// 16-bit Destination port
	tpack.tcp.seq=0;				// 32-bit Sequence Number */
	tpack.tcp.ack_seq=0;				// 32-bit Acknowledgement Number */
	tpack.tcp.doff=5;				// Data offset */
	tpack.tcp.res1=0;				// reserved */
	tpack.tcp.urg=0;				// Urgent offset valid flag */
	tpack.tcp.ack=0;				// Acknowledgement field valid flag */
	tpack.tcp.psh=0;				// Push flag */
	tpack.tcp.rst=0;				// Reset flag */
	tpack.tcp.syn=0;				// Synchronize sequence numbers flag */
	tpack.tcp.fin=1;				// Finish sending flag */
	tpack.tcp.window=0;				// 16-bit Window size */
	tpack.tcp.check=0;				// space for 16-bit checksum
	tpack.tcp.urg_ptr=0;				// 16-bit urgent offset */

	//  IP header
	tpack.ip.version=4;				// 4-bit Version */
	tpack.ip.ihl=5; 				// 4-bit Header Length */
	tpack.ip.tos=0x10;				// 8-bit Type of service */
	tpack.ip.tot_len=htons(IPHDR+TCPHDR);		// 16-bit Total length */
	tpack.ip.id=0;					// 16-bit ID field */
	tpack.ip.frag_off=htons(0x4000);		// 13-bit Fragment offset */
	tpack.ip.ttl=0xff;				// 8-bit Time To Live */
	tpack.ip.protocol=IPPROTO_TCP;			// 8-bit Protocol */
	tpack.ip.check=0;				// space for 16-bit Header checksum
	tpack.ip.saddr = from_ip;
	tpack.ip.daddr = to_ip;

	// IP header checksum
	tpack.ip.check=in_cksum((unsigned short *)&tpack.ip,IPHDR);

	// TCP header checksum
	pheader.source_address=(unsigned)tpack.ip.saddr;
	pheader.dest_address=(unsigned)tpack.ip.daddr;
	pheader.placeholder=0;
	pheader.protocol=IPPROTO_TCP;
	pheader.tcp_length=htons(TCPHDR);
	bcopy((char *)&tpack.tcp,(char *)&pheader.tcp,TCPHDR);
	tpack.tcp.check=in_cksum((unsigned short *)&pheader,TCPHDR+12);

	/*
	 * Open a PACKET (layer 2) socket.
	 */

	if ((raw_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) == -1) {
		perror("open packet socket");
		return FALSE;
	}

	if (!getnexthop(to_ip, interf, &gateway_ip)) {
		fprintf(stderr, "Cant find next hop gateway in routing table\n");
		close(raw_sock);
		return FALSE;
	}

	/*
	 * Determine the ifindex of the interface we are going to use with
	 * our layer 2 comms.
	 */

	memset(&ifr, 0, sizeof(ifr));
	strncpy(ifr.ifr_name, interf, sizeof(ifr.ifr_name));
	if (ioctl(raw_sock, SIOCGIFINDEX, &ifr) == -1) {
		perror("ioctl - get ifindex");
		close(raw_sock);
		return FALSE;
	}
	ifindex = ifr.ifr_ifindex;

	if (!getmac( gateway_ip == 0 ? to_ip : gateway_ip, mac) ) {
		memset(&ifr, 0, sizeof(ifr));
		strncpy(ifr.ifr_name, interf, sizeof(ifr.ifr_name));
		if (ioctl(raw_sock, SIOCGIFHWADDR, &ifr) == -1) {
			perror("ioctl - get hwaddr");
			close(raw_sock);
			return FALSE;
		}
		memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
	}

	/*
	 * Bind to the interface. We can use a null MAC address because the
	 * system will fill it in for us.
	 */

	memset(&myaddr, 0, sizeof(myaddr));
	myaddr.sll_family = AF_PACKET;
	myaddr.sll_ifindex = ifindex;
	if(bind(raw_sock, (struct sockaddr*)(&myaddr), sizeof(myaddr)) != 0) {
		perror("bind");
		close(raw_sock);
		return FALSE;
	}

	memset(&hisaddr, 0, sizeof(hisaddr));
	hisaddr.sll_family = AF_PACKET;
	hisaddr.sll_protocol = htons(ETH_P_IP);
	hisaddr.sll_ifindex = ifindex;
	hisaddr.sll_halen = 6;
	memcpy(hisaddr.sll_addr, mac, 6);

	/* Send our invalid FIN packet */

	i_result = sendto(raw_sock,&tpack,PACKETSIZE,0,(void*)&hisaddr,sizeof(hisaddr));
	if (i_result != PACKETSIZE) {
		perror("sendto - fin packet");
		close(raw_sock);
		return FALSE;
	}

	/* Wait for the ACK we expect back from the peer, and send an RST with
	 * the sequence numbers gleaned from the ACK */

	tstart = time(0);

	for ( ; time(0) < tstart + 15; ) {	// give the peer 15 seconds to respond
		struct sockaddr_ll gotaddr;
		int addrlen = sizeof(gotaddr);
		fd_set readfds;
		struct timeval tv;

		FD_ZERO(&readfds);
		FD_SET(raw_sock, &readfds);

		tv.tv_sec = 1;
		tv.tv_usec = 0;

		if (select(raw_sock + 1, &readfds, NULL, NULL, &tv) == 0)
			continue;

		memcpy(&gotaddr, &hisaddr, addrlen);
		memset(&rpack, 0, sizeof(rpack));
		i_result = recvfrom(raw_sock, &rpack, sizeof(rpack), 0, (void*)&gotaddr, &addrlen);
		if (i_result < 16) {
			perror( "recvfrom - waiting for SYN" );
			close(raw_sock);
			return FALSE;
		}
		if (
			rpack.ip.version == 4 &&
			rpack.ip.ihl == 5 &&
			rpack.ip.protocol == IPPROTO_TCP &&
			rpack.ip.saddr == tpack.ip.daddr &&
			rpack.ip.daddr == tpack.ip.saddr &&
			rpack.tcp.source == tpack.tcp.dest &&
			rpack.tcp.dest == tpack.tcp.source &&
			rpack.tcp.ack == 1
		) {
			tpack.tcp.seq = rpack.tcp.ack_seq;
			tpack.tcp.ack_seq = 0;
			tpack.tcp.fin = 0;
			tpack.tcp.rst = 1;
			tpack.tcp.check = 0;

			pheader.source_address=(unsigned)tpack.ip.saddr;
			pheader.dest_address=(unsigned)tpack.ip.daddr;
			pheader.placeholder=0;
			pheader.protocol=IPPROTO_TCP;
			pheader.tcp_length=htons(TCPHDR);
			bcopy((char *)&tpack.tcp,(char *)&pheader.tcp,TCPHDR);
			tpack.tcp.check=in_cksum((unsigned short *)&pheader,TCPHDR+12);

			i_result = sendto(raw_sock,&tpack,PACKETSIZE,0,(void*)&hisaddr,sizeof(hisaddr));
			if (i_result != PACKETSIZE) {
				perror("sendto - reset packet");
				close(raw_sock);
				return FALSE;
			}
			close(raw_sock);
			return TRUE;
		}
	}
	printf("\t\tACK not seen so RST not sent (sorry!)\n");
	close(raw_sock);

	return FALSE;
}

int match(in_addr_t match_ip, int match_port, in_addr_t found_ip, int found_port)
{
	if (
		(match_ip == 0 || match_ip == found_ip) &&
		(match_port == 0 || match_port == found_port)
	)
		return TRUE;
	else
		return FALSE;
}

int scan_conntrack(in_addr_t ip1, int port1, in_addr_t ip2, int port2)
{
	char *junk[] = { "[UNREPLIED]", "[ASSURED]", NULL };
	FILE *id = fopen( "/proc/net/ip_conntrack", "r" );
	char src1[32], dst1[32], src2[32], dst2[32];
	int sport1, dport1, sport2, dport2, i, n, life, use;
	in_addr_t src1n, src2n, dst1n, dst2n;
	char buff[1024], *p;
	int found = 0;
	int ok = TRUE;

	if (id == NULL) {
		perror( "openning /proc/net/ip_conntrack" );
		return FALSE;
	}

	while (fgets(buff, sizeof(buff), id) != NULL) {
		for (i=0; junk[i]; i++)
			while ((p = strstr(buff, junk[i])))
				strcpy(p, p+strlen(junk[i]));

		if (sscanf(buff,
			"tcp %d %d ESTABLISHED "
			"src=%s dst=%s sport=%d dport=%d "
			"src=%s dst=%s sport=%d dport=%d "
			"use=%d",
			&n, &life,
			src1, dst1, &sport1, &dport1,
			src2, dst2, &sport2, &dport2,
			&use
		) != 11)
			continue;

		src1n = inet_addr(src1);
		src2n = inet_addr(src2);
		dst1n = inet_addr(dst1);
		dst2n = inet_addr(dst2);

		if (
			(match(ip1,port1,src2n,sport2) && match(ip2,port2,dst2n,dport2)) ||
			(match(ip1,port1,dst2n,dport2) && match(ip2,port2,src2n,sport2)) ||
			(match(ip1,port1,src1n,sport1) && match(ip2,port2,dst1n,dport1)) ||
			(match(ip1,port1,dst1n,dport1) && match(ip2,port2,src1n,sport1))
		) {
			/*
			 * local network to public network - forwarded connection
			 */

			if (!localip(src1n) && !localip(dst1n) && !localip(src2n) && localip(dst2n)) {
				found ++;
				printf("For connection %s:%d -> %s:%d\n", src1, sport1, dst1, dport1);
				ok = send_rst(dst1,dport1,src1,sport1) && ok;
				ok = send_rst(dst2,dport2,src2,sport2) && ok ;
			}

			/* Inbound connection forwarded to private network device */

			else if (!localip(src1n) && localip(dst1n) && !localip(src2n) && !localip(dst2n)) {
				found ++;
				printf("For connection %s:%d -> %s:%d\n", dst2, dport2, src2, sport2);
				ok = send_rst(dst1,dport1,src1,sport1) && ok;
				ok = send_rst(dst2,dport2,src2,sport2) && ok;
			}
		}
	}

	if (found == 0) {
		fprintf(stderr, "No matching connections found\n");
		return FALSE;
	}

	return ok;
}

int main(int argc, char *argv[])
{
	in_addr_t ip1 = argc > 1 ? inet_addr(argv[1]) : 0;
	int port1     = argc > 2 ? atoi(argv[2])      : 0;
	in_addr_t ip2 = argc > 3 ? inet_addr(argv[3]) : 0;
	int port2    =  argc > 4 ? atoi(argv[4])      : 0;

	if (argc < 2) {
		fprintf(stderr, "usage: cutter ip [ port [ ip [ port ] ] ]\n");
		exit(EXIT_FAILURE);
	}

	if (ip1 == -1 || ip2 == -1) {
		fprintf(stderr, "Invalid IP address\n");
		exit(EXIT_FAILURE);
	}

	getifconfig();
	if (scan_conntrack(ip1, port1, ip2, port2))
		return EXIT_SUCCESS;
	else
		return EXIT_FAILURE;
}