Skip to content

Commit 58954cd

Browse files
committed
Modified setup.py scanlogger.py using thread, using script as entry
1 parent dc90aae commit 58954cd

File tree

3 files changed

+37
-66
lines changed

3 files changed

+37
-66
lines changed

pyscanlogd3

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
# Entry point script
3+
sudo -H $(which python) scanlogger.py $*

scanlogger.py

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
by scanlogd {http://www.openwall.com/scanlogd} but with
66
added ability to log slow port-scans.
77
8+
This is able to detect all standard TCP/UDP/SCTP scans
9+
documented in the nmap book - https://nmap.org/book/man-port-scanning-techniques.html .
10+
811
Features
912
1013
1. Detects all stealth (half-open) and full-connect scans.
11-
2. Detects Idle scan and logs it correctly using correlation!
12-
3. Detects SCTP scan.
13-
4. Detects slow port-scans also.
14+
2. Detects SCTP scan.
15+
3. Custom thresholding
16+
4. Ignore duplicate scans.
1417
1518
"""
1619

@@ -20,6 +23,9 @@
2023
import socket
2124
import time
2225
import argparse
26+
import threading
27+
import multiprocessing as mp
28+
2329
import hasher
2430
import utils
2531
import entry
@@ -34,9 +40,9 @@
3440
'low': (1, 3)
3541
}
3642

37-
PIDFILE="/var/run/pyscanlogger.pid"
43+
PIDFILE="/var/run/pyscanlogd3.pid"
3844

39-
class ScanLogger:
45+
class ScanLogger(threading.Thread):
4046
""" Port scan detector and logger class """
4147

4248
# TCP flags to scan type mapping
@@ -56,7 +62,7 @@ class ScanLogger:
5662
TH_RST_ACK: TCP_REPLY}
5763

5864
def __init__(self, timeout, threshold, itf=None, maxsize=8192,
59-
daemon=True, ignore_duplicates=False, logfile='/var/log/pyscanlogd3.log'):
65+
ignore_duplicates=False, logfile='/var/log/pyscanlogd3.log'):
6066
self.scans = entry.EntryLog(maxsize)
6167
self.maxsize = maxsize
6268
self.long_scans = entry.EntryLog(maxsize)
@@ -72,8 +78,6 @@ def __init__(self, timeout, threshold, itf=None, maxsize=8192,
7278
self.timeout_l = 3600
7379
# Long-period scan threshold
7480
self.threshold_l = self.threshold/2
75-
# Daemonize ?
76-
self.daemon = daemon
7781
# Interface
7882
self.itf = itf
7983
# Log file
@@ -94,7 +98,8 @@ def __init__(self, timeout, threshold, itf=None, maxsize=8192,
9498
# a scan occurs at most every 5 seconds, this would be 12.
9599
self.recent_scans = timerlist.TimerList(12, 60.0)
96100
self.status_report()
97-
101+
threading.Thread.__init__(self, None)
102+
98103
def status_report(self):
99104
""" Report current configuration before starting """
100105

@@ -116,8 +121,7 @@ def log(self, msg):
116121
self.scanlog.write(line + '\n')
117122
self.scanlog.flush()
118123

119-
if not self.daemon:
120-
print(line, file=sys.stderr)
124+
print(line, file=sys.stderr)
121125

122126
def log_scan(self, scan):
123127
""" Log the scan to file and/or console """
@@ -329,9 +333,6 @@ def inspect_scan(self, scan, slow_scan=False):
329333

330334
def process(self, ts, pkt, decode=None):
331335
""" Process an incoming packet looking for scan signatures """
332-
333-
pkt = decode(pkt)
334-
335336
# Dont process non-IP packets
336337
if not 'ip' in pkt.__dict__:
337338
return
@@ -432,7 +433,10 @@ def process(self, ts, pkt, decode=None):
432433
# print(src, dst, dport, flags)
433434
# print(scan)
434435
self.scans[key] = scan
435-
436+
437+
def run(self):
438+
self.loop()
439+
436440
def loop(self):
437441
""" Run the main logic in a loop listening to packets """
438442

@@ -443,50 +447,15 @@ def loop(self):
443447

444448
try:
445449
print('listening on %s: %s' % (pc.name, pc.filter))
446-
pc.loop(-1, self.process, decode)
450+
for ts, pkt in pc:
451+
self.process(ts, decode(pkt))
447452
except KeyboardInterrupt:
448-
if not self.daemon:
449-
nrecv, ndrop, nifdrop = pc.stats()
450-
print('\n%d packets received by filter' % nrecv)
451-
print('%d packets dropped by kernel' % ndrop)
452-
453-
def run_daemon(self):
454-
# Disconnect from tty
455-
try:
456-
pid = os.fork()
457-
if pid>0:
458-
sys.exit(0)
459-
except OSError as e:
460-
print("fork #1 failed", e, file=sys.stderr)
461-
sys.exit(1)
462-
463-
os.setsid()
464-
os.umask(0)
465-
466-
# Second fork
467-
try:
468-
pid = os.fork()
469-
if pid>0:
470-
open(PIDFILE,'w').write(str(pid))
471-
sys.exit(0)
472-
except OSError as e:
473-
print("fork #2 failed", e, file=sys.stderr)
474-
sys.exit(1)
475-
476-
self.loop()
477-
478-
def run(self):
479-
# If dameon, then create a new thread and wait for it
480-
if self.daemon:
481-
print('Daemonizing...')
482-
self.run_daemon()
483-
else:
484-
# Run in foreground
485-
self.loop()
453+
nrecv, ndrop, nifdrop = pc.stats()
454+
print('\n%d packets received by filter' % nrecv)
455+
print('%d packets dropped by kernel' % ndrop)
486456

487457
def main():
488458
parser = argparse.ArgumentParser(prog='pyscanlogd3', description='pyscanlogd3: Python3 port-scan detection program')
489-
parser.add_argument('-d', '--daemonize', help='Daemonize', action='store_true', default=False)
490459
parser.add_argument('-f', '--logfile',help='File to save logs to',default='/var/log/pyscanlogd3.log')
491460
parser.add_argument('-l','--level',default='medium', choices=levelParams.keys(),
492461
help='Default threshold level for detection')
@@ -497,9 +466,8 @@ def main():
497466

498467
timeout, threshold = levelParams[args.level]
499468
s=ScanLogger(timeout, threshold, itf=args.interface, maxsize=8192,
500-
daemon=args.daemonize, ignore_duplicates=args.ignore_duplicates,
501-
logfile=args.logfile)
502-
s.run()
503-
469+
ignore_duplicates=args.ignore_duplicates, logfile=args.logfile)
470+
s.start()
471+
504472
if __name__ == '__main__':
505473
main()

setup.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
version=version,
1010
description="pyscanlogd3 is a port scan detection tool written in Python3",
1111
long_description="""\
12-
Pyscanlogd is a port scan detection tool written in pure Python. It can
13-
detect most fast port scans and even can detect port-scans of longer
14-
duration upto an hour. It can run as a daemon as well as in the foreground.
12+
Pyscanlogd3 is a port scan detection tool written in pure Python.
13+
It can detect most fast port scans using nmap.
1514
""",
1615
# Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
1716
classifiers=[
@@ -33,10 +32,11 @@
3332
include_package_data = True, # include everything in source control
3433
py_modules = ['scanlogger','timerlist','entry','utils','hasher','constants'],
3534
zip_safe=False,
36-
entry_points="""
37-
[console_scripts]
38-
pyscanlogd3 = scanlogger:main
39-
""",
35+
scripts=['pyscanlogd3'],
36+
#entry_points="""
37+
#[console_scripts]
38+
# pyscanlogd3 = main:main
39+
#""",
4040
install_requires = [
4141
'setuptools',
4242
'dpkt',

0 commit comments

Comments
 (0)