Skip to content

Commit a3880a4

Browse files
committed
Add offline parameter
1 parent 3b4b261 commit a3880a4

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

pwnlib/libcdb.py

+26-11
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,12 @@ def provider_local_database(hex_encoded_id, hash_type):
143143

144144
return None
145145

146-
PROVIDERS = [provider_local_system, provider_local_database, provider_libcdb, provider_libc_rip]
146+
PROVIDERS = {
147+
"offline": [provider_local_system, provider_local_database],
148+
"online": [provider_libcdb, provider_libc_rip]
149+
}
147150

148-
def search_by_hash(hex_encoded_id, hash_type='build_id', unstrip=True):
151+
def search_by_hash(hex_encoded_id, hash_type='build_id', unstrip=True, offline=False):
149152
assert hash_type in HASHES, hash_type
150153

151154
# Ensure that the libcdb cache directory exists
@@ -157,8 +160,12 @@ def search_by_hash(hex_encoded_id, hash_type='build_id', unstrip=True):
157160
if cache is None:
158161
return None
159162

163+
providers = PROVIDERS["offline"]
164+
if not offline:
165+
providers += PROVIDERS["online"]
166+
160167
# Run through all available libc database providers to see if we have a match.
161-
for provider in PROVIDERS:
168+
for provider in providers:
162169
data = provider(hex_encoded_id, hash_type)
163170
if data and data.startswith(b'\x7FELF'):
164171
break
@@ -603,7 +610,7 @@ def search_by_symbol_offsets(symbols, select_index=None, unstrip=True, return_as
603610
selected_libc = _handle_multiple_matching_libcs(matching_libcs)
604611
return search_by_build_id(selected_libc['buildid'], unstrip=unstrip)
605612

606-
def search_by_build_id(hex_encoded_id, unstrip=True):
613+
def search_by_build_id(hex_encoded_id, unstrip=True, offline=False):
607614
"""
608615
Given a hex-encoded Build ID, attempt to download a matching libc from libcdb.
609616
@@ -612,6 +619,8 @@ def search_by_build_id(hex_encoded_id, unstrip=True):
612619
Hex-encoded Build ID (e.g. 'ABCDEF...') of the library
613620
unstrip(bool):
614621
Try to fetch debug info for the libc and apply it to the downloaded file.
622+
offline(bool):
623+
Enable offline search mode, which will disable online providers.
615624
616625
Returns:
617626
Path to the downloaded library on disk, or :const:`None`.
@@ -627,9 +636,9 @@ def search_by_build_id(hex_encoded_id, unstrip=True):
627636
>>> hex(ELF(filename).symbols.read)
628637
'0xeef40'
629638
"""
630-
return search_by_hash(hex_encoded_id, 'build_id', unstrip)
639+
return search_by_hash(hex_encoded_id, 'build_id', unstrip, offline)
631640

632-
def search_by_md5(hex_encoded_id, unstrip=True):
641+
def search_by_md5(hex_encoded_id, unstrip=True, offline=False):
633642
"""
634643
Given a hex-encoded md5sum, attempt to download a matching libc from libcdb.
635644
@@ -638,6 +647,8 @@ def search_by_md5(hex_encoded_id, unstrip=True):
638647
Hex-encoded md5sum (e.g. 'ABCDEF...') of the library
639648
unstrip(bool):
640649
Try to fetch debug info for the libc and apply it to the downloaded file.
650+
offline(bool):
651+
Enable offline search mode, which will disable online providers.
641652
642653
Returns:
643654
Path to the downloaded library on disk, or :const:`None`.
@@ -653,9 +664,9 @@ def search_by_md5(hex_encoded_id, unstrip=True):
653664
>>> hex(ELF(filename).symbols.read)
654665
'0xeef40'
655666
"""
656-
return search_by_hash(hex_encoded_id, 'md5', unstrip)
667+
return search_by_hash(hex_encoded_id, 'md5', unstrip, offline)
657668

658-
def search_by_sha1(hex_encoded_id, unstrip=True):
669+
def search_by_sha1(hex_encoded_id, unstrip=True, offline=False):
659670
"""
660671
Given a hex-encoded sha1, attempt to download a matching libc from libcdb.
661672
@@ -664,6 +675,8 @@ def search_by_sha1(hex_encoded_id, unstrip=True):
664675
Hex-encoded sha1sum (e.g. 'ABCDEF...') of the library
665676
unstrip(bool):
666677
Try to fetch debug info for the libc and apply it to the downloaded file.
678+
offline(bool):
679+
Enable offline search mode, which will disable online providers.
667680
668681
Returns:
669682
Path to the downloaded library on disk, or :const:`None`.
@@ -679,10 +692,10 @@ def search_by_sha1(hex_encoded_id, unstrip=True):
679692
>>> hex(ELF(filename).symbols.read)
680693
'0xeef40'
681694
"""
682-
return search_by_hash(hex_encoded_id, 'sha1', unstrip)
695+
return search_by_hash(hex_encoded_id, 'sha1', unstrip, offline)
683696

684697

685-
def search_by_sha256(hex_encoded_id, unstrip=True):
698+
def search_by_sha256(hex_encoded_id, unstrip=True, offline=False):
686699
"""
687700
Given a hex-encoded sha256, attempt to download a matching libc from libcdb.
688701
@@ -691,6 +704,8 @@ def search_by_sha256(hex_encoded_id, unstrip=True):
691704
Hex-encoded sha256sum (e.g. 'ABCDEF...') of the library
692705
unstrip(bool):
693706
Try to fetch debug info for the libc and apply it to the downloaded file.
707+
offline(bool):
708+
Enable offline search mode, which will disable online providers.
694709
695710
Returns:
696711
Path to the downloaded library on disk, or :const:`None`.
@@ -706,7 +721,7 @@ def search_by_sha256(hex_encoded_id, unstrip=True):
706721
>>> hex(ELF(filename).symbols.read)
707722
'0xeef40'
708723
"""
709-
return search_by_hash(hex_encoded_id, 'sha256', unstrip)
724+
return search_by_hash(hex_encoded_id, 'sha256', unstrip, offline)
710725

711726

712727

0 commit comments

Comments
 (0)