-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmimidump.c
More file actions
445 lines (400 loc) · 12 KB
/
mimidump.c
File metadata and controls
445 lines (400 loc) · 12 KB
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
#define APP_NAME "Mimidump"
#define APP_DESC "Sniffer for the miminet using libpcap"
#define APP_COPYRIGHT "Copyright (c) 2024 Ilya Zelenechuk"
#define APP_DISCLAIMER "THERE IS ABSOLUTELY NO WARRANTY FOR THIS PROGRAM."
#define APP_AUTHOR "Ilya Zelenchuk, Vladimir Kutuev"
#include <bsd/string.h>
#include <pcap/pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
#include <pthread.h>
#include <signal.h>
#include <sys/poll.h>
#include <sys/timerfd.h>
/* Max number of packet to be captured */
#define MAX_PACKET_CAPTURE 100
/* Max lenght of packet filter string */
#define MAX_FILTER_STRING 512
/* handle settings*/
/* default snap length (maximum bytes per packet to capture) */
#define HANDLE_SNAP_LEN 1518
/* if is non-zero, promiscuous mode will be set */
#define HANDLE_PROMISC 1
/* delay to accumulate packets before being delivered */
#define HANDLE_BUFFER_TIMEOUT 1000
/* timeout to wait interface up in sec */
#define IFUP_TIMEOUT_S 100
/**
* @brief Structure describing captor based on pcap.
* @struct
*/
struct captor
{
pcap_t *handle; /* live capture handle */
pcap_dumper_t *dump; /* pcap dump file */
struct bpf_program bprog; /* compiled packages filter */
};
/**
* @brief Thread info structure. An argument for pthread_create(...).
* @struct
*/
struct thread_info
{
struct captor captor; /* captor */
pthread_t thread_id; /* ID returned from pthread_create() */
int thread_num; /* thread number */
int num_packets; /* max number of packets to be captures */
};
#define NUM_THREADS 2
static const size_t num_threads = NUM_THREADS;
static struct thread_info tinfo[NUM_THREADS];
/*
* print help text
*/
void print_app_usage(void)
{
printf("Usage: %s interface inout_pcap_file out_pcap_file <filter>\n", APP_NAME);
printf("\n");
printf("Options:\n");
printf(" interface Listen on <interface> for packets.\n");
printf(" inout_pcap_file Where to write IN/OUT raw packets.\n");
printf(" out_pcap_file Where to write only OUT raw packets.\n");
printf(" <filter> Tcpdump like filter string.\n");
printf("\n");
}
/* Signal handler */
void sig_handler(int signo)
{
if (signo == SIGINT) {
printf("Got SIGINT. Call pcap_breakloop.\n");
pcap_breakloop(tinfo[0].captor.handle);
pcap_breakloop(tinfo[1].captor.handle);
}
}
static void *thread_handle_packets(void *arg)
{
struct thread_info *local_tinfo = arg;
pcap_loop(local_tinfo->captor.handle, local_tinfo->num_packets, &pcap_dump, (u_char *)local_tinfo->captor.dump);
return 0;
}
/**
* @internal
* @brief Configure created pcap capture @p handle with
* promisc mode, snapshot length and buffer timeout before activation.
* @param[in, out] handle
* @return @p 0 on success and @p PCAP_ERROR_ACTIVATED if @p handle has been activated.
*/
static int configure_pcap_handle(pcap_t *handle)
{
return pcap_set_promisc(handle, HANDLE_PROMISC) || pcap_set_snaplen(handle, HANDLE_SNAP_LEN) ||
pcap_set_timeout(handle, HANDLE_BUFFER_TIMEOUT);
}
/**
* @internal
* @brief Create and bind netlink socket.
* @return socket file descriptor on success and @p -1 on failure.
*/
static int open_netlink_socket(void)
{
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (fd < 0) {
fprintf(stderr, "Cannot open netlink socket\n");
return -1;
}
struct sockaddr_nl sa;
memset(&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK;
sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
sa.nl_pid = getpid();
if (bind(fd, (const struct sockaddr *)&sa, sizeof(sa))) {
fprintf(stderr, "Cannot bind netlink socket\n");
close(fd);
return -1;
}
return fd;
}
/**
* @internal
* @brief Read netlink message from @p fd socket and check interface with index @p ifindex is up.
* @param[in] fd netlink socket
* @param[in] ifindex network interface name
* @return @p -1 on error, positive value if interface become UP and @p 0 else.
*/
static int read_netlink_msg_ifup(int fd, int ifindex)
{
struct nlmsghdr buf[8192 / sizeof(struct nlmsghdr)];
struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) };
struct sockaddr_nl sa;
struct msghdr msg = { .msg_name = &sa,
.msg_namelen = sizeof(sa),
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = NULL,
.msg_controllen = 0,
.msg_flags = 0 };
ssize_t len = recvmsg(fd, &msg, 0);
if (len < 0) {
fprintf(stderr, "Cannot receive netlink message\n");
return -1;
}
if (msg.msg_namelen != sizeof(sa)) {
fprintf(stderr, "Invalid address length in netlink message\n");
return -1;
}
for (struct nlmsghdr *nh = buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) {
if (nh->nlmsg_type == NLMSG_DONE) {
return 0;
}
if (nh->nlmsg_type == NLMSG_ERROR) {
fprintf(stderr, "\n");
return -1;
}
if (nh->nlmsg_type != RTM_NEWLINK) {
continue;
}
struct ifinfomsg *ifinfo = NLMSG_DATA(nh);
if (ifinfo->ifi_index != ifindex) {
continue;
}
return ((int)ifinfo->ifi_flags & IFF_UP);
}
return 0;
}
/**
* @internal
* @brief Wait until interface @p dev become UP or some timeout expired.
* @param[in] dev interface name
* @return @p -1 on error, positive value if interface become UP and @p 0 else.
*/
static int wait_interface_up(const char *dev)
{
printf("Waiting interface %s UP\n", dev);
int ifindex = (int)if_nametoindex(dev);
if (!ifindex) {
fprintf(stderr, "Cannot get interface index by name\n");
return -1;
}
/* Configure netlink socket and timerfd */
int netlinkfd = open_netlink_socket();
if (netlinkfd < 0) {
return -1;
}
int timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
if (timerfd < 0) {
fprintf(stderr, "Cannot open timerfd socket\n");
close(netlinkfd);
return -1;
}
struct itimerspec timerspec = { .it_interval = { 0, 0 }, { IFUP_TIMEOUT_S, 0 } };
timerfd_settime(timerfd, 0, &timerspec, NULL);
/* Poll netlinkfd and timerfd until interface UP or timeout expired */
struct pollfd pfds[] = { { .fd = netlinkfd, .events = POLLIN }, { .fd = timerfd, .events = POLLIN } };
struct pollfd *netlinkpfd = pfds, *timerpfd = pfds + 1;
int ifup = 0;
for (;;) {
int prc = poll(pfds, sizeof(pfds) / sizeof(pfds[0]), IFUP_TIMEOUT_S * 1000);
if (!prc) {
continue;
}
if (prc < 0) {
fprintf(stderr, "Cannot poll netlink socket\n");
ifup = -1;
break;
}
if (timerpfd->revents & POLLIN) {
/* Timeout expired */
timerpfd->revents = 0;
ifup = 0;
break;
}
if (netlinkpfd->revents & POLLIN) {
netlinkpfd->revents = 0;
ifup = read_netlink_msg_ifup(netlinkfd, ifindex);
if (ifup) {
/* Interface UP or error */
break;
}
}
}
close(netlinkfd);
close(timerfd);
return ifup;
}
/**
* @internal
* @brief Create, configure and activate pcap live capture handle for @p dev interface.
* @param[in] dev interface name
* @return Activated handle on success and @p NULL on failure.
*/
static pcap_t *create_pcap_handle_waiting_ifup(const char *dev)
{
char errbuf[PCAP_ERRBUF_SIZE];
int wait = 1;
pcap_t *handle = NULL;
while (!handle) {
handle = pcap_create(dev, errbuf);
if (!handle) {
fprintf(stderr, "%s", errbuf);
return NULL;
}
if (configure_pcap_handle(handle) == PCAP_ERROR_ACTIVATED) {
fprintf(stderr, "Handle for interface %s has been already activated\n", dev);
pcap_close(handle);
return NULL;
}
int rc_activate = pcap_activate(handle);
if (wait && rc_activate == PCAP_ERROR_IFACE_NOT_UP) {
pcap_close(handle);
handle = NULL;
if (!wait_interface_up(dev)) {
fprintf(stderr, "Interface %s is DOWN\n", dev);
return NULL;
}
wait = 0;
continue;
} else if (rc_activate < 0) {
pcap_perror(handle, "Cannot create pcap handle");
pcap_close(handle);
return NULL;
} else if (rc_activate > 0) {
pcap_perror(handle, "Warning");
}
}
return handle;
}
/**
* @internal
* @brief Initialize captor for interface @p dev.
* @param[in, out] cptr pointer to captor
* @param[in] dev name of the interface to be captured
* @param[in] direction direction that packets will be captured
* @param[in] filter_string string for specifying the captor filter program
* @param[in] output_filename filename to save pcap dump
* @return @p 0 on success and nonzero value on failure.
*/
int init_captor(struct captor *cptr,
const char *dev,
pcap_direction_t direction,
const char *filter_string,
const char *output_filename)
{
char *direction_str = direction == PCAP_D_INOUT ? "IN/OUT" : "OUT";
cptr->handle = create_pcap_handle_waiting_ifup(dev);
if (!cptr->handle) {
return 1;
}
printf("Pcap handle for %s captor successfully created\n", direction_str);
pcap_setdirection(cptr->handle, direction);
/* Set filters */
if (pcap_compile(cptr->handle, &cptr->bprog, filter_string, 1, PCAP_NETMASK_UNKNOWN) < 0) {
fprintf(stderr, "Error compiling %s bpf filter on: %s", direction_str, pcap_geterr(cptr->handle));
return 1;
}
if (pcap_setfilter(cptr->handle, &cptr->bprog) < 0) {
fprintf(stderr, "Error installing %s bpf filter on: %s", direction_str, pcap_geterr(cptr->handle));
return 1;
}
/*
* Open dump device for writing packet capture data.
*/
if ((cptr->dump = pcap_dump_open(cptr->handle, output_filename)) == NULL) {
fprintf(stderr, "Error opening savefile \"%s\" for writing: %s\n", output_filename, pcap_geterr(cptr->handle));
return 1;
}
return 0;
}
int main(int argc, char **argv)
{
/* check for capture device name on command-line */
if (argc < 4) {
fprintf(stderr, "Invalid command-line options count\n\n");
print_app_usage();
return EXIT_FAILURE;
}
char dev[IF_NAMESIZE];
if (strlcpy(dev, argv[1], sizeof(dev)) >= sizeof(dev)) {
fprintf(stderr, "Invalid interface name.\n");
return EXIT_FAILURE;
}
char filter_string[MAX_FILTER_STRING];
filter_string[0] = '\0';
/* Read filters */
/* Be sure you have not less 4 arguments */
for (int i = 4; i < argc; i++) {
unsigned int pos = strlen(filter_string);
size_t len = strlen(argv[i]);
/* Do we have a room for another one argument? */
if ((MAX_FILTER_STRING - len - pos) <= 0) {
fprintf(stderr, "Filter string is too long. Must be less than 512 symbols\n");
return EXIT_FAILURE;
}
// Copy a whitespace
if (pos > 0) {
memcpy(&filter_string[pos], " \0", 2);
pos++;
}
memcpy(&filter_string[pos], argv[i], len);
filter_string[pos + len] = '\0';
}
#ifdef PCAP_AVAILABLE_1_10
// Initialize pcap library
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_init(PCAP_CHAR_ENC_LOCAL, errbuf) == PCAP_ERROR) {
fprintf(stderr, "%.*s\n", PCAP_ERRBUF_SIZE, errbuf);
return EXIT_FAILURE;
}
#endif
/* Init pthread attributes */
pthread_attr_t attr;
int thrc = pthread_attr_init(&attr);
if (thrc != 0) {
fprintf(stderr, "pthread_attr_init error\n");
return EXIT_FAILURE;
}
/* Configure threads */
tinfo[0].thread_num = 1;
tinfo[0].num_packets = MAX_PACKET_CAPTURE;
if (init_captor(&tinfo[0].captor, dev, PCAP_D_INOUT, filter_string, argv[2])) {
return EXIT_FAILURE;
}
tinfo[1].thread_num = 2;
if (init_captor(&tinfo[1].captor, dev, PCAP_D_OUT, filter_string, argv[3])) {
return EXIT_FAILURE;
}
tinfo[1].num_packets = MAX_PACKET_CAPTURE;
/* Set SIGINT handler */
if (signal(SIGINT, sig_handler) == SIG_ERR) {
fprintf(stderr, "Can't catch SIGINT\n");
return EXIT_FAILURE;
}
/* Start threads */
for (size_t i = 0; i < num_threads; ++i) {
thrc = pthread_create(&tinfo[i].thread_id, &attr, &thread_handle_packets, &tinfo[i]);
if (thrc != 0) {
fprintf(stderr, "Can't create thread_handle_out_packets\n");
return EXIT_FAILURE;
}
}
/* Now join with each thread, and display its returned value. */
for (size_t i = 0; i < num_threads; ++i) {
void *res;
thrc = pthread_join(tinfo[i].thread_id, &res);
if (thrc != 0) {
fprintf(stderr, "Error while join the thread 1\n");
return EXIT_FAILURE;
}
free(res);
}
for (size_t i = 0; i < num_threads; ++i) {
pcap_dump_close(tinfo[i].captor.dump);
pcap_close(tinfo[i].captor.handle);
}
return EXIT_SUCCESS;
}