Created
May 7, 2020 22:10
-
-
Save packmad/cf9dfedce50c70410ad5418066d9e2a5 to your computer and use it in GitHub Desktop.
Get SONAME, if exists, of input ELF file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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