Created
May 14, 2019 09:08
-
-
Save 0xTowel/ca23761fc4b0fd239441b7a2667acb6d to your computer and use it in GitHub Desktop.
An example tool to dump resources from a file using radare2
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 | |
"""A small example utility to demonstrate r2pipe scripting | |
by extracting resources from a file. | |
Written as an example for someone on IRC. | |
--Towel, 2019 | |
""" | |
import r2pipe | |
import click | |
def writefile(name, data): | |
"""Write bytes data to a specified file""" | |
with click.open_file(name, 'wb') as fout: | |
fout.write(data) | |
def get_bytes(size, vaddr): | |
"""Return a 'size' length bytes object from data at 'vaddr'""" | |
b = r2.cmdj(f'pcj {size} @ {vaddr}') | |
return bytes(b if b is not None else []) | |
@click.command(context_settings=dict(help_option_names=["-h", "--help"])) | |
@click.argument('input_file', type=click.Path(exists=True)) | |
def cli(input_file): | |
"""An example tool to dump resources from a file using radare2""" | |
global r2 | |
r2 = r2pipe.open(input_file) | |
resources = r2.cmdj('iRj') | |
if not len(resources): | |
click.echo(f'[!] No resources found in {input_file}', err=True) | |
for rsrc in resources: | |
name = f'{rsrc["index"]}_{rsrc["type"]}' | |
writefile(name, get_bytes(rsrc["size"], rsrc["vaddr"])) | |
click.echo(f"[+] '{name}' written.") | |
# If moving to a proper setuptools program, remove this | |
if __name__ == '__main__': | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment