Skip to content

Instantly share code, notes, and snippets.

@affilares
Forked from liba2k/ghidra.py
Created April 6, 2024 04:21
Show Gist options
  • Save affilares/49d32da39a063da0970d0db2abbd9186 to your computer and use it in GitHub Desktop.
Save affilares/49d32da39a063da0970d0db2abbd9186 to your computer and use it in GitHub Desktop.

Revisions

  1. @liba2k liba2k revised this gist Jun 25, 2023. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion ghidra.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    #!/usr/bin/env python3

    import os
    import sys
    import click
    @@ -9,7 +10,7 @@
    from time import sleep

    PROJECT_DIRECTORY = '/tmp'
    GHIDRA_PATH = '/Applications/ghidra_10.0.2_PUBLIC/'
    GHIDRA_PATH = '/Applications/ghidra_10.3_PUBLIC/'

    def uniquify(path, sep = ''):
    def name_sequence():
    @@ -24,6 +25,7 @@ def name_sequence():
    dirname, basename = os.path.split(path)
    filename, ext = os.path.splitext(basename)
    fd, filename = tempfile.mkstemp(dir = dirname, prefix = filename, suffix = ext)
    os.remove(filename)
    tempfile._name_sequence = orig
    return filename

  2. @liba2k liba2k revised this gist Dec 21, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion ghidra.py
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,6 @@ def shouldRun():

    @click.command()
    @click.argument('filename', type=click.Path(exists=True))
    # @click.option('--verbose', '-v', help="Print more output.")
    @click.option('-t', '--temp', 'temp', is_flag=True)
    def main(filename, temp):
    if os.path.isdir(filename):
  3. @liba2k liba2k created this gist Dec 21, 2021.
    66 changes: 66 additions & 0 deletions ghidra.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    #!/usr/bin/env python3
    import os
    import sys
    import click
    import subprocess
    import tempfile
    import itertools as IT
    import select
    from time import sleep

    PROJECT_DIRECTORY = '/tmp'
    GHIDRA_PATH = '/Applications/ghidra_10.0.2_PUBLIC/'

    def uniquify(path, sep = ''):
    def name_sequence():
    count = IT.count()
    yield ''
    while True:
    yield '{s}_{n:d}'.format(s = sep, n = next(count))
    orig = tempfile._name_sequence
    with tempfile._once_lock:
    tempfile._name_sequence = name_sequence()
    path = os.path.normpath(path)
    dirname, basename = os.path.split(path)
    filename, ext = os.path.splitext(basename)
    fd, filename = tempfile.mkstemp(dir = dirname, prefix = filename, suffix = ext)
    tempfile._name_sequence = orig
    return filename

    def shouldRun():
    click.secho('Will run analysis in 3 seconds, press any key to cancel', fg='green')
    i, o, e = select.select( [sys.stdin], [], [], 3 )

    if (i):
    return False
    else:
    return True

    @click.command()
    @click.argument('filename', type=click.Path(exists=True))
    # @click.option('--verbose', '-v', help="Print more output.")
    @click.option('-t', '--temp', 'temp', is_flag=True)
    def main(filename, temp):
    if os.path.isdir(filename):
    return os.system(f'{GHIDRA_PATH}ghidraRun')
    if '.gpr' in filename:
    os.system(f'{GHIDRA_PATH}ghidraRun "{os.path.abspath(filename)}"')
    return
    if temp:
    proj_file = uniquify(os.path.join(PROJECT_DIRECTORY, os.path.basename(filename) + '.gpr'))
    out_dir = PROJECT_DIRECTORY
    else:
    proj_file = uniquify(filename + '.gpr')
    out_dir = os.path.dirname(filename)
    out_dir = out_dir if out_dir != '' else '.'
    proj_name = os.path.splitext(os.path.basename(proj_file))[0]
    file_output = subprocess.check_output(f'file "{filename}"', shell=True).decode('utf8')
    click.secho(file_output, fg='yellow')
    r = shouldRun()
    if r:
    os.system(f'{GHIDRA_PATH}support/analyzeHeadless {out_dir} "{proj_name}" -import "{filename}"')
    os.system(f'{GHIDRA_PATH}ghidraRun "{proj_file}"')


    if __name__ == '__main__':
    main()