Skip to content

Commit 0957bb9

Browse files
committed
Add local libc database provider for libcdb
1 parent 197f008 commit 0957bb9

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

pwnlib/args.py

+5
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ def STDERR(v):
159159
"""Sends logging to ``stderr`` by default, instead of ``stdout``"""
160160
context.log_console = sys.stderr
161161

162+
def LOCAL_LIBCDB(v):
163+
"""Sets local libcdb path"""
164+
context.defaults['local_libcdb'] = v
165+
162166
hooks = {
163167
'LOG_LEVEL': LOG_LEVEL,
164168
'LOG_FILE': LOG_FILE,
@@ -170,6 +174,7 @@ def STDERR(v):
170174
'NOASLR': NOASLR,
171175
'NOPTRACE': NOPTRACE,
172176
'STDERR': STDERR,
177+
'LOCAL_LIBCDB': LOCAL_LIBCDB,
173178
}
174179

175180
def initialize():

pwnlib/context/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ class ContextType(object):
360360
'endian': 'little',
361361
'gdbinit': "",
362362
'kernel': None,
363+
'local_libcdb': "/var/lib/libc-database",
363364
'log_level': logging.INFO,
364365
'log_file': _devnull(),
365366
'log_console': sys.stdout,
@@ -1071,6 +1072,10 @@ def log_console(self, stream):
10711072
stream = open(stream, 'wt')
10721073
return stream
10731074

1075+
@_validator
1076+
def local_libcdb(self, path):
1077+
return path
1078+
10741079
@property
10751080
def mask(self):
10761081
return (1 << self.bits) - 1

pwnlib/libcdb.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from pwnlib.context import context
1313
from pwnlib.elf import ELF
14+
from pwnlib.filesystem.path import Path
1415
from pwnlib.log import getLogger
1516
from pwnlib.tubes.process import process
1617
from pwnlib.util.fiddling import enhex
@@ -126,7 +127,24 @@ def provider_local_system(hex_encoded_id, hash_type):
126127
return local_libc.data
127128
return None
128129

129-
PROVIDERS = [provider_local_system, provider_libcdb, provider_libc_rip]
130+
# Offline search https://github.com/niklasb/libc-database for hash type
131+
def provider_local_database(hex_encoded_id, hash_type):
132+
assert context.local_libcdb
133+
assert hash_type in HASHES
134+
135+
localdb = Path(context.local_libcdb)
136+
if not localdb.is_dir():
137+
log.warn_once("%s does not exist, please download libc-database first.", str(localdb))
138+
return None
139+
140+
log.debug("Searching local libc database, %s: %s", hash_type, hex_encoded_id)
141+
for libc_path in localdb.rglob("*.so"):
142+
if hex_encoded_id == HASHES[hash_type](libc_path):
143+
return read(libc_path)
144+
145+
return None
146+
147+
PROVIDERS = [provider_local_database, provider_local_system, provider_libcdb, provider_libc_rip]
130148

131149
def search_by_hash(hex_encoded_id, hash_type='build_id', unstrip=True):
132150
assert hash_type in HASHES, hash_type

0 commit comments

Comments
 (0)