-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
misc.py
361 lines (292 loc) · 11.9 KB
/
misc.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
import base64
import errno
import os
import platform
import re
import socket
import stat
import string
from pwnlib.context import context
from pwnlib.log import getLogger
from pwnlib.util import fiddling
from pwnlib.util import lists
log = getLogger(__name__)
def align(alignment, x):
"""align(alignment, x) -> int
Rounds `x` up to nearest multiple of the `alignment`.
Example:
>>> [align(5, n) for n in range(15)]
[0, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 15, 15, 15, 15]
"""
return ((x + alignment - 1) // alignment) * alignment
def align_down(alignment, x):
"""align_down(alignment, x) -> int
Rounds `x` down to nearest multiple of the `alignment`.
Example:
>>> [align_down(5, n) for n in range(15)]
[0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10]
"""
a = alignment
return (x // a) * a
def binary_ip(host):
"""binary_ip(host) -> str
Resolve host and return IP as four byte string.
Example:
>>> binary_ip("127.0.0.1")
'\\x7f\\x00\\x00\\x01'
"""
return socket.inet_aton(socket.gethostbyname(host))
def size(n, abbrev = 'B', si = False):
"""size(n, abbrev = 'B', si = False) -> str
Convert the length of a bytestream to human readable form.
Arguments:
n(int,iterable): The length to convert to human readable form,
or an object which can have ``len()`` called on it.
abbrev(str): String appended to the size, defaults to ``'B'``.
Example:
>>> size(451)
'451B'
>>> size(1000)
'1000B'
>>> size(1024)
'1.00KB'
>>> size(1024, ' bytes')
'1.00K bytes'
>>> size(1024, si = True)
'1.02KB'
>>> [size(1024 ** n) for n in range(7)]
['1B', '1.00KB', '1.00MB', '1.00GB', '1.00TB', '1.00PB', '1024.00PB']
>>> size([])
'0B'
>>> size([1,2,3])
'3B'
"""
if hasattr(n, '__len__'):
n = len(n)
base = 1000.0 if si else 1024.0
if n < base:
return '%d%s' % (n, abbrev)
for suffix in ['K', 'M', 'G', 'T']:
n /= base
if n < base:
return '%.02f%s%s' % (n, suffix, abbrev)
return '%.02fP%s' % (n / base, abbrev)
KB = 1024
MB = 1024 * KB
GB = 1024 * MB
KiB = 1000
MiB = 1000 * KB
GiB = 1000 * MB
def read(path, count=-1, skip=0):
r"""read(path, count=-1, skip=0) -> str
Open file, return content.
Examples:
>>> read('/proc/self/exe')[:4]
'\x7fELF'
"""
path = os.path.expanduser(os.path.expandvars(path))
with open(path) as fd:
if skip:
fd.seek(skip)
return fd.read(count)
def write(path, data = '', create_dir = False, mode = 'w'):
"""Create new file or truncate existing to zero length and write data."""
path = os.path.expanduser(os.path.expandvars(path))
if create_dir:
path = os.path.realpath(path)
mkdir_p(os.path.dirname(path))
with open(path, mode) as f:
f.write(data)
def which(name, all = False):
"""which(name, flags = os.X_OK, all = False) -> str or str set
Works as the system command ``which``; searches $PATH for ``name`` and
returns a full path if found.
If `all` is :const:`True` the set of all found locations is returned, else
the first occurence or :const:`None` is returned.
Arguments:
`name` (str): The file to search for.
`all` (bool): Whether to return all locations where `name` was found.
Returns:
If `all` is :const:`True` the set of all locations where `name` was found,
else the first location or :const:`None` if not found.
Example:
>>> which('sh')
'/bin/sh'
"""
# If name is a path, do not attempt to resolve it.
if os.path.sep in name:
return name
isroot = os.getuid() == 0
out = set()
try:
path = os.environ['PATH']
except KeyError:
log.exception('Environment variable $PATH is not set')
for p in path.split(os.pathsep):
p = os.path.join(p, name)
if os.access(p, os.X_OK):
st = os.stat(p)
if not stat.S_ISREG(st.st_mode):
continue
# work around this issue: https://bugs.python.org/issue9311
if isroot and not \
st.st_mode & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH):
continue
if all:
out.add(p)
else:
return p
if all:
return out
else:
return None
def run_in_new_terminal(command, terminal = None, args = None):
"""run_in_new_terminal(command, terminal = None) -> None
Run a command in a new terminal.
When ``terminal`` is not set:
- If ``context.terminal`` is set it will be used.
If it is an iterable then ``context.terminal[1:]`` are default arguments.
- If a ``pwntools-terminal`` command exists in ``$PATH``, it is used
- If ``$TERM_PROGRAM`` is set, that is used.
- If X11 is detected (by the presence of the ``$DISPLAY`` environment
variable), ``x-terminal-emulator`` is used.
- If tmux is detected (by the presence of the ``$TMUX`` environment
variable), a new pane will be opened.
Arguments:
command (str): The command to run.
terminal (str): Which terminal to use.
args (list): Arguments to pass to the terminal
Note:
The command is opened with ``/dev/null`` for stdin, stdout, stderr.
Returns:
PID of the new terminal process
"""
if not terminal:
if context.terminal:
terminal = context.terminal[0]
args = context.terminal[1:]
elif which('pwntools-terminal'):
terminal = 'pwntools-terminal'
args = []
elif 'TERM_PROGRAM' in os.environ:
terminal = os.environ['TERM_PROGRAM']
args = []
elif 'DISPLAY' in os.environ and which('x-terminal-emulator'):
terminal = 'x-terminal-emulator'
args = ['-e']
elif 'TMUX' in os.environ and which('tmux'):
terminal = 'tmux'
args = ['splitw']
if not terminal:
log.error('Could not find a terminal binary to use. Set context.terminal to your terminal.')
elif not which(terminal):
log.error('Could not find terminal binary %r. Set context.terminal to your terminal.' % terminal)
if isinstance(args, tuple):
args = list(args)
argv = [which(terminal)] + args
if isinstance(command, str):
if ';' in command:
log.error("Cannot use commands with semicolon. Create a script and invoke that directly.")
argv += [command]
elif isinstance(command, (list, tuple)):
if any(';' in c for c in command):
log.error("Cannot use commands with semicolon. Create a script and invoke that directly.")
argv += list(command)
log.debug("Launching a new terminal: %r" % argv)
pid = os.fork()
if pid == 0:
# Closing the file descriptors makes everything fail under tmux on OSX.
if platform.system() != 'Darwin':
devnull = open(os.devnull, 'rwb')
os.dup2(devnull.fileno(), 0)
os.dup2(devnull.fileno(), 1)
os.dup2(devnull.fileno(), 2)
os.execv(argv[0], argv)
os._exit(1)
return pid
def parse_ldd_output(output):
"""Parses the output from a run of 'ldd' on a binary.
Returns a dictionary of {path: address} for
each library required by the specified binary.
Arguments:
output(str): The output to parse
Example:
>>> sorted(parse_ldd_output('''
... linux-vdso.so.1 => (0x00007fffbf5fe000)
... libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007fe28117f000)
... libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fe280f7b000)
... libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe280bb4000)
... /lib64/ld-linux-x86-64.so.2 (0x00007fe2813dd000)
... ''').keys())
['/lib/x86_64-linux-gnu/libc.so.6', '/lib/x86_64-linux-gnu/libdl.so.2', '/lib/x86_64-linux-gnu/libtinfo.so.5', '/lib64/ld-linux-x86-64.so.2']
"""
expr_linux = re.compile(r'\s(?P<lib>\S?/\S+)\s+\((?P<addr>0x.+)\)')
expr_openbsd = re.compile(r'^\s+(?P<addr>[0-9a-f]+)\s+[0-9a-f]+\s+\S+\s+[01]\s+[0-9]+\s+[0-9]+\s+(?P<lib>\S+)$')
libs = {}
for s in output.split('\n'):
match = expr_linux.search(s) or expr_openbsd.search(s)
if not match:
continue
lib, addr = match.group('lib'), match.group('addr')
libs[lib] = int(addr, 16)
return libs
def mkdir_p(path):
"""Emulates the behavior of ``mkdir -p``."""
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def dealarm_shell(tube):
"""Given a tube which is a shell, dealarm it.
"""
tube.clean()
tube.sendline('which python || echo')
if tube.recvline().startswith('/'):
tube.sendline('''exec python -c "import signal, os; signal.alarm(0); os.execl('$SHELL','')"''')
return tube
tube.sendline('which perl || echo')
if tube.recvline().startswith('/'):
tube.sendline('''exec perl -e "alarm 0; exec '${SHELL:-/bin/sh}'"''')
return tube
return None
def register_sizes(regs, in_sizes):
"""Create dictionaries over register sizes and relations
Given a list of lists of overlapping register names (e.g. ['eax','ax','al','ah']) and a list of input sizes,
it returns the following:
* all_regs : list of all valid registers
* sizes[reg] : the size of reg in bits
* bigger[reg] : list of overlapping registers bigger than reg
* smaller[reg]: list of overlapping registers smaller than reg
Used in i386/AMD64 shellcode, e.g. the mov-shellcode.
Example:
>>> regs = [['eax', 'ax', 'al', 'ah'],['ebx', 'bx', 'bl', 'bh'],
... ['ecx', 'cx', 'cl', 'ch'],
... ['edx', 'dx', 'dl', 'dh'],
... ['edi', 'di'],
... ['esi', 'si'],
... ['ebp', 'bp'],
... ['esp', 'sp'],
... ]
>>> all_regs, sizes, bigger, smaller = register_sizes(regs, [32, 16, 8, 8])
>>> all_regs
['eax', 'ax', 'al', 'ah', 'ebx', 'bx', 'bl', 'bh', 'ecx', 'cx', 'cl', 'ch', 'edx', 'dx', 'dl', 'dh', 'edi', 'di', 'esi', 'si', 'ebp', 'bp', 'esp', 'sp']
>>> sizes
{'ch': 8, 'cl': 8, 'ah': 8, 'edi': 32, 'al': 8, 'cx': 16, 'ebp': 32, 'ax': 16, 'edx': 32, 'ebx': 32, 'esp': 32, 'esi': 32, 'dl': 8, 'dh': 8, 'di': 16, 'bl': 8, 'bh': 8, 'eax': 32, 'bp': 16, 'dx': 16, 'bx': 16, 'ecx': 32, 'sp': 16, 'si': 16}
>>> bigger
{'ch': ['ecx', 'cx', 'ch'], 'cl': ['ecx', 'cx', 'cl'], 'ah': ['eax', 'ax', 'ah'], 'edi': ['edi'], 'al': ['eax', 'ax', 'al'], 'cx': ['ecx', 'cx'], 'ebp': ['ebp'], 'ax': ['eax', 'ax'], 'edx': ['edx'], 'ebx': ['ebx'], 'esp': ['esp'], 'esi': ['esi'], 'dl': ['edx', 'dx', 'dl'], 'dh': ['edx', 'dx', 'dh'], 'di': ['edi', 'di'], 'bl': ['ebx', 'bx', 'bl'], 'bh': ['ebx', 'bx', 'bh'], 'eax': ['eax'], 'bp': ['ebp', 'bp'], 'dx': ['edx', 'dx'], 'bx': ['ebx', 'bx'], 'ecx': ['ecx'], 'sp': ['esp', 'sp'], 'si': ['esi', 'si']}
>>> smaller
{'ch': [], 'cl': [], 'ah': [], 'edi': ['di'], 'al': [], 'cx': ['cl', 'ch'], 'ebp': ['bp'], 'ax': ['al', 'ah'], 'edx': ['dx', 'dl', 'dh'], 'ebx': ['bx', 'bl', 'bh'], 'esp': ['sp'], 'esi': ['si'], 'dl': [], 'dh': [], 'di': [], 'bl': [], 'bh': [], 'eax': ['ax', 'al', 'ah'], 'bp': [], 'dx': ['dl', 'dh'], 'bx': ['bl', 'bh'], 'ecx': ['cx', 'cl', 'ch'], 'sp': [], 'si': []}
"""
sizes = {}
bigger = {}
smaller = {}
for l in regs:
for r, s in zip(l, in_sizes):
sizes[r] = s
for r in l:
bigger[r] = [r_ for r_ in l if sizes[r_] > sizes[r] or r == r_]
smaller[r] = [r_ for r_ in l if sizes[r_] < sizes[r]]
return lists.concat(regs), sizes, bigger, smaller