-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecap.c
1220 lines (954 loc) · 33.2 KB
/
execap.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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* execap -- Snarf Windows executables off the wire (Driftnet for EXEs)
* Copyright (C) 2010-2011, Brandon Enright <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* $Id$
*/
#include "execap.h"
#include "pavl.h"
int main(int argc, char * const argv[]) {
/* === PCAP vars === */
char * dev = NULL; /* The device to sniff on */
char pc_errbuf[PCAP_ERRBUF_SIZE]; /* Error string */
char base_filter_str[] = "ip and tcp"; /* The constant filter expression */
char filter_str[BPF_FILTER_LEN]; /* The combined filter string */
struct bpf_program filter_prog;/* The compiled filter */
bpf_u_int32 mask;/* Our netmask */
bpf_u_int32 net;/* Our IP */
int pret; /* For holding on to return values */
struct pcap_stat stats;
/* === Signal stuff === */
struct sigaction sa_new, sa_old;
/* === Thread vars === */
pthread_t connection_reaper;
int thread_ret;
/* === Argument parsing vars === */
int arg_val;
int option_index;
/* === Daemonize vars === */
pid_t fork_ret;
char pidtext[128];
int pipefd[2];
char pipebuf = '\0';
int devnullfd;
/* == Scratch vars === */
int i;
int wlen;
/* ===
* Handle command line parameters
* ===
*/
struct option long_options[] = {
{"interface", required_argument, 0, 'i'},
{"logdir", required_argument, 0, 'l'},
{"exedir", required_argument, 0, 'e'},
{"discard-exes", no_argument, 0, 0},
{"verbose", no_argument, 0, 'v'},
{"daemonize", no_argument, 0, 'D'},
{"pidfile", required_argument, 0, 0},
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
/* getopt_long() loop */
while ((arg_val = getopt_long(argc, argv, "i:l:e:DhVv",
long_options, &option_index)) != EOF) {
if (arg_val == 'h') {
printf("execap v%s\n"
"Usage: execap -i interface [OPTIONS] [CAPTURE FILTER]\n\n"
"Options:\n"
"-i, --interface specify interface to capture on\n"
"-l, --logdir save logs to this directory\n"
"-e, --exedir save executables to this directory\n"
"--discard-exes do not store executables on disk\n"
"-v, --verbose turn on verbose output\n"
"-D, --daemonize run as daemon in background\n"
"--pidfile store daemon pid to this file\n"
"-V, --version display version and exit\n"
"-h, --help display this help and exit\n\n"
"execap web page: http://code.google.com/p/execap\n"
"Report bugs to: [email protected] "
"/ http://code.google.com/p/execap/issues\n",
EXECAPVER);
return 0;
}
else if (arg_val == 'V') {
printf("execap v%s\n\n"
"Copyright (C) 2010-2011 Brandon Enright <[email protected]>.\n"
"This is free software; see the source for copying conditions. "
"There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS "
"FOR A PARTICULAR PURPOSE.\n\n", EXECAPVER);
return 0;
}
else if (arg_val == 'v') {
/* fprintf(stderr, "Turning on verbose alerts\n"); */
}
else if (arg_val == 'i') {
dev = strdup(optarg);
}
else if (arg_val == 'l') {
strncpy(logdir, optarg, MAX_PATH_LEN);
logdir[MAX_PATH_LEN - 1] = '\0';
}
else if (arg_val == 'e') {
strncpy(exedir, optarg, MAX_PATH_LEN);
exedir[MAX_PATH_LEN - 1] = '\0';
}
else if (arg_val == 'D') {
daemonize = 1;
}
else if (arg_val == 0) {
/* These are the long options only */
if (strcmp(long_options[option_index].name, "pidfile") == 0) {
strncpy(pidfile, optarg, MAX_PATH_LEN);
pidfile[MAX_PATH_LEN - 1] = '\0';
}
else if (strcmp(long_options[option_index].name, "discard-exes") == 0) {
discard_exes = 1;
}
else {
fprintf(stderr, "Got unknown long option, quitting!\n");
return -1;
}
}
else {
fprintf(stderr, "Got unknown short option, quitting!\n");
return -1;
}
}
/* Check for bpf filter at end of options */
if (optind < argc) {
snprintf(filter_str, BPF_FILTER_LEN, "(%s) and (%s)", base_filter_str,
argv[optind]);
}
else {
snprintf(filter_str, BPF_FILTER_LEN, "%s", base_filter_str);
}
/* Make sure the arguments we need were provided or fill in defaults */
if (dev == NULL) {
fprintf(stderr, "A listening interface must be provided with -i / "
"--interface\n");
return -1;
}
if (logdir[0] == '\0') {
strncpy(logdir, "/var/log/execap", MAX_PATH_LEN);
logdir[MAX_PATH_LEN - 1] = '\0';
}
if (exedir[0] == '\0') {
strncpy(exedir, "/var/log/execap/exes", MAX_PATH_LEN);
exedir[MAX_PATH_LEN - 1] = '\0';
}
if (pidfile[0] == '\0') {
strncpy(pidfile, "/var/run/execap/execap.pid", MAX_PATH_LEN);
pidfile[MAX_PATH_LEN - 1] = '\0';
}
else {
if (daemonize == 0) {
fprintf(stderr, "WARNING: pidfile specified but not in daemon mode!\n");
}
}
/* ===
* Daemonize if we need to
* ===
*/
if (daemonize == 1) {
fprintf(stderr, "Daemonizing...\n");
if (pipe(pipefd) == -1) {
fprintf(stderr, "Failed to create pipe.\n");
return -1;
}
/* === Become two processes === */
fork_ret = fork(); /* fork into background */
if (fork_ret == -1) {
fprintf(stderr, "Forking into the background failed.\n");
return -1;
}
else if (fork_ret != 0) {
/* This is the parent */
/* Close the write end of the pipe */
close(pipefd[1]);
/* Now read from the child */
if (read(pipefd[0], &pipebuf, 1) != 1) {
fprintf(stderr, "Background process died.\n");
return -1;
}
close(pipefd[0]);
fprintf(stderr, "execap successfully started in background.\n");
return 0;
}
else {
/* Break away from the parent */
daemon_pid = setsid();
/* Close the read end of the pipe */
close(pipefd[0]);
/* No idea how this could fail */
if (daemon_pid == -1) {
fprintf(stderr, "Unable to break away from parent.\n");
return -1;
}
/* === Write our PID to a file === */
/* Open up the pid file */
if ((pid_fd = open(pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644)) == -1) {
fprintf(stderr, "PID: Opening of %s for writing failed!\n", pidfile);
return -1;
}
/* Make the pid text ready for write() */
snprintf(pidtext, sizeof(pidtext), "%d\n", daemon_pid);
wlen = write(pid_fd, pidtext, strlen(pidtext));
/* Close the pid file now */
close(pid_fd);
}
}
/* Open the log file handle */
snprintf(log_file, MAX_PATH_LEN,
"%s/log_execap.%d", logdir, (int)time(NULL));
if ((log_fd = open(log_file, O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) {
fprintf(stderr, "LOG: Opening of %s for writing failed!\n", log_file);
return -1;
}
/* Report that we've started up */
fprintf(stderr, "execap v%s started...\n\n", EXECAPVER);
/* Report that we're about to do the PCAP stuff */
fprintf(stderr, "PCAP: Going to listen on interface %s\n", dev);
/* Get the netmask and IP from the device */
if (pcap_lookupnet(dev, &net, &mask, pc_errbuf) != 0) {
fprintf(stderr, "PCAP: Couldn't get netmask for device %s\n", pc_errbuf);
net = 0;
mask = 0;
}
/* Older versions of libpcap don't offer some of the nicer features
* such as setting a buffer size. configure.ac is setup to check
* for these features and set OLDPCAP to 0 or 1. If they aren't
* available we'll have to suffer without them
*/
#if OLDPCAP == 1
if ((pch = pcap_open_live(dev, SNAPLEN, 1, PCAPTIMEOUT, pc_errbuf)) == 0) {
fprintf(stderr, "PCAP: Unable to get pcap handle for %s: %s\n",
dev, pc_errbuf);
return -1;
}
#else
/* Get our pcap handle */
if ((pch = pcap_create(dev, pc_errbuf)) == 0) {
fprintf(stderr, "PCAP: Unable to get pcap handle for %s: %s\n",
dev, pc_errbuf);
return -1;
}
/* Set a large pcap buffer */
if (pcap_set_buffer_size(pch, CAPTUREBUFFER) != 0) {
fprintf(stderr, "PCAP: Unable to set pcap buffer to %u bytes\n",
CAPTUREBUFFER);
}
/* Make sure we're capturing all of the packet */
if (pcap_set_snaplen(pch, SNAPLEN) != 0) {
fprintf(stderr, "PCAP: Unable to set snaplen to %u bytes\n", SNAPLEN);
}
/* Set the timeout */
if (pcap_set_timeout(pch, PCAPTIMEOUT) != 0) {
fprintf(stderr, "PCAP: Unable to set the timeout to %u\n", SNAPLEN);
}
/* Capture in PROMISC mode */
if (pcap_set_promisc(pch, 1) != 0) {
fprintf(stderr, "PCAP: Unable to set PROMISC capture mode\n");
}
/* Activate the pcap handle */
if ((pret = pcap_activate(pch)) != 0) {
fprintf(stderr, "PCAP: Activating the pcap handle failed\n");
if ((pret == PCAP_WARNING) || (pret == PCAP_ERROR)) {
fprintf(stderr, "PCAP: The activation error was %s\n", pcap_geterr(pch));
}
return -1;
}
#endif
/* Make sure the datalink is Ethernet */
if (pcap_datalink(pch) != DLT_EN10MB) {
fprintf(stderr, "PCAP: The datalink is not Ethernet, exiting!\n");
return -1;
}
/* The capture filter must be compiled after the handle is activated */
if (pcap_compile(pch, &filter_prog, (char*)filter_str, 1, mask) != 0) {
fprintf(stderr, "PCAP: Filter compilation failed: %s\n", pcap_geterr(pch));
return -1;
}
/* Now apply the filter to an activated handle */
fprintf(stderr, "PCAP: Setting capture filter to: %s\n", filter_str);
if ((pret = pcap_setfilter(pch, &filter_prog)) != 0) {
fprintf(stderr, "PCAP: Setting the filter failed with %d; err: %s\n",
pret, pcap_geterr(pch));
return -1;
}
/* Before we start capturing packets we need to setup a signal
* handler to be able to break out of pcap_loop() */
memset(&sa_new, 0, sizeof(struct sigaction));
sa_new.sa_handler = sig_stop_pcap;
sigaction(SIGTERM, &sa_new, &sa_old);
memset(&sa_new, 0, sizeof(struct sigaction));
sa_new.sa_handler = sig_stop_pcap;
sigaction(SIGINT, &sa_new, &sa_old);
/* Create the connection trees */
for (i = 0; i < TREES; i++) {
connection_tree[i].tree = pavl_create(compare_connections, NULL, NULL);
pthread_mutex_init(&(connection_tree[i].tree_mutex), NULL);
}
/* Start time */
stats_start = time(NULL);
/* Before listening, start the connection reaper thread */
thread_ret = pthread_create(&connection_reaper, NULL,
thread_connection_reaper, NULL);
/*
* Before we go into the pcap loop, we need to tell the parent
* that we made it.
*/
if (daemonize == 1) {
if (write(pipefd[1], &pipebuf, 1) == -1) {
fprintf(stderr, "Unable to notify parent of success.\n");
}
close(pipefd[1]);
/* == Make stdin, stdout, and stderr all /dev/null === */
if ((devnullfd = open("/dev/null", O_RDWR, 0)) == -1) {
fprintf(stderr, "Unable to open /dev/null\n");
return -1;
}
/* Force std{in,out,err} to /dev/null */
dup2(devnullfd, STDIN_FILENO);
dup2(devnullfd, STDOUT_FILENO);
dup2(devnullfd, STDERR_FILENO);
/* Close devnullfd if we need to */
if (devnullfd > STDERR_FILENO) {
close(devnullfd);
}
}
/* Now start capturing and handling packets */
pcap_loop(pch, -1, packet_callback, NULL);
fprintf(stderr, "\nSignal caught, PCAP loop terminated.\n");
/* There is a chance the pcap_loop() call returned for a reason
* other than a signal. We need to make sure the thread terminates.
*/
terminate = 1;
/* === Stopped listening, wait for the thread to die === */
fprintf(stderr, "Waiting for threads to finish before exiting...\n");
pthread_join(connection_reaper, NULL);
/* End time */
stats_end = time(NULL);
/* Grab the capture statistics */
if (pcap_stats(pch, &stats) != 0) {
fprintf(stderr, "PCAP: Unable to get statistics: %s\n", pcap_geterr(pch));
return -1;
}
fprintf(stderr, "\n-- \n");
fprintf(stderr, "%llu bytes captured (%.02f Mbps average rate)\n",
stats_bytes, (((double)(stats_bytes * 8) /
(double)((stats_end - stats_start) + 1)) /
(double)1000000));
fprintf(stderr, "%llu packets captured\n", stats_packets);
fprintf(stderr, "%u packets received by filter\n", stats.ps_recv);
fprintf(stderr, "%u packets dropped by kernel\n", stats.ps_drop);
/* Now close the pcap handle */
pcap_close(pch);
/* And flush and close our log file */
fsync(log_fd);
close(log_fd);
/* Free some memory */
free(dev);
return 0;
}
void packet_callback(u_char * user, const struct pcap_pkthdr *header,
const u_char *packet) {
struct ip iph;
struct tcphdr tcph;
size_t iphlen;
size_t iplen;
size_t tcphlen;
size_t data_offset;
size_t datalen;
struct connection conn;
struct connection *conn_copy;
struct connection **conn_probe;
struct packet_data cur_packet;
struct packet_data **pp_working_packet;
struct packet_data **pp_last_packet;
struct packet_data **pp_next_packet;
struct packet_data *p_temp_packet;
time_t cur_time = time(NULL);
tcp_seq thisseq;
unsigned int pos = 0;
u_char *temp_data;
size_t overlap;
u_char insert = 1;
u_short tree_num;
u_char handle_case = 0;
/* The EXE search vars */
u_char *next_offset;
u_char *exe_offset;
size_t exe_size;
u_short exe_machine;
u_short exe_subsystem;
u_short exe_characteristics;
u_char newformat;
/* The log and exe saving vars */
u_char exe_md5[33];
struct tm time_detail;
char exe_log[MAX_PATH_LEN];
size_t exe_log_len;
char exe_file[MAX_PATH_LEN];
size_t exe_file_len;
int exe_fd;
struct stat file_stat;
ssize_t write_len;
/* Record some basic stats */
stats_packets++;
stats_bytes += header->caplen;
/*fprintf(stderr, "Got packet\n");*/
/* Now grab out the IP header */
memcpy(&iph, packet + ETH_HDR_SIZE, sizeof(iph));
iphlen = (iph.ip_hl << 2);
/* Do some IP header sanity checks */
if ((iph.ip_v != 4) || (iphlen < MIN_IP_HDR_SIZE)) {
fprintf(stderr, "Got broken IP header (v=%d; hl=%d)\n",
iph.ip_v, iph.ip_hl);
return;
}
/* Make sure this capture wasn't truncated */
iplen = ntohs(iph.ip_len);
if ((iplen + ETH_HDR_SIZE > header->caplen) || (iplen < iphlen)
|| ((iplen - iphlen) < MIN_TCP_HDR_SIZE)) {
/*fprintf(stderr, "Got truncated IP packet (IP size=%u; caplen=%u)\n",
(unsigned int)iplen, (unsigned int)header->caplen);*/
return;
}
/* Now grab out the TCP header */
memcpy(&tcph, packet + ETH_HDR_SIZE + iphlen, sizeof(tcph));
/* Now do some TCP header sanity checks */
tcphlen = (tcph.th_off << 2);
if (tcphlen < MIN_TCP_HDR_SIZE) {
/* fprintf(stderr, "Got broken TCP header (hl=%d)\n", tcphlen); */
return;
}
/* Make sure we have a complete tcp header */
if (iplen < (iphlen + tcphlen)) {
/*fprintf(stderr, "Got truncated TCP header (IP size=%u; caplen=%u)\n",
(unsigned int)iplen, (unsigned int)header->caplen);*/
return;
}
/* We now know the data offset */
data_offset = ETH_HDR_SIZE + iphlen + tcphlen;
/* Sanity check the data offset */
if (data_offset > header->caplen) {
/* fprintf(stderr, "Got incomplete IP + TCP header\n"); */
return;
}
/*
* Now figure out the data length (must use IP length, not caplen because
* Ethernet frames can have padding).
*/
datalen = iplen - (iphlen + tcphlen);
/* Sanity check size of data returned */
if (datalen > SNAPLEN) {
fprintf(stderr,
"Got unexpected packet length (caplen=%u, doff=%u, dlen=%u)\n",
header->caplen, (unsigned int )data_offset,
(unsigned int)datalen);
return;
}
/* Fill out the connection struct */
memcpy(&(conn.ip_src), &(iph.ip_src), sizeof(struct in_addr));
memcpy(&(conn.ip_dst), &(iph.ip_dst), sizeof(struct in_addr));
conn.th_sport = tcph.th_sport;
conn.th_dport = tcph.th_dport;
conn.last_seen = cur_time;
conn.abandon = 0;
conn.total_data = 0;
conn.search_offset = 0;
conn.datalist = NULL;
conn_copy = copy_connection(&conn, NULL);
tree_num = TREEHASH(conn_copy);
/* === *** ACQUIRE TREE LOCK *** === */
pthread_mutex_lock(&(connection_tree[tree_num].tree_mutex));
conn_probe = (struct connection **)pavl_probe(connection_tree[tree_num].tree,
conn_copy);
if (conn_probe == NULL) {
fprintf(stderr, "There was a failure inserting connection into tree.\n");
goto unlock_and_return;
}
if (*conn_probe == conn_copy) {
/* Just inserted, nothing to do */
}
else {
/* Update the last seen time */
(*conn_probe)->last_seen = conn.last_seen;
/* We don't need the conn_copy anymore */
free(conn_copy);
conn_copy = NULL;
}
/* If we have data to insert, do it */
if ((datalen > 0) && ((*conn_probe)->abandon == 0)) {
thisseq = ntohl(tcph.th_seq);
/* Find the packet spot or merge */
pp_last_packet = NULL;
pp_working_packet = &((*conn_probe)->datalist);
pos = 0;
while (*pp_working_packet != NULL) {
/* ===
* It isn't safe to pass sequences that exceed MAX_SEQ_DIST
* to GT_32() and the like. Make sure we don't since the rest
* of the code assumes the GT/LT checks are always correct
* and if they aren't a segfault could occur. See issue 19 for
* more details.
* ===
*/
if (EXCEEDS_DIST(thisseq, (*pp_working_packet)->seq)) {
/* fprintf(stderr, "Sequence exceeds max dist, abandoning\n"); */
abandon_packets((*conn_probe)->datalist);
(*conn_probe)->datalist = NULL;
(*conn_probe)->abandon = 1;
insert = 0;
break;
}
/* ===
* If there are next packets and this one eclipses them then delete
* === */
while (((*pp_working_packet)->next != NULL) &&
(LT_32(thisseq, (((*pp_working_packet)->next)->seq))) &&
(GTE_32((thisseq + datalen) & 0xFFFFFFFF,
(((((*pp_working_packet)->next)->seq) +
((*pp_working_packet)->next)->datalen) &
0xFFFFFFFF)))) {
/* Account for this data removal */
(*conn_probe)->total_data -= ((*pp_working_packet)->next)->datalen;
/* Grab the next pointer before we break the pointer to it */
p_temp_packet = (*pp_working_packet)->next;
/* Now fix our next pointer */
(*pp_working_packet)->next = ((*pp_working_packet)->next)->next;
/* Now free old data */
free(p_temp_packet->data);
/* And free the struct */
free(p_temp_packet);
}
/* ===
* If this packet eclipses the working packet replace it
* === */
if ((LTE_32(thisseq, (*pp_working_packet)->seq)) &&
(GTE_32((thisseq + datalen) & 0xFFFFFFFF,
((*pp_working_packet)->seq + (*pp_working_packet)->datalen) &
0xFFFFFFFF))) {
/* Resize to fit new data */
(*pp_working_packet)->data = realloc((*pp_working_packet)->data,
datalen);
/* Copy in the new data */
memcpy((*pp_working_packet)->data, packet + data_offset, datalen);
/* Update seq */
(*pp_working_packet)->seq = thisseq;
/* Update datalen */
(*pp_working_packet)->datalen = datalen;
/* We changed this packet, reset searched */
(*pp_working_packet)->searched = 0;
insert = 0;
handle_case = 1;
break;
} /* end eclipse */
/* ===
* This packet fits before a previous packet
* === */
if ((LT_32(thisseq, (*pp_working_packet)->seq)) &&
(GTE_32((thisseq + datalen) & 0xFFFFFFFF,
(*pp_working_packet)->seq))) {
/* Find the overlap */
overlap = (((thisseq + datalen) & 0xFFFFFFFF) -
(*pp_working_packet)->seq) & 0xFFFFFFFF;
/* We need to make some space were we can combined the data */
temp_data = malloc(((*pp_working_packet)->datalen + datalen) -
overlap);
/* Now copy in this packet's data */
memcpy(temp_data, packet + data_offset, datalen);
/* Now copy in the existing packet data */
memcpy(temp_data + datalen, (*pp_working_packet)->data + overlap,
(*pp_working_packet)->datalen - overlap);
/* Now get rid of the old data */
free((*pp_working_packet)->data);
/* And set the pointer to the new data */
(*pp_working_packet)->data = temp_data;
/* We need to update the new sequence */
(*pp_working_packet)->seq = thisseq;
/* We need to record this new size */
(*pp_working_packet)->datalen += (datalen - overlap);
/* We changed this packet, reset searched */
(*pp_working_packet)->searched = 0;
/* Track this data */
(*conn_probe)->total_data += (datalen - overlap);
insert = 0;
handle_case = 2;
break;
} /* END starts before */
/* ===
* If this packet starts after a previous packet starts
* === */
if ((GTE_32(thisseq, (*pp_working_packet)->seq)) &&
(LTE_32(thisseq, (((*pp_working_packet)->seq +
(*pp_working_packet)->datalen) & 0xFFFFFFFF)))) {
/* If the packet is contained in the working packet just skip it */
if (LTE_32((thisseq + datalen) & 0xFFFFFFFF,
(((*pp_working_packet)->seq +
(*pp_working_packet)->datalen) & 0xFFFFFFFF))) {
/* fprintf(stderr, "Got a duplicate packet, skipping\n"); */
insert = 0;
break;
}
/* Find the overlap */
overlap = ((((*pp_working_packet)->seq +
(*pp_working_packet)->datalen) & 0xFFFFFFFF) - thisseq) &
0xFFFFFFFF;
/* We need to realloc() the data from the working packet */
(*pp_working_packet)->data = realloc((*pp_working_packet)->data,
(((*pp_working_packet)->datalen +
datalen) - overlap));
/* We need to copy the new data in */
memcpy((*pp_working_packet)->data + (*pp_working_packet)->datalen,
packet + data_offset + overlap, datalen - overlap);
/* We need to record this new size */
(*pp_working_packet)->datalen += (datalen - overlap);
/* Track this data */
(*conn_probe)->total_data += (datalen - overlap);
insert = 0;
handle_case = 3;
break;
} /* end after working packing */
/* ===
* If we aren't far enough along in the linked list
* === */
if (GTE_32(thisseq, (*pp_working_packet)->seq)) {
/* Go on */
pp_last_packet = pp_working_packet;
pp_working_packet = &((*pp_working_packet)->next);
/* We're going on to the next one, track it */
pos++;
}
else {
break;
}
} /* END while pp_working_packet != NULL */
/* If the pos is too big we have too many fragments */
if (pos > MAX_CONN_FRAG) {
abandon_packets((*conn_probe)->datalist);
(*conn_probe)->datalist = NULL;
(*conn_probe)->abandon = 1;
insert = 0;
}
/* We might need to insert a packet instead */
if ((pp_working_packet != NULL) && (insert == 1) &&
((*conn_probe)->abandon == 0)) {
/* We can't combine with anything, insert infront of working packet */
cur_packet.next = *pp_working_packet;
cur_packet.seq = thisseq;
cur_packet.searched = 0;
cur_packet.datalen = datalen;
/* Allocate our packet data space */
if ((cur_packet.data = malloc(datalen)) == 0) {
fprintf(stderr, "malloc() failed allocating space for data!\n");
goto unlock_and_return;
}
/* Copy our data in */
memcpy(cur_packet.data, packet + data_offset, datalen);
/* Allocate our packet_data struct space */
if ((*pp_working_packet = malloc(sizeof(struct packet_data))) == 0) {
fprintf(stderr,
"malloc() failed allocating space for struct packet_data!\n");
}
/* Copy our struct in */
memcpy(*pp_working_packet, &cur_packet, sizeof(struct packet_data));
/* Now fixup the previous packet pointer if we need to */
if ((pp_last_packet != NULL) && (*pp_last_packet != NULL)) {
(*pp_last_packet)->next = *pp_working_packet;
}
/* Track this data */
(*conn_probe)->total_data += datalen;
handle_case = 4;
} /* END insert packet */
/* See if we can merge some packets */
pp_working_packet = &((*conn_probe)->datalist);
/* Find the packets to merge */
while ((*pp_working_packet != NULL) && ((*conn_probe)->abandon == 0)) {
pp_next_packet = &((*pp_working_packet)->next);
while (((*pp_next_packet) != NULL) &&
(GTE_32((((*pp_working_packet)->seq +
(*pp_working_packet)->datalen) & 0xFFFFFFFF),
(*pp_next_packet)->seq))) {
/* Find the overlap */
overlap = ((((*pp_working_packet)->seq +
(*pp_working_packet)->datalen) & 0xFFFFFFFF) -
(*pp_next_packet)->seq) & 0xFFFFFFFF;
/* We need to realloc() the data from the working packet */
(*pp_working_packet)->data = realloc((*pp_working_packet)->data,
(((*pp_working_packet)->datalen +
(*pp_next_packet)->datalen) -
overlap));
/* We need to copy the new data in */
memcpy((*pp_working_packet)->data + (*pp_working_packet)->datalen,
(*pp_next_packet)->data + overlap,
(*pp_next_packet)->datalen - overlap);
/* We need to record this new size */
(*pp_working_packet)->datalen += (*pp_next_packet)->datalen - overlap;
/* Grab the next pointer before we break the pointer to it */
p_temp_packet = *pp_next_packet;
/* Now fix our next pointer */
(*pp_working_packet)->next = (*pp_next_packet)->next;
/* Now free old data */
free(p_temp_packet->data);
/* And free the struct */
free(p_temp_packet);
/* There in a new next packet now */
pp_next_packet = &((*pp_working_packet)->next);
}
/* Go on with the merge checks */
pp_working_packet = &((*pp_working_packet)->next);
} /* end while merge packets */
} /* END if datalen > 0 */
/* If the data wasn't searched reset the search_offset */
if (((*conn_probe)->abandon == 0) &&
((*conn_probe)->datalist != NULL) &&
(((*conn_probe)->datalist)->searched == 0)) {
(*conn_probe)->search_offset = 0;
}
/* Check if we should search for an EXE */
if (((*conn_probe)->abandon == 0) && ((*conn_probe)->total_data >= 2048) &&
((*conn_probe)->datalist != NULL) &&
(((*conn_probe)->datalist)->datalen > (*conn_probe)->search_offset) &&
((((*conn_probe)->datalist)->datalen -
(*conn_probe)->search_offset) >= 2048) &&
(((*conn_probe)->datalist)->next == NULL)) {
exe_offset = NULL;
exe_size = 0;
/* Try to find an EXE */
next_offset = find_exe(((*conn_probe)->datalist)->data +
(*conn_probe)->search_offset,
((*conn_probe)->datalist)->datalen -
(*conn_probe)->search_offset,
&exe_offset, &exe_size, &exe_machine,
&exe_subsystem, &exe_characteristics,
&newformat);
/* Find the new offset */
(*conn_probe)->search_offset = next_offset -
((*conn_probe)->datalist)->data;
/* Did we find an EXE? */
if (exe_size != 0) {
/* Get the detailed time info */
gmtime_r(&cur_time, &time_detail);
/* Compute the MD5*/
md5_hex(exe_offset, exe_size, exe_md5);
exe_log_len = 0;
exe_log_len +=
snprintf(exe_log + exe_log_len, MAX_PATH_LEN - exe_log_len,
"%04d-%02d-%02dT%02d:%02d:%02d (UTC) -- EXE -- %s:%u",
time_detail.tm_year + 1900, time_detail.tm_mon + 1,
time_detail.tm_mday, time_detail.tm_hour, time_detail.tm_min,
time_detail.tm_sec, inet_ntoa((*conn_probe)->ip_src),
ntohs((*conn_probe)->th_sport));
if (exe_log_len >= MAX_PATH_LEN) {
exe_log_len = MAX_PATH_LEN - 1;
}
exe_log_len +=
snprintf(exe_log + exe_log_len, MAX_PATH_LEN - exe_log_len,
" -> %s:%u (Size=%u; Machine=0x%04x; "
"Subsystem=0x%04x; IsDLL=%u; IsPE32+=%u; MD5=%s)\n",
inet_ntoa((*conn_probe)->ip_dst),
ntohs((*conn_probe)->th_dport),
(unsigned int)exe_size,
exe_machine, exe_subsystem,
((exe_characteristics & 0x2000) > 0),
newformat, exe_md5);
if (exe_log_len >= MAX_PATH_LEN) {
exe_log_len = MAX_PATH_LEN - 1;
}
/* Report this entry to the console */
fprintf(stderr, "%s", exe_log);
/* And write and sync it to the log */
write_len = write(log_fd, exe_log, exe_log_len);
fsync(log_fd);
/* Make the EXE filename */
exe_file_len = snprintf(exe_file, MAX_PATH_LEN,
"%s/exe_%s", exedir, exe_md5);
if (exe_file_len >= MAX_PATH_LEN) {
exe_file_len = MAX_PATH_LEN - 1;
}
/* Now we should check if we need to store this executable on
* disk and if so, do it without replacing it if it is already
* there.
*/
if (discard_exes == 0) {
/* Stat that file to check for existence */
if (stat(exe_file, &file_stat) == -1) {
/* This open will fail if the file is already there */
if ((exe_fd = open(exe_file,
O_WRONLY | O_CREAT | O_EXCL, 0644)) != -1) {