-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanBlocker.py
178 lines (155 loc) · 6.46 KB
/
scanBlocker.py
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
# Detect portscans of host, automatically blacklist addresses that scan and runs connecting addresses to see if they were similar to blacklisted
from scapy.all import *
import os
import sys
from datetime import datetime
import socket
from collections import OrderedDict
import iptc
import argparse
import logging
# Logic of program:
# Sniff network for (TCP ONLY RIGHT NOW) connections
# Log each connection in the dictionary
# ->Only keep track of N number of connections
# ->If an incoming connection connects to more than the scan threshold of ports
# add it to the blocklist
# -> If in blocklist, make an IPtable rule to prevent future connections
#
#This value is how many port connections an IP connects to on the host before it is considered hostile
SCANTHRESHOLD = 25
logger = logging.getLogger('ScanBlocker')
class Connection():
def __init__(self, pkt):
self.src = pkt[IP].src
self.dst = pkt[IP].dst
self.ports = set()
self.time = pkt.time
self.timeStamp = datetime.fromtimestamp(pkt.time)
self.pkt = pkt
def printTime(t):
return str(t.strftime('%Y-%m-%d %H:%M:%S.%f'))
def timeStamp(pkt):
#print(datetime.fromtimestamp(pkt.time).strftime('%Y-%m-%d %H:%M:%S.%f'))
return datetime.fromtimestamp(pkt.time)
def getFlags(pkt):
F = pkt[TCP].flags
return F
def printFlag(flags,scan):
print("Flag: "+ str(flags))
if scan:
if flags ==0:
print("TCP Null Scan")
if flags == 1:
print("TCP Fin Scan")
if flags == 2:
print("TCP Syn Scan")
if flags == 16:
print("TCP Ack Scan")
if flags == 18:
print("TCP Connect Scan")
blockList = set()
connections = OrderedDict() #a dictionary of connections to the host, maintains insertion order
#k,v = ip, pkt
maxConnections = 50
#_______FIGURE OUT A WAY TO HAVE A MAX SIZE DICT
def addConn(key,conn):
connections[key] = conn
if len(connections) > maxConnections:
connections.popitem(False); #pops the fist item
chain = None
def block(ip):
rule = iptc.Rule()
rule.protocol = "tcp"
rule.src = ip
rule.target = rule.create_target("DROP")
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), "INPUT")
if rule not in chain.rules:
chain.insert_rule(rule)
print("[!]ADDING RULE TO CHAIN")
def process_packet(pkt):
#TCP Connect scan
# Connect 18
# Syn 2
# Fin 1
# Ack 16
if TCP in pkt:
srcIP = pkt[IP].src
dstIP = pkt[IP].dst
srcPrt = pkt[IP].sport
dstPrt = pkt[IP].dport
flags = getFlags(pkt)
if pkt[IP].src in blockList:
rule = iptc.Rule()
rule.src = srcIP
key = srcIP
if key in connections:
connection = connections[key]
connection.timeStamp = timeStamp(pkt)
connection.time = pkt.time
connection.ports.add(dstPrt)
connection.port = pkt[IP].dport
if key not in connections:
connection = Connection(pkt)
connection.port = pkt[IP].dport
addConn(key,connection)
print("----------------Adding Connection-----------------")
print("[+] "+ str(datetime.fromtimestamp(connection.time)) +" "+srcIP+":"+str(srcPrt) +" -> "+dstIP+":"+str(dstPrt))
printFlag(flags,False)
print("--------------------------------------------------")
if len(connections[key].ports) >= SCANTHRESHOLD:
if connections[key] not in blockList:
blockList.add(connections[key])
print("[!]=============Port scan detected================")
printFlag(flags,True)
print(str(datetime.fromtimestamp(connections[key].time))+": "+connections[key].src)
block(str(key))
block(str(key))
print("--------------------------------------------------")
return
def log(iface=None, hostIP=None):
hostIP = str(socket.gethostbyname(socket.gethostname()))
filterStr = "ip and dst host "+hostIP
sniff(filter= filterStr, prn=process_packet, iface = iface)
def main():
#check if user is root/sudo
try:
if os.geteuid() == 0:
parser = argparse.ArgumentParser(description='Detect and block TCP port scans of your host.')
parser.add_argument('-H',
'--hostIP',
action='store',
help="Specify your host inet address. Will default to the inet address of default interface.")
parser.add_argument('-i',
'--interface',
action='store',
help='Specify an interface to sniff for port scans on. Will default to system default.')
parser.add_argument('-c',
'--clear',
action='store_true',
help='Use this flag to clear all blocked IPs before running.')
#parser.add_argument('-d',
# '--daemon',
# action='store_true',
# help='Use this flag to run as daemon proccess and log to file.')
args = parser.parse_args()
if args.clear:
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), "INPUT")
chain.flush()
if args.interface and args.hostIP:
log(args.interface,args.hostIP)
if args.interface:
log(args.interface)
if args.hostIP:
log(None,args.hostIP)
else:
log()
else:
print("[-] Warning: Must run as root.")
sys.exit()
finally:
print("\nIP addresses blocked this session:")
print([x.src for x in blockList])
sys.exit(0)
if __name__ == '__main__':
main()