-
Notifications
You must be signed in to change notification settings - Fork 3
/
botsbegone.py
39 lines (32 loc) · 1.28 KB
/
botsbegone.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
#!/usr/bin/python3
import os
import json
import ipaddress
data_path = '/var/lib/BotsBeGone'
os.system(f'/bin/git -C {os.path.join(data_path, "BotsBeGone")} pull origin')
with open(os.path.join(data_path, 'BotsBeGone', 'ip_addresses.json')) as new_file:
new_data = json.load(new_file)
local_file_path = os.path.join(data_path, 'local', 'ip_addresses.json')
old_data = {}
if os.path.exists(local_file_path):
with open(os.path.join(data_path, 'local', 'ip_addresses.json')) as old_file:
old_data = json.load(old_file)
else:
# this directory needs to exist to save data
os.mkdir(os.path.join(data_path, 'local'))
new_addresses = []
removed_addresses = []
# new ip addresses
for ip_address in new_data:
if ip_address not in old_data:
# roundtrip through the ipaddress library to prevent command injections in case of source repository compromise
i = str(ipaddress.ip_address(ip_address))
os.system(f'ufw insert 1 deny from {i}')
# ip addresses to delete
for ip_address in old_data:
if ip_address not in new_data:
i = str(ipaddress.ip_address(ip_address))
os.system(f'ufw delete deny from {i}')
# save new_data as old
with open(os.path.join(data_path, 'local', 'ip_addresses.json'), 'w') as local_file:
json.dump(new_data, local_file)