File: psad

package info (click to toggle)
psad 2.4.3-1.2~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,884 kB
  • sloc: perl: 13,751; ansic: 1,322; sh: 342; makefile: 74
file content (11465 lines) | stat: -rwxr-xr-x 418,541 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
#!/usr/bin/perl -w
#
################################################################################
#
# File: psad (/usr/sbin/psad)
#
# URL: http://www.cipherdyne.org/psad/
#
# Purpose: psad makes use of iptables logs to detect port scans,
#          probes for backdoors and DDoS tools, and other suspect traffic
#          (many signatures were adapted from the SNORT intrusion
#          detection system).  Data is provided by parsing syslog
#          firewall messages out of /var/log/messages (or wherever syslog
#          is configured to write iptables logs to).
#
#          For more information read the psad man page or view the
#          documentation provided at: http://www.cipherdyne.org/psad/
#
# Author: Michael Rash (mbr@cipherdyne.org)
#
# Credits: (see the CREDITS file bundled with the psad sources.)
#
# Version: 2.4.3
#
# Copyright (C) 1999-2015 Michael Rash (mbr@cipherdyne.org)
#
# Reference: SNORT is a registered trademark of Sourcefire, Inc.
#
# License (GNU Public License):
#
#    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
#
# TODO: (see the TODO file bundled with the psad sources.)
#
# Default behavior is as follows.  Each of these features can be disabled
# with command line arguments:
#
#       - passive OS fingerprinting                   = yes
#       - snort rule matching                         = yes
#       - write fw errors to error log                = yes
#       - daemon mode                                 = yes
#       - reverse dns lookups                         = yes
#       - validate firewall rules                     = yes
#       - whois lookups of scanning IPs               = yes
#       - parse netstat output for local server ports = yes
#
# Coding Style:  All configuration variables from psad.conf are stored in
#   the %config hash by keys that are in capital letters.  This is
#   the only place in the code where capital letters will be used in
#   variables names.  There are several variables with file-scope, and
#   these variables are clearly commented near the top of each of the
#   psad daemons.  Lines are generally limited to 80 characters for easy
#   reading.
#
# Scan hash key explanation:
#   absnum        - Total number of packets from $src to $dst
#   tot_protocols - Total number of IP protocols scanned by $src against $dst
#   chain         - iptables chain under which the scan packets appear in the
#                   logs.
#   s_time        - Start time for the first packet seen from src to dst.
#   alerted       - An alert has been sent.
#   pkts          - Number of packets (used for signatures and a packet counter
#                   for the current interval.
#   flags         - Keeps track of tcp flags.
#   sid           - Signature tracking
#   abs_sp        - Absolute starting port.
#   abs_ep        - Absolute ending port.
#   strtp         - Starting port.
#   endp          - Ending port.
#
# Sample iptables log messages:
#
#  Sample tcp packet (rejected by iptables... --log-prefix = "DROP ")
#
#  Mar 11 13:15:52 orthanc kernel: DROP IN=lo OUT= MAC=00:00:00:00:00:00:00:00:
#  00:00:00:00:08:00 SRC=127.0.0.1 DST=127.0.0.1 LEN=60 TOS=0x00 PREC=0x00
#  TTL=64 ID=0 DF PROTO=TCP SPT=44847 DPT=35 WINDOW=32304 RES=0x00 SYN URGP=0
#
#  Sample icmp packet rejected by iptables INPUT chain:
#
#  Nov 27 15:45:51 orthanc kernel: DROP IN=eth1 OUT= MAC=00:a0:cc:e2:1f:f2:00:
#  20:78:10:70:e7:08:00 SRC=192.168.10.20 DST=192.168.10.1 LEN=84 TOS=0x00
#  PREC=0x00 TTL=64 ID=0 DF PROTO=ICMP TYPE=8 CODE=0 ID=61055 SEQ=256
#
#  Sample icmp packet logged through FORWARD chain:
#
#  Aug 20 21:23:32 orthanc kernel: SID365 IN=eth2 OUT=eth1 SRC=192.168.20.25
#  DST=192.168.10.15 LEN=84 TOS=0x00 PREC=0x00 TTL=63 ID=0 DF PROTO=ICMP TYPE=8
#  CODE=0 ID=19467 SEQ=256
#
#  Occasionally the kernel klogd ring buffer must become full since log
#  entries are sometimes generated by a long port scan like this (note
#  there is no 'DPT' field):
#
#  Mar 16 23:50:25 orthanc kernel: DROP IN=lo OUT= MAC=00:00:00:00:00:00:00:
#  00:00:00:00:00:08:00 SRC=127.0.0.1 DST=127.0.0.1 LEN=60 TOS=0x00 PREC=0x00
#  TTL=64 ID=0 DF PROTO=TCP SPT=39935 DINDOW=32304 RES=0x00 SYN URGP=0
#
# Note on iptables tcp log messages:
#
#   iptables reports tcp flags in the following order:
#
#       URG ACK PSH RST SYN FIN
#
# Files specification for /var/log/psad/<srcip> directories:
#
#   psad creates a new directory "/var/log/psad/<src>" for each new <src>
#   from which a scan is detected.  Under this directory several files are
#   created:
#
#       danger_level       - Overall danger level aggregated for all scans.
#       p0f_guess          - Passive OS fingerprint guess for <src>.
#       <src/dst>_whois    - Whois information for <src> (or <dst> if src
#                            is a local IP).
#       <dst>_email_ctr    - Total email alerts sent for <src>.
#       <dst>_email_alert  - The most recent email alert for <dst>.
#       <dst>_packet_ctr   - Packet counters for <dst>.
#       <dst>_signatures   - Signatures detected against <dst>.
#
#   Note that some of the files above contain the destination address since a
#   single source address may scan several destination addresses.
#
###############################################################################
#

### modules used by psad
use File::Copy;
use File::Path;
use IO::Socket;
use IO::Select;
use Socket;
use POSIX;
use IO::Handle;
use Data::Dumper;
use Getopt::Long 'GetOptions';
use strict;

### ========================== main =================================

### set the current version
my $version = '2.4.3';

### default config file for psad (can be changed with
### --config switch)
my $config_file  = '/etc/psad/psad.conf';

### this will be set to either FW_DATA_FILE, ULOG_DATA_FILE
### or IPT_SYSLOG_FILE
my $fw_data_file = '';

### disable debugging by default
my $debug     = 0;
my $debug_sid = 0;  ### debug a specific signature

my $flush_fw = 0;

### build the iptables blocking configuration out of the
### IPT_AUTO_CHAIN variable
my @ipt_config = ();

### main configuration hash
my %config = ();
my $override_config_str = '';

### local subnets
my @local_nets = ();

### fw search string array
my @fw_search = ();

### socket for --fw-block
my $ipt_sock = '';

### commands hash
my %cmds = ();

### main psad data structure; contains ips, port ranges,
### protocol info, tcp flags, etc.
my %scan = ();

### cache scan danger levels
my %scan_dl = ();

### cache scan email counters
my %scan_email_ctrs = ();

### cache executions of external script (only used if
### ENABLE_EXT_SCRIPT_EXEC is set to 'Y');
my %scan_ext_exec = ();

### cache p0f-based passive os fingerprinting information
my %p0f = ();

### cache p0f-based passive os fingerprinting signature information
my %p0f_ipv4_sigs = ();
my %p0f_ipv6_sigs = ();

### cache TOS-based passive os fingerprinting information
my %posf = ();

### cache TOS-based passive os fingerprinting signature information
my %posf_sigs = ();

### cache valid icmp types and corresponding codes
my %valid_icmp_types  = ();
my %valid_icmp6_types = ();

### Cache snort rule messages unless --no-snort-sids switch was
### given.  This is only useful if iptables includes rules
### that log things like "SID123".  "fwsnort"
### (http://www.cipherdyne.org/fwsnort/) will automatically
### build such a ruleset from snort signatures.
my %fwsnort_sigs = ();

### Cache snort classification.config file for class priorities
my %snort_class_dl = ();

### Cache any individual Snort rule priority definitions from
### the snort_rule_dl file
my %snort_rule_dl = ();

### Cache Snort rule reference configuration
my %snort_ref_baseurl = ();

### cache all scan signatures from /etc/psad/signatures file
my %sigs = ();
my %sig_search = ();
my %sig_ip_objs = ();

### cache iptables prefixes
my %ipt_prefixes = ();

### ignore ports
my %ignore_ports = ();

### ignore protocols
my %ignore_protocols = ();

### ignore interfaces
my %ignore_interfaces = ();

### data array used for dshield.org logs
my @dshield_data = ();

### track the last time we sent an alert to dshield.org
my $last_dshield_alert = '';

### calculate how often a dshield alert will be sent
my $dshield_alert_interval = '';

### dshield stats counters
my $dshield_email_ctr = 0;
my $dshield_lines_ctr = 0;

### get the current timezone for dshield (this is calculated
### and re-calculated since the timezone may change).
my $timezone = '';

### get the current year for dshield
my $year = '';

### keep track of how many CHECK_INTERVALS have elapsed; this is
### useful for TOP_SCANS_CTR_THRESHOLD
my $check_interval_ctr = 0;

### track the number of scan IP pairs for MAX_SCAN_IP_PAIRS thresholding
my $scan_ip_pairs = 0;

### %auto_dl holds all ip addresses that should automatically
### be assigned a danger level (or ignored).
my %auto_dl = ();
my %auto_dl_ip_objs = ();
my %auto_assigned_msg = ();
my $PERMANENT = 0;

### cache the source ips that we have automatically blocked
### (if ENABLE_AUTO_IDS == 'Y')
my %auto_blocked_ips = ();

### counter to check psad iptables chains and jump rules
my $iptables_prereq_check = 0;

### cache the addresses we have issued dns lookups against.
my %dns_cache = ();

### cache the addresses we have executed whois lookups against.
my %whois_cache = ();

### cache ports the local machine is listening on (periodically
### updated by get_listening_ports()).
my %local_ports = ();

### cache the ip addresses associated with each interface on the
### local machine.
my %local_ips = ();

### Top attacking statistics
my %top_tcp_ports = ();
my %top_udp_ports = ();
my %top_udplite_ports = ();
my %top_sigs    = ();
my %sig_sources = ();
my %top_sig_counts = ();
my %top_packet_counts = ();
my %local_src = ();

### regex to match IP addresses
my $ipv4_re = qr|(?:[0-2]?\d{1,2}\.){3}[0-2]?\d{1,2}|;  ### IPv4

### IPv6 - full version in ip6tables logs
my $ipv6_re = qr|(?:[a-f0-9]{4}:){7}(?:[a-f0-9]{4})|i;

### ttl values are decremented depending on the number of hops
### the packet has taken before it hits the firewall.  We will
### assume packets will not jump through more than 20 hops on
### average.
my $max_hops = 20;

### initial set of protocol packet counters (may get expanded through
### things like protocol scan detection)
my %protocols = (
    'tcp'     => '',
    'udp'     => '',
    'udplite' => '',
    'icmp'    => '',
    'icmp6'   => ''
);
my %proto_ctrs = ();
my %protocol_strings = ();

### pid file hash
my %pidfiles = ();

### for the IPTables::ChainMgr module
my %ipt_opts = ();

### initialize and scope some default variables (command
### line args can override some default values)
my $sigs_file        = '';
my $posf_file        = '';
my $auto_dl_file     = '';
my $snort_rules_dir  = '';
my $srules_type      = '';
my $cmdline_file     = '';
my $analyze_mode     = 0;
my $analysis_fields  = '';
my $analysis_tokens_ar = [];
my $analysis_match_criteria_ar = [];
my $analyze_mode_auto_block = 0;
my $get_next_rule_id = 0;
my $test_mode        = 0;
my $syslog_server    = 0;
my $kill             = 0;
my $restart          = 0;
my $restrict_ip      = '';
my $restrict_ip_cmdline = '';
my $status_mode      = 0;
my $status_ip        = '';
my $status_min_dl    = 0;
my $status_summary   = 0;
my $fw_list_auto     = 0;
my $fw_block_ip      = '';
my $fw_rm_block_ip   = '';
my $fw_del_chains    = 0;
my $gnuplot_mode     = 0;
my $gnuplot_year     = 0;
my $gnuplot_prev_mon = 0;
my $gnuplot_title    = '';
my $gnuplot_legend_title = '';
my $gnuplot_grayscale = 0;
my $gnuplot_x_label = '';
my $gnuplot_x_range = '';
my $gnuplot_y_label = '';
my $gnuplot_y_range = '';
my $gnuplot_z_label = '';
my $gnuplot_z_range = '';
my $gnuplot_3d   = 0;
my $gnuplot_view = '';
my $gnuplot_sort_style = 'value';
my $gnuplot_graph_style = '';
my $gnuplot_count_type  = '';
my $gnuplot_count_element = -1;
my %gnuplot_cache_uniq = ();
my @gnuplot_data = ();
my $gnuplot_data_file = 'psad_iptables.dat';
my $gnuplot_plot_file = 'psad_iptables.gnu';
my $gnuplot_png_file  = 'psad_iptables.png';
my $gnuplot_file_prefix = '';
my $gnuplot_template_file = '';
my $store_file       = '';
my $gnuplot_interactive = 0;
my $plot_separator   = ', ';  ### default to CSV format for plot data
my $csv_mode         = 0;
my $csv_stdin        = 0;
my $csv_fields       = '';
my $csv_print_uniq   = 0;
my $csv_line_limit   = 0;
my $csv_start_line   = 0;
my $csv_end_line     = 0;
my $csv_regex        = '';
my $csv_neg_regex    = '';
my $csv_have_timestamp = 0;
my $pkts_from_stdin = 0;
my $dump_ipt_policy  = 0;
my $fw_include_ips   = 0;
my $benchmark        = 0;
my $num_packets      = 0;
my $usr1             = 0;
my $hup              = 0;
my $usr1_flag        = 0;
my $hup_flag         = 0;
my $verbose          = 0;
my $print_ver        = 0;
my $help             = 0;
my $dump_conf        = 0;
my $download_sigs    = 0;
my $chk_interval     = 0;
my $log_len          = 23;  ### used in scan_logr()
my $fw_analyze       = 0;
my $fw_file          = '';
my $lib_dir          = '';
my $rm_data_ctr      = 0;
my $analysis_emails  = 0;
my $analysis_whois   = 0;
my $enable_analysis_dns = 0;
my $netstat_lkup_ctr = 0;
my $kmsgsd_started   = 0;
my $warn_msg         = '';
my $die_msg          = '';
my $skip_first_loop  = 1;
my $fw_msg_read_continue = 1;
my $fw_msg_read_do_sleep = 1;
my $fw_read_cmd = '';
my $cmdl_interface  = '';
my $analyze_write_data = 0;
my $local_ips_lkup_ctr = 0;
my $num_hash_marks = 76;  ### for gnuplot output
my $imported_syslog_module = 0;
my $TRUNCATE    = 1;
my $NO_TRUNCATE = 0;
my $s = ''; ### for IO::Select

### these flags are used to disable several features
### in psad if specified from the command line
my $no_snort_sids = 0;
my $no_signatures = 0;
my $no_icmp_types = 0;
my $no_icmp6_types = 0;
my $no_auto_dl    = 0;
my $no_posf       = 0;
my $no_daemon     = 0;
my $no_ipt_errors = 0;
my $no_rdns       = 0;
my $no_whois      = 0;
my $no_netstat    = 0;
my $no_fwcheck    = 0;
my $no_kmsgsd     = 0;
my $no_email_alerts  = 0;
my $no_syslog_alerts = 0;

### tcp option types
my $tcp_nop_type       = 1;
my $tcp_mss_type       = 2;
my $tcp_win_scale_type = 3;
my $tcp_sack_type      = 4;
my $tcp_timestamp_type = 8;

### for EXPECT_TCP_OPTIONS handling
my $found_one_tcp_options_field = 0;

my %tcp_p0f_opt_types = (
    'N' => $tcp_nop_type,
    'M' => $tcp_mss_type,
    'W' => $tcp_win_scale_type,
    'S' => $tcp_sack_type,
    'T' => $tcp_timestamp_type
);

my %ip_options = ();

### for ICMP
my $ICMP_ECHO_REQUEST  = 8;
my $ICMP_ECHO_REPLY    = 0;
my $ICMP6_ECHO_REQUEST = 128;
my $ICMP6_ECHO_REPLY   = 129;

### These are not directly support by psad because they
### do not appear in iptables logs; however, several of
### these options are supported if fwsnort is also running.
my @unsupported_snort_opts = qw(
    pcre
    fragbits
    content-list
    rpc
    byte_test
    byte_jump
    distance
    within
    flowbits
    rawbytes
    regex
    isdataat
    uricontent
    content
    offset
    replace
    resp
    flowbits
    ip_proto
);  ### the ip_proto keyword could be supported, but would require
    ### refactoring parse_NF_pkt_str().

### for Snort signature sp/dp matching
my @port_types = (
    {'sp' => 'norm', 'dp' => 'norm'},
    {'sp' => 'norm', 'dp' => 'neg'},
    {'sp' => 'neg',  'dp' => 'norm'},
    {'sp' => 'neg',  'dp' => 'neg'},
);

### main packet data structure
my %pkt_NF_init = (

    ### data link layer
    'src_mac' => '',
    'dst_mac' => '',
    'intf'    => '',   ### FIXME in and out interfaces?

    ### network layer
    'src'    => '',
    's_obj'  => '',
    'dst'    => '',
    'd_obj'  => '',
    'proto'  => '',
    'ip_len' => -1,
    'ip_opts'  => '',  ### v4 or v6

    ### IPv4
    'ip_id'  => -1,
    'ttl'    => -1,
    'tos'    => '',
    'frag_bit' => 0,

    ### IPv6
    'is_ipv6' => 0,
    'tc'      => -1,
    'hop_limit'  => -1,
    'flow_label' => -1,

    ### ICMP
    'itype'    => -1,
    'icode'    => -1,
    'icmp_seq' => -1,
    'icmp_id'  => -1,

    ### transport layer
    'sp'  => -1,
    'dp'  => -1,
    'win' => -1,
    'flags' => -1,
    'tcp_seq'  => -1,
    'tcp_ack'  => -1,
    'tcp_opts' => '',
    'udp_len'  => -1,

    ### extra fields for psad internals (DShield reporting, fwsnort
    ### sid matching, iptables logging prefixes and chains, etc.)
    'fwsnort_sid'   => 0,
    'fwsnort_rnum'  => 0,
    'fwsnort_estab' => 0,
    'is_topera'     => 0,  ### Topera IPv6 scanner detection, requires --log-ip-options
    'is_masscan'    => 0,  ### Masscan detection, requires --log-tcp-options
    'chain'         => '',
    'log_prefix'    => '',
    'dshield_str'   => '',
    'syslog_host'   => '',
    'timestamp'     => ''
);

my %gnuplot_non_digit_packet_fields = (
    ### 'hashentry' - maps the field to an integer based on whether
    ###               it has been seen before
    ### 'intf2int'  - converts interface number to an integer (e.g. eth0 -> 0)
    ### 'ip2int'    - converts IP address to integer representation

    ### data link layer
    'src_mac' => 'hashentry',
    'dst_mac' => 'hashentry',
    'intf'    => 'intf2int',

    ### network layer
    'src'      => 'ip2int',
    'dst'      => 'ip2int',
    'proto'    => 'proto2int',
    'tos'      => 'hashentry',
    'ip_opts'  => 'hashentry',
    'frag_bit' => 'hashentry',

    ### transport layer
    'flags'    => 'hashentry',
    'tcp_opts' => 'hashentry',

    ### extra fields for psad internals (DShield reporting, fwsnort
    ### sid matching, iptables logging prefixes and chains, etc.)
    'chain'         => 'hashentry',
    'log_prefix'    => 'hashentry',
    'dshield_str'   => 'hashentry',
    'syslog_host'   => 'hashentry',
);
my %gnuplot_non_digit_map = ();
my %ip2int_cache   = ();
my %gnuplot_ip2int = ();

my $EXIT_SUCCESS = 0;
my $EXIT_FAILURE = 1;

### packet parsing return values
my $PKT_ERROR   = 0;
my $PKT_SUCCESS = 1;
my $PKT_IGNORE  = 2;

### icmp header validation
my $BAD_ICMP_TYPE = 1;
my $BAD_ICMP_CODE = 2;

my $SIG_MATCH    = 1;
my $NO_SIG_MATCH = 0;

### header lengths
my $TCP_HEADER_LEN   = 20;  ### excludes options
my $TCP_MAX_OPTS_LEN = 44;
my $UDP_HEADER_LEN   = 8;
my $ICMP_HEADER_LEN  = 4;
my $IP_HEADER_LEN    = 20;  ### excludes options

### save a copy of the command line arguments
my @args_cp = @ARGV;

### handle command line args
&getopt_wrapper();

### Everthing after this point must be executed as root (psad
### only needs root if run in auto-blocking mode; should take
### this into account and drop privileges).
&is_root();

### Import all psad configuration and signatures files
### (psad.conf, posf, signatures, psad_icmp_types,
### and auto_dl), and call setup().
&psad_init();

### check to make sure another psad process is not already running.
&unique_pid($config{'PSAD_PID_FILE'});

### get the ip addresses that are local to this machine
&get_local_ips();

### get the current services running on this machine
&get_listening_ports() unless $no_netstat;

### daemonize psad unless running with --no-daemon or an
### analysis mode
unless ($no_daemon or $debug) {
    my $pid = fork();
    exit 0 if $pid;
    die "[*] $0: Couldn't fork: $!" unless defined $pid;
    POSIX::setsid() or die "[*] $0: Can't start a new session: $!";
}

### write the current pid associated with psad to the psad pid file
&write_pid($config{'PSAD_PID_FILE'});

### write the command line args used to start psad to $cmdline_file
&write_cmd_line(\@args_cp, $cmdline_file)
    unless $debug;

### psad requires that kmsgsd is running to receive any data (unless
### SYSLOG_DAEMON is set to ulogd or psad is configured to acquire data
### from a normal file via IPT_SYSLOG_FILE), so let's start it here for good
### measure (as of 0.9.2 it makes use of the pid files and unique_pid(),
### so we don't have to worry about starting a duplicate copy).  While
### we're at it, start psadwatchd as well.  Note that this is the best
### place to start the other daemons since we just wrote the psad pid
### to PID_FILE above.
my $cmd = '';
unless ($config{'ENABLE_SYSLOG_FILE'} eq 'Y'
        or $no_kmsgsd
        or $config{'SYSLOG_DAEMON'} =~ /ulog/i
        or $kmsgsd_started) {
    $cmd  = $cmds{'kmsgsd'};
    $cmd .= " -c $config_file";
    $cmd .= " -O $override_config_str"
        if $override_config_str;
    open KMSGSD, "| $cmd" or die "[*] Could not execute $cmds{'kmsgsd'}";
    close KMSGSD;
    $kmsgsd_started = 1;
}

unless ($kmsgsd_started) {
    my $pid = &is_running($pidfiles{'kmsgsd'});

    if ($pid) {
        kill 9, $pid unless kill 15, $pid;
    }
    unlink $pidfiles{'kmsgsd'} if -e $pidfiles{'kmsgsd'};
}

unless ($debug or $no_daemon or $config{'ENABLE_PSADWATCHD'} eq 'N') {
    $cmd  = $cmds{'psadwatchd'};
    $cmd .= " -c $config_file";
    $cmd .= " -O $override_config_str"
        if $override_config_str;
    open PSADWATCHD, "| $cmd" or die "[*] Could not ",
        "execute $cmds{'psadwatchd'}";
    close PSADWATCHD;
}

if ($config{'ENABLE_AUTO_IDS'} eq 'Y') {
    ### always flush old rules (the subsequent renew_auto_blocked_ips()
    ### will re-instantiate any that should not have been expired).
    &flush_auto_blocked_ips() if $config{'FLUSH_IPT_AT_INIT'} eq 'Y';

    ### Check to see if psad automatically blocked some IPs from
    ### a previous run.  This feature is most useful for preserving
    ### auto-block rules for IPs after a reboot or after restarting
    ### psad.  (Note that ENABLE_AUTO_IDS is disabled by psad_init()
    ### if we are running on a syslog server or if we are running
    ### in -A mode).
    &renew_auto_blocked_ips();
}

### Install signal handlers for debugging %scan with Data::Dumper,
### and for reaping zombie whois processes.
$SIG{'__WARN__'} = \&warn_handler;
$SIG{'__DIE__'}  = \&die_handler;
$SIG{'USR1'}     = \&usr1_handler;
$SIG{'HUP'}      = \&hup_handler;

if ($config{'ENABLE_DSHIELD_ALERTS'} eq 'Y') {
    $last_dshield_alert = time() unless $last_dshield_alert;
}

### Initialize current time for disk space checking.
my $last_disk_check = time();

if ($config{'IMPORT_OLD_SCANS'} eq 'Y') {
    ### import old scans and counters from /var/log/psad/
    &import_old_scans();
} elsif ($config{'ENABLE_SCAN_ARCHIVE'} eq 'Y') {
    &archive_data();
} else {
    &remove_old_scans();
}

### zero out the packet counter file (the counters
### are all zero at this point anyway unless we
### imported old scans).
&write_global_packet_counters();

### zero out prefix counters
&write_prefix_counters();

### zero out dshield alert stats (note we do this here regardless of
### whether DShield alerting is enabled since if it isn't we will
### just zero out the counters).
&write_dshield_stats();

my $fw_data_file_size  = -s $fw_data_file;
my $fw_data_file_inode = (stat($fw_data_file))[1];

my $fw_data_file_check_ctr = 0;

### main firewall packets array
my @fw_packets = ();

### Get an open filehandle for the main firewall data file FW_DATA_FILE.
my $fwdata_fh = &open_fw_data();

&get_auto_response_domain_sock()
    if $config{'ENABLE_AUTO_IDS'} eq 'Y';

###=========================================================###
######                    MAIN LOOP                      ######
###=========================================================###
MAIN: for (;;) {

    ### for --fw-block <ip>
    my @add_ipt_addrs = ();

    $fwdata_fh = &hup_re_init($fwdata_fh) if $hup_flag;

    &usr1_print_scan() if $usr1_flag;

    ### allow the contents of the fwdata file to be processed only after
    ### the first loop has been executed.
    if ($skip_first_loop) {

        $skip_first_loop = 0;
        unless ($config{'ENABLE_FW_MSG_READ_CMD'} eq 'Y') {
            seek $fwdata_fh,0,SEEK_END;  ### seek to the end of the file
        }
        next MAIN;

    } else {

        ### Get any new packets have been written to syslog
        if ($config{'ENABLE_FW_MSG_READ_CMD'} eq 'Y') {
            if ($s->can_read(.5)) {
                my $syslog_line = <$fwdata_fh>;
                $fwdata_fh->flush();
                push @fw_packets, $syslog_line unless $fw_msg_read_continue;
                $fw_msg_read_do_sleep = 0;
            } else {
                $fw_msg_read_continue = 0;
                $fw_msg_read_do_sleep = 1;
            }

        } else {
            @fw_packets = <$fwdata_fh>;
        }

        if ($config{'ENABLE_AUTO_IDS'} eq 'Y') {

            ### get IP from the domain socket
            my $ipt_add_connection = $ipt_sock->accept();
            if ($ipt_add_connection) {
                @add_ipt_addrs = <$ipt_add_connection>;
            }
        }
    }

    $fwdata_fh = &fw_data_file_check($fwdata_fh)
        unless $config{'ENABLE_FW_MSG_READ_CMD'} eq 'Y';

    ### sleep for the check interval seconds
    if ($config{'ENABLE_FW_MSG_READ_CMD'} eq 'Y') {

        if ($fw_msg_read_do_sleep
                or (($#fw_packets+1) % $config{'FW_MSG_READ_MIN_PKTS'} == 0)) {

            if (@fw_packets) {
                ### Extract data and summarize scan packets, assign danger
                ### level, send email/syslog alerts.
                &check_scan(\@fw_packets);
            }

            &post_scan_processing($#fw_packets+1, \@add_ipt_addrs);
            @fw_packets = ();
        }

        unless (&look_for_process(quotemeta($fw_read_cmd))) {
            &sys_log("firewall logs read command '$fw_read_cmd' " .
                "is not running, restarting");
            close $fwdata_fh;
            $s->remove($fwdata_fh);
            undef $s;
            $fwdata_fh = &open_fw_data();
            $fw_msg_read_continue = 1;
        }

        sleep $config{'CHECK_INTERVAL'} if $fw_msg_read_do_sleep;

    } else {

        if (@fw_packets) {
            ### Extract data and summarize scan packets, assign danger
            ### level, send email/syslog alerts.
            &check_scan(\@fw_packets);
        }

        &post_scan_processing($#fw_packets+1, \@add_ipt_addrs);

        @fw_packets = ();

        ### clearerr() on the $fwdata_fh file handle to be ready
        ### for new packets
        $fwdata_fh->clearerr();

        sleep $config{'CHECK_INTERVAL'};
    }

    $check_interval_ctr++;
    $fw_data_file_check_ctr++;

} ### MAIN

### for completeness
close $fwdata_fh;
exit 0;
###=========================================================###
######                    END MAIN                       ######
###=========================================================###

#=================== BEGIN SUBROUTINES ========================

sub post_scan_processing() {
    my ($num_packets, $add_ipt_addrs_ar) = @_;

    ### log top scans data
    my $do_log = 0;
    if ($config{'TOP_SCANS_CTR_THRESHOLD'} == 0) {
        $do_log = 1;
    } elsif ($check_interval_ctr % $config{'TOP_SCANS_CTR_THRESHOLD'} == 0) {
        $do_log = 1;
    }

    if ($do_log) {
        ### log the top port and signature matches
        &log_top_scans();
    }

    ### Write the number of tcp/udp/icmp packets out
    ### to the global packet counters file
    &write_global_packet_counters();

    ### Write out log prefix counters
    &write_prefix_counters();

    if ($config{'ENABLE_AUTO_IDS'} eq 'Y') {
        ### Timeout any auto-blocked IPs that are past due (need to
        ### check the timeouts against existing IPs in the scan hash
        ### even if new packets are not found).
        &timeout_auto_blocked_ips();

        ### see if we need to add any IP addresses from the domain
        ### socket
        &check_ipt_cmd($add_ipt_addrs_ar) if $#$add_ipt_addrs_ar > -1;
    }

    ### Send logs to dshield in dshield format
    &dshield_email_log() if $config{'ENABLE_DSHIELD_ALERTS'} eq 'Y';

    ### Allow disk space utilization checks to be disabled by
    ### setting DISK_MAX_PERCENTAGE = 0.
    if ($config{'DISK_MAX_PERCENTAGE'} > 0
            and (time() - $last_disk_check) > $config{'DISK_CHECK_INTERVAL'}) {
        $fwdata_fh = &disk_check($fwdata_fh);
    }

    &check_auto_response_sock()
        if $config{'ENABLE_AUTO_IDS'} eq 'Y';

    &notifications($num_packets);

    ### see if we need to timeout any old scans
    &scan_timeouts() if $config{'ENABLE_PERSISTENCE'} eq 'N';

    return;
}

### Keeps track of scanning ip's, increments packet counters,
### keeps track of tcp flags for each scan, test for snort sid
### values in iptables packets (if fwsnort is being used).
sub check_scan() {
    my $fw_packets_ar = shift;

    print STDERR "[+] check_scan()...\n" if $debug;

    if ($config{'ENABLE_DSHIELD_ALERTS'} eq 'Y') {
        ### calculate the timezone offset
        $timezone = sprintf("%.2d", (Timezone())[3]) . ':00';
        $year     = This_Year();
    }

    unless ($no_netstat) {
        ### we don't expect the list of ports the machine is listening
        ### on to change very often.
        if ($netstat_lkup_ctr == 10) {
            &get_listening_ports();
            $netstat_lkup_ctr = 0;
        }
        $netstat_lkup_ctr++;
    }
    ### the local machine ip addresses could change (dhcp, etc.)
    ### but not that often.
    if ($local_ips_lkup_ctr == 30) {
        &get_local_ips();
        $local_ips_lkup_ctr = 0;
    }
    $local_ips_lkup_ctr++;

    my %curr_scan = ();
    my %curr_sigs_dl = ();
    my %curr_sids_dl = ();
    my @err_pkts     = ();
    my %auto_block_regex_match = ();

    my $pkt_ctr = 0;
    my $log_scan_ip_pair_max = 0;

    my $print_scale_factor = &get_scale_factor($#$fw_packets_ar);

    ### loop through all of the packet log messages we have just acquired
    ### from iptables

    PKT: for my $pkt_str (@$fw_packets_ar) {

        ### main packet data structure
        my %pkt = %pkt_NF_init;

        if ($analyze_mode) {
            $pkt_ctr++;
            if ($pkt_ctr % $print_scale_factor == 0) {
                print "[+] Processed $pkt_ctr packets...\n";
            }
        }

        ### main parsing routine for the iptables packet logging message
        my $pkt_parse_rv = &parse_NF_pkt_str(\%pkt, $pkt_str);
        print STDERR Dumper \%pkt if $debug and $verbose;
        if ($pkt_parse_rv == $PKT_ERROR) {
            push @err_pkts, $pkt_str unless $no_ipt_errors;
            next PKT;
        } elsif ($pkt_parse_rv == $PKT_IGNORE) {
            next PKT;
        }

        if ($analyze_mode and $analysis_fields) {
            my ($matched_fields_ar, $gnuplot_comment_str)
                = &ipt_match_criteria(\%pkt, $analysis_tokens_ar,
                        $analysis_match_criteria_ar);
            next PKT unless $#$matched_fields_ar > -1;
        }

        $proto_ctrs{$pkt{'proto'}}++;
        $protocols{$pkt{'proto'}} = '';
        if ($pkt{'proto'} eq 'tcp') {
            $top_tcp_ports{$pkt{'dp'}}++;
        } elsif ($pkt{'proto'} eq 'udp') {
            $top_udp_ports{$pkt{'dp'}}++;
        } elsif ($pkt{'proto'} eq 'udplite') {
            $top_udplite_ports{$pkt{'dp'}}++;
        }

        ### If we made it here then we correctly matched packets
        ### that the firewall logged.
        print STDERR "[+] valid packet: $pkt{'src'} ($pkt{'sp'}) -> ",
            "$pkt{'dst'} ($pkt{'dp'}) $pkt{'proto'}\n" if $debug;

        ### see if we have hit the MAX_SCAN_IP_PAIRS threshold
        if ($config{'MAX_SCAN_IP_PAIRS'} > 0) {
            if ($scan_ip_pairs >= $config{'MAX_SCAN_IP_PAIRS'}) {
                unless (defined $scan{$pkt{'src'}}
                        and defined $scan{$pkt{'src'}}{$pkt{'dst'}}) {
                    print STDERR "[-] excluding $pkt{'src'} -> $pkt{'dst'}, ",
                            "scan IP pairs too high: $scan_ip_pairs\n"
                        if $debug;
                    $log_scan_ip_pair_max = 1;
                    next PKT;
                }
            }
            if (not defined $scan{$pkt{'src'}}) {
                $scan_ip_pairs++;
            } elsif (not defined $scan{$pkt{'src'}}{$pkt{'dst'}}) {
                $scan_ip_pairs++;
            }
        }

        ### track packet counts for this source
        $top_packet_counts{$pkt{'src'}}++;

        if ($config{'HOME_NET'} ne 'any') {
            if ($pkt{'chain'} eq 'INPUT') {
                $local_src{$pkt{'dst'}} = '';
            } elsif ($pkt{'chain'} eq 'OUTPUT') {
                $local_src{$pkt{'src'}} = '';
            } elsif ($pkt{'chain'} eq 'FORWARD') {
                $local_src{$pkt{'src'}} = ''
                    if &is_local($pkt{'src'}, $pkt{'s_obj'});
            }
        }

        ### initialize the danger level to 0 if it is not already defined
        ### (note the same source address might have already scanned a
        ### different destination IP, so the danger level represents the
        ### aggregate danger level).
        unless (defined $scan_dl{$pkt{'src'}}) {
            $scan_dl{$pkt{'src'}} = 0;
            $scan{$pkt{'src'}}{$pkt{'dst'}}{'alerted'} = 0
                if $config{'ALERT_ALL'} eq 'N';
        }

        ### see if we need to assign a danger level according to the auto_dl
        ### file.  The return value is the auto-assigned danger level (or
        ### -1 if there is no auto-assigned danger level.
        unless ($no_auto_dl) {
            my $rv = &assign_auto_danger_level(\%pkt);

            print STDERR "[+] assign_auto_danger_level() returned: $rv\n"
                if $debug;
            if ($rv == 0) {
                print STDERR "[+] ignoring $pkt{'src'} $pkt{'proto'} ",
                    "$pkt{'dp'} scan.\n" if $debug;
                next PKT;
            }
        }

        if ($pkt{'proto'} eq 'icmp') {

            ### validate icmp type and code fields against the official values
            ### in RFC 792.  See %inval_type_code for corresponding signature
            ### message text and danger levels.
            my $type_code_rv = &check_icmp_type(
                    'icmp', \%valid_icmp_types,
                    $pkt{'itype'}, $pkt{'icode'});

            my $update_dl = 0;
            if ($type_code_rv == $BAD_ICMP_TYPE) {

                $scan{$pkt{'src'}}{$pkt{'dst'}}{'icmp'}
                    {'invalid_type'}{$pkt{'itype'}}
                    {$pkt{'chain'}}{'pkts'}++;

                $update_dl = 1;

            } elsif ($type_code_rv == $BAD_ICMP_CODE) {

                $scan{$pkt{'src'}}{$pkt{'dst'}}{'icmp'}
                    {'invalid_code'}{$pkt{'itype'}}{$pkt{'icode'}}
                    {$pkt{'chain'}}{'pkts'}++;

                $update_dl = 1;
            }

            if ($update_dl) {
                if (defined $scan_dl{$pkt{'src'}}) {
                    if ($scan_dl{$pkt{'src'}} < 2) {
                        $scan_dl{$pkt{'src'}} = 2;
                    }
                } else {
                    $scan_dl{$pkt{'src'}} = 2;
                }
            }

        } elsif ($pkt{'proto'} eq 'icmp6') {

            ### validate icmp6 type and code fields against the official values
            ### defined by IANA.  See %inval_type_code for corresponding signature
            ### message text and danger levels.
            my $type_code_rv = &check_icmp_type(
                    'icmp6', \%valid_icmp6_types,
                    $pkt{'itype'}, $pkt{'icode'});

            my $update_dl = 0;
            if ($type_code_rv == $BAD_ICMP_TYPE) {

                $scan{$pkt{'src'}}{$pkt{'dst'}}{'icmp6'}
                    {'invalid_type'}{$pkt{'itype'}}
                    {$pkt{'chain'}}{'pkts'}++;

                $update_dl = 1;

            } elsif ($type_code_rv == $BAD_ICMP_CODE) {

                $scan{$pkt{'src'}}{$pkt{'dst'}}{'icmp6'}
                    {'invalid_code'}{$pkt{'itype'}}{$pkt{'icode'}}
                    {$pkt{'chain'}}{'pkts'}++;

                $update_dl = 1;
            }

            if ($update_dl) {
                if (defined $scan_dl{$pkt{'src'}}) {
                    if ($scan_dl{$pkt{'src'}} < 2) {
                        $scan_dl{$pkt{'src'}} = 2;
                    }
                } else {
                    $scan_dl{$pkt{'src'}} = 2;
                }
            }
        }

        unless ($no_snort_sids) {
            if ($pkt{'fwsnort_sid'}) {

                ### found a fwsnort sid in the packet log message
                my ($dl, $is_sig_match) = &add_fwsnort_sid(\%pkt);

                if ($dl) {
                    $curr_sids_dl{$pkt{'src'}} = $dl;
                } else {
                    ### a signature matched but is supposed
                    ### to be ignored
                    next PKT if $is_sig_match == $SIG_MATCH;
                }

            } else {
                ### attempt to match any tcp/udp/icmp signatures in the
                ### main signatures hash
                unless ($no_signatures) {

                    my ($dl, $is_sig_match) = &match_sigs(\%pkt);

                    print STDERR "    match_sigs() returned DL: $dl\n"
                        if $debug and $verbose;

                    if ($dl) {
                        $curr_sigs_dl{$pkt{'src'}} = $dl;
                    } else {
                        ### a signature matched but is supposed
                        ### to be ignored
                        next PKT if $is_sig_match == $SIG_MATCH;
                    }
                }
            }
        }

        ### note that we send this packet data off to DShield regardless
        ### of whether psad decides that it is associated with a scan so
        ### that DShield can make its own determination
        if ($config{'ENABLE_DSHIELD_ALERTS'} eq 'Y'
                and not $benchmark
                and not $analyze_mode
                and $pkt{'dshield_str'}) {
            if ($pkt{'timestamp'} =~ /^\s*(\w+)\s+(\d+)\s+(\S+)/) {
                my $m_tmp = $1;  ### kludge for Decode_Month() call
                my $month   = Decode_Month($m_tmp);
                my $day     = sprintf("%.2d", $2);
                my $time_24 = $3;
                push @dshield_data, "$year-$month-$day $time_24 " .
                    "$timezone\t$config{'DSHIELD_USER_ID'}\t1" .
                    "\t$pkt{'dshield_str'}\n";
            }
        }

        ### record the absolute starting time of the scan
        unless (defined $scan{$pkt{'src'}}{$pkt{'dst'}}{'s_time'}) {
            if ($analyze_mode) {
                if ($pkt_str =~ /^(.*?)\s+\S+\s+kernel:/) {
                    $scan{$pkt{'src'}}{$pkt{'dst'}}{'s_time'}
                        = &date_time($1);
                } elsif ($pkt_str =~ /^\s*(\S+\s+\S+\s+\S+)/) {
                    $scan{$pkt{'src'}}{$pkt{'dst'}}{'s_time'}
                        = &date_time($1);
                } else {
                    die "[*] Could not extract time from packet: $pkt_str\n",
                        "    Please send a bug report to: ",
                        "mbr\@cipherdyne.org\n";
                }
            } else {
                $scan{$pkt{'src'}}{$pkt{'dst'}}{'s_time'} = time();
            }
        }


        ### increment hash values
        $scan{$pkt{'src'}}{$pkt{'dst'}}{'absnum'}++;

        $scan{$pkt{'src'}}{$pkt{'dst'}}{'tot_protocols'} = 0
            unless defined $scan{$pkt{'src'}}{$pkt{'dst'}}{'tot_protocols'};
        $scan{$pkt{'src'}}{$pkt{'dst'}}{'tot_protocols'}++
            unless defined $scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}};

        $scan{$pkt{'src'}}{$pkt{'dst'}}{'chain'}
            {$pkt{'chain'}}{$pkt{'intf'}}{$pkt{'proto'}}++;

        ### keep track of MAC addresses
        $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{'s_mac'} = $pkt{'src_mac'};
        $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{'d_mac'} = $pkt{'dst_mac'};

        $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{'tot_protocols'} = 0
            unless defined $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{'tot_protocols'};
        $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{'tot_protocols'}++
            unless defined $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}};

        $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'pkts'}++;
        $curr_scan{$pkt{'src'}}{$pkt{'dst'}}
            {$pkt{'proto'}}{'flags'}{$pkt{'flags'}}++ if $pkt{'flags'};

        ### keep track of which syslog daemon reported the message.
        $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{'syslog_host'}
            {$pkt{'syslog_host'}} = '' if $pkt{'syslog_host'};

        $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{'is_topera'}  = $pkt{'is_topera'};
        $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{'is_masscan'} = $pkt{'is_masscan'};

        if ($pkt{'log_prefix'}) {
            ### see if the logging prefix matches the blocking
            ### regex, and if not the IP will not be blocked
            if ($config{'ENABLE_AUTO_IDS'} eq 'Y'
                    and $config{'ENABLE_AUTO_IDS_REGEX'} eq 'Y'
                    and $config{'AUTO_BLOCK_REGEX'} ne 'NONE') {
                ### we require a match
                if (not defined $auto_block_regex_match{$pkt{'src'}}
                        and $pkt{'log_prefix'} =~ /$config{'AUTO_BLOCK_REGEX'}/) {
                    $auto_block_regex_match{$pkt{'src'}} = '';
                }
            }
        } else {
            $pkt{'log_prefix'} = '*noprfx*';
        }

        ### keep track of iptables chain and logging prefix
        $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'chain'}
                {$pkt{'chain'}}{$pkt{'log_prefix'}}++;

        if ($pkt{'proto'} eq 'tcp' or $pkt{'proto'} eq 'udp'
                or $pkt{'proto'} eq 'udplite') {

            ### initialize the start and end port for the scanned port range
            if (not defined $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'strtp'}) {
                ### make sure the initial start port is not too low
                $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'strtp'} = 65535;
                ### make sure the initial end port is not too high
                $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'endp'} = 0;
            }
            unless (defined $scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}
                    and defined $scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'abs_sp'}) {
                ### This is the absolute starting port since the
                ### first packet was detected.  Make sure the initial
                ### start port is not too low
                $scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'abs_sp'} = 65535;
                ### make sure the initial end port is not too high
                $scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'abs_ep'} = 0;
            }

            ### see if the destination port lies outside our current range
            ### and change if needed
            ($curr_scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'strtp'},
                    $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'endp'}) =
                &check_range($pkt{'dp'},
                    $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'strtp'},
                    $curr_scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'endp'});
            ($scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'abs_sp'},
                    $scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'abs_ep'}) =
                &check_range($pkt{'dp'},
                    $scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'abs_sp'},
                    $scan{$pkt{'src'}}{$pkt{'dst'}}{$pkt{'proto'}}{'abs_ep'});

        }

        if ($debug and $verbose) {
            print STDERR "    print Dumper scan hash $pkt{'src'} -> $pkt{'dst'}\n";
            print STDERR Dumper $scan{$pkt{'src'}}{$pkt{'dst'}};
        }

        ### attempt to passively guess the remote operating
        ### system based on the ttl, id, len, window, and tos
        ### fields in tcp syn packets (this technique is based
        ### on the paper "Passive OS Fingerprinting: Details
        ### and Techniques" by Toby Miller).  Also attempt to
        ### fingerprint with a re-implementation of Michal Zalewski's
        ### p0f that only requires iptables log messages
        unless ($no_posf) {
            ### make sure we have not already guessed the OS,
            ### and if we have been unsuccessful in guessing
            ### the OS after 100 packets don't keep trying.
            if ($pkt{'proto'} eq 'tcp' and $pkt{'flags'} =~ /SYN/) {
                if ($pkt{'tcp_opts'}) {  ### got the tcp options portion of the header

                    ### p0f based fingerprinting
                    &p0f(\%pkt);

                } elsif (not defined $posf{$pkt{'src'}}{'guess'}
                        and $scan{$pkt{'src'}}{$pkt{'dst'}}{'absnum'} < 100) {
                    &posf(\%pkt);
                }
            }
        }

        %pkt = ();
    }

    ### write bogus packets to the error log.
    if ($benchmark) {
        print scalar localtime(), ' [+] Err packets: ' .
            ($#err_pkts+1) . ".\n";
    } else {
        &collect_errors(\@err_pkts) unless $no_ipt_errors;
    }

    ### Assign a danger level to the scan
    print "[+] Assigning scan danger levels...\n" if $analyze_mode;
    &assign_danger_level(\%curr_scan, \%curr_sigs_dl, \%curr_sids_dl);

    my $tot_scan_ips = 0;
    if ($analyze_mode) {
        for (my $dl=1; $dl <= 5; $dl++) {
            my $num_ips = 0;
            for my $src (keys %curr_scan) {
                $num_ips++ if $scan_dl{$src} == $dl;
            }
            $tot_scan_ips += $num_ips;
            print "    Level $dl: $num_ips IP addresses\n";
        }
        print "\n    Tracking $tot_scan_ips total IP addresses\n";
    }

    ### display the scan analysis
    &print_scan_status() if $analyze_mode;

    ### log scan data to the filesystem
    &scan_logr(\%curr_scan);

    ### remember that ENABLE_AUTO_IDS may have been set to 'N' if we
    ### are running on a syslog server, of if we are running in -A mode.
    &auto_psad_response(\%curr_scan, \%auto_block_regex_match)
        if $config{'ENABLE_AUTO_IDS'} eq 'Y'
            and (not $analyze_mode or $analyze_mode_auto_block);

    if ($config{'ENABLE_AUTO_IDS'} eq 'Y' and $analyze_mode_auto_block) {
        sleep $config{'AUTO_BLOCK_TIMEOUT'} + 1;
        &timeout_auto_blocked_ips();
    }

    if ($log_scan_ip_pair_max) {
        &sys_log("scan IP pairs threshold reached");
    }

    return;
}

sub parse_NF_pkt_str() {
    my ($pkt_hr, $pkt_str) = @_;

    my $is_ipv6    = 0;
    my $is_tcp     = 0;
    my $is_udp     = 0;
    my $is_udplite = 0;
    my $is_icmp    = 0;
    my $is_icmp6   = 0;
    my $proto_num  = -1;
    my $proto_str  = '';
    my $is_proto_num = 0;

    ### with ENABLE_SYSLOG_FILE enabled, psad sees all sorts of syslog
    ### messages that aren't just from iptables (kmsgsd is not running
    ### to filter them), so require a preliminary match
    if ($config{'ENABLE_SYSLOG_FILE'} eq 'Y') {
        return $PKT_IGNORE unless $pkt_str =~ /IN=.*OUT=/;
    }

    print STDERR "\n", $pkt_str if $debug;

    $pkt_hr->{'raw'} = $pkt_str if $csv_mode or $gnuplot_mode;

    ### see if there is a logging prefix (used for scan email alert even
    ### if we are running with FW_SEARCH_ALL = Y).  Note that sometimes
    ### there is a buffering issue in the kernel ring buffer that is used
    ### to hold the iptables log message, so we want to get only the
    ### very last possible candidate for the log prefix (this is why the
    ### "kernel:" string is preceded by .*).
    if ($pkt_str =~ /.*kernel:\s+(.*?)\s*IN=/) {
        $pkt_hr->{'log_prefix'} = $1;
        $pkt_hr->{'log_prefix'} =~ s|\[\s*\d+\.\d+\s*\]\s*||
            if ($config{'IGNORE_KERNEL_TIMESTAMP'} eq 'Y');
        if ($pkt_hr->{'log_prefix'} =~ /\S/) {
            if ($config{'IGNORE_LOG_PREFIXES'} ne 'NONE') {
                return $PKT_IGNORE if $pkt_hr->{'log_prefix'}
                        =~ m|$config{'IGNORE_LOG_PREFIXES'}|;
            }
            $ipt_prefixes{$pkt_hr->{'log_prefix'}}++;
        }
    }

    ### get the in/out interface and iptables chain (the code below
    ### allows the iptables log message to contain the PHYSDEV stuff):
    ### Feb 25 12:13:27 bridge kernel: INBOUND TCP: IN=br0 PHYSIN=eth0 OUT=br0
    ### PHYSOUT=eth1 SRC=63.147.183.21 DST=11.11.79.100 LEN=48 TOS=0x00
    ### PREC=0x00 TTL=113 ID=19664 DF PROTO=TCP SPT=4918 DPT=135 WINDOW=64240
    ### RES=0x00 SYN URGP=0
    ### Note the lack of whitespace requirement before the IN= interface
    ### because the logging prefix might not have contained it.
    if ($pkt_str =~ /IN=(\S+)\s+PHYSIN=.*?\sOUT=\s/
            or $pkt_str =~ /IN=(\S+).*?\sOUT=\s/) {
        $pkt_hr->{'intf'}  = $1;
        $pkt_hr->{'chain'} = 'INPUT';
    } elsif ($pkt_str =~ /IN=(\S+)\s+PHYSIN=.*?\sOUT=\S/
            or $pkt_str =~ /IN=(\S+).*?\sOUT=\S/) {
        $pkt_hr->{'intf'}  = $1;
        $pkt_hr->{'chain'} = 'FORWARD';
    } elsif ($pkt_str =~ /IN=\s+PHYSIN=.*?\sOUT=(\S+)/
            or $pkt_str =~ /IN=\s+OUT=(\S+)/) {
        $pkt_hr->{'intf'}  = $1;
        $pkt_hr->{'chain'} = 'OUTPUT';
    }

    ### -I was used on the command line to require a specific interface
    if ($cmdl_interface) {
        return $PKT_IGNORE unless $pkt_hr->{'intf'} eq $cmdl_interface;
    }

    if ($pkt_str =~ /\sMAC=(\S+)/) {
        my $mac_str = $1;
        if ($mac_str =~ /^((?:\w{2}\:){6})((?:\w{2}\:){6})/) {
            $pkt_hr->{'dst_mac'} = $1;
            $pkt_hr->{'src_mac'} = $2;
        }
    }
    if ($pkt_hr->{'src_mac'}) {
        $pkt_hr->{'src_mac'} =~ s/:$//;
        print STDERR "[+] src mac addr: $pkt_hr->{'src_mac'}\n" if $debug;
    }
    if ($pkt_hr->{'dst_mac'}) {
        $pkt_hr->{'dst_mac'} =~ s/:$//;
        print STDERR "[+] dst mac addr: $pkt_hr->{'dst_mac'}\n" if $debug;
    }

    unless ($pkt_hr->{'intf'} and $pkt_hr->{'chain'}) {
        print STDERR "[-] err packet: could not determine ",
            "interface and chain.\n" if $debug;
        return $PKT_ERROR;
    }

    if (%ignore_interfaces) {
        for my $ignore_intf (keys %ignore_interfaces) {
            return $PKT_IGNORE if $pkt_hr->{'intf'} eq $ignore_intf;
        }
    }

    $pkt_hr->{'syslog_host'} = '';

    ### get the syslog logging host and timestamp for this packet
    if ($config{'ENABLE_CUSTOM_SYSLOG_TS_RE'} eq 'Y') {
        if ($pkt_str =~ /$config{'CUSTOM_SYSLOG_TS_RE'}/) {
            $pkt_hr->{'timestamp'}   = $1;
            $pkt_hr->{'syslog_host'} = $2;
        }
    } else {
        if ($pkt_str =~ /^\s*((?:\S+\s+){2}\S+)\s+(\S+)\s+kernel\:/) {
            $pkt_hr->{'timestamp'}   = $1;
            $pkt_hr->{'syslog_host'} = $2;
        } elsif ($pkt_str =~ /^\s*([\d\-]+T(?:\d{2}\:){2}\d{2}\S+)\s+(\S+)\s+kernel\:/) {
            ### 2015-03-08T02:25:11.444012+02:00 servername kernel: ...
            $pkt_hr->{'timestamp'}   = $1;
            $pkt_hr->{'syslog_host'} = $2;
        }
    }

    unless ($pkt_hr->{'syslog_host'}) {
        $pkt_hr->{'timestamp'}   = localtime();
        $pkt_hr->{'syslog_host'} = 'unknown';
    }

    if ($debug) {
        print "[+] timestamp: $pkt_hr->{'timestamp'}\n",
            "    syslog hostname: $pkt_hr->{'syslog_host'}\n";
    }

    ### try to extract a snort sid (generated by fwsnort) from
    ### the packet
    unless ($no_snort_sids) {
        if ($pkt_hr->{'log_prefix'}) {

            if ($pkt_hr->{'log_prefix'} =~ /$config{'SNORT_SID_STR'}(\d+)/) {
                $pkt_hr->{'fwsnort_sid'} = $1;

                ### try to extract the fwsnort rule number (must be
                ### fwsnort-0.9.0 or greater)
                if ($pkt_hr->{'log_prefix'} =~ /\[(\d+)\]/) {
                    $pkt_hr->{'fwsnort_rnum'} = $1;
                }

                if ($pkt_hr->{'log_prefix'} =~ /ESTAB/) {
                    $pkt_hr->{'fwsnort_estab'} = 1;
                }
            }
        }
    }

    unless ($pkt_hr->{'fwsnort_sid'} or $config{'FW_SEARCH_ALL'} eq 'Y') {
        ### note that this is not _too_ strict since people
        ### have different ways of writing --log-prefix strings
        my $matched = 0;
        for my $fw_search_str (@fw_search) {
            $matched = 1 if $pkt_str =~ /$fw_search_str/;
        }
        return $PKT_IGNORE unless $matched;
    }

    ### test for IPv6
    if ($pkt_str =~ /HOPLIMIT=\d+\s+FLOWLBL=\d+/) {
        return $PKT_IGNORE unless $config{'ENABLE_IPV6_DETECTION'} eq 'Y';
        $is_ipv6 = 1;
        $pkt_hr->{'is_ipv6'} = 1;
    }

    ### test for IPv4 "don't fragment" bit
    unless ($is_ipv6) {
        $pkt_hr->{'frag_bit'} = 1 if $pkt_str =~ /\sDF\s+PROTO/;
    }

    ### get IP options if --log-ip-options is used - they appear before the
    ### PROTO= field for either IPv4 or IPv6 packets
    if ($pkt_str =~ /OPT\s+\((\S+)\)\s+PROTO=/) {
        $pkt_hr->{'ip_opts'} = $1;
    }

    if ($is_ipv6 and $pkt_str =~ /PROTO=ICMPv6\s/) {
        ### test for ICMP before TCP or UDP because ICMP destination
        ### unreachable messages can contain embedded TCP/UDP specifics like
        ### so:

        ### Jul 21 19:07:39 minastirith kernel: [1912155.755921] IPv6 Packet
        ### IN=lo OUT= MAC=00:00:00:00:00:00:00:00:00:00:00:00:86:dd
        ### SRC=0000:0000:0000:0000:0000:0000:0000:0001
        ### DST=0000:0000:0000:0000:0000:0000:0000:0001 LEN=107 TC=0
        ### HOPLIMIT=64 FLOWLBL=0 PROTO=ICMPv6 TYPE=1 CODE=4
        ### [SRC=0000:0000:0000:0000:0000:0000:0000:0001
        ### DST=0000:0000:0000:0000:0000:0000:0000:0001 LEN=59 TC=0
        ### HOPLIMIT=64 FLOWLBL=0 PROTO=UDP SPT=35186 DPT=12345 LEN=19 ]

        $is_icmp6  = 1;
        $proto_str = 'icmp6'
    } elsif ($pkt_str =~ /PROTO=ICMP\s/) {
        ### test for ICMP before TCP or UDP because ICMP destination
        ### unreachable messages can contain embedded TCP/UDP specifics like
        ### so:

        ### Sep  8 18:04:26 minastirith kernel: [28241.572876] IN_DROP
        ### IN=wlan0 OUT= MAC=00:1a:9f:91:df:ae:00:12:27:12:0a:a0:12:00
        ### SRC=10.0.0.138 DST=192.168.1.103 LEN=96 TOS=0x00 PREC=0xC0
        ### TTL=254 ID=63642 PROTO=ICMP TYPE=3 CODE=3 [SRC=192.168.1.103
        ### DST=10.0.0.138 LEN=68 TOS=0x00 PREC=0x00 TTL=0 ID=22458 PROTO=UDP
        ### SPT=35080 DPT=33434 LEN=48 ]

        $is_icmp   = 1;
        $proto_str = 'icmp';
    } elsif ($pkt_str =~ /PROTO=TCP\s/) {
        $is_tcp    = 1;
        $proto_str = 'tcp';
    } elsif ($pkt_str =~ /PROTO=UDPLITE\s/) {
        $is_udplite = 1;
        $proto_str  = 'udplite';
    } elsif ($pkt_str =~ /PROTO=UDP\s/) {
        $is_udp = 1;
        $proto_str  = 'udp';
    } elsif ($pkt_str =~ /PROTO=(\d+)\s/) {
        $proto_num = $1;
        $is_proto_num = 1;
    } else {
        print STDERR "[-] err packet: unrecognized protocol\n" if $debug;
        return $PKT_ERROR;
    }

    ### see if we need to ignore this packet based on the
    ### IGNORE_PROTOCOLS config keyword.
    return $PKT_IGNORE if &check_ignore_proto($proto_str,
            $proto_num, $is_proto_num);

    if ($is_tcp) {
        if ($is_ipv6) {

            ### Jul 18 19:19:08 lorien kernel: [ 1835.131574] IPV6 packet IN=lo
            ### OUT= MAC=00:00:00:00:00:00:00:00:00:00:00:00:86:dd
            ### SRC=0000:0000:0000:0000:0000:0000:0000:0001
            ### DST=0000:0000:0000:0000:0000:0000:0000:0001 LEN=72 TC=0
            ### HOPLIMIT=255 FLOWLBL=0 PROTO=TCP SPT=22 DPT=57005 WINDOW=512
            ### RES=0x00 ACK FIN URGP=0

            ### Topera SYN scan with --log-ip-options turned on:
            ### Dec 20 20:10:51 rohan kernel: [  499.178765] DROP IN=eth0 OUT=
            ### MAC=00:1b:f9:76:9c:e4:0a:13:33:3a:33:36:86:ed
            ### SRC=2012:1234:1234:0000:0000:0000:0000:0001
            ### DST=2012:1234:1234:0000:0000:0000:0000:0002 LEN=132 TC=0 HOPLIMIT=64
            ### FLOWLBL=0 OPT ( ) OPT ( ) OPT ( ) OPT ( ) OPT ( ) OPT ( ) OPT ( )
            ### OPT ( ) OPT ( ) PROTO=TCP SPT=26732 DPT=78 WINDOW=8192 RES=0x00 SYN URGP=0 
            if ($pkt_str =~ /SRC=($ipv6_re)\s+DST=($ipv6_re)\s+LEN=(\d+)\s+
                    TC=(\d+)\s+HOPLIMIT=(\d+)\s+FLOWLBL=(\d+)\s+.*PROTO=TCP\s+
                    SPT=(\d+)\s+DPT=(\d+)\s+WINDOW=(\d+)\s+
                    (.*)\s+URGP=/x) {

                ($pkt_hr->{'src'}, $pkt_hr->{'dst'}, $pkt_hr->{'ip_len'},
                    $pkt_hr->{'tc'}, $pkt_hr->{'hop_limit'},
                    $pkt_hr->{'flow_label'}, $pkt_hr->{'sp'}, $pkt_hr->{'dp'},
                    $pkt_hr->{'win'}, $pkt_hr->{'flags'})
                        = ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10);

                $pkt_hr->{'s_obj'} = new6 NetAddr::IP($pkt_hr->{'src'})
                    or return $PKT_ERROR;
                $pkt_hr->{'d_obj'} = new6 NetAddr::IP($pkt_hr->{'dst'})
                    or return $PKT_ERROR;

                if ($pkt_str =~ /(?:OPT\s+\(\s+\)\s+){7,}/) {
                    $pkt_hr->{'is_topera'} = 1;
                    print STDERR "    Topera IPv6 scan\n"
                        if $debug;
                }

            } else {
                print STDERR "[-] err packet: strange IPv6 TCP format\n"
                    if $debug;
                return $PKT_ERROR;
            }

        } else {

            ### May 18 22:21:26 orthanc kernel: DROP IN=eth2 OUT=
            ### MAC=00:60:1d:23:d0:01:00:60:1d:23:d3:0e:08:00 SRC=192.168.20.25
            ### DST=192.168.20.1 LEN=60 TOS=0x10 PREC=0x00 TTL=64 ID=47300 DF
            ### PROTO=TCP SPT=34111 DPT=6345 WINDOW=5840 RES=0x00 SYN URGP=0

            if ($pkt_str =~ /SRC=($ipv4_re)\s+DST=($ipv4_re)\s+LEN=(\d+)\s+TOS=(\S+)
                    \s*.*\s+TTL=(\d+)\s+ID=(\d+)\s*.*\s+PROTO=TCP\s+
                    SPT=(\d+)\s+DPT=(\d+)\s.*\s*WINDOW=(\d+)\s+
                    (.*)\s+URGP=/x) {

                ($pkt_hr->{'src'}, $pkt_hr->{'dst'}, $pkt_hr->{'ip_len'},
                    $pkt_hr->{'tos'}, $pkt_hr->{'ttl'}, $pkt_hr->{'ip_id'},
                    $pkt_hr->{'sp'}, $pkt_hr->{'dp'}, $pkt_hr->{'win'},
                    $pkt_hr->{'flags'})
                        = ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10);

                $pkt_hr->{'s_obj'} = new NetAddr::IP($pkt_hr->{'src'})
                    or return $PKT_ERROR;
                $pkt_hr->{'d_obj'} = new NetAddr::IP($pkt_hr->{'dst'})
                    or return $PKT_ERROR;

            } else {
                print STDERR "[-] err packet: strange IPv4 TCP format\n"
                    if $debug;
                return $PKT_ERROR;
            }
        }

        $pkt_hr->{'proto'} = 'tcp';

        ### the reserve bits are not reported by ulogd, but normal
        ### iptables syslog messages contain them.
        $pkt_hr->{'flags'} =~ s/\s*RES=\S+\s*//;

        ### default to NULL
        $pkt_hr->{'flags'} = 'NULL' unless $pkt_hr->{'flags'};

        if (not $pkt_hr->{'fwsnort_sid'}
                and $config{'IGNORE_CONNTRACK_BUG_PKTS'} eq 'Y' &&
                ($pkt_hr->{'flags'} =~ /ACK/ || $pkt_hr->{'flags'} =~ /RST/)) {

            ### $dp > 1024 && ($pkt_hr->{'flags'} =~ /ACK/ ||

            ### FIXME: ignore TCP packets that have the ACK or RST
            ### bits set (unless we matched a snort sid) since
            ### _usually_ we see these packets as a result of the
            ### iptables connection tracking bug.  Also, note that
            ### no signatures make use of the RST flag.

            print STDERR "[-] err packet: matched ACK or RST flag.\n"
                if $debug;
            return $PKT_IGNORE;
        }

        ### per page 595 of the Camel book, "if /blah1|blah2/"
        ### can be slower than "if /blah1/ || /blah2/
        unless ($pkt_hr->{'flags'} !~ /WIN/ &&
                $pkt_hr->{'flags'} =~ /ACK/ ||
                $pkt_hr->{'flags'} =~ /SYN/ ||
                $pkt_hr->{'flags'} =~ /RST/ ||
                $pkt_hr->{'flags'} =~ /URG/ ||
                $pkt_hr->{'flags'} =~ /PSH/ ||
                $pkt_hr->{'flags'} =~ /FIN/ ||
                $pkt_hr->{'flags'} eq 'NULL') {

            print STDERR "[-] err packet: bad tcp flags.\n" if $debug;
            return $PKT_ERROR;
        }

        ### look for TCP options, but don't pickup IP options if
        ### also --log-ip-options is used (IP options appear before
        ### the PROTO= field).
        if ($pkt_str =~ /URGP=\S+\s+OPT\s+\((\S+)\)/) {
            $pkt_hr->{'tcp_opts'} = $1;
            $found_one_tcp_options_field = 1
                if $config{'EXPECT_TCP_OPTIONS'} eq 'Y';
        }

        if ($pkt_hr->{'flags'}
                and $pkt_hr->{'flags'} eq 'SYN'
                and $found_one_tcp_options_field
                and not $pkt_hr->{'tcp_opts'}) {
            $pkt_hr->{'is_masscan'} = 1;
            print STDERR "    Masscan SYN scan\n"
                if $debug;
        }

        if ($pkt_str =~ /\sSEQ=(\d+)\s+ACK=(\d+)/) {
            $pkt_hr->{'tcp_seq'} = $1;
            $pkt_hr->{'tcp_ack'} = $2;
        }

        ### see if we need to ignore this packet based on the
        ### IGNORE_PORTS config keyword
        return $PKT_IGNORE if &check_ignore_port($pkt_hr->{'dp'},
                $pkt_hr->{'proto'});

        if ($config{'ENABLE_DSHIELD_ALERTS'} eq 'Y'
                and not $benchmark
                and not $analyze_mode) {

            my $dflags = $pkt_hr->{'flags'};
            $dflags =~ s/\s/,/g;

            $pkt_hr->{'dshield_str'} = "$pkt_hr->{'src'}\t$pkt_hr->{'sp'}\t" .
                "$pkt_hr->{'dst'}\t$pkt_hr->{'dp'}\t$pkt_hr->{'proto'}\t" .
                "$dflags";
        }

    } elsif ($is_udp or $is_udplite) {

        if ($is_udplite) {
            $pkt_hr->{'proto'} = 'udplite';
        } else {
            $pkt_hr->{'proto'} = 'udp';
        }

        if ($is_ipv6) {

            ### Jul 21 21:07:39 minastirith kernel: [1912155.755862] IPv6 Packet IN=lo
            ### OUT= MAC=00:00:00:00:00:00:00:00:00:00:00:00:86:dd
            ### SRC=0000:0000:0000:0000:0000:0000:0000:0001
            ### DST=0000:0000:0000:0000:0000:0000:0000:0001 LEN=59 TC=0 HOPLIMIT=64
            ### FLOWLBL=0 PROTO=UDP SPT=35186 DPT=12345 LEN=19 
            if ($pkt_str =~ /SRC=($ipv6_re)\s+DST=($ipv6_re)\s+LEN=(\d+)\s+
                    TC=(\d+)\s+HOPLIMIT=(\d+)\s+FLOWLBL=(\d+)\s+
                    PROTO=UDP(?:LITE)?\s+SPT=(\d+)\s+DPT=(\d+)\s+LEN=(\d+)/x) {

                ($pkt_hr->{'src'}, $pkt_hr->{'dst'}, $pkt_hr->{'ip_len'},
                    $pkt_hr->{'tc'}, $pkt_hr->{'hop_limit'},
                    $pkt_hr->{'flow_label'}, $pkt_hr->{'sp'}, $pkt_hr->{'dp'},
                    $pkt_hr->{'udp_len'}) = ($1,$2,$3,$4,$5,$6,$7,$8,$9);

                $pkt_hr->{'s_obj'} = new6 NetAddr::IP($pkt_hr->{'src'})
                    or return $PKT_ERROR;
                $pkt_hr->{'d_obj'} = new6 NetAddr::IP($pkt_hr->{'dst'})
                    or return $PKT_ERROR;

            } else {
                print STDERR "[-] err packet: strange IPv6 UDP format\n"
                    if $debug;
                return $PKT_ERROR;
            }

        } else {

            ### May 18 22:21:26 orthanc kernel: DROP IN=eth2 OUT=
            ### MAC=00:60:1d:23:d0:01:00:60:1d:23:d3:0e:08:00
            ### SRC=192.168.20.25 DST=192.168.20.1 LEN=28 TOS=0x00 PREC=0x00
            ### TTL=40 ID=47523 PROTO=UDP SPT=57339 DPT=305 LEN=8

            if ($pkt_str =~ /SRC=($ipv4_re)\s+DST=($ipv4_re)\s+LEN=(\d+)\s+TOS=(\S+)
                      \s.*TTL=(\d+)\s+ID=(\d+)\s*.*\s+PROTO=UDP\s+
                      SPT=(\d+)\s+DPT=(\d+)\s+LEN=(\d+)/x) {

                ($pkt_hr->{'src'}, $pkt_hr->{'dst'}, $pkt_hr->{'ip_len'},
                    $pkt_hr->{'tos'}, $pkt_hr->{'ttl'}, $pkt_hr->{'ip_id'},
                    $pkt_hr->{'sp'}, $pkt_hr->{'dp'}, $pkt_hr->{'udp_len'})
                        = ($1,$2,$3,$4,$5,$6,$7,$8,$9);

                $pkt_hr->{'s_obj'} = new NetAddr::IP($pkt_hr->{'src'})
                    or return $PKT_ERROR;
                $pkt_hr->{'d_obj'} = new NetAddr::IP($pkt_hr->{'dst'})
                    or return $PKT_ERROR;

            } else {
                print STDERR "[-] err packet: strange IPv4 UDP format\n"
                    if $debug;
                return $PKT_ERROR;
            }
        }

        ### see if we need to ignore this packet based on the
        ### IGNORE_PORTS config keyword
        return $PKT_IGNORE if &check_ignore_port($pkt_hr->{'dp'},
                $pkt_hr->{'proto'});

        if ($config{'ENABLE_DSHIELD_ALERTS'} eq 'Y'
                and not $benchmark
                and not $analyze_mode) {

            $pkt_hr->{'dshield_str'} = "$pkt_hr->{'src'}\t$pkt_hr->{'sp'}\t" .
                "$pkt_hr->{'dst'}\t$pkt_hr->{'dp'}\t$pkt_hr->{'proto'}";
        }

    } elsif ($is_icmp6 or $is_icmp) {

        $pkt_hr->{'sp'} = $pkt_hr->{'dp'} = 0;

        if ($is_ipv6) {

            ### Jul 18 17:18:19 lorien kernel: [ 1786.520508] IPV6 packet IN=lo
            ### OUT= MAC=00:00:00:00:00:00:00:00:00:00:00:00:86:dd
            ### SRC=0000:0000:0000:0000:0000:0000:0000:0001
            ### DST=0000:0000:0000:0000:0000:0000:0000:0001 LEN=104 TC=0
            ### HOPLIMIT=255 FLOWLBL=0 PROTO=ICMPv6 TYPE=129 CODE=0 ID=14997 SEQ=1 

            if ($pkt_str =~ /SRC=($ipv6_re)\s+DST=($ipv6_re)\s+LEN=(\d+)\s+TC=(\d+)\s+
                    HOPLIMIT=(\d+)\s+FLOWLBL=(\d+)\s+PROTO=ICMPv6\s+TYPE=(\d+)\s+
                    CODE=(\d+)/x) {

                ($pkt_hr->{'src'}, $pkt_hr->{'dst'}, $pkt_hr->{'ip_len'},
                    $pkt_hr->{'tc'}, $pkt_hr->{'hop_limit'},
                    $pkt_hr->{'flow_label'}, $pkt_hr->{'itype'},
                    $pkt_hr->{'icode'}) = ($1,$2,$3,$4,$5,$6,$7,$8);

                $pkt_hr->{'s_obj'} = new6 NetAddr::IP($pkt_hr->{'src'})
                    or return $PKT_ERROR;
                $pkt_hr->{'d_obj'} = new6 NetAddr::IP($pkt_hr->{'dst'})
                    or return $PKT_ERROR;

            } else {
                print STDERR "[-] err packet: strange IPv6 ICMP format\n"
                    if $debug;
                return $PKT_ERROR;
            }

            $pkt_hr->{'proto'} = 'icmp6';

        } else {

            ### Nov 27 15:45:51 orthanc kernel: DROP IN=eth1 OUT= MAC=00:a0:cc:e2:1f:f2:00:
            ### 20:78:10:70:e7:08:00 SRC=192.168.10.20 DST=192.168.10.1 LEN=84 TOS=0x00
            ### PREC=0x00 TTL=64 ID=0 DF PROTO=ICMP TYPE=8 CODE=0 ID=61055 SEQ=256

            if ($pkt_str =~ /SRC=($ipv4_re)\s+DST=($ipv4_re)\s+LEN=(\d+).*
                      TTL=(\d+)\s+ID=(\d+).*PROTO=ICMP\s+TYPE=(\d+)\s+
                      CODE=(\d+)/x) {

                ($pkt_hr->{'src'}, $pkt_hr->{'dst'}, $pkt_hr->{'ip_len'},
                    $pkt_hr->{'ttl'}, $pkt_hr->{'ip_id'}, $pkt_hr->{'itype'},
                    $pkt_hr->{'icode'}) = ($1,$2,$3,$4,$5,$6,$7);

                $pkt_hr->{'s_obj'} = new NetAddr::IP($pkt_hr->{'src'})
                    or return $PKT_ERROR;
                $pkt_hr->{'d_obj'} = new NetAddr::IP($pkt_hr->{'dst'})
                    or return $PKT_ERROR;

            } else {
                print STDERR "[-] err packet: strange IPv4 ICMP format\n"
                    if $debug;
                return $PKT_ERROR;
            }

            $pkt_hr->{'proto'} = 'icmp';

            if ($pkt_hr->{'itype'} == $ICMP_ECHO_REQUEST
                    or $pkt_hr->{'itype'} == $ICMP_ECHO_REPLY) {

                ### we expect the ICMP ID and SEQ fields to be populated
                if ($pkt_str =~ /CODE=(\d+)\s+ID=(\d+)\s+SEQ=(\d+)/) {
                    $pkt_hr->{'icmp_id'}  = $1;
                    $pkt_hr->{'icmp_seq'} = $2;
                } else {
                    return $PKT_ERROR;
                }
            }
        }

        if ($config{'ENABLE_DSHIELD_ALERTS'} eq 'Y'
                and not $benchmark
                and not $analyze_mode) {

            $pkt_hr->{'dshield_str'} = "$pkt_hr->{'src'}\t$pkt_hr->{'itype'}" .
                "\t$pkt_hr->{'dst'}\t$pkt_hr->{'icode'}\t$pkt_hr->{'proto'}";
        }

    } elsif ($is_proto_num) {

        if ($is_ipv6) {

            ### FIXME future

        } else {

            ### Mar 15 20:59:04 linux kernel: [810523.812773] DROP IN=eth0 OUT=
            ### MAC=23:87:fc:c6:24:58:00:21:3f:98:99:78:09:00 SRC=192.168.10.55
            ### DST=192.168.10.1 LEN=20 TOS=0x00 PREC=0x00 TTL=59 ID=3052 PROTO=29

            if ($pkt_str =~ /SRC=($ipv4_re)\s+DST=($ipv4_re)\s+LEN=(\d+).*
                      TTL=(\d+)\s+ID=(\d+).*PROTO=/x) {

                ($pkt_hr->{'src'}, $pkt_hr->{'dst'}, $pkt_hr->{'ip_len'},
                    $pkt_hr->{'ttl'}, $pkt_hr->{'ip_id'}) = ($1,$2,$3,$4,$5);

                $pkt_hr->{'s_obj'} = new NetAddr::IP($pkt_hr->{'src'})
                    or return $PKT_ERROR;
                $pkt_hr->{'d_obj'} = new NetAddr::IP($pkt_hr->{'dst'})
                    or return $PKT_ERROR;
            }

            $pkt_hr->{'proto'} = $proto_num;
        }

    } else {
        ### Sometimes the iptables log entry gets messed up due to
        ### buffering issues so we write it to the error log.
        print STDERR "[-] err packet: no regex match.\n" if $debug;
        return $PKT_ERROR;
    }

    if ($restrict_ip) {
        ### we are looking to analyze packets from a specific IP/subnet
        if ($pkt_hr->{'is_ipv6'}) {
            if ($restrict_ip->version() == 6) {
                return $PKT_IGNORE unless
                    $pkt_hr->{'s_obj'}->within($restrict_ip) or
                    $pkt_hr->{'d_obj'}->within($restrict_ip);
            }
        } else {
            if ($restrict_ip->version() == 4) {
                return $PKT_IGNORE unless
                    $pkt_hr->{'s_obj'}->within($restrict_ip) or
                    $pkt_hr->{'d_obj'}->within($restrict_ip);
            }
        }
    }

    return $PKT_SUCCESS;
}

sub check_ignore_proto() {
    my ($proto_str, $proto_num, $is_proto_num) = @_;

    return 0 unless %ignore_protocols;

    if ($is_proto_num) {
        return 1 if defined $ignore_protocols{$proto_num};
    } else {
        return 1 if defined $ignore_protocols{$proto_str};
    }

    return 0;
}

sub match_sigs() {
    my $pkt_hr = shift;

    my $dl = 0;
    my $is_sig_match = $NO_SIG_MATCH;

    print STDERR "[+] match_sigs()\n" if $debug and $verbose;

    ### always run the IP protocol sigs
    PROTO: for my $proto ($pkt_hr->{'proto'}, 'ip') {

        next PROTO unless defined $sig_search{$proto};

        SRC: for my $src (keys %{$sig_search{$proto}}) {

            print STDERR "[+] match_sigs() pkt src: $pkt_hr->{'src'} within sig src: $src ?..."
                if $debug and $verbose;

            if ($pkt_hr->{'s_obj'}->within($sig_ip_objs{$src})) {
                print STDERR "yes\n"
                    if $debug and $verbose;
            } else {
                print STDERR "no\n"
                    if $debug and $verbose;
                next SRC;
            }

            DST: for my $dst (keys %{$sig_search{$proto}{$src}}) {

                print STDERR "[+] match_sigs() pkt dst: $pkt_hr->{'dst'} within sig dst: $dst ?..."
                    if $debug and $verbose;

                if ($pkt_hr->{'d_obj'}->within($sig_ip_objs{$dst})) {
                    print STDERR "yes\n"
                        if $debug and $verbose;
                } else {
                    print STDERR "no\n"
                        if $debug and $verbose;
                    next DST;
                }

                print STDERR "    Matched sig IP criteria.\n"
                    if $debug and $verbose;

                if ($proto eq 'tcp' or $proto eq 'udp' or $proto eq 'udplite') {

                    TYPE: for my $hr (@port_types) {
                        my $sp_type = $hr->{'sp'};
                        my $dp_type = $hr->{'dp'};

                        next TYPE unless
                            defined $sig_search{$proto}{$src}{$dst}{$sp_type};

                        my $sp_hr = $sig_search{$proto}{$src}{$dst}{$sp_type};

                        SP_S: for my $sp_s (keys %{$sp_hr}) {

                            if ($sp_type eq 'norm') {
                                ### normal match on the starting port value
                                next SP_S unless $pkt_hr->{'sp'} >= $sp_s;
                            }

                            SP_E: for my $sp_e (keys %{$sp_hr->{$sp_s}}) {

                                if ($sp_type eq 'norm') {
                                    ### normal match on the ending port value
                                    next SP_E unless $pkt_hr->{'sp'} <= $sp_e;
                                } else {
                                    ### negative match on the ending port value
                                    ### (note the "or" condition)
                                    next SP_E unless ($pkt_hr->{'sp'} > $sp_e
                                        or $pkt_hr->{'sp'} < $sp_s);
                                }

                                next TYPE unless defined
                                    $sp_hr->{$sp_s}->{$sp_e}->{$dp_type};

                                my $dp_hr = $sp_hr->{$sp_s}->{$sp_e}->{$dp_type};

                                DP_S: for my $dp_s (keys %$dp_hr) {
                                    if ($dp_type eq 'norm') {
                                        next DP_S unless $pkt_hr->{'dp'} >= $dp_s;
                                    }

                                    DP_E: for my $dp_e (keys %{$dp_hr->{$dp_s}}) {
                                        if ($dp_type eq 'norm') {
                                            next DP_E unless $pkt_hr->{'dp'} <= $dp_e;
                                        } else {
                                            ### negative match on the ending port value
                                            ### (note the "or" condition)
                                            next DP_E unless ($pkt_hr->{'dp'} > $dp_e
                                                or $pkt_hr->{'dp'} < $dp_s);
                                        }

                                        ### now we have the set of applicable
                                        ### signatures that match the sip/dip
                                        ### and sp/dp, so match any Snort
                                        ### keywords
                                        my ($dl_tmp, $sig_match_tmp) =
                                                &match_snort_keywords($pkt_hr,
                                                    $dp_hr->{$dp_s}->{$dp_e});

                                        print STDERR "    match_snort_keywords() ",
                                            " return DL: $dl_tmp\n" if $debug;

                                        ### return maximal danger level from all
                                        ### signature matches
                                        $dl = $dl_tmp if $dl_tmp > $dl;
                                        $is_sig_match = $SIG_MATCH
                                            if $sig_match_tmp == $SIG_MATCH;
                                    }
                                }
                            }
                        }
                    }
                } else {
                    ### now we have the set of applicable icmp
                    ### signatures that match the sip/dip
                    my ($dl_tmp, $sig_match_tmp) = &match_snort_keywords(
                            $pkt_hr, $sig_search{$proto}{$src}{$dst});

                    print STDERR "    match_snort_keywords() ",
                        " return DL: $dl_tmp\n" if $debug;
                    ### return maximal danger level from all signature matches
                    $dl = $dl_tmp if $dl_tmp > $dl;
                    $is_sig_match = $SIG_MATCH if $sig_match_tmp == $SIG_MATCH;
                }
            }
        }
    }
    return $dl, $is_sig_match;
}

sub match_snort_keywords() {
    my ($pkt_hr, $sigs_ids_hr) = @_;

    print STDERR "[+] match_snort_keywords()\n" if $debug;

    my $dl = 0;
    my $matched_sig = $NO_SIG_MATCH;

    ### see if all Snort keywords match the packet - the sigs hash has
    ### been restricted to the appropriate protocol
    SIG: for my $sid (keys %$sigs_ids_hr) {

        next SIG unless defined $sigs{$sid};  ### should never happen

        my $sig_hr = $sigs{$sid};

        ### iptables logging messages always include TTL and IP ID
        ### values (at least for ipv4, see
        ### linux/net/ipv4/netfilter/ipt_LOG.c)
        my $dl_tmp = 0;

        my ($rv, $sig_match_rv) = &match_snort_ip_keywords($pkt_hr, $sig_hr);

        if ($sig_match_rv == $SIG_MATCH) {
            $matched_sig = $SIG_MATCH;
            $dl_tmp = $rv;
            if ($rv == 0) {
                next SIG;  ### ignore signature
            }
        } elsif ($sig_match_rv == $NO_SIG_MATCH) {
            ### there were network-layer keywords that did not match
            next SIG unless $rv;
            ### else there were no network-layer keywords so continue on
        }

        $dl = $dl_tmp if $dl_tmp > $dl;

        if ($debug and $debug_sid == $sid) {
            print STDERR "[+] SID: $sid, passed match_snort_ip_keywords() ",
                "tests.\n";
        }

        if ($sig_hr->{'proto'} eq 'tcp') {

            ($rv, $sig_match_rv) = &match_snort_tcp_keywords($pkt_hr, $sig_hr);

            if ($sig_match_rv == $SIG_MATCH) {

                $matched_sig = $SIG_MATCH;
                next SIG if $rv == 0;

                $dl = $rv if $rv > $dl;

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tot_protocols'} = 0
                    unless defined $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tot_protocols'};

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tot_protocols'}++
                    unless defined $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tcp'};

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tcp'}
                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'dp'}
                        = $pkt_hr->{'dp'};

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tcp'}
                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'flags'}
                        = $pkt_hr->{'flags'};

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tcp'}
                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'pkts'}++;

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tcp'}
                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'is_fwsnort'} = 0;

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tcp'}
                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'time'} = time();

                $sig_sources{$sid}{$pkt_hr->{'src'}} = '';
                $top_sigs{$sid}++;
                $top_sig_counts{$pkt_hr->{'src'}}++;
            }

        } elsif ($sig_hr->{'proto'} eq 'udp') {

            ($rv, $sig_match_rv) = &match_snort_udp_keywords($pkt_hr, $sig_hr);

            if ($sig_match_rv == $SIG_MATCH) {

                $matched_sig = $SIG_MATCH;
                next SIG if $rv == 0;

                $dl = $rv if $rv > $dl;

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tot_protocols'} = 0
                    unless defined $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tot_protocols'};

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tot_protocols'}++
                    unless defined $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'udp'};

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'udp'}
                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'dp'}
                         = $pkt_hr->{'dp'};

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'udp'}
                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'pkts'}++;

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'udp'}
                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'is_fwsnort'} = 0;

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'udp'}
                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'time'} = time();

                $sig_sources{$sid}{$pkt_hr->{'src'}} = 0; ### not fwsnort
                $top_sigs{$sid}++;
                $top_sig_counts{$pkt_hr->{'src'}}++;
            }

        } elsif ($sig_hr->{'proto'} eq 'icmp') {

            ($rv, $sig_match_rv) = &match_snort_icmp_keywords($pkt_hr, $sig_hr);

            if ($sig_match_rv == $SIG_MATCH) {

                $matched_sig = $SIG_MATCH;
                next SIG if $rv == 0;

                $dl = $rv if $rv > $dl;

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tot_protocols'} = 0
                    unless defined $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tot_protocols'};

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tot_protocols'}++
                    unless defined $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'icmp'};

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'icmp'}{'sid'}
                    {$sid}{$pkt_hr->{'chain'}}{'pkts'}++;

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'icmp'}{'sid'}
                    {$sid}{$pkt_hr->{'chain'}}{'is_fwsnort'} = 0;

                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'icmp'}{'sid'}
                    {$sid}{$pkt_hr->{'chain'}}{'time'} = time();

                $sig_sources{$sid}{$pkt_hr->{'src'}} = 0; ### not fwsnort
                $top_sigs{$sid}++;
                $top_sig_counts{$pkt_hr->{'src'}}++;
            }

        } elsif ($sig_hr->{'proto'} eq 'icmp6') {
            ### FIXME icmp6 specifc Snort matches?
        }
    }
    return $dl, $matched_sig;
}

sub match_snort_tcp_keywords() {
    my ($pkt_hr, $sig_hr) = @_;

    if ($debug and $debug_sid == $sig_hr->{'sid'}) {
        print STDERR "[+] SID: $sig_hr->{'sid'} match_snort_tcp_keywords()\n";
    }

    if (defined $sig_hr->{'flags'}) {
        unless ($pkt_hr->{'flags'} eq $sig_hr->{'flags'}) {
            if ($debug and $debug_sid == $sig_hr->{'sid'}) {
                print STDERR "[-] SID: $sig_hr->{'sid'} ",
                    "$pkt_hr->{'flags'} != $sig_hr->{'flags'}\n";
            }
            return 0, $NO_SIG_MATCH;
        }
    }

    my $header_len = $IP_HEADER_LEN + $TCP_HEADER_LEN;

    if ($pkt_hr->{'flags'} =~ m|SYN|) {
        ### extend the header length to compensate for TCP options
        $header_len += $TCP_MAX_OPTS_LEN;
    }

    if (defined $sig_hr->{'dsize'} and defined $sig_hr->{'dsize_s'}) {
        return 0, $NO_SIG_MATCH unless &check_sig_int_range(
                ($pkt_hr->{'ip_len'}-$header_len), 'dsize', $sig_hr);
    }

    if (defined $sig_hr->{'psad_dsize'} and defined $sig_hr->{'psad_dsize_s'}) {
        return 0, $NO_SIG_MATCH unless &check_sig_int_range(
                ($pkt_hr->{'ip_len'}-$header_len), 'psad_dsize', $sig_hr);
    }

    if (defined $sig_hr->{'window'} and defined $sig_hr->{'window_s'}) {
        return 0, $NO_SIG_MATCH
            unless &check_sig_int_range($pkt_hr->{'win'}, 'window', $sig_hr);
    }

    if (defined $sig_hr->{'seq'} and defined $sig_hr->{'seq_s'}) {
        return 0, $NO_SIG_MATCH
            unless &check_sig_int_range($pkt_hr->{'tcp_seq'}, 'seq', $sig_hr);
    }

    if (defined $sig_hr->{'ack'} and defined $sig_hr->{'ack_s'}) {
        return 0, $NO_SIG_MATCH
            unless &check_sig_int_range($pkt_hr->{'tcp_ack'}, 'ack', $sig_hr);
    }

    ### matched the signature
    if ($debug) {
        print STDERR "[+] packet matched tcp keywords for sid: ",
            "$sig_hr->{'sid'} (psad_id: $sig_hr->{'psad_id'})\n",
            qq|    "$sig_hr->{'msg'}"\n|;
    }

    return &assign_sid_dl($sig_hr->{'sid'}, $sig_hr->{'dl'}), $SIG_MATCH;
}

sub match_snort_udp_keywords() {
    my ($pkt_hr, $sig_hr) = @_;

    if (defined $sig_hr->{'dsize'} and defined $sig_hr->{'dsize_s'}) {
        return 0, $NO_SIG_MATCH unless &check_sig_int_range(
                ($pkt_hr->{'udp_len'}-$UDP_HEADER_LEN),
                'dsize', $sig_hr);
    }

    if (defined $sig_hr->{'psad_dsize'} and defined $sig_hr->{'psad_dsize_s'}) {
        return 0, $NO_SIG_MATCH unless &check_sig_int_range(
                ($pkt_hr->{'udp_len'}-$UDP_HEADER_LEN),
                'psad_dsize', $sig_hr);
    }

    ### matched the signature
    if ($debug) {
        print STDERR "[+] packet matched udp keywords for sid: ",
            "$sig_hr->{'sid'} (psad_id: $sig_hr->{'psad_id'})\n",
            qq|    "$sig_hr->{'msg'}"\n|;
    }

    return &assign_sid_dl($sig_hr->{'sid'}, $sig_hr->{'dl'}), $SIG_MATCH;
}

sub match_snort_icmp_keywords() {
    my ($pkt_hr, $sig_hr) = @_;

    if (defined $sig_hr->{'dsize'} and defined $sig_hr->{'dsize_s'}) {
        return 0, $NO_SIG_MATCH unless &check_sig_int_range(
                ($pkt_hr->{'ip_len'}-$IP_HEADER_LEN-$ICMP_HEADER_LEN),
                'dsize', $sig_hr);
    }

    if (defined $sig_hr->{'psad_dsize'} and defined $sig_hr->{'psad_dsize_s'}) {
        return 0, $NO_SIG_MATCH unless &check_sig_int_range(
                ($pkt_hr->{'ip_len'}-$IP_HEADER_LEN-$ICMP_HEADER_LEN),
                'psad_dsize', $sig_hr);
    }

    if (defined $sig_hr->{'itype'} and defined $sig_hr->{'itype_s'}) {
        return 0, $NO_SIG_MATCH
            unless &check_sig_int_range($pkt_hr->{'itype'}, 'itype', $sig_hr);
    }
    if (defined $sig_hr->{'icode'} and defined $sig_hr->{'icode_s'}) {
        return 0, $NO_SIG_MATCH
            unless &check_sig_int_range($pkt_hr->{'icode'}, 'icode', $sig_hr);
    }
    if (defined $sig_hr->{'icmp_seq'} and defined $sig_hr->{'icmp_seq_s'}) {
        return 0, $NO_SIG_MATCH
            unless &check_sig_int_range($pkt_hr->{'icmp_seq'}, 'icmp_seq', $sig_hr);
    }
    if (defined $sig_hr->{'icmp_id'} and defined $sig_hr->{'icmp_id_s'}) {
        return 0, $NO_SIG_MATCH
            unless &check_sig_int_range($pkt_hr->{'icmp_id'}, 'icmp_id', $sig_hr);
    }

    ### matched the signature
    if ($debug) {
        print STDERR "[+] packet matched icmp keywords for sid: ",
            "$sig_hr->{'sid'} (psad_id: $sig_hr->{'psad_id'})\n",
            qq|    "$sig_hr->{'msg'}"\n|;
    }

    return &assign_sid_dl($sig_hr->{'sid'}, $sig_hr->{'dl'}), $SIG_MATCH;
}

sub match_snort_ip_keywords() {
    my ($pkt_hr, $sig_hr) = @_;

    if ($pkt_hr->{'is_ipv6'}) {
        ### we need to build IPv6 signature keywords
        return 1, $NO_SIG_MATCH;
    }

    if (defined $sig_hr->{'ttl'} and defined $sig_hr->{'ttl_s'}) {
        return 0, $NO_SIG_MATCH
            unless &check_sig_int_range($pkt_hr->{'ttl'}, 'ttl', $sig_hr);
    }

    if (defined $sig_hr->{'id'} and defined $sig_hr->{'id_s'}) {
        return 0, $NO_SIG_MATCH
            unless &check_sig_int_range($pkt_hr->{'ip_id'}, 'id', $sig_hr);
    }

    if (defined $sig_hr->{'psad_ip_len'} and defined $sig_hr->{'psad_ip_len_s'}) {
        return 0, $NO_SIG_MATCH
            unless &check_sig_int_range($pkt_hr->{'ip_len'},
                    'psad_ip_len', $sig_hr);
    }

    ### to handle the ip_proto keyword parse_NF_pkt_str() would have to be
    ### modified to handle packets besides TCP, UDP, and ICMP.
    return 0, $NO_SIG_MATCH if defined $sig_hr->{'ip_proto'};

    ### handle the sameip keyword
    if (defined $sig_hr->{'sameip'} and $sig_hr->{'sameip'}) {
        return 0, $NO_SIG_MATCH if $pkt_hr->{'intf'} eq 'lo';
        return 0, $NO_SIG_MATCH unless $pkt_hr->{'src'} eq $pkt_hr->{'dst'};
    }

    return 0, $NO_SIG_MATCH
        unless &check_sig_ipopts($pkt_hr->{'ip_opts'}, 'ipopts', $sig_hr);

    if ($sig_hr->{'proto'} eq 'ip') {
        ### signature match
        if ($debug) {
            print STDERR "[+] packet matched ip keywords for sid: ",
                "$sig_hr->{'sid'} (psad_id: $sig_hr->{'psad_id'})\n",
                qq|    "$sig_hr->{'msg'}"\n|;
        }

        $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'ip'}{'sid'}
            {$sig_hr->{'sid'}}{$pkt_hr->{'chain'}}{'pkts'}++;

        $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'ip'}{'sid'}
            {$sig_hr->{'sid'}}{$pkt_hr->{'chain'}}{'is_fwsnort'} = 0;

        $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'ip'}{'sid'}
            {$sig_hr->{'sid'}}{$pkt_hr->{'chain'}}{'time'} = time();

        $sig_sources{$sig_hr->{'sid'}}{$pkt_hr->{'src'}} = 0; ### not fwsnort
        $top_sigs{$sig_hr->{'sid'}}++;
        $top_sig_counts{$pkt_hr->{'src'}}++;

        return &assign_sid_dl($sig_hr->{'sid'}, $sig_hr->{'dl'}), $SIG_MATCH;
    }
    return 1, $NO_SIG_MATCH;
}

sub check_sig_int_range() {
    my ($pkt_val, $keyword, $sig_hr) = @_;

    $pkt_val = 0 if $pkt_val < 0;

    if ($sig_hr->{"${keyword}_neg"}) {
        if ($pkt_val <= $sig_hr->{"${keyword}_e"}
                and $pkt_val >= $sig_hr->{"${keyword}_s"}) {
            if ($debug) {
                if ($verbose or $debug_sid == $sig_hr->{'sid'}) {
                    print STDERR "[-] SID: $sig_hr->{'sid'} failed ",
                        "$keyword test, $pkt_val <= ",
                        qq|$sig_hr->{"${keyword}_e"} and $pkt_val |,
                        qq|>= $sig_hr->{"${keyword}_s"}\n|;
                }
            }
            return 0;
        }
    } else {
        ### normal match
        if ($pkt_val < $sig_hr->{"${keyword}_s"}) {
            if ($debug) {
                if ($verbose or $debug_sid == $sig_hr->{'sid'}) {
                    print STDERR "[-] SID: $sig_hr->{'sid'} failed ",
                        "$keyword test, $pkt_val < ",
                        qq|$sig_hr->{"${keyword}_s"} (range start)\n|;
                }
            }
            return 0;
        }
        if ($pkt_val > $sig_hr->{"${keyword}_e"}) {
            if ($debug) {
                if ($verbose or $debug_sid == $sig_hr->{'sid'}) {
                    print STDERR "[-] SID: $sig_hr->{'sid'} failed ",
                        "$keyword test, $pkt_val > ",
                        qq|$sig_hr->{"${keyword}_e"} (range end)\n|;
                }
            }
            return 0;
        }
    }
    return 1;
}

sub check_sig_ipopts() {
    my ($pkt_val, $keyword, $sig_hr) = @_;

    return 1 unless defined $sig_hr->{$keyword};
    return 0 unless $pkt_val;
    return 1 if $sig_hr->{$keyword} eq 'any';

    my $pkt_opts_hr = &parse_ip_options($pkt_val);

    return 0 unless defined $pkt_opts_hr->{$sig_hr->{$keyword}};
    return 1;
}

sub check_ignore_port() {
    my ($port, $proto) = @_;
    return 0 unless defined $ignore_ports{$proto};
    return &match_port(\%{$ignore_ports{$proto}}, $port);
}

sub match_port() {
    my ($hr, $port) = @_;
    if (defined $hr->{'port'}) {
        return 1 if defined $hr->{'port'}->{$port};
    }
    if (defined $hr->{'range'}) {
        for my $low_port (keys %{$hr->{'range'}}) {
            my $high_port = $hr->{'range'}->{$low_port};
            return 1 if ($port >= $low_port and $port <= $high_port);
        }
    }
    return 0;
}

sub p0f() {
    my $pkt_hr = shift;

    if ($pkt_hr->{'is_ipv6'}) {
        &p0f_ipv6($pkt_hr);
    } else {
        &p0f_ipv4($pkt_hr);
    }
    return;
}

sub p0f_ipv6() {
    my $pkt_hr = shift;
    return;
}

sub p0f_ipv4() {
    my $pkt_hr = shift;

    print STDERR "[+] p0f_ipv4(): $pkt_hr->{'src'} len: $pkt_hr->{'ip_len'}, ",
        "frag_bit: $pkt_hr->{'frag_bit'}, ttl: $pkt_hr->{'ttl'}, ",
        "win: $pkt_hr->{'win'}\n" if $debug;

    # p0f Fingerprint entry format:
    #
    # wwww:ttt:D:ss:OOO...:OS:Version:Subtype:Details
    #
    # wwww     - window size (can be *, %nnn, Snn or Tnn).  The special values
    #            "S" and "T" which are a multiple of MSS or a multiple of MTU
    #            respectively.
    # ttt      - initial TTL
    # D        - don't fragment bit (0 - not set, 1 - set)
    # ss       - overall SYN packet size
    # OOO      - option value and order specification (see below)
    # OS       - OS genre (Linux, Solaris, Windows)
    # Version  - OS Version (2.0.27 on x86, etc)
    # Subtype  - OS subtype or patchlevel (SP3, lo0)
    # details  - Generic OS details
    #
    ### S4:64:1:60:M*,S,T,N,W7:     Linux:2.6:8:Linux 2.6.8 and newer (?)

    my $options_ar = &parse_tcp_options($pkt_hr->{'src'},
            $pkt_hr->{'tcp_opts'});

    unless ($options_ar) {
        print STDERR "[-] Could not fingerprint remote OS.\n" if $debug;
        return;
    }

    my $matched_os = 0;

    ### try to match SYN packet length
    LEN: for my $sig_len (keys %p0f_ipv4_sigs) {
        my $matched_len = 0;
        if ($sig_len eq '*') {  ### len can be wildcarded in pf.os
            $matched_len = 1;
        } elsif ($sig_len =~ /^\%(\d+)/) {
            if (($pkt_hr->{'ip_len'} % $1) == 0) {
                $matched_len = 1;
            }
        } elsif ($pkt_hr->{'ip_len'} == $sig_len) {
            $matched_len = 1;
        }
        next LEN unless $matched_len;

        ### try to match fragmentation bit
        FRAG: for my $test_frag_bit ($pkt_hr->{'frag_bit'}, '*') {  ### don't need "%nnn" check
            next FRAG unless defined $p0f_ipv4_sigs{$sig_len}{$test_frag_bit};

            ### find out for which p0f sigs the TTL is within range
            TTL: for my $sig_ttl (keys %{$p0f_ipv4_sigs{$sig_len}{$test_frag_bit}}) {
                unless ($pkt_hr->{'ttl'} > $sig_ttl - $config{'MAX_HOPS'}
                        and $pkt_hr->{'ttl'} <= $sig_ttl) {
                    next TTL;
                }

                ### match tcp window size
                WIN: for my $sig_win_size (keys
                        %{$p0f_ipv4_sigs{$sig_len}{$test_frag_bit}{$sig_ttl}}) {
                    my $matched_win_size = 0;
                    if ($sig_win_size eq '*') {
                        $matched_win_size = 1;
                    } elsif ($sig_win_size =~ /^\%(\d+)/) {
                        if (($pkt_hr->{'win'} % $1) == 0) {
                            $matched_win_size = 1;
                        }
                    } elsif ($sig_win_size =~ /^S(\d+)/) {
                        ### window size must be a multiple of maximum
                        ### seqment size
                        my $multiple = $1;
                        for my $opt_hr (@$options_ar) {
                            if (defined $opt_hr->{$tcp_p0f_opt_types{'M'}}) {
                                my $mss_val = $opt_hr->{$tcp_p0f_opt_types{'M'}};
                                if ($pkt_hr->{'win'} == $mss_val * $multiple) {
                                    $matched_win_size = 1;
                                }
                            }
                            last;
                        }
                    } elsif ($sig_win_size == $pkt_hr->{'win'}) {
                        $matched_win_size = 1;
                    }

                    next WIN unless $matched_win_size;
                    TCPOPTS: for my $sig_opts (keys %{$p0f_ipv4_sigs{$sig_len}
                            {$test_frag_bit}{$sig_ttl}{$sig_win_size}}) {
                        my @sig_opts = split /\,/, $sig_opts;
                        for (my $i=0; $i<=$#sig_opts; $i++) {
                            ### tcp option order is important.  Check to see if
                            ### the option order in the packet matches the order we
                            ### expect to see in the signature
                            if ($sig_opts[$i] =~ /^([NMWST])/) {
                                my $sig_letter = $1;

                                unless (defined $options_ar->[$i]->
                                        {$tcp_p0f_opt_types{$sig_letter}}) {
                                    next TCPOPTS;  ### could not match tcp option order
                                }

                                ### MSS, window scale, and timestamp have
                                ### specific signatures requirements on values
                                if ($sig_letter eq 'M') {
                                    if ($sig_opts[$i] =~ /M(\d+)/) {
                                        my $sig_mss_val = $1;
                                        next TCPOPTS unless $options_ar->[$i]->
                                            {$tcp_p0f_opt_types{$sig_letter}}
                                                == $sig_mss_val;
                                    } elsif ($sig_opts[$i] =~ /M\%(\d+)/) {
                                        my $sig_mss_mod_val = $1;
                                        next TCPOPTS unless (($options_ar->[$i]->
                                            {$tcp_p0f_opt_types{$sig_letter}}
                                                % $sig_mss_mod_val) == 0);
                                    } ### else it is "M*" which always matches
                                } elsif ($sig_letter eq 'W') {
                                    if ($sig_opts[$i] =~ /W(\d+)/) {
                                        my $sig_win_val = $1;
                                        next TCPOPTS unless $options_ar->[$i]->
                                            {$tcp_p0f_opt_types{$sig_letter}}
                                                == $sig_win_val;
                                    } elsif ($sig_opts[$i] =~ /W\%(\d+)/) {
                                        my $sig_win_mod_val = $1;
                                        next TCPOPTS unless (($options_ar->[$i]->
                                            {$tcp_p0f_opt_types{$sig_letter}}
                                                % $sig_win_mod_val) == 0);
                                    } ### else it is "W*" which always matches
                                } elsif ($sig_letter eq 'T') {
                                    if ($sig_opts[$i] =~ /T0/) {
                                        next TCPOPTS unless $options_ar->[$i]->
                                            {$tcp_p0f_opt_types{$sig_letter}}
                                                == 0;
                                    }  ### else it is just "T" which matches
                                }

                            }
                        }
                        OS: for my $os (keys %{$p0f_ipv4_sigs{$sig_len}
                                {$test_frag_bit}{$sig_ttl}{$sig_win_size}
                                {$sig_opts}}) {
                            my $sig = $p0f_ipv4_sigs{$sig_len}
                                {$test_frag_bit}{$sig_ttl}{$sig_win_size}
                                {$sig_opts}{$os};
                            print STDERR "[+] os: $os, $sig\n" if $debug;
                            $matched_os = 1;
                            $p0f{$pkt_hr->{'src'}}{$os} = '';
                        }
                    }
                }
            }
        }
    }
    if ($debug and not $matched_os) {
        print STDERR "    Could not match $pkt_hr->{'src'} against any p0f signature.\n";
    }
    return;
}

sub parse_tcp_options() {
    my ($src, $tcp_options) = @_;
    my @opts = ();
    my @hex_nums = ();
    my $debug_str = '';

    unless ($tcp_options) {
        print STDERR 'no tcp options' if $debug;
        return [];
    }

    if (length($tcp_options) % 2 != 0) {  ### make sure length is a multiple of two
        print STDERR 'tcp options length not a multiple of two.' if $debug;
        return [];
    }
    ### $tcp_options is a hex string like "020405B401010402" from the iptables
    ### log message
    my @chars = split //, $tcp_options;
    for (my $i=0; $i <= $#chars; $i += 2) {
        my $str = $chars[$i] . $chars[$i+1];
        push @hex_nums, $str;
    }

    my $max_parse_attempts = $#chars;
    my $parse_ctr = 0;

    OPT: for (my $opt_kind=0; $opt_kind <= $#hex_nums;) {

        $parse_ctr++;
        if ($parse_ctr > $max_parse_attempts) {
            print STDERR "    p0f() parse_ctr $parse_ctr > ",
                "max_parse_attempts $max_parse_attempts\n" if $debug;
            return [];
        }

        last OPT unless defined $hex_nums[$opt_kind+1];

        my $is_nop = 0;
        my $len = hex($hex_nums[$opt_kind+1]);
        if (hex($hex_nums[$opt_kind]) == $tcp_nop_type) {
            $debug_str .= 'NOP, ' if $debug;
            push @opts, {$tcp_nop_type => ''};
            $is_nop = 1;
        } elsif (hex($hex_nums[$opt_kind]) == $tcp_mss_type) {  ### MSS
            my $mss_hex = '';
            for (my $i=$opt_kind+2; $i < ($opt_kind+$len); $i++) {
                $mss_hex .= $hex_nums[$i];
            }
            my $mss = hex($mss_hex);
            push @opts, {$tcp_mss_type => $mss};
            $debug_str .= 'MSS: ' . hex($mss_hex) . ', ' if $debug;
        } elsif (hex($hex_nums[$opt_kind]) == $tcp_win_scale_type) {
            my $window_scale_hex = '';
            for (my $i=$opt_kind+2; $i < ($opt_kind+$len); $i++) {
                $window_scale_hex .= $hex_nums[$i];
            }
            my $win_scale = hex($window_scale_hex);
            push @opts, {$tcp_win_scale_type => $win_scale};
            $debug_str .= 'Win Scale: ' . hex($window_scale_hex) . ', ' if $debug;
        } elsif (hex($hex_nums[$opt_kind]) == $tcp_sack_type) {
            push @opts, {$tcp_sack_type => ''};
            $debug_str .= 'SACK, ' if $debug;
        } elsif (hex($hex_nums[$opt_kind]) == $tcp_timestamp_type) {
            my $timestamp_hex = '';
            for (my $i=$opt_kind+2; $i < ($opt_kind+$len) - 4; $i++) {
                $timestamp_hex .= $hex_nums[$i];
            }
            my $timestamp = hex($timestamp_hex);
            push @opts, {$tcp_timestamp_type => $timestamp};
            $debug_str .= 'Timestamp: ' . hex($timestamp_hex) . ', ' if $debug;
        } elsif (hex($hex_nums[$opt_kind]) == 0) {  ### End of option list
            last OPT;
        }
        if ($is_nop) {
            $opt_kind += 1;
        } else {
            if ($len == 0 or $len == 1) {
                ### this should never happen; it indicates a broken TCP stack
                ### or maliciously constructed options since the len field is
                ### not large enough to accomodate the TLV encoding
                my $msg = "broken $len-byte len field within TCP options " .
                    "string: $tcp_options from source IP: $src";
                print STDERR "    $msg\n" if $debug;
                &sys_log($msg);
                return [];
            }
            ### get to the next option-kind field
            $opt_kind += $len;
        }
    }
    if ($debug) {
        $debug_str =~ s/\,$//;
        print STDERR "[+] $debug_str\n" if $debug;
    }
    return \@opts;
}

sub parse_ip_options() {
    my $ip_opts_str = shift;

    my %ip_opts  = ();
    my @hex_nums = ();

    if (length($ip_opts_str) % 2 != 0) {  ### make sure length is a multiple of two
        print STDERR 'IP options length not a multiple of two.' if $debug;
        return '';
    }
    print STDERR "[+] parse_ip_options(): matched " if $debug;

    push @hex_nums, $1 while $ip_opts_str =~ m|(.{2})|g;

    OPT: for (my $i=0; $i <= $#hex_nums; $i++) {
        my $val = hex($hex_nums[$i]);

        for my $rfc_opt_val (keys %ip_options) {
            next unless $val == $rfc_opt_val;
            if ($ip_options{$rfc_opt_val}{'len'} ne '-1') {
                $i += $ip_options{$rfc_opt_val}{'len'}
                    unless $ip_options{$rfc_opt_val}{'len'} == 1;
            } else {
                return \%ip_opts if ($i+1 > $#hex_nums);
                ### subtract out the option and length fields
                my $pkt_opt_len = hex($hex_nums[$i+1]) - 2;
                if ($i + $pkt_opt_len > $#hex_nums) {
                    ### this should not happen unless the IP packet
                    ### was truncated (i.e. the length argument for
                    ### this option is past the IP options portion
                    ### of the header).
                    return \%ip_opts;
                }
                $i += $pkt_opt_len;
            }
            if ($debug) {
                printf STDERR ("$ip_options{$rfc_opt_val}{'sig_keyword'} " .
                    "(0x%x) ", $val) unless defined $ip_opts{$ip_options
                    {$rfc_opt_val}{'sig_keyword'}};
            }
            $ip_opts{$ip_options{$rfc_opt_val}{'sig_keyword'}} = '';
        }
    }
    print STDERR "\n" if $debug;

    return \%ip_opts;
}

sub posf() {
    my $pkt_hr = shift;

    if ($pkt_hr->{'is_ipv6'}) {
        &posf_ipv6($pkt_hr);
    } else {
        &posf_ipv4($pkt_hr);
    }
    return;
}

sub posf_ipv6() {
    my $pkt_hr = shift;
    return;
}

sub posf_ipv4() {
    my $pkt_hr = shift;

    my $src = $pkt_hr->{'src'};
    my $len = $pkt_hr->{'ip_len'};
    my $tos = $pkt_hr->{'tos'};
    my $ttl = $pkt_hr->{'ttl'};
    my $id  = $pkt_hr->{'ip_id'};
    my $win = $pkt_hr->{'win'};

    my $min_ttl;
    my $max_ttl;
    my $id_str;

    $posf{$src}{'len'}{$len}++;
    $posf{$src}{'tos'}{$tos}++;
    $posf{$src}{'ttl'}{$ttl}++;
    $posf{$src}{'win'}{$win}++;
    $posf{$src}{'ctr'}++;
    push @{$posf{$src}{'id'}}, $id;  ### need to maintain ordering

    print STDERR "[+] posf():  $src  LEN: $len, TOS: $tos, TTL: $ttl, ",
        "ID: $id, WIN: $win\n" if $debug;

    $id_str = &id_incr(\@{$posf{$src}{'id'}});
    for my $os (keys %posf_sigs) {
        if ($posf{$src}{'ctr'} >= $posf_sigs{$os}{'numpkts'}) {
            ($min_ttl, $max_ttl) = &ttl_range($posf{$src}{'ttl'});
            if (defined $posf{$src}{'win'}{$posf_sigs{$os}{'win'}}
#                    and defined $posf{$src}{'tos'}{$posf_sigs{$os}{'tos'}}
                    and defined $posf{$src}{'len'}{$posf_sigs{$os}{'len'}}
                    ### ttl's only decrease
                    and ($min_ttl > ($posf_sigs{$os}{'ttl'}-$max_hops))
                    and ($max_ttl <= $posf_sigs{$os}{'ttl'})
                    and $id_str eq $posf_sigs{$os}{'id'}) {
                $posf{$src}{'guess'} = $os;
                print STDERR "[+] posf(): matched OS: $os\n" if $debug;
                return;
            }
        }
    }
    return;
}

sub id_incr() {
    my $aref = shift;
    for (my $i=0; $i<$#$aref; $i++) {
        return 'RANDOM'
            unless ($aref->[$i] < $aref->[$i+1]
                and ($aref->[$i+1] - $aref->[$i]) < 1000);
    }
    return 'SMALLINCR';
}

sub ttl_range() {
    my $hr = shift;
    my $min_ttl = 256;
    my $max_ttl = 0;
    for my $ttl (keys %$hr) {
        $min_ttl = $ttl if $ttl < $min_ttl;
        $max_ttl = $ttl if $ttl > $max_ttl;
    }
    return $min_ttl, $max_ttl;
}

sub assign_sid_dl() {
    my ($sid, $dl) = @_;

    ### see if /etc/psad/snort_rule_dl assigns a DL (may be
    ### zero).
    if (defined $snort_rule_dl{$sid}) {
        $dl = $snort_rule_dl{$sid};
    }

    print STDERR "[+] assign_sid_dl(): snort_rule_dl ",
        "assigning SID $sid a danger level of ",
        "$dl\n" if $debug;

    return $dl;
}

sub add_fwsnort_sid() {
    my $pkt_hr = shift;

    my $sid = $pkt_hr->{'fwsnort_sid'};

    if (defined $fwsnort_sigs{$sid}) {

        ### see if we need to ignore this signature match
        my $dl = &assign_sid_dl($sid, 2);

        unless ($dl) {
            print "[+] add_fwsnort_sid(): ignoring fwsnort signature ",
                "match for SID: $sid (DL=0)\n" if $debug;
            return 0, $SIG_MATCH;
        }

        $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tot_protocols'} = 0
            unless defined $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tot_protocols'};

        $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tot_protocols'}++
            unless defined $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{$pkt_hr->{'proto'}};

        $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{$pkt_hr->{'proto'}}
            {'sid'}{$sid}{$pkt_hr->{'chain'}}{'pkts'}++;

        $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{$pkt_hr->{'proto'}}
            {'sid'}{$sid}{$pkt_hr->{'chain'}}{'is_fwsnort'} = 1;

        $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{$pkt_hr->{'proto'}}
            {'sid'}{$sid}{$pkt_hr->{'chain'}}{'time'} = time();

        $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{$pkt_hr->{'proto'}}
            {'sid'}{$sid}{$pkt_hr->{'chain'}}{'fwsnort_rnum'}
                = $pkt_hr->{'fwsnort_rnum'};

        $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{$pkt_hr->{'proto'}}
            {'sid'}{$sid}{$pkt_hr->{'chain'}}{'fwsnort_estab'}
                = $pkt_hr->{'fwsnort_estab'};

        $sig_sources{$sid}{$pkt_hr->{'src'}} = 1; ### is an fwsnort sid
        $top_sigs{$sid}++;
        $top_sig_counts{$pkt_hr->{'src'}}++;

        if ($pkt_hr->{'proto'} eq 'tcp' or $pkt_hr->{'proto'} eq 'udp') {

            $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{$pkt_hr->{'proto'}}
                {'sid'}{$sid}{$pkt_hr->{'chain'}}{'dp'}
                    = $pkt_hr->{'dp'};

            if ($pkt_hr->{'proto'} eq 'tcp') {
                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{$pkt_hr->{'proto'}}
                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'flags'}
                        = $pkt_hr->{'flags'};
            }
        }

        return $dl, $SIG_MATCH;

    } else {
        print "[-] Found sid: $sid in packet, but no ",
            "corresponding fwsnort rule.\n" if $debug;
    }
    return 0, $NO_SIG_MATCH;
}

sub delete_old_scans() {

    my $current_time = time();

    print STDERR "[+] delete_old_scans()\n" if $debug;

    ### see if we need to timeout any old scans
    for my $src (keys %scan) {
        for my $dst (keys %{$scan{$src}}) {
            next unless defined $scan{$src}{$dst}{'s_time'};
            if (($current_time - $scan{$src}{$dst}{'s_time'})
                    >= $config{'SCAN_TIMEOUT'}) {
                print STDERR "    delete old scan $src -> $dst\n"
                    if $debug;

                delete $scan{$src}{$dst};

                if ($config{'MAX_SCAN_IP_PAIRS'} > 0) {
                    $scan_ip_pairs-- if $scan_ip_pairs > 0;
                }
            }
        }
    }
    return;
}

sub dshield_email_log() {
    ### dshield alert interval is in hours.  Check to see if there are more
    ### than 10,000 lines of log data (and if the last alert was sent more than
    ### two hours later than the previous alert), and if yes send the alert
    ### email.
    if (@dshield_data and ((time() - $last_dshield_alert)
            >= $dshield_alert_interval)
            or (($#dshield_data > 10000)
            and ((time() - $last_dshield_alert) >= 2*3600))) {
        my $dshield_version = $version;
        $dshield_version =~ s/^(\d+\.\d+)\.\d+/$1/;
        $dshield_version =~ s/-pre\d+//;
        my $subject = "FORMAT DSHIELD USERID $config{'DSHIELD_USER_ID'} " .
            "TZ $timezone psad Version $dshield_version";
        if ($config{'DSHIELD_USER_EMAIL'} eq 'NONE') {
            open MAIL, qq(| $cmds{'mail'} -s "$subject" ) .
                $config{'DSHIELD_ALERT_EMAIL'} or die '[*] Could not send ',
                'dshield alert email.';
            ### save this email to disk also
            open DSSAVE, "> $config{'DSHIELD_EMAIL_FILE'}" or die '[*] ',
                "Could not open $config{'DSHIELD_EMAIL_FILE'}: $!";
            if ($config{'DSHIELD_DL_THRESHOLD'} > 0) {
                for my $line (@dshield_data) {
                    if ($line =~ /^.*?($ipv4_re)/) {
                        my $src = $1;
                        if (defined $scan_dl{$src}
                                and ($scan_dl{$src}
                                    >= $config{'DSHIELD_DL_THRESHOLD'})) {
                            print MAIL $line;
                            print DSSAVE $line;
                        }
                    }
                }
            } else {
                print MAIL for @dshield_data;
                print DSSAVE for @dshield_data;
            }
            close MAIL;
            close DSSAVE;
        } else {
            open MAIL, "| $cmds{'sendmail'} -oi -t" or die '[*] Could not ',
                'send dshield alert email.';
            ### save this email to disk also
            open DSSAVE, "> $config{'DSHIELD_EMAIL_FILE'}" or die '[*] ',
                "Could not open $config{'DSHIELD_EMAIL_FILE'}: $!";
            print MAIL "From: $config{'DSHIELD_USER_EMAIL'}\n",
                "To: $config{'DSHIELD_ALERT_EMAIL'}\n",
                "Subject: $subject\n";
            print DSSAVE "From: $config{'DSHIELD_USER_EMAIL'}\n",
                "To: $config{'DSHIELD_ALERT_EMAIL'}\n",
                "Subject: $subject\n";
            if ($config{'DSHIELD_DL_THRESHOLD'} > 0) {
                for my $line (@dshield_data) {
                    if ($line =~ /^.*?($ipv4_re)/) {
                        my $src = $1;
                        if (defined $scan_dl{$src}
                                and ($scan_dl{$src}
                                    >= $config{'DSHIELD_DL_THRESHOLD'})) {
                            print MAIL $line;
                            print DSSAVE $line;
                        }
                    }
                }
            } else {
                print MAIL for @dshield_data;
                print DSSAVE for @dshield_data;
            }
            close MAIL;
            close DSSAVE;
        }

        &sys_log("sent $#dshield_data lines of log data to " .
            $config{'DSHIELD_ALERT_EMAIL'});

        ### store the current time
        $last_dshield_alert = time();

        ### increment stats counters
        $dshield_email_ctr++;
        $dshield_lines_ctr += $#dshield_data;

        ### clear the dshield data array so we don't re-send
        ### any old data.
        @dshield_data = ();

        ### Write Dshield stats to disk
        &write_dshield_stats();
    }
    return;
}

sub check_icmp_type() {
    my ($proto, $valid_types_hr, $type, $code) = @_;
    print STDERR "    check_icmp_type(type: $type, code: $code)\n" if $debug;
    if (not defined $valid_types_hr->{$type}) {
        print STDERR "        bad $proto type\n" if $debug;
        return $BAD_ICMP_TYPE;
    } elsif (not defined $valid_types_hr->{$type}->{'codes'}->{$code}) {
        print STDERR "        bad $proto code\n" if $debug;
        return $BAD_ICMP_CODE;
    }
    print STDERR "        valid $proto type/code\n" if $debug;
    return 0;
}

sub import_perl_modules() {

    my $mod_paths_ar = &get_mod_paths();

    if ($#$mod_paths_ar > -1) {  ### /usr/lib/psad/ exists
        push @$mod_paths_ar, @INC;
        splice @INC, 0, $#$mod_paths_ar+1, @$mod_paths_ar;
    }

    if ($debug) {
        print STDERR "[+] import_perl_modules(): The \@INC array:\n";
        print STDERR "$_\n" for @INC;
    }

    require IPTables::ChainMgr;
    require NetAddr::IP;
    require Date::Calc;
    require Unix::Syslog;
    require Cwd;
    require Storable if $store_file;

    Date::Calc->import(qw(Timezone This_Year Decode_Month
            Today Date_to_Time This_Year Mktime Localtime));
    Unix::Syslog->import(qw(:subs :macros));
    Storable->import(qw(retrieve store)) if $store_file;

    $imported_syslog_module = 1;

    return;
}

sub get_mod_paths() {

    my @paths = ();

    $config{'PSAD_LIBS_DIR'} = $lib_dir if $lib_dir;

    unless (-d $config{'PSAD_LIBS_DIR'}) {
        my $dir_tmp = $config{'PSAD_LIBS_DIR'};
        $dir_tmp =~ s|lib/|lib64/|;
        if (-d $dir_tmp) {
            $config{'PSAD_LIBS_DIR'} = $dir_tmp;
        } else {
            return [];
        }
    }

    opendir D, $config{'PSAD_LIBS_DIR'}
        or die "[*] Could not open $config{'PSAD_LIBS_DIR'}: $!";
    my @dirs = readdir D;
    closedir D;

    push @paths, $config{'PSAD_LIBS_DIR'};

    for my $dir (@dirs) {
        ### get directories like "/usr/lib/psad/x86_64-linux"
        next unless -d "$config{'PSAD_LIBS_DIR'}/$dir";
        push @paths, "$config{'PSAD_LIBS_DIR'}/$dir"
            if $dir =~ m|linux| or $dir =~ m|thread|
                or (-d "$config{'PSAD_LIBS_DIR'}/$dir/auto");
    }
    return \@paths;
}

sub psad_init() {

    %config = ();
    %cmds   = ();

    ### set umask to -rw-------
    umask 0077;

    ### turn off buffering
    $| = 1;

    $no_syslog_alerts = 1 if $analyze_mode or $status_mode or $test_mode;
    $no_email_alerts = 1 if $test_mode;
    $debug = 1 if $test_mode;

    ### import any override config files first
    &import_override_configs() if $override_config_str;

    ### import psad.conf
    &import_config($config_file);

    ### import FW_MSG_SEARCH strings
    &import_fw_search($config_file);

    ### expand any embedded vars within config values
    &expand_vars();

    ### pid file hash
    %pidfiles = (
        'psadwatchd'   => $config{'PSADWATCHD_PID_FILE'},
        'psad'         => $config{'PSAD_PID_FILE'},
        'psad_fw_read' => $config{'PSAD_FW_READ_PID_FILE'},
        'kmsgsd'       => $config{'KMSGSD_PID_FILE'},
    );

    ### dump configuration to STDOUT
    if ($dump_conf or $dump_ipt_policy) {
        my $rv = 0;
        my $rv_tmp = 0;
        $rv = &dump_conf() if $dump_conf;
        $rv_tmp = &dump_ipt_policy() if $dump_ipt_policy;
        $rv += $rv_tmp if $rv_tmp != 0;
        exit $rv;
    }

    &dump_conf() if $test_mode;

    ### make sure all necessary configuration variables
    ### are defined
    &required_vars();

    ### store the psad command line.
    $cmdline_file = $config{'PSAD_CMDLINE_FILE'};

    ### make sure the values in the config file make sense
    &validate_config();

    ### setup the appropriate iptables data file depending on whether
    ### SYSLOG_DAEMON is set to ulogd, or if ENABLE_SYSLOG_FILE is set
    ### to 'Y'
    unless ($benchmark or $fw_data_file) {
        if ($config{'SYSLOG_DAEMON'} =~ /ulog/i) {
            $fw_data_file = $config{'ULOG_DATA_FILE'};
        } elsif ($config{'ENABLE_SYSLOG_FILE'} eq 'Y') {
            $fw_data_file = $config{'IPT_SYSLOG_FILE'};
        } else {
            $fw_data_file = $config{'FW_DATA_FILE'};
        }
    }

    ### check to make sure the commands specified in the config section
    ### are in the right place, and attempt to correct automatically if not.
    ### (wget is only needed in --sig-update mode)
    my %cmd_exceptions = ('wget' => '');
    $cmd_exceptions{'ifconfig'} = '' if $config{'IFCFGTYPE'} =~ /iproute2/i;
    &check_commands(\%cmd_exceptions);

    ### import psad perl modules
    &import_perl_modules();

    ### download latest signatures from
    ### http://www.cipherdyne.org/psad/signatures
    exit &download_signatures() if $download_sigs;

    ### set some config variables based on command line input
    &handle_cmdline();

    $ipt_opts{'tmpdir'}     = $config{'PSAD_DIR'};
    $ipt_opts{'iptout_pat'} = $config{'IPT_OUTPUT_PATTERN'};
    $ipt_opts{'ipterr_pat'} = $config{'IPT_ERROR_PATTERN'};

    $ipt_opts{'debug'}   = 1 if $debug;
    $ipt_opts{'verbose'} = 1 if $verbose;

    ### build iptables block config hash out of IPT_AUTO_CHAIN keywords
    ### (we don't check ENABLE_AUTO_IDS here since someone may have turned
    ### it off but still want to run --Status checks or use --Flush).
    &build_ipt_config() unless $syslog_server;

    ### The --Kill command line switch was given.
    exit &stop_psad() if $kill;

    ### The --HUP command line switch was given.
    exit &hup() if $hup;

    ### The --USR1 command line switch was given.
    exit &usr1() if $usr1;

    ### The --Flush command line switch was given.
    exit &sockwrite_flush_auto_rules() if $flush_fw;

    ### the --Restart command line switch was given
    exit &restart() if $restart;

    ### list any existing iptables IPT_AUTO_CHAIN chains
    exit &ipt_list_auto_chains() if $fw_list_auto;

    ### add an IP/network to the psad auto blocking chains via the
    ### domain socket (note that &sockwrite_add_ipt_block_ip() calls
    ### &import_auto_dl() to make sure we don't add an IP that should
    ### be ignored).
    exit &sockwrite_add_ipt_block_ip() if $fw_block_ip;

    ### delete IP/network from psad auto blocking chains
    exit &sockwrite_rm_ipt_block_ip() if $fw_rm_block_ip;

    ### send a warning via syslog if the HOME_NET variable definition
    ### appears to include a subnet that is not directly connected to
    ### the local system.
    &validate_home_net();

    ### import icmp types and codes from psad_icmp_types; icmp "type"
    ### and "code" fields will be validated against the values in this
    ### file.
    &import_icmp_types('icmp', \%valid_icmp_types, $config{'ICMP_TYPES_FILE'})
        unless $no_icmp_types;
    &import_icmp_types('icmp6', \%valid_icmp6_types,
        $config{'ICMP6_TYPES_FILE'}) unless $no_icmp6_types;

    ### import p0f-based passive OS fingerprinting signatures
    &import_p0f_ipv4_sigs() unless $no_posf;

    ### import TOS-based passive OS fingerprinting signatures
    &import_posf_sigs() unless $no_posf;

    ### import IP protocols detail (derived from /etc/protocols)
    &import_protocol_strings();

    ### import auto_dl file for automatic ip/network danger
    ### level assignment
    &import_auto_dl() unless $no_auto_dl;

    ### parse snort rules if we enable psad to match on iptables log
    ### messages that include snort SID's (see "fwsnort":
    ### http://www.cipherdyne.org/fwsnort).
    &import_snort_rules() unless $no_snort_sids;

    ### import psad signatures (note that these signatures have been
    ### adapted from the Snort IDS and contain several keywords that
    ### were added by the psad project).
    &import_signatures() unless $no_signatures;

    ### the --Status command line switch was given
    exit &status() if $status_mode;

    ### there is a set of ports that should be ignored
    &parse_ignore_ports();

    ### there is a set of protocols that should be ignored
    &parse_ignore_protocols();

    ### there is a set of interfaces that should be ignored
    &parse_ignore_interfaces();

    ### enter iptables analysis mode.
    exit &analysis_mode() if $analyze_mode;

    ### enter CSV output mode.
    exit &csv_mode() if $csv_mode or $gnuplot_mode;

    ### enter benchmarking mode
    exit &benchmark_mode() if $benchmark;

    ### analyze the iptables policy and exit
    my $rv = &fw_analyze_mode();
    exit $rv if $fw_analyze;

    ### make sure we are setup to run
    &setup();

    ### dump config
    &dump_conf() if $debug;

    if ($restrict_ip_cmdline) {
        $restrict_ip = new NetAddr::IP $restrict_ip_cmdline
            or die "[*] Could not acquire NetAddr::IP object for $restrict_ip";
    }

    return;
}

sub validate_config() {

    &check_enable_vars_value();

    &is_digit_range('PORT_RANGE_SCAN_THRESHOLD', 0, 65535);
    &is_digit_range('TOP_SCANS_CTR_THRESHOLD', 0, 500);
    &is_digit_range('DSHIELD_ALERT_INTERVAL', 1, 24);
    &is_digit_range('TOP_PORTS_LOG_THRESHOLD', 0, 65535);
    &is_digit_range('STATUS_PORTS_THRESHOLD', 0, 65535);
    &is_digit_range('TOP_IP_LOG_THRESHOLD', 0, 10000);
    &is_digit_range('STATUS_IP_THRESHOLD', 0, 10000);

    ### it will be a long time before there are 100000 signatures
    &is_digit_range('TOP_SIGS_LOG_THRESHOLD', 0, 100000);
    &is_digit_range('STATUS_SIGS_THRESHOLD', 0, 10000);

    die qq([*] Invalid EMAIL_ADDRESSES value: "$config{'EMAIL_ADDRESSES'}")
        unless $config{'EMAIL_ADDRESSES'} =~ /\S+\@\S+/;

    ### translate commas into spaces
    $config{'EMAIL_ADDRESSES'} =~ s/\s*\,\s/ /g;

    if ($config{'ENABLE_AUTO_IDS'} eq 'Y'
            and $config{'IPTABLES_BLOCK_METHOD'} eq 'N'
            and $config{'TCPWRAPPERS_BLOCK_METHOD'} eq 'N') {
        die 'config warning, ENABLE_AUTO_IDS=Y, but ' .
            'both IPTABLES_BLOCK_METHOD and TCPWRAPPERS_BLOCK_METHOD are ' .
            'set to N.';
    }
    if ($status_min_dl and $status_min_dl > 5) {
        die '[*] The --status-dl must be between 1 and 5.';
    }
    if ($no_kmsgsd and not $debug) {
        die '[*] The --no-kmsgsd option can only be used with --debug.';
    }

    if ($fw_del_chains and not $flush_fw) {
        die '[*] The --fw-del-chains option can only be used with --Flush.';
    }

    if ($fw_block_ip) {
        unless ($fw_block_ip =~ m|^\s*$ipv4_re\s*$|
                or $fw_block_ip =~ m|^\s*$ipv4_re/\d+\s*$|
                or $fw_block_ip =~ m|^\s*$ipv4_re/$ipv4_re\s*$|) {
            die '[*] The --fw-block-ip argument accepts ' .
                'an IP address or network.';
        }
    }

    if ($fw_rm_block_ip) {
        unless ($fw_rm_block_ip =~ m|^\s*$ipv4_re\s*$|
                or $fw_rm_block_ip =~ m|^\s*$ipv4_re/\d+\s*$|
                or $fw_rm_block_ip =~ m|^\s*$ipv4_re/$ipv4_re\s*$|) {
            die '[*] The --fw-rm-block-ip argument accepts ' .
                'an IP address or network.';
        }
    }

    if ($config{'ENABLE_SYSLOG_FILE'} eq 'Y') {
        die "[*] Cannot set IPT_SYSLOG_FILE and FW_DATA_FILE to point ",
            "at the same file." if $config{'IPT_SYSLOG_FILE'}
            eq $config{'FW_DATA_FILE'};
    }

    unless ($config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL7/i
            or $config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL6/i
            or $config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL5/i
            or $config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL4/i
            or $config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL3/i
            or $config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL2/i
            or $config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL1/i
            or $config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL0/i) {
        die "[*] Unrecognized SYSLOG_FACILITY, see psad.conf";
    }

    unless ($config{'SYSLOG_PRIORITY'} =~ /LOG_INFO/i
            or $config{'SYSLOG_PRIORITY'} =~ /LOG_DEBUG/i
            or $config{'SYSLOG_PRIORITY'} =~ /LOG_NOTICE/i
            or $config{'SYSLOG_PRIORITY'} =~ /LOG_WARNING/i
            or $config{'SYSLOG_PRIORITY'} =~ /LOG_ERR/i
            or $config{'SYSLOG_PRIORITY'} =~ /LOG_CRIT/i
            or $config{'SYSLOG_PRIORITY'} =~ /LOG_ALERT/i
            or $config{'SYSLOG_PRIORITY'} =~ /LOG_EMERG/i) {
        die "[*] Unrecognized SYSLOG_PRIORITY, see psad.conf";
    }

    if ($analyze_mode or $gnuplot_mode or $csv_mode) {
        $fw_data_file = $config{'IPT_SYSLOG_FILE'}
            unless $fw_data_file;
    }

    if ($gnuplot_mode and not $csv_fields) {
        die "[*] Must specify which iptables fields to plot with the ",
            "--CSV-fields argument."
    }

    my $journalctl_is_running = 0;
    $journalctl_is_running = 1
        if &look_for_process(qr/^root\s.*systemd\-journald\b/);

    if ($config{'AUTO_DETECT_JOURNALCTL'} eq 'Y') {
        if ($journalctl_is_running) {
            $config{'ENABLE_FW_MSG_READ_CMD'} = 'Y';
            print STDERR "[*] journalctl appears to be running.\n" if $debug;
        } else {
            $config{'ENABLE_FW_MSG_READ_CMD'} = 'N';
            print STDERR "[*] journalctl appears to NOT be running.\n" if $debug;
        }
    } else {
        if ($config{'ENABLE_FW_MSG_READ_CMD'} eq 'Y') {
            die "[*] Could not find/execute FW_MSG_READ_CMD: $config{'FW_MSG_READ_CMD'}"
                unless -e $config{'FW_MSG_READ_CMD'} and -x $config{'FW_MSG_READ_CMD'};
            ### make sure systemd-journald is running and warn if not
            unless ($journalctl_is_running) {
                &sys_log('warning: it looks like systemd-journald is not running, ' .
                    'but ENABLE_FW_MSG_READ_CMD is enabled.');
            }
        } else {
            ### warn if systemd-journald is running since we likely won't see any
            ### firewall logs
            if ($journalctl_is_running) {
                &sys_log('warning: it looks like systemd-journald is running, ' .
                    'but ENABLE_FW_MSG_READ_CMD is not enabled.');
            }
        }
    }

    if ($config{'ENABLE_CUSTOM_SYSLOG_TS_RE'} eq 'Y') {
        ### validate a dummy regex match for syntax issues
        my $test_re = qr/$config{'CUSTOM_SYSLOG_TS_RE'}/;
        if ('teststr' =~ $test_re) {
            print STDERR "[+] Syslog parsing regex compiled.\n" if $debug;
        }
    }

    return;
}

sub open_fw_data() {

    if ($config{'ENABLE_FW_MSG_READ_CMD'} eq 'Y') {

        my $fwdata_fh = &fork_fw_read_cmd();

        $s = IO::Select->new();
        $s->add($fwdata_fh);

        $fwdata_fh->flush();

        return $fwdata_fh;

    } else {

        print STDERR "[+] Opening iptables data file: $fw_data_file\n"
            if $debug;

        open my $fwdata_fh, $fw_data_file or die '[*] Could not open ',
            "$fw_data_file: $!";
        return $fwdata_fh;
    }

    die "[*] Did not open a source of firewall log data";
}

sub reopen_fw_data() {
    my ($fwdata_fh, $truncate_flag) = @_;

    close $fwdata_fh;

    ### truncate fwdata file
    &truncate_file($fw_data_file) if $truncate_flag;

    open $fwdata_fh, $fw_data_file or die
        "[*] Could not open $fw_data_file: $!";

    return $fwdata_fh;
}

sub fork_fw_read_cmd() {

    $fw_read_cmd = $config{'FW_MSG_READ_CMD'};
    $fw_read_cmd .= " $config{'FW_MSG_READ_CMD_ARGS'} "
        if $config{'USE_FW_MSG_READ_CMD_ARGS'} eq 'Y';
    $fw_read_cmd =~ s/\s$//;

    print STDERR "[+] Reading iptables logs via: '$fw_read_cmd'\n"
        if $debug;

    ### Credit:
    ###   http://stackoverflow.com/questions/9214964/how-do-i-watch-a- \
    ###   running-programs-stdout-from-a-perl-program-that-called-it
    socketpair CMDA,CMDB,AF_UNIX,SOCK_STREAM,PF_UNSPEC;
    if (fork() == 0) {
        &write_pid($config{'PSAD_FW_READ_PID_FILE'});
        open my $fh, '-|', $fw_read_cmd;
        while (<$fh>) {
            print CMDB;
            CMDB->flush();
        }
        close $fh;
        exit $? >> 8;
    }
    return *CMDA;
}

sub check_enable_vars_value() {
    for my $var (keys %config) {
        next unless $var =~ /^ENABLE_/;
        ### make sure that all of the "ENABLE_" vars have a value of
        ### 'Y' or 'N'
        $config{$var} = uc($config{$var});
        unless ($config{$var} eq 'Y' or $config{$var} eq 'N'
                or $config{$var} eq 'YES' or $config{$var} eq 'NO') {
            die "[*] $var variable must be 'Y' or 'N'";
        }
    }
    return;
}

sub is_digit_range() {
    my ($key, $min, $max) = @_;
    die "[*] $key must be an integer >= $min and <= $max"
        unless $config{$key} =~ m|^\d+$| and $config{$key} >= $min
            and $config{$key} <= $max;
    return;
}

sub get_connected_subnets() {

    my @connected_subnets = ();

    if ($config{'IFCFGTYPE'} =~ /iproute2/i) {
        my @ip_out = @{&run_command($cmds{'ip'}, 'addr')};
        my $intf_name    = '';
        my $home_net_str = '';
        for my $line (@ip_out) {
            if ($line =~ /^\d+:\s+(\S+): </) {
                $intf_name = $1;
                next;
            }
            next if $intf_name eq 'lo';
            next if $intf_name =~ /dummy/i;
            if ($line =~ /^\s+inet.*?($ipv4_re)\/(\d+)/i) {
                push @connected_subnets, new NetAddr::IP($1, $2);
            } elsif ($line =~ /inet6\s(\S+)/) {
                push @connected_subnets, new6 NetAddr::IP($1);
            }
        }
    } else {
        my @ifconfig_out = @{&run_command($cmds{'ifconfig'}, '-a')};
        my $intf_name    = '';
        my $home_net_str = '';
        for my $line (@ifconfig_out) {
            if ($line =~ /^(\S+)\s+Link/) {
                $intf_name = $1;
                next;
            }
            next if $intf_name eq 'lo';
            next if $intf_name =~ /dummy/i;
            if ($line =~ /^\s+inet.*?:($ipv4_re).*:($ipv4_re)/i) {
                push @connected_subnets, new NetAddr::IP($1, $2);
            } elsif ($line =~ /^\s+inet6\saddr:\s+(\S+)/) {
                push @connected_subnets, new6 NetAddr::IP($1);
            }
        }
    }
    return \@connected_subnets;
}

sub validate_home_net() {

    @local_nets = ();

    return if $config{'HOME_NET'} eq 'any'
        and $config{'ENABLE_INTF_LOCAL_NETS'} eq 'N';

    my $connected_subnets_ar = &get_connected_subnets();

    if ($config{'ENABLE_INTF_LOCAL_NETS'} eq 'Y' and not $analyze_mode) {

        my $connected_str = '';
        for my $net (@$connected_subnets_ar) {
            push @local_nets, $net;
            $connected_str .= $net->network()->cidr() . ", ";
        }
        $connected_str =~ s|,\s*$||;

        $config{'HOME_NET'} = $connected_str;
        $config{'HOME_NET'} = 'any' unless $connected_str;

    } else {

        if ($config{'HOME_NET'} =~ /CHANGEME/) {
            &sys_log('config warning: the HOME_NET ' .
                'variable has not been set, defaulting to "any"');

            $config{'HOME_NET'} = 'any';
            return;
        }
        my @home_nets = split /\s*\,\s*/, $config{'HOME_NET'};
        my $found_one_net = 0;
        for my $net (@home_nets) {
            my $home_net = '';
            if ($net =~ m|($ipv4_re/$ipv4_re)|) {
                $home_net = new NetAddr::IP $1;
            } elsif ($net =~ m|($ipv4_re/\d+)|) {
                $home_net = new NetAddr::IP $1;
            } elsif ($net =~ m|($ipv4_re)|) {
                $home_net = new NetAddr::IP $1;
            } else {
                next;
            }
            push @local_nets, new NetAddr::IP $net;
            my $found = 0;
            for my $net (@$connected_subnets_ar) {
                $found = $found_one_net = 1
                    if $home_net->within($net);
            }
            unless ($found) {
                ### note that this might be ok if psad is running on a syslog
                ### server, but the most likely explanation is that there was a
                ### typo in the HOME_NET variable defintion.
                &sys_log('config warning: HOME_NET definition ' .
                    qq|in psad.conf contains "$home_net" which does not appear | .
                    "to be directly connected to the local system.");
            }
        }
        $config{'HOME_NET'} = 'any' unless $found_one_net;
    }
    return;
}

sub is_local() {
    my ($ip, $ip_obj) = @_;

    print STDERR "[+] is_local(): $ip..." if $debug;

    if ($test_mode) {
        print STDERR "(test mode enabled) no\n";
        return 0;
    }

    my $found = 0;
    for my $net (@local_nets) {
        if ($ip_obj->within($net)) {
            $found = 1;
            last;
        }
    }
    if ($debug) {
        $found == 0 ? print STDERR "no\n" : print STDERR "yes\n";
    }
    return $found;
}

sub import_ip_options() {

    %ip_options = ();

    open O, "< $config{'IP_OPTS_FILE'}" or die
        "[*] Couild not open IP options file $config{'IP_OPTS_FILE'}: $!";
    while (<O>) {
        next unless /\S/;
        next if /^\s*#/;
        ### 136  4   satid       Stream Identifier
        ### 145  -1  extproto    Extended Internet Proto
        if (/^\s*(\d+)\s+(\S+)\s+(\w+)\s+(.*)\s*/) {
            $ip_options{$1}{'len'}         = $2;
            $ip_options{$1}{'sig_keyword'} = $3;
            $ip_options{$1}{'desc'}        = $4;
        } else {
        }
    }
    close O;

    print STDERR "[+] IP options:\n", Dumper(\%ip_options)
        if $debug and $verbose;

    return;
}

sub import_fw_search() {
    my $config_file = shift;

    @fw_search = ();

    open F, "< $config_file" or die "[*] Could not open config ",
        "string file $config_file: $!";
    my @lines = <F>;
    close F;
    my $found_fw_search = 0;
    for my $line (@lines) {
        next unless $line =~ /\S/;
        next if $line =~ /^\s*#/;
        if ($line =~ /^\s*FW_MSG_SEARCH\s+(.*?);/) {
            push @fw_search, $1;
            $found_fw_search = 1;
        } elsif ($line =~ /^\s*FW_SEARCH_ALL\s+(\w+);/) {
            my $strategy = $1;
            if ($strategy eq 'Y' or $strategy eq 'N') {
                $config{'FW_SEARCH_ALL'} = $strategy;
            }
        }
    }
    unless (defined $config{'FW_SEARCH_ALL'}) {
        $config{'FW_SEARCH_ALL'} = 'Y';
    }

    unless ($config{'FW_SEARCH_ALL'} eq 'Y' or
            $config{'FW_SEARCH_ALL'} eq 'N') {
        $config{'FW_SEARCH_ALL'} = 'Y';
    }

    if ($config{'FW_SEARCH_ALL'} eq 'N' and not $found_fw_search) {
        push @fw_search, 'DROP';
    }
    return;
}

sub parse_ignore_ports() {

    ### zero out the hash since a HUP signal may have been received
    %ignore_ports = ();

    return if $config{'IGNORE_PORTS'} eq 'NONE';

    &parse_port_range(\%ignore_ports, $config{'IGNORE_PORTS'});

    return;
}

sub parse_port_range() {
    my ($hr, $line) = @_;

    my @fields = split /\s*,\s*/, $line;
    for my $field (@fields) {
        if ($field =~ m/(tcp|udp)\/(\d+)\s*-\s*(\d+)/i) {
            my $proto = lc($1);
            my $low   = $2;
            my $high  = $3;
            if ($low < $high) {
                my $existing_high = 0;
                if (defined $hr->{$proto}
                        and defined $hr->{$proto}->{'range'}
                        and defined $hr->{$proto}->{'range'}->{$low}) {
                    $existing_high = $hr->{$proto}->{'range'}->{$low};
                }
                if ($existing_high) {
                    if ($high > $existing_high) {
                        $hr->{$proto}->{'range'}->{$low} = $high;
                    }
                } else {
                    $hr->{$proto}->{'range'}->{$low} = $high;
                }
            }
        } elsif ($field =~ m/(tcp|udp)\/(\d+)/i) {
            my $proto = lc($1);
            my $port  = $2;
            $hr->{$proto}->{'port'}->{$port} = '';
        }
    }
    return;
}

sub parse_ignore_protocols() {

    ### zero out the hash since a HUP signal may have been received
    %ignore_protocols = ();

    return if $config{'IGNORE_PROTOCOLS'} eq 'NONE';

    my @protos = split /\s*,\s*/, $config{'IGNORE_PROTOCOLS'};
    for my $proto (@protos) {
        if ($proto =~ /\W/) {
            &sys_log('invalid protocol in IGNORE_PROTOCOLS var');
        } else {
            if ($proto =~ /^\d+$/) {
                ### IP protocol number
                $ignore_protocols{$proto} = '';
                ### add the string as well
                $ignore_protocols{lc($protocol_strings{$proto}{'name'})} = ''
                    if defined $protocol_strings{$proto};
            } else {
                $ignore_protocols{lc($proto)} = '';
                ### try to add the number as well
                for my $proto_num (keys %protocol_strings) {
                    if ($proto eq lc($protocol_strings{$proto_num}{'name'})) {
                        $ignore_protocols{$proto_num} = '';
                        last;
                    }
                }
            }
        }
    }
    return;
}

sub parse_ignore_interfaces() {

    ### zero out the hash since a HUP signal may have been received
    %ignore_interfaces = ();

    return if $config{'IGNORE_INTERFACES'} eq 'NONE';

    my @interfaces = split /\s*,\s*/, $config{'IGNORE_INTERFACES'};
    for my $intf (@interfaces) {
        if ($intf !~ /^[\w.:]+$/) {
            &sys_log('invalid interface in IGNORE_INTERFACES var');
        } else {
            $ignore_interfaces{$intf} = '';
        }
    }
    return;
}

sub import_snort_rules() {

    %fwsnort_sigs = ();

    for my $dir ($config{'SNORT_RULES_DIR'},
            $config{'FWSNORT_RULES_DIR'}) {
        next unless -d $dir;

        opendir D, $dir or die "[*] Could not open $dir: $!";
        my @rfiles = readdir D;
        closedir D;

        FILE: for my $rfile (@rfiles) {
            next FILE unless $rfile =~ /\.rules$/;
            if ($srules_type) {
                next FILE unless $rfile =~ /^${srules_type}\.rules$/;
            }
            my ($type) = ($rfile =~ /(\w+)\.rules/);

            open R, "< $dir/${rfile}" or
                die "[*] Could not open: ${srules_type}/${rfile}";

            while (<R>) {
                next unless /^\s*alert/;

                my $sid;  ### snort rule id
                if (/[\s;]sid:\s*(\d+)\s*;/) {
                    $sid = $1;
                } else {
                    next;
                }

                $fwsnort_sigs{$sid}{'msg'} = $1
                    if /msg:\s*\"(.*?)\"\s*;/;
                $fwsnort_sigs{$sid}{'is_psad_id'} = 0;

                if (/^\s*alert\s+(\w+)/) {
                    $fwsnort_sigs{$sid}{'proto'} = lc($1);
                }

                if (/[\s;]classtype:\s*(.*?)\s*;/) {
                    $fwsnort_sigs{$sid}{'classtype'} = $1;
                } else {
                    $fwsnort_sigs{$sid}{'classtype'} = '';
                }

                $fwsnort_sigs{$sid}{'priority'} = &convert_snort_priority($1)
                    if /[\s;]priority:\s*(\d+)\s*;/;

                ### import multiple content fields; someone could have built
                ### a series of custom iptables chains in order to detect
                ### multiple content strings.
                while (/[\s;](?:uri)?content:\s*\"(.*?)\"\s*;/g) {
                    push @{$fwsnort_sigs{$sid}{'content'}}, $1;
                }

                while (/[\s;]reference:\s*(.*?)\s*;/g) {
                    my $ref = $1;
                    if ($ref =~ /^(\w+),(\S+)/) {
                        ### reference:bugtraq,9732;
                        push @{$fwsnort_sigs{$sid}{'reference'}{lc($1)}}, $2;
                    }
                }

                next unless defined $fwsnort_sigs{$sid}{'msg'}
                    and defined $fwsnort_sigs{$sid}{'classtype'}
                    and defined $fwsnort_sigs{$sid}{'content'};
            }

            close R;
        }
    }

    ### import the Snort classification.config file
    &import_snort_class_priorities();

    ### import the reference.config file
    &import_snort_reference_config();

    ### import any specific SID -> DL mappings from the
    ### snort_rule_dl file
    &import_snort_rule_dl();

    print STDERR Dumper %fwsnort_sigs if $debug and $verbose;
    &sys_log("imported original Snort rules in " .
        "$config{'SNORT_RULES_DIR'}/ for reference info");
    return;
}

sub import_snort_class_priorities() {

    my $snort_class_file = "$config{'SNORT_RULES_DIR'}/classification.config";

    return unless -e $snort_class_file;
    open F, "< $snort_class_file" or die $!;
    while (<F>) {
        ### config classification: rpc-portmap-decode,Decode of an RPC Query,2
        if (/config\s+classification:\s+(\S+),.*(\d+)/) {
            ### the snort priority value can go from 1 to 10, with 1 being the
            ### worst offense and 10 being the least.  Most priorities are
            ### from 1 to 4.  We need to map these into the psad danger levels
            ### (reversed).  NOTE: the Snort engine does not enforce the 1-10
            ### range.
            $snort_class_dl{$1} = &convert_snort_priority($2);
        }
    }
    close F;
    &sys_log('imported Snort classification.config');
    return;
}

sub convert_snort_priority() {
    my $snort_priority = shift;
    my $psad_dl = 1;

    if ($snort_priority == 1) {
        $psad_dl = 5;
    } elsif ($snort_priority == 2) {
        $psad_dl = 4;
    } elsif ($snort_priority == 3) {
        $psad_dl = 3;
    } elsif ($snort_priority == 4) {
        $psad_dl = 2;
    }
    return $psad_dl;
}

sub import_snort_reference_config() {

    my $ref_file = "$config{'SNORT_RULES_DIR'}/reference.config";
    return unless -e $ref_file;

    open F, "< $ref_file" or die $!;
    while (<F>) {
        if (/^\s*config\s+reference:\s+(\w+)\s+(\S+)/) {
            ### config reference: bugtraq   http://www.securityfocus.com/bid/
            $snort_ref_baseurl{lc($1)} = $2;
        }
    }
    close F;
    return;
}

sub import_snort_rule_dl() {

    %snort_rule_dl = ();

    ### parse the snort_rule_dl file
    return unless -e $config{'SNORT_RULE_DL_FILE'};
    open F, "< $config{'SNORT_RULE_DL_FILE'}" or die $!;
    while (<F>) {
        next unless /^\s*\d/;
        if (/^\s*(\d+)\s+(\d+)/) {
            ### <sid>  <dl>
            my $sid = $1;
            my $dl  = $2;
            unless ($dl >= 0 and $dl < 6) {
                next;
            }
            $snort_rule_dl{$sid} = $dl;
        }
    }
    close F;
    return;
}

### for signatures that psad is able to detect with iptables logs that do
### not contain "SIDnnn" messages generated by fwsnort (and hence have no
### application layer matching criteria)

sub import_signatures() {

    ### import the ip_options file so that psad can make use of the
    ### ipopts keyword in Snort rules (requires --log-ip-options to
    ### iptables)
    &import_ip_options();

    ### undef so we don't leave old signatures around if
    ### we execute this code after receiving a HUP signal.
    %sigs = ();
    %sig_search = ();
    %sig_ip_objs = ();

    ### make sure no duplicate psad_id and sid fields exist
    my %psad_ids = ();
    my %sids = ();

    open SIGS, "< $config{'SIGS_FILE'}" or die
        "[*] Could not open the signatures file $config{'SIGS_FILE'}: $!";

    my $line_num = 0;
    my $sig_ctr  = 0;
    my $next_available_sid = 1;

    SIG: while (<SIGS>) {

        $line_num++;
        next SIG unless /\S/;
        next SIG if /^\s*#/;

        my %sig = ();

        ### alert tcp $HOME_NET 12345:12346 -> $EXTERNAL_NET any
        ### (msg:"BACKDOOR netbus active"; flow:from_server,established;
        ### content:"NetBus"; reference:arachnids,401; classtype:misc-activity;
        ### sid:109; rev:4; psad_dlevel:2)

        my $rule_hdr     = '';
        my $rule_options = '';

        if (m|^(.*?)\s+\((.*)\)|) {
            $rule_hdr     = $1;
            $rule_options = $2;
        } else {
            die "[*] import_signatures(): bad signature on line: ",
                "$line_num";
        }

        ### parse rule header (routine taken from fwsnort).
        if ($rule_hdr =~ m|^\s*alert\s+(\S+)\s+(\S+)\s+(\S+)
                            \s+(\S+)\s+(\S+)\s+(\S+)|x) {

            my $direction = $4;
            if ($direction eq '<>') {
                $sig{'bidir'} = 1;
            } else {
                $sig{'bidir'} = 0;
            }

            my $src = '';
            my $dst = '';
            if ($direction eq '<-') {
                $sig{'proto'} = lc($1);
                $src = $5;  ### switch src and dst
                $dst = $2;
                $sig{'sp'} = $3;
                $sig{'dp'} = $6;
            } else {
                $sig{'proto'} = lc($1);
                $src = $2;  ### normal src -> dst
                $dst = $5;
                $sig{'sp'} = $3;
                $sig{'dp'} = $6;
            }

            $sig{'src'} = &expand_sig_ips($src, $line_num);
            $sig{'dst'} = &expand_sig_ips($dst, $line_num);

            ### assign the source and destination port ranges
            &build_sig_int_range(\%sig, 'sp', 1, 65535, $line_num);
            &build_sig_int_range(\%sig, 'dp', 1, 65535, $line_num);

        } else {
            die "[*] import_signatures(): bad rule ",
                "header on line: $line_num";
        }

        ### make sure the signature does not contain any unsupported
        ### Snort rule options
        unless (&check_supported_options($rule_options, $line_num)) {
            next SIG;
        }

        ### parse rule options
        if ($rule_options =~ /[\s;]psad_id:\s*(\d+)\s*;/) {
            my $psad_id = $1;
            if (defined $psad_ids{$psad_id}) {
                die "[*] import_signatures(): Duplicate psad_id: $psad_id ",
                    qq|on line: $line_num|;
            } elsif (defined $sids{$psad_id}) {
                die "[*] import_signatures(): Duplicate psad_id: $psad_id ",
                    qq|to sid on line: $line_num|;
            }
            $psad_ids{$psad_id} = '';
            $sig{'psad_id'} = $psad_id;
            $next_available_sid = $psad_id if $psad_id > $next_available_sid;
        } else {
            my $msg = "[*] import_signatures(): could not find signature" .
                qq| "psad_id" on line: $line_num|;
            if ($config{'ENABLE_SNORT_SIG_STRICT'} eq 'Y') {
                die $msg;
            } else {
                &sys_log($msg);
                next SIG;
            }
        }

        ### original Snort sid
        if ($rule_options =~ /[\s;]sid:\s*(\d+)\s*;/) {
            my $sid = $1;
            if (defined $sids{$sid}) {
                die "[*] import_signatures(): Duplicate sid: $sid ",
                    qq|on line: $line_num|;
            } elsif (defined $psad_ids{$sid}) {
                die "[*] import_signatures(): Duplicate sid: $sid ",
                    qq|to psad_id on line: $line_num|;
            }
            $sids{$sid} = '';
            $sig{'sid'} = $sid;
            $sig{'is_psad_id'} = 0;
            $next_available_sid = $sid if $sid > $next_available_sid;
        } else {
            ### the signature was derived from several Snort rules
            $sig{'sid'} = $sig{'psad_id'};
            $sig{'is_psad_id'} = 1;
        }

        ### msg field
        if ($rule_options =~ /msg:\s*\"(.+?)\"\s*;/) {
            $sig{'msg'} = $1;
        } else {
            die "[*] import_signatures(): could not find ",
                qq|"msg" keyword on line: $line_num|;
        }

        ### classtype field
        if ($rule_options =~ /[\s;]classtype:\s*(.+?)\s*;/) {
            $sig{'classtype'} = $1;
        } else {
            $sig{'classtype'} = '';
        }

        ### reference field
        while ($rule_options =~ /[\s;]reference:\s*(.*?)\s*;/g) {
            my $ref = $1;
            if ($ref =~ /^(\w+),(\S+)/) {
                ### reference:bugtraq,9732;
                push @{$sig{'reference'}{lc($1)}}, $2;
            }
        }

        ### psad danger level
        $sig{'dl'} = 2;  ### default danger level
        if ($rule_options =~ /[\s;]psad_dl:\s*(\d+)/) {
            $sig{'dl'} = $1;
        } elsif ($sig{'classtype'}) {
            ### assign the danger level from the classification.config
            ### file if the psad_dl field does not exist
            if (defined $snort_class_dl{$sig{'classtype'}}) {
                $sig{'dl'} = $snort_class_dl{$sig{'classtype'}};
            }
        }

        ### see the signature was derived from a set of Snort rules
        if ($rule_options =~ /[\s;]psad_derived_sids:\s*(.+?)\s*;/) {
            $sig{'psad_derived_sids'} = [split /\s*,\s*/, $1];
        }

        ### sameip keyword
        if ($rule_options =~ /[\s;]sameip;/) {
            $sig{'sameip'} = 1;
        }

        ### psad_dsize keyword
        if ($rule_options =~ /[\s;]psad_dsize:\s*(.+?)\s*;/i) {
            $sig{'psad_dsize'} = $1;
            &build_sig_int_range(\%sig, 'psad_dsize', 1, 1514, $line_num);
        }

        ### psad_ip_len keyword
        if ($rule_options =~ /[\s;]psad_ip_len:\s*(.+?)\s*;/i) {
            $sig{'psad_ip_len'} = $1;
            ### technically, the minimum length must be $IP_HEADER_LEN
            &build_sig_int_range(\%sig, 'psad_ip_len', 1, 65536, $line_num);
        }

        ### dsize keyword
        if ($rule_options =~ /[\s;]dsize:\s*(.+?)\s*;/i) {
            $sig{'dsize'} = $1;
            &build_sig_int_range(\%sig, 'dsize', 1, 1514, $line_num);
        }

        ### ttl keyword
        if ($rule_options =~ /[\s;]ttl:\s*(.+?)\s*;/i) {
            $sig{'ttl'} = $1;
            &build_sig_int_range(\%sig, 'ttl', 1, 255, $line_num);
        }

        ### id keyword (for IP ID value)
        if ($rule_options =~ /[\s;]id:\s*(.+?)\s*;/i) {
            $sig{'id'} = $1;
            &build_sig_int_range(\%sig, 'id', 1, 65535, $line_num);
        }

        ### ipopts keyword (for IP options)
        if ($rule_options =~ /[\s;]ipopts:\s*(.+?)\s*;/i) {
            $sig{'ipopts'} = lc($1);

            ### make sure the IP option is defined in the ip_options
            ### file
            my $found = 0;
            for my $opt_val (keys %ip_options) {
                $found = 1
                    if $sig{'ipopts'} eq $ip_options{$opt_val}{'sig_keyword'};
            }
            unless ($found) {
                print STDERR qq|[-] Invalid argument "$sig{'ipopts'}" to |,
                    "ipopts keyword\n" if $debug;
            }
        }

        ### TCP keywords
        if ($sig{'proto'} eq 'tcp') {
            my $require_ack = 0;
            if ($rule_options =~ /[\s;]flow:\s*established\s*\;/i) {
                $require_ack = 1;
            }

            ### TCP flags
            if ($rule_options =~ /[\s;]flags:\s*(.+?)\s*;/) {
                my $sig_flags = $1;
                my $flags     = '';
                ### make flags identical to what iptables log messages
                ### would report (check in iptables flag reporting order).
                if ($sig_flags =~ /U/) {
                    if ($flags) {
                        $flags = 'URG ' . $flags;
                    } else {
                        $flags .= 'URG ';
                    }
                }
                if ($sig_flags =~ /A/ or $require_ack) {
                    $flags .= 'ACK ';
                }
                $flags .= 'PSH ' if $sig_flags =~ /P/;
                $flags .= 'RST ' if $sig_flags =~ /R/;
                $flags .= 'SYN ' if $sig_flags =~ /S/;
                $flags .= 'FIN ' if $sig_flags =~ /F/;

                ### if no flags are set iptables simply reports no flags
                ### at all instead of reporting "NULL".
                $flags = 'NULL' if $sig_flags =~ /N/;
                $flags =~ s/\s*$// if $flags;

                $sig{'flags'} = $flags;
            }

            ### seq keyword (TCP sequence number)
            if ($rule_options =~ /[\s;]seq:\s*(.+?)\s*;/i) {
                $sig{'seq'} = $1;
                &build_sig_int_range(\%sig, 'seq', 1,
                        4294967296, $line_num);
            }

            ### ack keyword (TCP acknowledgement number)
            if ($rule_options =~ /[\s;]ack:\s*(.+?)\s*;/i) {
                $sig{'ack'} = $1;
                &build_sig_int_range(\%sig, 'ack', 1,
                        4294967296, $line_num);
            }

            ### window keyword (TCP window size)
            if ($rule_options =~ /[\s;]window:\s*(.+?)\s*;/i) {
                $sig{'window'} = $1;
                &build_sig_int_range(\%sig, 'window', 1,
                        65535, $line_num);
            }

        } elsif ($sig{'proto'} eq 'udp') {
            ### no specific UDP header tests
        } elsif ($sig{'proto'} eq 'icmp') {

            ### ICMP keywords
            if ($rule_options =~ /[\s;]itype:\s*(.+?)\s*;/i) {
                $sig{'itype'} = $1;
                &build_sig_int_range(\%sig, 'itype', 1,
                        255, $line_num);
            }

            ### itype keyword (ICMP type)
            if ($rule_options =~ /[\s;]icode:\s*(.+?)\s*;/i) {
                $sig{'icode'} = $1;
                &build_sig_int_range(\%sig, 'icode', 1,
                        255, $line_num);
            }

            ### icmp_seq keyword (ICMP sequence value)
            if ($rule_options =~ /[\s;]icmp_seq:\s*(.+?)\s*;/i) {
                $sig{'icmp_seq'} = $1;
                &build_sig_int_range(\%sig, 'icmp_seq',
                        1, 255, $line_num);
            }

            ### icmp_id keyword (ICMP ID value)
            if ($rule_options =~ /[\s;]icmp_id:\s*(.+?)\s*;/i) {
                $sig{'icmp_id'} = $1;
                &build_sig_int_range(\%sig, 'icmp_id',
                        'icmp_id_s', 'icmp_id_e', 'icmp_id_neg',
                        1, 255, $line_num);
            }

        } elsif ($sig{'proto'} eq 'ip') {

            ### ip_proto keyword (IP protocol value)
            if ($rule_options =~ /[\s;]ip_proto:\s*(.+?)\s*;/i) {
                $sig{'ip_proto'} = $1;
                &build_sig_int_range(\%sig, 'ip_proto',
                        1, 255, $line_num);
            }
        } else {
            die "[*] import_signatures(): unsupported protocol: ",
                "$sig{'proto'} at line: $line_num";
        }

        ### add this signature into attributes cache
        $sigs{$sig{'sid'}} = \%sig;

        ### add this signature into the fast lookup cache
        for my $src (@{$sig{'src'}}) {

            for my $dst (@{$sig{'dst'}}) {

                if ($sig{'proto'} eq 'icmp' or $sig{'proto'} eq 'ip') {

                    $sig_search{$sig{'proto'}}{$src}{$dst}{$sig{'sid'}} = '';

                } elsif ($sig{'proto'} eq 'tcp' or $sig{'proto'} eq 'udp'
                        or $sig{'proto'} eq 'udplite') {

                    my $sp_type = 'norm';
                    $sp_type = 'neg' if $sig{'sp_neg'};
                    my $dp_type = 'norm';
                    $dp_type = 'neg' if $sig{'dp_neg'};

                    $sig_search{$sig{'proto'}}{$src}{$dst}{$sp_type}{$sig{'sp_s'}}
                        {$sig{'sp_e'}}{$dp_type}{$sig{'dp_s'}}{$sig{'dp_e'}}
                        {$sig{'sid'}} = '';
                }
            }
        }

        $sig_ctr++;
    }
    close SIGS;
    if ($get_next_rule_id) {
        $next_available_sid++;
        print "[+] Next available rule ID: $next_available_sid\n";
        exit 0;
    }
    if ($debug and $verbose) {
        print STDERR "[+] Main signatures hash:\n",
            Dumper(\%sig_search), Dumper(\%sigs);
    }
    &sys_log("imported $sig_ctr psad Snort signatures " .
        "from $config{'SIGS_FILE'}");
    return;
}

sub check_supported_options() {
    my ($rule_options, $line_num) = @_;
    for my $opt (@unsupported_snort_opts) {
        ### see if we match a regex belonging to an unsupported option
        if ($rule_options =~ m|[\s;]$opt:\s*.*?\s*;|) {
            my $msg = '[*] import_signatures(): Unsupported rule option: ' .
                    qq|"$opt" at line: $line_num|;
            if ($config{'ENABLE_SNORT_SIG_STRICT'} eq 'Y') {
                die $msg;
            } else {
                &sys_log($msg);
                return 0;
            }
        }
    }
    return 1;
}

sub import_icmp_types() {
    my ($proto, $type_hr, $file) = @_;

    open F, "< $file" or die "[*] Could not open $file: $!";
    my $icmp_type = -1;
    while (<F>) {
        next if /^\s*#/;
        if (/^(\d+)\s+(.*)/) {
            $icmp_type = $1;
            my $icmp_type_text = $2;
            if ($icmp_type_text =~ /unassigned/i) {
                $icmp_type = -1;
            }
            $type_hr->{$icmp_type}->{'text'} = $icmp_type_text;
            $type_hr->{$icmp_type}->{'codes'}{-1} = '';
            next;
        }
        if (/^\s+(\d+)\s+(.*)/ and $icmp_type > -1) {
            my $icmp_code      = $1;
            my $icmp_code_text = $2;
            next if $icmp_code_text =~ /unassigned/i;
            ### don't really need to add the icmp code text here since
            ### we validate against the icmp type first (i.e. an invalid
            ### icmp code is meaningless unless we first have a valid
            ### icmp type).
            $type_hr->{$icmp_type}->{'codes'}->{$icmp_code} = '';
        }
    }
    close F;
    print STDERR Dumper $type_hr if $debug and $verbose;
    &sys_log("imported valid $proto types and codes");
    return;
}

sub expand_sig_ips() {
    my ($ip_str, $line_num) = @_;

    my @arr = ();

    ### resolve any embedded vars
    if ($ip_str =~ m|\$.+\$|) {
        die "[*] import_signature(): Multiple embedded vars not supported ",
            "at line: $line_num";
    } elsif ($ip_str =~ m|\$(\w+)|) {
        my $sub_var = $1;
        if (defined $config{$sub_var}) {
            $ip_str =~ s|\$\w+|$config{$sub_var}|;
        } else {
            die qq|[*] import_signatures(): sub-var "$sub_var" at line: |,
                "$line_num not defined in psad.conf";
        }
    }

    if ($ip_str =~ m|,|) {
        my @ips = split /\s*,\s*/, $ip_str;
        for my $ip (@ips) {
            if ($ip =~ m|($ipv4_re/$ipv4_re)|
                    or $ip =~ m|($ipv4_re/\d+)|
                    or $ip =~ m|($ipv4_re)|) {
                push @arr, $1;
                $sig_ip_objs{$1} = new NetAddr::IP($1);
            } elsif ($ip =~ m|\:|) {
                push @arr, $ip;
                $sig_ip_objs{$ip} = new NetAddr::IP($ip)
                    or die "[*] NetAddr::IP error for $ip";
            }
        }

    } elsif ($ip_str =~ m|($ipv4_re/$ipv4_re)|
            or $ip_str =~ m|($ipv4_re/\d+)|
            or $ip_str =~ m|($ipv4_re)|) {
        push @arr, $1;
        $sig_ip_objs{$1} = new NetAddr::IP($1)
            or die "[*] NetAddr::IP error for $1";

    } elsif ($ip_str =~ m|\:|) {

        push @arr, $ip_str;
        $sig_ip_objs{$ip_str} = new NetAddr::IP($ip_str)
            or die "[*] NetAddr::IP error for $ip_str";

    } elsif ($ip_str eq 'any' or $ip_str =~ m|NOT_?USED|i) {
        ### handle NOT_USED case from older psad versions
        push @arr, 'any';

        ### this will match any IPv4 or IPv6 address
        $sig_ip_objs{'any'}
            = new6 NetAddr::IP('0000:0000:0000:0000:0000:0000:0000:0000/0');

    } else {
        die "[*] import_signatures(): Unrecognized src/dst: $ip_str ",
            "at line: $line_num";
    }
    return \@arr;
}

sub build_sig_int_range() {
    my ($sig_hr, $keyword, $range_start, $range_end, $line_num) = @_;

    my $start_key = "${keyword}_s";
    my $end_key   = "${keyword}_e";
    my $neg_key   = "${keyword}_neg";

    my $val = $sig_hr->{$keyword};

    ### resolve any embedded vars
    if ($val =~ m|\$.+\$|) {
        die "[*] import_signature(): Multiple embedded vars not supported ",
            "at line: $line_num";
    } elsif ($val =~ m|\$(\w+)|) {
        my $sub_var = $1;
        if (defined $config{$sub_var}) {
            $val =~ s|\$\w+|$config{$sub_var}|;
            $sig_hr->{$keyword} = $val;
        } else {
            die qq|[*] import_signatures(): sub-var "$sub_var" at line: |,
                "$line_num not defined in psad.conf";
        }
    }

    $sig_hr->{$start_key} = $range_start;
    $sig_hr->{$end_key}   = $range_end;

    if ($val =~ m|\!|) {
        $sig_hr->{$neg_key} = 1;
    } else {
        $sig_hr->{$neg_key} = 0;
    }
    $val =~ s|\!||;

    return if $val eq 'any';

    if ($val =~ m|^\s*(\d+)\s*:\s*(\d+)|) {
        $sig_hr->{$start_key} = $1;
        $sig_hr->{$end_key}   = $2;
    } elsif ($val =~ m|^\s*(\d+)\s*:|) {
        $sig_hr->{$start_key} = $1;
    } elsif ($val =~ m|^\s*:(\d+)|) {
        $sig_hr->{$end_key} = $1;
    } elsif ($val =~ m|^\s*\<=\s*(\d+)|) {
        $sig_hr->{$end_key} = $1;
    } elsif ($val =~ m|^\s*\<\s*(\d+)|) {
        $sig_hr->{$end_key} = $1-1;
    } elsif ($val =~ m|^\s*\>=\s*(\d+)|) {
        $sig_hr->{$start_key} = $1;
    } elsif ($val =~ m|^\s*\>\s*(\d+)|) {
        $sig_hr->{$start_key} = $1+1;
    } elsif ($val =~ m|^\s*(\d+)|) {
        $sig_hr->{$start_key} = $sig_hr->{$end_key} = $1;
    } else {
        die "[*] import_signatures(): Unrecognized value: ",
            "$val at line: $line_num";
    }
    return;
}

sub import_auto_dl() {

    %auto_dl = ();  ### undef so we don't leave old ips in %auto_dl

    my $i = 0;
    open F, "< $config{'AUTO_DL_FILE'}" or die '[*] Could not open ',
        "$config{'AUTO_DL_FILE'}: $!";
    while (<F>) {
        $i++;
        next unless /\S/;
        next if /^\s*#/;
        my $ip   = '';
        my $mask = '';
        my $dl   = '';
        my $opt_criteria = '';
        if (m|^\s*($ipv4_re)\s*/\s*($ipv4_re)\s+([0-5])\s*(.*);|) {
            $ip   = $1;
            $mask = $2;
            $dl   = $3;
            $opt_criteria = $4;
        } elsif (m|^\s*($ipv4_re)\s*/\s*(\d+)\s+([0-5])\s*(.*);|) {
            $ip   = $1;
            $mask = $2;
            $dl   = $3;
            $opt_criteria = $4;
        } elsif (m|^\s*($ipv4_re)\s+([0-5])\s*(.*);|) {
            $ip   = $1;
            $mask = '32';  ### single IP
            $dl   = $2;
            $opt_criteria = $3;
        } elsif (m|^\s*(\S+)\s+([0-5])\s*(.*);|) {
            my $str = $1;
            $dl           = $2;
            $opt_criteria = $3;
            if ($str =~ /\:/) {
                ### check to see if this is an IPv6 address
                if ($str =~ m|(\S+)/(\S+)|) {
                    $ip   = $1;
                    $mask = $2;
                } else {
                    $ip   = $str;
                    $mask = '128';
                }
            } else {
                ### check to see if it is a hostname
                my $hostname  = $str;
                my $iaddr = 0;
                $iaddr = inet_aton($hostname);
                unless ($iaddr) {
                    &sys_log(qq|could not resolve auto_dl "$hostname" | .
                        "to an IP at line $i");
                    next;
                }
                $ip = inet_ntoa($iaddr) or
                    &sys_log(qq|could not resolve auto_dl "$hostname" | .
                        "to an IP at line $i");
                $mask = '32';  ### single IP
            }
        } else {
            &sys_log("improperly formatted auto_dl line $i");
            next;
        }

        $auto_dl_ip_objs{$ip} = new NetAddr::IP($ip, $mask);
        unless (defined $auto_dl_ip_objs{$ip} and $auto_dl_ip_objs{$ip}) {
            &sys_log("auto_dl could not acquire NetAddr::IP object at line $i");
            next;
        }

        $auto_dl{$ip}{'mask'} = $mask;
        $auto_dl{$ip}{'dl'}   = $dl;

        if (m|icmp|i) {
            $auto_dl{$ip}{'proto'}{'icmp'}{'all'} = '';
        }

        ### check for optional port/protocol criteria
        if (/tcp/i or /udp/i) {
            &parse_port_range(\%{$auto_dl{$ip}{'proto'}}, $opt_criteria);
        }

        if (m|tcp|i and not defined $auto_dl{$ip}{'proto'}{'tcp'}) {
            $auto_dl{$ip}{'proto'}{'tcp'}{'all'} = '';
        }
        if (m|udplite|i and
                not defined $auto_dl{$ip}{'proto'}{'udplite'}) {
            $auto_dl{$ip}{'proto'}{'udplite'}{'all'} = '';
        }
        if (m|udp|i and not defined $auto_dl{$ip}{'proto'}{'udp'}) {
            $auto_dl{$ip}{'proto'}{'udp'}{'all'} = '';
        }

        unless ($ip and ($mask =~ /^0$/ or $mask)) {  ### allow a mask of "0"
            unless ($fw_block_ip) {
                my $subject = "$config{'MAIL_ERROR_PREFIX'} import warning: " .
                    "$config{'AUTO_DL_FILE'} error on line: $i";
                &send_mail($subject, '', $config{'EMAIL_ADDRESSES'},
                    $cmds{'mail'});
            }
        }
    }
    close F;

    if (%auto_dl) {

        ### don't write syslog message if we are running in --fw-block-ip
        ### mode
        unless ($fw_block_ip) {
            my $msg = "imported auto_dl file, got " . (keys %auto_dl) .
                " IP addrs/networks";
            print STDERR $msg, "\n" if $debug;
            &sys_log($msg);
        }
    }
    return;
}

sub import_p0f_ipv4_sigs() {

    %p0f_ipv4_sigs = ();

    my $p0f_file = $config{'P0F_FILE'};
    open P, "< $p0f_file" or die '[*] Could not open ',
        "$p0f_file: $!";
    my @lines = <P>;
    close P;
    my $os = '';
    for my $line (@lines) {
        chomp $line;
        next if $line =~ /^\s*#/;
        next unless $line =~ /\S/;

        ### S3:64:1:60:M*,S,T,N,W1:        Linux:2.5::Linux 2.5 (sometimes 2.4)
        ### 16384:64:1:60:M*,N,W0,N,N,T:   FreeBSD:4.4::FreeBSD 4.4
        ### 16384:64:1:44:M*:              FreeBSD:2.0-2.2::FreeBSD 2.0-4.1

        if ($line =~ /^(\S+?):(\S+?):(\S+?):(\S+?):(\S+?):\s+(.*)\s*/) {
            my $win_size = $1;
            my $ttl      = $2;
            my $frag_bit = $3;
            my $len      = $4;
            my $options  = $5;
            my $os       = $6;

            my $sig_str = "$win_size:$ttl:$frag_bit:$len:$options";
            ### don't know how to handle MTU-based window size yet
            unless ($win_size =~ /T/) {
                $p0f_ipv4_sigs{$len}{$frag_bit}{$ttl}{$win_size}{$options}{$os}
                    = $sig_str;
            }
        }
    }

    print STDERR Dumper %p0f_ipv4_sigs if $debug and $verbose;
    &sys_log('imported p0f-based passive OS fingerprinting signatures');
    return;
}

sub import_posf_sigs() {

    %posf_sigs = ();

    my $posf_file = $config{'POSF_FILE'};
    open P, "< $posf_file" or die '[*] Could not open ',
        "$posf_file: $!";
    my @lines = <P>;
    close P;
    my $os = '';
    for my $line (@lines) {
        chomp $line;
        next if ($line =~ /^\s*#/);
        next unless ($line =~ /\S/);
        if ($line =~ /^\s*OS\s+(.*);/) {
            $os = $1;
        } elsif ($line =~ /^\s*NUMPKTS\s+(\d+);/) {
            $posf_sigs{$os}{'numpkts'} = $1;
        } elsif ($line =~ /^\s*TOS\s+(\w+);/) {
            $posf_sigs{$os}{'tos'} = $1;
        } elsif ($line =~ /^\s*LEN\s+(\d+);/) {
            $posf_sigs{$os}{'len'} = $1;
        } elsif ($line =~ /^\s*TTL\s+(\d+);/) {
            $posf_sigs{$os}{'ttl'} = $1;
        } elsif ($line =~ /^\s*ID\s+(\w+);/) {
            $posf_sigs{$os}{'id'} = $1;
        } elsif ($line =~ /^\s*WINDOW\s+(\d+);/) {
            $posf_sigs{$os}{'win'} = $1;
        }
    }
    ### make sure each of the os signatures has all fields defined
    OS: for my $os (keys %posf_sigs) {
        unless (defined $posf_sigs{$os}{'numpkts'}) {
            &sys_log("$posf_file: missing " .
                "NUMPKTS for os: $os");
            delete $posf_sigs{$os};
            next OS;
        }
        unless (defined $posf_sigs{$os}{'tos'}) {
            &sys_log("$posf_file: missing " .
                "TOS for os: $os");
            delete $posf_sigs{$os};
            next OS;
        }
        unless (defined $posf_sigs{$os}{'len'}) {
            &sys_log("$posf_file: missing " .
                "LEN for os: $os");
            delete $posf_sigs{$os};
            next OS;
        }
        unless (defined $posf_sigs{$os}{'ttl'}) {
            &sys_log("$posf_file: missing " .
                "TTL for os: $os");
            delete $posf_sigs{$os};
            next OS;
        }
        unless (defined $posf_sigs{$os}{'id'}) {
            &sys_log("$posf_file: missing " .
                "ID for os: $os");
            delete $posf_sigs{$os};
            next OS;
        } else {
            unless ($posf_sigs{$os}{'id'} eq 'SMALLINCR'
                    || $posf_sigs{$os}{'id'} eq 'RANDOM') {
                &sys_log("$posf_file: ID must " .
                    "be either SMALLINCR or RANDOM for os: $os");
                delete $posf_sigs{$os};
                next OS;
            }
        }
        unless (defined $posf_sigs{$os}{'win'}) {
            &sys_log("$posf_file: missing " .
                "WINDOW for os: $os");
            delete $posf_sigs{$os};
            next OS;
        }
    }
    print STDERR Dumper %posf_sigs if $debug and $verbose;
    &sys_log('imported TOS-based passive OS fingerprinting signatures');
    return;
}

sub check_range() {
    my ($port, $start, $end) = @_;
    $start = $port if ($port < $start);
    $end   = $port if ($port > $end);
    return $start, $end;
}

### assign a danger level to each scan in the current interval.
sub assign_danger_level() {
    my ($curr_scan_hr, $curr_sigs_dl_hr, $curr_sids_dl_hr) = @_;

    SRC: for my $src (keys %$curr_scan_hr) {

        my $changed_dl = 0;

        print STDERR "[+] assign_danger_level(): source IP: $src (dl: ",
            "$scan_dl{$src})\n" if $debug;

        if (defined $curr_sigs_dl_hr->{$src}) {
            if ($scan_dl{$src} < $curr_sigs_dl_hr->{$src}) {
                $scan_dl{$src} = $curr_sigs_dl_hr->{$src};
                $changed_dl = 1;
            }
        }

        if (defined $curr_sids_dl_hr->{$src}) {
            if ($scan_dl{$src} < $curr_sids_dl_hr->{$src}) {
                $scan_dl{$src} = $curr_sids_dl_hr->{$src};
                $changed_dl = 1;
            }
        }

        DST: for my $dst (keys %{$curr_scan_hr->{$src}}) {
            my $absnum        = $scan{$src}{$dst}{'absnum'};
            my $tot_protocols = $scan{$src}{$dst}{'tot_protocols'};
            my $range = 0;
            my $s_port = 65535;
            my $e_port = 0;

            if ($changed_dl) {
                $scan{$src}{$dst}{'alerted'} = 0
                    if $config{'ALERT_ALL'} eq 'N';
            }

            ### calculate the range over _both_ tcp and udp
            for my $proto (qw(tcp udp udplite)) {
                next unless defined $scan{$src}{$dst}{$proto};
                next unless defined $scan{$src}{$dst}{$proto}{'abs_sp'};
                if ($s_port > $scan{$src}{$dst}{$proto}{'abs_sp'}) {
                    $s_port = $scan{$src}{$dst}{$proto}{'abs_sp'};
                }
                if ($e_port < $scan{$src}{$dst}{$proto}{'abs_ep'}) {
                    $e_port = $scan{$src}{$dst}{$proto}{'abs_ep'};
                }
            }
            if ($e_port) {
                $range = $e_port - $s_port;
            } else {  ### for icmp
                $range = $absnum;
            }

            ### if PORT_RANGE_SCAN_THRESHOLD is >= 1, then psad will not assign
            ### a danger level to repeated packets to the same port
            if ($absnum < $config{'DANGER_LEVEL1'}) {
                ### don't have enough packets to even reach danger level 1 yet.
                next DST;
            }
            if ($range >= $config{'PORT_RANGE_SCAN_THRESHOLD'}
                    or $tot_protocols >= $config{'PROTOCOL_SCAN_THRESHOLD'}) {
                if ($absnum < $config{'DANGER_LEVEL2'}) {
                    if ($scan_dl{$src} < 1) {
                        $scan{$src}{$dst}{'alerted'} = 0
                            if $config{'ALERT_ALL'} eq 'N';
                        $scan_dl{$src} = 1;
                    }
                } elsif ($absnum < $config{'DANGER_LEVEL3'}) {
                    if ($scan_dl{$src} < 2) {
                        $scan{$src}{$dst}{'alerted'} = 0
                            if $config{'ALERT_ALL'} eq 'N';
                        $scan_dl{$src} = 2;
                    }
                } elsif ($absnum < $config{'DANGER_LEVEL4'}) {
                    if ($scan_dl{$src} < 3) {
                        $scan{$src}{$dst}{'alerted'} = 0
                            if $config{'ALERT_ALL'} eq 'N';
                        $scan_dl{$src} = 3;
                    }
                } elsif ($absnum < $config{'DANGER_LEVEL5'}) {
                    if ($scan_dl{$src} < 4) {
                        $scan{$src}{$dst}{'alerted'} = 0
                            if $config{'ALERT_ALL'} eq 'N';
                        $scan_dl{$src} = 4;
                    }
                } elsif ($scan_dl{$src} < 5) {
                    $scan{$src}{$dst}{'alerted'} = 0
                            if $config{'ALERT_ALL'} eq 'N';
                    $scan_dl{$src} = 5;
                }
            }
        }
        print STDERR '[+] assign_danger_level(): DL (after assignment) = ',
            "$scan_dl{$src}\n" if $debug;
    }
    return;
}

sub assign_auto_danger_level() {
    my $pkt_hr = shift;

    ### see if the source should automatically be assigned a
    ### danger level
    NET: for my $net (keys %auto_dl) {
        my $dl   = $auto_dl{$net}{'dl'};
        my $mask = $auto_dl{$net}{'mask'};  ### may be a /32 (single IP)

        ### check to see if $pkt_hr->{'src'} is contained within an auto_dl network
        next NET unless $pkt_hr->{'s_obj'}->within($auto_dl_ip_objs{$net});

        ### $pkt_hr->{'src'} is part of an ignored network
        return 0 if $dl == 0;

        if ($scan_dl{$pkt_hr->{'src'}} < $dl) {
            if (not defined $auto_dl{$net}{'proto'}) {
                ### all protocols are applicable
                $scan_dl{$pkt_hr->{'src'}} = $dl;
                &sys_log('auto-assigned danger level: ' .
                    "$dl for IP: $pkt_hr->{'src'}");
                return $dl;
            } else {
                for my $proto (keys %{$auto_dl{$net}{'proto'}}) {
                    next unless $pkt_hr->{'proto'} eq $proto;
                    if (defined $auto_dl{$net}{'proto'}{$proto}{'port'}
                            or defined $auto_dl{$net}{'proto'}{$proto}{'range'}) {
                        if (&match_port(\%{$auto_dl{$net}{'proto'}{$proto}},
                                $pkt_hr->{'dp'})) {
                            $scan_dl{$pkt_hr->{'src'}} = $dl;
                            &sys_log('auto-assigned danger ' .
                                "level: $dl for IP: $pkt_hr->{'src'}");
                            return $dl;
                        }
                    } elsif (defined $auto_dl{$net}{'proto'}{$proto}{'all'}) {
                        ### we only require to match the protocol
                         $scan_dl{$pkt_hr->{'src'}} = $dl;
                         &sys_log('auto-assigned danger ' .
                             "level: $dl for IP: $pkt_hr->{'src'}");
                         return $dl;
                    }
                }
            }
        }
    }
    return -1;
}

sub check_scan_proto() {
    my ($proto, $scan_hr) = @_;
    for my $dst (keys %$scan_hr) {
        return 1 if defined $scan_hr->{$dst}->{$proto};
    }
    return 0;
}

sub write_global_packet_counters() {
    open P, "> $config{'PACKET_COUNTER_FILE'}" or
        die "[*] Could not open $config{'PACKET_COUNTER_FILE'}: $!";
    for my $proto (keys %protocols) {
        next unless defined $proto_ctrs{$proto};
        printf P "%8s: %d\n", $proto, $proto_ctrs{$proto};
    }
    close P;
    return;
}

sub write_prefix_counters() {
    open P, "> $config{'IPT_PREFIX_COUNTER_FILE'}" or
        die "[*] Could not open $config{'IPT_PREFIX_COUNTER_FILE'}: $!";
    for my $prefix (keys %ipt_prefixes) {
        my $count = $ipt_prefixes{$prefix};
        print P "$prefix: $count\n";
    }
    close P;
    return;
}

sub write_dshield_stats() {
    open D, "> $config{'DSHIELD_COUNTER_FILE'}" or
        die "[*] Could not open $config{'DSHIELD_COUNTER_FILE'}: $!";
    print D "total emails: $dshield_email_ctr\n",
        "total packets: $dshield_lines_ctr\n";
    close D;
    return;
}

sub write_src_packet_counters() {
    my ($hr, $tcp_absrange, $udp_absrange, $file) = @_;
    open P, "> $file" or
        die "[*] Could not open $file: $!";
    for my $chain (keys %$hr) {
        for my $intf (keys %{$hr->{$chain}}) {
            for my $proto (keys %protocols) {
                next unless defined $hr->{$chain}->{$intf}->{$proto};
                if ($proto eq 'tcp' and $tcp_absrange) {
                    print P "${chain}_${intf}_${proto}:  ",
                        "$hr->{$chain}->{$intf}->{$proto} [$tcp_absrange]\n";
                } elsif (($proto eq 'udp' or $proto eq 'udplite') and $udp_absrange) {
                    print P "${chain}_${intf}_${proto}:  ",
                        "$hr->{$chain}->{$intf}->{$proto} [$udp_absrange]\n";
                } else {
                    print P "${chain}_${intf}_${proto}:  ",
                        "$hr->{$chain}->{$intf}->{$proto}\n";
                }
            }
        }
    }
    close P;
    return;
}

sub collect_errors() {
    my $bad_packets_ar = shift;
    open ERR, ">> $config{'FW_ERROR_LOG'}" or die '[*] Could not open ',
        "$config{'FW_ERROR_LOG'}: $!";
    for my $line (@$bad_packets_ar) {
        print ERR $line;
    }
    close ERR;
    return;
}

sub log_top_scans() {

    ### top scanned ports
    open P, "> $config{'TOP_SCANNED_PORTS_FILE'}.tmp" or
        die "[*] Could not open $config{'TOP_SCANNED_PORTS_FILE'}: $!";
    print P "#\n# Format: <proto> <port> <packets>\n#\n\n";

    if (%top_tcp_ports) {
        print P "# Top scanned TCP ports:\n";
        my $ctr = 0;
        for my $dp (sort {$top_tcp_ports{$b} <=> $top_tcp_ports{$a}}
                keys %top_tcp_ports) {
            printf P "tcp %-5d $top_tcp_ports{$dp}\n", $dp;
            $ctr++;
            if ($config{'TOP_PORTS_LOG_THRESHOLD'} > 0) {
                last if $ctr >= $config{'TOP_PORTS_LOG_THRESHOLD'};
            }
        }
        print P "\n" if %top_udp_ports;
    }
    if (%top_udp_ports) {
        my $ctr = 0;
        print P "# Top scanned UDP ports:\n";
        for my $dp (sort {$top_udp_ports{$b} <=> $top_udp_ports{$a}}
                keys %top_udp_ports) {
            printf P "udp %-5d $top_udp_ports{$dp}\n", $dp;
            $ctr++;
            if ($config{'TOP_PORTS_LOG_THRESHOLD'} > 0) {
                last if $ctr >= $config{'TOP_PORTS_LOG_THRESHOLD'};
            }
        }
    }
    close P;
    move "$config{'TOP_SCANNED_PORTS_FILE'}.tmp",
        $config{'TOP_SCANNED_PORTS_FILE'};

    ### top signature matches
    open S, "> $config{'TOP_SIGS_FILE'}.tmp" or
        die "[*] Could not open $config{'TOP_SIGS_FILE'}: $!";
    print S "#\n# Format: <sid> \"<msg>\" <matches> <num_sources> ",
        "<sig_proto>\n#\n\n";
    my $ctr = 0;
    for my $sid (sort {$top_sigs{$b} <=> $top_sigs{$a}} keys %top_sigs) {
        my $found = 0;
        my $num_sources = keys %{$sig_sources{$sid}};
        if (defined $sigs{$sid} and defined $sigs{$sid}{'msg'}) {
            print S qq|$sid "$sigs{$sid}{'msg'}" $top_sigs{$sid} | .
                qq|$num_sources $sigs{$sid}{'proto'}\n|;
            $found = 1;
        } elsif (defined $fwsnort_sigs{$sid}
                and defined $fwsnort_sigs{$sid}{'msg'}) {
            print S qq|$sid "$fwsnort_sigs{$sid}{'msg'}" $top_sigs{$sid} | .
                qq|$num_sources $fwsnort_sigs{$sid}{'proto'}\n|;
            $found = 1;
        }
        $ctr++ if $found;
        if ($config{'TOP_SIGS_LOG_THRESHOLD'} > 0) {
            last if $ctr >= $config{'TOP_SIGS_LOG_THRESHOLD'};
        }
    }
    close S;
    move "$config{'TOP_SIGS_FILE'}.tmp",
        $config{'TOP_SIGS_FILE'};

    ### top attackers
    open A, "> $config{'TOP_ATTACKERS_FILE'}" or
        die "[*] Could not open $config{'TOP_ATTACKERS_FILE'}: $!";
    $ctr = 0;
    print A "#\n# Format: <IP> <DL> <total_packets> ",
        "<uniq_sigs> <sig_matches> <is_local>\n#\n\n";
    for my $src (sort {$scan_dl{$b} cmp $scan_dl{$a}} keys %scan_dl) {
        next unless $scan_dl{$src} >= $config{'MIN_DANGER_LEVEL'};
        next unless defined $top_packet_counts{$src}
                or defined $top_sig_counts{$src};
        my $str = sprintf "%-15s %d", $src, $scan_dl{$src};
        if (defined $top_packet_counts{$src}) {
            $str .= " $top_packet_counts{$src}";
        } else {
            $str .= ' 0';
        }
        my $uniq_sigs = 0;
        for my $sid (keys %sig_sources) {
            $uniq_sigs++ if defined $sig_sources{$sid}{$src};
        }
        $str .= " $uniq_sigs";
        if (defined $top_sig_counts{$src}) {
            $str .= " $top_sig_counts{$src}";
        } else {
            $str .= ' 0';
        }
        if (defined $local_src{$src}) {
            $str .= ' 1';
        } else {
            $str .= ' 0';
        }
        print A $str, "\n";
        $ctr++;
        if ($config{'TOP_IP_LOG_THRESHOLD'} > 0) {
            last if $ctr >= $config{'TOP_IP_LOG_THRESHOLD'};
        }
    }
    close A;
    return;
}

sub scan_logr() {
    my $curr_scan_hr = shift;

    return if $benchmark;

    if ($analyze_mode) {
        return unless $analyze_write_data;

        ### Log scan data
        print "[+] Writing $config{'PSAD_DIR'}/<IP> directories.\n";
        print "    This may take a while...\n" if ((keys %$curr_scan_hr) > 1000);
        if ($analysis_emails) {
            print "[+] Generating email alerts...\n";
            unless ($no_whois or $config{'ENABLE_WHOIS_LOOKUPS'} eq 'N') {
                print "[+] Issuing whois lookups (may take several seconds).\n";
            }
        }
    }

    SRC: for my $src (keys %$curr_scan_hr) {
        print STDERR "[+] scan_logr(): source IP: $src\n" if $debug;
        ### only send alerts for scans that are at least at
        ### danger level 1 or above.
        unless ($scan_dl{$src} >= $config{'MIN_DANGER_LEVEL'}) {
            print STDERR "    No alerts sent for $src; danger ",
                "level $scan_dl{$src} not high enough\n" if $debug;
            next SRC;
        }

        if ($config{'ENABLE_EMAIL_LIMIT_PER_DST'} eq 'N') {
            unless (defined $scan_email_ctrs{$src}{'email_ctr'}) {
                $scan_email_ctrs{$src}{'email_ctr'} = 0;
            } elsif ($config{'EMAIL_LIMIT'} > 0
                    and $scan_email_ctrs{$src}{'email_ctr'}
                    >= $config{'EMAIL_LIMIT'}) {
                ### ignore EMAIL_LIMIT if it is zero
                unless (defined $scan_email_ctrs{$src}{'stop_email'}
                        or $config{'EMAIL_LIMIT_STATUS_MSG'} eq 'N') {
                    &email_limit_reached($src, '');
                }
                &sys_log("email limit reached for src: $src");
                next SRC;
            }
            unless ($no_email_alerts) {
                $scan_email_ctrs{$src}{'email_ctr'}++;
            }
            if ($config{'EMAIL_THROTTLE'} > 1) {
                next SRC if (($scan_email_ctrs{$src}{'email_ctr'}
                        % $config{'EMAIL_THROTTLE'}) == 0);
            }
        }

        DST: for my $dst (keys %{$curr_scan_hr->{$src}}) {

            ### see if we have already sent an alert for $src
            ### (against $dst) for this danger level.
            if ($config{'ALERT_ALL'} eq 'N') {
                next DST if $scan{$src}{$dst}{'alerted'};
            }
            my $syslog_flags = '';
            my $src_dns_str  = '';
            my $dst_dns_str  = '';
            my $rdns         = '';
            my $src_subj     = '';
            my $dst_subj     = '';
            my $src_mac      = '';
            my $dst_mac      = '';
            my $syslog_range = '';
            my $tcp_newrange = '';
            my $tcp_absrange = '';
            my $udp_newrange = '';
            my $udp_absrange = '';
            my $tcp_newpkts  = 0;
            my $udp_newpkts  = 0;
            my $icmp_newpkts = 0;
            my $other_proto_newpkts = 0;
            my $tcp_f  = 0;
            my $udp_f  = 0;
            my $icmp_f = 0;
            my $scan_style_str = '';
            my $whois_info_ar;

            ### get the current danger level and the absolute number
            ### of packets used in the scan so far
            my $curr_dl = $scan_dl{$src};

            if ($config{'ENABLE_EMAIL_LIMIT_PER_DST'} eq 'Y') {
                unless (defined $scan_email_ctrs{$src}{$dst}{'email_ctr'}) {
                    $scan_email_ctrs{$src}{$dst}{'email_ctr'} = 0;
                } elsif ($config{'EMAIL_LIMIT'} > 0
                        and $scan_email_ctrs{$src}{$dst}{'email_ctr'}
                        >= $config{'EMAIL_LIMIT'}) {
                    ### ignore EMAIL_LIMIT if it is zero
                    unless (defined $scan_email_ctrs{$src}{$dst}{'stop_email'}
                            or $config{'EMAIL_LIMIT_STATUS_MSG'} eq 'N') {
                        &email_limit_reached($src, $dst);
                    }
                    &sys_log("email limit reached for src: $src " .
                        "against dst: $dst");
                    next DST;
                }
                unless ($no_email_alerts) {
                    $scan_email_ctrs{$src}{$dst}{'email_ctr'}++;
                }
                if ($config{'EMAIL_THROTTLE'} > 1) {
                    next DST if (($scan_email_ctrs{$src}{$dst}{'email_ctr'}
                            % $config{'EMAIL_THROTTLE'}) == 0);
                }
            }
            print STDERR "[+] scan_logr(): dst IP: $dst\n" if $debug;

            ### make $src directory here in /var/log/psad
            ### unless it already exists
            mkdir "$config{'PSAD_DIR'}/${src}", 0700
                unless -d "$config{'PSAD_DIR'}/${src}";
            my $src_dir = "$config{'PSAD_DIR'}/${src}";
            my $dl_file      = "${src_dir}/danger_level";
            my $posf_file    = "${src_dir}/os_guess";
            my $p0f_file     = "${src_dir}/p0f_guess";
            my $email_file   = "${src_dir}/${dst}_email_alert";
            my $log_sigs     = "${src_dir}/${dst}_signatures";
            my $s_time_file  = "${src_dir}/${dst}_start_time";
            my $pkt_ctr_file = "${src_dir}/${dst}_packet_ctr";
            my $ecount_file  = "${src_dir}/email_ctr";

            if ($config{'ENABLE_EMAIL_LIMIT_PER_DST'} eq 'Y') {
                $ecount_file = "${src_dir}/${dst}_email_ctr";
            }

            ### print the current danger level to the danger_level file.
            open DL, "> $dl_file" or die "[*] Could not open $dl_file: $!";
            print DL $curr_dl, "\n";
            close DL;

            ### write out the TOS-based os guess (if there is one).
            if (defined $posf{$src} and defined $posf{$src}{'guess'}) {
                open P, "> $posf_file" or
                    die "[*] Could not open $posf_file: $!";
                print P $posf{$src}{'guess'}, "\n";
                close P;
            }

            ### write out the p0f-based os guess(es) (if there is one).
            if (defined $p0f{$src}) {
                open P, "> $p0f_file" or
                    die "[*] Could not open $p0f_file: $!";
                for my $os (keys %{$p0f{$src}}) {
                    print P "$os\n";
                }
                close P;
            }

            ### write out the start time.
            open T, "> $s_time_file" or
                die "[*] Could not open $s_time_file: $!";
            print T $scan{$src}{$dst}{'s_time'}, "\n";
            close T;

            $src_mac = $curr_scan_hr->{$src}->{$dst}->{'s_mac'}
                if defined $curr_scan_hr->{$src}->{$dst}->{'s_mac'};
            $dst_mac = $curr_scan_hr->{$src}->{$dst}->{'d_mac'}
                if defined $curr_scan_hr->{$src}->{$dst}->{'d_mac'};

            if (defined $scan{$src}{$dst}{'tcp'}
                    and defined $scan{$src}{$dst}{'tcp'}{'abs_sp'}) {
                my $tcp_s_port
                    = $scan{$src}{$dst}{'tcp'}{'abs_sp'};
                my $tcp_e_port
                    = $scan{$src}{$dst}{'tcp'}{'abs_ep'};
                if ($tcp_s_port == $tcp_e_port) {
                    $tcp_absrange = $tcp_s_port;
                } else {
                    $tcp_absrange = "$tcp_s_port-$tcp_e_port";
                }
            }
            if (defined $curr_scan_hr->{$src}->{$dst}->{'tcp'}
                    and defined $curr_scan_hr->{$src}->{$dst}->{'tcp'}->{'strtp'}) {
                $tcp_f = 1;
                my $tcp_s_port
                    = $curr_scan_hr->{$src}->{$dst}->{'tcp'}->{'strtp'};
                my $tcp_e_port
                    = $curr_scan_hr->{$src}->{$dst}->{'tcp'}->{'endp'};
                if ($tcp_s_port == $tcp_e_port) {
                    $tcp_newrange = $tcp_s_port;
                } else {
                    $tcp_newrange = "$tcp_s_port-$tcp_e_port";
                }
                $tcp_newpkts =
                    $curr_scan_hr->{$src}->{$dst}->{'tcp'}->{'pkts'};
            }
            if (defined $scan{$src}{$dst}{'udp'}
                    and $scan{$src}{$dst}{'udp'}{'abs_sp'}) {
                my $udp_s_port
                    = $scan{$src}{$dst}{'udp'}{'abs_sp'};
                my $udp_e_port
                    = $scan{$src}{$dst}{'udp'}{'abs_ep'};
                if ($udp_s_port == $udp_e_port) {
                    $udp_absrange = $udp_s_port;
                } else {
                    $udp_absrange = "$udp_s_port-$udp_e_port";
                }
            }
            if (defined $curr_scan_hr->{$src}->{$dst}->{'udp'}
                    and $curr_scan_hr->{$src}->{$dst}->{'udp'}->{'strtp'}) {
                $udp_f = 1;
                my $udp_s_port
                    = $curr_scan_hr->{$src}->{$dst}->{'udp'}->{'strtp'};
                my $udp_e_port
                    = $curr_scan_hr->{$src}->{$dst}->{'udp'}->{'endp'};
                if ($udp_s_port == $udp_e_port) {
                    $udp_newrange = $udp_s_port;
                } else {
                    $udp_newrange = "$udp_s_port-$udp_e_port";
                }
                $udp_newpkts = $curr_scan_hr->{$src}->{$dst}->{'udp'}->{'pkts'};
            }
            if (defined $curr_scan_hr->{$src}->{$dst}->{'icmp'}) {
                $icmp_f = 1;
                $icmp_newpkts =
                    $curr_scan_hr->{$src}->{$dst}->{'icmp'}->{'pkts'};
            }

            for my $str (keys %{$curr_scan_hr->{$src}->{$dst}}) {
                next if $str eq 'tcp' or $str eq 'udp'
                    or $str eq 'udplite' or $str eq 'icmp'
                    or $str eq 'icmp6' or $str eq 'tot_protocols';
                next unless defined $scan{$src}{$dst}{$str};
                next unless defined $curr_scan_hr->{$src}->{$dst}->{$str}->{'pkts'};
                $other_proto_newpkts +=
                    $curr_scan_hr->{$src}->{$dst}->{$str}->{'pkts'};
            }

            ### write out the overall packet counters for $src.
            &write_src_packet_counters($scan{$src}{$dst}{'chain'},
                $tcp_absrange, $udp_absrange, $pkt_ctr_file);

            ### get reverse dns info
            $src_subj = $src;
            $dst_subj = $dst;
            unless ($no_rdns or $config{'ENABLE_DNS_LOOKUPS'} eq 'N') {
                $src_dns_str = &get_dns_info($src);
                if ($src_dns_str) {
                    $src_subj = $src_dns_str;
                } else {
                    $src_dns_str = '[No reverse dns info available]';
                }
                $dst_dns_str = &get_dns_info($dst);
                if ($dst_dns_str) {
                    $dst_subj = $dst_dns_str;
                } else {
                    $dst_dns_str = '[No reverse dns info available]';
                }
            }

            ### get whois info
            my $whois_which_ip_str = 'source IP';
            unless ($no_whois or $config{'ENABLE_WHOIS_LOOKUPS'} eq 'N') {
                my $lookup_ip = $src;  ### default

                ### we're most likely interested in the whois information for
                ### a non local IP address
                if ($config{'ENABLE_WHOIS_FORCE_SRC_IP'} eq 'N'
                        and &is_local($src, new NetAddr::IP($src))
                        and not &is_local($dst, new NetAddr::IP($dst))) {
                    $whois_which_ip_str = 'destination IP';
                    $lookup_ip = $dst;
                }

                $whois_info_ar = &get_whois_info($lookup_ip,
                                    "${src_dir}/${lookup_ip}_whois");

                if ($debug and $verbose) {
                    print STDERR for @$whois_info_ar;
                }
            }
            print STDERR "[+] scan_logr(): generating email.....\n"
                if $test_mode or $debug;

            ### get the absolute starting time for the scan and the
            ### current time
            my $s_time = '';
            my $abs_s_time = scalar localtime $scan{$src}{$dst}{'s_time'};

            if (not $analyze_mode and ((time() - $config{'CHECK_INTERVAL'}) <
                    $scan{$src}{$dst}{'s_time'})) {
                $s_time = $abs_s_time;
            } else {
                $s_time = scalar localtime((time()
                        - $config{'CHECK_INTERVAL'}));
            }
            my $time = scalar localtime();

            ### email file handle
            my $fh;

            ### open the email alert file
            if ($no_daemon) {
                $fh = *STDOUT;
            } else {
                open E, "> $email_file" or
                    die "[*] Could not open $email_file: $!";
                $fh = *E;
            }

            print $fh "=-=-=-=-=-=-=-=-=-=-=-= $time =-=-=-=-=-=-=-=",
                "-=-=-=-=\n\n\n";

            printf $fh "%${log_len}s%s", 'Danger level: ',
                "[$scan_dl{$src}] (out of 5)";

            if ($scan{$src}{$dst}{'tot_protocols'} > 1) {
                print $fh ' Multi-Protocol';
            }
            if (defined $auto_assigned_msg{$src}) {
                print $fh ' Auto-assigned';
                delete $auto_assigned_msg{$src};
            }
            print $fh "\n\n";

            if ($curr_scan_hr->{$src}->{$dst}->{'tot_protocols'}
                    >= $config{'PROTOCOL_SCAN_THRESHOLD'}) {
                printf $fh "%${log_len}s%s%s\n", 'IP Protocol scan: ',
                    "[$curr_scan_hr->{$src}->{$dst}->{'tot_protocols'}",
                    ' unique protocols, Nmap -sO]';
                $scan_style_str .= 'Nmap -sO IP protocol scan,';
            }

            if ($tcp_f) {
                printf $fh "%${log_len}s%s\n", 'Scanned TCP ports: ',
                    "[$tcp_newrange: $tcp_newpkts packets]";
                my $prefix = 'TCP flags: ';

                for my $flags (keys %{$curr_scan_hr->{$src}->
                        {$dst}->{'tcp'}->{'flags'}}) {

                    my $nmap_opts = '';
                    $syslog_flags .= "$flags ";
                    my $n_pkts = $curr_scan_hr->{$src}->{$dst}->
                        {'tcp'}->{'flags'}->{$flags};

                    ### FUTURE: replace this with a simple hash lookup
                    if ($flags eq 'SYN') {
                        $nmap_opts = '-sT or -sS';
                    } elsif ($flags eq 'FIN') {
                        $nmap_opts = '-sF';
                    } elsif ($flags eq 'URG PSH FIN') {
                        $nmap_opts = '-sX';
                    } elsif ($flags eq 'NULL') {
                        $nmap_opts = '-sN';
                    } elsif ($flags eq 'URG PSH SYN FIN') {
                        $nmap_opts = '-O';
                    }

                    if ($curr_scan_hr->{$src}->{$dst}->{'is_topera'}) {
                        $scan_style_str = "Topera $flags scan";
                        printf $fh "%${log_len}s%s\n", $prefix,
                            "[$flags: $n_pkts packets, Topera $flags scan]";
                    } elsif ($curr_scan_hr->{$src}->{$dst}->{'is_masscan'}) {
                        $scan_style_str = "Masscan $flags scan";
                        printf $fh "%${log_len}s%s\n", $prefix,
                            "[$flags: $n_pkts packets, Masscan $flags scan]";
                    } else {
                        if ($nmap_opts) {
                            printf $fh "%${log_len}s%s\n", $prefix,
                                "[$flags: $n_pkts packets, Nmap: $nmap_opts]";
                        } else {
                            printf $fh "%${log_len}s%s\n", $prefix,
                                "[$flags: $n_pkts packets]";
                        }
                        if ($scan_style_str) {
                            $scan_style_str .= " $nmap_opts scan,";
                        } else {
                            $scan_style_str = "Nmap $nmap_opts scan,";
                        }
                        $prefix = '';
                    }
                }
                if (defined $curr_scan_hr->{$src}->{$dst}->{'tcp'}->{'chain'}) {
                    &print_chains_and_prefixes(
                        $curr_scan_hr->{$src}->{$dst}->{'tcp'}->{'chain'}, $fh);
                }
                $syslog_flags =~ s/\s*$//;
                $syslog_range = "tcp: [$tcp_newrange]";
                $syslog_range .= " flags: $syslog_flags" if $syslog_flags;
            }
            if ($udp_f) {
                printf $fh "%${log_len}s%s\n", 'Scanned UDP ports: ',
                    "[$udp_newrange: $udp_newpkts packets, Nmap: -sU]";
                if (defined $curr_scan_hr->{$src}->{$dst}->{'udp'}->{'chain'}) {
                    &print_chains_and_prefixes(
                        $curr_scan_hr->{$src}->{$dst}->{'udp'}->{'chain'},
                        $fh);
                }
                $syslog_range = "udp: [$udp_newrange]";
                $scan_style_str .= ' -sU scan,';
            }
            if ($icmp_f) {
                printf $fh "%${log_len}s%s\n", 'icmp packets: ',
                    "[$icmp_newpkts]";
                if (defined $curr_scan_hr->{$src}->{$dst}->{'icmp'}->{'chain'}) {
                    &print_chains_and_prefixes(
                        $curr_scan_hr->{$src}->{$dst}->{'icmp'}->{'chain'},
                        $fh);
                }
            }
            $scan_style_str =~ s/\,$//;
            printf $fh "\n%${log_len}s%s\n", 'Source: ', $src;
            printf $fh "%${log_len}s%s\n", 'DNS: ', $src_dns_str
                unless $no_rdns or $config{'ENABLE_DNS_LOOKUPS'} eq 'N';
            if ($config{'ENABLE_MAC_ADDR_REPORTING'} eq 'Y') {
                printf $fh "%${log_len}s%s\n", 'MAC: ', $src_mac
                    if $src_mac;
            }
            unless ($no_posf) {
                if (defined $p0f{$src}) {  ### prefer p0f-based fingerprinting
                    ### any p0f fingerprint that contains a "@" is an
                    ### approximate match
                    my $found_exact_match = 0;
                    for my $os (keys %{$p0f{$src}}) {
                        if ($os !~ /\@/) {
                            $found_exact_match = 1;
                            last;
                        }
                    }
                    my $printed_guess_line = 0;
                    for my $os (keys %{$p0f{$src}}) {
                        if ($found_exact_match) {
                            next if $os =~ /\@/;
                        }
                        if ($printed_guess_line) {
                            printf $fh "%${log_len}s%s\n", ' ', $os;
                        } else {
                            printf $fh "%${log_len}s%s\n", 'OS guess: ',
                                $os;
                        }
                        $printed_guess_line = 1;
                    }
                } elsif (defined $posf{$src}{'guess'}) {
                    printf $fh "%${log_len}s%s\n", 'OS guess: ',
                        $posf{$src}{'guess'};
                }
            }
            printf $fh "\n%${log_len}s%s\n", 'Destination: ', $dst;
            printf $fh "%${log_len}s%s\n", 'DNS: ', $dst_dns_str
                unless $no_rdns or $config{'ENABLE_DNS_LOOKUPS'} eq 'N';
            if ($config{'ENABLE_MAC_ADDR_REPORTING'} eq 'Y') {
                printf $fh "%${log_len}s%s\n", 'MAC: ', $dst_mac
                    if $dst_mac;
            }
            print $fh "\n";

            ### print the overall stats since the scan began
            printf $fh "%${log_len}s%s\n", 'Overall scan start: ',
                $abs_s_time;

            if ($config{'ENABLE_EMAIL_LIMIT_PER_DST'} eq 'Y') {
                printf $fh "%${log_len}s%s\n", 'Total email alerts: ',
                    $scan_email_ctrs{$src}{$dst}{'email_ctr'};
            } else {
                printf $fh "%${log_len}s%s\n", 'Total email alerts: ',
                    $scan_email_ctrs{$src}{'email_ctr'};
            }

            if ($tcp_absrange) {
                printf $fh "%${log_len}s%s\n", 'Complete TCP range: ',
                    "[$tcp_absrange]";
            }
            if ($udp_absrange) {
                printf $fh "%${log_len}s%s\n", 'Complete UDP range: ',
                    "[$udp_absrange]";
            }
            if (defined $curr_scan_hr->{$src}->{$dst}->{'syslog_host'}) {
                my $syslog_hosts = '';
                $syslog_hosts .= "$_, " for keys
                    %{$curr_scan_hr->{$src}->{$dst}->{'syslog_host'}};
                $syslog_hosts =~ s/\,\s+$//;
                if ($syslog_hosts =~ /\,/) {
                    printf $fh "%${log_len}s%s\n", 'Syslog hostnames: ',
                        $syslog_hosts;
                } else {
                    printf $fh "%${log_len}s%s\n", 'Syslog hostname: ',
                        $syslog_hosts;
                }
            }
            printf $fh "\n";
            printf $fh "%${log_len}s\n", 'Global stats: ';

            printf $fh "%${log_len}s%-9s%-12s%-11s%-10s\n", '',
                 'chain:', 'interface:', 'protocol:', 'packets:';

            for my $chain (keys %{$scan{$src}{$dst}{'chain'}}) {
                for my $intf (keys %{$scan{$src}{$dst}{'chain'}{$chain}}) {
                    for my $proto (sort {$a cmp $b} keys %{$scan{$src}{$dst}{'chain'}{$chain}{$intf}}) {
                        my $pkts = $scan{$src}{$dst}{'chain'}{$chain}{$intf}{$proto};
                        if (defined $protocol_strings{$proto}) {
                            printf $fh "%${log_len}s%-9s%-12s%-11s%-10s\n", '', $chain,
                                $intf, $protocol_strings{$proto}{'name'}, $pkts;
                        } else {
                            printf $fh "%${log_len}s%-9s%-12s%-11s%-10s\n", '', $chain,
                                $intf, $proto, $pkts;
                        }
                    }
                }
            }

            ### print out any matched signatures to the email
            ### alert file and also to the signature log
            &scan_logr_signatures($src, $dst, $fh, $log_sigs);

            ### write a scan message to syslog
            my $syslog_str = 'scan detected ';
            $syslog_str .= "($scan_style_str): " if $scan_style_str;
            $syslog_str .= "$src -> $dst";
            $syslog_str .= " $syslog_range" if $syslog_range;
            $syslog_str .= " tcp pkts: $tcp_newpkts" if $tcp_newpkts;
            $syslog_str .= " udp pkts: $udp_newpkts" if $udp_newpkts;
            $syslog_str .= " icmp pkts: $icmp_newpkts" if $icmp_newpkts;
            $syslog_str .= " other proto pkts: $other_proto_newpkts"
                if $other_proto_newpkts;
            $syslog_str .= " DL: $curr_dl";

            &sys_log($syslog_str);

            unless ($no_whois or $config{'ENABLE_WHOIS_LOOKUPS'} eq 'N') {
                print $fh "\n[+] Whois Information ($whois_which_ip_str):\n";
                for my $line (@$whois_info_ar) {
                    print $fh $line;
                }
            }
            print $fh "\n=-=-=-=-=-=-=-=-=-=-=-= $time =-=-=-=-=-=-=-=",
                "-=-=-=-=\n";
            close $fh unless $no_daemon;
            if ($curr_dl >= $config{'EMAIL_ALERT_DANGER_LEVEL'}
                    and not $no_daemon) {
                unless ($analyze_mode and not $analysis_emails) {
                    my $subject;
                    if ($analyze_mode) {
                        $subject = "$config{'MAIL_ALERT_PREFIX'} " .
                            "DL$curr_dl (analysis " .
                            "mode) src: $src_subj dst: $dst_subj";
                    } else {
                        $subject = "$config{'MAIL_ALERT_PREFIX'} " .
                            "DL$curr_dl src: " .
                            "$src_subj dst: $dst_subj";
                    }
                    &send_mail($subject, $email_file,
                        $config{'EMAIL_ADDRESSES'}, $cmds{'mail'});

                    ### print the number of email alerts we have sent
                    open E, "> $ecount_file" or die "[*] Could not open ",
                        "$ecount_file: $!";

                    if ($config{'ENABLE_EMAIL_LIMIT_PER_DST'} eq 'Y') {
                        print E $scan_email_ctrs{$src}{$dst}{'email_ctr'}, "\n";
                    } else {
                        print E $scan_email_ctrs{$src}{'email_ctr'}, "\n";
                    }
                    close E;

                    if ($config{'ENABLE_EXT_SCRIPT_EXEC'} eq 'Y') {
                        if ($config{'EXEC_EXT_SCRIPT_PER_ALERT'} eq 'Y') {
                            &exec_external_script($src, $config{'EXTERNAL_SCRIPT'});
                            $scan_ext_exec{$src} = '';
                        } else {
                            unless (defined $scan_ext_exec{$src}) {
                                &exec_external_script($src, $config{'EXTERNAL_SCRIPT'});
                                $scan_ext_exec{$src} = '';
                            }
                        }
                    }
                }
            }

            ### we have sent an alert for $dst
            if ($config{'ALERT_ALL'} eq 'N') {
                $scan{$src}{$dst}{'alerted'} = 1;
            }
        }
    }
    return;
}

sub scan_timeouts() {
    my $do_timeout = 0;
    if ($config{'PERSISTENCE_CTR_THRESHOLD'} == 0) {
        $do_timeout = 1;
    } elsif ($check_interval_ctr % $config{'PERSISTENCE_CTR_THRESHOLD'} == 0) {
        $do_timeout = 1;
    }

    &delete_old_scans() if $do_timeout;
    return;
}

sub scan_logr_signatures() {
    my ($src, $dst, $email_fh, $log_sigs_file) = @_;

    my @del_sigs = ();
    my @log_sigs = ();
    my $found_sid = 0;

    for my $proto (keys %protocols) {
        next unless defined $scan{$src}{$dst}{$proto};
        my $hr = $scan{$src}{$dst}{$proto};

        next unless (defined $hr->{'sid'}
                or defined $hr->{'invalid_type'}
                or defined $hr->{'invalid_code'});

        print $email_fh "\n\n[+] " . uc($proto) . " scan signatures:\n\n";
        print STDERR "[+] scan_logr_signatures(): src: $src dst: $dst ",
            "proto: $proto\n" if $debug;

        my $sid_ctr = 0;
        for my $sid (keys %{$hr->{'sid'}}) {
            $sid_ctr++;
            if ($sid_ctr <= $config{'SIG_SID_SYSLOG_THRESHOLD'}) {
                $found_sid = 1;
            }
            for my $chain (keys %{$hr->{'sid'}->{$sid}}) {

                my $sig_hr      = '';
                my $email_chain = $chain;
                my $is_fwsnort  = $hr->{'sid'}->{$sid}
                    ->{$chain}->{'is_fwsnort'};

                if ($is_fwsnort) {
                    next unless defined $fwsnort_sigs{$sid};
                    $sig_hr = $fwsnort_sigs{$sid};
                } else {
                    next unless defined $sigs{$sid};
                    $sig_hr = $sigs{$sid};
                }

                my $dp    = 0;
                my $flags = 'NA';
                if ($proto eq 'tcp' or $proto eq 'udp'
                        or $proto eq 'udplite') {
                    $dp = $hr->{'sid'}->{$sid}->{$chain}->{'dp'};
                    if ($proto eq 'tcp') {
                        $flags = $hr->{'sid'}->{$sid}->{$chain}->{'flags'};
                    }
                }

                my $pkts = $hr->{'sid'}->{$sid}->{$chain}->{'pkts'};

                ### string for the <ip>_signatures file
                push @log_sigs, "$hr->{'sid'}->{$sid}->{$chain}->{'time'} " .
                    qq|$sid $pkts "$sig_hr->{'msg'}" $chain $proto $dp "$flags" | .
                    "$is_fwsnort $sig_hr->{'is_psad_id'}";

                if ($config{'ENABLE_SIG_MSG_SYSLOG'} eq 'Y'
                        and $sid_ctr <= $config{'SIG_MSG_SYSLOG_THRESHOLD'}) {
                    my $sig_log_str = "src: $src signature match: " .
                        qq|"$sig_hr->{'msg'}" (sid: $sid) $proto|;

                    if ($proto eq 'tcp' or $proto eq 'udp'
                            or $proto eq 'udplite') {
                        $sig_log_str .= " port: $dp";
                    }

                    if ($is_fwsnort) {
                        $sig_log_str .= ' fwsnort';

                        if (defined $hr->{'sid'}->{$sid}->{$chain}
                                ->{'fwsnort_estab'}) {

                            if ($chain eq 'INPUT') {
                                $sig_log_str .= ' chain: FWSNORT_INPUT';
                                $email_chain = 'FWSNORT_INPUT';
                            } elsif ($chain eq 'FORWARD') {
                                $sig_log_str .= ' chain: FWSNORT_FORWARD';
                                $email_chain = 'FWSNORT_FORWARD';
                            } elsif ($chain eq 'OUTPUT') {
                                $sig_log_str .= ' chain: FWSNORT_OUTPUT';
                                $email_chain = 'FWSNORT_OUTPUT';
                            }

                            if ($hr->{'sid'}->{$sid}
                                    ->{$chain}->{'fwsnort_estab'}) {

                                $sig_log_str .= '_ESTAB';
                                $email_chain .= '_ESTAB';
                            }
                        }

                        if (defined $hr->{'sid'}->{$sid}->{$chain}
                                ->{'fwsnort_rnum'} and $hr->{'sid'}->{$sid}
                                ->{$chain}->{'fwsnort_rnum'}) {

                            $sig_log_str .= ' rule: ' .
                                $hr->{'sid'}->{$sid}->{$chain}
                                ->{'fwsnort_rnum'};
                        }
                    }

                    ### write the signature match to syslog
                    &sys_log($sig_log_str);
                }

                print $email_fh qq|   "$sig_hr->{'msg'}"\n|;

                if ($proto eq 'tcp' or $proto eq 'udp'
                        or $proto eq 'udplite') {
                    if ($chain eq 'INPUT') {
                        if (defined $local_ports{$proto}
                                and defined $local_ports{$proto}{$dp}) {
                            print $email_fh "       dst port:  $dp (server ",
                                  "bound to local port!)\n";
                            print $email_fh "       flags:     $flags\n"
                                if $proto eq 'tcp';
                        } else {
                            print $email_fh "       dst port:  $dp (no server ",
                                  "bound to local port)\n";
                            print $email_fh "       flags:     $flags\n"
                                if $proto eq 'tcp';
                        }
                    } else {
                        print $email_fh "       dst port:  $dp\n";
                        print $email_fh "       flags:     $flags\n"
                            if $proto eq 'tcp';
                    }
                }

                if ($is_fwsnort) {
                    for my $content (@{$sig_hr->{'content'}}) {
                        print $email_fh qq(       content:   "$content"\n);
                    }
                }
                my $sid_str = '';
                if ($sig_hr->{'is_psad_id'}) {
                    $sid_str = "       psad_id:   $sid";
                } else {
                    $sid_str = "       sid:       $sid";
                }
                if (defined $sig_hr->{'psad_derived_sids'}) {
                    $sid_str .= ' (derived from: ';
                    $sid_str .= "$_ " for @{$sig_hr->{'psad_derived_sids'}};
                    $sid_str =~ s|\s*$||;
                    $sid_str .= ')';
                }
                print $email_fh "$sid_str\n",
                    "       chain:     $email_chain\n",
                    "       packets:   $pkts\n";

                if ($sig_hr->{'classtype'}) {
                    print $email_fh "       classtype: $sig_hr->{'classtype'}\n";
                }
                if (defined $sig_hr->{'reference'}
                        and $sig_hr->{'reference'}) {
                    for my $reftype (keys %{$sig_hr->{'reference'}}) {
                        my $baseurl = '';
                        if (defined $snort_ref_baseurl{$reftype}) {
                            $baseurl = $snort_ref_baseurl{$reftype};
                        } else {
                            next;
                        }
                        for my $ref (@{$sig_hr->{'reference'}->{$reftype}}) {
                            print $email_fh "       reference: ($reftype) ",
                                  "${baseurl}$ref\n";
                        }
                    }
                }
                print $email_fh "\n";
            }
        }
        if (defined $hr->{'invalid_type'}) {
            for my $type (keys %{$hr->{'invalid_type'}}) {
                for my $chain (keys %{$hr->{'invalid_type'}->{$type}}) {
                    my $pkts = $hr->{'invalid_type'}->
                    {$type}->{$chain}->{'pkts'};
                    print $email_fh
                        qq|   Invalid ICMP type "$type" chain=$chain packets=$pkts\n|;
                }
            }
        }
        if (defined $hr->{'invalid_code'}) {
            for my $type (keys %{$hr->{'invalid_code'}}) {
                for my $code (keys %{$hr->{'invalid_code'}->{$type}}) {
                    for my $chain (keys %{$hr->{'invalid_code'}->{$type}->{$code}}) {
                        my $pkts = $hr->{'invalid_code'}->
                        {$type}->{$code}->{$chain}->{'pkts'};
                        if ($proto eq 'icmp') {
                            print $email_fh qq|   Invalid ICMP code "$code" for ICMP |,
                                qq|"$valid_icmp_types{$type}{'text'}" packet\n|,
                                "       chain=$chain packets=$pkts\n";
                         } elsif ($proto eq 'icmp6') { 
                            print $email_fh qq|   Invalid ICMP code "$code" for ICMP |,
                                qq|"$valid_icmp6_types{$type}{'text'}" packet\n|,
                                "       chain=$chain packets=$pkts\n";
                        }
                    }
                }
            }
        }
        push @del_sigs, $hr;
    }

    if (@log_sigs) {
        open LS, ">> $log_sigs_file.tmp" or
            die "[*] Could not open $log_sigs_file: $!";
        print LS "#\n# Format: <sig_time> <sid> <matches> \"<msg>\" <chain> ",
            "<proto> <dst port> \"<flags>\" <is_fwsnort> <is_psad>\n#\n\n";
        for my $str (@log_sigs) {
            print LS $str, "\n";
        }
        close LS;
        move "$log_sigs_file.tmp", $log_sigs_file;
    }
    unless ($config{'SHOW_ALL_SIGNATURES'} eq 'Y') {
        for my $hr (@del_sigs) {
            ### need to delete the current signature so it
            ### won't show up in the next alert
            delete $hr->{'sid'}
                if defined $hr->{'sid'};
            delete $hr->{'invalid_type'}
                if defined $hr->{'invalid_type'};
            delete $hr->{'invalid_code'}
                if defined $hr->{'invalid_code'};
        }
    }
    close LS;
    return;
}

sub print_chains_and_prefixes() {
    my ($chain_hr, $fh) = @_;
    for my $chain (keys %$chain_hr) {
        for my $prefix (keys %{$chain_hr->{$chain}}) {
            my $count = $chain_hr->{$chain}->{$prefix};
            if ($prefix eq '*noprfx*') {
                printf $fh "%${log_len}s%s\n", 'iptables chain: ',
                    "$chain, $count packets";
            } else {
                my $print_chain  = $chain;
                my $fwsnort_rnum = 0;
                if ($prefix =~ /$config{'SNORT_SID_STR'}/) {
                    if ($prefix =~ /\[(\d+)\]/) {
                        $fwsnort_rnum = $1;
                    }
                    if ($chain eq 'INPUT') {
                        $print_chain = 'FWSNORT_INPUT';
                    } elsif ($chain eq 'FORWARD') {
                        $print_chain = 'FWSNORT_FORWARD';
                    } elsif ($chain eq 'OUTPUT') {
                        $print_chain = 'FWSNORT_OUTPUT';
                    }
                    if ($prefix =~ /ESTAB/) {
                        $print_chain .= '_ESTAB';
                    }
                }
                printf $fh "%${log_len}s%s\n", 'iptables chain: ',
                    qq/$print_chain (prefix "$prefix"), $count packets/;
                if ($fwsnort_rnum) {
                    printf $fh "%${log_len}s%s\n", 'fwsnort rule: ',
                        $fwsnort_rnum;
                }
            }
        }
    }
    return;
}

sub exec_external_script() {
    my ($src, $cmd) = @_;

    $cmd =~ s/SRCIP/$src/;

    my $pid;
    local $SIG {'ALRM'} = sub {kill 15, $pid or die "[*] kill: $!";
        die "[*] External script timeout.\n"};
    eval {
        $pid = fork();

        die "Could not fork for external script: $!" unless defined $pid;

        unless ($pid) {
            exec qq{$cmd};
            die "script exec failed: $!";
        }
        alarm 30;

        waitpid $pid => 0;

        alarm 0;
    };
    return;
}

sub renew_auto_blocked_ips() {

    ### note that if we are renewing IP blocking rules, we just use
    ### the AUTO_BLOCK_TIMEOUT value initially and then the AUTO_BLOCK_DLN...
    ### values will take over as psad gets up and running
    my $timeout_str = '.';
    if ($config{'AUTO_BLOCK_TIMEOUT'} > 0) {
        $timeout_str = "for $config{'AUTO_BLOCK_TIMEOUT'} seconds.";
    } else {
        $timeout_str = '(unlimited time).';
    }

    if ($config{'IPTABLES_BLOCK_METHOD'} eq 'Y'
            and -e $config{'AUTO_BLOCK_IPT_FILE'}) {
        open B, "< $config{'AUTO_BLOCK_IPT_FILE'}" or
            die "[*] Could not open $config{'AUTO_BLOCK_IPT_FILE'}: $!";
        my @lines = <B>;
        close B;

        for my $line (@lines) {
            if ($line =~ /^\s*($ipv4_re)\s+(\d+)/) {
                my $ip = $1;
                my $orig_block_time = $2;

                if ($config{'AUTO_BLOCK_TIMEOUT'} == 0) {

                    ### set the DL here to the minimum currently required
                    $scan_dl{$ip} = $config{'AUTO_IDS_DANGER_LEVEL'};

                    ### block the IP address (note that checks are built
                    ### into this function to not add a duplicate rule)
                    &ipt_block($ip, 'renew');

                    ### reset the block time to the original time that
                    ### the rule was added so the rule does not stay
                    ### around longer than it should.
                    $auto_blocked_ips{$ip} = $orig_block_time;
                } else {
                    if ((time() - $orig_block_time)
                            > $config{'AUTO_BLOCK_TIMEOUT'}) {
                        ### timeout has expired, so we should remove any
                        ### existing blocking rule
                        &ipt_rm_block($ip);

                    } else {

                        ### set the DL here to the minimum currently required
                        $scan_dl{$ip} = $config{'AUTO_IDS_DANGER_LEVEL'};

                        ### block the IP address (note that checks are built
                        ### into this function to not add a duplicate rule)
                        &ipt_block($ip, 'renew');

                        $auto_blocked_ips{$ip} = $orig_block_time;
                    }
                }
            }
        }
    }
    if ($config{'TCPWRAPPERS_BLOCK_METHOD'} eq 'Y'
            && -e $config{'ETC_HOSTS_DENY_FILE'}) {
        open B, "< $config{'ETC_HOSTS_DENY_FILE'}" or
            die "[*] Could not open $config{'ETC_HOSTS_DENY_FILE'}: $!";
        my @lines = <B>;
        close B;
        for my $line (@lines) {
            if ($line =~ /^\s*($ipv4_re)\s+(\d+)/) {
                my $ip = $1;
                my $orig_block_time = $2;

                if ($config{'AUTO_BLOCK_TIMEOUT'} == 0) {

                    ### set the DL here to the minimum currently required
                    $scan_dl{$ip} = $config{'AUTO_IDS_DANGER_LEVEL'};

                    ### block the IP address (note that checks are built
                    ### into this function to not add a duplicate rule)
                    if (&tcpwr_test_block($ip)) {
                        $auto_blocked_ips{$ip} = $orig_block_time
                            unless defined $auto_blocked_ips{$ip};
                    } else {
                        &sys_log("renewing tcpwrappers auto-block " .
                            "against $ip $timeout_str");
                        &tcpwr_block($ip);
                        if ($config{'ENABLE_RENEW_BLOCK_EMAILS'} eq 'Y'
                                and defined $scan_dl{$ip}
                                and $scan_dl{$ip} >= $config{'EMAIL_ALERT_DANGER_LEVEL'}) {
                            &send_mail("$config{'MAIL_STATUS_PREFIX'} RENEWED " .
                                "tcpwrappers BLOCK against $ip $timeout_str", '',
                                $config{'EMAIL_ADDRESSES'}, $cmds{'mail'});
                        }
                        $auto_blocked_ips{$ip} = $orig_block_time;
                    }
                } else {
                    if ((time() - $orig_block_time)
                            > $config{'AUTO_BLOCK_TIMEOUT'}) {
                        ### timeout has expired, so we should remove any
                        ### existing blocking rule
                        &tcpwr_rm_block($ip);

                    } else {

                        ### set the DL here to the minimum currently required
                        $scan_dl{$ip} = $config{'AUTO_IDS_DANGER_LEVEL'};

                        ### block the IP address (note that checks are built
                        ### into this function to not add a duplicate rule)
                        if (&tcpwr_test_block($ip)) {
                            $auto_blocked_ips{$ip} = $orig_block_time
                                unless defined $auto_blocked_ips{$ip};
                        } else {
                            &sys_log("renewing tcpwrappers auto-block " .
                                "against $ip $timeout_str");
                            &tcpwr_block($ip);
                            if ($config{'ENABLE_RENEW_BLOCK_EMAILS'} eq 'Y'
                                    and defined $scan_dl{$ip}
                                    and $scan_dl{$ip} >= $config{'EMAIL_ALERT_DANGER_LEVEL'}) {
                                &send_mail("$config{'MAIL_STATUS_PREFIX'} RENEWED " .
                                    "tcpwrappers BLOCK against $ip $timeout_str", '',
                                    $config{'EMAIL_ADDRESSES'}, $cmds{'mail'});
                            }
                            $auto_blocked_ips{$ip} = $orig_block_time;
                        }
                    }
                }
            }
        }
    }
    return;
}

sub sockwrite_flush_auto_rules() {
    if (-e $config{'PSAD_PID_FILE'}) {
        if (&is_running($config{'PSAD_PID_FILE'})
                and $config{'ENABLE_AUTO_IDS'} eq 'Y') {
            print "[+] Flushing psad chains via running psad daemon within\n",
                "    $config{'CHECK_INTERVAL'} seconds.\n";
            die "[*] $config{'AUTO_IPT_SOCK'} file is missing. Did something ",
                "remove it?\n    You should stop psad, run the -F command ",
                "again, then restart psad." unless -e $config{'AUTO_IPT_SOCK'};
            ### open domain socket with running psad process
            my $sock = IO::Socket::UNIX->new($config{'AUTO_IPT_SOCK'})
                or die "[*] Could not acquire $config{'AUTO_IPT_SOCK'} ",
                "socket: $!";
            if ($fw_del_chains) {
                print $sock "flush delchains\n";
            } else {
                print $sock "flush\n";
            }
            close $sock;
            return $EXIT_SUCCESS;
        }
    }

    ### if we make it here then we have to flush manually because
    ### it looks like psad is not running.
    if ($fw_del_chains) {
        print "[+] Flushing and deleting psad chains.\n";
    } else {
        print "[+] Flushing psad chains.\n";
    }
    &flush_auto_blocked_ips();
    return $EXIT_SUCCESS;
}

sub flush_auto_blocked_ips() {

    my $ipt = new IPTables::ChainMgr(%ipt_opts)
        or die '[*] Could not acquire IPTables::ChainMgr object.';

    &sys_log('flushing existing psad iptables ' .
        'auto-response chains');

    if (@ipt_config) {
        for my $hr (@ipt_config) {
            my $table      = $hr->{'table'};
            my $from_chain = $hr->{'from_chain'};
            my $to_chain   = $hr->{'to_chain'};

            my ($rv, $out_ar, $err_ar)
                = $ipt->chain_exists($table, $to_chain);

            if ($rv) {
                if ($fw_del_chains) {
                    ($rv, $out_ar, $err_ar) = $ipt->delete_chain($table,
                        $from_chain, $to_chain);
                    if ($rv) {
                        &sys_log("deleted $table table $to_chain chain");
                    } else {
                        &sys_log("could not delete $table " .
                            "table $to_chain chain");
                        &sys_log_mline($err_ar);
                    }
                } else {
                    ($rv, $out_ar, $err_ar)
                        = $ipt->flush_chain($table, $to_chain);
                    if ($rv) {
                        &sys_log("flushed: $to_chain");
                    } else {
                        &sys_log("could not flush: $to_chain");
                        &sys_log_mline($err_ar);
                    }
                }
            }
        }
    }

    ### zero out the in-memory cache of blocked addresses
    %auto_blocked_ips = ();

    if (-e $config{'AUTO_BLOCK_IPT_FILE'}) {
        ### we have removed the iptables block rules, so truncate
        ### the cache file.
        &truncate_file($config{'AUTO_BLOCK_IPT_FILE'});
    }

    if (-e $config{'AUTO_BLOCK_TCPWR_FILE'}) {
        my $found_blocked = 0;
        &sys_log("removing tcpwrapper auto-generated block rules.");
        open B, "< $config{'AUTO_BLOCK_TCPWR_FILE'}" or
            die "[*] Could not open $config{'AUTO_BLOCK_TCPWR_FILE'}: $!";
        my @lines = <B>;
        close B;
        for my $line (@lines) {
            if ($line =~ /($ipv4_re)/) {
                my $ip = $1;
                ### remove block rules for $ip if it has been blocked
                &tcpwr_rm_block($ip) if &tcpwr_test_block($ip);
                $found_blocked = 1;
            }
        }
        ### we have removed the tcpwrapper block rules, so truncate
        ### the cache file.
        &truncate_file($config{'AUTO_BLOCK_TCPWR_FILE'});
        unless ($found_blocked) {
            &sys_log("currently there are no auto-generated " .
                "tcpwrapper blocking rules in effect.");
        }
    }
    return;
}

sub get_auto_response_domain_sock() {

    ### $ipt_sock is global
    $ipt_sock = IO::Socket::UNIX->new(
        Type   => SOCK_STREAM,
        Local  => $config{'AUTO_IPT_SOCK'},
        Listen => SOMAXCONN,
        Timeout => .1
    ) or die "[*] Could not acquire auto-response domain ",
        "socket $config{'AUTO_IPT_SOCK'}: $!";
    return;
}

sub check_auto_response_sock() {

    ### we expect that the AUTO_IPT_SOCK file should exist
    ### in the filesystem.  If not, then something deleted it
    ### and we can recover by reopening it.
    return if -e $config{'AUTO_IPT_SOCK'};

    &sys_log("domain socket $config{'AUTO_IPT_SOCK'} does not " .
        "exist (a separate process must have deleted it), recreating it.");

    close $ipt_sock;

    ### reopen the socket
    &get_auto_response_domain_sock();

    return;
}

sub ipt_block() {
    my ($ip, $renewed_status) = @_;

    return unless $ip and $ip =~ /^$ipv4_re$/;

    ### see if the IP is already blocked
    if (defined $auto_blocked_ips{$ip}) {
        print STDERR "[-] ipt_block($ip) already blocked.\n" if $debug;
        return;
    }

    print STDERR "[+] ipt_block($ip)\n" if $debug;

    my $ipt = new IPTables::ChainMgr(%ipt_opts)
        or die '[*] Could not acquire IPTables::ChainMgr object.';

    my $block_success   = 0;
    my $already_blocked = 0;

    my ($timeout, $timeout_str) = &get_block_timeout($scan_dl{$ip});

    if ($config{'IPTABLES_PREREQ_CHECK'} > 1) {
        $iptables_prereq_check++;
        $iptables_prereq_check = 1
            if $iptables_prereq_check == $config{'IPTABLES_PREREQ_CHECK'};
    }

    ### add block rule for $ip unless it is already blocked
    for my $hr (@ipt_config) {
        my $target     = $hr->{'target'};
        my $direction  = $hr->{'direction'};
        my $table      = $hr->{'table'};
        my $from_chain = $hr->{'from_chain'};
        my $to_chain   = $hr->{'to_chain'};
        my $jump_rule_position = $hr->{'jump_rule_position'};
        my $auto_rule_position = $hr->{'auto_rule_position'};

        my $src = '';
        my $dst = '';

        if ($config{'IPTABLES_PREREQ_CHECK'} == 1
                or $iptables_prereq_check == 1) {
            print STDERR "[+] iptables chains and jump rule check.\n"
                if $debug;

            ### make sure "to_chain" exists
            my ($rv, $out_ar, $err_ar)
                = $ipt->create_chain($table, $to_chain);

            unless ($rv) {
                my $msg = "could not create $table $to_chain chain";
                &sys_log($msg);
                &sys_log_mline($err_ar);
                print STDERR "[-] ipt_block(): $msg\n" if $debug;
                next;
            }

            unless ($test_mode) {
                ### add jump rule to the "to_chain" from the "from_chain"
                ($rv, $out_ar, $err_ar) = $ipt->add_jump_rule($table,
                    $from_chain, $jump_rule_position, $to_chain);

                unless ($rv) {
                    my $msg = "could not add jump rule to $to_chain chain";
                    &sys_log($msg);
                    &sys_log_mline($err_ar);
                    print STDERR "[-] ipt_block(): $msg\n" if $debug;
                    next;
                }
            }
        }

        if ($direction eq 'src' or $direction eq 'both') {
            $src = $ip;
            $dst = '0.0.0.0/0';
        } elsif ($direction eq 'dst') {
            $src = '0.0.0.0/0';
            $dst = $ip;
        }

        my ($rv, $num_chain_rules) = $ipt->find_ip_rule($src, $dst,
                $table, $to_chain, $target);

        if ($rv) {
            print STDERR "[-] Test1, IP rule ($src->$dst $to_chain) ",
                "already exists.\n" if $debug;
            $already_blocked = 1;
        } else {
            my ($rv, $out_ar, $err_ar) = $ipt->add_ip_rule($src, $dst,
                $auto_rule_position, $table, $to_chain, $target);
            if ($rv) {
                print STDERR "[+] Test1 block success.\n" if $debug;
                $block_success = 1;
            } else {
                my $msg = "could not add block rule for $src -> $dst";
                &sys_log($msg);
                &sys_log_mline($err_ar);
                print STDERR "[-] ipt_block(): $msg\n" if $debug;
            }
        }

        if ($direction eq 'both') {
            ### need to add reverse rule for FORWARD chain
            my $src2 = $dst;
            my $dst2 = $src;
            my ($rv, $num_chain_rules) = $ipt->find_ip_rule($src2,
                    $dst2, $table, $to_chain, $target);

            if ($rv) {
                print STDERR "[-] Test2, IP rule ($src2->$dst2 $to_chain) ",
                    "already exists.\n" if $debug;
                $already_blocked = 1;
            } else {
                my ($rv, $out_ar, $err_ar) = $ipt->add_ip_rule($src2, $dst2,
                    $auto_rule_position, $table, $to_chain, $target);
                if ($rv) {
                    print STDERR "[+] Test2 block success.\n" if $debug;
                    $block_success = 1;
                } else {
                    my $msg = "could not add block rule for $src2 -> $dst2";
                    &sys_log($msg);
                    &sys_log_mline($err_ar);
                    print STDERR "[-] ipt_block(): $msg\n" if $debug;
                }
            }
        }
    }
    if ($already_blocked) {
        &sys_log("block rule for IP: $ip already exists");
        print STDERR "[-] Block rule for IP: $ip already exists\n"
            if $debug;
    } elsif ($block_success) {

        ### make sure the ip is in the auto_blocked_ips cache (the ip
        ### may have come from the command line with --fw-block-ip
        ### instead of through the iptables log).  Also, don't re-define
        ### the block time if it has already been defined (note that we
        ### may instantiated multiple block actions in the for loop
        ### above).
        $auto_blocked_ips{$ip} = time()
            unless defined $auto_blocked_ips{$ip};

        my $mail_msg = "iptables auto-block against $ip $timeout_str";
        if ($renewed_status) {
            $mail_msg = "renewed $mail_msg";
        } else {
            $mail_msg = "added $mail_msg";
        }

        if ($config{'ENABLE_AUTO_IDS_EMAILS'} eq 'Y') {
            if ($renewed_status) {
                if ($config{'ENABLE_RENEW_BLOCK_EMAILS'} eq 'Y'
                        and defined $scan_dl{$ip}
                        and $scan_dl{$ip} >= $config{'EMAIL_ALERT_DANGER_LEVEL'}) {
                    &send_mail("$config{'MAIL_STATUS_PREFIX'} $mail_msg",
                        '', $config{'EMAIL_ADDRESSES'}, $cmds{'mail'});
                }
            } else {
                if (defined $scan_dl{$ip}
                        and $scan_dl{$ip} >= $config{'EMAIL_ALERT_DANGER_LEVEL'}) {
                    &send_mail("$config{'MAIL_STATUS_PREFIX'} $mail_msg", '',
                        $config{'EMAIL_ADDRESSES'}, $cmds{'mail'});
                }
            }
        }
        &sys_log($mail_msg);

        if ($config{'ENABLE_EXT_BLOCK_SCRIPT_EXEC'} eq 'Y') {
            &exec_external_script($ip, $config{'EXTERNAL_BLOCK_SCRIPT'});
        }

        ### write the ip out to the auto blocked file
        &diskwrite_blocked_ip($ip,
            $config{'AUTO_BLOCK_IPT_FILE'}, $renewed_status);

        print STDERR "[+] ipt_block(): added block for $ip\n"
            if $debug;
    } else {
        &sys_log("could not add iptables " .
            "block rule for: $ip");
        print STDERR "[-] Could not add iptables block rule for: $ip\n"
            if $debug;
    }
    return;
}

sub ipt_rm_block() {
    my $ip = shift;

    my $ipt = new IPTables::ChainMgr(%ipt_opts)
        or die '[*] Could not acquire IPTables::ChainMgr object.';

    print STDERR "[+] ipt_rm_block($ip)\n" if $debug;

    ### delete block rule for $ip
    my $rm_block = 0;
    for my $hr (@ipt_config) {
        my $target    = $hr->{'target'};
        my $direction = $hr->{'direction'};
        my $table     = $hr->{'table'};
        my $to_chain  = $hr->{'to_chain'};

        my $src = '';
        my $dst = '';
        if ($direction eq 'src' or $direction eq 'both') {
            $src = $ip;
            $dst = '0.0.0.0/0';
        } elsif ($direction eq 'dst') {
            $src = '0.0.0.0/0';
            $dst = $ip;
        }

        my ($rv, $out_ar, $err_ar) = $ipt->delete_ip_rule($src, $dst,
            $table, $to_chain, $target);
        if ($rv) {
            $rm_block = 1;
        } else {
            my $msg = "could not delete rule for $src -> $dst";
            &sys_log($msg);
            &sys_log_mline($err_ar);
            print STDERR "[-] ipt_rm_block(): $msg\n" if $debug;
        }

        if ($direction eq 'both') {
            ### need to delete reverse rule for FORWARD chain
            my $src2 = $dst;
            my $dst2 = $src;
            ($rv, $out_ar, $err_ar) = $ipt->delete_ip_rule($src2, $dst2,
                $table, $to_chain, $target);
            if ($rv) {
                $rm_block = 1;
            } else {
                my $msg = "could not delete rule for $src -> $dst";
                &sys_log($msg);
                &sys_log_mline($err_ar);
                print STDERR "[-] ipt_rm_block(): $msg\n" if $debug;
            }
        }
    }

    ### delete the ip from the hash (if new packets are seen
    ### from the same ip, then the hash will be updated again
    ### in check_scan()).
    delete $auto_blocked_ips{$ip}
        if defined $auto_blocked_ips{$ip};

    &diskwrite_rm_blocked_ip($ip, $config{'AUTO_BLOCK_IPT_FILE'});

    if ($rm_block) {
        unless ($flush_fw) {
            ### don't send timeout email if we are manually flushing
            ### the auto-block rules from the command line with --Flush.
            &sys_log("removed iptables auto-block against $ip");
            if ($config{'ENABLE_AUTO_IDS_EMAILS'} eq 'Y'
                    and defined $scan_dl{$ip}
                    and $scan_dl{$ip} >= $config{'EMAIL_ALERT_DANGER_LEVEL'}) {
                &send_mail("$config{'MAIL_STATUS_PREFIX'} removed " .
                    "iptables block against $ip", '',
                    $config{'EMAIL_ADDRESSES'}, $cmds{'mail'});
            }
        }
        print STDERR "[+] ipt_rm_block(): removed iptables block ",
            "against $ip\n" if $debug;

        return 1;
    }

    &sys_log('warning: could not remove iptables ' .
        "block rule for $ip");
    print STDERR "[-] Could not remove iptables block rule for $ip\n"
        if $debug;
    return 0;
}

sub ipt_list_auto_chains() {

    my $ipt = new IPTables::ChainMgr(%ipt_opts)
        or die '[*] Could not acquire IPTables::ChainMgr object.';

    print "[+] Listing chains from IPT_AUTO_CHAIN keywords...\n";
    if ($config{'ENABLE_AUTO_IDS'} eq 'N') {
        print '[-] NOTE: ENABLE_AUTO_IDS is currently disabled ',
            "in $config_file\n";
    }
    print "\n";
    for my $hr (@ipt_config) {
        my $table    = $hr->{'table'};
        my $to_chain = $hr->{'to_chain'};

        my ($rv, $out_ar, $err_ar)
            = $ipt->chain_exists($table, $to_chain);

        if ($rv) {
            ($rv, $out_ar, $err_ar) =
                $ipt->run_ipt_cmd("$ipt->{'_cmd'} -t " .
                    "$table -n -L $to_chain -v");
            if ($rv and $out_ar) {
                print for @$out_ar;
            }
            print "\n";
        } else {
            print "[-] Table: $table, chain: $to_chain, does not exist\n";
        }
    }
    return $EXIT_SUCCESS;
}

sub check_ipt_cmd() {
    my $lines_ar = shift;

    my %uniq_cmds = ();
    for my $line (@$lines_ar) {
        chomp $line;
        next if defined $uniq_cmds{$line};
        $uniq_cmds{$line} = '';
        if ($line =~ /flush/i) {
            my $del_chains = 0;
            $del_chains = 1 if $line =~ /delchains/i;
            &flush_auto_blocked_ips($del_chains);
        } else {
            if ($line =~ m|add\s+($ipv4_re)\s*$|i
                    or $line =~ m|add\s+($ipv4_re/\d+)\s*$|i
                    or $line =~ m|add\s+($ipv4_re/$ipv4_re)\s*$|i) {
                my $ip_net = $1;

                my $ip_only = '';
                $ip_only = $1 if $line =~ /\s($ipv4_re)/;

                ### instantiate the blocking rule, force a DL value
                ### equal to the AUTO_IDS_DANGER_LEVEL setting
                $scan_dl{$ip_only} = $config{'AUTO_IDS_DANGER_LEVEL'};

                &ipt_block($1, '');
            } elsif ($line =~ m|del\s+($ipv4_re)\s*$|i
                    or $line =~ m|del\s+($ipv4_re/\d+)\s*$|i
                    or $line =~ m|del\s+($ipv4_re/$ipv4_re)\s*$|i) {
                ### remove the blocking rule
                &ipt_rm_block($1);
            }
        }
    }
    return;
}

### this only gets used when issuing --fw-block
### from the command line.
sub sockwrite_add_ipt_block_ip() {

    die "[*] --fw-block-ip takes either an IP or subnet as an argument."
        unless $fw_block_ip =~ /$ipv4_re/;

    my $block_ip   = '';
    my $block_mask = '';
    my $block_net  = '';

    if ($fw_block_ip =~ m|^\s*($ipv4_re)\s*$|) {
        $block_ip   = $1;
        $block_mask = '32';
        $block_net = new NetAddr::IP($1);
    } elsif ($fw_block_ip =~ m|^\s*($ipv4_re)/($ipv4_re)\s*$|) {
        $block_ip   = $1;
        $block_mask = $2;
        $block_net = new NetAddr::IP($1, $2);
    } elsif ($fw_block_ip =~ m|^\s*($ipv4_re)/(\d+)\s*$|) {
        $block_ip   = $1;
        $block_mask = $2;
        $block_net = new NetAddr::IP($1, $2);
    } else {
        die "[*] Badly formatted block IP: $fw_block_ip";
    }

    if ($block_net->masklen() != 32) {
        ### a subnet was given on the command line, so make
        ### sure we were also given a network address (iptables
        ### converts to the network address in -nL output)
        $fw_block_ip = $block_net->network()->cidr();
    }

    ### import auto_dl file
    &import_auto_dl();

    ### make sure $fw_block_ip is not supposed to be ignored
    NET: for my $net (keys %auto_dl) {
        my $dl   = $auto_dl{$net}{'dl'};
        my $mask = $auto_dl{$net}{'mask'};  ### may be a /32 (single IP)

        next NET unless $dl == 0;  ### only care about the ignored IPs/nets

        if ($block_net->within($auto_dl_ip_objs{$net})) {
            die "[*] $fw_block_ip overlaps with whitelisted ",
                "$net/$mask in $config{'AUTO_DL_FILE'}";
        }
    }

    if (-e $config{'PSAD_PID_FILE'}) {
        if ($config{'ENABLE_AUTO_IDS'} ne 'Y') {
            die "[*] ENABLE_AUTO_IDS is not set to 'Y', exiting.";
        }
        if (&is_running($config{'PSAD_PID_FILE'})) {
            print "[+] Writing $fw_block_ip to socket; psad will add the IP\n",
                "    within $config{'CHECK_INTERVAL'} seconds.\n";

            ### open domain socket with running psad process
            my $sock = IO::Socket::UNIX->new($config{'AUTO_IPT_SOCK'})
                or die "[*] Could not acquire $config{'AUTO_IPT_SOCK'} ",
                "socket: $!";
            print $sock "add $fw_block_ip\n";
            close $sock;
        } else {
            print "[-] There is no running psad process. Exiting.\n";
        }
    } else {
        print "[-] There is no running psad process. Exiting.\n";
    }
    return $EXIT_SUCCESS;
}

sub sockwrite_rm_ipt_block_ip() {

    die "[*] --fw-rm-block-ip takes an IP/subnet as an argument."
        unless $fw_rm_block_ip =~ /$ipv4_re/;

    my $rm_block_ip   = '';
    my $rm_block_mask = '';
    my $rm_block_net  = '';

    if ($fw_rm_block_ip =~ m|^\s*($ipv4_re)\s*$|) {
        $rm_block_ip   = $1;
        $rm_block_mask = '32';
        $rm_block_net = new NetAddr::IP($1);
    } elsif ($fw_rm_block_ip =~ m|^\s*($ipv4_re)/($ipv4_re)\s*$|) {
        $rm_block_ip   = $1;
        $rm_block_mask = $2;
        $rm_block_net = new NetAddr::IP($1, $2);
    } elsif ($fw_rm_block_ip =~ m|^\s*($ipv4_re)/(\d+)\s*$|) {
        $rm_block_ip   = $1;
        $rm_block_mask = $2;
        $rm_block_net = new NetAddr::IP($1, $2);
    } else {
        die "[*] Badly formatted rm block IP: $fw_rm_block_ip";
    }

    if ($rm_block_net->masklen() != '32') {
        ### a subnet was given on the command line, so make
        ### sure we were also given a network address (iptables
        ### converts to the network address in -nL output)
        $fw_rm_block_ip = $rm_block_net->network()->cidr();
    }

    if (-e $config{'PSAD_PID_FILE'}) {
        if ($config{'ENABLE_AUTO_IDS'} ne 'Y') {
            die "[*] ENABLE_AUTO_IDS is not set to 'Y', exiting.";
        }
        if (&is_running($config{'PSAD_PID_FILE'})) {
            print "[+] Writing $fw_rm_block_ip to socket; psad will remove the IP\n",
                "    within $config{'CHECK_INTERVAL'} seconds.\n";

            ### open domain socket with running psad process
            my $sock = IO::Socket::UNIX->new($config{'AUTO_IPT_SOCK'})
                or die "[*] Could not acquire $config{'AUTO_IPT_SOCK'} ",
                "socket: $!";
            print $sock "del $fw_rm_block_ip\n";
            close $sock;
            return $EXIT_SUCCESS;
        }
    }
    return $EXIT_SUCCESS;
}

sub tcpwr_test_block() {
    my $ip = shift;
    open T, "< $config{'ETC_HOSTS_DENY_FILE'}" or die "[*] Could not open ",
        "$config{'ETC_HOSTS_DENY_FILE'}: $!";
    my @lines = <T>;
    close T;
    for my $line (@lines) {
        chomp $line;
        return 1 if $line =~ /ALL:\s+$ip$/;
    }
    return 0;
}

sub tcpwr_block() {
    my $ip = shift;
    open T, ">> $config{'ETC_HOSTS_DENY_FILE'}" or die "[*] Could not open ",
        "$config{'ETC_HOSTS_DENY_FILE'}: $!";
    print T "ALL: $ip\n";
    close T;
    return;
}

sub tcpwr_rm_block() {
    my $ip = shift;
    my $rv = 0;
    open T, "< $config{'ETC_HOSTS_DENY_FILE'}" or die '[*] Could not open ',
        "$config{'ETC_HOSTS_DENY_FILE'}: $!";
    my @lines = <T>;
    close T;

    my $orig_perms = (stat($config{'ETC_HOSTS_DENY_FILE'}))[2] & 07777;

    open T, "> $config{'ETC_HOSTS_DENY_FILE'}.tmp" or die '[*] Could not open ',
        "$config{'ETC_HOSTS_DENY_FILE'}.tmp: $!";
    for my $line (@lines) {
        chomp $line;
        if ($line =~ /ALL:\s+$ip$/) {
            &diskwrite_rm_blocked_ip($ip, $config{'AUTO_BLOCK_TCPWR_FILE'});
            if ($config{'ENABLE_AUTO_IDS_EMAILS'} eq 'Y'
                    and defined $scan_dl{$ip}
                    and $scan_dl{$ip} >= $config{'EMAIL_ALERT_DANGER_LEVEL'}) {
                &send_mail("$config{'MAIL_STATUS_PREFIX'} removed " .
                    "tcpwrappers block against $ip (timeout expired).", '',
                    $config{'EMAIL_ADDRESSES'}, $cmds{'mail'});
            }
            $rv = 1;
        } else {
            print T "$line\n";
        }
    }
    close T;

    ### set permissions on the tmp file to be identical to the original
    chmod $orig_perms, "$config{'ETC_HOSTS_DENY_FILE'}.tmp";

    move "$config{'ETC_HOSTS_DENY_FILE'}.tmp", $config{'ETC_HOSTS_DENY_FILE'}
        or die "[*] Could not move $config{'ETC_HOSTS_DENY_FILE'}.tmp -> ",
        "$config{'ETC_HOSTS_DENY_FILE'}";

    ### delete the ip from the hash (if new packets are seen
    ### from the same ip, then the hash will be updated again
    ### in check_scan()).
    delete $auto_blocked_ips{$ip}
        if defined $auto_blocked_ips{$ip};
    return $rv;
}

sub auto_psad_response() {
    my ($curr_scan_hr, $auto_block_regex_match_hr) = @_;

    print STDERR "[+] auto_psad_response()\n" if $debug;

    SRC: for my $src (keys %$curr_scan_hr) {

        ### make sure we are not attempting to block 0.0.0.0
        ### or 127.0.0.1 or any of the local interface IP's.
        next SRC if &auto_block_ignore_ip($src);

        if ($config{'ENABLE_AUTO_IDS_REGEX'} eq 'Y'
                and $config{'AUTO_BLOCK_REGEX'} ne 'NONE') {
            ### skip if AUTO_BLOCK_REGEX did not match --log-prefix
            unless (defined $auto_block_regex_match_hr->{$src}) {
                print STDERR "[+] Skipping IP from auto-block, ",
                    "AUTO_BLOCK_REGEX $config{'AUTO_BLOCK_REGEX'} ",
                    "did not match.\n" if $debug;
                next SRC;
            }
        }

        my $dl = $scan_dl{$src};

        ### We only want to block the IP once.  Currently this will block
        ### all traffic from the host to _all_ destinations that are
        ### protected by the firewall if the IP trips the $auto_psad_level
        ### threshold for _any_ destination.
        if ($dl >= $config{'AUTO_IDS_DANGER_LEVEL'}) {

            next SRC if defined $auto_blocked_ips{$src};

            my ($timeout, $timeout_str) = &get_block_timeout($dl);

            ### we have seen at least one packet logged by the firewall
            ### at this point
            if ($config{'IPTABLES_BLOCK_METHOD'} eq 'Y') {
                &ipt_block($src, '');
            }
            if ($config{'TCPWRAPPERS_BLOCK_METHOD'} eq 'Y') {
                &sys_log('initiating tcpwrappers auto-block ' .
                    "against $src $timeout_str");
                if ($config{'ENABLE_AUTO_IDS_EMAILS'} eq 'Y'
                        and defined $scan_dl{$src}
                        and $scan_dl{$src} >= $config{'EMAIL_ALERT_DANGER_LEVEL'}) {
                    &send_mail("$config{'MAIL_STATUS_PREFIX'} " .
                        "tcpwrappers AUTO-BLOCK against $src $timeout_str",
                        '', $config{'EMAIL_ADDRESSES'}, $cmds{'mail'});
                }
                my $found = 0;
                open H, "< $config{'ETC_HOSTS_DENY_FILE'}" or die
                    "[*] Could not open $config{'ETC_HOSTS_DENY_FILE'}: $!";
                my @lines = <H>;
                close H;
                for my $line (@lines) {
                    chomp $line;
                    $found = 1 if $line =~ /ALL:\s+$src$/;
                }
                unless ($found) {
                    open H, ">> $config{'ETC_HOSTS_DENY_FILE'}" or die
                        "[*] Could not open $config{'ETC_HOSTS_DENY_FILE'}: $!";
                    print H "ALL: $src\n";
                    close H;
                    $auto_blocked_ips{$src} = time()
                        unless defined $auto_blocked_ips{$src};
                    ### write the ip out to the auto blocked file
                    &diskwrite_blocked_ip($src,
                        $config{'AUTO_BLOCK_TCPWR_FILE'}, '');
                }
            }
        }
    }
    return;
}

sub auto_block_ignore_ip() {
    my $ip = shift;
    for my $local_ip (keys %local_ips) {
        if ($ip eq $local_ip) {
            print STDERR "[+] Skipping local IP $ip from auto-block.\n"
                if $debug;
            return 1;
        }
    }
    ### matching the following two addresses is less likely (assuming
    ### iptables is not logging traffic from localhost) than matching
    ### a legitimate interface address
    if ($ip eq '127.0.0.1' or $ip eq '0.0.0.0' or $ip eq '0.0.0.0/0') {
        print STDERR "[+] Skipping IP $ip from auto-block.\n"
            if $debug;
        return 1;
    }
    return 0;
}

sub get_block_timeout() {
    my $dl = shift;

    my $timeout = $config{'AUTO_BLOCK_DL1_TIMEOUT'};
    if ($dl == 2) {
        $timeout = $config{'AUTO_BLOCK_DL2_TIMEOUT'};
    } elsif ($dl == 3) {
        $timeout = $config{'AUTO_BLOCK_DL3_TIMEOUT'};
    } elsif ($dl == 4) {
        $timeout = $config{'AUTO_BLOCK_DL4_TIMEOUT'};
    } elsif ($dl == 5) {
        $timeout = $config{'AUTO_BLOCK_DL5_TIMEOUT'};
    }

    my $timeout_str = "for $timeout seconds";
    $timeout_str = '(unlimited timeout)' if $timeout == $PERMANENT;

    print STDERR "[+] get_block_timeout() dl: $dl, $timeout, $timeout_str\n"
        if $debug;

    return ($timeout, $timeout_str);
}

sub timeout_auto_blocked_ips() {

    print STDERR "[+] timeout_auto_block_ips()\n" if $debug or $test_mode;

    for my $ip (keys %auto_blocked_ips) {
        my ($timeout, $timeout_str) = &get_block_timeout($scan_dl{$ip});
        next if $timeout == $PERMANENT;

        if ((time() - $auto_blocked_ips{$ip}) > $timeout) {

            ### remove all Netfiler blocking rules for $ip
            if ($config{'IPTABLES_BLOCK_METHOD'} eq 'Y') {
                &ipt_rm_block($ip);
            }

            ### remove all tcpwrapper blocking rules for $ip
            if ($config{'TCPWRAPPERS_BLOCK_METHOD'} eq 'Y') {
                &tcpwr_rm_block($ip);
            }
        }
    }
    return;
}

sub build_ipt_config() {

    @ipt_config = ();

    &make_psad_dirs() unless -d $config{'PSAD_DIR'};

    my $ipt = new IPTables::ChainMgr(%ipt_opts)
        or die '[*] Could not acquire IPTables::ChainMgr object.';

    my $ctr = 1;

    VAR: while (defined $config{"IPT_AUTO_CHAIN$ctr"}) {
        my $value = $config{"IPT_AUTO_CHAIN$ctr"};

        my @block = split /\s*,\s*/, $value;
        if ($#block == 4 or $#block == 6) {
            my %hsh = ();
            if ($#block == 4) {
                ### DROP, src, filter, INPUT, PSAD_BLOCK_INPUT;
                %hsh = (
                    'target'     => $block[0],
                    'direction'  => $block[1],
                    'table'      => $block[2],
                    'from_chain' => $block[3],
                    'to_chain'   => $block[4],
                    'jump_rule_position' => 1,
                    'auto_rule_position' => 1
                );
                ### this is the old format; generate a warning
                my $msg = "the IPT_AUTO_CHAIN$ctr variable in psad.conf " .
                    "needs to be updated to set the jump rule position and " .
                    "the auto rule position; defaulting both to 1.";
                    &sys_log($msg);
                    print STDERR "[-] build_ipt_config(): $msg\n"
                        if $debug;
            } else {
                ### DROP, src, filter, INPUT, 1, PSAD_BLOCK_INPUT, 1;
                %hsh = (
                    'target'     => $block[0],
                    'direction'  => $block[1],
                    'table'      => $block[2],
                    'from_chain' => $block[3],
                    'jump_rule_position' => $block[4],
                    'to_chain'   => $block[5],
                    'auto_rule_position' => $block[6]
                );
            }
            unless ($hsh{'direction'} eq 'src' or
                        $hsh{'direction'} eq 'dst' or
                        $hsh{'direction'} eq 'both') {
                my $msg = "invalid direction $hsh{'direction'} " .
                    "in IPT_AUTO_CHAIN$ctr keyword";
                &sys_log($msg);
                print STDERR "[-] build_ipt_config(): $msg\n"
                    if $debug;
                next VAR;
            }
            if ($hsh{'from_chain'} eq $hsh{'to_chain'}) {
                my $msg = "cannot have identical from_chain and to_chain " .
                    "in IPT_AUTO_CHAIN$ctr keyword";
                &sys_log($msg);
                print STDERR "[-] build_ipt_config(): $msg\n"
                    if $debug;
                next VAR;
            }
            my ($rv, $out_ar, $err_ar)
                = $ipt->chain_exists($hsh{'table'}, $hsh{'from_chain'});

            if ($rv) {
                push @ipt_config, \%hsh;
            } else {
                my $msg = "invalid IPT_AUTO_CHAIN$ctr keyword, " .
                    "$hsh{'from_chain'} chain does not exist.";
                &sys_log($msg);
                print STDERR "[-] build_ipt_config(): $msg\n"
                    if $debug;
            }
        } else {
            my $msg = "invalid IPT_AUTO_CHAIN$ctr variable: $value";
            &sys_log($msg);
            print STDERR "[-] build_ipt_config(): $msg\n" if $debug;
        }
        $ctr++;
    }
    return;
}

### this is the main caching function that adds an IP upon a
### successful block.
sub diskwrite_blocked_ip() {
    my ($src, $file, $renewed_status) = @_;
    print STDERR "[+] diskwrite_blocked_ip($src, $file, $renewed_status)\n"
        if $debug;
    my @lines = ();
    my $skip_src = 0;
    if (-e $file) {
        open F, "< $file" or die "[*] Could not open ",
            "$file: $!";
        my @tmplines = <F>;
        close F;
        ### see if we have already written the ip to the block
        ### file (or update the time if $renewed_status)
        for my $line (@tmplines) {
            chomp $line;
            if ($line =~ /^\s*($ipv4_re)\s*$/) {  ### old format; update to include time
                my $tmpsrc = $1;
                push @lines, "$tmpsrc " . $auto_blocked_ips{$tmpsrc};
                $skip_src = 1 if $tmpsrc eq $src;
            } else {
                if ($renewed_status) {  ### must update the time to now
                    if ($line =~ m|^\s*$src\s|) {
                        push @lines, "$src " . $auto_blocked_ips{$src};
                        $skip_src = 1;
                    } else {
                        push @lines, $line;
                    }
                } else {
                    if ($line =~ m|^\s*$src\s+\d+|) {
                        return;  ### already blocked $src, do nothing
                    } else {
                        push @lines, $line;
                    }
                }
            }
        }
    }
    unless ($skip_src) {
        push @lines, "$src " . $auto_blocked_ips{$src};
    }

    return unless @lines;
    open B, "> ${file}.tmp" or
        die "[*] Could not write to $file: $!";
    print B $_, "\n" for @lines;
    close B;
    move "${file}.tmp", $file or die "[*] Could not move ",
        "${file}.tmp -> $file: $!";
    return;
}

### this gets called when we want to remove an IP from the disk
### cache
sub diskwrite_rm_blocked_ip() {
    my ($src, $file) = @_;
    print STDERR "[+] rm_blocked_ip($src, $file)\n" if $debug;
    return unless -e $file;

    open B, "< $file" or
        die "[*] Could not open $file: $!";
    my @lines = <B>;
    close B;

    return unless @lines;

    open W, "> ${file}.tmp" or
        die "[*] Could not open ${file}.tmp: $!";
    for my $line (@lines) {
        chomp $line;
        if ($line =~ /^\s*($ipv4_re)/) {
            print W $line, "\n" unless $src eq $1;
        }
    }
    close W;

    move "${file}.tmp", $file or die "[*] Could not move ",
        "${file}.tmp -> $file: $!";
    return;
}

sub email_limit_reached() {
    my ($src, $dst) = @_;
    my $subject = "$config{'MAIL_STATUS_PREFIX'} reached email message " .
        "limit for $src on $config{'HOSTNAME'}";
    $subject .= " ($dst)" if $dst;
    &send_mail($subject, '', $config{'EMAIL_ADDRESSES'}, $cmds{'mail'});

    if ($dst) {
        $scan_email_ctrs{$src}{$dst}{'stop_email'} = 1;
    } else {
        $scan_email_ctrs{$src}{'stop_email'} = 1;
    }
    return;
}

sub print_scan() {  ### this should primarily be used for debugging
    my $scanfile = "$config{'PRINT_SCAN_HASH'}.$$";
    open PSCAN, "> $scanfile" or warn '[-] Could not open ',
        "$scanfile: $!" and return;
    print PSCAN "[+] Passive OS fingerprinting hash:\n",
        Dumper(\%posf),
        "[+] Scan danger level hash:\n",
        Dumper(\%scan_dl),
        "[+] Main scan hash:\n",
        Dumper(\%scan);
    close PSCAN;
    chmod 0600, $scanfile;
    return;
}

sub get_local_ips() {
    print STDERR "[+] get_local_ips()\n" if $debug;
    if ($config{'IFCFGTYPE'} =~ /iproute2/i) {
        print STDERR "[+] : Using IFCFGTYPE iproute2\n" if $debug;
        my @ips = @{&run_command($cmds{'ip'}, 'addr')};
        return unless @ips;
        for my $line (@ips) {
            if ($line =~ /inet\s+($ipv4_re)\/\d+\s/) {
                print STDERR "[+] : Adding $1 to local_ips\n" if $debug;
                $local_ips{$1} = '';
            } elsif ($line =~ /inet6\s(\S+)/) {
                print STDERR "[+] : Adding $1 to local_ips\n" if $debug;
                $local_ips{$1} = '';
            }
        }
    } else {
        print STDERR "[+] : Using IFCFGTYPE ifconfig\n" if $debug;
        my @ips = @{&run_command($cmds{'ifconfig'}, '-a')};
        return unless @ips;
        for my $line (@ips) {
            if ($line =~ /inet\s+.*?:($ipv4_re)\s/) {
                print STDERR "[+] : Adding $1 to local_ips\n" if $debug;
                $local_ips{$1} = '';
            } elsif ($line =~ /inet6\s+addr:\s+(\S+)/) {
                print STDERR "[+] : Adding $1 to local_ips\n" if $debug;
                $local_ips{$1} = '';
            }
        }
    }
    return;
}

sub get_listening_ports() {
    %local_ports = ();
    my @lines = @{&run_command($cmds{'netstat'}, '-an 2> /dev/null')};
    return unless @lines;
    for my $line (@lines) {
        next unless $line;
        chomp $line;
        if ($line =~ m/^\s*(tcp|udp)\s+\d+\s+\d+\s+\S+:(\d+)\s/) {
            ### $1 == protocol (tcp/udp), $2 == port number
            $local_ports{$1}{$2} = '';
        }
    }
    return;
}

sub get_dns_info() {
    my $ip = shift;
    my $dns_str = '';
    if (defined $dns_cache{$ip}
            and $dns_cache{$ip}{'ctr'}
            < $config{'DNS_LOOKUP_THRESHOLD'}) {
        $dns_str = $dns_cache{$ip}{'hostname'};
        $dns_cache{$ip}{'ctr'}++;
    } else {
        my $ipaddr = gethostbyname($ip);
        ### gethostbyaddr($ipaddr, AF_INET);
        if (defined $ipaddr) {
            my $dns_tmp = gethostbyaddr($ipaddr, 2);
            $dns_str = $dns_tmp if $dns_tmp;
            $dns_cache{$ip}{'ctr'}      = 0;
            $dns_cache{$ip}{'hostname'} = $dns_str;
        }
    }
    return $dns_str;
}

sub get_whois_info() {
    my ($ip, $whois_datafile) = @_;

    print STDERR "[+] get_whois_info() IP: $ip\n" if $debug;

    my @whois_data;
    if (defined $whois_cache{$ip}
            and $whois_cache{$ip} < $config{'WHOIS_LOOKUP_THRESHOLD'}
            and -e $whois_datafile) {
        $whois_cache{$ip}++;
    } else {
        $whois_cache{$ip} = 0;

        my $pid;

        local $SIG {'ALRM'} = sub {kill 15, $pid or die "[*] kill: $!";
            die "whois alarm"};
        eval {
            $pid = fork();

            die "Could not fork(): $!" unless defined $pid;

            unless ($pid) {

                ### perl invokes the shell because of the output
                ### redirect
                exec "$cmds{'whois'} $ip > $whois_datafile";

                die "whois $ip exec failed: $!";
            }
            alarm $config{'WHOIS_TIMEOUT'};

            waitpid $pid => 0;

            alarm 0;
        };

        if ($@) {
            system "$cmds{'killall'} $cmds{'whois'}";
            kill 9, $pid unless kill 15, $pid;
            print STDERR "[-] whois alarm on pid: $pid\n" if $debug;
            warn $@;
            $#whois_data = 0;
            @whois_data = ("Whois data not available!\n");
            unlink $whois_datafile;
            return \@whois_data;
        }
    }
    open W, "< $whois_datafile" or
        die "[*] Could not open $whois_datafile: $!";
    @whois_data = <W>;
    close W;

    if ($config{'ENABLE_WHOIS_FORCE_ASCII'} eq 'Y') {
        for my $line (@whois_data) {
            ### replace any non-ascii chars in whois output with 'NA'
            $line =~ s/[^\x20-\x7e]/NA/g;
        }
    }

    return \@whois_data;
}

sub stop_psad() {
    my $rv = $EXIT_SUCCESS;

    &sys_log('shutting down psad daemons');
    ### must kill psadwatchd first since if not, it might try to restart
    ### any of the other two daemons.
    for my $pidname (qw(psadwatchd kmsgsd psad psad_fw_read)) {
        my $pidfile = $pidfiles{$pidname};
        if (-e $pidfile) {
            my $pid = &is_running($pidfile);
            if ($pid) {
                print "[+] Stopping $pidname, pid: $pid\n";
                unless (kill 15, $pid) {
                    unless (kill 9, $pid) {
                        print "[*] psad: Could not kill ",
                            "$pidname, pid: $pid $!\n";
                        $rv = $EXIT_FAILURE;
                    }
                } else {
                    unlink $pidfile;
                }
            } else {
                my $print = 1;
                if ($pidname eq 'kmsgsd'
                        and ($config{'SYSLOG_DAEMON'} =~ /ulog/i
                        or $config{'ENABLE_SYSLOG_FILE'} eq 'Y')) {
                    $print = 0;
                }
                print "[-] psad: $pidname is not running on ",
                    "$config{'HOSTNAME'}\n" if $print;
            }
        } else {
            my $print = 1;
            if ($pidname eq 'kmsgsd'
                    and ($config{'SYSLOG_DAEMON'} =~ /ulog/i
                    or $config{'ENABLE_SYSLOG_FILE'} eq 'Y')) {
                $print = 0;
            }
            print "[-] psad: pid file $pidfile does not exist for ",
                "$pidname on $config{'HOSTNAME'}\n" if $print;
        }
    }
    return $rv;
}

sub restart() {
    my $cmdline = '';
    if (-e $cmdline_file) {
        open CMD, "< $cmdline_file" or die '[*] Could not open ',
            "$cmdline_file: $!";
        $cmdline = <CMD>;
        close CMD;
        chomp $cmdline;
    }

    ### stop any running psad daemons.
    &stop_psad();

    print "[+] Restarting psad daemons on $config{'HOSTNAME'}\n";
    if ($cmdline) {
        system "$cmds{'psad'} $cmdline";
    } else {
        system $cmds{'psad'};
    }
    return $EXIT_SUCCESS;
}

sub analysis_mode() {

    ($analysis_tokens_ar, $analysis_match_criteria_ar) = &analysis_fields()
        if $analysis_fields;

    &make_psad_dirs();

    unless (-d $config{'PSAD_DIR'}) {
        mkdir $config{'PSAD_DIR'} or die "[*] Could not mkdir ",
            "$config{'PSAD_DIR'}: $!";
    }

    if (-d $config{'ANALYSIS_MODE_DIR'}) {
        print "[+] Removing old $config{'ANALYSIS_MODE_DIR'} directory.\n";
        rmtree $config{'ANALYSIS_MODE_DIR'} or die "[*] Could not ",
            "remove $config{'ANALYSIS_MODE_DIR'}\n";
    }

    mkdir $config{'ANALYSIS_MODE_DIR'} or die "[*] Could not mkdir ",
        $config{'ANALYSIS_MODE_DIR'};

    ### change path for counter files, etc.
    for my $var (keys %config) {
        next if $var eq 'PSAD_DIR';
        next if $var eq 'ANALYSIS_MODE_DIR';
        next if $var eq 'ANALYSIS_OUTPUT_FILE';
        my $val = $config{$var};
        if ($val =~ m|$config{'PSAD_DIR'}/|) {
            $val =~ s|$config{'PSAD_DIR'}|$config{'ANALYSIS_MODE_DIR'}|;
            $config{$var} = $val;
        }
    }
    &make_psad_dirs();  ### one more time now that paths are updated

    ### setup to put all <ip> files in the ANALYSIS_MODE_DIR
    ### (by setting PSAD_DIR to ANALYSIS_MODE_DIR subroutines
    ### work more easily).
    $config{'PSAD_DIR'} = $config{'ANALYSIS_MODE_DIR'};

    ### build @local_nets array
    my $connected_subnets_ar = &get_connected_subnets();
    for my $net (@$connected_subnets_ar) {
        push @local_nets, $net;
    }

    my $fh = '';
    if ($pkts_from_stdin) {
        print "[+] Entering analysis mode.  Parsing STDIN\n";
        $fh = *STDIN;
    } else {
        print "[+] Entering analysis mode.  Parsing $fw_data_file\n";
        open MSGS, "< $fw_data_file" or die "[*] Could not open ",
            "$fw_data_file: $!";
        $fh = *MSGS;
    }
    my @ipt_msgs = ();
    my $pkt_ctr = 0;
    my $line_ctr = 0;
    PKT: while (<$fh>) {
        my $line = $_;
        $line_ctr++;
        if ($num_packets > 0) {
            last PKT if $pkt_ctr >= $num_packets;
        }
        if ($line =~ /IN.*OUT/) {
            if ($config{'FW_SEARCH_ALL'} eq 'Y') {
                push @ipt_msgs, $line;
                $pkt_ctr++;
            } else {
                if ($line =~ /$config{'SNORT_SID_STR'}/) {
                    push @ipt_msgs, $line;
                    $pkt_ctr++;
                } else {
                    for my $fw_search_str (@fw_search) {
                        if ($line =~ /$fw_search_str/) {
                            push @ipt_msgs, $line;
                            $pkt_ctr++;
                        }
                    }
                }
            }
        }
    }
    close $fh unless $pkts_from_stdin;

    print "[+] Found ", ($#ipt_msgs+1), " iptables log messages out of " .
        "$line_ctr total lines.\n";
    print "    This may take a while...\n" if $#ipt_msgs > 15000;

    ### analyze all packets
    &check_scan(\@ipt_msgs);

    print "\n[+] Finished --Analyze cycle.\n";

    return $EXIT_SUCCESS;
}

sub ipt_match_criteria() {
    my ($pkt_hr, $tokens_ar, $match_criteria_ar) = @_;

    my @matched_fields = ();
    my $gnuplot_comment_str = '';
    for (my $i=0; $i <= $#$tokens_ar; $i++) {
        my $tok = $tokens_ar->[$i];
        if ($match_criteria_ar) {
            my $match_hr = $match_criteria_ar->[$i];
            if (defined $match_hr->{'num'}) {
                return [], '' unless $pkt_hr->{$tok} =~ m|^\d+$|;
                if ($match_hr->{'negate'}) {
                    return [], '' if $pkt_hr->{$tok} == $match_hr->{'num'};
                } else {
                    return [], '' unless $pkt_hr->{$tok} == $match_hr->{'num'};
                }
            } elsif (defined $match_hr->{'gt'}) {
                return [], '' unless $pkt_hr->{$tok} =~ m|^\d+$|;
                if ($match_hr->{'negate'}) {
                    return [], '' unless $pkt_hr->{$tok} <= $match_hr->{'gt'};
                } else {
                    return [], '' unless $pkt_hr->{$tok} > $match_hr->{'gt'};
                }
            } elsif (defined $match_hr->{'lt'}) {
                return [], '' unless $pkt_hr->{$tok} =~ m|^\d+$|;
                if ($match_hr->{'negate'}) {
                    return [], '' unless $pkt_hr->{$tok} >= $match_hr->{'lt'};
                } else {
                    return [], '' unless $pkt_hr->{$tok} < $match_hr->{'lt'};
                }
            } elsif (defined $match_hr->{'str'}) {
                if ($match_hr->{'negate'}) {
                    return [], '' if $pkt_hr->{$tok} eq $match_hr->{'str'};
                } else {
                    return [], '' unless $pkt_hr->{$tok} eq $match_hr->{'str'};
                }
            } elsif (defined $match_hr->{'re'}) {
                if ($match_hr->{'negate'}) {
                    return [], '' if $pkt_hr->{$tok} =~ m|$match_hr->{'re'}|;
                } else {
                    return [], '' unless $pkt_hr->{$tok} =~ m|$match_hr->{'re'}|;
                }
            } elsif (defined $match_hr->{'net'} or defined $match_hr->{'ip'}) {
                my $net_or_ip_key = 'ip_obj';
                $net_or_ip_key = 'net_obj' if defined $match_hr->{'net'};
                if ($pkt_hr->{$tok} =~ m|$ipv4_re|
                        or $pkt_hr->{$tok} =~ m|$ipv6_re|) {
                    my $ip_match_obj = '';
                    if ($tok eq 'src') {
                        $ip_match_obj = $pkt_hr->{'s_obj'};
                    } elsif ($tok eq 'dst') {
                        $ip_match_obj = $pkt_hr->{'d_obj'};
                    }
                    if ($match_hr->{'negate'}) {
                        return [], '' if $ip_match_obj->within($match_hr->{$net_or_ip_key});
                    } else {
                        return [], '' unless $ip_match_obj->within($match_hr->{$net_or_ip_key});
                    }
                } else {
                    return [], '';
                }
            }
        }
        push @matched_fields, $pkt_hr->{$tok};
    }
    return \@matched_fields, $gnuplot_comment_str;
}

sub csv_mode() {

    if ($gnuplot_file_prefix) {
        $gnuplot_data_file = "$gnuplot_file_prefix.dat";
        $gnuplot_plot_file = "$gnuplot_file_prefix.gnu";
        $gnuplot_png_file  = "$gnuplot_file_prefix.png";
    }

    print "[+] Entering Gnuplot mode...\n" if $gnuplot_mode;

    ### see what we should be parsing out of the iptables logs
    my ($tokens_ar, $match_criteria_ar) = &csv_tokens();

    $csv_regex = qr/$csv_regex/ if $csv_regex;
    $csv_neg_regex = qr/$csv_neg_regex/ if $csv_neg_regex;

    my %csv_uniq_lines = ();

    if ($csv_start_line) {
        die "[*] Cannot have start line > end line."
            if $csv_start_line > $csv_end_line;
    }

    my $fh = '';
    if ($csv_stdin) {
        print "[+] Parsing iptables log messages from STDIN\n"
            if $gnuplot_mode;
        $fh = *STDIN;
    } else {
        print "[+] Parsing iptables log messages from file: $fw_data_file\n"
            if $gnuplot_mode;
        open MSGS, "< $fw_data_file" or die "[*] Could not open ",
            "$fw_data_file: $!";
        $fh = *MSGS;
    }
    my $ctr = 0;
    my $line_ctr = 0;

    if ($gnuplot_mode and $store_file and -e $store_file) {

        @gnuplot_data = @{retrieve($store_file)};

    } else {

        MSG: while (<$fh>) {
            my $pkt_str = $_;
            $line_ctr++;
            if ($csv_start_line) {
                next MSG unless $line_ctr >= $csv_start_line;
            }
            if ($csv_end_line) {
                last MSG if $line_ctr == $csv_end_line;
            }
            next MSG unless $pkt_str =~ /IN.*OUT/;
            my %pkt = %pkt_NF_init;
            if ($config{'FW_SEARCH_ALL'} eq 'Y') {
                my $rv = &parse_NF_pkt_str(\%pkt, $pkt_str);
                next MSG if $rv == $PKT_ERROR or $rv == $PKT_IGNORE;
            } else {
                if ($pkt_str =~ /$config{'SNORT_SID_STR'}/) {
                    my $rv = &parse_NF_pkt_str(\%pkt, $pkt_str);
                    next MSG if $rv == $PKT_ERROR or $rv == $PKT_IGNORE;
                } else {
                    for my $fw_search_str (@fw_search) {
                        if ($pkt_str =~ /$fw_search_str/) {
                            my $rv = &parse_NF_pkt_str(\%pkt, $pkt_str);
                            next MSG if $rv == $PKT_ERROR or $rv == $PKT_IGNORE;
                        }
                    }
                }
            }
            if ($csv_regex) {
                next MSG unless $pkt{'raw'} =~ m|$csv_regex|;
            }
            if ($csv_neg_regex) {
                next MSG unless $pkt{'raw'} !~ m|$csv_neg_regex|;
            }
            $pkt{'log_prefix'} =~ s/\W//g;
            $pkt{'log_prefix'} =~ s/\s//g;

            my ($matched_fields_ar, $gnuplot_comment_str)
                    = &ipt_match_criteria(\%pkt, $tokens_ar, $match_criteria_ar);

            next MSG unless $#$matched_fields_ar > -1;

            $ctr++;
            if ($csv_line_limit > 0) {
                last if $ctr >= $csv_line_limit;
            }

            if ($gnuplot_mode) {
                ### cache the data since IP addresses have to mapped to
                ### integers across the entire data set
                push @gnuplot_data, $matched_fields_ar;
            } else {

                my $str = '';

                ### here is where the output string is assembled
                $str .= $_ . $plot_separator for @$matched_fields_ar;
                $str =~ s/$plot_separator$//;

                if ($csv_print_uniq) {
                    $csv_uniq_lines{$str} = '';
                } else {
                    ### The CSV data is printed here unless
                    ### we are running with --CSV-unique-lines
                    ### or we are running in --gnuplot mode
                    print $str, "\n";
                }
            }
        }
        close $fh unless $csv_stdin;
    }

    if ($gnuplot_mode) {

        if ($store_file) {
            if (-e $store_file) {
                print "[+] Retrieved Gnuplot array from: $store_file\n";
            } else {
                ### store the @gnuplot_data array to disk for fast
                ### retrieval next time
                print "[+] Storing Gnuplot data to: $store_file\n";
                store(\@gnuplot_data, $store_file);
                chmod 0644, $store_file;
            }
        } else {
            print "[+] Parsed $line_ctr iptables log messages.\n";
        }
        ### print out the gnuplot data after appropriate
        ### integer conversions
        &gnuplot_write_data($tokens_ar);

        ### write out the gnuplot file
        &gnuplot_write_plot_file($tokens_ar);

    } elsif ($csv_print_uniq) {
        print "$_\n" for keys %csv_uniq_lines;
    }
    return $EXIT_SUCCESS;
}

sub gnuplot_header() {
    my @lines = ();
    push @lines, '#', '#'x$num_hash_marks, '#',
        "# Generated by psad v$version",
        "# Command line: 'psad @args_cp'",
        "# Time stamp:  " . localtime(),
        '#', '#'x$num_hash_marks, '#', '';
    return \@lines;
}

sub gnuplot_write_plot_file() {
    my $tokens_ar = shift;

    unless ($gnuplot_legend_title) {
        $gnuplot_legend_title = '(';
        for my $tok (@$tokens_ar) {
            $gnuplot_legend_title .= "$tok,";
        }
        $gnuplot_legend_title =~ s/,$//;
        $gnuplot_legend_title .= ')';
        $gnuplot_legend_title  =~ s/_//g;
    }

    my @tokens = split /\s+/, $csv_fields;
    if ($gnuplot_template_file) {
        print "[+] Using Gnuplot template file: $gnuplot_template_file\n";
        copy $gnuplot_template_file, $gnuplot_plot_file or die "[*] ",
            "Could not copy $gnuplot_template_file -> $gnuplot_plot_file: $!";
    } else {
        print "[+] Writing gnuplot directive file: $gnuplot_plot_file\n";
        open GP, "> $gnuplot_plot_file" or die "[*] Could not open ",
            "$gnuplot_plot_file: $!";
        print GP "$_\n", for @{&gnuplot_header()};
        unless ($gnuplot_title) {
            $gnuplot_title = "psad iptables log visualization: $csv_fields";
            $gnuplot_title =~ s/_//g;  ### some fonts used by Gnuplot don't like "-" chars
        }
        print GP "reset\n", qq|set title "$gnuplot_title"\n|;
        unless ($gnuplot_interactive) {
            if ($gnuplot_grayscale) {
                print GP "set terminal png transparent xffffff x000000 x606060 ",
                    "x606060 x606060 x606060 x606060 x606060 nocrop enhanced\n",
                    qq|set output "$gnuplot_png_file"\n|;
            } else {
                print GP "set terminal png transparent nocrop enhanced\n",
                    qq|set output "$gnuplot_png_file"\n|;
            }
        }
        if ($tokens_ar->[0] eq 'timestamp') {
            print GP "set xdata time\n",
                qq|set timefmt x "%s"\n|,
                qq|set format x "%m/%d"\n|,
                qq|set xlabel "time"\n|;
            if ($gnuplot_x_range) {
                die "[*] range argument must be formatted as <start>:<end>"
                    unless $gnuplot_x_range =~ /^\S+:\S+$/;
                $gnuplot_x_range =~ s/:/":"/;
                print GP qq|set xrange ["$gnuplot_x_range"]\n|;
            }
        } else {
            if ($gnuplot_x_label) {
                print GP qq|set xlabel "$gnuplot_x_label"\n|;
            } else {
                $tokens[0] =~ s/_//g;
                print GP qq|set xlabel "$tokens[0]"\n|;
            }
            if ($gnuplot_x_range) {
                die "[*] range argument must be formatted as <start>:<end>, ",
                    "e.g. 1:10" unless $gnuplot_x_range =~ /^\d+:\d+$/;
                print GP qq|set xrange [$gnuplot_x_range]\n|;
            }
        }
        if ($gnuplot_y_label) {
            print GP qq|set ylabel "$gnuplot_y_label"\n|;
        } else {
            $tokens[1] =~ s/_//g;
            print GP qq|set ylabel "$tokens[1]"\n|;
        }
        if ($gnuplot_y_range) {
                die "[*] range argument must be formatted as <start>:<end>, e.g. 1:10"
                unless $gnuplot_y_range =~ /^\d+:\d+$/;
            print GP qq|set yrange [$gnuplot_y_range]\n|;
        }
        if ($#$tokens_ar == 2 or $gnuplot_3d) {
            ### include zaxis
            if ($gnuplot_z_label) {
                print GP qq|set zlabel "$gnuplot_z_label"\n|;
            } else {
                if ($gnuplot_count_type) {
                    print GP qq|set zlabel "$gnuplot_count_type"\n|;
                } else {
                    $tokens[2] =~ s/_//g;
                    print GP qq|set zlabel "$tokens[2]"\n|;
                }
            }
            if ($gnuplot_z_range) {
                die "[*] range argument must be formatted as <start>:<end>, ",
                    "e.g. 1:10" unless $gnuplot_z_range =~ /^\d+:\d+$/;
                print GP qq|set zrange [$gnuplot_z_range]\n|;
            }
            unless ($gnuplot_graph_style) {
                $gnuplot_graph_style = 'points';
            }
            if ($gnuplot_view) {
                die "[*] View must be a coordinate pair such as 60,30"
                    unless $gnuplot_view =~ /^\d+,\d+/;
                print GP qq|set view $gnuplot_view\n|;
            }
            print GP qq|splot '$gnuplot_data_file' using 1:2:3 with | .
                "$gnuplot_graph_style title '$gnuplot_legend_title'\n";
        } else {
            unless ($gnuplot_graph_style) {
                $gnuplot_graph_style = 'linespoints';
            }
            print GP qq|plot '$gnuplot_data_file' using 1:2 with | .
                "$gnuplot_graph_style title '$gnuplot_legend_title'\n";
        }
        close GP;
    }
    chmod 0644, $gnuplot_plot_file;
    return;
}

sub gnuplot_write_data() {
    my $tokens_ar = shift;

    ### see how many years we need to go back
    &gnuplot_set_start_year($tokens_ar);

    ### resolve any IP addresses to minimal integers
    ### (this mapping preserves IP address subnet relationships
    ### in that IP's on the same subnet will appear close together
    ### in the plot).
    for my $aref (@gnuplot_data) {
        for (my $i=0; $i <= $#$tokens_ar; $i++) {
            my $tok = $tokens_ar->[$i];
            if ($tok eq 'src' or $tok eq 'dst') {

                ### add the IP into the ip2int cache
                &ip2int($aref->[$i]);
            }
        }
    }

    ### now that all IP addresses have been mapped into the cache
    ### we map each IP to a mimimal integer
    my $ip_ctr = 1;
    for my $ip (sort {$ip2int_cache{$a} <=> $ip2int_cache{$b}}
                keys %ip2int_cache) {
        unless (defined $gnuplot_ip2int{$ip}) {
            $gnuplot_ip2int{$ip} = $ip_ctr;
            $ip_ctr++;
        }
    }

    ### check to see if we are generating data counts instead of
    ### values (for example number of packets to ports instead
    ### of just the port values themselves)
    my @gnuplot_count_data = ();

    print "[+] Writing parsed iptables data to: $gnuplot_data_file\n";
    open GP, "> $gnuplot_data_file" or die "[*] Could not open ",
        "$gnuplot_data_file: $!";
    print GP "$_\n", for @{&gnuplot_header()};

    ### write gnuplot data out to stdout
    for my $aref (@gnuplot_data) {

        my @matched_fields = ();
        my $gnuplot_comment_str = '';
        for (my $i=0; $i <= $#$tokens_ar; $i++) {
            my $tok = $tokens_ar->[$i];
            my $val = $aref->[$i];

            my ($rv, $gnuplot_comment) = &gnuplot_value($tok, $val);
            push @matched_fields, $rv;
            $gnuplot_comment_str .= "$rv=$gnuplot_comment "
                if $gnuplot_comment;
        }
        next unless @matched_fields;

        if ($gnuplot_count_type) {
            push @matched_fields, $gnuplot_comment_str;
            push @gnuplot_count_data, \@matched_fields;
        } else {
            my $str = '';
            $str .= $_ . $plot_separator for @matched_fields;
            $str =~ s/$plot_separator$//;
            $str .= "  ### $gnuplot_comment_str" if $gnuplot_comment_str;
            print GP $str, "\n";
        }
    }

    if ($gnuplot_count_type) {
        &render_count_data(\@gnuplot_count_data, $tokens_ar, *GP);
    }
    close GP;
    chmod 0644, $gnuplot_data_file;
    return;
}

sub render_count_data() {
    my ($count_data_ar, $tokens_ar, $render_fh) = @_;

    my %render_count_data = ();
    my %render_comments   = ();
    my $start_time = '';
    my $end_time = '';
    my $time_interval_incr = 0;

    if ($gnuplot_count_type eq 'countday'
            or $gnuplot_count_type eq 'counthour'
            or $gnuplot_count_type eq 'countmin'
            or $gnuplot_count_type eq 'countdayuniq'
            or $gnuplot_count_type eq 'counthouruniq'
            or $gnuplot_count_type eq 'countminuniq') {

        $start_time = $count_data_ar->[0]->[0];
        $end_time   = $count_data_ar->[$#$count_data_ar]->[0];

        if ($gnuplot_count_type eq 'countday') {
            $time_interval_incr = 60 * 60 * 24;
            $gnuplot_count_type = 'count';
        } elsif ($gnuplot_count_type eq 'counthour') {
            $time_interval_incr = 60 * 60;
            $gnuplot_count_type = 'count';
        } elsif ($gnuplot_count_type eq 'countmin') {
            $time_interval_incr = 60;
            $gnuplot_count_type = 'count';
        } elsif ($gnuplot_count_type eq 'countdayuniq') {
            $time_interval_incr = 60 * 60 * 24;
            $gnuplot_count_type = 'countuniq';
        } elsif ($gnuplot_count_type eq 'counthouruniq') {
            $time_interval_incr = 60 * 60;
            $gnuplot_count_type = 'countuniq';
        } elsif ($gnuplot_count_type eq 'countminuniq') {
            $time_interval_incr = 60;
            $gnuplot_count_type = 'countuniq';
        }

    }

    for my $aref (@$count_data_ar) {
        my $x_axis = $aref->[0];
        my $y_axis = $aref->[1];
        my $z_axis = '';

        if ($time_interval_incr) {
            ($x_axis, $start_time) = &gnuplot_time_to_interval($x_axis, $start_time,
                $end_time, $time_interval_incr);
        }

        if ($gnuplot_count_type eq 'count') {

            if ($gnuplot_count_element == 1) {

                if ($gnuplot_3d) {
                    if ($#$aref == 2) {
                        $render_comments{$x_axis}{$y_axis} = $aref->[2];
                    }
                    $render_count_data{$x_axis}{$y_axis}++;
                } else {
                    if ($#$aref == 2) {
                        $render_comments{$x_axis} = $aref->[2];
                    }
                    $render_count_data{$x_axis}++;
                }

            } elsif ($gnuplot_count_element == 2) {

                $z_axis = $aref->[2];
                if ($#$aref == 3) {
                    $render_comments{$x_axis}{$y_axis} = $aref->[3];
                }
### FIXME
                $render_count_data{$x_axis}{$y_axis}{$z_axis}++;

            }

        } elsif ($gnuplot_count_type eq 'countuniq') {

            if ($gnuplot_count_element == 1) {

                if ($gnuplot_3d) {
                    if ($#$aref == 2) {
                        $render_comments{$x_axis}{$y_axis} = $aref->[2];
                    }
                    unless (defined $gnuplot_cache_uniq{$x_axis}) {
                        $render_count_data{$x_axis}{$y_axis}++;
                        $gnuplot_cache_uniq{$x_axis}{$y_axis} = '';
                    }
                    unless (defined $gnuplot_cache_uniq{$x_axis}{$y_axis}) {
                        $render_count_data{$x_axis}{$y_axis}++;
                        $gnuplot_cache_uniq{$x_axis}{$y_axis} = '';
                    }

                } else {
                    if ($#$aref == 2) {
                        $render_comments{$x_axis} = $aref->[2];
                    }

                    unless (defined $gnuplot_cache_uniq{$x_axis}) {
                        $render_count_data{$x_axis}++;
                        $gnuplot_cache_uniq{$x_axis}{$y_axis} = '';
                    }
                    unless (defined $gnuplot_cache_uniq{$x_axis}{$y_axis}) {
                        $render_count_data{$x_axis}++;
                        $gnuplot_cache_uniq{$x_axis}{$y_axis} = '';
                    }
                }

            } elsif ($gnuplot_count_element == 2) {

                $z_axis = $aref->[2];
                if ($#$aref == 3) {
                    $render_comments{$x_axis}{$y_axis} = $aref->[3];
                }

                unless (defined $gnuplot_cache_uniq{$x_axis}) {
                    $render_count_data{$x_axis}{$y_axis}++;
                    $gnuplot_cache_uniq{$x_axis}{$y_axis}{$z_axis} = '';
                }
                unless (defined $gnuplot_cache_uniq{$x_axis}{$y_axis}) {
                    $render_count_data{$x_axis}{$y_axis}++;
                    $gnuplot_cache_uniq{$x_axis}{$y_axis}{$z_axis} = '';
                }
                unless (defined $gnuplot_cache_uniq{$x_axis}{$y_axis}{$z_axis}) {
                    $render_count_data{$x_axis}{$y_axis}++;
                    $gnuplot_cache_uniq{$x_axis}{$y_axis}{$z_axis} = '';
                }

            }
        }
    }

    ### print the data to the .dat file now that it has been counted
    if ($gnuplot_count_type eq 'count') {

        if ($gnuplot_count_element == 1) {
            if ($gnuplot_3d) {

                &gnuplot_print_count_3d_data(\%render_count_data,
                    \%render_comments, $render_fh);

            } else {

                &gnuplot_print_count_2d_data(\%render_count_data,
                    \%render_comments, $render_fh);

            }
        } elsif ($gnuplot_count_element == 2) {
            for my $x_axis (sort {$a <=> $b} keys %render_count_data) {
                for my $y_axis (sort keys %{$render_count_data{$x_axis}}) {
                    for my $key (keys %{$render_count_data{$x_axis}{$y_axis}}) {
                        my $z_axis = $render_count_data{$x_axis}{$y_axis}{$key};
                        if (defined $render_comments{$x_axis}
                                and defined $render_comments{$x_axis}{$y_axis}) {
                            print $render_fh $x_axis . $plot_separator . $y_axis .
                                $plot_separator. $z_axis .
                                "  ### $render_comments{$x_axis}{$y_axis}", "\n";
                        } else {
                            print $render_fh $x_axis . $plot_separator .
                                $y_axis . $plot_separator . $z_axis, "\n";
                        }
                    }
                }
            }
        }

    } elsif ($gnuplot_count_type eq 'countuniq') {

        if ($gnuplot_count_element == 1) {

            if ($gnuplot_3d) {

                &gnuplot_print_count_3d_data(\%render_count_data,
                    \%render_comments, $render_fh);

            } else {

                &gnuplot_print_count_2d_data(\%render_count_data,
                    \%render_comments, $render_fh);

            }
        } elsif ($gnuplot_count_element == 2) {

            &gnuplot_print_count_3d_data(\%render_count_data,
                \%render_comments, $render_fh);

        }

    }

    return;
}

sub gnuplot_time_to_interval() {
    my ($time, $start_time, $end_time, $time_interval_incr) = @_;
    my $mapped_time = $time;

    if ($time <= $start_time) {
        $mapped_time = $start_time;
    } elsif ($time >= $end_time) {
        $mapped_time = $end_time;
    } else {
        while ($start_time < $time - $time_interval_incr) {
            $start_time += $time_interval_incr;
        }
        $mapped_time = $start_time;
    }

    return ($mapped_time, $start_time);
}

sub gnuplot_print_count_3d_data() {
    my ($data_hr, $comments_hr, $render_fh) = @_;

    for my $x_axis (sort {$a <=> $b}
                keys %$data_hr) {
        for my $y_axis (sort keys %{$data_hr->{$x_axis}}) {
            my $z_axis = $data_hr->{$x_axis}->{$y_axis};
            if (defined $comments_hr->{$x_axis} and
                    defined $comments_hr->{$x_axis}{$y_axis}) {
                print $render_fh $x_axis . $plot_separator .
                    $y_axis . $plot_separator . $z_axis .
                    "  ### $comments_hr->{$x_axis}->{$y_axis}",
                    "\n";
            } else {
                print $render_fh $x_axis . $plot_separator .
                    $y_axis . $plot_separator . $z_axis, "\n";
            }
        }
    }
    return;
}

sub gnuplot_print_count_2d_data() {
    my ($data_hr, $comments_hr, $render_fh) = @_;

    if ($gnuplot_sort_style eq 'time') {
        for my $x_axis (sort {$a <=> $b}
                    keys %$data_hr) {
            my $y_axis = $data_hr->{$x_axis};
            if (defined $comments_hr->{$x_axis}) {
                print $render_fh $x_axis . $plot_separator . $y_axis .
                    "  ### $comments_hr->{$x_axis}", "\n";
            } else {
                print $render_fh $x_axis . $plot_separator .
                    $y_axis, "\n";
            }
        }
    } elsif ($gnuplot_sort_style eq 'value') {
        for my $x_axis (sort {$data_hr->{$b} <=> $data_hr->{$a}}
                    keys %$data_hr) {
            my $y_axis = $data_hr->{$x_axis};
            if (defined $comments_hr->{$x_axis}) {
                print $render_fh $x_axis . $plot_separator . $y_axis .
                    "  ### $comments_hr->{$x_axis}", "\n";
            } else {
                print $render_fh $x_axis . $plot_separator .
                    $y_axis, "\n";
            }
        }
    } else {
        for my $x_axis (keys %$data_hr) {
            my $y_axis = $data_hr->{$x_axis};
            if (defined $comments_hr->{$x_axis}) {
                print $render_fh $x_axis . $plot_separator . $y_axis .
                    "  ### $comments_hr->{$x_axis}", "\n";
            } else {
                print $render_fh $x_axis . $plot_separator .
                    $y_axis, "\n";
            }
        }
    }
    return;
}

sub gnuplot_set_start_year() {
    my $tokens_ar = shift;

    return unless $csv_have_timestamp;

    my $timestamp_field = -1;
    for (my $i=0; $i <= $#$tokens_ar; $i++) {
        if ($tokens_ar->[$i] eq 'timestamp') {
            $timestamp_field = $i;
            last;
        }
    }
    ### calculate the starting year by looking at the most recent
    ### event and working backwards
    die unless $timestamp_field > -1;

    my ($today_year, $today_mon, $today_day) = Today();
    $gnuplot_year = $today_year;
    my $prev_month = 0;
    for (my $i=$#gnuplot_data; $i >= 0; $i--) {
        if ($gnuplot_data[$i]->[$timestamp_field] =~ /^\s*(\w+)\s+(\d+)\s+(\S+)/) {
            my $m_tmp = $1;  ### kludge for Decode_Month() call
            my $mon  = Decode_Month($m_tmp);
            my $day  = $2;
            my $time = $3;
            if ($gnuplot_year) {
                if ($prev_month < $mon) {
                    ### i.e., switched from Jan to Dec as we go backwards
                    ### in the log (but there could be a hole like Jan to
                    ### Oct as well, the key is that the new mon is less
                    ### than the previous one)
                    $gnuplot_year--;
                }
                $prev_month = $mon;
            } else {
                $prev_month = $mon;
                if ($today_mon < $mon) {
                    $gnuplot_year = $today_year - 1;
                } else {
                    $gnuplot_year = $today_year;
                }
            }
        }
    }
    return;
}

sub gnuplot_value() {
    my ($tok, $packet_val) = @_;

    my $rv = $packet_val;
    my $gnuplot_comment_str = '';

    if ($tok eq 'timestamp') {

        ### reformat timestamp (e.g. "Feb  1 00:00:27"
        ### becomes 02/02/04:03:00:17)
        if ($packet_val =~ /^\s*(\w+)\s+(\d+)\s+(\S+)/) {
            my $m_tmp = $1;  ### kludge for Decode_Month() call
            my $mon  = Decode_Month($m_tmp);
            my $day  = $2;
            my $time = $3;
            my $hour = 0;
            my $min  = 0;
            my $sec  = 0;
            if ($time =~ /(\d{2}):(\d{2}):(\d{2})/) {
                $hour = $1;
                $min  = $2;
                $sec  = $3;
            }
            $gnuplot_prev_mon = $mon unless $gnuplot_prev_mon;
            if ($mon < $gnuplot_prev_mon) {
                $gnuplot_year++;
            }
            $gnuplot_prev_mon = $mon;
            $rv = Mktime($gnuplot_year, $mon, $day, $hour, $min, $sec);
            $gnuplot_comment_str = $packet_val;
        }

    } else {

        ### see if this field is non-digit data, and map
        ### to a digit if so
        if (defined $gnuplot_non_digit_packet_fields{$tok}) {

            ### it is a non-digit value from the packet, so append a comment
            ### to the gnuplot data file that contains that original value.
            ### This allows the user to map integers back to their original
            ### value (say IP integers back to the dotted-quad notation).
            $gnuplot_comment_str = $packet_val;

            if ($gnuplot_non_digit_packet_fields{$tok} eq 'ip2int') {

                ### get the minimal IP integer from the %gnuplot_ip2int cache
                $rv = $gnuplot_ip2int{$packet_val};

            } elsif ($gnuplot_non_digit_packet_fields{$tok} eq 'proto2int') {

                ### convert protocol to integer
                $rv = &proto2int($packet_val);

            } elsif ($gnuplot_non_digit_packet_fields{$tok} eq 'intf2int') {

                ### convert interface to integer
                $rv = &intf2int($packet_val);

            } elsif ($gnuplot_non_digit_packet_fields{$tok} eq 'hashentry') {

                ### map the value to a digit
                unless (defined $gnuplot_non_digit_map{$tok}
                        and defined $gnuplot_non_digit_map{$tok}{'data'}
                        and defined $gnuplot_non_digit_map{$tok}
                        {'data'}{$packet_val}) {

                    unless (defined $gnuplot_non_digit_map{$tok}{'ctr'}) {
                       $gnuplot_non_digit_map{$tok}{'ctr'} = 0;
                    }

                    $gnuplot_non_digit_map{$tok}{'ctr'}++;

                    $gnuplot_non_digit_map{$tok}{'data'}{$packet_val}
                        = $gnuplot_non_digit_map{$tok}{'ctr'};
                }
                $rv = $gnuplot_non_digit_map{$tok}{'data'}{$packet_val};
            }
        }
    }
    return $rv, $gnuplot_comment_str;
}

sub ip2int() {
    my $ip = shift;
    my $ip_int = 0;
    unless (defined $ip2int_cache{$ip}) {
        # my @octets = split /\./, $ip;
        for (split /\./, $ip) {
            $ip_int = $ip_int*256 + $_;
        }
        $ip2int_cache{$ip} = $ip_int;
    }
    return;
}

sub proto2int() {
    my $proto = shift;
    my $rv = -1;
    if ($proto =~ /^\d+$/) {
        $rv = $proto;
    } elsif ($proto =~ /tcp/i) {
        $rv = 6;
    } elsif ($proto =~ /udp/i) {
        $rv = 17;
    } elsif ($proto =~ /icmp/i) {
        $rv = 1;
    }
    return $rv;
}

sub intf2int() {
    my $intf = shift;
    my $rv = -1;
    if ($intf =~ /(\d+)$/) {
        $rv = $1;
    }
    return $rv;
}

sub analysis_fields() {
    $csv_fields = $analysis_fields;
    return &csv_tokens();
}

sub csv_tokens() {

    my @tokens = ();
    my @match_criteria = ();

    if ($csv_fields) {
        my @tok_tmp = split /\s+/, $csv_fields;
        for my $tok_str (@tok_tmp) {
            my $token  = $tok_str;
            my $search = '';
            my $negate = 0;
            if ($tok_str =~ m|(\w+):(\S+)|) {
                $token  = $1;
                $search = $2;
            } else {
                if ($analysis_fields) {
                    die "[*] $tok_str requires a search criteria in -A mode.";
                }
            }
            $search =~ s/\,$//;
            if ($token eq 'timestamp') {
                $csv_have_timestamp = 1;
            }
            if ($search =~ /^not/) {
                $negate = 1;
                $search =~ s/^not//;
            }

            for my $count_type (qw/countabs countuniq
                    countday counthouruniq countminuniq countdayuniq
                    counthour countmin countday count/) {

                if ($search =~ /,$count_type$/ or $search =~ /^$count_type/) {

                    $search =~ s/,$count_type$//;
                    $search =~ s/^$count_type//;

                    die "[*] Counts against multiple fields are not supported."
                        if $gnuplot_count_type;

                    $gnuplot_count_type = $count_type;
                    $gnuplot_count_element = $#tokens + 1;

                    if ($count_type eq 'countday'
                            or $count_type eq 'counthour'
                            or $count_type eq 'countmin'
                            or $count_type eq 'countdayuniq'
                            or $count_type eq 'counthouruniq'
                            or $count_type eq 'countminuniq') {

                        $gnuplot_sort_style = 'time';
                        die "[*] The first search field must be 'timestamp' ",
                            "for time-based counts" if $#tokens == -1;
                        die "[*] The first search field must be 'timestamp' ",
                            "for time-based counts"
                            unless $tokens[0] eq 'timestamp';
                    }
                }
            }

            $token = 'src'   if $token eq 'SRC';
            $token = 'dst'   if $token eq 'DST';
            $token = 'sp'    if $token eq 'SPT';
            $token = 'dp'    if $token eq 'DPT';
            $token = 'proto' if $token eq 'PROTO';
            $token = 'tos'   if $token eq 'TOS';
            $token = 'win'   if $token eq 'WIN';
            $token = 'itype' if $token eq 'TYPE';
            $token = 'icode' if $token eq 'CODE';
            $token = 'ttl'   if $token eq 'TTL';
            $token = 'ip_id' if $token eq 'ID';
            $token = 'icmp_seq' if $token eq 'SEQ';
            $token = 'ip_len'   if $token eq 'LEN';
            $token = 'intf'     if $token eq 'IN' or $token eq 'OUT';
            unless (defined $pkt_NF_init{$token}) {
                print "[*] $token is not a valid packet field; valid ",
                    "fields are:\n";
                for my $key (sort keys %pkt_NF_init) {
                    print "    $key\n";
                }
                die;
            }
            push @tokens, $token;

            if ($search) {
                my %search_hsh = ('negate' => $negate);
                if ($search =~ m|^\d+$|) {
                    $search_hsh{'num'} = $search;
                } elsif ($search =~ m|^>(\d+)$|) {
                    $search_hsh{'gt'} = $1;
                    die "[*] $token value must be >= 0"
                        unless $1 >= 0;
                } elsif ($search =~ m|^<(\d+)$|) {
                    $search_hsh{'lt'} = $1;
                    die "[*] $token value must be >= 0"
                        unless $1 >= 0;
                } elsif ($search =~ m|^/(.*?)/$|) {
                    $search_hsh{'re'} = qr|$1|;
                } elsif ($search =~ m|^'(.*?)'$|) {
                    $search_hsh{'str'} = $1;
                } elsif ($search =~ m|^$ipv4_re/$ipv4_re$|) {
                    $search_hsh{'net'} = $search;
                    $search_hsh{'net_obj'} = new NetAddr::IP($search)
                        or die "[*] NetAddr::IP($search) error";
                } elsif ($search =~ m|^$ipv4_re/\d+$|) {
                    $search_hsh{'net'} = $search;
                    $search_hsh{'net_obj'} = new NetAddr::IP($search)
                        or die "[*] NetAddr::IP($search) error";
                } elsif ($search =~ m|^$ipv4_re$|) {
                    $search_hsh{'ip'} = $search;
                    $search_hsh{'ip_obj'} = new NetAddr::IP($search)
                        or die "[*] NetAddr::IP($search) error";
                } elsif ($search =~ m|\:|) {
                    ### see if this is an IPv6 address
                    if ($search =~ m|\/|) {
                        $search_hsh{'net'} = $search;
                        $search_hsh{'net_obj'} = new6 NetAddr::IP($search)
                            or die "[*] NetAddr::IP($search) error";
                    } else {
                        $search_hsh{'net'} = $search;
                        $search_hsh{'net_obj'} = new6 NetAddr::IP($search)
                            or die "[*] NetAddr::IP($search) error";
                    }
                } else {
                    die "[*] Unrecognized value for $token";
                }
                push @match_criteria, \%search_hsh;
            } else {
                push @match_criteria, {};
            }
        }
    } else {
        @tokens = (qw(
            timestamp
            src
            dst
            sp
            dp
            proto
            flags
            ip_len
            intf
            chain
            log_prefix
        ));
    }

    return \@tokens, \@match_criteria;
}

sub benchmark_mode() {

    my @fw_packets = ();

    print scalar localtime(), " [+] Entering benchmark mode.\n";

    if ($num_packets) {
        print scalar localtime(),
            " [+] Executing a $num_packets packet test.\n";
    } else {
        if ($fw_data_file) {
            print scalar localtime(), ' [+] The --packets command line ',
                "option was not specified.\n";
            print scalar localtime(),
                " [+] Defaulting to read the entire $fw_data_file file.\n";
        } else {
            print scalar localtime(), ' [+] The --packets command line ',
                "option was not specified.\n";
            print scalar localtime(),
                " [+] Defaulting to a 10,000 packet test.\n";
            $num_packets = 10000;
        }
    }

    ### initialize benchmarking test packets if we are running
    ### in benchmark mode
    if ($fw_data_file) {
        if ($num_packets) {
            print scalar localtime(), " [+] Creating $num_packets packet ",
                "array from $fw_data_file\n";
        } else {
            print scalar localtime(), " [+] Creating packet ",
                "array from complete $fw_data_file\n";
        }
        my $ctr = 0;
        open F, "< $fw_data_file" or die "[*] Could not open $fw_data_file: $!";
        while (<F>) {
            chomp;
            push @fw_packets, $_;
            $ctr++;
            last if $num_packets > 0 and $ctr >= $num_packets;
        }
        close F;
    } else {
        ### FIXME better random packet data tests, add IP and TCP options, etc.
        my $test_pkt = 'Feb 15 16:42:58 orthanc kernel: DROP IN=eth0 ' .
            'OUT= MAC=00:a0:cc:28:42:5a:00:03:6c:00:98:54:08:00 ' .
            'SRC=192.168.10.2 DST=192.168.10.1 LEN=48 TOS=0x00 PREC=0x00 ' .
            'TTL=110 ID=13383 DF PROTO=TCP SPT=1389 ';
        my $test_pktend = 'WINDOW=16384 RES=0x00 SYN URGP=0';

        print scalar localtime(), " [+] Creating packet array.\n";
        my $dp = 1000;
        for (my $i=0; $i <= $num_packets; $i++) {
            push @fw_packets, "$test_pkt DPT=$dp $test_pktend";
            $dp++ if $dp < 50000;
        }
    }

    my $b_time = time();
    print scalar localtime(), " [+] check_scan()\n" if $benchmark;
    &check_scan(\@fw_packets);

    print scalar localtime(), " [+] Packet creation and processing time: ",
        time() - $b_time, " sec.\n";
    print scalar localtime(), " [+] Exiting benchmark mode.\n";
    return $EXIT_SUCCESS;
}

sub fw_analyze_mode() {
    my $run_fw_check = 0;

    if ($fw_analyze) {
        $run_fw_check = 1;
    } else {
        ### if psad is running on a syslog server, do not check the firewall
        ### rules since they may not be local.  Also, do not check the
        ### firewall if psad is configured to parse all iptables messages.
        unless ($no_fwcheck or $syslog_server
                or $config{'ENABLE_FW_LOGGING_CHECK'} eq 'N') {
            $run_fw_check = 1;
        }
    }

    my $exit_status = $EXIT_SUCCESS;
    if ($run_fw_check) {
        my $opts = "-c $config_file ";
        $opts .= " --fw-analyze" if $fw_analyze;
        $opts .= " --fw-file $fw_file" if $fw_file;
        $opts .= " -L $lib_dir" if $lib_dir;
        $opts .= " --test-mode" if $test_mode;
        $opts .= " -O $override_config_str" if $override_config_str;
        $opts .= " --no-fw-search-all" if $config{'FW_SEARCH_ALL'} eq 'N';
        $exit_status = (system "$cmds{'fwcheck_psad'} $opts") >> 8;
    }
    return $exit_status;
}

### display the status of all four psad daemons
sub status() {

    my $rv = $EXIT_SUCCESS;

    for my $pidname (qw(psadwatchd kmsgsd psad_fw_read psad)) {
        my $pidfile = $pidfiles{$pidname};
        if (-e $pidfile) {
            my $pid = &is_running($pidfile);
            if ($pid) {
                print "[+] $pidname (pid: $pid)";
                ### FIXME: should probably just parse /proc instead of
                ### using ps
                my @ps_out = @{&run_command($cmds{'ps'}, 'auxww')};
                PS: for my $line (@ps_out) {
                    chomp $line;
                    if ($line =~ /^\S+\s+$pid\s+(\S+)\s+(\S+)/) {
                        print "  %CPU: $1  %MEM: $2\n";
                        print "    Running since: " .
                            localtime((stat($pidfile))[9]) . "\n";
                        ### print individual ip info
                        &status_psad_daemon() if $pidname eq 'psad';
                    }
                }
                print "\n";
            } else {
                my $print = 1;
                if ($pidname eq 'kmsgsd'
                        and ($config{'SYSLOG_DAEMON'} =~ /ulog/i
                        or $config{'ENABLE_SYSLOG_FILE'} eq 'Y')) {
                    $print = 0;
                }
                print "[-] psad: $pidname is not running on ",
                    "$config{'HOSTNAME'}\n" if $print;
                $rv = $EXIT_FAILURE;
            }
        } else {
            my $print = 1;
            if ($pidname eq 'kmsgsd'
                    and ($config{'SYSLOG_DAEMON'} =~ /ulog/i
                    or $config{'ENABLE_SYSLOG_FILE'} eq 'Y')) {
                $print = 0;
            }
            print "[-] psad: pid file $pidfile does not exist for ",
                "$pidname on $config{'HOSTNAME'}\n" if $print;
            if ($rv  eq 'psad') {
                $rv = $EXIT_FAILURE;
            }
        }
    }
    return $rv;
}

sub status_psad_daemon() {
    my $cmdline;

    ### get any command line args
    if (-e $cmdline_file) {
        open CMD, "< $cmdline_file" or die '[*] Could not open ',
            "$cmdline_file: $!";
        $cmdline = <CMD>;
        chomp $cmdline;
    }
    if ($cmdline) {
        print "    Command line arguments: $cmdline\n";
    } else {
        print "    Command line arguments: [none specified]\n";
    }
    print "    Alert email address(es): ",
        "$config{'EMAIL_ADDRESSES'}\n\n";

    ### build @local_nets array
    my $connected_subnets_ar = &get_connected_subnets();
    for my $net (@$connected_subnets_ar) {
        push @local_nets, $net;
    }

    ### import filesystem information in /var/log/psad/
    &import_filesystem_scan_data();

    ### print main status output to stdout
    &print_scan_status();

    return;
}

sub import_filesystem_scan_data() {

    ### import /var/log/psad/<ip> directories
    &import_ip_dirs();

    ### import global packet counters
    &import_packet_counters();

    ### import dshield stats
    &import_dshield_stats();

    ### import iptables prefix stats
    &import_ipt_prefixes();

    ### import top scanned ports
    &import_top_scanned_ports();

    ### import top signatures
    &import_top_sigs();

    ### import top attackers
    &import_top_attackers();

    return;
}

sub print_scan_status() {

    my @lines = ();
    push @lines, "[+] Version: psad v$version\n\n";

    ### print top signature matches
    push @lines, $_ for @{&print_top_sigs()};

    ### print top attackers
    push @lines, $_ for @{&print_top_attackers()};

    ### print top scanned ports
    push @lines, $_ for @{&print_top_scanned_ports()};

    ### print iptables prefixes
    push @lines, $_ for @{&print_ipt_prefixes()};

    ### print iptables prefixes
    unless ($analyze_mode) {
        push @lines, $_ for @{&print_dshield_stats()};

        ### print block status of IP addresses blocked by iptables
        if ($status_ip) {
            my $ar = &print_blocked_ip_status($status_ip);
            push @lines, $_ for @$ar;
        } else {
            my $ar = &print_blocked_ip_status('');
            push @lines, $_ for @$ar;
        }
    }

    ### print packet counters
    push @lines, $_ for @{&print_packet_counters()};

    return if $status_summary;

    push @lines, "[+] IP Status Detail:\n";

    my %uniq_srcs = ();
    my %uniq_dsts = ();

    my $printed = 0;
    for my $dl (qw/5 4 3 2 1/) {
        SRC: for my $src (sort keys %scan) {
            next SRC unless $scan_dl{$src} == $dl;
            my $dl = $scan_dl{$src};
            next SRC unless $dl >= $config{'MIN_DANGER_LEVEL'};
            if ($status_min_dl) {
                next unless $dl >= $status_min_dl;
            }
            $uniq_srcs{$src} = '';
            my $total_dsts = keys %{$scan{$src}};
            my $tot_pkts   = 0;
            my $uniq_sigs  = 0;
            my $tot_protocols = 0;
            for my $dst (keys %{$scan{$src}}) {
                if (defined $scan{$src}{$dst}{'tot_protocols'}) {
                    $tot_protocols += $scan{$src}{$dst}{'tot_protocols'};
                }
                next unless defined $scan{$src}{$dst}{'absnum'};
                $tot_pkts += $scan{$src}{$dst}{'absnum'};
            }
            for my $proto (keys %protocols) {
                for my $dst (keys %{$scan{$src}}) {
                    next unless defined $scan{$src}{$dst}{$proto}
                            and defined $scan{$src}{$dst}{$proto}{'sid'};
                    for my $sid (keys %{$scan{$src}{$dst}{$proto}{'sid'}}) {
                        $uniq_sigs++;
                    }
                }
            }

            ### source IP line
            my $src_str = "\nSRC:  $src, DL: $dl, Dsts: $total_dsts" .
                ", Pkts: $tot_pkts, Total protocols: $tot_protocols, " .
                "Unique sigs: $uniq_sigs";

            my $src_obj = new NetAddr::IP($src) or die "[*] NetAddr::IP($src) error";
            if ($src_obj->version() == 6) {
                $src_str = "\nSRC:  $src (" . $src_obj->short() .
                    "), DL: $dl, Dsts: $total_dsts" .
                    ", Pkts: $tot_pkts, Total protocols: $tot_protocols, " .
                    "Unique sigs: $uniq_sigs";
            }

            unless ($analyze_mode) {
                my $tot_alerts = 0;
                if ($config{'ENABLE_EMAIL_LIMIT_PER_DST'} eq 'Y') {
                    for my $dst (keys %{$scan{$src}}) {
                        $tot_alerts += $scan_email_ctrs{$src}{$dst}{'email_ctr'};
                    }
                } else {
                    $tot_alerts += $scan_email_ctrs{$src}{'email_ctr'};
                }
                $src_str .= ", Email alerts: $tot_alerts";
            }
            if (&is_local($src, $src_obj)) {
                $src_str .= ', Local IP';
            }
            $printed = 1;
            push @lines, "$src_str\n";
            if (defined $p0f{$src}) {
                push @lines, "    Source OS fingerprint(s):\n";
                for my $os (keys %{$p0f{$src}}) {
                    push @lines, "        $os\n";
                }
            } elsif (defined $posf{$src} and defined $posf{$src}{'guess'}) {
                push @lines, "    Source OS fingerprint:\n",
                    "        $posf{$src}{'guess'}\n";
            }
            next unless $total_dsts > 0;
            push @lines, "\n";

            DST: for my $dst (keys %{$scan{$src}}) {
                my $dst_str = "    DST: $dst";
                my $dst_obj = new NetAddr::IP($dst)
                    or die "[*] NetAddr::IP($dst) error";
                if ($dst_obj->version() == 6) {
                    $dst_str = "    DST: $dst (" . $dst_obj->short() . ')';
                }
                if (&is_local($dst, $dst_obj)) {
                    $dst_str .= ', Local IP';
                }
                push @lines, "$dst_str\n";
                $uniq_dsts{$dst} = '';

                if (defined $scan{$src}{$dst}{'chain'}) {
                    for my $chain (keys %{$scan{$src}{$dst}{'chain'}}) {
                        for my $intf (keys %{$scan{$src}{$dst}{'chain'}
                                    {$chain}}) {
                            my $tot_scanned_protocols = 0;
                            for my $proto (keys %{$scan{$src}{$dst}{'chain'}
                                        {$chain}{$intf}}) {
                                $tot_scanned_protocols++;
                                next unless $proto eq 'tcp' or $proto eq 'udp'
                                    or $proto eq 'udplite';
                                my $start_port = $scan{$src}{$dst}
                                        {$proto}{'abs_sp'};
                                my $end_port   = $scan{$src}{$dst}
                                        {$proto}{'abs_ep'};
                                my $ctr = $scan{$src}{$dst}{'chain'}{$chain}
                                        {$intf}{$proto};
                                if ($start_port == $end_port) {
                                    push @lines, "        Scanned ports: " .
                                        uc($proto) . " $start_port, Pkts: $ctr, " .
                                        "Chain: $chain, Intf: $intf\n";
                                } else {
                                    push @lines, "        Scanned ports: " .
                                        uc($proto) . " $start_port-$end_port, Pkts: " .
                                        "$ctr, Chain: $chain, Intf: $intf\n";
                                }
                            }
                            push @lines, "        Total scanned IP protocols: " .
                                "$tot_scanned_protocols, Chain: $chain, Intf: $intf\n";
                        }
                    }
                }

                ### signature matches
                for my $proto (keys %protocols) {
                    next unless defined $scan{$src}{$dst}{$proto}
                        and defined $scan{$src}{$dst}{$proto}{'sid'};

                    for my $sid (keys %{$scan{$src}{$dst}{$proto}{'sid'}}) {
                        my $msg = '[NA]';
                        if (defined $fwsnort_sigs{$sid}
                                    and defined $fwsnort_sigs{$sid}{'msg'}) {
                            $msg = $fwsnort_sigs{$sid}{'msg'};
                        } elsif (defined $sigs{$sid}
                                    and defined $sigs{$sid}{'msg'}) {
                            $msg = $sigs{$sid}{'msg'};
                        }

                        for my $chain (keys %{$scan{$src}{$dst}{$proto}
                                    {'sid'}{$sid}}) {

                            my $matches = $scan{$src}{$dst}{$proto}
                                {'sid'}{$sid}{$chain}{'pkts'};

                            my $sig_str = qq|        Signature match: "$msg"\n| .
                                "            " . uc($proto) .
                                ", Chain: $chain, Count: $matches";

                            if ($proto eq 'tcp' or $proto eq 'udp'
                                    or $proto eq 'udplite') {
                                my $dp = $scan{$src}{$dst}{$proto}
                                    {'sid'}{$sid}{$chain}{'dp'};
                                $sig_str .= ", DP: $dp";
                                if ($proto eq 'tcp') {
                                    my $flags = $scan{$src}{$dst}{$proto}
                                        {'sid'}{$sid}{$chain}{'flags'};
                                    $sig_str .= ", $flags";
                                }
                            }
                            $sig_str .= ", Sid: $sid";
                            push @lines, $sig_str, "\n";
                        }
                    }
                }

                ### icmp validation
                for my $proto ('icmp', 'icmp6') {
                    next unless defined $scan{$src}{$dst}{$proto};
                    if (defined $scan{$src}{$dst}{$proto}{'invalid_type'}) {
                        my $hr = $scan{$src}{$dst}{$proto}{'invalid_type'};
                        for my $type (keys %$hr) {
                            for my $chain (keys %{$hr->{$type}}) {
                                my $pkts = $hr->{$type}->{$chain}->{'pkts'};
                                push @lines, qq|        Invalid | . uc($proto) .
                                    qq| type: "$type" Chain: $chain, Packets: | .
                                    "$pkts\n";
                            }
                        }
                    }
                    if (defined $scan{$src}{$dst}{$proto}{'invalid_code'}) {
                        my $hr = $scan{$src}{$dst}{$proto}{'invalid_code'};
                        for my $type (keys %$hr) {
                            for my $code (keys %{$hr->{$type}}) {
                                for my $chain (keys %{$hr->{$type}->{$code}}) {
                                    my $pkts = $hr->{$type}->{$code}->{$chain}->{'pkts'};
                                    my $type_text = '';
                                    if ($proto eq 'icmp') {
                                        $type_text = $valid_icmp_types{$type}{'text'};
                                    } else {
                                        $type_text = $valid_icmp6_types{$type}{'text'};
                                    }
                                    push @lines, qq|        Invalid | . uc($proto) .
                                        qq| code: "$code" for | . uc($proto) .
                                        qq| "$type_text" packet | .
                                        qq|Chain: $chain, Packets: $pkts\n|;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    unless ($printed) {
        push @lines, "        [NONE]\n";
    }

    push @lines, "\n    Total scan sources: " . (keys %uniq_srcs) . "\n";
    push @lines, "    Total scan destinations: " . (keys %uniq_dsts) . "\n\n";

    my $out_file = $config{'STATUS_OUTPUT_FILE'};
    if ($analyze_mode) {
        $out_file = $config{'ANALYSIS_OUTPUT_FILE'};
    }

    open F, "> $out_file" or die "[*] Could not open $out_file: $!";
    for my $line (@lines) {
        print $line;
        print F $line;
    }
    close F;
    print "[+] These results are available in: $out_file\n";

    return;
}

sub print_blocked_ip_status() {
    my $specific_ip = shift;

    return unless -e $config{'AUTO_BLOCK_IPT_FILE'};

    unlink "$config{'AUTO_BLOCK_IPT_FILE'}.status"
        if -e "$config{'AUTO_BLOCK_IPT_FILE'}.status";

    copy $config{'AUTO_BLOCK_IPT_FILE'},
        "$config{'AUTO_BLOCK_IPT_FILE'}.status";

    open F, "< $config{'AUTO_BLOCK_IPT_FILE'}.status" or
        die "[*] $config{'AUTO_BLOCK_IPT_FILE'}.status: $!";
    my @lines = <F>;
    close F;

    unlink "$config{'AUTO_BLOCK_IPT_FILE'}.status";

    my @print_lines = ();
    if ($specific_ip) {
        push @print_lines,  "    iptables auto-blocking status for: $specific_ip: \n";
    } else {
        push @print_lines,  "    iptables auto-blocked IPs:\n";
    }

    my $ipt = new IPTables::ChainMgr(%ipt_opts)
        or die '[*] Could not acquire IPTables::ChainMgr object.';

    my $found_line = 0;
    for my $line (@lines) {
        chomp $line;
        if ($line =~ /^\s*(\S+)/) {
            my $ip = $1;  ### this may be a subnet
            next unless $ip =~ /$ipv4_re/;
            if ($specific_ip) {
                next unless $ip eq $specific_ip;
            }
            my $timestamp = '';
            my $time_remain = 0;
            ### older versions do not have the timestamp
            if ($line =~ /^\s*\S+\s+(\d+)/) {
                $timestamp = $1;
                unless (defined $scan_dl{$ip}) {
                    ### set the DL here to the minimum currently required
                    $scan_dl{$ip} = $config{'AUTO_IDS_DANGER_LEVEL'};
                }
                my ($timeout, $timeout_str) = &get_block_timeout($scan_dl{$ip});
                if ($timeout > 0) {
                    $time_remain = $timeout - (time() - $timestamp);
                    $time_remain = 0 if $time_remain < 0;

                    push @print_lines,  "      $ip ($time_remain ",
                        "seconds remaining)\n";
                } else {
                    push @print_lines,  "      $ip (unlimited timeout)\n";
                }
            } else {
                push @print_lines,  "      $ip\n";
            }
            my $blocked = 0;
            for my $hr (@ipt_config) {
                if ($ipt->find_ip_rule(
                        $ip,
                        '0.0.0.0/0',
                        $hr->{'table'},
                        $hr->{'to_chain'},
                        $hr->{'target'})) {
                    $blocked = 1;
                    push @print_lines,  "            $hr->{'to_chain'}",
                        "($hr->{'target'})\n" if $verbose;
                }
            }
            $found_line = 1;

            if ($time_remain > 0) {
                ### we should see that the IP is still blocked
                unless ($blocked) {
                    push @print_lines,  '            [not currently blocked, sending ',
                        "cleanup message]\n";
                    system "$cmds{'psad'} --fw-rm-block-ip $ip";
                }
            } else {
                push @print_lines,  '            [expired timeout, sending ',
                    "cleanup message]\n";
                system "$cmds{'psad'} --fw-rm-block-ip $ip";
            }
        }
    }
    push @print_lines,  "        [NONE]\n"
        unless $found_line;
    push @print_lines,  "\n";
    return \@print_lines;
}

sub import_old_scans() {

    &import_filesystem_scan_data();

    &sys_log('imported ' . (keys %scan_dl) . ' scanning IP ' .
            'addresses from previous psad instance');
    return;
}

sub import_packet_counters() {
    return unless -e $config{'PACKET_COUNTER_FILE'};
    open F, "< $config{'PACKET_COUNTER_FILE'}" or
        die "[*] $config{'PACKET_COUNTER_FILE'}: $!";
    while (<F>) {
        if (/(\w+):\s+(\d+)/) {
            $proto_ctrs{$1} = $2;
        }
    }
    close F;
    return;
}

sub print_packet_counters() {
    my $str = "    Total protocol packet counters:\n";
    for my $proto (sort keys %protocols) {
        next unless defined $proto_ctrs{$proto};
        if (defined $protocol_strings{$proto}) {
            $str .= sprintf "%12s: %s\n", $protocol_strings{$proto}{'name'},
                "$proto_ctrs{$proto} pkts, ($protocol_strings{$proto}{'desc'})";
        } else {
            $str .= sprintf "%12s: %s\n", $proto, "$proto_ctrs{$proto} pkts";
        }
    }
    $str .= "\n";

    return [$str];
}

sub import_protocol_strings() {
    open F, "< $config{'PROTOCOLS_FILE'}" or die "[*] Could not ",
        "open $config{'PROTOCOLS_FILE'}: $!";
    while (<F>) {
        ### gre     47      GRE             # General Routing Encapsulation
        next if /^\s*#/;
        if (/^\s*(\S+)\s+(\d+)\s+\S+\s+#\s+(.*)/) {
            $protocol_strings{$2}{'name'} = $1;
            $protocol_strings{$2}{'desc'} = $3;
        }
    }
    close F;

    return;
}

sub import_dshield_stats() {
    if ($config{'ENABLE_DSHIELD_ALERTS'} eq 'Y'
            and -e $config{'DSHIELD_COUNTER_FILE'}) {
        open DS, "< $config{'DSHIELD_COUNTER_FILE'}" or die "[*] Could not ",
            "open $config{'DSHIELD_COUNTER_FILE'}: $!";
        my @lines = <DS>;
        close DS;
        for my $line (@lines) {
            if ($line =~ /emails:\s+(\d+)/) {
                $dshield_email_ctr = $1;
            } elsif ($line =~ /packets:\s+(\d+)/) {
                $dshield_lines_ctr = $1;
            }
        }
    }
    return;
}

sub print_dshield_stats() {
    my @lines = ();
    if ($config{'ENABLE_DSHIELD_ALERTS'} eq 'Y'
            and -e $config{'DSHIELD_COUNTER_FILE'}) {
        push @lines, "    DShield stats:\n";
        push @lines, "      total emails: $dshield_email_ctr\n";
        push @lines, "      total packets: $dshield_lines_ctr\n\n";
    }
    return \@lines;
}

sub import_ipt_prefixes() {
    if (-e $config{'IPT_PREFIX_COUNTER_FILE'}) {
        open F, "< $config{'IPT_PREFIX_COUNTER_FILE'}" or die "[*] Could not ",
            "open $config{'IPT_PREFIX_COUNTER_FILE'}: $!";
        while (<F>) {
            if (/^\s*(.*?):\s+(\d+)/) {
                my $prefix = $1;
                my $count  = $2;
                $ipt_prefixes{$prefix} = $count;
            }
        }
        close F;
    }
    return;
}

sub print_ipt_prefixes() {
    my @lines = ();
    push @lines, "[+] iptables log prefix counters:\n";
    if (%ipt_prefixes) {
        for my $prefix (keys %ipt_prefixes) {
            my $count = $ipt_prefixes{$prefix};
            push @lines, "      \"$prefix\": $count\n";
        }
    } else {
        push @lines, "        [NONE]\n";
    }
    push @lines, "\n";
    return \@lines;
}

sub import_ip_dirs() {

    opendir D, $config{'PSAD_DIR'} or
        die "[*] Could not open dir: $config{'PSAD_DIR'}: $!";
    my @files = readdir D;
    closedir D;

    my $import_ctr = 0;

    my $curr_pwd = getcwd();
    chdir $config{'PSAD_DIR'} or die $!;

    SRCIP: for my $src (@files) {
        next SRCIP unless ($src =~ /$ipv4_re/ and -d $src);
        ### define as many hash keys as we can (older versions
        ### of psad don't include several of these files).
        my $num_emails = 0;
        if (-e "${src}/danger_level") {
            open DL, "< ${src}/danger_level" or next SRCIP;
            my $dl = <DL>;
            close DL;
            chomp $dl;
            next SRCIP unless $dl >= 1;
            $scan_dl{$src} = $dl;  ### set the dl for $src
        }
        if (-e "${src}/os_guess") {
            open OS, "< ${src}/os_guess" or next SRCIP;
            my $os_guess = <OS>;
            close OS;
            chomp $os_guess;
            ### set the os guess for $src
            $posf{$src}{'guess'} = $os_guess;
        }
        if (-e "${src}/p0f_guess") {
            open OS, "< ${src}/p0f_guess" or next SRCIP;
            my @lines = <OS>;
            close OS;
            for my $line (@lines) {
                chomp $line;
                $p0f{$src}{$line} = '';
            }
        }
        opendir IPDIR, $src or next SRCIP;
        my @scan_files = readdir IPDIR;
        closedir IPDIR;

        ### get all of the destination ip addresses
        my %dst_ips;
        for my $scan_file (@scan_files) {
            next if $scan_file =~ /_whois/;  ### may be <src>_whois
            if ($scan_file =~ /($ipv4_re)/) {
                $dst_ips{$1} = '';
            }
        }
        for my $dst (keys %dst_ips) {
            my $email_ctr_file = '';
            if (-e "${src}/${dst}_email_ctr") {
                $email_ctr_file = "${src}/${dst}_email_ctr";
            } elsif (-e "${src}/email_ctr") {
                $email_ctr_file = "${src}/email_ctr";
            }
            if ($email_ctr_file) {
                open E, "< $email_ctr_file" or die "[*] Could not open ",
                    "$email_ctr_file: $!";
                $num_emails = <E>;
                close E;
                chomp $num_emails;
                if ($config{'ENABLE_EMAIL_LIMIT_PER_DST'} eq 'Y') {
                    $scan_email_ctrs{$src}{$dst}{'email_ctr'} = $num_emails;
                } else {
                    $scan_email_ctrs{$src}{'email_ctr'} = $num_emails;
                }
                $scan{$src}{$dst}{'alerted'} = 1;
            } else {
                if ($config{'ENABLE_EMAIL_LIMIT_PER_DST'} eq 'Y') {
                    $scan_email_ctrs{$src}{$dst}{'email_ctr'} = 0;
                } else {
                    $scan_email_ctrs{$src}{'email_ctr'} = 0;
                }
                $scan{$src}{$dst}{'alerted'}   = 0;
            }
            if (-e "${src}/${dst}_packet_ctr") {
                open PKTS, "< ${src}/${dst}_packet_ctr" or die $!;
                my @lines = <PKTS>;
                close PKTS;
                for my $line (@lines) {
                    my $chain;
                    my $intf;
                    my $pkts;
                    my $proto;
                    if ($line =~ /^(\w+)_(\w+)_icmp:\s+(\d+)/) {
                        $chain = $1;
                        $intf  = $2;
                        $pkts  = $3;
                        $chain = uc $chain if $chain eq 'input'
                                or $chain eq 'forward'
                                or $chain eq 'output';
                        $proto = 'icmp';
                        $scan{$src}{$dst}{'chain'}{$chain}{$intf}{$proto}
                            += $pkts;
                        $scan{$src}{$dst}{'absnum'} += $pkts;
                        $scan{$src}{$dst}{'tot_protocols'}++;
                    } elsif ($line =~ /^(\w+)_(\w+)_(tcp|udp):
                            \s+(\d+)\s+\[(\S+)\]/x) {
                        $chain = $1;
                        $intf  = $2;
                        $proto = $3;
                        $pkts  = $4;
                        $chain = uc $chain if $chain eq 'input'
                                or $chain eq 'forward'
                                or $chain eq 'output';
                        my $port_rng = $5;
                        if ($port_rng =~ /(\d+)\-(\d+)/) {
                            $scan{$src}{$dst}{$proto}{'abs_sp'} = $1;
                            $scan{$src}{$dst}{$proto}{'abs_ep'} = $2;
                        } elsif ($port_rng =~ /(\d+)/) {
                            $scan{$src}{$dst}{$proto}{'abs_sp'} = $1;
                            $scan{$src}{$dst}{$proto}{'abs_ep'} = $1;
                        }
                        $scan{$src}{$dst}{'chain'}{$chain}{$intf}{$proto}
                            += $pkts;
                        $scan{$src}{$dst}{'absnum'} += $pkts;
                        $scan{$src}{$dst}{'tot_protocols'}++;
                    } elsif ($line =~ /^(\w+)_(\w+)_(\d+):\s+(\d+)/) {
                        $chain = $1;
                        $intf  = $2;
                        $proto = $3;
                        $pkts  = $4;
                        $chain = uc $chain if $chain eq 'input'
                                or $chain eq 'forward'
                                or $chain eq 'output';
                        $scan{$src}{$dst}{'chain'}{$chain}{$intf}{$proto}
                            += $pkts;
                        $scan{$src}{$dst}{'absnum'} += $pkts;
                        $scan{$src}{$dst}{'tot_protocols'}++;
                    }
                }
            }
            if (-e "${src}/${dst}_start_time") {
                open ST, "< ${src}/${dst}_start_time" or next SRCIP;
                my $s_time = <ST>;
                close ST;
                chomp $s_time;
                $scan{$src}{$dst}{'s_time'} = $s_time;
            }
            if (-e "${src}/${dst}_signatures") {
                open F, "< ${src}/${dst}_signatures" or
                    die "[*] Could not open ",
                        "${src}/${dst}_signatures: $!";
                my @lines = <F>;
                close F;
                for my $line (@lines) {
                    ### Format: <sig time> <sid> <matches> "<msg>" <chain> \
                    ### <proto> <dst port> <flags> <is_fwsnort> <is_psad>
                    ### 1165099853 249 1 "DDOS mstream client to handler" \
                    ### INPUT tcp 15104 SYN 0 0
                    if ($line =~ /^\s*(\d+)\s+(\d+)\s+(\d+)\s+\"(.*?)\"
                            \s+(\w+)\s+(\w+)\s+(\d+)\s+\"(.*?)\"\s+(\d+)
                            \s+(\d+)/x) {
                        my $time    = $1;
                        my $sid     = $2;
                        my $matches = $3;
                        my $msg     = $4;
                        my $chain   = $5;
                        my $proto   = $6;
                        my $dp      = $7;
                        my $flags   = $8;
                        my $is_fwsnort = $9;
                        my $is_psad    = $10;

                        ### build up the %scan sid data
                        $scan{$src}{$dst}{$proto}{'sid'}{$sid}{$chain}
                            {'time'} = $time;
                        $scan{$src}{$dst}{$proto}{'sid'}{$sid}{$chain}
                            {'dp'} = $dp;
                        $scan{$src}{$dst}{$proto}{'sid'}{$sid}{$chain}
                            {'flags'} = $flags;
                        $scan{$src}{$dst}{$proto}{'sid'}{$sid}{$chain}
                            {'pkts'} = $matches;
                        $scan{$src}{$dst}{$proto}{'sid'}{$sid}{$chain}
                            {'is_fwsnort'} = $is_fwsnort;
                        $scan{$src}{$dst}{$proto}{'sid'}{$sid}{$chain}
                            {'is_psad'} = $is_psad;
                    }
                }
            }
        }
        $import_ctr++;
    }
    chdir $curr_pwd or die $!;

    return;
}

sub import_top_sigs() {
    return unless -e $config{'TOP_SIGS_FILE'};

    open F, "< $config{'TOP_SIGS_FILE'}" or die "[*] Could not ",
        "open $config{'TOP_SIGS_FILE'}: $!";
    while (<F>) {
        next unless /\S/;
        next if /^\s*#/;
        if (/^\s*(\d+)\s+\"(.*?)\"\s+(\d+)\s+(\d+)\s+(\w+)/) {
            my $sid = $1;
            my $msg = $2;
            my $matches = $3;
            my $sources = $4;
            my $proto   = $5;
            $top_sigs{$sid} = $matches;
            for (my $i=0; $i<$sources; $i++) {
                ### this is a hack since we can't recover
                ### the specific IP addresses
                $sig_sources{$sid}{$i} = '';
            }
        }
    }
    close F;
    return;
}

sub print_top_sigs() {
    my @lines = ();
    my $printed = 0;
    if ($config{'STATUS_SIGS_THRESHOLD'} > 0) {
        push @lines, "[+] Top $config{'STATUS_SIGS_THRESHOLD'} " .
            "signature matches:\n";
    } else {
        push @lines, "[+] Top signature matches:\n";
    }

    my $ctr = 0;
    for my $sid (sort {$top_sigs{$b} <=> $top_sigs{$a}} keys %top_sigs) {
        my $found = 0;
        my $num_sources = keys %{$sig_sources{$sid}};
        if (defined $sigs{$sid} and defined $sigs{$sid}{'msg'}) {
            push @lines, qq|      "$sigs{$sid}{'msg'}" | .
                qq|($sigs{$sid}{'proto'}),  | .
                qq|Count: $top_sigs{$sid},  Unique sources: | .
                qq|$num_sources,  Sid: $sid\n|;
            $found   = 1;
            $printed = 1;
        } elsif (defined $fwsnort_sigs{$sid}
                and defined $fwsnort_sigs{$sid}{'msg'}) {
            push @lines,  qq|      "$fwsnort_sigs{$sid}{'msg'}" | .
                qq|($fwsnort_sigs{$sid}{'proto'}),  | .
                qq|Count: $top_sigs{$sid},  Unique sources: | .
                qq|$num_sources,  Sid: $sid\n|;
            $found   = 1;
            $printed = 1;
        }
        $ctr++ if $found;
        if ($config{'STATUS_SIGS_THRESHOLD'} > 0) {
            last if $ctr >= $config{'STATUS_SIGS_THRESHOLD'};
        }
    }
    unless ($printed) {
        push @lines, "        [NONE]\n";
    }
    push @lines, "\n";
    return \@lines;
}

sub import_top_scanned_ports() {
    return unless -e $config{'TOP_SCANNED_PORTS_FILE'};

    open F, "< $config{'TOP_SCANNED_PORTS_FILE'}" or die "[*] Could not open ",
        "$config{'TOP_SCANNED_PORTS_FILE'}: $!";
    while (<F>) {
        next unless /\S/;
        next if /^\s*#\s*$/;
        next if /^\s*#\s*Format/;
        chomp;
        ### Format: <proto> <port> <packets>
        if (/^\s*(\w+)\s+(\d+)\s+(\d+)/) {
            my $proto = $1;
            my $port  = $2;
            my $count = $3;
            if ($proto eq 'tcp') {
                $top_tcp_ports{$port} = $count;
            } elsif ($proto eq 'udp') {
                $top_udp_ports{$port} = $count;
            } elsif ($proto eq 'udplite') {
                $top_udplite_ports{$port} = $count;
            }
        }
    }
    close F;
    return;
}

sub print_top_scanned_ports() {
    my @lines = ();
    my $printed = 0;
    if ($config{'STATUS_PORTS_THRESHOLD'} > 0) {
        push @lines, "[+] Top $config{'STATUS_PORTS_THRESHOLD'} scanned ports:\n";
    } else {
        push @lines, "[+] Top scanned ports:\n";
    }

    if (%top_tcp_ports) {
        my $ctr = 0;
        for my $dp (sort {$top_tcp_ports{$b} <=> $top_tcp_ports{$a}}
                keys %top_tcp_ports) {
            my $str = sprintf "      tcp %-5d $top_tcp_ports{$dp} packets\n", $dp;
            push @lines, $str;
            $printed = 1;
            $ctr++;
            if ($config{'STATUS_PORTS_THRESHOLD'} > 0) {
                last if $ctr >= $config{'STATUS_PORTS_THRESHOLD'};
            }
        }
    }

    if (%top_udp_ports) {
        my $ctr = 0;
        push @lines, "\n";
        for my $dp (sort {$top_udp_ports{$b} <=> $top_udp_ports{$a}}
                keys %top_udp_ports) {
            my $str = sprintf "      udp %-5d $top_udp_ports{$dp} packets\n", $dp;
            push @lines, $str;
            $printed = 1;
            $ctr++;
            if ($config{'STATUS_PORTS_THRESHOLD'} > 0) {
                last if $ctr >= $config{'STATUS_PORTS_THRESHOLD'};
            }
        }
    }
    unless ($printed) {
        push @lines, "        [NONE]\n";
    }
    push @lines, "\n";
    return \@lines;
}

sub import_top_attackers() {

    return unless -e $config{'TOP_ATTACKERS_FILE'};

    open F, "< $config{'TOP_ATTACKERS_FILE'}" or die "[*] Could not open ",
        "$config{'TOP_ATTACKERS_FILE'}: $!";
    while (<F>) {
        next unless /\S/;
        next if /^\s*#\s*$/;
        ###  Format: <IP> <DL> <total_packets> <uniq_sigs> <sig_matches> <is_local>
        if (/^\s*($ipv4_re)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d)/) {
            my $ip = $1;
            my $dl = $2;
            my $tot_pkts = $3;
            my $uniq_sigs = $4;
            my $sig_matches = $5;
            my $is_local = $6;

            ### do not add any IP from the top_attackers file
            ### that does not have a /var/log/psad/<ip>
            ### directory
            next unless defined $scan_dl{$ip};
            if (defined $scan_dl{$ip}) {
                $scan_dl{$ip} = $dl
                    if $scan_dl{$ip} < $dl;
            } else {
                $scan_dl{$ip} = $dl;
            }
            $local_src{$ip} = '' if $is_local;
            $top_packet_counts{$ip} = $tot_pkts;
            $top_sig_counts{$ip}    = $sig_matches;
        }
    }
    close F;
    return;
}

sub print_top_attackers() {
    my @lines = ();
    my $printed = 0;
    if ($config{'STATUS_IP_THRESHOLD'} > 0) {
        push @lines, "[+] Top $config{'STATUS_IP_THRESHOLD'} attackers:\n";
    } else {
        push @lines, "[+] Top attackers:\n";
    }

    my $ctr = 0;
    my %pre_sort_dl = ();
    my %ip_objs = ();

    for my $src (sort {$scan_dl{$b} cmp $scan_dl{$a}} keys %scan_dl) {
        if ($status_min_dl) {
            next unless $scan_dl{$src} >= $status_min_dl;
        } else {
            next unless $scan_dl{$src} >= $config{'MIN_DANGER_LEVEL'};
        }
        $pre_sort_dl{$scan_dl{$src}}{$src} = '';
    }

    my $ip6_short_len = 0;
    for my $dl (qw/5 4 3 2 1/) {
        next unless defined $pre_sort_dl{$dl};

        for my $src (sort keys %{$pre_sort_dl{$dl}}) {
            next unless defined $top_packet_counts{$src}
                    or defined $top_sig_counts{$src};

            my $ip_obj = new NetAddr::IP($src)
                or die "[*] NetAddr::IP $src error";

            $ip_objs{$src} = $ip_obj;
            if ($ip_obj->version() == 6) {
                if (length($ip_obj->short()) > $ip6_short_len) {
                    $ip6_short_len = length($ip_obj->short());
                }
            }
        }
    }

    for my $dl (qw/5 4 3 2 1/) {
        next unless defined $pre_sort_dl{$dl};

        for my $src (sort keys %{$pre_sort_dl{$dl}}) {
            next unless defined $top_packet_counts{$src}
                    or defined $top_sig_counts{$src};
            my $str = sprintf "      %-15s DL: %d", $src, $scan_dl{$src};
            if ($ip_objs{$src}->version() == 6) {
                $str = sprintf "      %s (%-${ip6_short_len}s) DL: %d", $src,
                    $ip_objs{$src}->short(), $scan_dl{$src};
            }
            if (defined $top_packet_counts{$src}) {
                $str .= ", Packets: $top_packet_counts{$src}";
            } else {
                $str .= ', Packets: 0';
            }
            if (defined $top_sig_counts{$src}) {
                $str .= ", Sig count: $top_sig_counts{$src}";
            } else {
                $str .= ', Sig count: 0';
            }
            if (defined $local_src{$src}) {
                $str .= ', (local IP)';
            }
            $ctr++;
            if ($config{'STATUS_IP_THRESHOLD'} > 0) {
                last if $ctr >= $config{'STATUS_IP_THRESHOLD'};
            }
            push @lines, $str, "\n";
            $printed = 1;
        }
    }
    unless ($printed) {
        push @lines, "        [NONE]\n";
    }
    push @lines, "\n";
    return \@lines;
}

sub remove_old_scans() {
    opendir D, $config{'PSAD_DIR'} or
        die "[*] Could not open dir: $config{'PSAD_DIR'}: $!";
    my @files = readdir D;
    closedir D;

    my $curr_pwd = getcwd();
    chdir $config{'PSAD_DIR'} or die $!;
    SRCIP: for my $src (@files) {
        next SRCIP unless ($src =~ /$ipv4_re/ and -d $src);
        rmtree $src or die
            "[*] Could not remove $config{'PSAD_DIR'}/$src: $!";
    }
    chdir $curr_pwd or die $!;
    return;
}

sub disk_check() {
    my $fwdata_fh = shift;

    ### See how we are doing on disk space, and remove data
    ### if necessary.
    if (&disk_space_exceeded() and $config{'ENABLE_SYSLOG_FILE'} ne 'Y') {
        $fwdata_fh = &reopen_fw_data($fwdata_fh, $TRUNCATE);
    }
    $last_disk_check = time();

    return $fwdata_fh;
}

sub fw_data_file_check() {
    my $fwdata_fh = shift;

    if ($fw_data_file_check_ctr == 10) {
        if (-e $fw_data_file) {
            my $size_tmp  = -s $fw_data_file;
            my $inode_tmp = (stat($fw_data_file))[1];
            if ($inode_tmp != $fw_data_file_inode
                    or $size_tmp < $fw_data_file_size) {

                &sys_log('[+]', "iptables syslog file $fw_data_file " .
                    "shrank or was rotated, so re-opening");

                $fwdata_fh = &reopen_fw_data($fwdata_fh, $NO_TRUNCATE);

                $skip_first_loop = 1;

                ### set file size and inode
                $fw_data_file_size  = $size_tmp;
                $fw_data_file_inode = $inode_tmp;
            }
        }
        $fw_data_file_check_ctr = 0;
    }
    return $fwdata_fh;
}

sub notifications() {
   my $num_packets = shift;

    ### Print the number of new packets we saw in FW_DATA_FILE if we are
    ### running in debug mode
    if ($debug) {
        print STDERR "[+] MAIN: number of new packets: $num_packets\n";
    }

    if ($die_msg) {
        &print_sys_msg($die_msg, "$config{'PSAD_ERR_DIR'}/psad.die");
        $die_msg = '';
    }

    if ($warn_msg) {
        &print_sys_msg($warn_msg, "$config{'PSAD_ERR_DIR'}/psad.warn");
        $warn_msg = '';
    }
    return;
}

sub usr1() {
    my $rv = $EXIT_SUCCESS;
    my $psad_pidfile = $pidfiles{'psad'};
    if (-e $psad_pidfile) {
        my $pid = &is_running($psad_pidfile);
        if ($pid) {  ### make sure psad is actually running
            if (kill 'USR1', $pid) {
                print "[+] USR1 signal sent to pid: $pid\n";
                for (my $try=0; $try<=20; $try++) {  ### limit attempts to 20
                    sleep 1;
                    print "[+] Checking for file: ",
                        "$config{'PSAD_DIR'}/scan_hash.${pid}\n";
                    if (-e "$config{'PSAD_DIR'}/scan_hash.${pid}") {
                        open U, "< $config{'PSAD_DIR'}/scan_hash.${pid}"
                            or print "[*] Sent psad pid $pid a USR1 ",
                                "signal, but could not open\n",
                                "\"$config{'PSAD_DIR'}/scan_hash.${pid}\n\""
                            and return $rv;
                        print while(<U>);
                        close U;
                        print "[+] Results available in: ",
                            "$config{'PSAD_DIR'}/scan_hash.${pid}\n";
                        last;
                    }
                }
            } else {
                print "[*] Could not send psad the USR1 signal on ",
                    "$config{'HOSTNAME'}\n";
                $rv = $EXIT_FAILURE;
            }
        } else {
            print "[-] psad is not running on $config{'HOSTNAME'}\n";
        }
    }
    return $rv;
}

sub usr1_handler() {
    $usr1_flag = 1;
    return;
}

sub usr1_print_scan() {
    $usr1_flag = 0;  ### clear the USR1 flag

    &sys_log('received USR1 signal, printing scan ' .
        "hashes to $config{'PSAD_DIR'}/scan_hash.$$");

    ### dump scan hash to filesystem
    &print_scan();

    return;
}

sub hup() {
    my $rv = $EXIT_SUCCESS;
    for my $pidname (qw(psadwatchd psad kmsgsd)) {
        my $pidfile = $pidfiles{$pidname};
        my $pid = &is_running($pidfile);
        if ($pid) {
            if (kill 'HUP', $pid) {
                print "[+] HUP signal sent to $pidname (pid: $pid)\n";
            } else {
                print "[*] Could not send $pidname ",
                    "(pid: $pid) a HUP signal.\n";
                $rv = $EXIT_FAILURE;
            }
        } else {
            my $print = 1;
            if ($pidname eq 'kmsgsd'
                    and ($config{'SYSLOG_DAEMON'} =~ /ulog/i
                    or $config{'ENABLE_SYSLOG_FILE'} eq 'Y')) {
                $print = 0;
            }
            print "[-] $pidname daemon not running.\n" if $print;
        }
    }
    return $rv;
}

sub hup_handler() {
    $hup_flag = 1;
    return;
}

sub hup_re_init() {
    my $fwdata_fh = shift;

    &sys_log('received HUP signal, re-importing psad.conf');

    print STDERR "[+] Received HUP signal, re-importing config...\n"
        if $debug;

    my $orig_fwdata = $fw_data_file;
    my $orig_ipt_sockfile = '';

    $orig_ipt_sockfile = $config{'AUTO_IPT_SOCK'}
        if $config{'ENABLE_AUTO_IDS'} eq 'Y';

    ### Re-import all used config files (psad.conf, auto_dl,
    ### posf, signatures) if we received a HUP signal.
    &psad_init();

    if ($orig_fwdata ne $fw_data_file) {
        $fwdata_fh = &reopen_fw_data($fwdata_fh, $NO_TRUNCATE);

        $skip_first_loop = 1;
    }

    if ($config{'ENABLE_AUTO_IDS'} eq 'Y') {
        if ($orig_ipt_sockfile ne $config{'AUTO_IPT_SOCK'}) {
            close $ipt_sock;

            &get_auto_response_domain_sock();

            $skip_first_loop = 1;
        }
    }
    $hup_flag = 0;  ### clear the HUP flag

    return $fwdata_fh;
}

sub die_handler() {
    $die_msg = shift;
    return;
}

### write all warnings to a logfile
sub warn_handler() {
    $warn_msg = shift;
    return;
}

sub archive_data() {

    my $curr_pwd = getcwd();
    chdir $config{'PSAD_DIR'} or die "[*] Could not chdir ",
        "$config{'PSAD_DIR'}: $!";
    unless (-d $config{'SCAN_DATA_ARCHIVE_DIR'}) {
        mkdir $config{'SCAN_DATA_ARCHIVE_DIR'}, 0700 or
            die "[*] Could not create dir: ",
            "$config{'SCAN_DATA_ARCHIVE_DIR'}: $!";
    }

    ### archive all of the old ip address directories since
    ### we are restarting psad (should add a way to import
    ### these directories back into memory)
    opendir D, $config{'PSAD_DIR'} or die "[*] Could not open dir: ",
        "$config{'PSAD_DIR'}: $!";
    my @files = readdir D;
    closedir D;

    IPDIR: for my $file (@files) {
        if ($file =~ /$ipv4_re/ and -d $file) {
            ### check for the danger level associated with this dir
            if (-e "$file/danger_level") {
                open F, "< $file/danger_level" or next IPDIR;
                my $dl = <F>;
                close F;
                chomp $dl;
                if ($dl >= $config{'MIN_ARCHIVE_DANGER_LEVEL'}) {
                    ### $file is an old scaning ip from
                    ### a previous psad execution
                    my $old_ipdir     = $file;
                    my $archive_ipdir =
                        "$config{'SCAN_DATA_ARCHIVE_DIR'}/$old_ipdir";
                    if (-d $archive_ipdir) {
                        rmtree $archive_ipdir;
                    }
                    move $old_ipdir, $archive_ipdir or die "[*] Could not ",
                        "move $old_ipdir -> $archive_ipdir";
                }
            }
        }
    }

    ### archive the fwdata file
    my $fwdata    = $fw_data_file;
    my $fwarchive = "$config{'SCAN_DATA_ARCHIVE_DIR'}/fwdata_archive";
    ### first see how big the archive file is and zero out if
    ### it is larger than about 10,000 lines
    if (-e $fwarchive && (-s $fwarchive) > 2367766) {  ### about 10,000 lines
        &truncate_file($fwarchive) unless $config{'ENABLE_SYSLOG_FILE'} eq 'Y';
    }
    unless (-e $fwdata) {
        chdir $curr_pwd or die $!;
        return;
    }
    open FW, "< $fwdata" or die "$fwdata exists but couldn't open it: $!";
    my @fwlines = <FW>;
    close FW;
    open AR, ">> $fwarchive" or die "Could not open $fwarchive: $!";
    print AR $_ for @fwlines;
    close AR;

    chdir $curr_pwd or die $!;
    return;
}

sub handle_cmdline() {

    if ($analysis_emails and not $analyze_mode) {
        die "[*] Can only specify --email-analysis flag ",
            "when run in --Analyze mode.";
    }

    ### be absolutely sure to disable auto-response for various
    ### offline modes
    $config{'ENABLE_AUTO_IDS'} = 'N'
        if ($analyze_mode and not $analyze_mode_auto_block)
            or $syslog_server or $benchmark or $status_mode;

    ### The -I switch was given
    $config{'CHECK_INTERVAL'} = $chk_interval if $chk_interval;

    ### The --snort-rdir switch was given
    $config{'SNORT_RULES_DIR'} = $snort_rules_dir if $snort_rules_dir;

    ### The --signatures switch was given
    $config{'SIGS_FILE'} = $sigs_file if $sigs_file;

    ### The --passive-os-file switch was given
    $config{'POSF_FILE'} = $posf_file if $posf_file;

    ### The --auto-dl switch was given
    $config{'AUTO_DL_FILE'} = $auto_dl_file if $auto_dl_file;

    ### make sure to go into status display mode if any of the following
    ### args were given.
    $status_mode = 1 if ($status_ip and not $status_mode);
    $status_mode = 1 if ($status_min_dl and not $status_mode);
    $status_mode = 1 if ($status_summary and not $status_mode);

    ### make sure to go into firewall analysis mode if a ruleset
    ### file was specified on the command line.
    $fw_analyze = 1 if $fw_file;

    ### disable whois and DNS lookups if we are running in -A mode.
    $no_whois = 1 if $analyze_mode and not $analysis_whois;
    $no_rdns = 1 if $analyze_mode and not $enable_analysis_dns;

    return;
}

sub make_psad_dirs() {
    for my $dir (qw(
        /var/lib
        /var/run
    )) {
        next if -d $dir;
        mkdir $dir, 0755 or die "[*] Could not mkdir $dir: $!";
    }

    for my $dir (qw(
        PSAD_DIR
        PSAD_RUN_DIR
        PSAD_FIFO_DIR
        PSAD_CONF_DIR
        CONF_ARCHIVE_DIR
        PSAD_ERR_DIR
    )) {
        next if -d $config{$dir};
        mkdir $config{$dir}, 0700 or
            die "[*] Could not mkdir $config{$dir}: $!";
    }
    return;
}

sub setup() {

    &make_psad_dirs();

    unless (-e $config{'PSAD_FIFO_FILE'} and -p $config{'PSAD_FIFO_FILE'}) {
        system "$cmds{'mknod'} -m 600 $config{'PSAD_FIFO_FILE'} p";
    }
    ### make sure the new whois path exists
    if (-x '/usr/bin/whois.psad' and not -x $cmds{'whois'}
            and '/usr/bin/whois.psad' ne $cmds{'whois'}) {
        move '/usr/bin/whois.psad', $cmds{'whois'} or die "[*] Could not ",
            "move /usr/bin/whois.psad -> $cmds{'whois'}";
    }

    $no_email_alerts  = 1 if $config{'ALERTING_METHODS'} =~ /no.?e?mail/i;
    $no_syslog_alerts = 1 if $config{'ALERTING_METHODS'} =~ /no.?syslog/i;

    ### initialize dshield alerting interval
    $dshield_alert_interval = 3600 * $config{'DSHIELD_ALERT_INTERVAL'};

    ### scale back the alerting interval from 24 hours by just enough
    ### to make sure that an alert will be sent each day.
    $dshield_alert_interval -= 1 + $config{'CHECK_INTERVAL'}
        if $config{'DSHIELD_ALERT_INTERVAL'} == 24;

    unless ($hup_flag) {
        my $truncate_or_create = 0;
        my $restart_kmsgsd     = 0;
        if ($config{'TRUNCATE_FWDATA'} eq 'Y'
                and $config{'ENABLE_SYSLOG_FILE'} ne 'Y') {
            $truncate_or_create = 1;
            $restart_kmsgsd     = 1;
        } else {
            unless (-e $fw_data_file) {
                $truncate_or_create = 1;
            }
        }

        ### create the iptables data file if it doesn't exist
        ### (this is better than dying because it isn't there).
        &truncate_file($fw_data_file) if $truncate_or_create;

        ### if we truncate fwdata then we have to restart
        ### any running kmsgsd process
        &restart_kmsgsd() if $restart_kmsgsd;

        ### unlink socket file if it exists from a previous run (only
        ### if we have not received a HUP signal)
        unlink $config{'AUTO_IPT_SOCK'} if -e $config{'AUTO_IPT_SOCK'};

        ### if we are not importing old scans, then remove old counter
        ### values
        if ($config{'IMPORT_OLD_SCANS'} eq 'N') {
            &truncate_file($config{'PACKET_COUNTER_FILE'});
            &truncate_file($config{'IPT_PREFIX_COUNTER_FILE'});
            &truncate_file($config{'DSHIELD_COUNTER_FILE'});
            &truncate_file($config{'TOP_SIGS_FILE'});
            &truncate_file($config{'TOP_SCANNED_PORTS_FILE'});
            &truncate_file($config{'TOP_ATTACKERS_FILE'});
        }

        if ($config{'ENABLE_SYSLOG_FILE'} ne 'Y') {
            chmod 0600, $fw_data_file;
        }
        chmod 0600, $config{'FW_ERROR_LOG'};
    }

    ### we assume that ulogd is properly configured (FIXME?)
    return if $config{'SYSLOG_DAEMON'} =~ /ulog/i;
    return if $config{'ENABLE_SYSLOG_FILE'} eq 'Y';

    die '[*] No system logger config file could be found.'
        unless (-e $config{'ETC_SYSLOG_CONF'}
                or -e $config{'ETC_RSYSLOG_CONF'}
                or -e $config{'ETC_SYSLOGNG_CONF'}
                or -e $config{'ETC_METALOG_CONF'});

    ### attempt to correct syslog config file if it is not configured
    ### correctly.
    my $syslog_conf = '';
    if ($config{'SYSLOG_DAEMON'} eq 'syslogd') {
        $syslog_conf = $config{'ETC_SYSLOG_CONF'};
    } elsif ($config{'SYSLOG_DAEMON'} eq 'rsyslogd') {
        $syslog_conf = $config{'ETC_RSYSLOG_CONF'};
    }

    if ($syslog_conf) {
        if (-e $syslog_conf) {
            unless (-e "$syslog_conf.orig") {
                copy $syslog_conf,
                    "$syslog_conf.orig" or die "[*] Could not ",
                    "copy $syslog_conf -> ",
                    "$syslog_conf.orig";
            }
            open RS, "< $syslog_conf" or
                die "[*] Unable to open $syslog_conf: $!";
            my @lines = <RS>;
            close RS;
            my $found = 0;
            for my $line (@lines) {
                if ($line =~ m/\|\s*$config{'PSAD_FIFO_FILE'}/) {
                    $found = 1;
                    last;
                }
            }
            unless ($found) {
                open SYSLOG, "> $syslog_conf" or
                    die "[*] Unable to open $syslog_conf: $!";
                ### this loop removes any old location for psadfifo
                for my $line (@lines) {
                    unless ($line =~ /psadfifo/i) {
                        print SYSLOG $line;
                    }
                }
                ### reinstate kernel logging to our named pipe
                print SYSLOG "\n### Send kern.info messages to psadfifo for ",
                    "analysis by kmsgsd\n";
                print SYSLOG "kern.info\t\t|$config{'PSAD_FIFO_FILE'}\n";
                close SYSLOG;
                &sys_log('reconfiguring syslogd to write ' .
                    "kern.info messages to $config{'PSAD_FIFO_FILE'}");
                system "$cmds{'killall'} -HUP syslogd";
            }
        } else {
            &send_mail("$config{'MAIL_ERROR_PREFIX'} " .
                "$syslog_conf does not " .
                "exist, check SYSLOG_DAEMON setting on $config{'HOSTNAME'}",
                '', $config{'EMAIL_ADDRESSES'}, $cmds{'mail'});
        }
    } elsif ($config{'SYSLOG_DAEMON'} eq 'syslog-ng') {
        if (-e $config{'ETC_SYSLOGNG_CONF'}) {
            unless (-e "$config{'ETC_SYSLOGNG_CONF'}.orig") {
                copy $config{'ETC_SYSLOGNG_CONF'},
                    "$config{'ETC_SYSLOGNG_CONF'}.orig" or die "[*] Could not ",
                    "copy $config{'ETC_SYSLOGNG_CONF'} -> ",
                    "$config{'ETC_SYSLOGNG_CONF'}.orig";
            }
            open RS, "< $config{'ETC_SYSLOGNG_CONF'}" or
                die "[*] Unable to open $config{'ETC_SYSLOGNG_CONF'}: $!\n";
            my @lines = <RS>;
            close RS;

            my $found = 0;
            for my $line (@lines) {
                next if $line =~ /^\s*#/;
                if ($line =~ m/$config{'PSAD_FIFO_FILE'}/ or $line =~ /psadfifo/) {
                    $found = 1;
                    last;
                }
            }
            unless ($found) {
                open SYSLOGNG, ">> $config{'ETC_SYSLOGNG_CONF'}" or
                    die "[*] Unable to open $config{'ETC_SYSLOGNG_CONF'}: $!";
                print SYSLOGNG "\n",
                    qq|source psadsrc { unix-stream("/dev/log"); |,
                    qq|internal(); pipe("/proc/kmsg"); };\n|,
                    qq|filter f_psad { facility(kern) and match("IN=") |,
                    qq|and match("OUT="); };\n|,
                    'destination psadpipe { ',
                    "pipe(\"$config{'PSAD_FIFO_FILE'}\"); };\n",
                    'log { source(psadsrc); filter(f_psad); ',
                    "destination(psadpipe); };\n";
                close SYSLOGNG;

                &sys_log('reconfiguring syslog-ng to write ' .
                    "kern.info messages to $config{'PSAD_FIFO_FILE'}");
                system "$cmds{'killall'} -HUP syslog-ng";
            }
        } else {
            &send_mail("$config{'MAIL_ERROR_PREFIX'} " .
                "$config{'ETC_SYSLOGNG_CONF'} does not " .
                "exist, check SYSLOG_DAEMON setting on $config{'HOSTNAME'}",
                '', $config{'EMAIL_ADDRESSES'}, $cmds{'mail'});
        }
    } elsif ($config{'SYSLOG_DAEMON'} eq 'metalog') {
         ### Metalog support added by Dennis Freise <cat@final-frontier.ath.cx>
        if (-e $config{'ETC_METALOG_CONF'}) {
            unless (-e "$config{'ETC_METALOG_CONF'}.orig") {
                copy $config{'ETC_METALOG_CONF'},
                    "$config{'ETC_METALOG_CONF'}.orig" or die "[*] Could not ",
                    "copy $config{'ETC_METALOG_CONF'} -> ",
                    "$config{'ETC_METALOG_CONF'}.orig";
            }
            open RS, "< $config{'ETC_METALOG_CONF'}" or
                die "[*] Unable to open $config{'ETC_METALOG_CONF'}: $!\n";
            my @lines = <RS>;
            close RS;

            my $found = 0;
            for my $line (@lines) {
                if ($line =~ m/psadpipe\.sh/) {
                    $found = 1;
                    last;
                }
            }
            unless ($found) {
                open METALOG, "> $config{'ETC_METALOG_CONF'}" or
                    die "[*] Unable to open $config{'ETC_METALOG_CONF'}: $!";
                print METALOG "\n",
                    "\nPSAD :\n",
                    "  facility = \"kern\"\n",
                    '  command  = ',
                    "\"/usr/sbin/psadpipe.sh\"\n";
                close METALOG;
                &sys_log('reconfiguring metalog to write ' .
                        "kern-facility messages to /usr/sbin/psadpipe.sh");

                open PIPESCRIPT, '> /usr/sbin/psadpipe.sh' or
                    die "[*] Unable to open /usr/sbin/psadpipe.sh: $!";
                print PIPESCRIPT "#!/bin/sh\n\n",
                    "echo \"\$3\" >> $config{'PSAD_FIFO_FILE'}\n";
                close PIPESCRIPT;
                chmod 0700, '/usr/sbin/psadpipe.sh';
                &sys_log('generated /usr/sbin/psadpipe.sh ' .
                        "which writes to $config{'PSAD_FIFO_FILE'}");

                ### Metalog seems to simply die on SIGHUP and SIGALRM, and I
                ### found no signal or option to reload it's config... :-(
                die '[*] All files written. You have to manually restart metalog! ',
                    'When done, start psad again.';
#          system "$cmds{'killall'} -HUP metalog";
            }
        } else {
            &send_mail("$config{'MAIL_ERROR_PREFIX'} " .
                "$config{'ETC_METALOG_CONF'} does not " .
                "exist, check SYSLOG_DAEMON setting on $config{'HOSTNAME'}",
                '', $config{'EMAIL_ADDRESSES'}, $cmds{'mail'});
        }
    }

    return;
}

sub restart_kmsgsd() {

    return if $config{'ENABLE_SYSLOG_FILE'} eq 'Y';
    return if $no_kmsgsd or $config{'SYSLOG_DAEMON'} =~ /ulog/i;
    return unless -e $pidfiles{'kmsgsd'};

    my $pid = &is_running($pidfiles{'kmsgsd'});

    return unless $pid;

    &sys_log('restarting kmsgsd since TRUNCATE_FWDATA is enabled');
    kill 9, $pid unless kill 15, $pid;
    system $cmds{'kmsgsd'};

    $kmsgsd_started = 1;

    return;
}

sub get_scale_factor() {
    my $num_packets = shift;

    return 0 unless $analyze_mode;

    my $val = 0;

    if ($num_packets < 100) {
        $val = $num_packets;
    } else {
        $val = int($num_packets/10);
        if ($val < 100) {
            $val -= $val % 10;
        } elsif ($val < 1000) {
            $val -= $val % 100;
        } elsif ($val < 10000) {
            $val -= $val % 1000;
        } elsif ($val < 100000) {
            $val -= $val % 10000;
        } elsif ($val < 1000000) {
            $val -= $val % 100000;
        } else {
            $val = 50000;
        }
    }
    $val++;
    return $val;
}

sub truncate_file() {
    my $file = shift;
    open F, "> $file" or die "[*] Could not open $file: $!";
    close F;
    return;
}

sub disk_space_exceeded() {

    my $curr_pwd = getcwd();
    my @df_data = @{&run_command($cmds{'df'}, $config{'PSAD_DIR'})};
    my ($prcnt) = ($df_data[$#df_data] =~ /(\d+)%/);
    my $rv = 0;
    if ($config{'DISK_MAX_PERCENTAGE'} > 0
            and $prcnt > $config{'DISK_MAX_PERCENTAGE'}) {
        ### need to remove data
        $rv = 1;
        $rm_data_ctr++;
        &sys_log("disk partition associated with " .
            "$config{'PSAD_DIR'} exceeded " .
            "$config{'DISK_MAX_PERCENTAGE'} prct utilization.");
        &send_mail("$config{'MAIL_ERROR_PREFIX'} Exceeded max disk " .
            "utilization for $config{'PSAD_DIR'} on $config{'HOSTNAME'}",
            '', $config{'EMAIL_ADDRESSES'}, $cmds{'mail'});
        &sys_log("removing data in $config{'PSAD_DIR'}");
        if (-d $config{'SCAN_DATA_ARCHIVE_DIR'}) {
            ### remove the entire archive directory (we have run out of
            ### disk so keeping old scan directories around is the least
            ### of our worries).
            &sys_log("removing $config{'SCAN_DATA_ARCHIVE_DIR'} directory");
            rmtree $config{'SCAN_DATA_ARCHIVE_DIR'};
            mkdir $config{'SCAN_DATA_ARCHIVE_DIR'}, 0700;
        }
        opendir D, $config{'PSAD_DIR'} or
            die "[*] Could not open dir: $config{'PSAD_DIR'}: $!";
        my @ipdirs = readdir D;
        closedir D;

        chdir $config{'PSAD_DIR'} or die $!;
        for my $ipdir (@ipdirs) {
            if ($ipdir =~ /$ipv4_re/ and -d $ipdir) {
                opendir IP, $ipdir or die $!;
                my @scanfiles = readdir IP;
                closedir IP;

                for my $file (@scanfiles) {
                    if (-e "${ipdir}/$file" and $file =~ /_signatures/) {
                        unlink "${ipdir}/$file";
                    }
                }
            }
        }
        if ($rm_data_ctr > $config{'DISK_MAX_RM_RETRIES'}) {
            &sys_log("could not sufficiently reduce disk " .
                "utilization in $config{'PSAD_DIR'} partition.  " .
                "Stopping psad!");
            &send_mail("$config{'MAIL_ERROR_PREFIX'} Could not " .
                "reduce disk utilization on " .
                $config{'HOSTNAME'}, '', $config{'EMAIL_ADDRESSES'},
                $cmds{'mail'});
            &send_mail("$config{'MAIL_FATAL_PREFIX'} Stopping psad " .
                "on $config{'HOSTNAME'}!",
                '', $config{'EMAIL_ADDRESSES'},
                $cmds{'mail'});
            for my $pidname (qw(psadwatchd kmsgsd)) {
                my $pidfile = $pidfiles{$pidname};
                my $pid = &is_running($pidfile);
                if ($pid) {
                    unless (kill 15, $pid) {  ### attempt to stop with SIGTERM
                        kill 9, $pid;
                    }
                }
            }
            exit 1;
        }
    } else {
        ### the disk check interval was exceeded but the utilization is ok.
        $rm_data_ctr = 0;
    }
    chdir $curr_pwd or die $!;
    return $rv;
}

sub dump_conf() {
    my $fh = *STDOUT;
    $fh = *STDERR if $debug;

    ### uname output
    print $fh "[+] uname output:\n";
    my @uname_out = @{&run_command($cmds{'uname'}, '-a')};
    if (@uname_out) {
        for (@uname_out) {
            s/Linux\s+(\S+)\s/Linux (removed) /;
            print $fh $_;
        }
    }
    print $fh "\n";

    ### perl version (we assume perl is in the path)
    print $fh "[+] perl info:\n";
    my @perl_info = @{&run_command('perl', '-V')};
    if (@perl_info) {
        print $fh $_ for @perl_info;
    }
    print $fh "\n";

    print $fh "[+] syslog processes:\n";
    my @ps_out = @{&run_command($cmds{'ps'}, 'auxww')};
    if (@ps_out) {
        for (@ps_out) {
            print $fh $_ if m|syslog|i;
        }
    }
    print $fh "\n";

    print $fh "[+] psad processes:\n";
    my @ps_psad_out = @{&run_command($cmds{'ps'}, 'auxww')};
    if (@ps_psad_out) {
        for (@ps_psad_out) {
            print $fh $_ if m|psad|i;
        }
    }
    print $fh "\n";

    if ($config{'IFCFGTYPE'} =~ /iproute2/i) {
        print $fh "[+] ip addr output:\n";
        my @ip_out = @{&run_command($cmds{'ip'}, 'addr')};
        if (@ip_out) {
            for (@ip_out) {
                s/$ipv4_re/x.x.x.x/g;
                s/inet6\s+\S+/inet6 (removed)/;
                print $fh $_;
            }
        }
    } else {
        print $fh "[+] ifconfig output:\n";
        my @ifconfig_out = @{&run_command($cmds{'ifconfig'}, '-a')};
        if (@ifconfig_out) {
            for (@ifconfig_out) {
                s/$ipv4_re/x.x.x.x/g;
                s/inet6\s+addr:\s+\S+/inet6 addr: (removed)/;
                print $fh $_;
            }
        }
    }
    print $fh "\n";

    print $fh "\n[+] psad v$version\n\n";
    my $install_log = '/var/log/psad/install.log';
    if (-e $install_log) {
        print $fh "[+] $install_log exists.\n\n";
    } else {
        print $fh "[+] $install_log does NOT exist.\n\n";
    }

    print $fh "[+] Dumping psad config from: $config_file\n\n";
    for my $var (sort keys %config) {
        my $str = $config{$var};
        ### sanitize sensitive information
        $str = '(removed)' if $var eq 'DSHIELD_USER_EMAIL';
        $str = '(removed)' if $var eq 'DSHIELD_USER_ID';
        $str = '(removed)' if $var eq 'EMAIL_ADDRESSES';
        $str = '(removed)' if $var eq 'HOME_NET';
        $str = '(removed)' if $var eq 'HOSTNAME';
        $str = '(removed)' if $var eq 'EXTERNAL_NET';
        $str = '(removed)' if $var =~ m|SERVERS|;
        printf $fh "%-30s %s\n", "    $var", $str;
    }
    print $fh "\n[+] Command paths:\n\n";
    for my $var (sort keys %cmds) {
        printf $fh "%-30s %s\n", "[+] $var", $cmds{$var};
    }

    ### sometimes we exit after calling this function, and if not
    ### the return value is ignored
    return $EXIT_SUCCESS;
}

sub dump_ipt_policy() {
    my $rv = $EXIT_SUCCESS;

    my $fh = *STDOUT;
    $fh = *STDERR if $debug;

    if ($config{'ENABLE_IPV6_DETECTION'} eq 'Y') {
        print $fh "\n[+] ip6tables policy dump:\n";
        if (defined $cmds{'ip6tables'} and -x $cmds{'ip6tables'}) {
            my @ipt_ver = @{&run_command($cmds{'ip6tables'}, '-V')};
            if (@ipt_ver) {
                print $fh $_ for @ipt_ver;
                print "\n";
            }
            my @lines = @{&run_command($cmds{'ip6tables'}, '-v -n -L')};
            for my $line (@lines) {
                unless ($fw_include_ips) {
                    ### always include ::/0
                    $line =~ s|\s\:\:/0\s|___PsAd0Net___|g;
                    $line =~ s|\s([A-Fa-f0-9\:]{2,40}/\d{1,3})\s| ::/x |g;
                    $line =~ s|___PsAd0Net___| ::/0 |g;
                }
                print $fh $line;
            }
        } else {
            print $fh "[*] Could not find ip6tables command.\n";
            $rv = $EXIT_FAILURE;
        }
    }

    print $fh "\n[+] iptables policy dump:\n";
    if (defined $cmds{'iptables'} and -x $cmds{'iptables'}) {
        my @ipt_ver = @{&run_command($cmds{'iptables'}, '-V')};
        if (@ipt_ver) {
            print $fh $_ for @ipt_ver;
            print "\n";
        }
        my @lines = @{&run_command($cmds{'iptables'}, '-v -n -L')};
        for my $line (@lines) {
            unless ($fw_include_ips) {
                ### always include 0.0.0.0/0
                $line =~ s|0\.0\.0\.0/0|___PsAd0Net___|g;
                $line =~ s|0\.0\.0\.0|___PsAd0IP___|g;
                $line =~ s|($ipv4_re/\d+)|x.x.x.x/x|g;
                $line =~ s|($ipv4_re)|x.x.x.x|g;
                $line =~ s|___PsAd0Net___|0.0.0.0/0|g;
                $line =~ s|___PsAd0IP___|0.0.0.0|g;
            }
            print $fh $line;
        }
    } else {
        print $fh "[*] Could not find iptables command.\n";
        $rv = $EXIT_FAILURE;
    }

    return $rv;
}

sub sys_log_mline() {
    my $aref = shift;
    for (my $i=0; $i<5 && $i<=$#$aref; $i++) {
        &sys_log($aref->[$i]);
    }
    return;
}

### write a message to syslog
sub sys_log() {
    my $msg = shift;

    print STDERR "[+] syslog msg: $msg\n" if ($test_mode or $debug);

    return unless $imported_syslog_module;
    return if $no_syslog_alerts;

    ### this is an ugly hack to avoid the 'can't use string as subroutine'
    ### error because of 'use strict'
    if ($config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL7/i) {
        openlog($config{'SYSLOG_IDENTITY'}, &LOG_DAEMON(), &LOG_LOCAL7());
    } elsif ($config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL6/i) {
        openlog($config{'SYSLOG_IDENTITY'}, &LOG_DAEMON(), &LOG_LOCAL6());
    } elsif ($config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL5/i) {
        openlog($config{'SYSLOG_IDENTITY'}, &LOG_DAEMON(), &LOG_LOCAL5());
    } elsif ($config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL4/i) {
        openlog($config{'SYSLOG_IDENTITY'}, &LOG_DAEMON(), &LOG_LOCAL4());
    } elsif ($config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL3/i) {
        openlog($config{'SYSLOG_IDENTITY'}, &LOG_DAEMON(), &LOG_LOCAL3());
    } elsif ($config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL2/i) {
        openlog($config{'SYSLOG_IDENTITY'}, &LOG_DAEMON(), &LOG_LOCAL2());
    } elsif ($config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL1/i) {
        openlog($config{'SYSLOG_IDENTITY'}, &LOG_DAEMON(), &LOG_LOCAL1());
    } elsif ($config{'SYSLOG_FACILITY'} =~ /LOG_LOCAL0/i) {
        openlog($config{'SYSLOG_IDENTITY'}, &LOG_DAEMON(), &LOG_LOCAL0());
    }

    if ($config{'SYSLOG_PRIORITY'} =~ /LOG_INFO/i) {
        syslog(&LOG_INFO(), $msg);
    } elsif ($config{'SYSLOG_PRIORITY'} =~ /LOG_DEBUG/i) {
        syslog(&LOG_DEBUG(), $msg);
    } elsif ($config{'SYSLOG_PRIORITY'} =~ /LOG_NOTICE/i) {
        syslog(&LOG_NOTICE(), $msg);
    } elsif ($config{'SYSLOG_PRIORITY'} =~ /LOG_WARNING/i) {
        syslog(&LOG_WARNING(), $msg);
    } elsif ($config{'SYSLOG_PRIORITY'} =~ /LOG_ERR/i) {
        syslog(&LOG_ERR(), $msg);
    } elsif ($config{'SYSLOG_PRIORITY'} =~ /LOG_CRIT/i) {
        syslog(&LOG_CRIT(), $msg);
    } elsif ($config{'SYSLOG_PRIORITY'} =~ /LOG_ALERT/i) {
        syslog(&LOG_ALERT(), $msg);
    } elsif ($config{'SYSLOG_PRIORITY'} =~ /LOG_EMERG/i) {
        syslog(&LOG_EMERG(), $msg);
    }

    closelog();
    return;
}

sub run_command() {
    my ($cmd_path, $args) = @_;
    my $cmd = $cmd_path;
    $cmd .= " $args" if $args;
    print STDERR "[+] run_command(): $cmd\n" if $debug;
    open CMD, "$cmd |" or die "[*] Could not ",
        "execute $cmd: $!";
    my @lines = <CMD>;
    close CMD;
    return \@lines;
}

sub download_signatures() {

    &archive_conf($config{'SIGS_FILE'});

    ### for wget
    &check_commands({'sendmail'=>'', 'mail'=>''});

    my $curr_pwd = getcwd();
    chdir '/tmp' or die $!;

    print "[+] Downloading latest signatures from:\n",
        "        $config{'SIG_UPDATE_URL'}\n";

    unlink 'signatures' if -e 'signatures';

    ### download the file
    system "$cmds{'wget'} $config{'SIG_UPDATE_URL'}";

    die "[*] Could not download signature file" unless -e 'signatures';

    unlink $config{'SIGS_FILE'} if -e $config{'SIGS_FILE'};
    move 'signatures', $config{'SIGS_FILE'};

    print
"[+] New signature file $config{'SIGS_FILE'} has been put in\n",
"    place. You can restart psad (or use 'psad -H') to import the\n",
"    new sigs.\n";

    chdir $curr_pwd or die $!;

    return $EXIT_SUCCESS;
}

sub date_time() {
    my $date_str = shift;

    my $time = time();

    my $date = $time;

    ### Feb 27 12:36:57
    if ($date_str =~ /^\s*(\w+)\s+(\d+)\s+(\d{2}):(\d{2}):(\d{2})/) {
        my $m_tmp = $1;  ### kludge for Decode_Month() call
        my $month = Decode_Month($m_tmp);
        my $day   = sprintf("%.2d", $2);
        my $hour  = $3;
        my $min   = $4;
        my $sec   = $5;

        $date = Date_to_Time(This_Year(), $month, $day, $hour, $min, $sec);

        if ($date > $time) {
            ### date is in the future because the iptables syslog message
            ### does not include the year - subtract one year
            $date -= 60*60*24*356;
        }
    }

    return $date;
}

sub archive_conf() {
    my $file = shift;

    my $curr_pwd = getcwd();
    chdir $config{'CONF_ARCHIVE_DIR'} or die $!;
    my ($filename) = ($file =~ m|.*/(.*)|);
    my $base = "${filename}.old";
    for (my $i = 5; $i > 1; $i--) {  ### keep five copies of old config files
        my $j = $i - 1;
        unlink "${base}${i}.gz" if -e "${base}${i}.gz";
        if (-e "${base}${j}.gz") {
            move "${base}${j}.gz", "${base}${i}.gz" or die "[*] Could not ",
                "move ${base}${j}.gz -> ${base}${i}.gz: $!";
        }
    }
    print "[+] Archiving original $file -> ${base}1\n";

    unlink "${base}1.gz" if -e "${base}1.gz";
    ### move $file into the archive directory
    copy $file, "${base}1" or die "[*] Could not copy ",
        "$file -> ${base}1: $!";

    system "$cmds{'gzip'} ${base}1";
    chdir $curr_pwd or die $!;

    return;
}

sub import_override_configs() {
    my @override_configs = split /,/, $override_config_str;
    for my $file (@override_configs) {
        die "[*] Override config file $file does not exist"
            unless -e $file;
        &import_config($file);
    }
    return;
}

sub import_config() {
    my $conf_file = shift;

    open C, "< $conf_file" or die "[*] Could not open " .
        "config file $conf_file: $!";
    my @lines = <C>;
    close C;
    for my $line (@lines) {
        chomp $line;
        next if ($line =~ /^\s*#/);
        if ($line =~ /^\s*(\S+)\s+(.*?)\;/) {
            my $varname = $1;
            my $val     = $2;
            if ($val =~ m|/.+| && $varname =~ /^\s*(\S+)Cmd$/) {
                ### found a command
                $cmds{$1} = $val unless defined $cmds{$1};
            } else {
                $config{$varname} = $val unless defined $config{$varname};
            }
        }
    }
    return;
}

sub expand_vars() {

    my $has_sub_var = 1;
    my $resolve_ctr = 0;

    while ($has_sub_var) {
        $resolve_ctr++;
        $has_sub_var = 0;
        if ($resolve_ctr >= 20) {
            die "[*] Exceeded maximum variable resolution counter.";
        }
        for my $hr (\%config, \%cmds) {
            for my $var (keys %$hr) {
                my $val = $hr->{$var};
                if ($val =~ m|\$(\w+)|) {
                    my $sub_var = $1;
                    die "[*] sub-ver $sub_var not allowed within same ",
                        "variable $var" if $sub_var eq $var;
                    if (defined $config{$sub_var}) {
                        if ($sub_var eq 'INSTALL_ROOT'
                                and $config{$sub_var} eq '/') {
                            $val =~ s|\$$sub_var||;
                        } else {
                            $val =~ s|\$$sub_var|$config{$sub_var}|;
                        }
                        $hr->{$var} = $val;
                    } else {
                        die "[*] sub-var \"$sub_var\" not defined in ",
                            "config for var: $var."
                    }
                    $has_sub_var = 1;
                }
            }
        }
    }
    return;
}

sub is_root() {

    ### exceptions
    for my $var ($download_sigs,
            $analyze_mode,
            $get_next_rule_id,
            $gnuplot_mode,
            $csv_mode,
            $dump_conf,
            $status_mode,
            $benchmark,
    ) {
        return if $var;
    }

    $< == 0 and $> == 0 or
        die '[*] psad: You must be root (or equivalent ',
            "UID 0 account) to execute psad!  Exiting.";
}

### check to make sure all required varables are defined in the config
### this subroutine is passed different variables by each script that
### correspond to only those variables needed be each script).
sub defined_vars() {
    my $varnames_ar = shift;
    for my $var (@$varnames_ar) {
        unless (defined $config{$var}) {  ### missing var
            die "[*] The config file \"$config_file\" does not " .
                  "contain the\nvariable: \"$var\".  Exiting!";
        }
    }
    return;
}

### check paths to commands and attempt to correct if any are wrong.
sub check_commands() {
    my $exceptions_hr = shift;

    if ($no_whois or $config{'ENABLE_WHOIS_LOOKUPS'} eq 'N') {
        $exceptions_hr->{'whois'} = '';
    } else {
        unless (defined $cmds{'whois'}
                and -x $cmds{'whois'}) {
            ### try setting to normal /usr/bin/whois path, and let
            ### the code below work out the correct path if necessary
            $cmds{'whois'} = '/usr/bin/whois';
        }
    }

    my @path = (qw(
        /bin
        /sbin
        /usr/bin
        /usr/sbin
        /usr/local/bin
        /usr/local/sbin
    ));

    CMD: for my $cmd (keys %cmds) {
        next CMD if defined $exceptions_hr->{$cmd};

        ### both mail and sendmail are special cases, mail is not required
        ### if "nomail" is set in REPORT_METHOD, and sendmail is only
        ### required if DShield alerting is enabled and a DShield user
        ### email is set.
        if ($cmd eq 'mail') {
            next CMD if $config{'ALERTING_METHODS'} =~ /no.?e?mail/i;
        } elsif ($cmd eq 'sendmail') {
            next CMD unless ($config{'ENABLE_DSHIELD_ALERTS'} eq 'Y'
                    and $config{'DSHIELD_ALERT_EMAIL'} ne 'NONE');
        }

        if ($cmd eq 'ip6tables') {
            next CMD unless $config{'ENABLE_IPV6_DETECTION'} eq 'Y';
        }

        unless (-x $cmds{$cmd}) {
            my $found = 0;
            PATH: for my $dir (@path) {
                if (-x "${dir}/${cmd}") {
                    $cmds{$cmd} = "${dir}/${cmd}";
                    $found = 1;
                    last PATH;
                }
            }
            unless ($found) {
                unless (defined $exceptions_hr->{$cmd}) {
                    die "[*] Could not find $cmd, edit $config_file";
                }
            }
        }
        unless (-x $cmds{$cmd}) {
            unless (defined $exceptions_hr->{$cmd}) {
                die "[*] $cmd is located at ",
                    "$cmds{$cmd}, but is not executable\n",
                    "    by uid: $<";
            }
        }
    }

    unless ($no_whois or $config{'ENABLE_WHOIS_LOOKUPS'} eq 'N') {
        ### disable whois lookups if a suitable whois client could not
        ### be found.
        unless (defined $cmds{'whois'}
                and -x $cmds{'whois'}) {  ### we couldn't find whois_psad
            warn '[-] Could not locate whois binary, ',
                "disabling whois lookups.\n";
            $no_whois = 1;
        }
    }

    return;
}

sub is_running() {
    my $pidfile = shift or die '[*] Must supply a pid file.';
    return 0 unless -e $pidfile;
    open PIDFILE, "< $pidfile" or die "[*] Could not open $pidfile: $!";
    my $pid = <PIDFILE>;
    close PIDFILE;
    chomp $pid;
    return $pid if (kill 0, $pid);  ### pid is running
    return 0;
}

sub look_for_process() {
    my $process_re = shift;
    for (@{&run_command($cmds{'ps'}, 'auxww')}) {
        return 1 if /$process_re/;
    }
    return 0;
}

### make sure pid is unique
sub unique_pid() {
    my $pidfile = shift;
    if (&is_running($pidfile)) {
        my $msg = "Another $0 process is already running, exiting.";
        &sys_log($msg);
        die "[*] $msg";
    }
    return;
}

### write the pid to the pid file
sub write_pid() {
    my $pidfile = shift;
    open PIDFILE, "> $pidfile" or die "[*] Could not ",
        "open pidfile $pidfile: $!\n";
    print PIDFILE $$ . "\n";
    close PIDFILE;
    chmod 0600, $pidfile;
    return;
}

### write command line to cmd file
sub write_cmd_line() {
    my ($args_ar, $cmdline_file) = @_;
    open CMD, "> $cmdline_file";
    print CMD "@$args_ar\n";
    close CMD;
    chmod 0600, $cmdline_file;
    return;
}

### send mail message to all addresses contained in the
### EMAIL_ADDRESSES variable within psad.conf ($addr_str).
### TODO:  Would it be better to use Net::SMTP here?
sub send_mail() {
    my ($subject, $body_file, $addr_str, $mailCmd) = @_;
    return if $no_email_alerts;

    my @mail_body_lines = ();
    if ($body_file) {
        open F, "< $body_file" or die "[*] Could not open mail file: ",
            "$body_file: $!";
        @mail_body_lines = <F>;
        close F;
    }

    open MAIL, qq{| $mailCmd -s "$subject" $addr_str > /dev/null 2>&1} or die
        qq{[*] Could not send mail: $mailCmd -s "$subject" $addr_str: $!};
    if ($body_file and @mail_body_lines) {
        print MAIL for @mail_body_lines;
    }
    close MAIL;
    return;
}

### write a message to a file
sub print_sys_msg() {
    my ($msg, $file) = @_;
    open F, ">> $file" or die "[*] Could not open $file: $!";
    print F scalar localtime(), " psad v$version ",
        "pid: $$ $msg";
    close F;
    return;
}

sub getopt_wrapper() {
    ### make Getopts case sensitive
    Getopt::Long::Configure('no_ignore_case');

    die "[*] See 'psad -h' for usage information" unless (GetOptions(
        'signatures=s'      => \$sigs_file,       # Path to psad signatures file.
        'sig-update'        => \$download_sigs,   # Download the latest signatures from
                                                  # http://www.cipherdyne.org/psad/signatures
        'passive-os-sigs=s' => \$posf_file,       # Path to passive os fingerprinting
                                                  #   signatures.
        'snort-type=s'      => \$srules_type,     # Only process snort rules of
                                                  #   this type (e.g. "ddos" or
                                                  #   "backdoor").
        'snort-rdir=s'      => \$snort_rules_dir, # Specify a directory for snort
                                                  #   rules.
        'auto-dl=s'         => \$auto_dl_file,    # Path to psad auto IPs file for
                                                  #   auto-setting IP danger level.
        'use-store-file=s'  => \$store_file,      # Path to parsed data written to by Storable
        'Analyze-msgs'      => \$analyze_mode,    # Analysis mode for old iptables
                                                  #   messages in the psad fwdata file
                                                  #   (or messages file; see
                                                  #   --messages).
        'analysis-write-data' => \$analyze_write_data, # Write data to filesystem from
                                                       # -A mode (this can take a long
                                                       # time).
        'analyze-write-data'  => \$analyze_write_data,
        'analysis-auto-block' => \$analyze_mode_auto_block, # enable auto-blocking (if
                                                  # so configured) in -A mode.
        'analysis-fields=s' => \$analysis_fields, # Place a criteria on various fields
                                                  #   that are parsed from an iptables
                                                  #   logfile.
        'analyze-fields=s'  => \$analysis_fields, # Synonym.
        'stdin'             => \$pkts_from_stdin,
        'whois-analysis'    => \$analysis_whois,  # Issue whois lookups in analysis
                                                  #   mode.
        'dns-analysis'      => \$enable_analysis_dns,  # Issue DNS lookups in -A mode.
        'email-analysis'    => \$analysis_emails, # Send analysis mode emails.
        'messages-file=s'   => \$fw_data_file,    # Specify the path to file containing
                                                  #   old iptables messages (fwdata by
                                                  #   default).
        'get-next-rule-id'  => \$get_next_rule_id, # Show the next available signature ID.

        ### gnuplot options
        'gnuplot'                => \$gnuplot_mode,         # gnuplot mode.
        'gnuplot-dat-file=s'     => \$gnuplot_data_file,    # gnuplot .dat file.
        'gnuplot-plot-file=s'    => \$gnuplot_plot_file,    # gnuplot .gnu file.
        'gnuplot-png-file=s'     => \$gnuplot_png_file,     # gnuplot .gnu file.
        'gnuplot-interactive'    => \$gnuplot_interactive,  # launch gnuplot.
        'gnuplot-title=s'        => \$gnuplot_title,        # Set gnuplot title.
        'gnuplot-legend-titls=s' => \$gnuplot_legend_title, # Set gnuplot legend title.
        'gnuplot-x-label=s'      => \$gnuplot_x_label,
        'gnuplot-xlabel=s'       => \$gnuplot_x_label,
        'gnuplot-x-range=s'      => \$gnuplot_x_range,
        'gnuplot-xrange=s'       => \$gnuplot_x_range,
        'gnuplot-y-label=s'      => \$gnuplot_y_label,
        'gnuplot-ylabel=s'       => \$gnuplot_y_label,
        'gnuplot-y-range=s'      => \$gnuplot_y_range,
        'gnuplot-yrange=s'       => \$gnuplot_y_range,
        'gnuplot-z-label=s'      => \$gnuplot_z_label,
        'gnuplot-zlabel=s'       => \$gnuplot_z_label,
        'gnuplot-z-range=s'      => \$gnuplot_z_range,
        'gnuplot-zrange=s'       => \$gnuplot_z_range,
        'gnuplot-graph-style=s'  => \$gnuplot_graph_style,
        'gnuplot-sort-style=s'   => \$gnuplot_sort_style,
        'gnuplot-3d'             => \$gnuplot_3d,
        'gnuplot-3D'             => \$gnuplot_3d,
        'gnuplot-view=s'         => \$gnuplot_view,
        'gnuplot-grayscale'      => \$gnuplot_grayscale,
        'gnuplot-file-prefix=s'  => \$gnuplot_file_prefix,
        'gnuplot-template=s'     => \$gnuplot_template_file,

        'CSV'               => \$csv_mode,        # CSV mode.
        'CSV-fields=s'      => \$csv_fields,      # Specify list of CSV fields.
        'CSV-unique-lines'  => \$csv_print_uniq,  # Only print unique lines in CSV output.
        'CSV-max-lines=i'   => \$csv_line_limit,  # Limit the number of CSV output lines.
        'CSV-start-line=i'  => \$csv_start_line,  # Starting line in CSV file.
        'CSV-end-line=i'    => \$csv_end_line,    # Ending line in CSV file.
        'CSV-regex=s'       => \$csv_regex,       # Require additional regex match.
        'CSV-neg-regex=s'   => \$csv_neg_regex,   # Require additional negative regex
                                                  #   match.
        'CSV-stdin'         => \$csv_stdin,       # Acquire iptables log data from
                                                  #   stdin.
        'plot-separator'    => \$plot_separator,  # Specify separator character for plot
                                                  #   data (both gnuplot and CSV data).
        'debug'             => \$debug,           # Run in debug mode.
        'debug-sid=i'       => \$debug_sid,       # Debug a specific signature.
        'Dump-conf'         => \$dump_conf,       # Dump config and exit.
        'Interval=i'        => \$chk_interval,    # Set $chk_interval from the
                                                  #   command line.
        'interface=s'       => \$cmdl_interface,  # Specify the IN interface manually
                                                  #   and ignore packets on all others.
        'config=s'          => \$config_file,     # Specify path to configuration file.
        'Override-config=s' => \$override_config_str,
        'fw-analyze'        => \$fw_analyze,      # Analyze the firewall ruleset and
                                                  #   exit.
        'fw-file=s'         => \$fw_file,         # Analyze ruleset contained within
                                                  #   $fw_file instead of a running
                                                  #   policy.
        'fw-list-auto'      => \$fw_list_auto,    # Display iptables chains used by
                                                  #   psad in auto blocking code.
        'List'              => \$fw_list_auto,    # Synonym for --fw-list-auto
        'fw-block-ip=s'     => \$fw_block_ip,     # Add an IP/net to the psad auto-
                                                  #   blocking chains.  Then psad can
                                                  #   manage timeouts, etc.
        'fw-rm-block-ip=s'  => \$fw_rm_block_ip,  # Delete any block rule against an IP
        'fw-del-chains'     => \$fw_del_chains,   # Delete psad chains in addition to
                                                  #   flushing them (requires --F as
                                                  #   well).
        'X'                 => \$fw_del_chains,   # Synonym for --fw-del-chains.
        'fw-dump'           => \$dump_ipt_policy, # Dump the iptables policy
                                                  #   (requires -D as well).
        'fw-include-ips'    => \$fw_include_ips,  # Include all IPs/nets in iptables
                                                  # dump (--fw-dump) output.
        'log-server'        => \$syslog_server,   # We are running psad on a syslog
                                                  #   logging server.
        'Kill'              => \$kill,            # Kill all running psad processes.
                                                  #   (psadwatchd, psad, kmsgsd)
        'Restart'           => \$restart,         # Restart psad with all options of
                                                  #   the currently running psad
                                                  #   process.
        'Flush'             => \$flush_fw,        # Flush any rules that psad previously
                                                  #   added via the auto blocking code.
        'Status'            => \$status_mode,     # Display status of any currently
                                                  #   running psad processes.
        'status-ip=s'       => \$status_ip,       # Display status for a specific IP.
        'status-dl=i'       => \$status_min_dl,   # Display status for scans that have
                                                  #   reached at least this danger
                                                  #   level.
        'status-summary'    => \$status_summary,  # Only display status summary info.
        'restrict-ip=s'     => \$restrict_ip,     # Only process packets that have
                                                  #   either this IP as the src or dst.
        'Benchmark'         => \$benchmark,       # Run in benchmark mode.
        'packets=i'         => \$num_packets,     # Specify number of packets to use
                                                  #   in benchmark test or in -A mode.
        'USR1'              => \$usr1,            # Send an existing psad process a
                                                  # USR1 signal (useful for debugging).
        'HUP'               => \$hup,             # Send psad processes a HUP signal
                                                  #   to re-import config.
        'lib-dir=s'         => \$lib_dir,         # Specify path to psad lib directory.
        'no-snort-sids'     => \$no_snort_sids,   # Disable search for snort SID's
                                                  #   in iptables messages.
        'no-whois'          => \$no_whois,        # Do not issue whois lookups against
        'no-passiveos'      => \$no_posf,         # Do not attempt to passively
        'no-passive-os'     => \$no_posf,         # Do not attempt to passively
                                                  #   fingerprint the remote OS.
        'no-signatures'     => \$no_signatures,   # Disable signature processing.
        'no-icmp-types'     => \$no_icmp_types,   # Disable icmp type/code validation.
        'no-icmp6-types'    => \$no_icmp6_types,  # Disable icmp6 type/code validation.
        'no-auto-dl'        => \$no_auto_dl,      # Disable auto danger level
                                                  #   assignment.
        'no-daemon'         => \$no_daemon,       # Do not run as a daemon.
        'no-fwcheck'        => \$no_fwcheck,      # Do not check firewall rules.
        'no-rdns'           => \$no_rdns,         # Do not issue dns lookups against
                                                  #   scanning IP address.
        'no-netstat'        => \$no_netstat,      # Do not check to see if the
                                                  #   firewall is listening on
                                                  #   localport that has been scanned.
        'no-ipt-errors'     => \$no_ipt_errors,   # Do not write malformed packet.
                                                  #   messages to error log.
        'no-kmsgsd'         => \$no_kmsgsd,       # Do not start kmsgsd (used for
                                                  #   debugging).
        'test-mode'         => \$test_mode,       # Enable test mode (used by the
                                                  #   test suite).
        'verbose'           => \$verbose,         # Verbose output (for both alerts
                                                  #   and debug info).
        'Version'           => \$print_ver,       # Print the psad version and exit.
        'help'              => \$help,            # Display help.
    ));
    &usage(0) if $help;

    ### Print the version number and exit if -V given on the command line.
    if ($print_ver) {
        print "[+] psad v$version by Michael Rash <mbr\@cipherdyne.org>\n";
        exit 0;
    }
    return;
}

sub required_vars() {
    my @required_vars = (qw(
        EMAIL_ADDRESSES CHECK_INTERVAL FW_DATA_FILE FW_ERROR_LOG
        HOME_NET SNORT_SID_STR ENABLE_AUTO_IDS IGNORE_CONNTRACK_BUG_PKTS
        SCAN_TIMEOUT DANGER_LEVEL1 DANGER_LEVEL2 DANGER_LEVEL3
        DANGER_LEVEL4 DANGER_LEVEL5 PORT_RANGE_SCAN_THRESHOLD ALERT_ALL
        EMAIL_LIMIT IPTABLES_BLOCK_METHOD TCPWRAPPERS_BLOCK_METHOD
        EMAIL_ALERT_DANGER_LEVEL PSAD_FIFO_FILE WHOIS_LOOKUP_THRESHOLD
        DNS_LOOKUP_THRESHOLD WHOIS_TIMEOUT SNORT_RULES_DIR HOSTNAME
        PACKET_COUNTER_FILE DSHIELD_COUNTER_FILE SCAN_DATA_ARCHIVE_DIR
        ENABLE_PERSISTENCE AUTO_BLOCK_IPT_FILE AUTO_BLOCK_TCPWR_FILE
        SIGS_FILE AUTO_DL_FILE AUTO_BLOCK_TIMEOUT EXTERNAL_SCRIPT
        ENABLE_EXT_SCRIPT_EXEC EXEC_EXT_SCRIPT_PER_ALERT
        ENABLE_DSHIELD_ALERTS SYSLOG_DAEMON DSHIELD_ALERT_INTERVAL
        DSHIELD_ALERT_EMAIL DSHIELD_USER_ID DSHIELD_USER_EMAIL
        DSHIELD_DL_THRESHOLD DISK_CHECK_INTERVAL DISK_MAX_PERCENTAGE
        DISK_MAX_RM_RETRIES ETC_HOSTS_DENY_FILE ETC_SYSLOG_CONF
        ETC_SYSLOGNG_CONF MIN_ARCHIVE_DANGER_LEVEL ANALYSIS_MODE_DIR
        IMPORT_OLD_SCANS ICMP_TYPES_FILE SHOW_ALL_SIGNATURES
        IPT_PREFIX_COUNTER_FILE IGNORE_PORTS ENABLE_SCAN_ARCHIVE
        EMAIL_LIMIT_STATUS_MSG P0F_FILE IGNORE_PROTOCOLS IPT_AUTO_CHAIN1
        AUTO_IPT_SOCK IGNORE_INTERFACES ALERTING_METHODS
        ULOG_DATA_FILE MAIL_ALERT_PREFIX MAIL_STATUS_PREFIX
        MAIL_ERROR_PREFIX MAIL_FATAL_PREFIX ENABLE_AUTO_IDS_EMAILS
        FLUSH_IPT_AT_INIT ENABLE_MAC_ADDR_REPORTING TRUNCATE_FWDATA
        PSAD_DIR PSAD_RUN_DIR PSAD_FIFO_DIR ENABLE_FW_LOGGING_CHECK
        ENABLE_RENEW_BLOCK_EMAILS DSHIELD_EMAIL_FILE AUTO_BLOCK_REGEX
        ENABLE_AUTO_IDS_REGEX IPTABLES_PREREQ_CHECK SNORT_RULE_DL_FILE
        IPT_OUTPUT_PATTERN IPT_ERROR_PATTERN PROC_FORWARD_FILE PSAD_CONF_DIR
        EXTERNAL_NET HTTP_SERVERS SMTP_SERVERS DNS_SERVERS SQL_SERVERS
        TELNET_SERVERS AIM_SERVERS HTTP_PORTS SHELLCODE_PORTS
        ORACLE_PORTS ENABLE_INTF_LOCAL_NETS ENABLE_SNORT_SIG_STRICT
        IP_OPTS_FILE SIG_UPDATE_URL CONF_ARCHIVE_DIR TOP_SIGS_FILE
        TOP_PORTS_LOG_THRESHOLD TOP_SIGS_LOG_THRESHOLD
        TOP_SCANNED_PORTS_FILE STATUS_OUTPUT_FILE TOP_ATTACKERS_FILE
        STATUS_PORTS_THRESHOLD STATUS_SIGS_THRESHOLD STATUS_IP_THRESHOLD
        ANALYSIS_OUTPUT_FILE TOP_IP_LOG_THRESHOLD PSAD_LIBS_DIR
        PSAD_PID_FILE PSAD_CMDLINE_FILE PSAD_ERR_DIR MIN_DANGER_LEVEL
        IGNORE_KERNEL_TIMESTAMP ENABLE_SIG_MSG_SYSLOG
        SIG_MSG_SYSLOG_THRESHOLD SIG_SID_SYSLOG_THRESHOLD
        PSADWATCHD_CHECK_INTERVAL PSADWATCHD_MAX_RETRIES SYSLOG_IDENTITY
        SYSLOG_FACILITY SYSLOG_PRIORITY ENABLE_EMAIL_LIMIT_PER_DST
        ENABLE_SYSLOG_FILE IPT_SYSLOG_FILE IPT_WRITE_FWDATA
        ETC_RSYSLOG_CONF IFCFGTYPE ENABLE_WHOIS_FORCE_ASCII
        ENABLE_WHOIS_FORCE_SRC_IP ENABLE_IPV6_DETECTION
        PERSISTENCE_CTR_THRESHOLD MAX_SCAN_IP_PAIRS INSTALL_ROOT
        ICMP6_TYPES_FILE PROTOCOL_SCAN_THRESHOLD PROTOCOLS_FILE
        AUTO_BLOCK_DL1_TIMEOUT AUTO_BLOCK_DL2_TIMEOUT
        AUTO_BLOCK_DL3_TIMEOUT AUTO_BLOCK_DL4_TIMEOUT
        AUTO_BLOCK_DL5_TIMEOUT EMAIL_THROTTLE EXPECT_TCP_OPTIONS
        ENABLE_PSADWATCHD ENABLE_EXT_BLOCK_SCRIPT_EXEC EXTERNAL_BLOCK_SCRIPT
        ENABLE_FW_MSG_READ_CMD FW_MSG_READ_CMD FW_MSG_READ_CMD_ARGS
        USE_FW_MSG_READ_CMD_ARGS CUSTOM_SYSLOG_TS_RE
        ENABLE_CUSTOM_SYSLOG_TS_RE FW_MSG_READ_MIN_PKTS ENABLE_DNS_LOOKUPS
        ENABLE_WHOIS_LOOKUPS PSAD_FW_READ_PID_FILE
    ));
    &defined_vars(\@required_vars);
    return;
}

sub usage() {
    my $exitcode = shift;
    print <<_HELP_;

psad: the Port Scan Attack Detector

[+] Version: $version
    By Michael Rash (mbr\@cipherdyne.org)
    URL: http://www.cipherdyne.org/psad/

Usage: psad [options]

Options:

    -A,  --Analyze-msgs           - Analyze iptables logfile and exit.
         --stdin                  - In --Analyze-msgs mode, acquire iptables
                                    log data from STDIN.
    -e,  --email-analysis         - Send emails for scans detected in
                                    offline analysis mode.
    -m,  --messages-file <file>   - Specify the path to the iptables logfile
                                    (for --Analyze-msgs mode).
    -i,  --interface <intf>       - Restrict detection to IN interface (for
                                    INPUT and FORWARD chains) or OUT
                                    interface (for OUPUT chain).
    --sig-update                  - Download the latest set of psad
                                    signatures from:
                                    http://www.cipherdyne.org/
    -w,  --whois-analysis         - Enable whois lookups when running in
                                    offline --Analyze-msgs mode.
    --dns-analysis                - Enable reverse DNS lookups in
                                    --Analyze-msgs mode.
    --fw-analyze                  - Analyze the local iptables ruleset and
                                    exit.
    --fw-list-auto                - List the contents of any iptables chains
                                    (for auto-blocking rules).
    --List                        - Synonym for --fw-list-auto (emulates
                                    iptables command line).
    --fw-block-ip  <ip>           - Add an IP/network to the auto-blocking
                                    chains.
    --fw-rm-block-ip  <ip>        - Remove an IP/network from the auto-
                                    blocking chains.
    --fw-file <rules file>        - Analyze the iptables ruleset contained
                                    within <rules file> instead of a running
                                    policy.
    --fw-del-chains               - Delete iptables chains used by psad for
                                    auto-blocking rules.
    -X                            - Synonym for --fw-del-chains (emulates
                                    iptables command line).
    --fw-dump                     - Dump a sanitized version of the local
                                    iptables policy.
    --fw-include-ips              - Include all IPs/nets in iptables dump
                                    (--fw-dump) output.
    --snort-rdir <rule dir>        - Path to snort rules directory.
    --debug,                      - Run psad in debugging mode.
    --debug-sid <sid>             - Debug a specific Snort rule.
    -D,  --Dump-conf              - Dump psad configuration on STDOUT and
                                    exit.
    -l,  --log-server             - psad is being run on a syslog logging
                                    server.
    -F,  --Flush                  - Remove any auto-generated firewall
                                    block rules (emulates iptables command
                                    line).
    -K,  --Kill                   - Kill all running psad processes.
    -R,  --Restart                - Restart all running psad processes.
    -S,  --Status                 - Displays the status of any currently
                                    running psad processes.
    --restrict-ip <IP or net>     - Only process packets that have this IP
                                    or network as the src or dst.
    --status-ip <IP address>      - View status for a specific IP.
    --status-dl <dl>              - Display status information for only
                                    those scans that have reach at least
                                    <dl> (from 1 to 5).
    --status-summary              - Only display summary status output in
                                    --Status and --Analyze modes.
    -B,  --Benchmark              - Run psad in benchmark mode.
    --packets <number>            - Specify number of packets to use in
                                    --Analyze (default is unlimited) or
                                    --Benchmark (default is 10,000) modes.
    -U,  --USR1                   - Send a running psad process a USR1
                                    signal (generates a dump of psad
                                    data structures on STDOUT).
    -H,  --HUP                    - Send all psad daemons a HUP signal to
                                    have them re-import configs.
    --get-next-rule-id            - Display the next available rule ID and
                                    exit.

    --gnuplot                     - Parse iptables log data and produce a
                                    file suitable for plotting with Gnuplot.
    --gnuplot-graph-style         - Set the Gnuplot graph style (e.g.
                                    "dots", "lines", "linespoints", etc.).
    --gnuplot-file-prefix         - Use a prefix for the .gnu, .dat, and
                                    .png files that are generated in Gnuplot
                                    mode.
    --gnuplot-interactive         - Do not add the terminal directive to the
                                    Gnuplot .gnu file, so when Gnuplot loads
                                    the file it will graph the data in an
                                    interactive window.
    --gnuplot-title               - Set the Gnuplot graph title.
    --gnuplot-legend-title        - Set the Gnuplot legend title.
    --gnuplot-x-label             - Set the x-axis label.
    --gnuplot-x-range             - Set the x-axis range.
    --gnuplot-y-label             - Set the y-axis label.
    --gnuplot-y-range             - Set the y-axis range.
    --gnuplot-z-label             - Set the z-axis label.
    --gnuplot-z-range             - Set the z-axis range.
    --gnuplot-sort-style          - Set the psad sorting style to either
                                    "time" or "value" (defaults to "value").
    --gnuplot-3D                  - Create three-dimensional Gnuplot graph.
    --gnuplot-view                - Set the viewing angle.
    --gnuplot-grayscale           - Only use grayscale colors.
    --gnuplot-template <file>     - Use a template file for all Gnuplot
                                    directives.
    --gnuplot-dat-file            - Specify path to .dat output file.
    --gnuplot-plot-file           - Specify path to .gnu output file.
    --gnuplot-png-file            - Specify path to .png output file.

    --CSV                         - Parse iptables log messages and dump
                                    fields to stdout in csv format.
    --CSV-fields <fields>         - Restrict --CSV output to a list of
                                    specfic fields.
    --CSV-unique-lines            - Only print unique lines in CSV output.
    --CSV-max-lines <num>         - Specify the maximum number of CSV output
                                    lines to print.
    --CSV-start-line <line>       - Starting line within iptables log file.
    --CSV-end-line <line>         - Ending line within iptables log file.
    --CSV-regex <regex>           - Require iptables log messages to match
                                    an additional regex in --CSV mode.
    --CSV-neg-regex <regex>       - Require iptables log messages to
                                    not match an additional regex in --CSV
                                    mode.
    --plot-separator <str>        - Specify a separator string between plot
                                    fields (in --gnuplot or --CSV plot
                                    modes (the default is a comma for CSV
                                    formatted output).

    --signatures <sigs file>      - Manually specify the path to the psad
                                    signatures file.
    --snort-type                  - Restrict psad to look for specific Snort
                                    sids such as those in ddos.rules or
                                    backdoor.rules.
    --passive-os-sigs <sigs file> - Manually specify the path to the passive
                                    os fingerprinting sigs.
    --auto-dl <dl file>           - Import auto-danger level file for
                                    automatic IP danger level increases or
                                    decreses.
    --analysis-write-data         - Write data to filesystem from -A mode
                                    (the disk IO involved in this step can
                                    take a long time).
    -c,  --config <config file>   - Use <config file> instead of the normal
                                    config file located at
                                    $config_file.
    -O, --Override-config <str>   - Allow config variables from the normal
                                    $config_file to be superseded with values
                                    from the specified file(s).
    -I,  --Interval <seconds>     - Configure the check interval from the
                                    command line to override the 5 second
                                    default.
    -v,  --verbose                - Run in verbose mode.
    -V,  --Version                - Print the psad version and exit.

    --no-snort-sids               - Disable examination for snort sids
                                    (such as those generated by fwsnort) in
                                    iptables log messages.
    --no-signatures               - Disable psad signature processing
                                    (independent of snort sid matching).
    --no-icmp-types               - Disable icmp type/code validation.
    --no-auto-dl                  - Disable auto danger level assignment.
    --no-daemon                   - Do not run as a daemon.
    --no-ipt-errors               - Do not write errors to the error log.
    --no-whois                    - Disable whois lookups.
    --no-fwcheck                  - Disable firewall rules verification.
    --no-rdns                     - Disable name resolution against scanning
                                    IP addresses.
    --no-kmsgsd                   - Disable startup of kmsgsd (useful for
                                    debugging with an existing file of
                                    iptables log messages).
    --no-netstat                  - Disable local port lookups for scan
                                    signatures.
    -h   --help                   - Display usage on STDOUT and exit.

_HELP_
    exit $exitcode;
}