21
21
import time
22
22
import argparse
23
23
import hasher
24
-
24
+ import utils
25
25
import entry
26
26
import timerlist
27
27
from constants import *
35
35
36
36
PIDFILE = "/var/run/pyscanlogger.pid"
37
37
38
- get_timestamp = lambda : time .strftime ('%Y-%m-%d %H:%M:%S' , time .localtime ())
39
- ip2quad = lambda x : socket .inet_ntoa (struct .pack ('I' , x ))
40
- scan_ip2quad = lambda scan : list (map (ip2quad , [scan .src , scan .dst ]))
41
-
42
38
class ScanLogger :
43
39
""" Port scan detector and logger class """
44
40
@@ -91,7 +87,7 @@ def __init__(self, timeout, threshold, maxsize=8192, daemon=True, logfile='/var/
91
87
def log (self , msg ):
92
88
""" Log a message to console and/or log file """
93
89
94
- line = f'[{ get_timestamp ()} ]: { msg } '
90
+ line = f'[{ utils . timestamp ()} ]: { msg } '
95
91
if self .scanlog :
96
92
self .scanlog .write (line + '\n ' )
97
93
self .scanlog .flush ()
@@ -102,7 +98,7 @@ def log(self, msg):
102
98
def log_scan (self , scan , continuation = False , slow_scan = False , unsure = False ):
103
99
""" Log the scan to file and/or console """
104
100
105
- srcip , dstip = scan_ip2quad (scan )
101
+ srcip , dstip = utils . scan_ip2quad (scan )
106
102
ports = ',' .join ([str (port ) for port in scan .ports ])
107
103
108
104
if not continuation :
@@ -112,7 +108,7 @@ def log_scan(self, scan, continuation=False, slow_scan=False, unsure=False):
112
108
if scan .type != 'Idle' :
113
109
line = '%s scan (flags:%d) from %s to %s (ports:%s)'
114
110
else :
115
- tup .append (ip2quad (scan .zombie ))
111
+ tup .append (utils . ip2quad (scan .zombie ))
116
112
line = '%s scan (flags: %d) from %s to %s (ports: %s) using zombie host %s'
117
113
else :
118
114
tup .append (scan .time_avg )
@@ -126,7 +122,7 @@ def log_scan(self, scan, continuation=False, slow_scan=False, unsure=False):
126
122
if scan .type != 'Idle' :
127
123
line = 'Continuation of %s scan from %s to %s (ports:%s)'
128
124
else :
129
- tup .append (ip2quad (scan .zombie ))
125
+ tup .append (utils . ip2quad (scan .zombie ))
130
126
line = 'Continuation of %s scan from %s to %s (ports: %s) using zombie host %s'
131
127
else :
132
128
tup .append (scan .time_avg )
@@ -223,7 +219,7 @@ def inspect_scan(self, scan, slow_scan=False):
223
219
if not recent .is_scan : continue
224
220
if recent .type == 'TCP full-connect' and ((scan .src == recent .dst ) and (scan .dst == recent .src )):
225
221
# Spurious
226
- self .log ("Ignoring spurious TCP full-connect scan from %s" % ' to ' .join (scan_ip2quad (scan )))
222
+ self .log ("Ignoring spurious TCP full-connect scan from %s" % ' to ' .join (utils . scan_ip2quad (scan )))
227
223
not_scan = True
228
224
break
229
225
@@ -234,7 +230,7 @@ def inspect_scan(self, scan, slow_scan=False):
234
230
recent1 = self .recent_scans [- 1 :- 2 :- 1 ]
235
231
for recent in recent1 :
236
232
if recent .type == 'Idle' and scan .src == recent .zombie :
237
- self .log ('Ignoring mis-interpreted syn scan from zombie host %s' % ' to ' .join (scan_ip2quad (scan )))
233
+ self .log ('Ignoring mis-interpreted syn scan from zombie host %s' % ' to ' .join (utils . scan_ip2quad (scan )))
238
234
break
239
235
# Reply from B->A for full-connect scan from A->B
240
236
elif (recent .type == 'reply' and ((scan .src == recent .dst ) and (scan .dst == recent .src ))):
0 commit comments