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
|
/* Copyright (C) 2007-2019 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* 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
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \file
*
* \author Victor Julien <victor@inliniac.net>
*
* Ports part of the detection engine.
*
* \todo more unit testing
*
*/
#include "suricata-common.h"
#include "decode.h"
#include "detect.h"
#include "flow-var.h"
#include "util-cidr.h"
#include "util-unittest.h"
#include "util-unittest-helper.h"
#include "util-rule-vars.h"
#include "detect-parse.h"
#include "detect-engine.h"
#include "detect-engine-mpm.h"
#include "detect-engine-siggroup.h"
#include "detect-engine-port.h"
#include "conf.h"
#include "util-debug.h"
#include "util-error.h"
#include "pkt-var.h"
#include "host.h"
#include "util-profiling.h"
#include "util-var.h"
#include "util-byte.h"
static int DetectPortCutNot(DetectPort *, DetectPort **);
static int DetectPortCut(DetectEngineCtx *, DetectPort *, DetectPort *,
DetectPort **);
DetectPort *PortParse(const char *str);
static bool DetectPortIsValidRange(char *, uint16_t *);
/**
* \brief Alloc a DetectPort structure and update counters
*
* \retval dp newly created DetectPort on success; or NULL in case of error.
*/
static DetectPort *DetectPortInit(void)
{
DetectPort *dp = SCCalloc(1, sizeof(DetectPort));
if (unlikely(dp == NULL))
return NULL;
return dp;
}
/**
* \brief Free a DetectPort and its members
*
* \param dp Pointer to the DetectPort that has to be freed.
*/
void DetectPortFree(const DetectEngineCtx *de_ctx, DetectPort *dp)
{
if (dp == NULL)
return;
/* only free the head if we have the original */
if (dp->sh != NULL && !(dp->flags & PORT_SIGGROUPHEAD_COPY)) {
SigGroupHeadFree(de_ctx, dp->sh);
}
dp->sh = NULL;
SCFree(dp);
}
/**
* \brief Helper function used to print the list of ports
* present in this DetectPort list.
*
* \param head Pointer to the DetectPort list head
*/
void DetectPortPrintList(DetectPort *head)
{
DetectPort *cur;
#ifdef DEBUG
uint16_t cnt = 0;
#endif
SCLogDebug("= list start:");
if (head != NULL) {
for (cur = head; cur != NULL; cur = cur->next) {
DetectPortPrint(cur);
#ifdef DEBUG
cnt++;
#endif
}
SCLogDebug(" ");
}
SCLogDebug("= list end (cnt %" PRIu32 ")", cnt);
}
/**
* \brief Free a DetectPort list and each of its members
*
* \param head Pointer to the DetectPort list head
*/
void DetectPortCleanupList (const DetectEngineCtx *de_ctx, DetectPort *head)
{
if (head == NULL)
return;
DetectPort *cur, *next;
for (cur = head; cur != NULL; ) {
next = cur->next;
cur->next = NULL;
DetectPortFree(de_ctx, cur);
cur = next;
}
}
/**
* \brief function for inserting a port group object. This also makes sure
* SigGroupContainer lists are handled correctly.
*
* \param de_ctx Pointer to the current detection engine context
* \param head Pointer to the DetectPort list head
* \param dp DetectPort to search in the DetectPort list
*
* \retval 1 inserted
* \retval 0 not inserted, memory of new is freed
* \retval -1 error
*
* \todo rewrite to avoid recursive calls
* */
int DetectPortInsert(DetectEngineCtx *de_ctx, DetectPort **head,
DetectPort *new)
{
if (new == NULL)
return 0;
//BUG_ON(new->next != NULL);
//BUG_ON(new->prev != NULL);
/* see if it already exists or overlaps with existing ports */
if (*head != NULL) {
DetectPort *cur = NULL;
int r = 0;
for (cur = *head; cur != NULL; cur = cur->next) {
r = DetectPortCmp(new,cur);
BUG_ON(r == PORT_ER);
/* if so, handle that */
if (r == PORT_EQ) {
SCLogDebug("PORT_EQ %p %p", cur, new);
/* exact overlap/match */
if (cur != new) {
SigGroupHeadCopySigs(de_ctx, new->sh, &cur->sh);
DetectPortFree(de_ctx, new);
return 0;
}
return 1;
} else if (r == PORT_GT) {
SCLogDebug("PORT_GT (cur->next %p)", cur->next);
/* only add it now if we are bigger than the last
* group. Otherwise we'll handle it later. */
if (cur->next == NULL) {
SCLogDebug("adding GT");
/* put in the list */
new->prev = cur;
cur->next = new;
return 1;
}
} else if (r == PORT_LT) {
SCLogDebug("PORT_LT");
/* see if we need to insert the ag anywhere */
/* put in the list */
if (cur->prev != NULL)
cur->prev->next = new;
new->prev = cur->prev;
new->next = cur;
cur->prev = new;
/* update head if required */
if (*head == cur) {
*head = new;
}
return 1;
/* alright, those were the simple cases,
* lets handle the more complex ones now */
} else {
DetectPort *c = NULL;
r = DetectPortCut(de_ctx, cur, new, &c);
if (r == -1)
goto error;
r = DetectPortInsert(de_ctx, head, new);
if (r == -1) {
if (c != NULL) {
DetectPortFree(de_ctx, c);
}
goto error;
}
if (c != NULL) {
SCLogDebug("inserting C (%p)", c);
if (SCLogDebugEnabled()) {
DetectPortPrint(c);
}
r = DetectPortInsert(de_ctx, head, c);
if (r == -1)
goto error;
}
return 1;
}
}
/* head is NULL, so get a group and set head to it */
} else {
SCLogDebug("setting new head %p", new);
*head = new;
}
return 1;
error:
/* XXX */
return -1;
}
/**
* \brief Function that cuts port groups and merge them
*
* \param de_ctx Pointer to the current detection engine context
* \param a pointer to DetectPort "a"
* \param b pointer to DetectPort "b"
* \param c pointer to DetectPort "c"
*
* \retval 0 ok
* \retval -1 error
* */
static int DetectPortCut(DetectEngineCtx *de_ctx, DetectPort *a,
DetectPort *b, DetectPort **c)
{
uint16_t a_port1 = a->port;
uint16_t a_port2 = a->port2;
uint16_t b_port1 = b->port;
uint16_t b_port2 = b->port2;
/* default to NULL */
*c = NULL;
int r = DetectPortCmp(a,b);
BUG_ON(r != PORT_ES && r != PORT_EB && r != PORT_LE && r != PORT_GE);
/* get a place to temporary put sigs lists */
DetectPort *tmp = DetectPortInit();
if (tmp == NULL) {
goto error;
}
/**
* We have 3 parts: [aaa[abab]bbb]
* part a: a_port1 <-> b_port1 - 1
* part b: b_port1 <-> a_port2
* part c: a_port2 + 1 <-> b_port2
*/
if (r == PORT_LE) {
SCLogDebug("cut r == PORT_LE");
a->port = a_port1;
a->port2 = b_port1 - 1;
b->port = b_port1;
b->port2 = a_port2;
DetectPort *tmp_c = DetectPortInit();
if (tmp_c == NULL) {
goto error;
}
*c = tmp_c;
tmp_c->port = a_port2 + 1;
tmp_c->port2 = b_port2;
SigGroupHeadCopySigs(de_ctx,b->sh,&tmp_c->sh); /* copy old b to c */
SigGroupHeadCopySigs(de_ctx,a->sh,&b->sh); /* copy a to b */
/**
* We have 3 parts: [bbb[baba]aaa]
* part a: b_port1 <-> a_port1 - 1
* part b: a_port1 <-> b_port2
* part c: b_port2 + 1 <-> a_port2
*/
} else if (r == PORT_GE) {
SCLogDebug("cut r == PORT_GE");
a->port = b_port1;
a->port2 = a_port1 - 1;
b->port = a_port1;
b->port2 = b_port2;
DetectPort *tmp_c = DetectPortInit();
if (tmp_c == NULL) {
goto error;
}
*c = tmp_c;
tmp_c->port = b_port2 + 1;
tmp_c->port2 = a_port2;
/**
* 'a' gets clean and then 'b' sigs
* 'b' gets clean, then 'a' then 'b' sigs
* 'c' gets 'a' sigs
*/
SigGroupHeadCopySigs(de_ctx,a->sh,&tmp->sh); /* store old a list */
SigGroupHeadClearSigs(a->sh); /* clean a list */
SigGroupHeadCopySigs(de_ctx,tmp->sh,&tmp_c->sh); /* copy old b to c */
SigGroupHeadCopySigs(de_ctx,b->sh,&a->sh); /* copy old b to a */
SigGroupHeadCopySigs(de_ctx,tmp->sh,&b->sh);/* prepend old a before b */
SigGroupHeadClearSigs(tmp->sh); /* clean tmp list */
/**
* We have 2 or three parts:
*
* 2 part: [[abab]bbb] or [bbb[baba]]
* part a: a_port1 <-> a_port2
* part b: a_port2 + 1 <-> b_port2
*
* part a: b_port1 <-> a_port1 - 1
* part b: a_port1 <-> a_port2
*
* 3 part [bbb[aaa]bbb]
* becomes[aaa[bbb]ccc]
*
* part a: b_port1 <-> a_port1 - 1
* part b: a_port1 <-> a_port2
* part c: a_port2 + 1 <-> b_port2
*/
} else if (r == PORT_ES) {
SCLogDebug("cut r == PORT_ES");
if (a_port1 == b_port1) {
SCLogDebug("1");
a->port = a_port1;
a->port2 = a_port2;
b->port = a_port2 + 1;
b->port2 = b_port2;
/** 'b' overlaps 'a' so 'a' needs the 'b' sigs */
SigGroupHeadCopySigs(de_ctx,b->sh,&a->sh);
} else if (a_port2 == b_port2) {
SCLogDebug("2");
a->port = b_port1;
a->port2 = a_port1 - 1;
b->port = a_port1;
b->port2 = a_port2;
/* [bbb[baba]] will be transformed into
* [aaa][bbb]
* steps: copy b sigs to tmp
* a overlaps b, so copy a to b
* clear a
* copy tmp to a */
SigGroupHeadCopySigs(de_ctx,b->sh,&tmp->sh); /* store old a list */
SigGroupHeadCopySigs(de_ctx,a->sh,&b->sh);
SigGroupHeadClearSigs(a->sh); /* clean a list */
SigGroupHeadCopySigs(de_ctx,tmp->sh,&a->sh);/* merge old a with b */
SigGroupHeadClearSigs(tmp->sh); /* clean tmp list */
} else {
SCLogDebug("3");
a->port = b_port1;
a->port2 = a_port1 - 1;
b->port = a_port1;
b->port2 = a_port2;
DetectPort *tmp_c = DetectPortInit();
if (tmp_c == NULL) {
goto error;
}
*c = tmp_c;
tmp_c->port = a_port2 + 1;
tmp_c->port2 = b_port2;
/**
* 'a' gets clean and then 'b' sigs
* 'b' gets clean, then 'a' then 'b' sigs
* 'c' gets 'b' sigs
*/
SigGroupHeadCopySigs(de_ctx,a->sh,&tmp->sh); /* store old a list */
SigGroupHeadClearSigs(a->sh); /* clean a list */
SigGroupHeadCopySigs(de_ctx,b->sh,&tmp_c->sh); /* copy old b to c */
SigGroupHeadCopySigs(de_ctx,b->sh,&a->sh); /* copy old b to a */
SigGroupHeadCopySigs(de_ctx,tmp->sh,&b->sh);/* merge old a with b */
SigGroupHeadClearSigs(tmp->sh); /* clean tmp list */
}
/**
* We have 2 or three parts:
*
* 2 part: [[baba]aaa] or [aaa[abab]]
* part a: b_port1 <-> b_port2
* part b: b_port2 + 1 <-> a_port2
*
* part a: a_port1 <-> b_port1 - 1
* part b: b_port1 <-> b_port2
*
* 3 part [aaa[bbb]aaa]
* becomes[aaa[bbb]ccc]
*
* part a: a_port1 <-> b_port2 - 1
* part b: b_port1 <-> b_port2
* part c: b_port2 + 1 <-> a_port2
*/
} else if (r == PORT_EB) {
SCLogDebug("cut r == PORT_EB");
if (a_port1 == b_port1) {
SCLogDebug("1");
a->port = b_port1;
a->port2 = b_port2;
b->port = b_port2 + 1;
b->port2 = a_port2;
/** 'b' overlaps 'a' so 'a' needs the 'b' sigs */
SigGroupHeadCopySigs(de_ctx,b->sh,&tmp->sh);
SigGroupHeadClearSigs(b->sh);
SigGroupHeadCopySigs(de_ctx,a->sh,&b->sh);
SigGroupHeadCopySigs(de_ctx,tmp->sh,&a->sh);
SigGroupHeadClearSigs(tmp->sh);
} else if (a_port2 == b_port2) {
SCLogDebug("2");
a->port = a_port1;
a->port2 = b_port1 - 1;
b->port = b_port1;
b->port2 = b_port2;
/** 'a' overlaps 'b' so 'b' needs the 'a' sigs */
SigGroupHeadCopySigs(de_ctx,a->sh,&b->sh);
} else {
SCLogDebug("3");
a->port = a_port1;
a->port2 = b_port1 - 1;
b->port = b_port1;
b->port2 = b_port2;
DetectPort *tmp_c = DetectPortInit();
if (tmp_c == NULL) {
goto error;
}
*c = tmp_c;
tmp_c->port = b_port2 + 1;
tmp_c->port2 = a_port2;
SigGroupHeadCopySigs(de_ctx,a->sh,&b->sh);
SigGroupHeadCopySigs(de_ctx,a->sh,&tmp_c->sh);
}
}
if (tmp != NULL) {
DetectPortFree(de_ctx, tmp);
}
return 0;
error:
if (tmp != NULL)
DetectPortFree(de_ctx, tmp);
return -1;
}
/**
* \brief Function that cuts port groups implementing group negation
*
* \param a pointer to DetectPort "a"
* \param b pointer to DetectPort "b"
*
* \retval 0 ok
* \retval -1 error
* */
static int DetectPortCutNot(DetectPort *a, DetectPort **b)
{
uint16_t a_port1 = a->port;
uint16_t a_port2 = a->port2;
/* default to NULL */
*b = NULL;
if (a_port1 != 0x0000 && a_port2 != 0xFFFF) {
a->port = 0x0000;
a->port2 = a_port1 - 1;
DetectPort *tmp_b;
tmp_b = DetectPortInit();
if (tmp_b == NULL) {
return -1;
}
tmp_b->port = a_port2 + 1;
tmp_b->port2 = 0xFFFF;
*b = tmp_b;
} else if (a_port1 == 0x0000 && a_port2 != 0xFFFF) {
a->port = a_port2 + 1;
a->port2 = 0xFFFF;
} else if (a_port1 != 0x0000 && a_port2 == 0xFFFF) {
a->port = 0x0000;
a->port2 = a_port1 - 1;
} else {
return -1;
}
return 0;
}
/**
* \brief Function that compare port groups
*
* \param a pointer to DetectPort "a"
* \param b pointer to DetectPort "b"
*
* \retval PORT_XX (Port enum value, XX is EQ, ES, EB, LE, etc)
* \retval PORT_ER on error
* */
int DetectPortCmp(DetectPort *a, DetectPort *b)
{
/* check any */
if ((a->flags & PORT_FLAG_ANY) && (b->flags & PORT_FLAG_ANY))
return PORT_EQ;
if ((a->flags & PORT_FLAG_ANY) && !(b->flags & PORT_FLAG_ANY))
return PORT_LT;
if (!(a->flags & PORT_FLAG_ANY) && (b->flags & PORT_FLAG_ANY))
return PORT_GT;
uint16_t a_port1 = a->port;
uint16_t a_port2 = a->port2;
uint16_t b_port1 = b->port;
uint16_t b_port2 = b->port2;
/* PORT_EQ */
if (a_port1 == b_port1 && a_port2 == b_port2) {
//SCLogDebug("PORT_EQ");
return PORT_EQ;
/* PORT_ES */
} else if (a_port1 >= b_port1 && a_port1 <= b_port2 && a_port2 <= b_port2) {
//SCLogDebug("PORT_ES");
return PORT_ES;
/* PORT_EB */
} else if (a_port1 <= b_port1 && a_port2 >= b_port2) {
//SCLogDebug("PORT_EB");
return PORT_EB;
} else if (a_port1 < b_port1 && a_port2 < b_port2 && a_port2 >= b_port1) {
//SCLogDebug("PORT_LE");
return PORT_LE;
} else if (a_port1 < b_port1 && a_port2 < b_port2) {
//SCLogDebug("PORT_LT");
return PORT_LT;
} else if (a_port1 > b_port1 && a_port1 <= b_port2 && a_port2 > b_port2) {
//SCLogDebug("PORT_GE");
return PORT_GE;
} else if (a_port1 > b_port2) {
//SCLogDebug("PORT_GT");
return PORT_GT;
} else {
/* should be unreachable */
BUG_ON(1);
}
return PORT_ER;
}
/**
* \brief Function that return a copy of DetectPort src sigs
*
* \param de_ctx Pointer to the current Detection Engine Context
* \param src Pointer to a DetectPort group to copy
*
* \retval Pointer to a DetectPort instance (copy of src)
* \retval NULL on error
* */
DetectPort *DetectPortCopySingle(DetectEngineCtx *de_ctx,DetectPort *src)
{
if (src == NULL)
return NULL;
DetectPort *dst = DetectPortInit();
if (dst == NULL) {
return NULL;
}
dst->port = src->port;
dst->port2 = src->port2;
SigGroupHeadCopySigs(de_ctx,src->sh,&dst->sh);
return dst;
}
/**
* \brief Function Match to Match a port against a DetectPort group
*
* \param dp Pointer to DetectPort group where we try to match the port
* \param port To compare/match
*
* \retval 1 if port is in the range (it match)
* \retval 0 if port is not in the range
* */
static int DetectPortMatch(DetectPort *dp, uint16_t port)
{
if (port >= dp->port &&
port <= dp->port2) {
return 1;
}
return 0;
}
/**
* \brief Helper function that print the DetectPort info
* \retval none
*/
void DetectPortPrint(DetectPort *dp)
{
if (dp == NULL)
return;
if (dp->flags & PORT_FLAG_ANY) {
SCLogDebug("=> port %p: ANY", dp);
// printf("ANY");
} else {
SCLogDebug("=> port %p %" PRIu32 "-%" PRIu32 "", dp, dp->port, dp->port2);
// printf("%" PRIu32 "-%" PRIu32 "", dp->port, dp->port2);
}
}
/**
* \brief Function that find the group matching address in a group head
*
* \param dp Pointer to DetectPort group where we try to find the group
* \param port port to search/lookup
*
* \retval Pointer to the DetectPort group of our port if it matched
* \retval NULL if port is not in the list
* */
DetectPort *DetectPortLookupGroup(DetectPort *dp, uint16_t port)
{
if (dp == NULL)
return NULL;
for (DetectPort *p = dp; p != NULL; p = p->next) {
if (DetectPortMatch(p, port) == 1) {
//SCLogDebug("match, port %" PRIu32 ", dp ", port);
//DetectPortPrint(p); SCLogDebug("");
return p;
}
}
return NULL;
}
/**
* \brief Checks if two port group lists are equal.
*
* \param list1 Pointer to the first port group list.
* \param list2 Pointer to the second port group list.
*
* \retval true On success.
* \retval false On failure.
*/
bool DetectPortListsAreEqual(DetectPort *list1, DetectPort *list2)
{
DetectPort *item = list1;
DetectPort *it = list2;
// First, compare items one by one.
while (item != NULL && it != NULL) {
if (DetectPortCmp(item, it) != PORT_EQ) {
return false;
}
item = item->next;
it = it->next;
}
// Are the lists of the same size?
if (!(item == NULL && it == NULL)) {
return false;
}
return true;
}
/******************* parsing routines ************************/
/**
* \brief Wrapper function that call the internal/real function
* to insert the new DetectPort
* \param head Pointer to the head of the DetectPort group list
* \param new Pointer to the new DetectPort group list
*
* \retval 1 inserted
* \retval 0 not inserted, memory of new is freed
* \retval -1 error
*/
static int DetectPortParseInsert(DetectPort **head, DetectPort *new)
{
return DetectPortInsert(NULL, head, new);
}
/**
* \brief Function to parse and insert the string in the DetectPort head list
*
* \param head Pointer to the head of the DetectPort group list
* \param s Pointer to the port string
*
* \retval 0 on success
* \retval -1 on error
*/
static int DetectPortParseInsertString(const DetectEngineCtx *de_ctx,
DetectPort **head, const char *s)
{
DetectPort *ad = NULL, *ad_any = NULL;
int r = 0;
bool port_any = false;
SCLogDebug("head %p, *head %p, s %s", head, *head, s);
/** parse the address */
ad = PortParse(s);
if (ad == NULL) {
SCLogError(" failed to parse port \"%s\"", s);
return -1;
}
if (ad->flags & PORT_FLAG_ANY) {
port_any = true;
}
/** handle the not case, we apply the negation then insert the part(s) */
if (ad->flags & PORT_FLAG_NOT) {
DetectPort *ad2 = NULL;
if (DetectPortCutNot(ad, &ad2) < 0) {
goto error;
}
/** normally a 'not' will result in two ad's unless the 'not' is on the
* start or end of the address space(e.g. 0.0.0.0 or 255.255.255.255)
*/
if (ad2 != NULL) {
if (DetectPortParseInsert(head, ad2) < 0) {
if (ad2 != NULL) SCFree(ad2);
goto error;
}
}
}
r = DetectPortParseInsert(head, ad);
if (r < 0)
goto error;
/** if any, insert 0.0.0.0/0 and ::/0 as well */
if (r == 1 && port_any) {
SCLogDebug("inserting 0:65535 as port is \"any\"");
ad_any = PortParse("0:65535");
if (ad_any == NULL)
goto error;
if (DetectPortParseInsert(head, ad_any) < 0)
goto error;
}
return 0;
error:
SCLogError("DetectPortParseInsertString error");
if (ad != NULL)
DetectPortCleanupList(de_ctx, ad);
if (ad_any != NULL)
DetectPortCleanupList(de_ctx, ad_any);
return -1;
}
/**
* \brief Parses a port string and updates the 2 port heads with the
* port groups.
*
* \todo We don't seem to be handling negated cases, like [port,![!port,port]],
* since we pass around negate without keeping a count of ! with depth.
* Can solve this by keeping a count of the negations with depth, so that
* an even no of negations would count as no negation and an odd no of
* negations would count as a negation.
*
* \param gh Pointer to the port group head that should hold port ranges
* that are not negated.
* \param ghn Pointer to the port group head that should hold port ranges
* that are negated.
* \param s Pointer to the character string holding the port to be
* parsed.
* \param negate Flag that indicates if the received address string is negated
* or not. 0 if it is not, 1 it it is.
*
* \retval 0 On successfully parsing.
* \retval -1 On failure.
*/
static int DetectPortParseDo(const DetectEngineCtx *de_ctx,
DetectPort **head, DetectPort **nhead,
const char *s, int negate,
ResolvedVariablesList *var_list, int recur)
{
size_t u = 0;
size_t x = 0;
int o_set = 0, n_set = 0, d_set = 0;
int range = 0;
int depth = 0;
size_t size = strlen(s);
char address[1024] = "";
const char *rule_var_port = NULL;
int r = 0;
if (recur++ > 64) {
SCLogError("port block recursion "
"limit reached (max 64)");
goto error;
}
SCLogDebug("head %p, *head %p, negate %d", head, *head, negate);
for (u = 0, x = 0; u < size && x < sizeof(address); u++) {
address[x] = s[u];
x++;
if (s[u] == ':')
range = 1;
if (range == 1 && s[u] == '!') {
SCLogError("Can't have a negated value in a range.");
return -1;
} else if (!o_set && s[u] == '!') {
SCLogDebug("negation encountered");
n_set = 1;
x--;
} else if (s[u] == '[') {
if (!o_set) {
o_set = 1;
x = 0;
}
depth++;
} else if (s[u] == ']') {
if (depth == 1) {
address[x - 1] = '\0';
SCLogDebug("Parsed port from DetectPortParseDo - %s", address);
x = 0;
r = DetectPortParseDo(de_ctx, head, nhead, address,
negate? negate: n_set, var_list, recur);
if (r == -1)
goto error;
n_set = 0;
}
depth--;
range = 0;
} else if (depth == 0 && s[u] == ',') {
if (o_set == 1) {
o_set = 0;
} else if (d_set == 1) {
char *temp_rule_var_port = NULL,
*alloc_rule_var_port = NULL;
address[x - 1] = '\0';
rule_var_port = SCRuleVarsGetConfVar(de_ctx, address,
SC_RULE_VARS_PORT_GROUPS);
if (rule_var_port == NULL)
goto error;
if (strlen(rule_var_port) == 0) {
SCLogError("variable %s resolved "
"to nothing. This is likely a misconfiguration. "
"Note that a negated port needs to be quoted, "
"\"!$HTTP_PORTS\" instead of !$HTTP_PORTS. See issue #295.",
s);
goto error;
}
if (negate == 1 || n_set == 1) {
alloc_rule_var_port = SCMalloc(strlen(rule_var_port) + 3);
if (unlikely(alloc_rule_var_port == NULL))
goto error;
snprintf(alloc_rule_var_port, strlen(rule_var_port) + 3,
"[%s]", rule_var_port);
} else {
alloc_rule_var_port = SCStrdup(rule_var_port);
if (unlikely(alloc_rule_var_port == NULL))
goto error;
}
temp_rule_var_port = alloc_rule_var_port;
r = DetectPortParseDo(de_ctx, head, nhead, temp_rule_var_port,
(negate + n_set) % 2, var_list, recur);
if (r == -1) {
SCFree(alloc_rule_var_port);
goto error;
}
d_set = 0;
n_set = 0;
SCFree(alloc_rule_var_port);
} else {
address[x - 1] = '\0';
SCLogDebug("Parsed port from DetectPortParseDo - %s", address);
if (negate == 0 && n_set == 0) {
r = DetectPortParseInsertString(de_ctx, head, address);
} else {
r = DetectPortParseInsertString(de_ctx, nhead, address);
}
if (r == -1)
goto error;
n_set = 0;
}
x = 0;
range = 0;
} else if (depth == 0 && s[u] == '$') {
d_set = 1;
} else if (depth == 0 && u == size-1) {
range = 0;
if (x == 1024) {
address[x - 1] = '\0';
} else {
address[x] = '\0';
}
SCLogDebug("%s", address);
if (AddVariableToResolveList(var_list, address) == -1) {
SCLogError("Found a loop in a port "
"groups declaration. This is likely a misconfiguration.");
goto error;
}
x = 0;
if (d_set == 1) {
char *temp_rule_var_port = NULL,
*alloc_rule_var_port = NULL;
rule_var_port = SCRuleVarsGetConfVar(de_ctx, address,
SC_RULE_VARS_PORT_GROUPS);
if (rule_var_port == NULL)
goto error;
if (strlen(rule_var_port) == 0) {
SCLogError("variable %s resolved "
"to nothing. This is likely a misconfiguration. "
"Note that a negated port needs to be quoted, "
"\"!$HTTP_PORTS\" instead of !$HTTP_PORTS. See issue #295.",
s);
goto error;
}
if ((negate + n_set) % 2) {
alloc_rule_var_port = SCMalloc(strlen(rule_var_port) + 3);
if (unlikely(alloc_rule_var_port == NULL))
goto error;
snprintf(alloc_rule_var_port, strlen(rule_var_port) + 3,
"[%s]", rule_var_port);
} else {
alloc_rule_var_port = SCStrdup(rule_var_port);
if (unlikely(alloc_rule_var_port == NULL))
goto error;
}
temp_rule_var_port = alloc_rule_var_port;
r = DetectPortParseDo(de_ctx, head, nhead, temp_rule_var_port,
(negate + n_set) % 2, var_list, recur);
SCFree(alloc_rule_var_port);
if (r == -1)
goto error;
d_set = 0;
} else {
if (!((negate + n_set) % 2)) {
r = DetectPortParseInsertString(de_ctx, head,address);
} else {
r = DetectPortParseInsertString(de_ctx, nhead,address);
}
if (r == -1)
goto error;
}
n_set = 0;
} else if (depth == 1 && s[u] == ',') {
range = 0;
}
}
if (depth > 0) {
SCLogError("not every port block was "
"properly closed in \"%s\", %d missing closing brackets (]). "
"Note: problem might be in a variable.",
s, depth);
goto error;
} else if (depth < 0) {
SCLogError("not every port block was "
"properly opened in \"%s\", %d missing opening brackets ([). "
"Note: problem might be in a variable.",
s, depth * -1);
goto error;
}
return 0;
error:
return -1;
}
/**
* \brief Check if the port group list covers the complete port space.
* \retval 0 no
* \retval 1 yes
*/
static int DetectPortIsCompletePortSpace(DetectPort *p)
{
uint16_t next_port = 0;
if (p == NULL)
return 0;
if (p->port != 0x0000)
return 0;
/* if we're ending with 0xFFFF while we know
we started with 0x0000 it's the complete space */
if (p->port2 == 0xFFFF)
return 1;
next_port = p->port2 + 1;
p = p->next;
for ( ; p != NULL; p = p->next) {
if (p->port != next_port)
return 0;
if (p->port2 == 0xFFFF)
return 1;
next_port = p->port2 + 1;
}
return 0;
}
/**
* \brief Helper function for the parsing process
*
* \param head Pointer to the head of the DetectPort group list
* \param nhead Pointer to the new head of the DetectPort group list
*
* \retval 0 on success
* \retval -1 on error
*/
static int DetectPortParseMergeNotPorts(const DetectEngineCtx *de_ctx,
DetectPort **head, DetectPort **nhead)
{
DetectPort *ad = NULL;
DetectPort *ag, *ag2;
int r = 0;
/** check if the full port space is negated */
if (DetectPortIsCompletePortSpace(*nhead) == 1) {
SCLogError("Complete port space is negated");
goto error;
}
/**
* step 0: if the head list is empty, but the nhead list isn't
* we have a pure not thingy. In that case we add a 0:65535
* first.
*/
if (*head == NULL && *nhead != NULL) {
SCLogDebug("inserting 0:65535 into head");
r = DetectPortParseInsertString(de_ctx, head,"0:65535");
if (r < 0) {
goto error;
}
}
/** step 1: insert our ghn members into the gh list */
for (ag = *nhead; ag != NULL; ag = ag->next) {
/** work with a copy of the ad so we can easily clean up
* the ghn group later.
*/
ad = DetectPortCopySingle(NULL, ag);
if (ad == NULL) {
goto error;
}
r = DetectPortParseInsert(head, ad);
if (r < 0) {
goto error;
}
ad = NULL;
}
/** step 2: pull the address blocks that match our 'not' blocks */
for (ag = *nhead; ag != NULL; ag = ag->next) {
SCLogDebug("ag %p", ag);
DetectPortPrint(ag);
for (ag2 = *head; ag2 != NULL; ) {
SCLogDebug("ag2 %p", ag2);
DetectPortPrint(ag2);
r = DetectPortCmp(ag, ag2);
if (r == PORT_EQ || r == PORT_EB) { /* XXX more ??? */
if (ag2->prev != NULL)
ag2->prev->next = ag2->next;
if (ag2->next != NULL)
ag2->next->prev = ag2->prev;
if (*head == ag2)
*head = ag2->next;
/** store the next ptr and remove the group */
DetectPort *next_ag2 = ag2->next;
DetectPortFree(de_ctx,ag2);
ag2 = next_ag2;
} else {
ag2 = ag2->next;
}
}
}
for (ag2 = *head; ag2 != NULL; ag2 = ag2->next) {
SCLogDebug("ag2 %p", ag2);
DetectPortPrint(ag2);
}
if (*head == NULL) {
SCLogError("no ports left after merging ports with negated ports");
goto error;
}
return 0;
error:
if (ad != NULL)
DetectPortFree(de_ctx, ad);
return -1;
}
int DetectPortTestConfVars(void)
{
SCLogDebug("Testing port conf vars for any misconfigured values");
ResolvedVariablesList var_list = TAILQ_HEAD_INITIALIZER(var_list);
ConfNode *port_vars_node = ConfGetNode("vars.port-groups");
if (port_vars_node == NULL) {
return 0;
}
ConfNode *seq_node;
TAILQ_FOREACH(seq_node, &port_vars_node->head, next) {
SCLogDebug("Testing %s - %s\n", seq_node->name, seq_node->val);
DetectPort *gh = DetectPortInit();
if (gh == NULL) {
goto error;
}
DetectPort *ghn = NULL;
if (seq_node->val == NULL) {
SCLogError("Port var \"%s\" probably has a sequence(something "
"in brackets) value set without any quotes. Please "
"quote it using \"..\".",
seq_node->name);
DetectPortCleanupList(NULL, gh);
goto error;
}
int r = DetectPortParseDo(NULL, &gh, &ghn, seq_node->val,
/* start with negate no */0, &var_list, 0);
CleanVariableResolveList(&var_list);
if (r < 0) {
DetectPortCleanupList(NULL, gh);
SCLogError("failed to parse port var \"%s\" with value \"%s\". "
"Please check its syntax",
seq_node->name, seq_node->val);
goto error;
}
if (DetectPortIsCompletePortSpace(ghn)) {
SCLogError("Port var - \"%s\" has the complete Port range negated "
"with its value \"%s\". Port space range is NIL. "
"Probably have a !any or a port range that supplies "
"a NULL address range",
seq_node->name, seq_node->val);
DetectPortCleanupList(NULL, gh);
DetectPortCleanupList(NULL, ghn);
goto error;
}
if (gh != NULL)
DetectPortCleanupList(NULL, gh);
if (ghn != NULL)
DetectPortCleanupList(NULL, ghn);
}
return 0;
error:
return -1;
}
/**
* \brief Function for parsing port strings
*
* \param de_ctx Pointer to the detection engine context
* \param head Pointer to the head of the DetectPort group list
* \param str Pointer to the port string
*
* \retval 0 on success
* \retval -1 on error
*/
int DetectPortParse(const DetectEngineCtx *de_ctx,
DetectPort **head, const char *str)
{
SCLogDebug("Port string to be parsed - str %s", str);
/* negate port list */
DetectPort *nhead = NULL;
int r = DetectPortParseDo(de_ctx, head, &nhead, str,
/* start with negate no */ 0, NULL, 0);
if (r < 0)
goto error;
SCLogDebug("head %p %p, nhead %p", head, *head, nhead);
/* merge the 'not' address groups */
if (DetectPortParseMergeNotPorts(de_ctx, head, &nhead) < 0)
goto error;
/* free the temp negate head */
DetectPortCleanupList(de_ctx, nhead);
return 0;
error:
DetectPortCleanupList(de_ctx, nhead);
return -1;
}
/**
* \brief Helper function for parsing port strings
*
* \param str Pointer to the port string
*
* \retval DetectPort pointer of the parse string on success
* \retval NULL on error
*/
DetectPort *PortParse(const char *str)
{
char *port2 = NULL;
char portstr[16];
/* strip leading spaces */
while (isspace(*str))
str++;
if (strlen(str) >= 16)
return NULL;
strlcpy(portstr, str, sizeof(portstr));
DetectPort *dp = DetectPortInit();
if (dp == NULL)
goto error;
/* we dup so we can put a nul-termination in it later */
char *port = portstr;
/* handle the negation case */
if (port[0] == '!') {
dp->flags |= PORT_FLAG_NOT;
port++;
}
/* see if the address is an ipv4 or ipv6 address */
if ((port2 = strchr(port, ':')) != NULL) {
/* 80:81 range format */
port2[0] = '\0';
port2++;
if (strcmp(port, "") != 0) {
if (!DetectPortIsValidRange(port, &dp->port))
goto error;
} else {
dp->port = 0;
}
if (strcmp(port2, "") != 0) {
if (!DetectPortIsValidRange(port2, &dp->port2))
goto error;
} else {
dp->port2 = 65535;
}
/* a > b is illegal, a == b is ok */
if (dp->port > dp->port2)
goto error;
} else {
if (strcasecmp(port,"any") == 0) {
dp->port = 0;
dp->port2 = 65535;
} else {
if (!DetectPortIsValidRange(port, &dp->port))
goto error;
dp->port2 = dp->port;
}
}
return dp;
error:
if (dp != NULL)
DetectPortCleanupList(NULL, dp);
return NULL;
}
/**
* \brief Helper function to check if a parsed port is in the valid range
* of available ports
*
* \param str Pointer to the port string
*
*
* \retval true if port is in the valid range
* \retval false if invalid
*/
static bool DetectPortIsValidRange(char *port, uint16_t *port_val)
{
if (StringParseUint16(port_val, 10, 0, (const char *)port) < 0)
return false;
return true;
}
/********************** End parsing routines ********************/
/* hash table */
/**
* \brief The hash function to be the used by the hash table -
* DetectEngineCtx->dport_hash_table.
*
* \param ht Pointer to the hash table.
* \param data Pointer to the DetectPort.
* \param datalen Not used in our case.
*
* \retval hash The generated hash value.
*/
static uint32_t DetectPortHashFunc(HashListTable *ht, void *data, uint16_t datalen)
{
DetectPort *p = (DetectPort *)data;
SCLogDebug("hashing port %p", p);
uint32_t hash = ((uint32_t)p->port << 16) | p->port2;
hash %= ht->array_size;
SCLogDebug("hash %"PRIu32, hash);
return hash;
}
/**
* \brief The Compare function to be used by the DetectPort hash table -
* DetectEngineCtx->dport_hash_table.
*
* \param data1 Pointer to the first DetectPort.
* \param len1 Not used.
* \param data2 Pointer to the second DetectPort.
* \param len2 Not used.
*
* \retval 1 If the 2 DetectPort sent as args match.
* \retval 0 If the 2 DetectPort sent as args do not match.
*/
static char DetectPortCompareFunc(void *data1, uint16_t len1,
void *data2, uint16_t len2)
{
DetectPort *dp1 = (DetectPort *)data1;
DetectPort *dp2 = (DetectPort *)data2;
if (data1 == NULL || data2 == NULL)
return 0;
if (dp1->port == dp2->port && dp1->port2 == dp2->port2)
return 1;
return 0;
}
static void DetectPortHashFreeFunc(void *ptr)
{
DetectPort *p = ptr;
DetectPortFree(NULL, p);
}
/**
* \brief Initializes the hash table in the detection engine context to hold the
* DetectPort hash.
*
* \param de_ctx Pointer to the detection engine context.
*
* \retval 0 On success.
* \retval -1 On failure.
*/
int DetectPortHashInit(DetectEngineCtx *de_ctx)
{
de_ctx->dport_hash_table = HashListTableInit(4096, DetectPortHashFunc,
DetectPortCompareFunc,
DetectPortHashFreeFunc);
if (de_ctx->dport_hash_table == NULL)
return -1;
return 0;
}
/**
* \brief Adds a DetectPort to the detection engine context DetectPort
* hash table.
*
* \param de_ctx Pointer to the detection engine context.
* \param dp Pointer to the DetectPort.
*
* \retval ret 0 on Successfully adding the DetectPort; -1 on failure.
*/
int DetectPortHashAdd(DetectEngineCtx *de_ctx, DetectPort *dp)
{
int ret = HashListTableAdd(de_ctx->dport_hash_table, (void *)dp, 0);
return ret;
}
/**
* \brief Used to lookup a DetectPort hash from the detection engine context
* DetectPort hash table.
*
* \param de_ctx Pointer to the detection engine context.
* \param sgh Pointer to the DetectPort.
*
* \retval rsgh On success a pointer to the DetectPort if the DetectPort is
* found in the hash table; NULL on failure.
*/
DetectPort *DetectPortHashLookup(DetectEngineCtx *de_ctx, DetectPort *dp)
{
SCEnter();
DetectPort *rdp = HashListTableLookup(de_ctx->dport_hash_table, (void *)dp, 0);
SCReturnPtr(rdp, "DetectPort");
}
/**
* \brief Frees the hash table - DetectEngineCtx->sgh_hash_table, allocated by
* DetectPortInit() function.
*
* \param de_ctx Pointer to the detection engine context.
*/
void DetectPortHashFree(DetectEngineCtx *de_ctx)
{
if (de_ctx->sgh_hash_table == NULL)
return;
HashListTableFree(de_ctx->dport_hash_table);
de_ctx->dport_hash_table = NULL;
return;
}
/*---------------------- Unittests -------------------------*/
#ifdef UNITTESTS
#include "packet.h"
/**
* \brief Do a sorted insert, where the top of the list should be the biggest
* port range.
*
* \todo XXX current sorting only works for overlapping ranges
*
* \param head Pointer to the DetectPort list head
* \param dp Pointer to DetectPort to search in the DetectPort list
* \retval 0 if dp is added correctly
*/
static int PortTestDetectPortAdd(DetectPort **head, DetectPort *dp)
{
DetectPort *cur, *prev_cur = NULL;
//SCLogDebug("DetectPortAdd: adding "); DetectPortPrint(ag); SCLogDebug("");
if (*head != NULL) {
for (cur = *head; cur != NULL; cur = cur->next) {
prev_cur = cur;
int r = DetectPortCmp(dp,cur);
if (r == PORT_EB) {
/* insert here */
dp->prev = cur->prev;
dp->next = cur;
cur->prev = dp;
if (*head == cur) {
*head = dp;
} else {
dp->prev->next = dp;
}
return 0;
}
}
dp->prev = prev_cur;
if (prev_cur != NULL)
prev_cur->next = dp;
} else {
*head = dp;
}
return 0;
}
/**
* \test Check if a DetectPort is properly allocated
*/
static int PortTestParse01 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"80");
FAIL_IF_NOT(r == 0);
DetectPortFree(NULL, dd);
PASS;
}
/**
* \test Check if two ports are properly allocated in the DetectPort group
*/
static int PortTestParse02 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"80");
FAIL_IF_NOT(r == 0);
r = DetectPortParse(NULL,&dd,"22");
FAIL_IF_NOT(r == 0);
DetectPortCleanupList(NULL, dd);
PASS;
}
/**
* \test Check if two port ranges are properly allocated in the DetectPort group
*/
static int PortTestParse03 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"80:88");
FAIL_IF_NOT(r == 0);
r = DetectPortParse(NULL,&dd,"85:100");
FAIL_IF_NOT(r == 0);
DetectPortCleanupList(NULL, dd);
PASS;
}
/**
* \test Check if a negated port range is properly allocated in the DetectPort
*/
static int PortTestParse04 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"!80:81");
FAIL_IF_NOT(r == 0);
DetectPortCleanupList(NULL, dd);
PASS;
}
/**
* \test Check if a negated port range is properly fragmented in the allowed
* real groups, ex !80:81 should allow 0:79 and 82:65535
*/
static int PortTestParse05 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"!80:81");
FAIL_IF_NOT(r == 0);
FAIL_IF_NULL(dd->next);
FAIL_IF_NOT(dd->port == 0);
FAIL_IF_NOT(dd->port2 == 79);
FAIL_IF_NOT(dd->next->port == 82);
FAIL_IF_NOT(dd->next->port2 == 65535);
DetectPortCleanupList(NULL, dd);
PASS;
}
/**
* \test Check if a negated port range is properly fragmented in the allowed
* real groups
*/
static int PortTestParse07 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"!21:902");
FAIL_IF_NOT(r == 0);
FAIL_IF_NULL(dd->next);
FAIL_IF_NOT(dd->port == 0);
FAIL_IF_NOT(dd->port2 == 20);
FAIL_IF_NOT(dd->next->port == 903);
FAIL_IF_NOT(dd->next->port2 == 65535);
DetectPortCleanupList(NULL, dd);
PASS;
}
/**
* \test Check if we dont allow invalid port range specification
*/
static int PortTestParse08 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"[80:!80]");
FAIL_IF(r == 0);
DetectPortCleanupList(NULL, dd);
PASS;
}
/**
* \test Check if we autocomplete correctly an open range
*/
static int PortTestParse09 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"1024:");
FAIL_IF_NOT(r == 0);
FAIL_IF_NULL(dd);
FAIL_IF_NOT(dd->port == 1024);
FAIL_IF_NOT(dd->port2 == 0xffff);
DetectPortCleanupList(NULL, dd);
PASS;
}
/**
* \test Test we don't allow a port that is too big
*/
static int PortTestParse10 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"77777777777777777777777777777777777777777777");
FAIL_IF(r == 0);
PASS;
}
/**
* \test Test second port of range being too big
*/
static int PortTestParse11 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"1024:65536");
FAIL_IF(r == 0);
PASS;
}
/**
* \test Test second port of range being just right
*/
static int PortTestParse12 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"1024:65535");
FAIL_IF_NOT(r == 0);
DetectPortFree(NULL, dd);
PASS;
}
/**
* \test Test first port of range being too big
*/
static int PortTestParse13 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"65536:65535");
FAIL_IF(r == 0);
PASS;
}
/**
* \test Test merging port groups
*/
static int PortTestParse14 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParseInsertString(NULL, &dd, "0:100");
FAIL_IF_NOT(r == 0);
r = DetectPortParseInsertString(NULL, &dd, "1000:65535");
FAIL_IF_NOT(r == 0);
FAIL_IF_NULL(dd->next);
FAIL_IF_NOT(dd->port == 0);
FAIL_IF_NOT(dd->port2 == 100);
FAIL_IF_NOT(dd->next->port == 1000);
FAIL_IF_NOT(dd->next->port2 == 65535);
DetectPortCleanupList(NULL, dd);
PASS;
}
/**
* \test Test merging negated port groups
*/
static int PortTestParse15 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"![0:100,1000:3000]");
FAIL_IF_NOT(r == 0);
FAIL_IF_NULL(dd->next);
FAIL_IF_NOT(dd->port == 101);
FAIL_IF_NOT(dd->port2 == 999);
FAIL_IF_NOT(dd->next->port == 3001);
FAIL_IF_NOT(dd->next->port2 == 65535);
DetectPortCleanupList(NULL, dd);
PASS;
}
static int PortTestParse16 (void)
{
DetectPort *dd = NULL;
int r = DetectPortParse(NULL,&dd,"\
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\
1:65535\
]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\
");
FAIL_IF_NOT(r == 0);
DetectPortFree(NULL, dd);
dd = NULL;
r = DetectPortParse(NULL,&dd,"\
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\
1:65535\
]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\
");
FAIL_IF(r == 0);
PASS;
}
/**
* \test Test general functions
*/
static int PortTestFunctions01(void)
{
DetectPort *head = NULL;
DetectPort *dp1= NULL;
int result = 0;
/* Parse */
int r = DetectPortParse(NULL,&head,"![0:100,1000:65535]");
if (r != 0 || head->next != NULL)
goto end;
/* We should have only one DetectPort */
if (!(head->port == 101))
goto end;
if (!(head->port2 == 999))
goto end;
if (!(head->next == NULL))
goto end;
r = DetectPortParse(NULL, &dp1,"2000:3000");
if (r != 0 || dp1->next != NULL)
goto end;
if (!(dp1->port == 2000))
goto end;
if (!(dp1->port2 == 3000))
goto end;
/* Add */
r = PortTestDetectPortAdd(&head, dp1);
if (r != 0 || head->next == NULL)
goto end;
if (!(head->port == 101))
goto end;
if (!(head->port2 == 999))
goto end;
if (!(head->next->port == 2000))
goto end;
if (!(head->next->port2 == 3000))
goto end;
/* Match */
if (!DetectPortMatch(head, 150))
goto end;
if (DetectPortMatch(head->next, 1500))
goto end;
if ((DetectPortMatch(head, 3500)))
goto end;
if ((DetectPortMatch(head, 50)))
goto end;
result = 1;
end:
if (dp1 != NULL)
DetectPortFree(NULL, dp1);
if (head != NULL)
DetectPortFree(NULL, head);
return result;
}
/**
* \test Test general functions
*/
static int PortTestFunctions02(void)
{
DetectPort *head = NULL;
DetectPort *dp1= NULL;
DetectPort *dp2= NULL;
int result = 0;
/* Parse */
int r = DetectPortParse(NULL,&head, "![0:100,1000:65535]");
if (r != 0 || head->next != NULL)
goto end;
r = DetectPortParse(NULL, &dp1, "!200:300");
if (r != 0 || dp1->next == NULL)
goto end;
/* Merge Nots */
r = DetectPortParseMergeNotPorts(NULL, &head, &dp1);
if (r != 0 || head->next != NULL)
goto end;
r = DetectPortParse(NULL, &dp2, "!100:500");
if (r != 0 || dp2->next == NULL)
goto end;
/* Merge Nots */
r = DetectPortParseMergeNotPorts(NULL, &head, &dp2);
if (r != 0 || head->next != NULL)
goto end;
if (!(head->port == 200))
goto end;
if (!(head->port2 == 300))
goto end;
result = 1;
end:
if (dp1 != NULL)
DetectPortFree(NULL, dp1);
if (dp2 != NULL)
DetectPortFree(NULL, dp2);
if (head != NULL)
DetectPortFree(NULL, head);
return result;
}
/**
* \test Test general functions
*/
static int PortTestFunctions03(void)
{
DetectPort *dp1= NULL;
DetectPort *dp2= NULL;
DetectPort *dp3= NULL;
int result = 0;
int r = DetectPortParse(NULL, &dp1, "200:300");
if (r != 0)
goto end;
r = DetectPortParse(NULL, &dp2, "250:300");
if (r != 0)
goto end;
/* Cut */
DetectPortCut(NULL, dp1, dp2, &dp3);
if (r != 0)
goto end;
if (!(dp1->port == 200))
goto end;
if (!(dp1->port2 == 249))
goto end;
if (!(dp2->port == 250))
goto end;
if (!(dp2->port2 == 300))
goto end;
dp1->port = 0;
dp1->port2 = 500;
dp2->port = 250;
dp2->port2 = 750;
/* Cut */
DetectPortCut(NULL, dp1, dp2, &dp3);
if (r != 0)
goto end;
if (!(dp1->port == 0))
goto end;
if (!(dp1->port2 == 249))
goto end;
if (!(dp2->port == 250))
goto end;
if (!(dp2->port2 == 500))
goto end;
if (!(dp3->port == 501))
goto end;
if (!(dp3->port2 == 750))
goto end;
result = 1;
end:
if (dp1 != NULL)
DetectPortFree(NULL, dp1);
if (dp2 != NULL)
DetectPortFree(NULL, dp2);
if (dp3 != NULL)
DetectPortFree(NULL, dp3);
return result;
}
/**
* \test Test general functions
*/
static int PortTestFunctions04(void)
{
DetectPort *dp1= NULL;
DetectPort *dp2= NULL;
int result = 0;
int r = DetectPortParse(NULL, &dp1, "200:300");
if (r != 0)
goto end;
dp2 = DetectPortInit();
/* Cut Not */
DetectPortCutNot(dp1, &dp2);
if (r != 0)
goto end;
if (!(dp1->port == 0))
goto end;
if (!(dp1->port2 == 199))
goto end;
if (!(dp2->port == 301))
goto end;
if (!(dp2->port2 == 65535))
goto end;
result = 1;
end:
if (dp1 != NULL)
DetectPortFree(NULL, dp1);
if (dp2 != NULL)
DetectPortFree(NULL, dp2);
return result;
}
/**
* \test Test general functions
*/
static int PortTestFunctions05(void)
{
DetectPort *dp1 = NULL;
DetectPort *dp2 = NULL;
DetectPort *dp3 = NULL;
int result = 0;
int r = 0;
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
Signature s[2];
memset(s,0x00,sizeof(s));
s[0].num = 0;
s[1].num = 1;
r = DetectPortParse(NULL, &dp1, "1024:65535");
if (r != 0) {
printf("r != 0 but %d: ", r);
goto end;
}
SigGroupHeadAppendSig(de_ctx, &dp1->sh, &s[0]);
r = DetectPortParse(NULL, &dp2, "any");
if (r != 0) {
printf("r != 0 but %d: ", r);
goto end;
}
SigGroupHeadAppendSig(de_ctx, &dp2->sh, &s[1]);
SCLogDebug("dp1");
DetectPortPrint(dp1);
SCLogDebug("dp2");
DetectPortPrint(dp2);
DetectPortInsert(de_ctx, &dp3, dp1);
DetectPortInsert(de_ctx, &dp3, dp2);
if (dp3 == NULL)
goto end;
SCLogDebug("dp3");
DetectPort *x = dp3;
for ( ; x != NULL; x = x->next) {
DetectPortPrint(x);
//SigGroupHeadPrintSigs(de_ctx, x->sh);
}
DetectPort *one = dp3;
DetectPort *two = dp3->next;
int sig = 0;
if ((one->sh->init->sig_array[sig / 8] & (1 << (sig % 8)))) {
printf("sig %d part of 'one', but it shouldn't: ", sig);
goto end;
}
sig = 1;
if (!(one->sh->init->sig_array[sig / 8] & (1 << (sig % 8)))) {
printf("sig %d part of 'one', but it shouldn't: ", sig);
goto end;
}
sig = 1;
if (!(two->sh->init->sig_array[sig / 8] & (1 << (sig % 8)))) {
printf("sig %d part of 'two', but it shouldn't: ", sig);
goto end;
}
result = 1;
end:
if (dp1 != NULL)
DetectPortFree(NULL, dp1);
if (dp2 != NULL)
DetectPortFree(NULL, dp2);
return result;
}
/**
* \test Test general functions
*/
static int PortTestFunctions06(void)
{
DetectPort *dp1 = NULL;
DetectPort *dp2 = NULL;
DetectPort *dp3 = NULL;
int result = 0;
int r = 0;
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
Signature s[2];
memset(s,0x00,sizeof(s));
s[0].num = 0;
s[1].num = 1;
r = DetectPortParse(NULL, &dp1, "1024:65535");
if (r != 0) {
printf("r != 0 but %d: ", r);
goto end;
}
SigGroupHeadAppendSig(de_ctx, &dp1->sh, &s[0]);
r = DetectPortParse(NULL, &dp2, "any");
if (r != 0) {
printf("r != 0 but %d: ", r);
goto end;
}
SigGroupHeadAppendSig(de_ctx, &dp2->sh, &s[1]);
SCLogDebug("dp1");
DetectPortPrint(dp1);
SCLogDebug("dp2");
DetectPortPrint(dp2);
DetectPortInsert(de_ctx, &dp3, dp2);
DetectPortInsert(de_ctx, &dp3, dp1);
if (dp3 == NULL)
goto end;
SCLogDebug("dp3");
DetectPort *x = dp3;
for ( ; x != NULL; x = x->next) {
DetectPortPrint(x);
//SigGroupHeadPrintSigs(de_ctx, x->sh);
}
DetectPort *one = dp3;
DetectPort *two = dp3->next;
int sig = 0;
if ((one->sh->init->sig_array[sig / 8] & (1 << (sig % 8)))) {
printf("sig %d part of 'one', but it shouldn't: ", sig);
goto end;
}
sig = 1;
if (!(one->sh->init->sig_array[sig / 8] & (1 << (sig % 8)))) {
printf("sig %d part of 'one', but it shouldn't: ", sig);
goto end;
}
sig = 1;
if (!(two->sh->init->sig_array[sig / 8] & (1 << (sig % 8)))) {
printf("sig %d part of 'two', but it shouldn't: ", sig);
goto end;
}
result = 1;
end:
if (dp1 != NULL)
DetectPortFree(NULL, dp1);
if (dp2 != NULL)
DetectPortFree(NULL, dp2);
return result;
}
/**
* \test Test general functions
*/
static int PortTestFunctions07(void)
{
DetectPort *dd = NULL;
// This one should fail due to negation in a range
FAIL_IF(DetectPortParse(NULL, &dd, "[80:!99]") == 0);
// Correct: from 80 till 100 but 99 excluded
FAIL_IF_NOT(DetectPortParse(NULL, &dd, "[80:100,!99]") == 0);
FAIL_IF_NULL(dd->next);
FAIL_IF_NOT(dd->port == 80);
FAIL_IF_NOT(dd->port2 == 98);
FAIL_IF_NOT(dd->next->port == 100);
// Also good: from 1 till 80 except of 2 and 4
FAIL_IF_NOT(DetectPortParse(NULL, &dd, "[1:80,![2,4]]") == 0);
FAIL_IF_NOT(dd->port == 1);
FAIL_IF_NULL(DetectPortLookupGroup(dd, 3));
FAIL_IF_NOT_NULL(DetectPortLookupGroup(dd, 2));
FAIL_IF_NULL(DetectPortLookupGroup(dd, 80));
DetectPortCleanupList(NULL, dd);
PASS;
}
/**
* \test Test packet Matches
* \param raw_eth_pkt pointer to the ethernet packet
* \param pktsize size of the packet
* \param sig pointer to the signature to test
* \param sid sid number of the signature
* \retval return 1 if match
* \retval return 0 if not
*/
static int PortTestMatchReal(uint8_t *raw_eth_pkt, uint16_t pktsize, const char *sig,
uint32_t sid)
{
int result = 0;
FlowInitConfig(FLOW_QUIET);
Packet *p = UTHBuildPacketFromEth(raw_eth_pkt, pktsize);
result = UTHPacketMatchSig(p, sig);
PacketRecycle(p);
FlowShutdown();
return result;
}
/**
* \brief Wrapper for PortTestMatchReal
*/
static int PortTestMatchRealWrp(const char *sig, uint32_t sid)
{
/* Real HTTP packeth doing a GET method
* tcp.sport=47370 tcp.dport=80
* ip.src=192.168.28.131 ip.dst=192.168.1.1
*/
uint8_t raw_eth_pkt[] = {
0x00,0x50,0x56,0xea,0x00,0xbd,0x00,0x0c,
0x29,0x40,0xc8,0xb5,0x08,0x00,0x45,0x00,
0x01,0xa8,0xb9,0xbb,0x40,0x00,0x40,0x06,
0xe0,0xbf,0xc0,0xa8,0x1c,0x83,0xc0,0xa8,
0x01,0x01,0xb9,0x0a,0x00,0x50,0x6f,0xa2,
0x92,0xed,0x7b,0xc1,0xd3,0x4d,0x50,0x18,
0x16,0xd0,0xa0,0x6f,0x00,0x00,0x47,0x45,
0x54,0x20,0x2f,0x20,0x48,0x54,0x54,0x50,
0x2f,0x31,0x2e,0x31,0x0d,0x0a,0x48,0x6f,
0x73,0x74,0x3a,0x20,0x31,0x39,0x32,0x2e,
0x31,0x36,0x38,0x2e,0x31,0x2e,0x31,0x0d,
0x0a,0x55,0x73,0x65,0x72,0x2d,0x41,0x67,
0x65,0x6e,0x74,0x3a,0x20,0x4d,0x6f,0x7a,
0x69,0x6c,0x6c,0x61,0x2f,0x35,0x2e,0x30,
0x20,0x28,0x58,0x31,0x31,0x3b,0x20,0x55,
0x3b,0x20,0x4c,0x69,0x6e,0x75,0x78,0x20,
0x78,0x38,0x36,0x5f,0x36,0x34,0x3b,0x20,
0x65,0x6e,0x2d,0x55,0x53,0x3b,0x20,0x72,
0x76,0x3a,0x31,0x2e,0x39,0x2e,0x30,0x2e,
0x31,0x34,0x29,0x20,0x47,0x65,0x63,0x6b,
0x6f,0x2f,0x32,0x30,0x30,0x39,0x30,0x39,
0x30,0x32,0x31,0x37,0x20,0x55,0x62,0x75,
0x6e,0x74,0x75,0x2f,0x39,0x2e,0x30,0x34,
0x20,0x28,0x6a,0x61,0x75,0x6e,0x74,0x79,
0x29,0x20,0x46,0x69,0x72,0x65,0x66,0x6f,
0x78,0x2f,0x33,0x2e,0x30,0x2e,0x31,0x34,
0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74,
0x3a,0x20,0x74,0x65,0x78,0x74,0x2f,0x68,
0x74,0x6d,0x6c,0x2c,0x61,0x70,0x70,0x6c,
0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,
0x78,0x68,0x74,0x6d,0x6c,0x2b,0x78,0x6d,
0x6c,0x2c,0x61,0x70,0x70,0x6c,0x69,0x63,
0x61,0x74,0x69,0x6f,0x6e,0x2f,0x78,0x6d,
0x6c,0x3b,0x71,0x3d,0x30,0x2e,0x39,0x2c,
0x2a,0x2f,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
0x38,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
0x74,0x2d,0x4c,0x61,0x6e,0x67,0x75,0x61,
0x67,0x65,0x3a,0x20,0x65,0x6e,0x2d,0x75,
0x73,0x2c,0x65,0x6e,0x3b,0x71,0x3d,0x30,
0x2e,0x35,0x0d,0x0a,0x41,0x63,0x63,0x65,
0x70,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,
0x69,0x6e,0x67,0x3a,0x20,0x67,0x7a,0x69,
0x70,0x2c,0x64,0x65,0x66,0x6c,0x61,0x74,
0x65,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
0x74,0x2d,0x43,0x68,0x61,0x72,0x73,0x65,
0x74,0x3a,0x20,0x49,0x53,0x4f,0x2d,0x38,
0x38,0x35,0x39,0x2d,0x31,0x2c,0x75,0x74,
0x66,0x2d,0x38,0x3b,0x71,0x3d,0x30,0x2e,
0x37,0x2c,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
0x37,0x0d,0x0a,0x4b,0x65,0x65,0x70,0x2d,
0x41,0x6c,0x69,0x76,0x65,0x3a,0x20,0x33,
0x30,0x30,0x0d,0x0a,0x43,0x6f,0x6e,0x6e,
0x65,0x63,0x74,0x69,0x6f,0x6e,0x3a,0x20,
0x6b,0x65,0x65,0x70,0x2d,0x61,0x6c,0x69,
0x76,0x65,0x0d,0x0a,0x0d,0x0a };
/* end raw_eth_pkt */
return PortTestMatchReal(raw_eth_pkt, (uint16_t)sizeof(raw_eth_pkt),
sig, sid);
}
/**
* \test Check if we match a dest port
*/
static int PortTestMatchReal01(void)
{
/* tcp.sport=47370 tcp.dport=80 */
const char *sig = "alert tcp any any -> any 80 (msg:\"Nothing..\"; content:\"GET\"; sid:1;)";
return PortTestMatchRealWrp(sig, 1);
}
/**
* \test Check if we match a source port
*/
static int PortTestMatchReal02(void)
{
const char *sig = "alert tcp any 47370 -> any any (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return PortTestMatchRealWrp(sig, 1);
}
/**
* \test Check if we match both of them
*/
static int PortTestMatchReal03(void)
{
const char *sig = "alert tcp any 47370 -> any 80 (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return PortTestMatchRealWrp(sig, 1);
}
/**
* \test Check if we negate dest ports correctly
*/
static int PortTestMatchReal04(void)
{
const char *sig = "alert tcp any any -> any !80 (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return (PortTestMatchRealWrp(sig, 1) == 0)? 1 : 0;
}
/**
* \test Check if we negate source ports correctly
*/
static int PortTestMatchReal05(void)
{
const char *sig = "alert tcp any !47370 -> any any (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return (PortTestMatchRealWrp(sig, 1) == 0)? 1 : 0;
}
/**
* \test Check if we negate both ports correctly
*/
static int PortTestMatchReal06(void)
{
const char *sig = "alert tcp any !47370 -> any !80 (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return (PortTestMatchRealWrp(sig, 1) == 0)? 1 : 0;
}
/**
* \test Check if we match a dest port range
*/
static int PortTestMatchReal07(void)
{
const char *sig = "alert tcp any any -> any 70:100 (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return PortTestMatchRealWrp(sig, 1);
}
/**
* \test Check if we match a source port range
*/
static int PortTestMatchReal08(void)
{
const char *sig = "alert tcp any 47000:50000 -> any any (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return PortTestMatchRealWrp(sig, 1);
}
/**
* \test Check if we match both port ranges
*/
static int PortTestMatchReal09(void)
{
const char *sig = "alert tcp any 47000:50000 -> any 70:100 (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return PortTestMatchRealWrp(sig, 1);
}
/**
* \test Check if we negate a dest port range
*/
static int PortTestMatchReal10(void)
{
const char *sig = "alert tcp any any -> any !70:100 (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return (PortTestMatchRealWrp(sig, 1) == 0)? 1 : 0;
}
/**
* \test Check if we negate a source port range
*/
static int PortTestMatchReal11(void)
{
const char *sig = "alert tcp any !47000:50000 -> any any (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return (PortTestMatchRealWrp(sig, 1) == 0)? 1 : 0;
}
/**
* \test Check if we negate both port ranges
*/
static int PortTestMatchReal12(void)
{
const char *sig = "alert tcp any !47000:50000 -> any !70:100 (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return (PortTestMatchRealWrp(sig, 1) == 0)? 1 : 0;
}
/**
* \test Check if we autocomplete ranges correctly
*/
static int PortTestMatchReal13(void)
{
const char *sig = "alert tcp any 47000:50000 -> any !81: (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return PortTestMatchRealWrp(sig, 1);
}
/**
* \test Check if we autocomplete ranges correctly
*/
static int PortTestMatchReal14(void)
{
const char *sig = "alert tcp any !48000:50000 -> any :100 (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return PortTestMatchRealWrp(sig, 1);
}
/**
* \test Check if we autocomplete ranges correctly
*/
static int PortTestMatchReal15(void)
{
const char *sig = "alert tcp any :50000 -> any 81:100 (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return (PortTestMatchRealWrp(sig, 1) == 0)? 1 : 0;
}
/**
* \test Check if we separate ranges correctly
*/
static int PortTestMatchReal16(void)
{
const char *sig = "alert tcp any 100: -> any ![0:79,81:65535] (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return PortTestMatchRealWrp(sig, 1);
}
/**
* \test Check if we separate ranges correctly
*/
static int PortTestMatchReal17(void)
{
const char *sig = "alert tcp any ![0:39999,48000:50000] -> any ![0:80,82:65535] "
"(msg:\"Nothing..\"; content:\"GET\"; sid:1;)";
return (PortTestMatchRealWrp(sig, 1) == 0)? 1 : 0;
}
/**
* \test Check if we separate ranges correctly
*/
static int PortTestMatchReal18(void)
{
const char *sig = "alert tcp any ![0:39999,48000:50000] -> any 80 (msg:\"Nothing"
" at all\"; content:\"GET\"; sid:1;)";
return PortTestMatchRealWrp(sig, 1);
}
/**
* \test Check if we separate ranges correctly
*/
static int PortTestMatchReal19(void)
{
const char *sig = "alert tcp any any -> any 80 (msg:\"Nothing..\";"
" content:\"GET\"; sid:1;)";
return PortTestMatchRealWrp(sig, 1);
}
static int PortTestMatchDoubleNegation(void)
{
int result = 0;
DetectPort *head = NULL, *nhead = NULL;
if (DetectPortParseDo(NULL, &head, &nhead, "![!80]", 0, NULL, 0) == -1)
return result;
result = (head != NULL);
result = (nhead == NULL);
return result;
}
// Test that negation is successfully parsed with whitespace for port strings of
// length < 16
static int DetectPortParseDoTest(void)
{
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
FAIL_IF_NULL(de_ctx);
DetectPort *head = NULL;
DetectPort *nhead = NULL;
const char *str = "[30:50, !45]";
int r = DetectPortParseDo(de_ctx, &head, &nhead, str, 0, NULL, 0);
// Assertions
FAIL_IF_NULL(head);
FAIL_IF_NULL(nhead);
FAIL_IF(r < 0);
FAIL_IF(head->port != 30);
FAIL_IF(head->port2 != 50);
FAIL_IF(nhead->port != 45);
FAIL_IF(nhead->port2 != 45);
DetectPortCleanupList(NULL, head);
DetectPortCleanupList(NULL, nhead);
PASS;
}
static int DetectPortParseDoTest2(void)
{
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
FAIL_IF_NULL(de_ctx);
DetectPort *head = NULL;
DetectPort *nhead = NULL;
const char *str = "[30:50, !45]";
int r = DetectPortParseDo(de_ctx, &head, &nhead, str, 0, NULL, 0);
FAIL_IF(r < 0);
DetectPortCleanupList(NULL, head);
DetectPortCleanupList(NULL, nhead);
PASS;
}
// Verifies correct parsing when negation port string length < 16
static int PortParseTestLessThan14Spaces(void)
{
const char *str = " 45";
DetectPort *dp = PortParse(str);
FAIL_IF_NULL(dp);
FAIL_IF(dp->port != 45);
FAIL_IF(dp->port2 != 45);
DetectPortFree(NULL, dp);
PASS;
}
// Verifies NULL returned when negation port string length == 16
static int PortParseTest14Spaces(void)
{
const char *str = " 45";
DetectPort *dp = PortParse(str);
FAIL_IF_NULL(dp);
FAIL_IF(dp->port != 45);
FAIL_IF(dp->port2 != 45);
DetectPortFree(NULL, dp);
PASS;
}
// Verifies NULL returned when negation port string length >= 16
static int PortParseTestMoreThan14Spaces(void)
{
const char *str = " 45";
DetectPort *dp = PortParse(str);
FAIL_IF_NULL(dp);
FAIL_IF(dp->port != 45);
FAIL_IF(dp->port2 != 45);
DetectPortFree(NULL, dp);
PASS;
}
void DetectPortTests(void)
{
UtRegisterTest("PortTestParse01", PortTestParse01);
UtRegisterTest("PortTestParse02", PortTestParse02);
UtRegisterTest("PortTestParse03", PortTestParse03);
UtRegisterTest("PortTestParse04", PortTestParse04);
UtRegisterTest("PortTestParse05", PortTestParse05);
UtRegisterTest("PortTestParse07", PortTestParse07);
UtRegisterTest("PortTestParse08", PortTestParse08);
UtRegisterTest("PortTestParse09", PortTestParse09);
UtRegisterTest("PortTestParse10", PortTestParse10);
UtRegisterTest("PortTestParse11", PortTestParse11);
UtRegisterTest("PortTestParse12", PortTestParse12);
UtRegisterTest("PortTestParse13", PortTestParse13);
UtRegisterTest("PortTestParse14", PortTestParse14);
UtRegisterTest("PortTestParse15", PortTestParse15);
UtRegisterTest("PortTestParse16", PortTestParse16);
UtRegisterTest("PortTestFunctions01", PortTestFunctions01);
UtRegisterTest("PortTestFunctions02", PortTestFunctions02);
UtRegisterTest("PortTestFunctions03", PortTestFunctions03);
UtRegisterTest("PortTestFunctions04", PortTestFunctions04);
UtRegisterTest("PortTestFunctions05", PortTestFunctions05);
UtRegisterTest("PortTestFunctions06", PortTestFunctions06);
UtRegisterTest("PortTestFunctions07", PortTestFunctions07);
UtRegisterTest("PortTestMatchReal01", PortTestMatchReal01);
UtRegisterTest("PortTestMatchReal02", PortTestMatchReal02);
UtRegisterTest("PortTestMatchReal03", PortTestMatchReal03);
UtRegisterTest("PortTestMatchReal04", PortTestMatchReal04);
UtRegisterTest("PortTestMatchReal05", PortTestMatchReal05);
UtRegisterTest("PortTestMatchReal06", PortTestMatchReal06);
UtRegisterTest("PortTestMatchReal07", PortTestMatchReal07);
UtRegisterTest("PortTestMatchReal08", PortTestMatchReal08);
UtRegisterTest("PortTestMatchReal09", PortTestMatchReal09);
UtRegisterTest("PortTestMatchReal10", PortTestMatchReal10);
UtRegisterTest("PortTestMatchReal11", PortTestMatchReal11);
UtRegisterTest("PortTestMatchReal12", PortTestMatchReal12);
UtRegisterTest("PortTestMatchReal13", PortTestMatchReal13);
UtRegisterTest("PortTestMatchReal14", PortTestMatchReal14);
UtRegisterTest("PortTestMatchReal15", PortTestMatchReal15);
UtRegisterTest("PortTestMatchReal16", PortTestMatchReal16);
UtRegisterTest("PortTestMatchReal17", PortTestMatchReal17);
UtRegisterTest("PortTestMatchReal18", PortTestMatchReal18);
UtRegisterTest("PortTestMatchReal19", PortTestMatchReal19);
UtRegisterTest("PortTestMatchDoubleNegation", PortTestMatchDoubleNegation);
UtRegisterTest("DetectPortParseDoTest", DetectPortParseDoTest);
UtRegisterTest("DetectPortParseDoTest2", DetectPortParseDoTest2);
UtRegisterTest("PortParseTestLessThan14Spaces", PortParseTestLessThan14Spaces);
UtRegisterTest("PortParseTest14Spaces", PortParseTest14Spaces);
UtRegisterTest("PortParseTestMoreThan14Spaces", PortParseTestMoreThan14Spaces);
}
#endif /* UNITTESTS */
|