-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbase_qry_alert.php
1168 lines (1035 loc) · 41.2 KB
/
base_qry_alert.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) 2004 BASE Project Team
** Copyright (C) 2000 Carnegie Mellon University
**
** (see the file 'base_main.php' for license details)
**
** Project Leads: Kevin Johnson <[email protected]>
** Built upon work by Roman Danyliw <[email protected]>, <[email protected]>
**
** Purpose: displays a single alert
**
** Input GET/POST variables
** - caller
** - submit:
********************************************************************************
** Authors:
********************************************************************************
** Kevin Johnson <[email protected]
**
********************************************************************************
*/
$sc = DIRECTORY_SEPARATOR;
require_once("includes$sc" . 'base_krnl.php');
include ("$BASE_path/includes/base_include.inc.php");
include_once ("$BASE_path/base_db_common.php");
include_once ("$BASE_path/base_qry_common.php");
include_once ("$BASE_path/base_stat_common.php");
AuthorizedRole(10000);
$payload = FALSE;
$offset = 0;
$tmp = 0; // Set cookie for packet display
if( GetAsciiClean() ){
$tmp = 1;
}
BCS('asciiclean', $tmp);
$sf_portscan_flag = 0;
function PrintCleanURL(){
// This function creates the url to display the cleaned up payload -- Kevin
$query = CleanVariable($_SERVER["QUERY_STRING"], VAR_PERIOD | VAR_DIGIT | VAR_PUNC | VAR_LETTER);
$sort_order=ImportHTTPVar("sort_order", VAR_LETTER | VAR_USCORE);
$url = '<center><a href="base_qry_alert.php?' . $query;
$url .= '&sort_order='.urlencode($sort_order).'&asciiclean=';
if( GetAsciiClean() ){ // Create link to non-cleaned payload display.
$url.= '0">'._QANORMALD;
}else{ // Create link to cleaned payload display.
$url.= '1">'._QAPLAIND;
}
$url.= '</a></center>';
return $url;
}
function PrintBinDownload( $db, $cid, $sid ){
// Offering a URL to a download possibility:
if( GetAsciiClean() ){
$tmp = 1;
}else{
$tmp = 0;
}
$query = CleanVariable($_SERVER["QUERY_STRING"], VAR_PERIOD | VAR_DIGIT | VAR_PUNC | VAR_LETTER);
$url = '<center><a href="base_payload.php?' . $query;
$url .= '&download=1';
$url .= '&cid='.urlencode($cid).'&sid='.urlencode($sid);
$url .= '&asciiclean=' . $tmp;
$url.= '">Download of Payload</a></center>';
return $url;
}
function PrintPcapDownload( $db, $cid, $sid ){
if (!isset($db))
{
error_log("ERROR: \$db is NOT set.");
ErrorMessage(__FILE__ . ":" . __LINE__ . ": db is NOT set. Ignoring.");
$debug_str = "<BR><PRE>\n\n" . debug_print_backtrace() . "\n\n</PRE><BR>\n";
ErrorMessage($debug_str);
}
if (!isset($db->DB))
{
error_log("ERROR: \$db->DB is NOT set.");
ErrorMessage(__FILE__ . ":" . __LINE__ . ": db->DB is NOT set. Ignoring.");
$debug_str = "<BR><PRE>\n\n" . debug_print_backtrace() . "\n\n</PRE><BR>\n";
ErrorMessage($debug_str);
}
if (!is_array($db->DB->MetaColumnNames('data')))
{
error_log("ERROR: \$db->DB->MetaColumnNames('data') is NOT an array.");
ErrorMessage(__FILE__ . ":" . __LINE__ . ": db->DB->MetaColumnNames('data') is NOT an array. Ignoring.");
print "<BR><PRE>\n\n";
debug_print_backtrace();
print "\n\n" ;
var_dump($db->DB->MetaColumnNames('data'));
print "</PRE><BR>\n\n" ;
}
if ( !in_array("pcap_header", $db->DB->MetaColumnNames('data')) ||
!in_array("data_header", $db->DB->MetaColumnNames('data'))) {
$type = 3;
} else {
$type = 2;
}
if( GetAsciiClean() ){
$tmp = 1;
}else{
$tmp = 0;
}
$query = CleanVariable($_SERVER["QUERY_STRING"], VAR_PERIOD | VAR_DIGIT | VAR_PUNC | VAR_LETTER);
$url = '<center><a href="base_payload.php?' . $query;
$url .= '&download='.urlencode($type);
$url .= '&cid='.urlencode($cid).'&sid='.urlencode($sid);
$url .= '&asciiclean=' . $tmp;
$url .= '">Download in pcap format</a></center>';
return $url;
}
function PrintPacketLookupBrowseButtons( $seq, $sql, $db, &$p_b, &$n_b ){
GLOBAL $debug_mode, $UIL;
$EMPfx = __FUNCTION__ . ': ';
if ( class_exists('UILang') ){ // Issue 11 backport shim.
$BtnLast = $UIL->CWA['Last'];
$BtnFirst = $UIL->CWA['First'];
}else{
$BtnLast = _LAST;
$BtnFirst = _FIRST;
}
if ( !is_int($seq) ){ // Input Validation
$seq = 0;
}
NLIO ("<!-- Single Alert Browsing Buttons -->");
if ( $seq > 0 ){
$start = $seq -1;
}else{
$start = $seq;
}
if ( $debug_mode > 0 ){
ErrorMessage (
$EMPfx. "Execute SQL: $sql LIMIT $start, 3",'black',1
);
}
$rs = $db->baseExecute($sql, $start, 3);
if (
$rs != false
&& $db->baseErrorMessage() == ''
&& $rs->baseRecordCount() > 0
){ // Error Check
if ( $debug_mode > 1 ){
ErrorMessage (
$EMPfx. "Records: ".$rs->baseRecordCount(),'black', 1
);
}
if ( $seq == 0 ){
$p_b = "[ $BtnFirst ]";
}
$Pfx = "<INPUT TYPE='submit' NAME='submit' VALUE='";
for ( $i = $start; $i <= $seq + 1; $i++ ){
$row = $rs->baseFetchRow();
if ( $debug_mode > 1 ){
ErrorMessage ("# $i - $seq", 'black',1);
var_dump($row);
}
if ( $row == '' ){
$n_b = "[ $BtnLast ]";
break;
}
$Sfx = '-('.$row[0].'-'.$row[1].")'>";
if ( $i == $seq - 1 ){
$p_b = $Pfx."<< "._PREVIOUS." #".($seq-1).$Sfx;
}elseif ( $i == $seq + 1 ){
$n_b = $Pfx.">> "._NEXT." #".($seq+1).$Sfx;
}
}
$rs->baseFreeRows();
if ( $debug_mode > 1 ){
ErrorMessage ( $EMPfx. "Ret-P: ".XSSPrintSafe($p_b),'black',1 );
ErrorMessage ( $EMPfx. "Ret-N: ".XSSPrintSafe($n_b),'black',1 );
}
}else{
ErrorMessage ($EMPfx. "BASE DB Error: ".$db->baseErrorMessage() == '');
}
}
$sort_order = ImportHTTPVar( 'sort_order', VAR_LETTER | VAR_USCORE );
// Need to import $submit and set the $QUERY_STRING early to support the back
// button. Otherwise, the value of $submit will not be passed to the history.
//
// $submit can contain values in the form of "#xx-(xx-xx)" and
// other "submit" values.
$submit = ImportHTTPVar(
'submit', VAR_DIGIT | VAR_PUNC | VAR_LETTER,
array(_SELECTED, _ALLONSCREEN, _ENTIREQUERY)
);
$_SERVER["QUERY_STRING"] = "submit=".rawurlencode($submit);
$cs = new CriteriaState("base_qry_main.php", "&new=1&submit="._QUERYDBP);
$cs->ReadState();
$qs = new QueryState();
$qs->current_sort_order = $sort_order;
$page_title = _ALERT;
PrintBASESubHeader($page_title, $page_title, $cs->GetBackLink(), $refresh_all_pages);
$db = NewBASEDBConnection($DBlib_path, $DBtype); // Connect to Alert DB.
$db->baseDBConnect(
$db_connect_method,$alert_dbname, $alert_host, $alert_port, $alert_user,
$alert_password
);
UpdateAlertCache($db);
if ( class_exists('UILang') ){ // Issue 11 backport shim.
$CPSig = $UIL->CWA['Sig'];
$CPSA = $UIL->CPA['SrcAddr'];
$CPDA = $UIL->CPA['DstAddr'];
$CPTs = $UIL->CWA['Ts'];
}else{
$CPSig = _SIGNATURE;
$CPSA = _NBSOURCEADDR;
$CPDA = _NBDESTADDR;
$CPTs = _TIMESTAMP;
}
PrintCriteria("");
$criteria_clauses = ProcessCriteria();
$from = " FROM acid_event ".$criteria_clauses[0];
$where = " WHERE ".$criteria_clauses[1];
$qs->AddValidAction("ag_by_id");
$qs->AddValidAction("ag_by_name");
$qs->AddValidAction("add_new_ag");
$qs->AddValidAction("del_alert");
$qs->AddValidAction("email_alert");
$qs->AddValidAction("email_alert2");
$qs->AddValidAction("archive_alert");
$qs->AddValidAction("archive_alert2");
$qs->AddValidActionOp(_SELECTED);
$qs->SetActionSQL($from.$where);
$et->Mark("Initialization");
$qs->RunAction($submit, PAGE_ALERT_DISPLAY, $db);
$et->Mark("Alert Action");
//If get a valid (sid,cid) store it in $caller. If $submit is returning
// from an alert action get the (sid,cid) back from $caller.
if ( $submit == _SELECTED ){
$submit = ImportHTTPVar('caller', VAR_DIGIT | VAR_PUNC);
}else{
$caller = $submit;
}
if ( $debug_mode > 0 ){
$TK = array ( 'caller', 'submit' );
$DI = array();
$DD = array();
foreach ( $TK as $val ){
array_push($DD, $val);
array_push($DI, $$val);
}
array_push($DD, 'QS-CCQ');
array_push($DI, $qs->GetCurrentCannedQuery());
DDT($DI,$DD,'Caller / Submit / QS-CCQ Values','',25);
}
/* Setup the Query Results Table -- However, this data structure is not
* really used for output. Rather, it duplicates the sort SQL set in
* base_qry_sqlcalls.php
*/
$qro = new QueryResultsOutput("");
// Common SQL Strings
$OB = ' ORDER BY';
$qro->AddTitle($CPSig,
"sig_a", " ", "$OB sig_name ASC",
"sig_d", " ", "$OB sig_name DESC"
);
$qro->AddTitle($CPTs,
"time_a", " ", "$OB timestamp ASC ",
"time_d", " ", "$OB timestamp DESC "
);
$qro->AddTitle($CPSA,
"sip_a", " ", "$OB ip_src ASC",
"sip_d", " ", "$OB ip_src DESC"
);
$qro->AddTitle($CPDA,
"dip_a", " ", "$OB ip_dst ASC",
"dip_d", " ", "$OB ip_dst DESC"
);
$qro->AddTitle(_NBLAYER4,
"proto_a", " ", "$OB ip_proto ASC",
"proto_d", " ", "$OB ip_proto DESC"
);
// Issue #168
$save_sql = "SELECT acid_event.sid, acid_event.cid";
$sqlPFX = $from.$where;
$sort_sql = $qro->GetSortSQL($qs->GetCurrentSort(), $qs->GetCurrentCannedQuerySort());
if ( !is_null($sort_sql) ){
$sqlPFX = $sort_sql[0].$sqlPFX.$sort_sql[1];
}
$save_sql .= $sqlPFX;
GetQueryResultID($submit, $seq, $sid, $cid);
if ( $debug_mode > 0 ){
if ( $qs->isCannedQuery() ){
$CCF = 'Yes';
$qs->PrintCannedQueryList();
}else{
$CCF = 'No';
}
print "Canned Query: $CCF <br/>";
$qs->DumpState();
print "SQL Saved: $save_sql <br/>";
$TK = array ( 'caller', 'submit', 'sid', 'cid', 'seq' );
$DI = array();
$DD = array();
foreach ( $TK as $val ){
array_push($DD, $val);
array_push($DI, $$val);
}
DDT($DI,$DD,'Alert Lookup ','',25);
}
// Verify (sid, cid) are extracted correctly.
if ( is_int($sid) && is_int($cid) && !($sid > 0 && $cid > 0) ){
// Added is_int checks as Issue #5 fix. If the above call to
// GetQueryResultID() fails, $sid & $cid will be defined but unset,
// which makes them of type string on PHP 5.2x & of type NULL on PHP
// 5.3+ See: https://travis-ci.org/NathanGibbs3/BASE/jobs/546765554
// This should only occur in the test conditions for Issue #5. This
// fix allows $sid, & $cid of any type except int to pass through
// without exiting the app while under test.
// Note if this breaks something in production.
// Comment at: https://github.com/NathanGibbs3/BASE/issues/5
FatalError(_QAINVPAIR." (".$sid.",".$cid.")");
}else{
if ( getenv('TRAVIS') && version_compare(PHP_VERSION, "5.3.0", "<") ){
// Issue #5 Test Shim
$sid = 1;
$cid = 1;
}
}
PrintPacketLookupBrowseButtons($seq, $save_sql, $db, $previous, $next);
echo "<FORM METHOD=\"GET\" ACTION=\"base_qry_alert.php\">\n";
echo "<CENTER>\n<B>"._ALERT." #".($seq)."</B><BR>\n$previous    \n$next\n</CENTER>\n";
echo "<HR>\n";
/* Make Selected */
echo "\n<INPUT TYPE=\"hidden\" NAME=\"action_chk_lst[0]\" VALUE=\"$submit\">\n";
/* Event */
$sql2 = "SELECT signature, timestamp FROM acid_event WHERE sid='".filterSql($sid)."' AND cid='".filterSql($cid)."'";
if ( $debug_mode > 0 ){
print "<BR><BR>\n\n" . __FILE__ . ":" . __LINE__ . ": DEBUG: \$sql2 = \"$sql2\"<BR><BR>\n\n";
}
$result2 = $db->baseExecute($sql2);
$myrow2 = $result2->baseFetchRow();
if ( is_array($myrow2) ){
if ( $myrow2[0] == "" ){
print '<center><b>'.returnErrorMessage(_QAALERTDELET).'</center></b>';
}
$Alert_Time = $myrow2[1];
$Alert_Sig = $myrow2[0];
}else{
$Alert_Time = 'Testing';
$Alert_Sig = 'Testing';
}
/* Get sensor parameters: */
$sql4 = "SELECT hostname, interface, filter, encoding, detail FROM sensor WHERE sid='".filterSql($sid)."'";
$result4 = $db->baseExecute($sql4);
$myrow4 = $result4->baseFetchRow();
$result4->baseFreeRows();
if ( is_array($myrow4) ){
$Sensor_Name = $myrow4[0];
if ( $myrow4[1] == "" ){
$Sensor_Int = " <I>"._NONE."</I> ";
}else{
$Sensor_Int = $myrow4[1];
}
if ( $myrow4[2] == "" ){
$Sensor_Filt = " <I>"._NONE."</I> ";
}else{
$Sensor_Filt = $myrow4[2];
}
$encoding = $myrow4[3];
$detail = $myrow4[4];
}else{
$Sensor_Name = _NONE;
$Sensor_Int = _NONE;
$Sensor_Filt = _NONE;
$encoding = 2;
$detail = 1;
}
echo '
<BLOCKQUOTE>
<TABLE BORDER=1 width="90%">
<TR><TD CLASS="metatitle" WIDTH=50 ALIGN=CENTER ROWSPAN=4>Meta
<TD>
<TABLE BORDER=1 CELLPADDING=4>
<TR><TD CLASS="plfieldhdr" >'._ID.' #</TD>
<TD CLASS="plfieldhdr">'._CHRTTIME.'</TD>
<TD CLASS="plfieldhdr">'._QATRIGGERSIG.'</TD></TR>
<TR><TD CLASS="plfield">'.($sid." - ".$cid).'</TD>
<TD CLASS="plfield">'.htmlspecialchars($Alert_Time).'</TD>
<TD CLASS="plfield">'.(GetTagTriger(BuildSigByID($Alert_Sig, $db), $db, $sid, $cid)).'</TD></TR>
</TABLE>
</TD>
</TR>';
echo ' <TR>
<TD>
<TABLE BORDER=1 CELLPADDING=4>
<TR><TD CLASS="metatitle" ALIGN=CENTER ROWSPAN=2>'._SENSOR.'</TD>
<TD class="plfieldhdr">',_SENSOR.' '._ADDRESS,'</TD>
<TD class="plfieldhdr">'._INTERFACE.'</TD>
<TD class="plfieldhdr">'._FILTER.'</TD>
</TR>
<TR><TD class="plfield">'.htmlspecialchars($Sensor_Name).'</TD>
<TD class="plfield">'.$Sensor_Int.'</TD>
<TD class="plfield">'.$Sensor_Filt.'</TD>
</TR>
</TABLE>
</TR>';
if ( isset($resolve_IP) && $resolve_IP == 1 ){
echo ' <TR>
<TD>
<TABLE BORDER=1 CELLPADDING=4>
<TR><TD CLASS="iptitle" ALIGN=CENTER ROWSPAN=2>FQDN</TD>
<TD class="plfieldhdr">'._SENSOR.' '._NAME.'</TD>
</TR>
<TR><TD class="plfield">';
# Is this a dotted IPv4 address?
$pattern = '/(\d{1,3}\.){3}\d{1,3}/';
if (preg_match($pattern, $myrow4[0]))
{
echo baseGetHostByAddr($myrow4[0], $db, $dns_cache_lifetime);
}
else
{
echo $myrow4[0];
}
echo ' </TD>
</TR>
</TABLE>
</TR>';
}
$result4->baseFreeRows();
$sql4 = "SELECT acid_ag_alert.ag_id, ag_name, ag_desc ".
"FROM acid_ag_alert LEFT JOIN acid_ag ON acid_ag_alert.ag_id = acid_ag.ag_id ".
"WHERE ag_sid='".$sid."' AND ag_cid='".$cid."'";
$result4 = $db->baseExecute($sql4);
$num = $result4->baseRecordCount();
echo ' <TR>
<TD>
<TABLE BORDER=1 CELLPADDING=4>
<TR><TD CLASS="metatitle" ALIGN=CENTER ROWSPAN='.($num+1).'>'._ALERTGROUP.'</TD>';
if ( $num > 0 )
echo ' <TD class="plfieldhdr">'._ID.'</TD>
<TD class="plfieldhdr">'._NAME.'</TD>
<TD class="plfieldhdr">'._DESC.'</TD></TR>';
else
echo ' <TD> <I>'._NONE.'</I> </TD></TR>';
for ($i = 0; $i < $num; $i++)
{
$myrow4 = $result4->baseFetchRow();
echo ' <TR><TD class="plfield">'.htmlspecialchars($myrow4[0]).'</TD>
<TD class="plfield">'.htmlspecialchars($myrow4[1]).'</TD>
<TD class="plfield">'.htmlspecialchars($myrow4[2]).'</TD>
</TR>';
}
echo ' </TABLE>';
$result4->baseFreeRows();
echo ' </TR>
</TABLE>';
$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 = -1;
if ( is_array($myrow2) ){
$IP_Src = $myrow2[0];
}else{
$IP_Src = '';
}
if ( $IP_Src != '' ){
$sql3 = "SELECT * FROM opt WHERE sid='".$sid."' AND cid='".$cid."' AND opt_proto='0'";
$result3 = $db->baseExecute($sql3);
$num_opt = $result3->baseRecordCount();
echo '
<TABLE BORDER=1 WIDTH="90%">
<TR><TD CLASS="iptitle" WIDTH=50 ROWSPAN=3 ALIGN=CENTER>IP';
echo ' <TD>';
echo ' <TABLE BORDER=1 CELLPADDING=2>';
echo ' <TR><TD class="plfieldhdr">'._NBSOURCEADDR.'</TD>
<TD class="plfieldhdr"> '._NBDESTADDR.' </TD>
<TD class="plfieldhdr">Ver</TD>
<TD class="plfieldhdr">Hdr Len</TD>
<TD class="plfieldhdr">TOS</TD>
<TD class="plfieldhdr">'._LENGTH.'</TD>
<TD class="plfieldhdr">'._ID.'</TD>
<TD class="plfieldhdr">fragment</TD>
<TD class="plfieldhdr">offset</TD>
<TD class="plfieldhdr">TTL</TD>
<TD class="plfieldhdr">chksum</TD></TR>';
echo ' <TR><TD class="plfield">
<A HREF="base_stat_ipaddr.php?ip='.baseLong2IP($myrow2[0]).'&netmask=32">'.
baseLong2IP($myrow2[0]).'</A></TD>';
echo ' <TD class="plfield">
<A HREF="base_stat_ipaddr.php?ip='.baseLong2IP($myrow2[1]).'&netmask=32">'.
baseLong2IP($myrow2[1]).'</A></TD>';
echo ' <TD class="plfield">'.htmlspecialchars($myrow2[2]).'</TD>';
echo ' <TD class="plfield">'.($myrow2[3] << 2).'</TD>'; /* ihl is in 32 bit words, must be multiplied by 4 to show in bytes */
echo ' <TD class="plfield">'.htmlspecialchars($myrow2[4]).'</TD>';
echo ' <TD class="plfield">'.htmlspecialchars($myrow2[5]).'</TD>';
echo ' <TD class="plfield">'.htmlspecialchars($myrow2[6]).'</TD>';
echo ' <TD class="plfield">';
if ($myrow2[7] == 1)
echo 'yes';
else
echo 'no';
echo '</TD>';
list( , $my_offset, ) = unpack("n", pack("S", $myrow2[8]));
echo ' <TD class="plfield">'. ($my_offset * 8) .'</TD>';
echo ' <TD class="plfield">'.htmlspecialchars($myrow2[9]).'</TD>';
echo ' <TD class="plfield">'.htmlspecialchars($myrow2[10]).'<BR>= 0x'.dechex($myrow2[10]).'</TD></TR>';
echo ' </TABLE>';
if ( isset($resolve_IP) && $resolve_IP == 1 ){
echo ' <TR>
<TD>
<TABLE BORDER=1 CELLPADDING=4>
<TR><TD CLASS="iptitle" ALIGN=CENTER ROWSPAN=2>FQDN</TD>
<TD class="plfieldhdr">'._SOURCENAME.'</TD>
<TD class="plfieldhdr">'._DESTNAME.'</TD>
</TR>
<TR><TD class="plfield">'.
(baseGetHostByAddr(baseLong2IP($myrow2[0]),
$db, $dns_cache_lifetime)).'</TD>
<TD class="plfield">'.
(baseGetHostByAddr(baseLong2IP($myrow2[1]),
$db, $dns_cache_lifetime)).'</TD>
</TR>
</TABLE>
</TR>';
}
echo ' <TR>';
echo ' <TD>';
echo ' <TABLE BORDER=1 CELLPADDING=4>';
echo ' <TR><TD CLASS="iptitle" ALIGN=CENTER ROWSPAN='.(($num_opt != 0) ? ($num_opt+1) : 1).'>'._OPTIONS.'</TD>';
$layer4_proto = $myrow2[11];
if ( $num_opt > 0 )
{
echo ' <TD></TD>
<TD class="plfieldhdr">'._CODE.'</TD>
<TD class="plfieldhdr">'._LENGTH.'</TD>
<TD class="plfieldhdr" ALIGN=CENTER>'._DATA.'</TD>';
for ( $i = 0; $i < $num_opt; $i++)
{
$myrow3 = $result3->baseFetchRow();
echo ' <TR><TD>#'.($i+1).'</TD>';
echo ' <TD class="plfield">'.IPOption2str($myrow3[4]).'</TD>';
echo ' <TD class="plfield">'.htmlspecialchars($myrow3[5]).'</TD>';
echo ' <TD class="plfield">';
if ($myrow3[6] != "" )
echo $myrow3[6];
else
echo ' ';
echo '</TD></TR>';
}
}
else
{
echo ' <TD>     <I>'._NONE.' </I></TD></TR>';
}
echo ' </TABLE></TD></TR>';
echo '</TABLE>';
$result3->baseFreeRows();
}
$result2->baseFreeRows();
/* If we have FLoP's (Fast Logging Project for Snort) extended
* database schema then we can show mac addresses from `data_header`
* field from `data` table
*/
if (!isset($db))
{
error_log("ERROR: \$db is NOT set.");
ErrorMessage(__FILE__ . ":" . __LINE__ . ": db is NOT set. Ignoring.");
$debug_str = "<BR><PRE>\n\n" . debug_print_backtrace() . "\n\n</PRE><BR>\n";
ErrorMessage($debug_str);
}
if (!isset($db->DB))
{
error_log("ERROR: \$db->DB is NOT set.");
ErrorMessage(__FILE__ . ":" . __LINE__ . ": db->DB is NOT set. Ignoring.");
$debug_str = "<BR><PRE>\n\n" . debug_print_backtrace() . "\n\n</PRE><BR>\n";
ErrorMessage($debug_str);
}
if (!is_array($db->DB->MetaColumnNames('data')))
{
error_log("ERROR: \$db->DB->MetaColumnNames('data') is NOT an array.");
ErrorMessage(__FILE__ . ":" . __LINE__ . ": db->DB->MetaColumnNames('data') is NOT an array. Ignoring.");
print "<BR><PRE>\n\n";
debug_print_backtrace();
print "\n\n" ;
var_dump($db->DB->MetaColumnNames('data'));
print "</PRE><BR>\n\n" ;
}
if (in_array("data_header", $db->DB->MetaColumnNames('data'))) {
$sql5 = "SELECT data_header FROM data WHERE sid='$sid' AND cid='$cid'";
$result5 = $db->baseExecute($sql5);
$myrow5 = $result5->baseFetchRow();
$result5->baseFreeRows();
if (is_array($myrow5))
{
if ( $debug_mode > 0 ) {
echo "<debug><BR>";
echo "Encoding: $encoding<BR>";
echo "Data header: <$myrow5[0]><BR>";
echo "strlen: " . strlen($myrow5[0]);
echo "<br>Base64 decoded: <" . base64_decode($myrow5[0]) . "><BR>";
echo "strlen: " . strlen(base64_decode($myrow5[0]));
echo "<br>bin2hex: <" . bin2hex(base64_decode($myrow5[0])) . "><BR>";
echo "strlen: " . strlen(bin2hex(base64_decode($myrow5[0])));
echo "<BR></debug><BR>";
}
/* 0 == hex, 1 == base64, 2 == ascii; cf. snort-2.4.4/src/plugbase.h */
if ($encoding == 0) {
$t = $myrow5[0];
} elseif ($encoding == 1) {
$t = bin2hex(base64_decode($myrow5[0]));
} else {
echo "<BR><BR>This type of encoding is not supported. Please use either hex oder ";
echo "base64 encoding. Do not use ascii, because ascii encoding loses data.<BR><BR>";
}
/* from here on $t is in hex format, even if original encoding was base64 */
/* "MACDAD" (ascii code in hex: 4d 41 43 44 41 44) is a key word used by
* sfPortscan, rather than a real MAC address; cf.
* snort-2.6.0/doc/README.sfportscan
* snort-2.6.0/src/preprocessors/spp_sfportscan.c
* snort-2.6.0/src/preprocessors/flow/portscan/flowps_snort.c */
if ( strlen($t) >= 24 && strncmp($t, '4d41434441444d4143444144', 24) != 0)
{
$dst_mac = $t[0].$t[1].':'.$t[2].$t[3].':'.$t[4].$t[5].':'.$t[6].$t[7].':'.$t[8].$t[9].':'.$t[10].$t[11];
$src_mac = $t[12].$t[13].':'.$t[14].$t[15].':'.$t[16].$t[17].':'.$t[18].$t[19].':'.$t[20].$t[21].':'.$t[22].$t[23];
echo '
<TABLE BORDER=1 WIDTH="90%">
<TR><TD CLASS="iptitle" WIDTH=50 ROWSPAN=3 ALIGN=CENTER>MAC';
echo ' <TD>';
echo ' <TABLE BORDER=1 CELLPADDING=2>';
echo ' <TR><TD class="plfieldhdr">'._NBSOURCEADDR.'</TD>
<TD class="plfieldhdr"> '._NBDESTADDR.' </TD></TR>
<TR><TD>'. $src_mac .'</TD>
<TD>'. $dst_mac .'</TD></TR>
<TR><TD>'. GetVendor($src_mac) .'</TD>
<TD>'. GetVendor($dst_mac) .'</TD></TR>';
echo ' </TABLE>';
echo '</TABLE></TD></TR>';
}
else
{
/* "MACDAD" indicates that this is an sfportscan packet. This means
the database does NOT contain a real packet. Therefore
building a pcap file won't be possible. */
$sf_portscan_flag = 1;
}
}
}
/* 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();
echo '
<TABLE BORDER=1 WIDTH="90%">
<TR><TD CLASS="layer4title" WIDTH=50 ROWSPAN=2 ALIGN=CENTER>TCP';
echo ' <TD>';
echo ' <TABLE BORDER=1 CELLPADDING=2>';
echo ' <TR><TD class="plfieldhdr">'._SHORTSOURCE.'<BR> '._PORT.'</TD>
<TD class="plfieldhdr"> '._SHORTDEST.'<BR>   '._PORT.'  </TD>
<TD class="plfieldhdr">R<BR>1</TD>
<TD class="plfieldhdr">R<BR>0</TD>
<TD class="plfieldhdr">U<BR>R<BR>G</TD>
<TD class="plfieldhdr">A<BR>C<BR>K</TD>
<TD class="plfieldhdr">P<BR>S<BR>H</TD>
<TD class="plfieldhdr">R<BR>S<BR>T</TD>
<TD class="plfieldhdr">S<BR>Y<BR>N</TD>
<TD class="plfieldhdr">F<BR>I<BR>N</TD>
<TD class="plfieldhdr">seq #</TD>
<TD class="plfieldhdr">ack</TD>
<TD class="plfieldhdr">offset</TD>
<TD class="plfieldhdr">res</TD>
<TD class="plfieldhdr">window</TD>
<TD class="plfieldhdr">urp</TD>
<TD class="plfieldhdr">chksum</TD></TR>';
$src_port = $myrow2[0].'<BR>';
foreach ($external_port_link as $name => $baseurl) {
$src_port = $src_port.'[<A HREF="'.$baseurl.$myrow2[0].'" TARGET="_ACID_PORT_">'.$name.'</A>] ';
}
$dst_port = $myrow2[1].'<BR>';
foreach ($external_port_link as $name => $baseurl) {
$dst_port = $dst_port.'[<A HREF="'.$baseurl.$myrow2[1].'" TARGET="_ACID_PORT_">'.$name.'</A>] ';
}
echo ' <TR><TD class="plfield">'.$src_port.'</TD>';
echo ' <TD class="plfield">'.$dst_port.'</TD>';
echo ' <TD class="plfield">';
if ( ($myrow2[6] & 128) != 0 )
echo 'X';
else
echo ' ';
echo ' </TD><TD class="plfield">';
if ( ($myrow2[6] & 64 ) != 0 )
echo 'X';
else
echo ' ';
echo ' </TD><TD class="plfield">';
if ( ($myrow2[6] & 32) != 0 )
echo 'X';
else
echo ' ';
echo ' </TD><TD class="plfield">';
if ( ($myrow2[6] & 16 ) != 0 )
echo 'X';
else
echo ' ';
echo ' </TD><TD class="plfield">';
if ( ($myrow2[6] & 8) != 0 )
echo 'X';
else
echo ' ';
echo ' </TD><TD class="plfield">';
if ( ($myrow2[6] & 4 ) != 0 )
echo 'X';
else
echo ' ';
echo ' </TD><TD class="plfield">';
if ( ($myrow2[6] & 2 ) != 0 )
echo 'X';
else
echo ' ';
echo ' </TD><TD class="plfield">';
if ( ($myrow2[6] & 1 ) != 0 )
echo 'X';
else
echo ' ';
echo ' </TD>';
echo ' <TD class="plfield">'.$myrow2[2].'</TD>';
echo ' <TD class="plfield">'.$myrow2[3].'</TD>';
/* data offset is in 32 bit words, cf. RFC 793, 3.1 (= p. 16),
* PrintTCPHeader() in snort-2.6.0/src/log.c
* DecodeTCP() in snort-2.6.0/src/decode.c
* #define TCP_OFFSET(tcph) in snort-2.6.0/src/decode.h
* Database() in snort-2.6.0/src/output-plugins/spo_database.c */
echo ' <TD class="plfield">'. ($myrow2[4] << 2) .'</TD>';
echo ' <TD class="plfield">'.$myrow2[5].'</TD>';
echo ' <TD class="plfield">'.$myrow2[7].'</TD>';
echo ' <TD class="plfield">'.$myrow2[9].'</TD>';
echo ' <TD class="plfield">'.$myrow2[8].'<BR>=<BR>0x'.dechex($myrow2[8]).'</TD></TR>';
echo ' </TABLE></TR>';
echo ' <TR>';
echo ' <TD>';
echo ' <TABLE BORDER=1 CELLPADDING=4>';
echo ' <TR><TD CLASS="layer4title" ALIGN=CENTER ROWSPAN='.(($num_opt != 0) ? ($num_opt+1) : 1).'>'._OPTIONS.'</TD>';
if ( $num_opt != 0)
{
echo ' <TD></TD>
<TD class="plfieldhdr">'._CODE.'</TD>
<TD class="plfieldhdr">'._LENGTH.'</TD>
<TD class="plfieldhdr">'._DATA.'</TD>';
/* Check which kind of encoding is used: */
$sql4 = 'SELECT encoding FROM sensor WHERE sid='.$sid;
$result4 = $db->baseExecute($sql4);
$myrow4 = $result4->baseFetchRow();
$result4->baseFreeRows();
for ( $i = 0; $i < $num_opt; $i++)
{
$myrow3 = $result3->baseFetchRow();
echo ' <TR><TD class="plfield">#'.($i+1).'</TD>';
echo ' <TD class="plfield">'.TCPOption2str($myrow3[4]).'</TD>';
echo ' <TD class="plfield">'.$myrow3[5].'</TD>';
echo ' <TD class="plfield">';
if ($myrow4[0] == 1)
/* base64 encoding */
{
if ($myrow3[5] > 0)
{
$mystr = bin2hex(base64_decode($myrow3[6]));
for ($j = 0; $j < $myrow3[5] * 2; $j = $j + 2)
{
echo $mystr[$j];
echo $mystr[$j + 1];
echo ' ';
}
echo '<BR>';
if (TCPOption2str($myrow3[4]) == "(8) TS")
/* timestamp: cf. RFC 1323, 3.2 */
{
/* TSval */
$tmpstr = "";
for ($j = 0; $j < 8; $j++)
{
$tmpstr = $tmpstr . $mystr[$j];
}
$TSval = hexdec($tmpstr);
echo ' TSval: ' . $TSval . '<BR>';
/* TSecr */
$tmpstr = "";
for ($j = 8; $j < 16; $j++)
{
$tmpstr = $tmpstr . $mystr[$j];
}
$TSecr = hexdec($tmpstr);
echo ' TSecr: ' . $TSecr . '<BR>';
}
echo ' </TD></TR>';
}
else
{
echo '{No data}</TD></TR>';
}
}
else
{
/* hexadecimal encoding (and ASCII) */
if ($myrow3[6] != "" )
echo $myrow3[6];
else
echo ' ';
echo '</TD></TR>';
}
}
}
else
{
echo ' <TD class="plfield"> <I>'._NONE.' </I></TD></TR>';
}
echo ' </TABLE></TD></TR>';
echo '</TABLE>';
$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();
echo '
<TABLE BORDER=1 WIDTH="90%">
<TR><TD CLASS="layer4title" WIDTH=50 ROWSPAN=2 ALIGN=CENTER>UDP</TD>';
echo ' <TD>';
echo ' <TABLE BORDER=1 CELLPADDING=2>';
echo ' <TR><TD class="plfieldhdr">'._SOURCEPORT.'</TD>
<TD class="plfieldhdr">'._DESTPORT.'</TD>
<TD class="plfieldhdr">'._LENGTH.'</TD></TR>';
$src_port = $myrow2[2].'<BR>';
foreach ($external_port_link as $name => $baseurl) {
$src_port = $src_port.'[<A HREF="'.$baseurl.$myrow2[2].'" TARGET="_ACID_PORT_">'.$name.'</A>] ';
}
$dst_port = $myrow2[3].'<BR>';
foreach ($external_port_link as $name => $baseurl) {
$dst_port = $dst_port.'[<A HREF="'.$baseurl.$myrow2[3].'" TARGET="_ACID_PORT_">'.$name.'</A>] ';
}
echo ' <TR><TD class="plfield">'.$src_port.'</TD>';
echo ' <TD class="plfield">'.$dst_port.'</TD>';
echo ' <TD class="plfield">'.$myrow2[4].'</TD></TR>';
echo ' </TABLE></TD></TR>';
echo '</TABLE>';
$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();
$ICMPitype = $myrow2[0];
$ICMPicode = $myrow2[1];
echo '
<TABLE BORDER=1 WIDTH="90%">
<TR><TD class="layer4title" WIDTH=50 ROWSPAN=2 ALIGN=CENTER>ICMP';
echo ' <TD>';
echo ' <TABLE BORDER=1 CELLPADDING=2>';
echo ' <TR><TD class="plfieldhdr">'._TYPE.'</TD>
<TD class="plfieldhdr">'._CODE.'</TD>
<TD class="plfieldhdr">checksum</TD>';
if ($ICMPitype == "5") {
echo ' <TD class="plfieldhdr">gateway address</TD>';
echo ' <TD class="plfieldhdr">gateway hostname</TD>';
} else {
echo ' <TD class="plfieldhdr">'._ID.'</TD>
<TD class="plfieldhdr">seq #</TD>';
}
echo ' </TR>';
echo ' <TR><TD class="plfield">('.$myrow2[0].') '.ICMPType2str($myrow2[0]).'</TD>';
echo ' <TD class="plfield">('.$myrow2[1].') '.ICMPCode2str($myrow2[0],$myrow2[1]).'</TD>';
echo ' <TD class="plfield">'.$myrow2[2].'<BR>=<BR>0x'. dechex($myrow2[2]) .'</TD>';
if ($ICMPitype == "5") {
$gateway_numeric_ip = (integer)($myrow2[3] / 256) . "." . ($myrow2[3] % 256) . ".". (integer)($myrow2[4] / 256) . "." . ($myrow2[4] % 256);
$gateway_hostname = basegetHostByAddr($gateway_numeric_ip, $db, $dns_cache_lifetime);
echo ' <TD class="plfield"><A HREF="base_stat_ipaddr.php?ip=' . $gateway_numeric_ip . '&netmask=32" TARGET="_PL_SIP">' . $gateway_numeric_ip . '</A></TD>';
echo ' <TD class="plfield">' . $gateway_hostname . '</TD>';
} else {
echo ' <TD class="plfield">'.$myrow2[3].'</TD>';
echo ' <TD class="plfield">'.$myrow2[4].'</TD>';
}
echo ' </TR>';
echo ' </TABLE>';
echo '</TABLE>';
$result2->baseFreeRows();
}
/* Print the Payload */
$sql2 = "SELECT data_payload FROM data WHERE sid='".$sid."' AND cid='".$cid."'";
$result2 = $db->baseExecute($sql2);
$myrow2 = $result2->baseFetchRow();
$result2->baseFreeRows();
!empty($myrow2) ? $payload = $myrow2[0] : '';
echo '
<TABLE BORDER=1 WIDTH="90%">
<TR><TD class="payloadtitle" WIDTH=50 ROWSPAN=2 ALIGN=CENTER>Payload';
echo("<br><br>".PrintCleanURL());
echo("<br>".PrintBinDownload($db, $cid, $sid));
if ($sf_portscan_flag != 1)
{
echo("<br>".PrintPcapDownload($db, $cid, $sid));