Skip to content

Commit e0cf14a

Browse files
committed
Initial commit:
Implemented Scapy to analyze packets
0 parents  commit e0cf14a

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

scanBlocker.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Detect portscans of host, automatically blacklist addresses that scan and runs connecting addresses to see if they were similar to blacklisted
2+
3+
from scapy.all import *
4+
import os
5+
import sys
6+
from datetime import datetime
7+
def timeStamp():
8+
return "{:%Y-%b-%d %H:%M:%S}".format(datetime.datetime.now())
9+
class Packet:
10+
src = None
11+
dst = None
12+
sport = None
13+
dport = None
14+
timeStamp = None
15+
flags = None
16+
17+
18+
19+
20+
def getFlags(pkt):
21+
F = pkt[TCP].flags
22+
return F
23+
24+
def process_packet(pkt):
25+
#TCP Connect scan
26+
# Connect 18
27+
# Syn 2
28+
# Fin 1
29+
# Ack 16
30+
if TCP in pkt:
31+
srcIP = pkt[IP].src
32+
dstIP = pkt[IP].dst
33+
srcPrt = pkt[IP].sport
34+
dstPrt = pkt[IP].dport
35+
flags = getFlags(pkt)
36+
print("[+] "+srcIP+":"+str(srcPrt) +" -> "+dstIP+":"+str(dstPrt))
37+
print("Flag: "+ str(flags))
38+
if flags == 18:
39+
print("Connect")
40+
if flags == 2:
41+
print("Syn")
42+
if flags == 1:
43+
print("Fin")
44+
if flags == 16:
45+
print("Ack")
46+
pass
47+
48+
def log(iface=None):
49+
sniff(filter="ip",prn=process_packet, iface = iface)
50+
51+
def main():
52+
#check if user is root/sudo
53+
if os.geteuid() == 0:
54+
log()
55+
else:
56+
print("[-] Warning: Must run as root.")
57+
sys.exit()
58+
59+
if __name__ == '__main__':
60+
main()

0 commit comments

Comments
 (0)