Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry failed lookups after one week in libcdb #2323

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ The table below shows which release corresponds to each branch, and what date th
- [#2328][2328] Lookup using $PATHEXT file extensions in `which` on Windows
- [#2189][2189] Explicitly define p64/u64 functions for IDE support
- [#2339][2339] Fix: Allow setting attributes on gdb Breakpoints
- [#2323][2323] Retry failed lookups after one week in libcdb

[2242]: https://github.com/Gallopsled/pwntools/pull/2242
[2277]: https://github.com/Gallopsled/pwntools/pull/2277
Expand All @@ -99,6 +100,7 @@ The table below shows which release corresponds to each branch, and what date th
[2328]: https://github.com/Gallopsled/pwntools/pull/2328
[2189]: https://github.com/Gallopsled/pwntools/pull/2189
[2339]: https://github.com/Gallopsled/pwntools/pull/2339
[2323]: https://github.com/Gallopsled/pwntools/pull/2323

## 4.12.0 (`beta`)

Expand Down
19 changes: 17 additions & 2 deletions pwnlib/libcdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import division

import os
import time
import six
import tempfile

Expand All @@ -29,6 +30,9 @@
urls = os.environ['DEBUGINFOD_URLS'].split(' ')
DEBUGINFOD_SERVERS = urls + DEBUGINFOD_SERVERS

# Retry failed lookups after some time
NEGATIVE_CACHE_EXPIRY = 60 * 60 * 24 * 7 # 1 week

# https://gitlab.com/libcdb/libcdb wasn't updated after 2019,
# but still is a massive database of older libc binaries.
def provider_libcdb(hex_encoded_id, hash_type):
Expand Down Expand Up @@ -109,6 +113,10 @@ def search_by_hash(hex_encoded_id, hash_type='build_id', unstrip=True):
cache, cache_valid = _check_elf_cache('libcdb', hex_encoded_id, hash_type)
if cache_valid:
return cache

# We searched for this buildid before, but didn't find anything.
if cache is None:
return None

# Run through all available libc database providers to see if we have a match.
for provider in PROVIDERS:
Expand Down Expand Up @@ -141,6 +149,10 @@ def _search_debuginfo_by_hash(base_url, hex_encoded_id):
cache, cache_valid = _check_elf_cache('libcdb_dbg', hex_encoded_id, 'build_id')
if cache_valid:
return cache

# We searched for this buildid before, but didn't find anything.
if cache is None:
return None

# Try to find separate debuginfo.
url = '/buildid/{}/debuginfo'.format(hex_encoded_id)
Expand Down Expand Up @@ -191,8 +203,11 @@ def _check_elf_cache(cache_type, hex_encoded_id, hash_type):

data = read(cache)
if not data.startswith(b'\x7FELF'):
log.info_once("Skipping unavailable ELF %s", hex_encoded_id)
return cache, False
# Retry failed lookups after some time
if time.time() > os.path.getmtime(cache) + NEGATIVE_CACHE_EXPIRY:
return cache, False
log.info_once("Skipping invalid cached ELF %s", hex_encoded_id)
return None, False

log.info_once("Using cached data from %r", cache)
return cache, True
Expand Down
Loading