-
Notifications
You must be signed in to change notification settings - Fork 18
/
identify.py
executable file
·68 lines (60 loc) · 2.67 KB
/
identify.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
#!/usr/bin/env python
import argparse
import ast
import json
import os
alignment = 4096
db = None
def identify_libc(**kwargs):
"""Identifies the used libc version based on known symbol addresses.
Example usage:
> for libc, symbols in identify_libc(main_call_site=0xf7602a63, printf=None):
print symbols, libc
{'printf':4150483888} ubuntu/libc6-i386_2.19-0ubuntu6.3_amd64/lib32/libc-2.19.so
{'printf':4150483888} ubuntu/libc6-i386_2.19-0ubuntu6.4_amd64/lib32/libc-2.19.so
...
"""
global db
if db is None:
current_dir = os.path.dirname(os.path.realpath(__file__))
db = json.loads(open(os.path.join(current_dir, 'symbols.json')).read())
query_symbols = filter(lambda k: kwargs[k] is None, kwargs.keys())
known_symbols = {key: kwargs[key] for key in kwargs if key not in query_symbols}
for libc in db:
libc_symbols = db[libc]
for key in known_symbols:
# Checks on each known symbol (presence, alignment)
if ((key not in libc_symbols) or
(known_symbols[key] % alignment != libc_symbols[key] % alignment) or
(known_symbols[key] < libc_symbols[key])):
break
else:
# Checks on symbol-pairs (address differences must match)
known_symbol_names = known_symbols.keys()
first = known_symbol_names[0]
if len(known_symbol_names) > 1:
ok = True
for other in known_symbol_names[1:]:
if known_symbols[first] - known_symbols[other] != libc_symbols[first] - libc_symbols[other]:
ok = False
break
if not ok:
continue
# Yielding results
offset = known_symbols[first] - libc_symbols[first]
found_symbols = {symbol: (libc_symbols[symbol] + offset if symbol in libc_symbols else None) for symbol in query_symbols}
yield libc, found_symbols
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Identify the libc version and find unknown symbol addresses based on known symbol addresses.')
parser.add_argument('symbol', nargs='+', metavar='symbol=address|?')
args = parser.parse_args()
symbols = {}
for symbol in args.symbol:
if '=' not in symbol:
symbol += '=?'
[name, address] = symbol.split('=')
symbols[name] = None if (address == '?') else ast.literal_eval(address)
for libc, identified_symbols in identify_libc(**symbols):
for symbol in identified_symbols:
print '%s=0x%016x' % (symbol, identified_symbols[symbol] or 0),
print libc