forked from abdurahman-ctis/Hackathor-ClouDek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_endpoints.py
166 lines (133 loc) · 4.9 KB
/
api_endpoints.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
import datetime
import json
from time import time
from urllib.parse import urlparse
from requests import post
import firebase_admin
from dateutil.parser import parse
from firebase_admin import credentials
from firebase_admin import db
from requests import post
from tornado.web import RequestHandler
cred = credentials.Certificate('CERTIFICATE_FILE')
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://ids-hackathor.firebaseio.com/'
})
ref = db.reference('')
intrusion_ref = db.reference('intrusions')
with open('payloads.json', encoding="utf8") as f:
loaded = json.load(f)
XSS = loaded['XSS']
TRAVERS = loaded['TRAVERS']
VIRUSTOTAL = 'API_KEY'
DOMAIN = "bilkent.com"
def send_ref(ip, param, val, type):
ref.push({
"ip": ip,
"type": type,
"query_key": param,
"query_val": val,
"timestamp": time()
})
def not_same_domain(url):
url = urlparse(url).netloc
index = url.find("@")
if index != -1:
url = url[index + 1:]
return url != DOMAIN
class BaseHandler(RequestHandler):
def set_default_headers(self):
print("setting headers!!!")
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "*")
self.set_header('Access-Control-Allow-Methods', '*')
def options(self):
# no body
self.set_status(204)
self.finish()
class AnalyzeQuery(BaseHandler):
def initialize(self, handlers):
self.report = handlers["report"]
async def get(self):
result = db.reference('').get()
self.write(result)
async def post(self):
print("Entered post")
ip = self.request.remote_ip
params = json.loads(self.request.body)
response = post("http://localhost:5000/hello/hikmet", json=params)
response_val = json.loads(response.text)
for i in response_val:
print(i)
send_ref(ip, i['param'], i['val'], i['type'])
self.report({i['type']: {"ip": ip, "param": i['param'], "val": i['val'],
"uid": 99, "confidence": i['confidence']}})
for param, val in params.items():
# CRLF
if '%0d' in val.lower() or '%0a' in val.lower():
send_ref(ip, param, val, 'CRLF')
self.report({"CRLF": {"ip": ip, "param": param, "val": val, "uid": 99}})
# OPEN Redirect
if len([i for i in ['url', 'redirect', 'next'] if i in param.lower()]) > 0 \
and not_same_domain(val):
send_ref(ip, param, val, 'Open Redirect')
self.report({"Redirect": {"ip": ip, "param": param, "val": val, "uid": 99}})
self.write({"Result": "200 Success"})
class ViralUrls(BaseHandler):
def initialize(self, handlers):
self.report = handlers["report"]
async def post(self):
params = json.loads(self.request.body)
malicious = []
for url in params:
post("https://www.virustotal.com/vtapi/v2/url/scan", data={'apikey': VIRUSTOTAL, 'url': url})
res = post("https://www.virustotal.com/vtapi/v2/url/report", data={'apikey': VIRUSTOTAL, 'resource': url})
for i in res.json()['scans'].values():
if i['detected']:
malicious.append(url)
break
self.report({"Viral": malicious})
self.write({"Result": "200 Success"})
class CSRF(BaseHandler):
def initialize(self, handlers):
self.report = handlers["report"]
async def post(self):
params = json.loads(self.request.body)
params['uid'] = '99'
self.report({"CSRF": params})
self.write({"Result": "200 Success"})
class IntrusionDetection(BaseHandler):
def initialize(self, handlers):
self.report = handlers["report"]
async def post(self):
params = json.loads(self.request.body)
print(params)
inter = intrusion_ref.get()
prev = [i for i in inter if inter[i]['path'] == params['path']]
if len(prev) == 0:
prev = None
key = None
else:
key = prev[0]
prev = inter[prev[0]]
print(prev)
if not prev:
cnt = 1
intrusion_ref.push({
"ip": self.request.remote_ip,
"path": params['path'],
"time": datetime.datetime.now().isoformat(),
"cnt": cnt
})
else:
cnt = prev['cnt']
earlier_time = parse(prev['time'])
now = datetime.datetime.now()
print((now - earlier_time).seconds)
if (now - earlier_time).seconds > 5 and cnt > 10:
print("printed")
self.report({"Intrusion": params})
cnt = 1
cnt += 1
intrusion_ref.child(key).update({'cnt': cnt})
self.write({"Result": "200 Success"})