-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoFirewall.py
More file actions
234 lines (220 loc) · 9.49 KB
/
Copy pathAutoFirewall.py
File metadata and controls
234 lines (220 loc) · 9.49 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import time, os, requests, subprocess, dns.resolver
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class MultiFileChangeHandler(FileSystemEventHandler):
def __init__(self, files_to_monitor):
self.files_to_monitor = files_to_monitor
self.last_sizes = {file_path: os.path.getsize(file_path) for file_path in files_to_monitor}
def on_modified(self, event):
if event.src_path in self.files_to_monitor:
file_path = event.src_path
current_size = os.path.getsize(file_path)
last_size = self.last_sizes[file_path]
if current_size > last_size:
with open(file_path, 'r') as file:
file.seek(last_size)
changes = file.read()
self.get_ip_info(changes)
self.last_sizes[file_path] = current_size
def get_ip_info(self, changes):
for line in changes.splitlines():
data = list(line.split(" "))
ip = data[0]
try:
method = data[1]
except:
pass
if self.is_valid_ip(ip):
print(f"[FIREWALL] {data}")
data = ""
with open("C:\\path\\to\\your\\blocked_ips.txt", "r") as f:
f.seek(0)
data = f.read()
f.close()
data = list(data.split("\n"))
if str(ip) in data:
print("[FIREWALL] - IP blocked.")
else:
if self.is_ip_malicious(ip):
print(f"[FIREWALL] IP {ip} is listed as malicious!\n")
with open("C:\\path\\to\\your\\blocked_ips.txt", "a") as f:
f.write(f'\n{ip}')
f.close()
self.block_ip(ip)
info = self.fetch_ip_info(ip)
if info:
print(f"IP Information for {ip}:")
try:
print(f"Coordinates: {info['loc']}")
except:
print("Coordinates hidden.")
try:
print(f"Hostname: {info['hostname']}")
except:
print("Hostname hidden. ---------------- SUSPICIOUS IP ----------------")
try:
print(f"City: {info['city']}")
except:
print("City hidden.")
try:
print(f"Region: {info['region']}")
except:
print("Region hidden.")
try:
print(f"Country: {info['country']}")
except:
print("Country hidden.")
try:
print(f"Org: {info['org']}")
except:
print("Organization hidden.")
try:
print(f"Postal code: {info['postal']}")
except:
print("Postal code hidden.")
try:
print(f"Timezone: {info['timezone']}\n")
except:
print("Timezone hidden.")
def is_valid_ip(self, ip):
import re
ip_pattern = re.compile(r'^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$')
return ip_pattern.match(ip) is not None
def fetch_ip_info(self, ip):
try:
response = requests.get(f"http://ipinfo.io/{ip}/json")
if response.status_code == 200:
return response.json()
else:
print(f"Failed to fetch IP info for {ip}: {response.status_code}")
except Exception as e:
print(f"Error fetching IP info for {ip}: {e}")
return None
def is_ip_malicious(self, ip):
rval = False
url = 'https://api.abuseipdb.com/api/v2/check'
rdata = []
headers = {
'Accept': 'application/json',
'Key': 'your_api_key'
}
params = {
'ipAddress': ip,
'maxAgeInDays': 90 # Check reports within the last 90 days
}
try:
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
data = response.json()
if data['data']['abuseConfidenceScore'] > 0:
print("[FIREWALL] Confirmed on AbuseIPdb")
rval = True
elif data['data']['abuseConfidenceScore'] == 0:
print("[FIREWALL] Passed on AbuseIPdb")
else:
print(f"Error checking IP {ip} on AbuseIPdb: {response.status_code}")
except Exception as e:
print(f"Error checking IP {ip} on AbuseIPdb: {e}")
# VirusTotal
url = 'https://www.virustotal.com/vtapi/v2/ip-address/report'
params = {'ip': ip, 'apikey': 'your_api_key'}
try:
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
# Assuming an IP is malicious if detected_urls are present
if 'detected_urls' in data and len(data['detected_urls']) > 0:
print("[FIREWALL] Confirmed on VirusTotal")
rval = True
elif 'detected_urls' not in data and len(data['detected_urls']) == 0:
print("[FIREWALL] Passed on VirusTotal")
else:
print(f"Error checking IP {ip} on VirusTotal: {response.status_code}")
else:
print(f"Error checking IP {ip} on VirusTotal: {response.status_code}")
except Exception as e:
print(f"Error checking IP {ip} on VirusTotal: {e}")
# AlienVault
headers = {
'X-OTX-API-KEY': "your_api_key"
}
url = f'https://otx.alienvault.com/api/v1/indicators/IPv4/{ip}/general'
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
if 'pulse_info' in data and data['pulse_info']['count'] > 0:
print("[FIREWALL] Confirmed on AlienVault")
rval = True
elif 'pulse_info' not in data and data['pulse_info']['count'] == 0:
print("[FIREWALL] Passed on AlienVault")
else:
print(f"Error checking IP {ip} on AlienVault: {response.status_code}")
else:
print(f"Error checking IP {ip} on AlienVault: {response.status_code}")
# SpamHaus
dnsbl_zone = 'zen.spamhaus.org'
try:
query = '.'.join(reversed(ip.split('.'))) + '.' + dnsbl_zone
response = dns.resolver.resolve(query, 'A')
if response:
print("[FIREWALL] Confirmed on SpamHaus")
rval = True
except dns.resolver.NXDOMAIN as e:
print(f"Error checking IP on SpamHaus: {e}")
except dns.resolver.NoAnswer as e:
print(f"Error checking IP on SpamHaus: {e}")
except dns.exception.DNSException as e:
print(f"Error checking IP on SpamHaus: {e}")
url = 'https://api.shodan.io/shodan/host/{ip}?key=your_api_key'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
# Check if there are any vulnerabilities or suspicious activity
if 'vulns' in data or 'data' in data:
print("[FIREWALL] Confirmed on Shodan")
rval = True
else:
print("[FIREWALL] Passed on Shodan")
else:
print(f"Error checking IP {ip} on Shodan: {response.status_code}")
url = f'https://api.greynoise.io/v3/community/{ip}'
headers = {
'key': 'your_api_key'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
if data['classification'] == 'malicious':
print("[FIREWALL] Confirmed on GreyNoise")
rval = True
else:
print("[FIREWALL] Passed on GreyNoise")
else:
print(f"Error checking IP {ip} on GreyNoise: {response.status_code}")
return rval # End of is_ip_malicious()
def block_ip(self, ip):
try:
command = f'netsh advfirewall firewall add rule name="Block IP {ip}" dir=in action=block remoteip={ip}'
subprocess.run(command, shell=True, check=True)
print(f"[FIREWALL] Successfully blocked IP {ip} using Windows Firewall. IP details below.\n")
except subprocess.CalledProcessError as e:
print(f"Failed to block IP {ip}: {e}")
def monitor_files(files_to_monitor):
event_handler = MultiFileChangeHandler(files_to_monitor)
observer = Observer()
for file_path in files_to_monitor:
observer.schedule(event_handler, path=os.path.dirname(file_path), recursive=False)
observer.start()
print(f"Monitoring {', '.join(files_to_monitor)} for changes...")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
files_to_monitor = [
"C:\\path\\your\\ip.log",
"C:\\path\\your\\other\\ip.log"
]
monitor_files(files_to_monitor)