Skip to content

Instantly share code, notes, and snippets.

@packmad
Created May 7, 2020 22:10
Show Gist options
  • Save packmad/cf9dfedce50c70410ad5418066d9e2a5 to your computer and use it in GitHub Desktop.
Save packmad/cf9dfedce50c70410ad5418066d9e2a5 to your computer and use it in GitHub Desktop.
Get SONAME, if exists, of input ELF file
#!/usr/bin/env python3
import r2pipe
import sys
from os.path import isfile
from typing import Optional
def get_soname(file_path) -> Optional[str]:
r = r2pipe.open(file_path)
barch: int = r.cmdj('iAj')['bins'][0]['bits']
dynstr_addr: Optional[int] = None
dynamic_addr: Optional[int] = None
for section in r.cmdj('iSj'):
if section['name'] == '.dynamic':
dynamic_addr = section['vaddr']
if section['name'] == '.dynstr':
dynstr_addr = section['vaddr']
soname: Optional[str] = None
if dynamic_addr is not None and dynstr_addr is not None:
s: int = dynamic_addr
while True:
d_tag: int = int(r.cmd(f'pb 0x8@{s}'), 2)
if d_tag == 0xe: # DT_SONAME
if barch == 64:
s += 0x8
wsz = 'q'
else:
s += 0x4
wsz = 'w'
offset: int = r.cmdj(f'px{wsz}j@{s}')[0]
soname = r.cmd(f'psz@{dynstr_addr + offset}')
break
if d_tag == 0:
break
if barch == 64:
s += 0x10
else:
s += 0x8
return soname
if __name__ == '__main__':
in_file = sys.argv[1]
assert isfile(in_file)
print('SONAME=', get_soname(in_file))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment