-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbase_common.php
1314 lines (1205 loc) · 39.2 KB
/
base_common.php
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
<?php
// Basic Analysis and Security Engine (BASE)
// Copyright (C) 2019-2023 Nathan Gibbs
// Copyright (C) 2004 BASE Project Team
// Copyright (C) 2000 Carnegie Mellon University
//
// For license info: See the file 'base_main.php'
//
// Project Lead: Nathan Gibbs
// Built upon work by: Kevin Johnson & the BASE Project Team
// Roman Danyliw <[email protected]>, <[email protected]>
//
// Purpose: Common Functions
//
// Author(s): Nathan Gibbs
// Kevin Johnson
function GetSensorIDs( $db ){
$result = $db->baseExecute("SELECT sid FROM sensor;");
while( $myrow = $result->baseFetchRow() ){
$sensor_ids[] = $myrow[0];
}
$result->baseFreeRows();
return $sensor_ids;
}
function GetSensorName( $sid, $db ){
$name = '';
$temp_sql = "SELECT sid, hostname, interface, filter FROM sensor WHERE sid='".$sid."'";
$tmp_result = $db->baseExecute($temp_sql);
if ( $tmp_result )
{
$myrow = $tmp_result->baseFetchRow();
$name = $myrow[1].':'.$myrow[2];
if ( $myrow[3] != "" )
$name = $name.':'.$myrow[3];
}
$tmp_result->baseFreeRows();
return $name;
}
function GetVendor( $mac ){
$mac = str_replace(':', '', $mac);
$mac = substr($mac, 0, 6);
$vendor = 'unknown';
$file = 'base_mac_prefixes.map';
if( ChkAccess($file) > 0 ){
$fp = fopen($file, 'r');
while( !feof($fp) ){
$line = fgets($fp);
if( strcmp($mac, substr($line, 0, 6)) == 0 ){
$vendor = substr($line, 8, strlen($line)-9);
}
}
fclose($fp);
}else{
return "Can't open vendor map.";
}
return $vendor;
}
function InputSafeSQL( &$SQLstr ){
// Removes the escape sequence of \' => ' which arise when a variable
// containing a '-character is passed through a POST query. This is
// needed since otherwise the MySQL parser complains.
$SQLstr = str_replace("\'", "'", $SQLstr);
$SQLstr = str_replace("\\\"", "\"", $SQLstr);
}
function PrintProtocolProfileGraphs( $db ){
$tcp_cnt = TCPPktCnt($db);
$udp_cnt = UDPPktCnt($db);
$icmp_cnt = ICMPPktCnt($db);
$portscan_cnt = PortscanPktCnt($db);
$layer4_cnt = $tcp_cnt + $udp_cnt + $icmp_cnt + $portscan_cnt;
$tcp_percent_show = HtmlPercent($tcp_cnt,$layer4_cnt);
$udp_percent_show = HtmlPercent($udp_cnt,$layer4_cnt);
$icmp_percent_show = HtmlPercent($icmp_cnt,$layer4_cnt);
$portscan_percent_show = HtmlPercent($portscan_cnt,$layer4_cnt);
echo '<TABLE WIDTH="100%" BORDER=0>
<TR><TD>TCP<A HREF="base_qry_main.php?new=1'.
'&layer4=TCP&num_result_rows=-1&sort_order=time_d&submit='._QUERYDBP.'">
('.$tcp_percent_show.')</A></TD><TD></TD></TR></TABLE>
<TABLE class="summarygraph" WIDTH="100%" BORDER=1 CELLSPACING=0 CELLPADDING=0>';
print '<tr>' . HBarGraph($tcp_cnt,$layer4_cnt,'ff0000','cccccc');
PrintFramedBoxFooter(0,2);
echo '<TABLE WIDTH="100%" BORDER=0>
<TR><TD>UDP<A HREF="base_qry_main.php?new=1'.
'&layer4=UDP&num_result_rows=-1&sort_order=time_d&submit='._QUERYDBP.'">
('.$udp_percent_show.')</A></TD><TD></TD></TR></TABLE>
<TABLE class="summarygraph" WIDTH="100%" BORDER=1 CELLSPACING=0 CELLPADDING=0>';
print '<tr>' . HBarGraph($udp_cnt,$layer4_cnt,'ff0000','cccccc');
PrintFramedBoxFooter(0,2);
echo '<TABLE WIDTH="100%" BORDER=0>
<TR><TD>ICMP<A HREF="base_qry_main.php?new=1'.
'&layer4=ICMP&num_result_rows=-1&sort_order=time_d&submit='._QUERYDBP.'">
('.$icmp_percent_show.')</A></TD><TD></TD></TR></TABLE>
<TABLE class="summarygraph" WIDTH="100%" BORDER=1 CELLSPACING=0 CELLPADDING=0>';
print '<tr>' . HBarGraph($icmp_cnt,$layer4_cnt,'ff0000','cccccc');
PrintFramedBoxFooter(0,2);
echo '<CENTER><HR NOSHADE WIDTH="70%"></CENTER>';
echo '<TABLE WIDTH="100%" BORDER=0>
<TR><TD>'._PORTSCAN.'
<A HREF="base_qry_main.php?new=1'.
'&layer4=RawIP&num_result_rows=-1&sort_order=time_d&submit='._QUERYDBP.'">('.$portscan_percent_show.')</A>
</TD><TD></TD></TR></TABLE>
<TABLE class="summarygraph" WIDTH="100%" BORDER=1 CELLSPACING=0 CELLPADDING=0>';
print '<tr>' . HBarGraph($portscan_cnt,$layer4_cnt,'ff0000','cccccc');
PrintFramedBoxFooter(0,2);
}
function BuildIPFormVars( $ipaddr ){
return '' .
'&ip_addr%5B0%5D%5B0%5D=+&ip_addr%5B0%5D%5B1%5D=ip_src&ip_addr%5B0%5D%5B2%5D=%3D'.
'&ip_addr%5B0%5D%5B3%5D='.$ipaddr.
'&ip_addr%5B0%5D%5B8%5D=+&ip_addr%5B0%5D%5B9%5D=OR'.
'&ip_addr%5B1%5D%5B0%5D=+&ip_addr%5B1%5D%5B1%5D=ip_dst&ip_addr%5B1%5D%5B2%5D=%3D'.
'&ip_addr%5B1%5D%5B3%5D='.$ipaddr.
'&ip_addr%5B1%5D%5B8%5D=+&ip_addr%5B1%5D%5B9%5D=+';
}
function BuildSrcIPFormVars( $ipaddr ){
return '' .
'&ip_addr%5B0%5D%5B0%5D=+&ip_addr%5B0%5D%5B1%5D=ip_src&ip_addr%5B0%5D%5B2%5D=%3D'.
'&ip_addr%5B0%5D%5B3%5D='.$ipaddr.
'&ip_addr%5B0%5D%5B8%5D=+&ip_addr%5B0%5D%5B9%5D=+';
}
function BuildDstIPFormVars( $ipaddr ){
return '' .
'&ip_addr%5B0%5D%5B0%5D=+&ip_addr%5B0%5D%5B1%5D=ip_dst&ip_addr%5B0%5D%5B2%5D=%3D'.
'&ip_addr%5B0%5D%5B3%5D='.$ipaddr.
'&ip_addr%5B0%5D%5B8%5D=+&ip_addr%5B0%5D%5B9%5D=+';
}
function BuildUniqueAddressLink( $addr_type, $raw = '' ){
return '<A HREF="base_stat_uaddr.php?addr_type=' . $addr_type . $raw . '">';
}
function BuildUniqueAlertLink( $raw ){
return '<A HREF="base_stat_alerts.php' . $raw . '">';
}
function BuildAddressLink( $ipaddr, $netmask ){
return '<A HREF="base_stat_ipaddr.php?ip=' . rawurlencode($ipaddr)
. '&netmask=' . $netmask . '">';
}
// Add blank row to given criteria element.
function AddCriteriaFormRow(
&$submit, $submit_value, &$cnt, &$criteria_array, $max
){
$submit = $submit_value;
++$cnt;
InitArray($criteria_array[$cnt-1], $max, 0, '');
}
function IPProto2str( $ipproto_code ){
switch( $ipproto_code ){
case 0:
return "IP";
case 1:
return "ICMP";
case 2:
return "IGMP";
case 4:
return "IPIP tunnels";
case 6:
return "TCP";
case 8:
return "EGP";
case 12:
return "PUP";
case 17:
return "UDP";
case 22:
return "XNS UDP";
case 29:
return "SO TP Class 4";
case 41:
return "IPv6 header";
case 43:
return "IPv6 routing header";
case 44:
return "IPv6 fragmentation header";
case 46:
return "RSVP";
case 47:
return "GRE";
case 50:
return "IPSec ESP";
case 51:
return "IPSec AH";
case 58:
return "ICMPv6";
case 59:
return "IPv6 no next header";
case 60:
return "IPv6 destination options";
case 92:
return "MTP";
case 98:
return "Encapsulation header";
case 103:
return "PIM";
case 108:
return "COMP";
case 255:
return "Raw IP";
default:
return $ipproto_code;
}
}
function TCPOption2str( $tcpopt_code ){ // per RFC(s) 1072, 1323, 1644
switch( $tcpopt_code ){
case 2: /* TCPOPT_MAXSEG - maximum segment*/
return "(2) MSS";
case 0: /* TCPOPT_EOL */
return "(0) EOL";
case 1: /* TCPOPT_NOP */
return "(1) NOP";
case 3: /* TCPOPT_WSCALE (rfc1072)- window scale factor */
return "(3) WS";
case 5: /* TCPOPT_SACK (rfc1072)- selective ACK */
return "(5) SACK";
case 4: /* TCPOPT_SACKOK (rfc1072)- selective ACK OK */
return "(4) SACKOK";
case 6: /* TCPOPT_ECHO (rfc1072)- echo */
return "(6) Echo";
case 7: /* TCPOPT_ECHOREPLY (rfc1072)- echo reply */
return "(7) Echo Reply";
case 8: /* TCPOPT_TIMESTAMP (rfc1323)- timestamps */
return "(8) TS";
case 9: /* RFC1693 */
return "(9) Partial Order Connection Permitted";
case 10: /* RFC1693 */
return "(10) Partial Order Service Profile";
case 11: /* TCPOPT_CC (rfc1644)- CC options */
return "(11) CC";
case 12: /* TCPOPT_CCNEW (rfc1644)- CC options */
return "(12) CCNEW";
case 13: /* TCPOPT_CCECHO (rfc1644)- CC options */
return "(13) CCECHO";
case 14: /* RFC1146 */
return "(14) TCP Alternate Checksum Request";
case 15: /* RFC1146 */
return "(15) TCP Alternate Checksum Data";
case 16:
return "(16) Skeeter";
case 17:
return "(17) Bubba";
case 18: /* Subbu and Monroe */
return "(18) Trailer Checksum Option";
case 19: /* Subbu and Monroe */
return "(19) MD5 Signature";
case 20: /* Scott */
return "(20) SCPS Capabilities";
case 21: /* Scott */
return "(21) Selective Negative Acknowledgements";
case 22: /* Scott */
return "(22) Record Boundaries";
case 23: /* Scott */
return "(23) Corruption Experienced";
case 24: /* Sukonnik */
return "(24) SNAP";
case 25:
return "(25) Unassigned";
case 26: /* Bellovin */
return "(26) TCP Compression Filter";
default:
return $tcpopt_code;
}
}
function IPOption2str( $ipopt_code ){
switch( $ipopt_code ){
case 7: /* IPOPT_RR */
return "RR";
case 0: /* IPOPT_EOL */
return "EOL";
case 1: /* IPOPT_NOP */
return "NOP";
case 0x44: /* IPOPT_TS */
return "TS";
case 0x82: /* IPOPT_SECURITY */
return "SEC";
case 0x83: /* IPOPT_LSRR */
return "LSRR";
case 0x84: /* IPOPT_LSRR_E */
return "LSRR_E";
case 0x88: /* IPOPT_SATID */
return "SID";
case 0x89: /* IPOPT_SSRR */
return "SSRR";
}
}
function ICMPType2str( $icmp_type ){
switch ( $icmp_type ){
case 0: /* ICMP_ECHOREPLY */
return "Echo Reply";
case 3: /* ICMP_DEST_UNREACH */
return "Destination Unreachable";
case 4: /* ICMP_SOURCE_QUENCH */
return "Source Quench";
case 5: /* ICMP_REDIRECT */
return "Redirect";
case 8: /* ICMP_ECHO */
return "Echo Request";
case 9:
return "Router Advertisement";
case 10:
return "Router Solicitation";
case 11: /* ICMP_TIME_EXCEEDED */
return "Time Exceeded";
case 12: /* ICMP_PARAMETERPROB */
return "Parameter Problem";
case 13: /* ICMP_TIMESTAMP */
return "Timestamp Request";
case 14: /* ICMP_TIMESTAMPREPLY */
return "Timestamp Reply";
case 15: /* ICMP_INFO_REQUEST */
return "Information Request";
case 16: /* ICMP_INFO_REPLY */
return "Information Reply";
case 17: /* ICMP_ADDRESS */
return "Address Mask Request";
case 18: /* ICMP_ADDRESSREPLY */
return "Address Mask Reply";
case 19:
return "Reserved (security)";
case 20:
return "Reserved (robustness)";
case 21:
return "Reserved (robustness)";
case 22:
return "Reserved (robustness)";
case 23:
return "Reserved (robustness)";
case 24:
return "Reserved (robustness)";
case 25:
return "Reserved (robustness)";
case 26:
return "Reserved (robustness)";
case 27:
return "Reserved (robustness)";
case 28:
return "Reserved (robustness)";
case 29:
return "Reserved (robustness)";
case 30:
return "Traceroute";
case 31:
return "Datagram Conversion Error";
case 32:
return "Mobile Host Redirect";
case 33:
return "IPv6 Where-Are-You";
case 34:
return "IPv6 I-Am-Here";
case 35:
return "Mobile Registration Request";
case 36:
return "Mobile Registration Reply";
case 37:
return "Domain Name Request";
case 38:
return "Domain Name Reply";
case 39:
return "SKIP";
case 40:
return "Photuris";
default:
return $icmp_type;
}
}
function ICMPCode2str( $icmp_type, $icmp_code ){
if ( $icmp_type == 3 ){
switch ( $icmp_code ){
case 0: /* ICMP_NET_UNREACH */
return "Network Unreachable";
case 1: /* ICMP_HOST_UNREACH */
return "Host Unreachable";
case 2: /* ICMP_PROT_UNREACH */
return "Protocol Unreachable";
case 3: /* ICMP_PORT_UNREACH */
return "Port Unreachable";
case 4: /* ICMP_FRAG_NEEDED */
return "Fragmentation Needed/DF set";
case 5: /* ICMP_SR_FAILED */
return "Source Route failed";
case 6: /* ICMP_NET_UNKNOWN */
return "Network Unknown";
case 7: /* ICMP_HOST_UNKNOWN */
return "Host Unknown";
case 8: /* ICMP_HOST_ISOLATED */
return "Host Isolated";
case 9: /* ICMP_NET_ANO */
return "Network ANO";
case 10: /* ICMP_HOST_ANO */
return "Host ANO";
case 11: /* ICMP_NET_UNR_TOS */
return "Network Unreach TOS";
case 12: /* ICMP_HOST_UNR_TOS */
return "Host Unreach TOS";
case 13: /* ICMP_PKT_FILTERED */
return "Packet Filtered";
case 14: /* ICMP_PREC_VIOLATION */
return "Precedence violation";
case 15: /* ICMP_PREC_CUTOFF */
return "Precedence cut off";
default:
return $icmp_code;
}
}elseif ( $icmp_type == 5 ){
switch ( $icmp_code ){
case 0:
return "Redirect datagram for network/subnet";
case 1:
return "Redirect datagram for host";
case 2:
return "Redirect datagram for ToS and network";
case 3:
return "Redirect datagram for Tos and host";
default:
return $icmp_code;
}
}elseif ( $icmp_type == 9 ){
switch ( $icmp_code ){
case 0:
return "Normal router advertisement";
case 16:
return "Does not route common traffic";
default:
return $icmp_code;
}
}elseif ( $icmp_type == 11 ){
switch ( $icmp_code ){
case 0:
return "TTL exceeded in transit";
case 1:
return "Fragment reassembly time exceeded";
default:
return $icmp_code;
}
}elseif ( $icmp_type == 12 ){
switch ($icmp_code)
{
case 0:
return "Pointer indicates error";
case 1:
return "Missing a required option";
case 2:
return "Bad length";
default:
return $icmp_code;
}
}elseif ( $icmp_type == 40 ){
switch ($icmp_code)
{
case 0:
return "Bad SPI";
case 1:
return "Authentication failed";
case 2:
return "Decompression failed";
case 3:
return "Decryption failed";
case 4:
return "Need authentication";
case 5:
return "Need authorization";
default:
return $icmp_code;
}
}else{
return $icmp_code;
}
}
function PrintPayloadChar( $char, $output_type ){
if ( $char >= 32 && $char <= 127 )
{
if ( $output_type == 2 )
return chr($char);
else
return htmlspecialchars(chr($char));
}
else
return '.';
}
function PrintBase64PacketPayload( $encoded_payload, $output_type ){
/* strip out the <CR> at the end of each block */
$encoded_payload = str_replace("\n", "", $encoded_payload);
$payload = base64_decode($encoded_payload);
$len = strlen($payload);
$s = " "._LENGTH." = ".strlen($payload)."\n";
for ($i = 0; $i < strlen($payload); $i++ )
{
if ( $i % 16 == 0 )
{
/* dump the ASCII characters */
if ( $i != 0 )
{
$s = $s.' ';
for ($j = $i-16; $j < $i; $j++ )
$s = $s.PrintPayloadChar(ord($payload[$j]), $output_type);
}
$s = $s.sprintf("\n%03x : ", $i);
}
$s = $s.sprintf("%s ", bin2hex($payload[$i]) );
}
/* print the remained of any ASCII chars */
if ( ($i % 16) != 0 )
{
for ( $j = 0; $j < 16 - ($i % 16); $j++)
$s = $s.' ';
$s = $s.' ';
for ( $j = $len - ($i % 16); $j < $len; $j++ )
$s = $s.PrintPayloadChar(ord($payload[$j]), $output_type);
} else {
$s = $s.' ';
for ( $j = $len - 16; $j < $len && $j > 0; $j++ )
$s = $s.PrintPayloadChar(ord($payload[$j]), $output_type);
}
return $s;
}
function PrintAsciiPacketPayload( $encoded_payload, $output_type ){
return wordwrap($encoded_payload, 70);
}
function PrintHexPacketPayload( $encoded_payload, $output_type ){
/* strip out the <CR> at the end of each block */
$encoded_payload = str_replace("\n", "", $encoded_payload);
$payload = $encoded_payload;
$len = strlen($payload);
$s = " "._LENGTH." = ".(strlen($payload)/2)."\n";
for ($i = 0; $i < strlen($payload); $i += 2 )
{
if ( $i % 32 == 0 )
{
/* dump the ASCII characters */
if ( $i != 0 )
{
$s = $s.' ';
for ($j = $i-32; $j < $i; $j+=2 )
{
$t = hexdec($payload[$j].$payload[$j+1]);
$s = $s.PrintPayloadChar($t, $output_type);
}
}
$s = $s.sprintf("\n%03x : ", $i/2);
}
$s = $s.sprintf("%s%s ", $payload[$i], $payload[$i+1] );
}
/* space through to align end of hex dump */
if ( $i % 32 )
for ( $j = 0; $j < 32 - ($i % 32); $j+=2)
$s = $s.' ';
$s = $s.' ';
/* print the ASCII decode */
if ( $i % 32 )
$start = $len - ($i % 32);
else
$start = $len - 32;
for ( $j = $start; $j < $i; $j+=2 )
{
$t = hexdec($payload[$j].$payload[$j+1]);
$s = $s.PrintPayloadChar($t, $output_type);
}
return $s;
}
function PrintCleanHexPacketPayload( $encoded_payload, $output_type ){
$len = strlen($encoded_payload);
$s = '';
$count = 0;
for ($i = 0; $i < $len; $i += 2 )
{
/* dump the ASCII characters */
$t = hexdec($encoded_payload[$i].$encoded_payload[$i+1]);
$s_tmp = PrintCleanPayloadChar($t, $output_type);
/* Join together several sequential non-ASCII characters displaying their count
* in one line. It makes easyer to look through payload in plain display mode.
* If current character is '<br>' and this is not last character of payload
* increment counter, else output non-ASCII count and flush counter.
*/
if ( ($s_tmp == '<br>') && !($i+2 == $len) ) {
$count++;
} else {
if ($count > 1)
$s .= '<DIV class="nonascii">['. $count .' non-ASCII characters]</DIV>';
elseif ($count == 1)
$s .= '<br>';
$s .= $s_tmp;
$count = 0;
}
}
return $s;
}
function PrintCleanPayloadChar( $char, $output_type ){
if ( $char >= 32 && $char <= 127 )
{
if ( $output_type == 2 )
return chr($char);
else
return htmlspecialchars(chr($char));
}
else
return '<br>';
}
function PrintPacketPayload( $data, $encode_type, $output_type ){
if ( $output_type == 1 )
printf("\n<PRE>\n");
/* print the packet based on encoding type */;
if ( $encode_type == "1" )
$payload = PrintBase64PacketPayload($data, $output_type);
else if ($encode_type == "0" )
{
if ( GetAsciiClean() ){ // Print clean ascii display
$payload = PrintCleanHexPacketPayload($data, $output_type);
}else{
$payload = PrintHexPacketPayload($data, $output_type);
}
}
else if ($encode_type == "2" )
$payload = PrintAsciiPacketPayload($data, $output_type);
if ( $output_type == 1 )
echo "$payload\n</PRE>\n";
return $payload;
}
function GetQueryResultID( $submit, &$seq, &$sid, &$cid ){
// Extract the sid and cid from the $submit variable of the form
// #XX-(XX-XX)
// | | |
// | | |--- cid
// | |------ sid
// |---------- sequence number of DB lookup
if ( preg_match('/#[0-9]+-\([0-9]+-[0-9]+\)$/', $submit) ){
$submit = strstr($submit, '#');
$find = array('#','(',')');
$submit = str_replace($find, '', $submit);
// Since the submit variable is not cleaned do so here:
$tmp = CleanVariable(explode("-", $submit), VAR_DIGIT);
$seq = intval($tmp[0]);
$sid = intval($tmp[1]);
$cid = intval($tmp[2]);
return true;
}else{
return false;
}
}
function ExportPacket( $sid, $cid, $db ){
GLOBAL $action, $action_arg;
// Event.
$sql2 = "SELECT signature, timestamp FROM acid_event WHERE sid='".$sid."' AND cid='".$cid."'";
$result2 = $db->baseExecute($sql2);
$myrow2 = $result2->baseFetchRow();
$s = "------------------------------------------------------------------------------\n";
$s = $s."#($sid - $cid) [$myrow2[1]] ".BuildSigByID($myrow2[0], $db, 2)."\r\n";
$sql4 = "SELECT hostname, interface, filter FROM sensor WHERE sid='".$sid."'";
$result4 = $db->baseExecute($sql4);
$myrow4 = $result4->baseFetchRow();
$result4->baseFreeRows();
$result2->baseFreeRows();
/* IP */
$sql2 = "SELECT ip_src, ip_dst, ".
"ip_ver, ip_hlen, ip_tos, ip_len, ip_id, ip_flags, ip_off, ip_ttl, ip_csum, ip_proto".
" FROM iphdr WHERE sid='".$sid."' AND cid='".$cid."'";
$result2 = $db->baseExecute($sql2);
$myrow2 = $result2->baseFetchRow();
$layer4_proto = $myrow2[11];
if ( $myrow2[0] != "" )
{
$sql3 = "SELECT * FROM opt WHERE sid='".$sid."' AND cid='".$cid."' AND opt_proto='0'";
$result3 = $db->baseExecute($sql3);
$num_opt = $result3->baseRecordCount();
$s = $s."IPv$myrow2[2]: ".
baseLong2IP($myrow2[0])." -> ".
baseLong2IP($myrow2[1])."\n".
" hlen=$myrow2[3] TOS=$myrow2[4] dlen=$myrow2[5] ID=$myrow2[6]".
" flags=$myrow2[7] offset=$myrow2[8] TTL=$myrow2[9] chksum=$myrow2[10]\n";
if ( $num_opt > 0 )
{
$s = $s." Options\n";
for ( $i = 0; $i < $num_opt; $i++)
{
$myrow3 = $result3->baseFetchRow();
$s = $s." #".($i+1)." - ".IPOption2str($myrow3[4])." len=$myrow3[5]";
if ( $myrow3[5] != 0 )
$s = $s." data=$myrow3[6]";
$s = $s."\n";
}
}
$result3->baseFreeRows();
}
$result2->baseFreeRows();
/* TCP */
if ( $layer4_proto == "6" )
{
$sql2 = "SELECT tcp_sport, tcp_dport, tcp_seq, tcp_ack, tcp_off, tcp_res, tcp_flags, tcp_win, ".
" tcp_csum, tcp_urp FROM tcphdr WHERE sid='".$sid."' AND cid='".$cid."'";
$result2 = $db->baseExecute($sql2);
$myrow2 = $result2->baseFetchRow();
$sql3 = "SELECT * FROM opt WHERE sid='".$sid."' AND cid='".$cid."' AND opt_proto='6'";
$result3 = $db->baseExecute($sql3);
$num_opt = $result3->baseRecordCount();
$s = $s."TCP: port=$myrow2[0] -> dport: $myrow2[1] flags=";
if ( ($myrow2[6] & 128) != 0 ) $s = $s.'2'; else $s = $s.'*';
if ( ($myrow2[6] & 64 ) != 0 ) $s = $s.'1'; else $s = $s.'*';
if ( ($myrow2[6] & 32) != 0 ) $s = $s.'U'; else $s = $s.'*';
if ( ($myrow2[6] & 16 ) != 0 ) $s = $s.'A'; else $s = $s.'*';
if ( ($myrow2[6] & 8) != 0 ) $s = $s.'P'; else $s = $s.'*';
if ( ($myrow2[6] & 4 ) != 0 ) $s = $s.'R'; else $s = $s.'*';
if ( ($myrow2[6] & 2 ) != 0 ) $s = $s.'S'; else $s = $s.'*';
if ( ($myrow2[6] & 1 ) != 0 ) $s = $s.'F'; else $s = $s.'*';
$s = $s." seq=$myrow2[2]\n".
" ack=$myrow2[3] off=$myrow2[4] res=$myrow2[5] win=$myrow2[7] urp=$myrow2[9] ".
"chksum=$myrow2[8]\n";
if ( $num_opt != 0)
{
$s = $s." Options:\n";
for ( $i = 0; $i < $num_opt; $i++)
{
$myrow3 = $result3->baseFetchRow();
$s = $s." #".($i+1)." - ".TCPOption2str($myrow3[4])." len=$myrow3[5]";
if ( $myrow3[5] != 0 )
$s = $s." data=".$myrow3[6];
$s = $s."\n";
}
}
$result2->baseFreeRows();
$result3->baseFreeRows();
}
/* UDP */
if ( $layer4_proto == "17" )
{
$sql2 = "SELECT * FROM udphdr WHERE sid='".$sid."' AND cid='".$cid."'";
$result2 = $db->baseExecute($sql2);
$myrow2 = $result2->baseFetchRow();
$s = $s."UDP: port=$myrow2[2] -> dport: $myrow2[3] len=$myrow2[4]\n";
$result2->baseFreeRows();
}
/* ICMP */
if ( $layer4_proto == "1" )
{
$sql2 = "SELECT icmp_type, icmp_code, icmp_csum, icmp_id, icmp_seq FROM icmphdr ".
"WHERE sid='".$sid."' AND cid='".$cid."'";
$result2 = $db->baseExecute($sql2);
$myrow2 = $result2->baseFetchRow();
$s = $s."ICMP: type=".ICMPType2str($myrow2[0])." code=".ICMPCode2str($myrow2[0],$myrow2[1])."\n".
" checksum=$myrow2[2] id=$myrow2[3] seq=$myrow2[4]\n";
$result2->baseFreeRows();
}
/* Print the Payload */
$sql2 = "SELECT data_payload FROM data WHERE sid='".$sid."' AND cid='".$cid."'";
$result2 = $db->baseExecute($sql2);
/* get encoding information and detail_level on the payload */
$sql3 = 'SELECT encoding, detail FROM sensor WHERE sid='.$sid;
$result3 = $db->baseExecute($sql3);
$myrow3 = $result3->baseFetchRow();
$s = $s."Payload: ";
$myrow2 = $result2->baseFetchRow();
if ( $myrow2 )
{
/* print the packet based on encoding type */
$s = $s.PrintPacketPayload($myrow2[0], $myrow3[0], 2)."\n";
$result3->baseFreeRows();
}
else
{
/* Don't have payload so lets print out why by checking the detail level */
/* if have fast detail level */
if ( $myrow3[1] == "0" )
$s = $s."Fast logging used so payload was discarded\n";
else
$s = $s."none\n";
}
$result2->baseFreeRows();
return $s;
}
function ExportPacket_summary( $sid, $cid, $db, $export_type = 0 ){
GLOBAL $action, $action_arg;
// Event.
$sql2 = "SELECT signature, timestamp FROM acid_event WHERE sid='".$sid."' AND cid='".$cid."'";
$result2 = $db->baseExecute($sql2);
$myrow2 = $result2->baseFetchRow();
$alert_timestamp = $myrow2[1];
$alert_sig = BuildSigByID($myrow2[0], $db, 2);
$result2->baseFreeRows();
/* IP */
$src_ip = $dst_ip = $src_port = $dst_port = "";
$sql2 = "SELECT ip_src, ip_dst, ip_proto".
" FROM iphdr WHERE sid='".$sid."' AND cid='".$cid."'";
$result2 = $db->baseExecute($sql2);
$myrow2 = $result2->baseFetchRow();
$layer4_proto = "";
if ( $myrow2[0] != "" )
{
$src_ip = baseLong2IP($myrow2[0]);
$dst_ip = baseLong2IP($myrow2[1]);
$layer4_proto = $myrow2[2];
}
$result2->baseFreeRows();
/* TCP */
if ( $layer4_proto == "6" )
{
$sql2 = "SELECT tcp_sport, tcp_dport FROM tcphdr WHERE sid='".$sid."' AND cid='".$cid."'";
$result2 = $db->baseExecute($sql2);
$myrow2 = $result2->baseFetchRow();
if ( $export_type == 0 )
{
$src_port = ":".$myrow2[0]." -> ";
$dst_port = ":".$myrow2[1];
}
else
{
$src_port = $myrow2[0];
$dst_port = $myrow2[1];
}
$result2->baseFreeRows();
}
/* UDP */
if ( $layer4_proto == "17" )
{
$sql2 = "SELECT * FROM udphdr WHERE sid='".$sid."' AND cid='".$cid."'";
$result2 = $db->baseExecute($sql2);
$myrow2 = $result2->baseFetchRow();
if ( $export_type == 0 )
{
$src_port = ":".$myrow2[2]." -> ";
$dst_port = ":".$myrow2[3];
}
else
{
$src_port = $myrow2[2];
$dst_port = $myrow2[3];
}
$result2->baseFreeRows();
}
/* ICMP */
if ( $layer4_proto == "1" )
{
if ( $export_type == 0 )
$src_ip = $src_ip." -> ";
$src_port = $dst_port = "";
}
/* Portscan Traffic */
if ( $layer4_proto == "255" )
{
if ( $export_type == 0 )
$src_ip = $src_ip." -> ";
}
if ( $export_type == 0 )
{
$s = sprintf("#%d-%d| [%s] %s%s%s%s %s\r\n",
$sid, $cid, $alert_timestamp,
$src_ip, $src_port, $dst_ip, $dst_port,
$alert_sig);
}
else /* CSV format */
{
$s = sprintf("\"%d\", \"%d\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"\r\n",
$sid, $cid, $alert_timestamp,
$src_ip, $src_port, $dst_ip, $dst_port,
$alert_sig);
}
return $s;
}
function base_microtime(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function Percent ( $Value = 1, $Count = 1 ){
if( !is_numeric($Value) ){ // Input Validation
$Value = 1;
}
if( !is_numeric($Count) ){
$Count = 1;
}
if( $Value > $Count ){
$Count = $Value;
}
if( $Count <= 0 ){
$Count = 1;
}
if( $Value <= 0 ){ // Set %
$Ret = 0;
}else{
$Ret = round($Value/$Count*100);
}
return $Ret;
}
// Returns true if file passes include safety checks.
// Also includes the file.
function base_include ( $file = '' ){
GLOBAL $BASE_path, $debug_mode;
$Ret = false;
$EMsg = '';
$tfile = "$BASE_path/custom/" . $file;
$ReqRE = preg_quote("$BASE_path/custom/",'/');
$ReqRE .= ".*\.htm(l)?";
if ( preg_match("/^" . $ReqRE ."$/i", $tfile) ){
// File must be in specific location with specific extension.
$Loc = realpath($tfile); // Final file must
if ( $Loc != false // exist and resolve to an absolute path.
&& fileowner($Loc) != false // not be owned by UID 0 (root).
&& ChkAccess($Loc) > 0 // be a real file & be readable.
){
if ( preg_match("/^" . $ReqRE ."$/i", $Loc) ){
// be in specific location with specific extension.
$Ret = true;
$EMsg = 'OK';
include_once($Loc);
}else{
$EMsg = 'Loc';
$tfile .= " -> $Loc";