-
Notifications
You must be signed in to change notification settings - Fork 0
/
nward.c
291 lines (263 loc) · 6.17 KB
/
nward.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
#include <pcap/pcap.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define NW_SNAPLEN 256
#include "modes.h"
static inline int ltype_to_lhdrlen (int linktype) {
int linkhdrlen;
switch (linktype)
{
case DLT_NULL:
linkhdrlen = 4;
break;
case DLT_EN10MB:
linkhdrlen = 14;
break;
case DLT_LINUX_SLL:
linkhdrlen = 16;
break;
case DLT_SLIP:
case DLT_PPP:
linkhdrlen = 24;
break;
default:
linkhdrlen = -1;
}
return linkhdrlen;
}
static void run_mode(pcap_t *pcap, int pc, int mt, useconds_t to, int warn, int live, struct mode_opt *mode)
{
struct bpf_program fp;
if (pcap_compile(pcap, &fp, mode->filter, 1, PCAP_NETMASK_UNKNOWN)) {
pcap_perror(pcap, "compile");
} else {
int linktype, linkhdrlen;
if ((linktype = pcap_datalink(pcap)) < 0)
{
pcap_perror(pcap, "run_mode");
} else {
// TODO semánticamente aquí no
if ((linkhdrlen = ltype_to_lhdrlen(linktype)) < 0)
fprintf(stderr, "Unsupported datalink (%d)\n", linktype);
else
if (pcap_setfilter(pcap, &fp))
pcap_perror(pcap, "compile");
else {
struct nward_hand_args args = { mode->name, linkhdrlen, mt, to, warn, live };
if (pcap_loop(pcap, pc, mode->callback, (u_char *) &args)) // TODO sólo devuelve 0 si no hay break
pcap_perror(pcap, "compile");
}
}
//pcap_freecode(&fp);
}
}
static pcap_t *init_pcap_file(char *filename, struct mode_opt *mode)
{
pcap_t *pcap = NULL;
char errbuf[PCAP_ERRBUF_SIZE];
pcap = pcap_open_offline(filename, errbuf);
if (pcap == NULL) {
// create fail
fprintf(stderr, "%s\n", errbuf);
}
return pcap;
}
static pcap_t *init_pcap_live(char *devname, struct mode_opt *mode)
{
int warn;
pcap_t *pcap = NULL;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *dev = NULL, *list, *it;
if (!pcap_findalldevs(&list, errbuf)) {
it = list;
if (it == NULL) {
if (devname == NULL)
fprintf(stderr, "no devices found\n");
else
fprintf(stderr, "device %s not found\n", devname);
} else while (it != NULL && dev == NULL) {
if (devname == NULL || !strcmp(devname, it->name))
dev = it;
it = it->next;
}
pcap = pcap_create(devname, errbuf);
if (pcap != NULL) {
pcap_set_buffer_size(pcap, BUFSIZ);
pcap_set_snaplen(pcap, NW_SNAPLEN);
pcap_set_timeout(pcap, 1);
// mode config
if (mode->config != NULL)
mode->config(pcap);
switch (warn = pcap_activate(pcap)) {
case 0: //OK
break;
//TODO añadir otros warns
default:
// activate faiu
pcap_perror(pcap, "init_pcap");
pcap_close(pcap);
pcap = NULL;
}
} else {
// create fail
fprintf(stderr, "%s\n", errbuf);
}
} else {
// findalldevs fail
fprintf(stderr, "%s\n", errbuf);
}
if (list != NULL) pcap_freealldevs(list);
return pcap;
}
static void list_devs ()
{
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *list;
if (!pcap_findalldevs(&list, errbuf)) {
while (list != NULL && list->next != NULL) {
printf("%s, ", list->name);
list = list->next;
}
if (list != NULL) {
printf("%s\n", list->name);
}
pcap_freealldevs(list);
} else {
fprintf(stderr, "%s\n", errbuf);
}
}
static void print_usage()
{
fprintf(stderr, "\
Usage:\n\
nward [options] -mode\n\
\n\
All options and arguments following the mode argument are ignored\n\
\n\
options:\n\
-c N - analyse up to N packets (0 or less for infinity, default: 0)\n\
-d «device»\n\
perform live session capturing traffic from «device».\n\
If not provided, chooses the first found. Incompatible with -f\n\
-f «file»\n\
perform offline session reading the PCAP file «file». Incompatible with -d\n\
-l - list devices available for live session and exit (discards all other options)\n\
-m M - tolerate up to M suspicious events (must be non-negative, default 10)\n\
-t T - a tick will last T milliseconds. Each tick a suspicious event is forgotten\n\
for every source IP address. In offline sessions, ticks are triggered by\n\
the packet reads and timeouts are based on the timestamps of the packets\n\
(must be positive, default 100)\n\
-w - print scan warnings when suspicious\n\
mode:\n\
-A - ward from ACK scans\n\
-F - ward from FIN scans\n\
-N - ward from NULL scans\n\
-S - ward from SYN scans\n\
-T - ward from Connect scans\n\
-U - ward from UDP scans\n\
-X - ward from Xmas scans\n\
Example:\n\
nward -f file.pcap -c 10 -m 10 -t 2000 -F\n\
");
}
// options:
// -D dev
// -sX Xmas
// -c count
// -s "filter"
int main(int argc, char **argv)
{
useconds_t to = 100000;
int err = 0, pc = 0, mt = 10, file = 0, warn = 0, opt;
char *devname = NULL;
pcap_t *pcap;
nward_mode_t mode = NULL;
char optstr[N_MODES+13] = "ld:m:c:t:f:w";
for (int i = 0; i < N_MODES; i++) {
optstr[i+12] = modes[i].opt;
}
while (mode == NULL && (opt = getopt(argc, argv, optstr)) >= 0) {
for (int i = 0; i < N_MODES; i++) {
if (modes[i].opt == opt) {
mode = &modes[i];
}
}
switch (opt) {
case 'w':
warn = 1;
break;
case 'f':
if (devname == NULL) {
devname = strdup(optarg);
file = 1;
} else {
print_usage();
exit(1);
}
break;
case 'd':
if (devname == NULL) {
devname = strdup(optarg);
file = 0;
} else {
print_usage();
exit(1);
}
break;
case 'm':
mt = strtol(optarg, NULL, 10);
if (errno == EINVAL || mt < 0) {
print_usage();
exit(1);
}
break;
case 'c':
pc = strtol(optarg, NULL, 10);
if (errno == EINVAL || pc < -1) {
print_usage();
exit(1);
}
break;
case 't':
to = strtol(optarg, NULL, 10);
if (errno == EINVAL || to <= 0) {
print_usage();
exit(1);
} else {
to *= 1000; // ms to us
}
break;
case 'l':
list_devs();
exit(0);
break;
case '?':
print_usage();
exit(1);
break;
}
}
if (mode != NULL) {
if (file) {
pcap = init_pcap_file(devname, mode);
} else {
pcap = init_pcap_live(devname, mode);
}
if (pcap != NULL) {
run_mode(pcap, pc, mt, to, warn, !file, mode);
pcap_close(pcap);
err = 0;
} else {
err = 1;
}
} else {
print_usage();
}
if (devname != NULL)
free(devname);
return err;
}