-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrepl_tool.py
executable file
·407 lines (335 loc) · 14.8 KB
/
repl_tool.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env python
# The MIT License (MIT)
#
# Copyright (c) 2017 Ivor Wanders
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import sys
import socket
import base64
import hashlib
import os
# Ensure we have raw input if running in python3
if sys.version_info.major == 3:
raw_input = input
class socketREPL(object):
def __init__(self, ip, port, echo=True):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((ip, port))
self.echo = echo
def write(self, z):
self.sock.sendall(z.encode('utf-8') + b"\n")
if (self.echo):
sys.stdout.write("\033[1m%s\033[0m\n" % z)
def read(self, print_function=None):
try:
b = b""
while True:
# Inefficient, but no one is transferring large amounts of data
# with this system...
d = self.sock.recv(1)
if (len(d) == 0):
# no more data to be read, socket closed?
return b.decode('utf-8')
if d and print_function:
# call the print function if it is set for local echo.
print_function(d.decode('utf-8'))
b += d
if b.endswith(b">>> "):
# We've detected a prompt, return
return b.decode('utf-8')
if b.endswith(b"... "):
# We've detected a prompt, return
return b.decode('utf-8')
except KeyboardInterrupt:
pass
def close(self):
try:
self.sock.shutdown(1)
self.sock.close()
except socket.error as e:
sys.stderr.write("Closing connection failed: {}\n".format(str(e)))
pass
def run_eval(args):
statement = args.statement.replace("\\n", "\n")
c = socketREPL(args.dest, args.port)
c.read(print_function=sys.stdout.write)
for line in statement.split("\n"):
c.write(line)
c.read(print_function=sys.stdout.write)
c.close()
def run_exec(args):
# Put all statements on one line, this is convenient as it requires
# only one read statement afterwards.
p = 'exec_name = "{}";'.format(args.filename)
p += 'execfile(exec_name);'
# check if we are verbose.
if args.verbose:
echo_flag = True
print_function = sys.stdout.write
else:
echo_flag = False
print_function = lambda x: None
c = socketREPL(args.dest, args.port, echo=echo_flag)
c.read(print_function=print_function)
c.write(p)
c.read(print_function=print_function)
c.close()
def run_upload(args):
# Grab the file's data, do this first because if this fails we don't need
# to open the connection.
with open(args.source, 'r') as f:
data = f.read()
# Yay, data acquired, encode it such that it contains no quotes etc.
bytes_data = data.encode("utf-8") if isinstance(data, str) else data
datab64 = base64.b64encode(bytes_data)
# craft the payload
p = 'import base64;'
if args.destination:
destination = args.destination
p += 'import os;'
p += 'dir = os.path.dirname("{}"); '.format(destination)
# Create the dirs if required, if statement on one line to avoid
# multiple prompts to be read.
p += "_ = os.makedirs(dir) if not os.path.isdir(dir) else True;"
else:
destination = args.source
p += 'f = open("{}", "wb");'.format(destination)
p += 'fdata = base64.b64decode("{}");'.format(datab64.decode("utf-8"))
p += 'f.write(fdata);'
p += 'f.close();'
# check if we are verbose.
if (args.verbose):
echo_flag = True
print_function = lambda x: sys.stdout.write(x)
else:
echo_flag = False
print_function = lambda x: None
# create the connection
c = socketREPL(args.dest, args.port, echo=echo_flag)
# Read the banner
c.read(print_function=print_function)
# Drop the payload.
c.write(p)
# Read the prompt after.
c.read(print_function=print_function)
if args.check:
# Calculate the hash of the file at the remote end.
p = "import hashlib;"
p += "print(hashlib.md5(fdata).hexdigest())"
c.write(p)
h = c.read(print_function=print_function).split("\n")[0]
# print(h)
# Calcualte the hash of the file as we have sent it.
x = hashlib.md5(bytes_data)
# print(repr(x), type(x), x.hexdigest(), x)
# print(repr(h), type(h))
# Compare them.
if h == x.hexdigest():
sys.stdout.write("md5 {} of received data"
" matches source data.\n".format(h))
else:
sys.stderr.write("md5 {} of received data"
" does not match source data.\n".format(h))
c.close()
def run_download(args):
# Payload to read data and print the base64 string.
p = 'import base64;'
p += 'f = open("{}", "rb");'.format(args.source)
p += 'data = f.read(); fdata = base64.b64encode(data);'
p += ' f.close();'
p += "print(fdata);" # drop the data!
# check if we are verbose.
if args.verbose:
echo_flag = True
print_function = sys.stdout.write
else:
echo_flag = False
print_function = lambda x: None
# Create connection.
c = socketREPL(args.dest, args.port, echo=echo_flag)
# read banner and prompt
c.read(print_function=print_function)
# drop the payload
c.write(p)
# Read the base64 string and split the prompt from it.
blob = c.read(print_function=print_function).split("\n")[0][2:-1]
# decode the data
data = base64.b64decode(blob)
if (args.check):
# calculate the md5 of the sent data
p = "import hashlib;"
p += "print(hashlib.md5(data).hexdigest())"
c.write(p)
h = c.read(print_function=print_function).split("\n")[0]
# calculate local md5 of the received data
x = hashlib.md5(data)
if h == x.hexdigest():
sys.stdout.write("md5 {} of received data"
" matches source data.\n".format(h))
else:
sys.stderr.write("md5 {} of received data"
" does not match source data.\n".format(h))
c.close()
# ensure destination folder exists, if no destination use basename to local
# folder.
if args.destination:
destination = args.destination
dest_dir = os.path.dirname(destination)
if dest_dir and not os.path.isdir(dest_dir):
os.makedirs(dest_dir)
else:
destination = os.path.basename(args.source)
# Finally, write the data to the destination file.
with open(destination, "wb") as f:
f.write(data)
def run_repl(args):
# print some info..
sys.stdout.write("KeyboardInterrupt is treated locally, two consecutive"
" KeyboardInterrupt \ncloses connection from this side),"
" control+D sends exit() to remote.\n")
# import convenience readline (history) and rlcompleter for tab completion
# of python functions.
import readline
# import rlcompleter
# readline.parse_and_bind("tab: complete")
# create the connection.
c = socketREPL(args.dest, args.port, echo=False)
def read_split():
z = c.read(print_function = lambda x: sys.stdout.write(x))
if (z.endswith(">>> ") or z.endswith("... ")):
# output is already echod as it comes in, but we have to remove
# the prompt as that is handled by raw_input.
sys.stdout.write(chr(8) * 4)
return z[:-4], z[-4:]
else:
return z, ""
interrupt_counter = 0
repling = True
# Read the prompt and banner
output, prompt = read_split()
while repling:
try: # outer loop for keyboard interrupt (control+C)
try: # try raw_input to catch control+D
line = raw_input(prompt)
except EOFError:
# got control+D, close everything gracefully.
repling = False
line = "exit()" # interpret as if exit() was typed.
sys.stdout.write("exit()\n") # ensure it shows in stdout.
# Reset the consecutive control+C counter.
interrupt_counter = 0
# Finally, drop the typed instruction into the socket.
c.write(line)
# read any output, and the prompt.
output, prompt = read_split()
except KeyboardInterrupt:
# increase consecutive control+C counter.
interrupt_counter += 1
sys.stdout.write("\n")
if (interrupt_counter > 1):
sys.stdout.write("Local KeyboardInterrupt,"
" closing connection.\n")
break
c.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="A helper script to interact"
" with a Python REPL exposed over tcp")
parser.add_argument('-d', '--dest', default=None,
help="Hostname or ip of target running REPL. Defaults"
" to 127.0.0.1, will use environment value of "
"REPL_HOST if set.")
parser.add_argument('-p', '--port', default=None, type=int,
help="Port of target turnning REPL. Defaults"
" to 1337, will use environment value of REPL_PORT if"
" set.")
subparsers = parser.add_subparsers(dest="command")
eval_description = ("This evaluates the one statement that is provided to"
" it. Any \\n occurances are replace by non escaped "
"newlines and the statement is executed line by line. "
"Output is read between each line. The statement may "
"span multiple lines, basically it's the same as "
"pasting this statement into a open REPL session.")
eval_parser = subparsers.add_parser('evaluate',
help="Evaluate a statement",
description=eval_description)
eval_parser.add_argument('statement', help="The string to evaluate, '\n'"
" is replaced by newline and the statement is "
"executed & read line by line")
eval_parser.set_defaults(func=run_eval)
upload_description = ("This allows uploading a file to the remote REPL "
" from the local computer. It overwrites the "
" destination without prompt.")
upload_parser = subparsers.add_parser('upload', help="Upload a file",
description=upload_description)
upload_parser.add_argument('source')
upload_parser.add_argument('-v', default=False, action="store_true",
dest="verbose", help="print all interaction")
upload_parser.add_argument('--no-check', default=True,
action="store_false", dest="check",
help="do not perform md5 transfer check")
upload_parser.add_argument('destination', type=str, default=None,
help="defaults to source path", nargs="?")
upload_parser.set_defaults(func=run_upload)
execute_description = ("This allows remote execution of a script.")
execute_parser = subparsers.add_parser('execute', help="Execute a file",
description=execute_description)
execute_parser.add_argument('filename')
execute_parser.add_argument('-q', default=True, action="store_false",
dest="verbose",
help="Inhibit printing all interaction")
execute_parser.set_defaults(func=run_exec)
download_description = ("This allows downloading a file from the remote "
" REPL to the local computer. It overwrites the "
" destination without prompt.")
download_parser = subparsers.add_parser('download', help="Download a file",
description=download_description)
download_parser.add_argument('source')
download_parser.add_argument('-v', default=False, action="store_true",
dest="verbose", help="print all interaction")
download_parser.add_argument('--no-check', default=True,
action="store_false", dest="check",
help="do not perform md5 transfer check")
download_parser.add_argument('destination', type=str, default=None,
help="defaults to source basename", nargs="?")
download_parser.set_defaults(func=run_download)
repl_description = ("This REPL command is slightly more convenient than "
"connecting to the socketserverREPL with netcat. "
" A command history is made available by using the"
" readline module.")
repl_parser = subparsers.add_parser('repl', help="Drop into a repl",
description=repl_description)
repl_parser.set_defaults(func=run_repl)
args = parser.parse_args()
if ("REPL_HOST" in os.environ) and args.dest is None:
args.dest = os.environ["REPL_HOST"]
if args.dest is None: # still None, go for fallback.
args.dest = "127.0.0.1"
if "REPL_PORT" in os.environ and args.port is None:
args.port = int(os.environ["REPL_PORT"])
if (args.port is None): # Still None, go for fallback.
args.port = 1337
# no command
if (args.command is None):
parser.print_help()
parser.exit()
sys.exit(1)
args.func(args)
sys.exit()