File: rstd.c

package info (click to toggle)
hunt 1.4-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 472 kB
  • ctags: 951
  • sloc: ansic: 10,168; makefile: 84; sh: 14
file content (395 lines) | stat: -rw-r--r-- 9,545 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
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
/*
 *
 *	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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

/*
 * reset daemon
 */

struct rst_db_item {
	unsigned int src_addr;
	unsigned int src_mask;
	unsigned int dst_addr;
	unsigned int dst_mask;
	unsigned int src_ports[MAX_PORTS + 1];
	unsigned int dst_ports[MAX_PORTS + 1];
	int rst_mode;
	int rst_only_syn;
	struct rst_db_item *next;
};

static struct list l_rst_packet = LIST_INIT(struct packet, p_next[MODULE_RSTD]);

static struct ifunc_item ifunc_tcp;
static pthread_t rstd_thr;
static int rstd_running = 0;
static struct list l_rst_db = LIST_INIT(struct rst_db_item, next);


static inline int packet_match_db_item(struct packet *p, struct rst_db_item *dbi)
{
	struct iphdr *iph = p->p_iph;
	struct tcphdr *tcph = p->p_hdr.p_tcph;
	
	if ((iph->saddr & dbi->src_mask) == (dbi->src_addr & dbi->src_mask) &&
	    (iph->daddr & dbi->dst_mask) == (dbi->dst_addr & dbi->dst_mask) &&
	     port_match(tcph->source, dbi->src_ports) &&
	     port_match(tcph->dest, dbi->dst_ports)) {
		if (!dbi->rst_only_syn) {
			return 1;
		}
		if (tcph->syn) {
			return 1;
		}
	}

	return 0;
}

static struct rst_db_item *packet_match_db(struct packet *p)
{
	struct list_iterator li;
	struct rst_db_item *dbi;
	
	list_lock(&l_rst_db);
	list_iter_set(&li, &l_rst_db);
	while ((dbi = list_iter_get(&li))) {
		if (packet_match_db_item(p, dbi))
			break;
	}
	list_iter_end(&li);
	list_unlock(&l_rst_db);
	return dbi;
}

/*
 * this function is running in the hunt thread
 */
static void func_tcp_packet(struct packet *p, void *arg)
{
	struct rst_db_item *dbi;
	
	if (/*p->p_hdr.p_tcph->syn || */ p->p_hdr.p_tcph->fin)
		return;
	if ((dbi = packet_match_db(p))) {
		packet_want(p);
		p->p_arg[MODULE_RSTD] = (void *) dbi->rst_mode;
		list_produce(&l_rst_packet, p);
	}
}

static void *rst_daemon_thr(void *arg)
{
	struct iphdr *iph;
	struct tcphdr *tcph;
	struct conn_info *ci, __ci;
	struct conn_info *pci;
	struct user_conn_info uci;
	struct packet *p;
	struct timespec ts;
	int rst_mode;

	pthread_sigmask(SIG_BLOCK, &intr_mask, NULL);
	setpriority(PRIO_PROCESS, getpid(), 0);
	while ((p = list_consume(&l_rst_packet, NULL))) {
		iph = p->p_iph;
		tcph = p->p_hdr.p_tcph;
		uci.src_addr = iph->saddr;
		uci.dst_addr = iph->daddr;
		uci.src_port = tcph->source;
		uci.dst_port = tcph->dest;
		if (!(pci = conn_get(&uci))) { /* try to get current seq numbers from connection */
			__ci.src_addr = iph->saddr;
			__ci.dst_addr = iph->daddr;
			__ci.src_port = tcph->source;
			__ci.dst_port = tcph->dest;
			
			__ci.src.next_seq = htonl(ntohl(tcph->seq) + p->p_data_len);
			__ci.src.next_d_seq = tcph->ack ? tcph->ack_seq : 0;
			memcpy(__ci.src.src_mac, p->p_ethh->h_source, ETH_ALEN);
			memcpy(__ci.src.dst_mac, p->p_ethh->h_dest, ETH_ALEN);
			__ci.src.window = tcph->window;	/* ok, wrong */
			__ci.src.id = iph->id;		/* ok, wrong */
			
			__ci.dst.next_seq = __ci.src.next_d_seq;
			__ci.dst.next_d_seq = __ci.src.next_seq;
#if 0			
			memcpy(__ci.dst.src_mac, right_arp_addr(p->p_ethh->h_dest), ETH_ALEN);
			memcpy(__ci.dst.dst_mac, spoofed_arp_addr(p->p_ethh->h_source), ETH_ALEN);
#else
			memcpy(__ci.dst.src_mac, p->p_ethh->h_dest, ETH_ALEN);
			memcpy(__ci.dst.dst_mac, p->p_ethh->h_source, ETH_ALEN);
#endif
			__ci.dst.window = tcph->window;	/* ok, wrong */
			__ci.dst.id = iph->id;		/* ok, wrong */
			
			ci = &__ci;
		} else
			ci = pci;
		packet_free(p);
		rst_mode = (int) p->p_arg[MODULE_RSTD];
		ts.tv_sec = 0;
		ts.tv_nsec = 100000000;
		switch (rst_mode) {
		    case MODE_SRC:
			rst(ci, 4, 0);
			nanosleep(&ts, NULL);
			rst(ci, 4, 0);
			break;
		    case MODE_DST:
			rst(ci, 4, 1);
			nanosleep(&ts, NULL);
			rst(ci, 4, 1);
			break;
		    case MODE_BOTH:
			rst(ci, 4, 0);
			rst(ci, 4, 1);
			nanosleep(&ts, NULL);
			rst(ci, 4, 0);
			rst(ci, 4, 1);
			break;
		}
		if (pci)
			conn_free(pci);
	}
	return NULL;
}

static void rst_daemon_start(void)
{
	if (rstd_running) {
		printf("daemon already running\n");
		return;
	}
	list_produce_start(&l_rst_packet);
	ifunc_tcp.func = func_tcp_packet;
	ifunc_tcp.arg = NULL;
	list_enqueue(&l_ifunc_tcp, &ifunc_tcp);
	
	pthread_create(&rstd_thr, NULL, rst_daemon_thr, NULL);
	rstd_running = 1;
	printf("rst daemon started\n");
}

static void rst_daemon_stop(void)
{
	if (!rstd_running) {
		printf("daemon isn't running\n");
		return;
	}
	list_remove(&l_ifunc_tcp, &ifunc_tcp);
	packet_flush(&l_rst_packet);
	list_produce_done(&l_rst_packet);
	
	pthread_join(rstd_thr, NULL);
	rstd_running = 0;
	printf("rst daemon stoped\n");
}

void print_rst_daemon(void)
{
	if (rstd_running) {
		if (pthread_kill(rstd_thr, 0) != 0) {
			pthread_join(rstd_thr, NULL);
			rstd_thr = (pthread_t) 0;
			rstd_running = 0;
			set_tty_color(COLOR_BRIGHTRED);
			printf("RST daemon failed - bug\n");
			set_tty_color(COLOR_LIGHTGRAY);
		} else
			printf("R");
	}
}

/*
 * 
 * user interface
 *
 */
static void db_item_print(int i, struct rst_db_item *dbi)
{
	char *str_mode;
	char buf_src_ports[BUFSIZE], buf_dst_ports[BUFSIZE];
	char buf[BUFSIZE];
	
	str_mode = sdbmode_to_char(dbi->rst_mode);
	sprintf_db_ports(dbi->src_ports, buf_src_ports, sizeof(buf_src_ports), 1);
	sprintf_db_ports(dbi->dst_ports, buf_dst_ports, sizeof(buf_dst_ports), 1);
	sprintf(buf, "%s/%d [%s]", host_lookup(dbi->src_addr, hl_mode),
		count_mask(dbi->src_mask), buf_src_ports);
	printf("%2d) %-24s --> %s/%d [%s] rst %s %s\n", i,
	       buf,
	       host_lookup(dbi->dst_addr, hl_mode), count_mask(dbi->dst_mask),
	       buf_dst_ports,
	       str_mode,
	       dbi->rst_only_syn ? "SYN only" : "all");
}

static void rst_list_items(void)
{
	struct list_iterator li;
	struct rst_db_item *dbi;
	int i = 0;
	
	list_iter_set(&li, &l_rst_db);
	while ((dbi = list_iter_get(&li))) {
		db_item_print(i++, dbi);
		if (i % lines_o == 0)
			lines_o_press_key();
	}
	list_iter_end(&li);
}


static void rst_add_item(void)
{
	struct rst_db_item *dbi;
	unsigned int src_ip, dst_ip;
	unsigned int src_mask, dst_mask;
	int src_ports[MAX_PORTS + 1], dst_ports[MAX_PORTS + 1];
	int mode, syn_mode;
	int nr;


	if (menu_choose_host_mask_ports_dfl("src ip addr/mask ports", &src_ip,
				&src_mask, src_ports, 0, 0, NULL) < 0)
		return;
	if (menu_choose_host_mask_ports_dfl("dst ip addr/mask ports", &dst_ip,
				&dst_mask, dst_ports, 0, 0, NULL) < 0)
		return;
	if ((mode = menu_choose_sdb("mode", 'b')) == -1)
		return;
	if ((syn_mode = menu_choose_char("reset only syn y/n", "yn", 'y')) == -1)
		return;
	if ((nr = menu_choose_unr("insert at", 0, list_count(&l_rst_db), list_count(&l_rst_db))) == -1)
		return;
	
	dbi = malloc(sizeof(struct rst_db_item));
	memset(dbi, 0, sizeof(struct rst_db_item));
	dbi->src_addr = src_ip;
	dbi->src_mask = src_mask;
	port_htons(src_ports);
	memcpy(dbi->src_ports, src_ports, sizeof(int) * (MAX_PORTS + 1));
	dbi->dst_addr = dst_ip;
	dbi->dst_mask = dst_mask;
	port_htons(dst_ports);
	memcpy(dbi->dst_ports, dst_ports, sizeof(int) * (MAX_PORTS + 1));
	dbi->rst_mode = sdb_to_int(mode);
	switch (syn_mode) {
	    case 'y':
		dbi->rst_only_syn = 1;
		break;
	    case 'n':
		dbi->rst_only_syn = 0;
		break;
	}
	list_insert_at(&l_rst_db, nr, dbi);
}

static void rst_mod_item(void)
{
	struct rst_db_item *dbi;
	unsigned int src_ip, dst_ip;
	unsigned int src_mask, dst_mask;
	int src_ports[MAX_PORTS + 1], dst_ports[MAX_PORTS + 1];
	int mode, syn_mode;
	int nr;
	
	rst_list_items();
	if ((nr = menu_choose_unr("choose item", 0, list_count(&l_rst_db) - 1, list_count(&l_rst_db) - 1)) == -1)
		return;
	if (!(dbi = list_at(&l_rst_db, nr)))
		return;
	if (menu_choose_host_mask_ports_dfl("src ip addr/mask ports",
			    &src_ip, &src_mask, src_ports,
			    dbi->src_addr, dbi->src_mask, dbi->src_ports) < 0)
		return;
	if (menu_choose_host_mask_ports_dfl("dst ip addr/mask ports", 
			    &dst_ip, &dst_mask, dst_ports,
			    dbi->dst_addr, dbi->dst_mask, dbi->dst_ports) < 0)
		return;
	if ((mode = menu_choose_sdb("mode", int_to_sdb(dbi->rst_mode))) == -1)
		return;
	if ((syn_mode = menu_choose_char("reset only syn y/n", "yn", dbi->rst_only_syn ? 'y' : 'n')) == -1)
		return;
	
	port_htons(src_ports);
	port_htons(dst_ports);

	dbi->src_addr = src_ip;
	dbi->src_mask = src_mask;
	memcpy(dbi->src_ports, src_ports, sizeof(int) * (MAX_PORTS + 1));
	dbi->dst_addr = dst_ip;
	dbi->dst_mask = dst_mask;
	memcpy(dbi->dst_ports, dst_ports, sizeof(int) * (MAX_PORTS + 1));
	dbi->rst_mode = sdb_to_int(mode);
	switch (syn_mode) {
	    case 'y':
		dbi->rst_only_syn = 1;
		break;
	    case 'n':
		dbi->rst_only_syn = 0;
		break;
	}
}

static void rst_del_item(void)
{
	int i;
	struct rst_db_item *dbi;
	
	rst_list_items();
	i = menu_choose_unr("item nr. to delete", 0, 
			   list_count(&l_rst_db) - 1, -1);
	if (i >= 0) {
		dbi = list_remove_at(&l_rst_db, i);
		free(dbi);
	}
}

void rstd_menu(void)
{
	char *r_menu =  "s/k)   start/stop daemon\n"
			"l)     list reset database\n"
			"a/m/d) add/mod/del entry\n"
			"x)     return\n";
	char *r_keys = "skladmx";
	int run_it;
	
	run_it = 1;
	while (run_it) {
		switch (menu("reset daemon", r_menu, "rstd", r_keys, 0)) {
		    case 's':
			rst_daemon_start();
			break;
		    case 'k':
			rst_daemon_stop();
			break;
		    case 'l':
			rst_list_items();
			break;
		    case 'a':
			rst_add_item();
			break;
		    case 'd':
			rst_del_item();
			break;
		    case 'm':
			rst_mod_item();
			break;
		    case 'x':
			run_it = 0;
			break;
		}
	}
}