|
14 | 14 | from pwnlib.log import getLogger
|
15 | 15 | from pwnlib.tubes.process import process
|
16 | 16 | from pwnlib.util.fiddling import enhex
|
| 17 | +from pwnlib.util.hashes import sha1filehex, sha256filehex, md5filehex |
17 | 18 | from pwnlib.util.misc import read
|
18 | 19 | from pwnlib.util.misc import which
|
19 | 20 | from pwnlib.util.misc import write
|
20 | 21 | from pwnlib.util.web import wget
|
21 | 22 |
|
22 | 23 | log = getLogger(__name__)
|
23 | 24 |
|
24 |
| -HASHES = ['build_id', 'sha1', 'sha256', 'md5'] |
| 25 | +HASHES = { |
| 26 | + 'build_id': lambda path: enhex(ELF(path, checksec=False).buildid or b''), |
| 27 | + 'sha1': sha1filehex, |
| 28 | + 'sha256': sha256filehex, |
| 29 | + 'md5': md5filehex, |
| 30 | +} |
25 | 31 | DEBUGINFOD_SERVERS = [
|
26 | 32 | 'https://debuginfod.elfutils.org/',
|
27 | 33 | ]
|
@@ -104,7 +110,23 @@ def provider_libc_rip(hex_encoded_id, hash_type):
|
104 | 110 | return None
|
105 | 111 | return data
|
106 | 112 |
|
107 |
| -PROVIDERS = [provider_libcdb, provider_libc_rip] |
| 113 | +# Check if the local system libc matches the requested hash. |
| 114 | +def provider_local_system(hex_encoded_id, hash_type): |
| 115 | + if hash_type == 'id': |
| 116 | + return None |
| 117 | + shell_path = os.environ.get('SHELL', None) or '/bin/sh' |
| 118 | + if not os.path.exists(shell_path): |
| 119 | + log.debug('Shell path %r does not exist. Skipping local system libc matching.', shell_path) |
| 120 | + return None |
| 121 | + local_libc = ELF(shell_path, checksec=False).libc |
| 122 | + if not local_libc: |
| 123 | + log.debug('Cannot lookup libc from shell %r. Skipping local system libc matching.', shell_path) |
| 124 | + return None |
| 125 | + if HASHES[hash_type](local_libc.path) == hex_encoded_id: |
| 126 | + return local_libc.data |
| 127 | + return None |
| 128 | + |
| 129 | +PROVIDERS = [provider_local_system, provider_libcdb, provider_libc_rip] |
108 | 130 |
|
109 | 131 | def search_by_hash(hex_encoded_id, hash_type='build_id', unstrip=True):
|
110 | 132 | assert hash_type in HASHES, hash_type
|
|
0 commit comments