-
Notifications
You must be signed in to change notification settings - Fork 1
/
attack_handler.py
51 lines (47 loc) · 2.28 KB
/
attack_handler.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
import requests
import json
import webhook
# initally saves the timestamp of the last attack
def time_stamp(auth_token, ip_list):
# creating header for authentication
header = {"Authorization": "Bearer " + auth_token}
last_attack_time = {}
# iterating over every serviceID and saving the last attack
for i in ip_list:
url = "https://api.tube-hosting.com/ips/" + str(i) + "/ddos/incidents"
response = requests.get(url, headers=header)
# checking if array is empty to prevent JSON error
if len(response.text) >= 3:
# convert JSON
attack_list = json.loads(response.text)
j = len(attack_list) - 1
last_attack_time[i] = float(attack_list[j]['time'].translate({ord(i): None for i in '-:TZ'}))
else:
last_attack_time[i] = None
return last_attack_time
# grab latest attacks from api
def check(auth_token, webhook_url, ip_list, last_attack_time):
# creating header for authentication
header = {"Authorization": "Bearer " + auth_token}
# iterating over every serviceID and checking for attacks
for i in ip_list:
url = "https://api.tube-hosting.com/ips/" + str(i) + "/ddos/incidents"
response = requests.get(url, headers=header)
# checking if array is empty to prevent JSON error
if len(response.text) >= 3:
# convert JSON
attack_list = json.loads(response.text)
# defining last attack to check wether there has been a newer attack
last_attack = last_attack_time[i]
# defining latest found attack
j = len(attack_list) - 1
last_found_attack = attack_list[j]
# removing unwanted chars and casting to float. Comparing it to the last timestamp.
if (last_attack is None and last_found_attack is not None) or float(
last_found_attack['time'].translate({ord(i): None for i in '-:TZ'})) > float(last_attack):
print(last_found_attack)
# sending found attack using webhook method
webhook.send(webhook_url, last_found_attack)
# setting new timestamp
last_attack_time[i] = float(last_found_attack['time'].translate({ord(i): None for i in '-:TZ'}))
return last_attack_time