Skip to content

Instantly share code, notes, and snippets.

@dipietrantonio
Created November 10, 2022 04:14
Show Gist options
  • Save dipietrantonio/f5cc159be8e14be5636172c5d78121f1 to your computer and use it in GitHub Desktop.
Save dipietrantonio/f5cc159be8e14be5636172c5d78121f1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import subprocess
def process_ldd_out(output):
libs = []
for line in output.splitlines():
line = line.strip()
if line == '' or line == 'statically linked': continue
try:
lib = line[line.index('>') + 1 : line.index('(')].strip()
except ValueError:
lib = line[0 : line.index('(')].strip()
libs.append(lib)
return libs
def get_ldd_libs(library):
ldd = subprocess.run(["ldd", library], capture_output=True)
return process_ldd_out(ldd.stdout.decode('utf-8'))
def search(start):
stack = [start]
visited = set()
while len(stack) > 0:
current = stack.pop()
if current not in visited:
print(current)
visited.add(current)
for child in get_ldd_libs(current):
stack.append(child)
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} <dynamically linked lib/exe>")
exit(0)
search(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment