-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanomalyd.py
55 lines (45 loc) · 1.35 KB
/
anomalyd.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
from scapy.all import *
from datetime import datetime
import binascii
import os
#(filter=....) need tcpdump installed
#.filter(lambda p: "TCP" in p) is
conf.use_pcap = True
def ScannerDetect(filename):
#tcp packages
pkts = rdpcap(filename).filter(lambda p: "TCP" in p)
ipSR = dict() #ip:[#SYN,#SYNACK]
for pkt in pkts:
if(pkt["IP"].src not in ipSR):
ipSR[pkt["IP"].src] = [0,0]
#SYN -- send
if(pkt["TCP"].flags == "S"):
ipSR[pkt["IP"].src][0] += 1
#SYNACK -- receive
if(pkt["TCP"].flags == "SA"):
ipSR[pkt["IP"].dst][1] += 1
#print("Find: ")
for ip in ipSR:
Nosyn = ipSR[ip][0]
Nosynack = 3*ipSR[ip][1]
if(Nosyn > Nosynack or (ipSR[ip][0] == 0 and ipSR[ip][1] == 0)):
print(ip)
filename = "dns-ethereal-trace-1"#"proj3.pcap"
st = datetime.now()
ScannerDetect(filename)
et = datetime.now()
tt = et - st
print("cost: {}".format(tt))
#pkts = rdpcap(filename).filter(lambda p: "TCP" in p) #read all
#print(len(a))
#for i in range(len(pkts)):
#print(i)
#if("TCP" in pkts[i]):
#pkts[i].show()
#break
#print(len(pkts))
# if TCP,IP,Ethernet in pkt
# src: [IP].src
# det: [IP].dst
# type: [IP].proto == tcp
# flags: [TCP].flags