-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcloudDog_Webserver_Protection.py
199 lines (181 loc) · 9.7 KB
/
cloudDog_Webserver_Protection.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
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
##################
#Author:Ennio Calderoni
###################
##Webserver_Protection
## v1
#This program is a free software; you can redistribute it and/or modify it under the terms of GPLv2.
import boto3
from datetime import datetime, timedelta
import time
import re
import os
import urllib.parse
import yaml
import logging
from cloudDog_CommonFunctions import block_on_vpc
from cloudDog_CommonFunctions import ipInCidr
from pid import PidFile
logging.basicConfig(filename = os.path.join('logs/', 'cloudDog_Webserver_Protection.log'), format='%(asctime)s %(message)s')
def waf(rule,richiesta,html_status_to_check,treshold,logStreamName,region,html_status_active,ipWhitelist):
ip=richiesta.group(1)
if not ipInCidr(ip,ipWhitelist):
param=richiesta.group(2)
html_status=int(richiesta.group(3))
AlreadyDone=[]
Attacker_Ips=[]
if html_status_active:
if html_status in html_status_to_check: # farlo per stato rallenta troppo
status={
"ip":ip,
"html_status":html_status
}
if ip not in AlreadyDone:
Found_StatusCodes.append(status)
if Found_StatusCodes.count(status) == treshold:
with open('results/ips_results.log', 'a') as waf:
toPrint=region+"; "+logStreamName+"; "+ip+"; the ip triggered an "+str(html_status)+" html status more than "+str(treshold)+" times; html_status_to_check; medium; "+param+"\n"
waf.write(toPrint)
attack={"region":region, "logStreamName":logStreamName,"attacker_ip":ip,"attack":"the ip triggered an "+str(html_status)+" html status more than "+str(treshold)+" times"}
Attacks.append(attack)
AlreadyDone.append(ip)
#Attacker_Ips.append(ip)
regole = compRegole.search(rule)
if (regole):
nome = regole.group(1)
tipo = regole.group(2)
regex= regole.group(3)
risk = regole.group(4)
candidato=True
if candidato:
verdetto1=re.search(regex, urllib.parse.unquote_plus(param))
verdetto2=re.search(regex, param)
if (verdetto1 or verdetto2):
with open('results/ips_results.log', 'a') as waf:
toPrint=region+"; "+logStreamName+"; "+ip+"; "+nome+"; "+tipo+"; "+risk+"; "+param+"\n"
waf.write(toPrint)
attack={"region":region, "logStreamName":logStreamName, "attacker_ip":ip, "attack":nome}
Attacks.append(attack)
return True
else:
return False
else:
return False
def aggregated_Attacker_Detail():
i=0
Attacker_details=[]
AlreadyDone=[]
for a1 in Attacks:
count=1
DifferentAttacks=[]
if a1["attacker_ip"] not in AlreadyDone:
if i != (len(Attacks)-1):
i=i+1
for a2 in range(i,len(Attacks)):
if a1["attacker_ip"]==Attacks[a2]["attacker_ip"]:
count=count+1
#DifferentAttacks.append(a1["attack"])
if Attacks[a2]["attack"] not in DifferentAttacks:
DifferentAttacks.append(Attacks[a2]["attack"])
attacker={"ip":a1["attacker_ip"],"nAttacks":count,"differentAttacks":len(DifferentAttacks)}
Attacker_details.append(attacker)
AlreadyDone.append(a1["attacker_ip"])
return Attacker_details
try:
with PidFile(piddir='pids/',pidname='cloudDog_Webserver_Protection'):
with open("configuration.yaml", "r") as conf:
configuration = yaml.load(conf)
active = configuration["apache_nginx_log"]["active"]
if active:
for regions_nacl in configuration["regions_nacl"]:
region=regions_nacl["region"]
nacl_id=regions_nacl["network_acl_id"]
client = boto3.client('logs',region_name=region)
log_group = configuration["apache_nginx_log"]["log_group"]
time_window = configuration["apache_nginx_log"]["time_window"]
ipWhitelist = configuration["apache_nginx_log"]["ipWhitelist"]
if ipWhitelist is None:
ipWhitelist = []
html_status_active = configuration["apache_nginx_log"]["html_status"]["active"]
if html_status_active:
treshold = configuration["apache_nginx_log"]["html_status"]["treshold"]
html_status_to_check = configuration["apache_nginx_log"]["html_status"]["html_status_to_check"]
print (html_status_to_check)
else:
treshold=0
html_status_to_check =[]
block = configuration["apache_nginx_log"]["block"]["active"]
if block:
block_treshold_Total_Attacks = configuration["apache_nginx_log"]["block"]["treshold_Total_Attacks"]
block_treshold_Different_Attack = configuration["apache_nginx_log"]["block"]["treshold_Different_Attack"]
block_treshold_operator = configuration["apache_nginx_log"]["block"]["treshold_operator"]
block_dedicated_nacl_RuleNumber_min = configuration["apache_nginx_log"]["block"]["dedicated_nacl_RuleNumber_min"]
block_dedicated_nacl_RuleNumber_max = configuration["apache_nginx_log"]["block"]["dedicated_nacl_RuleNumber_max"]
try:
time=int((datetime.today() - timedelta(minutes=time_window)).timestamp())*1000
response = client.filter_log_events(
logGroupName=log_group,
logStreamNamePrefix='i-',
startTime=time,
#endTime=int((datetime.today() - timedelta(minutes=1)).timestamp()),
#limit=10000,
)
#print (response)
except Exception as e:
logging.warning("wrong configuration detected, check the configuartion.yaml")
print(e)
continue
cloudDogRules = ["CloudDog_Rules/wazuh-webrules.txt"]
Attackers_Ip=[]
Found_StatusCodes=[]
Attacks=[]
AlreadyBlocked=[]
if os.path.isfile("results/Attackers_Ip.log"):
with open("results/Attackers_Ip.log", 'r',errors='ignore') as file:
Attackers_Ip=file.read().splitlines()
file.closed
for cdr in cloudDogRules:
with open(cdr, 'r',errors='ignore') as file:
rules=file.readlines()
file.closed
compRegole= re.compile("(.+?);;;(.+?);;;(.+?);;;(.+?)\n")
compRichiesta = re.compile("(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).+?\"(.*?) HTTP.+\s([0-9]{3})\s[0-9]{3}")
print(cdr)
for log in response["events"]:
try:
richiesta=compRichiesta.search(log["message"])
#print(log[1]["value"])
if richiesta:
for rule in rules:
try:
a=waf(rule,richiesta,html_status_to_check,treshold,log["logStreamName"],region,html_status_active,ipWhitelist)
if a:
break
except Exception as e:
print(e)
break
# if html_status_active:
# html_status_attacks()
except Exception as e:
print(e)
break
print("noBlock finito")
if block:
Attacker_details=aggregated_Attacker_Detail()
print(Attacker_details)
if block_treshold_operator == "and":
for attacker in Attacker_details:
if attacker["nAttacks"] >= block_treshold_Total_Attacks and attacker["differentAttacks"]>= block_treshold_Different_Attack:
blockIP=block_on_vpc(nacl_id,block_dedicated_nacl_RuleNumber_min,block_dedicated_nacl_RuleNumber_max,attacker["ip"],region,AlreadyBlocked)
if blockIP:
print(attacker["ip"])
AlreadyBlocked.append(attacker["ip"])
else:
for attacker in Attacker_details:
if attacker["nAttacks"] >= block_treshold_Total_Attacks or attacker["differentAttacks"]>= block_treshold_Different_Attack:
blockIP=block_on_vpc(nacl_id,block_dedicated_nacl_RuleNumber_min,block_dedicated_nacl_RuleNumber_max,attacker["ip"],region,AlreadyBlocked)
if blockIP:
print(attacker["ip"])
AlreadyBlocked.append(attacker["ip"])
except Exception as e:
logging.warning("service failed")
print(e)