-
Notifications
You must be signed in to change notification settings - Fork 0
/
blacklist.py
executable file
·56 lines (44 loc) · 1.58 KB
/
blacklist.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
#!/usr/bin/python
from datetime import datetime
from sys import argv
import csv
import re
ban_date = datetime.now().strftime('%Y-%m-%d')
ban_time = datetime.now().strftime('%H:%M:%S')
def ip_validation(ip=None):
val = re.compile("^0*([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.0*"
"([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.0*"
"([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.0*"
"([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])$")
if val.match(ip):
return True
else:
print("Invalid IP: %s" % ip)
def ban_check(ip=None):
if ip:
with open("/etc/fail2ban/blacklist.csv", "rb") as blacklist_file:
blacklist = csv.DictReader(blacklist_file)
for record in blacklist:
if ip in record['IP_ADDRESS']:
print("IP Already banned!")
return True
def ban(ip=None):
if ip:
with open("/etc/fail2ban/blacklist.csv", "ab") as blacklist_file:
fieldnames = ['DATE', 'TIME', 'IP_ADDRESS']
blacklist = csv.DictWriter(blacklist_file,
delimiter=',',
fieldnames=fieldnames)
blacklist.writerow({'DATE': ban_date,
'TIME': ban_time,
'IP_ADDRESS': ip})
def main():
try:
ip = argv[1]
if ip_validation(ip):
if not ban_check(ip=ip):
ban(ip)
except IndexError:
print("Please provide an IP to blacklist.")
if __name__ == '__main__':
main()