1
+ import requests
2
+ import os
3
+ import json
4
+ import subprocess
5
+
6
+ logfile = "/var/log/apache2/access.log"
7
+ #logfile = "access.log"
8
+
9
+ processed_ips = []
10
+
11
+ ips = []
12
+ with open (logfile ) as f :
13
+ for line in f :
14
+ ips .append (line .split ()[0 ])
15
+
16
+ ips = list (set (ips ))
17
+
18
+ response_types = [
19
+ "known-apache-attack"
20
+ "known-bot" ,
21
+ "known-brute-force-attack" ,
22
+ "known-email-attack" ,
23
+ "known-imap-attack" ,
24
+ "known-ssh-attack" ,
25
+ "known-voip-attack" ,
26
+ "web-brute-force"
27
+ ]
28
+
29
+ def process_ip (ip , flags ):
30
+ global processed_ips
31
+ for flag in flags :
32
+ if flag == "known-bot" or flag == "known-brute-force-attack" or flag == "web-brute-force" or flag not in response_types :
33
+ # Block from accessing the entire network
34
+ ip_tables_cmd = f"sudo iptables -A INPUT -s { ip } -j DROP"
35
+ os .system (ip_tables_cmd )
36
+ print (f"Blocked { ip } from accessing the network" )
37
+ elif flag == "known-apache-attack" :
38
+ # Change firewall settings to block this IP from accessing port 80 and 443
39
+ ip_tables_cmds = [
40
+ f"sudo iptables -A INPUT -p tcp --dport 80 -s { ip } -j DROP" ,
41
+ f"sudo iptables -A INPUT -p tcp --dport 443 -s { ip } -j DROP"
42
+ ]
43
+ #ip_tables_cmd_to_unblock = f"sudo iptables -D INPUT -p tcp --dport 80 -s {ip} -j DROP"
44
+ #os.system("sudo iptables -F")
45
+ for ip_tables_cmd in ip_tables_cmds :
46
+ os .system (ip_tables_cmd )
47
+ print (f"Blocked { ip } from accessing port 80 and 443" )
48
+ elif flag == "known-email-attack" :
49
+ # Block from accessing email default ports (25, 465, 587)
50
+ ip_tables_cmds = [
51
+ f"sudo iptables -A INPUT -p tcp --dport 25 -s { ip } -j DROP" ,
52
+ f"sudo iptables -A INPUT -p tcp --dport 465 -s { ip } -j DROP" ,
53
+ f"sudo iptables -A INPUT -p tcp --dport 587 -s { ip } -j DROP"
54
+ ]
55
+ for ip_tables_cmd in ip_tables_cmds :
56
+ os .system (ip_tables_cmd )
57
+ print (f"Blocked { ip } from accessing email default ports (25, 465, 587)" )
58
+ elif flag == "known-imap-attack" :
59
+ # Block from accessing imap default ports (143, 993)
60
+ ip_tables_cmds = [
61
+ f"sudo iptables -A INPUT -p tcp --dport 143 -s { ip } -j DROP" ,
62
+ f"sudo iptables -A INPUT -p tcp --dport 993 -s { ip } -j DROP"
63
+ ]
64
+ for ip_tables_cmd in ip_tables_cmds :
65
+ os .system (ip_tables_cmd )
66
+ print (f"Blocked { ip } from accessing imap default ports (143, 993)" )
67
+ elif flag == "known-ssh-attack" :
68
+ # Block from accessing ssh default port (22)
69
+ ip_tables_cmd = f"sudo iptables -A INPUT -p tcp --dport 22 -s { ip } -j DROP"
70
+ os .system (ip_tables_cmd )
71
+ print (f"Blocked { ip } from accessing ssh default port (22)" )
72
+ elif flag == "known-voip-attack" :
73
+ # Block from accessing voip default ports (5060, 5061)
74
+ ip_tables_cmds = [
75
+ f"sudo iptables -A INPUT -p tcp --dport 5060 -s { ip } -j DROP" ,
76
+ f"sudo iptables -A INPUT -p tcp --dport 5061 -s { ip } -j DROP"
77
+ ]
78
+ for ip_tables_cmd in ip_tables_cmds :
79
+ os .system (ip_tables_cmd )
80
+ print (f"Blocked { ip } from accessing voip default ports (5060, 5061)" )
81
+
82
+ if __name__ == "__main__" :
83
+ with open ("processed_ips.txt" , "r" ) as f :
84
+ processed_ips = f .read ().splitlines ()
85
+
86
+ while True :
87
+ ips = []
88
+ with open (logfile ) as f :
89
+ for line in f :
90
+ ips .append (line .split ()[0 ])
91
+ ips = list (set (ips ))
92
+
93
+ for ip in ips :
94
+ #ip = '23.129.64.227'
95
+ if ip not in processed_ips :
96
+ response = requests .get (
97
+ "https://84h9dq7p3c.execute-api.eu-west-1.amazonaws.com/live/GetIPReputation" ,
98
+ params = {"ip" : ip },
99
+ headers = {"x-api-key" : "RcXcE6oFZb8Mn8bhNeDXT1qwaFZEOGKHag8ivBcB" },
100
+ ).text
101
+ #ip_ = '51.89.153.112'
102
+ #response = "{'ip': '{ip_}', 'ttps': ['known-ssh-attack', 'known-apache-attack']}"
103
+ flags = eval (response )["ttps" ]
104
+ process_ip (ip , flags )
105
+ processed_ips .append (ip )
106
+ with open ("processed_ips.txt" , "a" ) as f :
107
+ f .write (ip + "\n " )
0 commit comments