forked from Ettercap/ettercap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec_filter.c
1331 lines (1088 loc) · 37.1 KB
/
ec_filter.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
/*
ettercap -- content filtering engine module
Copyright (C) ALoR & NaGA
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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <ec.h>
#include <ec_filter.h>
#include <ec_version.h>
#include <ec_threads.h>
#include <ec_send.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <regex.h>
#ifdef HAVE_PCRE
#include <pcre.h>
#endif
#define JIT_FAULT(x, ...) do { USER_MSG("JIT FILTER FAULT: " x "\n", ## __VA_ARGS__); return -E_FATAL; } while(0)
/* since we need a recursive mutex, we cannot initialize it here statically */
static pthread_mutex_t filters_mutex;
#define FILTERS_LOCK do{ pthread_mutex_lock(&filters_mutex); }while(0)
#define FILTERS_UNLOCK do{ pthread_mutex_unlock(&filters_mutex); }while(0)
/* protos */
static void reconstruct_strings(struct filter_env *fenv, struct filter_header *fh);
static int compile_regex(struct filter_env *fenv);
static int filter_engine(struct filter_op *fop, struct packet_object *po);
static int execute_test(struct filter_op *fop, struct packet_object *po);
static int execute_assign(struct filter_op *fop, struct packet_object *po);
static int execute_incdec(struct filter_op *fop, struct packet_object *po);
static int execute_func(struct filter_op *fop, struct packet_object *po);
static int func_search(struct filter_op *fop, struct packet_object *po);
static int func_regex(struct filter_op *fop, struct packet_object *po);
static int func_pcre(struct filter_op *fop, struct packet_object *po);
static int func_replace(struct filter_op *fop, struct packet_object *po);
static int func_inject(struct filter_op *fop, struct packet_object *po);
static int func_execinject(struct filter_op *fop, struct packet_object *po);
static int func_log(struct filter_op *fop, struct packet_object *po);
static int func_drop(struct packet_object *po);
static int func_kill(struct packet_object *po);
static int func_exec(struct filter_op *fop);
static int cmp_eq(u_int32 a, u_int32 b);
static int cmp_neq(u_int32 a, u_int32 b);
static int cmp_lt(u_int32 a, u_int32 b);
static int cmp_gt(u_int32 a, u_int32 b);
static int cmp_leq(u_int32 a, u_int32 b);
static int cmp_geq(u_int32 a, u_int32 b);
/*******************************************/
/* initialize the filter mutex */
void filter_init_mutex(void) {
pthread_mutexattr_t at;
pthread_mutexattr_init(&at);
/* we want an recursive mutex, so we can re-aquire it in the same thread */
pthread_mutexattr_settype(&at, PTHREAD_MUTEX_RECURSIVE_NP);
pthread_mutex_init(&filters_mutex, &at);
}
/*
* JIT interpreter for binary filters.
* it process the filter_ops and apply the instructions
* on the given packet object
*/
static int filter_engine(struct filter_op *fop, struct packet_object *po)
{
u_int32 eip = 0;
u_int32 flags = 0;
#define FLAG_FALSE 0
#define FLAG_TRUE 1
/* sanity check */
BUG_IF(fop == NULL);
FILTERS_LOCK;
/* loop until EXIT */
while (fop[eip].opcode != FOP_EXIT) {
switch (fop[eip].opcode) {
case FOP_TEST:
if (execute_test(&fop[eip], po) == FLAG_TRUE)
flags |= FLAG_TRUE;
else
flags &= ~(FLAG_TRUE);
break;
case FOP_ASSIGN:
execute_assign(&fop[eip], po);
/* assignment always returns true */
flags |= FLAG_TRUE;
break;
case FOP_INC:
case FOP_DEC:
execute_incdec(&fop[eip], po);
/* inc/dec always return true */
flags |= FLAG_TRUE;
break;
case FOP_FUNC:
if (execute_func(&fop[eip], po) == FLAG_TRUE)
flags |= FLAG_TRUE;
else
flags &= ~(FLAG_TRUE);
break;
case FOP_JMP:
/* jump the the next eip */
eip = fop[eip].op.jmp;
continue;
break;
case FOP_JTRUE:
/* jump the the next eip if the TRUE FLAG is set*/
if (flags & FLAG_TRUE) {
eip = fop[eip].op.jmp;
continue;
}
break;
case FOP_JFALSE:
/* jump the the next eip if the TRUE FLAG is NOT set */
if (!(flags & FLAG_TRUE)) {
eip = fop[eip].op.jmp;
continue;
}
break;
default:
FILTERS_UNLOCK;
JIT_FAULT("unsupported opcode [%d] (execution interrupted)", fop[eip].opcode);
break;
}
/* autoincrement the instruction pointer */
eip++;
}
FILTERS_UNLOCK;
return 0;
}
/*
* pass a packet through every (enabled) filter loaded
*/
void filter_packet(struct packet_object *po) {
struct filter_list **l;
for (l = GBL_FILTERS; *l != NULL; l = &(*l)->next) {
/* if a script drops the packet, do not present it to following scripts */
if ( po->flags & PO_DROPPED )
break;
/* check whether the filter script is enabled */
if ((*l)->enabled)
filter_engine((*l)->env.chain, po);
}
}
/*
* execute a function.
* return FLAG_TRUE if the function was successful
*/
static int execute_func(struct filter_op *fop, struct packet_object *po)
{
switch (fop->op.func.op) {
case FFUNC_SEARCH:
/* search the string */
if (func_search(fop, po) == E_SUCCESS)
return FLAG_TRUE;
break;
case FFUNC_REGEX:
/* search the string with a regex */
if (func_regex(fop, po) == E_SUCCESS)
return FLAG_TRUE;
break;
case FFUNC_PCRE:
/* evaluate a perl regex */
if (func_pcre(fop, po) == E_SUCCESS)
return FLAG_TRUE;
break;
case FFUNC_REPLACE:
/* replace the string */
if (func_replace(fop, po) == E_SUCCESS)
return FLAG_TRUE;
break;
case FFUNC_INJECT:
/* replace the string */
if (func_inject(fop, po) == E_SUCCESS)
return FLAG_TRUE;
break;
case FFUNC_EXECINJECT:
/* replace the string through output of a executable */
if (func_execinject(fop, po) == E_SUCCESS)
return FLAG_TRUE;
break;
case FFUNC_LOG:
/* log the packet */
if (func_log(fop, po) == E_SUCCESS)
return FLAG_TRUE;
break;
case FFUNC_DROP:
/* drop the packet */
func_drop(po);
return FLAG_TRUE;
break;
case FFUNC_KILL:
/* kill the connection */
func_kill(po);
return FLAG_TRUE;
break;
case FFUNC_MSG:
/* display the message to the user */
USER_MSG("%s\n", fop->op.func.string);
return FLAG_TRUE;
break;
case FFUNC_EXEC:
/* execute the command */
if (func_exec(fop) == E_SUCCESS)
return FLAG_TRUE;
break;
default:
JIT_FAULT("unsupported function [%d]", fop->op.func.op);
break;
}
return FLAG_FALSE;
}
/*
* execute a test.
* return FLAG_TRUE if the test was successful
*/
static int execute_test(struct filter_op *fop, struct packet_object *po)
{
/* initialize to the beginning of the packet */
u_char *base = po->L2.header;
int (*cmp_func)(u_int32, u_int32) = &cmp_eq;
/*
* point to the right base.
* if the test is L3.ttl, we have to start from
* L3.header to count for offset.
*/
switch (fop->op.test.level) {
case 2:
base = po->L2.header;
break;
case 3:
base = po->L3.header;
break;
case 4:
base = po->L4.header;
break;
case 5:
base = po->DATA.data;
break;
case 6:
base = po->DATA.disp_data;
break;
default:
JIT_FAULT("unsupported test level [%d]", fop->op.test.level);
break;
}
/* set the pointer to the comparison function */
switch(fop->op.test.op) {
case FTEST_EQ:
cmp_func = &cmp_eq;
break;
case FTEST_NEQ:
cmp_func = &cmp_neq;
break;
case FTEST_LT:
cmp_func = &cmp_lt;
break;
case FTEST_GT:
cmp_func = &cmp_gt;
break;
case FTEST_LEQ:
cmp_func = &cmp_leq;
break;
case FTEST_GEQ:
cmp_func = &cmp_geq;
break;
default:
JIT_FAULT("unsupported test operation");
break;
}
/*
* get the value with the proper size.
* 0 is a special case for strings (even binary)
*/
switch (fop->op.test.size) {
case 0:
/* string comparison */
if (cmp_func(memcmp(base + fop->op.test.offset, fop->op.test.string, fop->op.test.slen), 0) )
return FLAG_TRUE;
break;
case 1:
/* char comparison */
if (cmp_func(*(u_int8 *)(base + fop->op.test.offset), (fop->op.test.value & 0xff)) )
return FLAG_TRUE;
break;
case 2:
/* short int comparison */
if (cmp_func(htons(*(u_int16 *)(base + fop->op.test.offset)), (fop->op.test.value & 0xffff)) )
return FLAG_TRUE;
break;
case 4:
/* int comparison */
if (cmp_func(htonl(*(u_int32 *)(base + fop->op.test.offset)), (fop->op.test.value & 0xffffffff)) )
return FLAG_TRUE;
break;
case 16: /* well IPv6 addresses should be handled as 16-byte pointer */
if (cmp_func(memcmp(base + fop->op.test.offset, fop->op.test.ipaddr, fop->op.test.size), 0) )
return FLAG_TRUE;
break;
default:
JIT_FAULT("unsupported test size [%d]", fop->op.test.size);
break;
}
return FLAG_FALSE;
}
/*
* make an assignment.
*/
static int execute_assign(struct filter_op *fop, struct packet_object *po)
{
/* initialize to the beginning of the packet */
u_char *base = po->L2.header;
/* check the offensiveness */
if (GBL_OPTIONS->unoffensive)
JIT_FAULT("Cannot modify packets in unoffensive mode");
DEBUG_MSG("filter engine: execute_assign: L%d O%d S%d", fop->op.assign.level, fop->op.assign.offset, fop->op.assign.size);
/*
* point to the right base.
*/
switch (fop->op.assign.level) {
case 2:
base = po->L2.header;
break;
case 3:
base = po->L3.header;
break;
case 4:
base = po->L4.header;
break;
case 5:
base = po->DATA.data;
break;
default:
JIT_FAULT("unsupported assignment level [%d]", fop->op.assign.level);
break;
}
/*
* get the value with the proper size.
* 0 is a special case for strings (even binary)
*/
switch (fop->op.assign.size) {
case 0:
memcpy(base + fop->op.assign.offset, fop->op.assign.string, fop->op.assign.slen);
break;
case 1:
*(u_int8 *)(base + fop->op.assign.offset) = (fop->op.assign.value & 0xff);
break;
case 2:
*(u_int16 *)(base + fop->op.assign.offset) = ntohs(fop->op.assign.value & 0xffff);
break;
case 4:
*(u_int32 *)(base + fop->op.assign.offset) = ntohl(fop->op.assign.value & 0xffffffff);
break;
default:
JIT_FAULT("unsupported assign size [%d]", fop->op.assign.size);
break;
}
/* mark the packet as modified */
po->flags |= PO_MODIFIED;
return FLAG_TRUE;
}
/*
* make an increment or decrement.
*/
static int execute_incdec(struct filter_op *fop, struct packet_object *po)
{
/* initialize to the beginning of the packet */
u_char *base = po->L2.header;
/* check the offensiveness */
if (GBL_OPTIONS->unoffensive)
JIT_FAULT("Cannot modify packets in unoffensive mode");
DEBUG_MSG("filter engine: execute_incdec: L%d O%d S%d", fop->op.assign.level, fop->op.assign.offset, fop->op.assign.size);
/*
* point to the right base.
*/
switch (fop->op.assign.level) {
case 2:
base = po->L2.header;
break;
case 3:
base = po->L3.header;
break;
case 4:
base = po->L4.header;
break;
case 5:
base = po->DATA.data;
break;
default:
JIT_FAULT("unsupported inc/dec level [%d]", fop->op.assign.level);
break;
}
/*
* inc/dec the value with the proper size.
*/
switch (fop->op.assign.size) {
case 1:
if (fop->opcode == FOP_INC)
*(u_int8 *)(base + fop->op.assign.offset) += (fop->op.assign.value & 0xff);
else
*(u_int8 *)(base + fop->op.assign.offset) -= (fop->op.assign.value & 0xff);
break;
case 2:
if (fop->opcode == FOP_INC)
*(u_int16 *)(base + fop->op.assign.offset) += ntohs(fop->op.assign.value & 0xffff);
else
*(u_int16 *)(base + fop->op.assign.offset) -= ntohs(fop->op.assign.value & 0xffff);
break;
case 4:
if (fop->opcode == FOP_INC)
*(u_int32 *)(base + fop->op.assign.offset) += ntohl(fop->op.assign.value & 0xffffffff);
else
*(u_int32 *)(base + fop->op.assign.offset) -= ntohl(fop->op.assign.value & 0xffffffff);
break;
default:
JIT_FAULT("unsupported inc/dec size [%d]", fop->op.assign.size);
break;
}
/* mark the packet as modified */
po->flags |= PO_MODIFIED;
return FLAG_TRUE;
}
/*
* search a string and return TRUE if found
*/
static int func_search(struct filter_op *fop, struct packet_object *po)
{
switch (fop->op.func.level) {
case 5:
/* search in the real packet */
if (memmem(po->DATA.data, po->DATA.len, fop->op.func.string, fop->op.func.slen))
return E_SUCCESS;
break;
case 6:
/* search in the decoded/decrypted packet */
if (memmem(po->DATA.disp_data, po->DATA.disp_len, fop->op.func.string, fop->op.func.slen))
return E_SUCCESS;
break;
default:
JIT_FAULT("unsupported search level [%d]", fop->op.func.level);
break;
}
return -E_NOTFOUND;
}
/*
* search a string with a regex and return TRUE if found
*/
static int func_regex(struct filter_op *fop, struct packet_object *po)
{
switch (fop->op.func.level) {
case 5:
/* search in the real packet */
if (regexec(fop->op.func.ropt->regex, (const char*)po->DATA.data, 0, NULL, 0) == 0)
return E_SUCCESS;
break;
case 6:
/* search in the decoded/decrypted packet */
if (regexec(fop->op.func.ropt->regex, (const char*)po->DATA.disp_data, 0, NULL, 0) == 0)
return E_SUCCESS;
break;
default:
JIT_FAULT("unsupported regex level [%d]", fop->op.func.level);
break;
}
return -E_NOTFOUND;
}
/*
* evaluate a perl regex and return TRUE if found
*/
static int func_pcre(struct filter_op *fop, struct packet_object *po)
{
#ifndef HAVE_PCRE
(void) fop;
(void) po;
JIT_FAULT("pcre_regex support not compiled in ettercap");
return -E_NOTFOUND;
#else
int ovec[PCRE_OVEC_SIZE];
int ret;
DEBUG_MSG("filter engine: func_pcre");
memset(&ovec, 0, sizeof(ovec));
switch (fop->op.func.level) {
case 5:
/* search in the real packet */
if ( (ret = pcre_exec(fop->op.func.ropt->pregex, fop->op.func.ropt->preg_extra, po->DATA.data, po->DATA.len, 0, 0, ovec, sizeof(ovec) / sizeof(*ovec))) < 0)
return -E_NOTFOUND;
/* the pcre wants to modify the packet */
if (fop->op.func.replace) {
u_char *replaced;
u_char *q = fop->op.func.replace;
size_t i;
int slen = 0;
/* don't modify if in unoffensive mode */
if (GBL_OPTIONS->unoffensive)
JIT_FAULT("Cannot modify packets in unoffensive mode");
/*
* worst case: the resulting string will need:
* (n * |input|) + |subst| bytes
* where
* |input| is the length of the matched input string (not the regex!)
* |subst| is the length of the substition string
* n is the number of replacement markers in subst
*
* therefore, we need to count the number of $ characters first
* to get an upper limit of the buffer space needed
*/
int markers = 0;
for (i=0; q[i]; i++) {
if (q[i] == '$') markers++;
}
/* now: i = strlen(q) */
SAFE_CALLOC(replaced, markers*(ovec[1]-ovec[0]) + i + 1, sizeof(char));
po->flags |= PO_MODIFIED;
/* make the replacement */
uint8_t escaped = 0;
for (i = 0; i < fop->op.func.rlen; i++) {
/* we encounter an escape character (\), so the next character is to be taken literally */
if (!escaped && q[i] == '\\') {
escaped = 1;
}
/* there is an unescaped position marker */
else if (!escaped && q[i] == '$') {
/* a marker is succeeded by an integer, make sure it is there */
if (q[i+1] == '\0')
JIT_FAULT("Incomplete marker at end of substitution string");
/* so now we can safely move on to the next character, our digit */
i++;
/* we only support up to 9 markers since we only parse a single digit */
if (q[i] < '0' || q[i] > '9')
JIT_FAULT("Incomplete marker without integer in substitution string");
int marker = q[i]-'0';
/* check if the requested marker was found in the pce */
if (marker > ret - 1 || marker == 0)
JIT_FAULT("Too many marker for this pcre expression");
int t = ovec[marker * 2];
int r = ovec[marker * 2 + 1];
/* copy the sub-string in place of the marker */
for ( ; t < r; t++)
replaced[slen++] = po->DATA.data[t];
}
/* anything else either has no special meaning or is escaped,
* so we just copy it
*/
else {
replaced[slen++] = q[i];
escaped = 0;
}
}
/* calculate the delta */
int delta = (ovec[0]-ovec[1])+slen;
/* check if we are overflowing pcap buffer */
BUG_IF(po->DATA.data < po->packet);
BUG_IF((u_int16)(GBL_PCAP->snaplen - (po->DATA.data - po->packet)) <= po->DATA.len+delta);
/* if the substitution string has a different length than the
* matched original string, we have to move around some data
*/
int size_left = po->DATA.len - ovec[0] - slen;
int data_left = po->DATA.len - ovec[1];
DEBUG_MSG("func_pcre: match from %d to %d, substitution length is %d\n", ovec[0], ovec[1], slen);
DEBUG_MSG("func_pcre: packet size changed by %d bytes\n", delta);
if (delta != 0) {
/* copy everything behind the matched string to the new position */
memcpy(po->DATA.data+ovec[0]+slen, po->DATA.data + ovec[1], size_left < data_left ? size_left : data_left);
}
/* copy the modified buffer on the original packet */
memcpy(po->DATA.data+ovec[0], replaced, slen);
po->DATA.delta += delta;
po->DATA.len += delta;
SAFE_FREE(replaced);
}
break;
case 6:
/* search in the decoded one */
if ( pcre_exec(fop->op.func.ropt->pregex, fop->op.func.ropt->preg_extra, po->DATA.disp_data, po->DATA.disp_len, 0, 0, NULL, 0) < 0)
return -E_NOTFOUND;
break;
default:
JIT_FAULT("unsupported pcre_regex level [%d]", fop->op.func.level);
break;
}
return E_SUCCESS;
#endif
}
/*
* replace a string in the packet object DATA.data
*/
static int func_replace(struct filter_op *fop, struct packet_object *po)
{
u_int8 *ptr;
u_int8 *end;
size_t len;
size_t slen = fop->op.func.slen;
size_t rlen = fop->op.func.rlen;
/* check the offensiveness */
if (GBL_OPTIONS->unoffensive)
JIT_FAULT("Cannot modify packets in unoffensive mode");
/* check if it exist at least one */
if (!memmem(po->DATA.data, po->DATA.len, fop->op.func.string, fop->op.func.slen) )
return -E_NOTFOUND;
DEBUG_MSG("filter engine: func_replace");
/*
* XXX BIG WARNING:
* maxlen is GBL_PCAP->snaplen, but we can't
* rely on this forever...
*/
/* take the beginning and the end of the data */
ptr = po->DATA.data;
end = ptr + po->DATA.len;
/* do the replacement */
do {
/* the len of the buffer to be analized */
len = end - ptr;
/* search the string */
ptr = memmem(ptr, len, fop->op.func.string, slen);
/* string no found, exit */
if (ptr == NULL)
break;
/* update the len */
len = end - ptr - slen;
/* set the delta */
po->DATA.delta += rlen - slen;
po->DATA.len += rlen - slen;
/* check if we are overflowing pcap buffer */
BUG_IF(po->DATA.data < po->packet);
BUG_IF((u_int16)(GBL_PCAP->snaplen - (po->DATA.data - po->packet)) <= po->DATA.len);
/* move the buffer to make room for the replacement string */
memmove(ptr + rlen, ptr + slen, len);
/* copy the replacemente string */
memcpy(ptr, fop->op.func.replace, rlen);
/* move the ptr after the replaced string */
ptr += rlen;
/* adjust the new buffer end */
end += rlen - slen;
/* mark the packet as modified */
po->flags |= PO_MODIFIED;
} while(ptr != NULL && ptr < end);
return E_SUCCESS;
}
/*
* inject a file into the communication
*/
static int func_inject(struct filter_op *fop, struct packet_object *po)
{
int fd;
void *file;
size_t size, ret;
/* check the offensiveness */
if (GBL_OPTIONS->unoffensive)
JIT_FAULT("Cannot inject packets in unoffensive mode");
DEBUG_MSG("filter engine: func_inject %s", fop->op.func.string);
/* open the file */
if ((fd = open((const char*)fop->op.func.string, O_RDONLY | O_BINARY)) == -1) {
USER_MSG("filter engine: inject(): File not found (%s)\n", fop->op.func.string);
return -E_FATAL;
}
/* get the size */
size = lseek(fd, 0, SEEK_END);
/* load the file in memory */
SAFE_CALLOC(file, size, sizeof(char));
/* rewind the pointer */
lseek(fd, 0, SEEK_SET);
ret = read(fd, file, size);
close(fd);
if (ret != size)
FATAL_MSG("Cannot read the file into memory");
/* check if we are overflowing pcap buffer */
if(GBL_PCAP->snaplen - (po->L4.header - (po->packet + po->L2.len) + po->L4.len) <= po->DATA.len + (unsigned)size)
JIT_FAULT("injected file too long");
/* copy the file into the buffer */
memcpy(po->DATA.data + po->DATA.len, file, size);
/* Adjust packet len and delta */
po->DATA.delta += size;
po->DATA.len += size;
/* mark the packet as modified */
po->flags |= PO_MODIFIED;
/* unset the flag to be dropped */
if (po->flags & PO_DROPPED)
po->flags ^= PO_DROPPED;
/* close and unmap the file */
SAFE_FREE(file);
return E_SUCCESS;
}
/*
* inject output of a executable into the communication
*/
static int func_execinject(struct filter_op *fop, struct packet_object *po)
{
FILE *pstream = NULL;
unsigned char *output = NULL;
size_t n = 0, offset = 0, size = 128;
unsigned char buf[size];
/* check the offensiveness */
if (GBL_OPTIONS->unoffensive)
JIT_FAULT("Cannot inject packets in unoffensive mode");
DEBUG_MSG("filter engine: func_execinject %s", fop->op.func.string);
/* open the pipe */
if ((pstream = popen((const char*)fop->op.func.string, "r")) == NULL) {
USER_MSG("filter engine: execinject(): Command not found (%s)\n", fop->op.func.string);
return -E_FATAL;
}
while ((n = read(fileno(pstream), buf, size)) != 0) {
if (output == NULL) {
SAFE_CALLOC(output, offset+n, sizeof(unsigned char));
}
else {
SAFE_REALLOC(output, sizeof(unsigned char)*(offset+n));
}
memcpy(output+offset, buf, n);
offset += n;
}
/* close pipe stream */
pclose(pstream);
/* check if we are overflowing pcap buffer */
if(GBL_PCAP->snaplen - (po->L4.header - (po->packet + po->L2.len) + po->L4.len) <= po->DATA.len + (unsigned)offset)
JIT_FAULT("injected output too long");
/* copy the output into the buffer */
memcpy(po->DATA.data + po->DATA.len, output, offset);
/* Adjust packet len and delta */
po->DATA.delta += offset;
po->DATA.len += offset;
/* mark the packet as modified */
po->flags |= PO_MODIFIED;
/* unset the flag to be dropped */
if (po->flags & PO_DROPPED)
po->flags ^= PO_DROPPED;
/* free memory */
SAFE_FREE(output);
return E_SUCCESS;
}
/*
* log the packet to a file
*/
static int func_log(struct filter_op *fop, struct packet_object *po)
{
int fd;
DEBUG_MSG("filter engine: func_log");
/* open the file */
fd = open((const char*)fop->op.func.string, O_CREAT | O_APPEND | O_RDWR | O_BINARY, 0600);
if (fd == -1) {
USER_MSG("filter engine: Cannot open file %s\n", fop->op.func.string);
return -E_FATAL;
}
/* which data should I have to log ? */
switch(fop->op.func.level) {
case 5:
if (write(fd, po->DATA.data, po->DATA.len) < 0)
USER_MSG("filter engine: Cannot write to file...%d\n", errno);
break;
case 6:
if (write(fd, po->DATA.disp_data, po->DATA.disp_len) < 0)
USER_MSG("filter engine: Cannot write to file...\n");
break;
default:
JIT_FAULT("unsupported log level [%d]", fop->op.func.level);
break;
}
/* close the file */
close(fd);
return E_SUCCESS;
}
/*
* drop the packet
*/
static int func_drop(struct packet_object *po)
{
DEBUG_MSG("filter engine: func_drop");
/* se the flag to be dropped */
po->flags |= PO_DROPPED;
/* the delta is all the payload */
po->DATA.delta -= po->DATA.len;
po->DATA.len = 0;
return E_SUCCESS;
}
/*
* kill the connection:
* TCP: reset
* UDP: icmp port unreachable
*/
static int func_kill(struct packet_object *po)
{
DEBUG_MSG("filter engine: func_kill");
if (po->L4.proto == NL_TYPE_TCP) {
/* reset both sides */
/*
* we can trust the ack number.
* at least one side will be reset. the other is automatically reset
*/
send_tcp(&po->L3.src, &po->L3.dst, po->L4.src, po->L4.dst, po->L4.seq, 0, TH_RST, NULL, 0);
send_tcp(&po->L3.dst, &po->L3.src, po->L4.dst, po->L4.src, po->L4.ack, 0, TH_RST, NULL, 0);
} else if (po->L4.proto == NL_TYPE_UDP) {
send_L3_icmp_unreach(po);
}
return E_SUCCESS;
}
/*
* execute the given command
*/
static int func_exec(struct filter_op *fop)
{
pid_t pid;
DEBUG_MSG("filter engine: func_exec: %s", fop->op.func.string);
/*
* the command must be executed by a child.
* we are forwding packets, and we cannot wait
* for the execution of the command
*/
pid = fork();
/* check if the fork was successfull */
if (pid == -1)
SEMIFATAL_ERROR("filter engine: fork() failed, cannot execute %s", fop->op.func.string);
/* differentiate between the parent and the child */
if (!pid) {
int k, param_length;
char **param = NULL;
char *q = (char*)fop->op.func.string;
char *p;
int i = 0;
/* split the string */
for (p = strsep(&q, " "); p != NULL; p = strsep(&q, " ")) {
/* allocate the array */
SAFE_REALLOC(param, (i + 1) * sizeof(char *));
/* copy the tokens in the array */