forked from EmpireProject/Empire
-
-
Notifications
You must be signed in to change notification settings - Fork 585
/
Copy pathsocks.py
60 lines (49 loc) · 1.7 KB
/
socks.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
import base64
import logging
import queue
from socket import socket
from secretsocks import secretsocks
log = logging.getLogger(__name__)
def create_client(main_menu, q, session_id):
log.info("Creating SOCKS client...")
return EmpireSocksClient(main_menu, q, session_id)
def start_client(client, port):
log.info("Starting SOCKS server...")
listener = secretsocks.Listener(client, host="127.0.0.1", port=port)
listener.wait()
class EmpireSocksClient(secretsocks.Client):
# Initialize our data channel
def __init__(self, main_menu, q, session_id):
secretsocks.Client.__init__(self)
self.main_menu = main_menu
self.q = q
self.agent_task_service = main_menu.agenttasksv2
self.session_id = session_id
self.alive = True
self.start()
# Receive data from our data channel and push it to the receive queue
def recv(self):
while self.alive:
try:
data = self.q.get()
self.recvbuf.put(data)
except socket.timeout:
continue
except Exception:
self.alive = False
# Take data from the write queue and send it over our data channel
def write(self):
while self.alive:
try:
data = self.writebuf.get(timeout=10)
if data:
self.agent_task_service.create_task_socks_data(
self.session_id,
base64.b64encode(data).decode("UTF-8"),
)
except queue.Empty:
continue
except Exception:
self.alive = False
def shutdown(self):
self.alive = False