Skip to content

Commit a400e0a

Browse files
committed
Custom thresholding for dup scans
1 parent 6432011 commit a400e0a

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

scanlogger.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def __init__(self, timeout, threshold, itf=None, maxsize=8192, daemon=True, logf
6060
self.long_scans = entry.EntryLog(maxsize)
6161
# Port scan weight threshold
6262
self.threshold = threshold
63+
# Custom thresholds
64+
self.custom_thresholds = {}
6365
# Timeout for scan entries
6466
self.timeout = timeout
6567
# Long-period scan timeouts
@@ -125,6 +127,10 @@ def log_scan(self, scan):
125127
# Continuing an already detected scan
126128
tup = [scan.type, srcip,dstip, ports]
127129
if not scan.slow_scan:
130+
# We dont want a continuing scan to log too many times
131+
# so update the threshold for the scan's hash
132+
custom_threshold = levelParams['max'][1]
133+
self.custom_thresholds[scan.hash] = custom_threshold
128134
if scan.type != TCP_IDLE_SCAN:
129135
line = 'Continuing %s scan from %s to %s (ports:%s)'
130136
else:
@@ -163,6 +169,10 @@ def inspect_scan(self, scan, slow_scan=False):
163169
else:
164170
threshold = self.threshold
165171

172+
if scan.hash in self.custom_thresholds:
173+
# Pick up custom threshold
174+
threshold = self.custom_thresholds[scan.hash]
175+
166176
# print(threshold, scan.weight)
167177
# Sure scan
168178
is_scan = (scan.weight >= threshold)
@@ -293,9 +303,11 @@ def inspect_scan(self, scan, slow_scan=False):
293303
else:
294304
return False
295305

296-
def process(self, ts, pkt):
306+
def process(self, ts, pkt, decode=None):
297307
""" Process an incoming packet looking for scan signatures """
298308

309+
pkt = decode(pkt)
310+
299311
# Dont process non-IP packets
300312
if not 'ip' in pkt.__dict__:
301313
return
@@ -397,16 +409,15 @@ def process(self, ts, pkt):
397409

398410
def loop(self):
399411
""" Run the main logic in a loop listening to packets """
412+
400413
pc = pcap.pcap(name=self.itf, promisc=True, immediate=True, timeout_ms=500)
401414
decode = { pcap.DLT_LOOP:dpkt.loopback.Loopback,
402415
pcap.DLT_NULL:dpkt.loopback.Loopback,
403416
pcap.DLT_EN10MB:dpkt.ethernet.Ethernet } [pc.datalink()]
404417

405418
try:
406419
print('listening on %s: %s' % (pc.name, pc.filter))
407-
for ts, pkt in pc:
408-
# print(ts, pkt)
409-
self.process(ts, decode(pkt))
420+
pc.loop(-1, self.process, decode)
410421
except KeyboardInterrupt:
411422
if not self.daemon:
412423
nrecv, ndrop, nifdrop = pc.stats()

0 commit comments

Comments
 (0)