-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathck_blacklist.py
More file actions
70 lines (61 loc) · 1.66 KB
/
Copy pathck_blacklist.py
File metadata and controls
70 lines (61 loc) · 1.66 KB
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
from typing import List, Any
import urllib3
import csv
from scapy.all import *
from scapy.config import conf
conf.use_pcap = True
ips = []
str1 = ''
pcap = rdpcap("capture.cap")
link = "https://isc.sans.edu/block.txt"
mylines = []
iplist: List[Any]=[]
newblocklist=[]
newcaplist=[]
def fatch(link):
http = urllib3.PoolManager()
response = http.request('GET', link)
data = response.data.decode('utf-8')
assert isinstance(data, object)
with open('data3.txt','w') as f:
f.write(data)
def remove_sign():
with open('data3.txt', 'r') as input_file:
with open('data4.txt', 'w') as output_file:
line: str
for line in input_file:
if (line[0] != "#"):
output_file.write(line)
def blacklist():
with open('data4.txt', 'r') as file:
reader: object = csv.DictReader(file, delimiter='\t')
for row in reader:
iplist.append(row['Start'])
return iplist
def remove_last_octet (n: object) -> object:
result = []
i = ""
for row in n:
i = '{}.'.format(row.rsplit('.', 1)[0])
result.append(i)
return result
def capture (pcap):
for pkt in pcap:
if (pkt.haslayer(IP)):
str1 = str(pkt[IP].src)
if str1 not in ips:
ips.append(str1)
return ips
def main():
fatch(link)
remove_sign()
blacklist()
capture(pcap)
newblocklist = remove_last_octet(iplist)
newcaplist = remove_last_octet(ips)
newcaplist.append('89.248.174.') # just to see the lambda work, append a blocked ip
print('Black List IPs: ',list(newblocklist)) # you can append any new block ip by x.y.z.
print('Captured IPs: ',list(newcaplist))
print('Alert: Attacked By: ', list(filter(lambda x: x in newcaplist,newblocklist)))
if __name__ == "__main__":
main()