NationalSecurityAgency / ghidra

Ghidra is a software reverse engineering (SRE) framework
https://www.nsa.gov/ghidra
Apache License 2.0
49.09k stars 5.65k forks source link

support python typing in pre/post script when using analyzeHeadless #6574

Closed axel7083 closed 1 month ago

axel7083 commented 1 month ago

Is your feature request related to a problem? Please describe.

I am using the analyzeHeadless, and provide a python script through the -postScript, however when trying to annotate my code with argument type, and return type for function it will raise an error.

Here is an example of a function I created

from typing import Optional

# ghidra imports
import ghidra.program.database.function.FunctionDB

# Note that multiple functions can share the same name, so this
# function will only return the first one
def find_function_by_name(name: str) -> Optional[FunctionDB]:
    funcs = getGlobalFunctions(name)
    print("Found {} function(s) with the name '{}'".format(len(funcs), name))
    if len(funcs) > 0:
        return funcs[0]
    return None

But once running I am getting the following error

INFO  SCRIPT: C:\Users\....\scripts\ghidra.py (HeadlessAnalyzer)  
  File "C:\Users\axels\OneDrive\Bureau\SM64DS\sm64-unpack\scripts\ghidra.py", line 8
    def find_function_by_name(name: str) -> Optional[FunctionDB]:
                                 ^
SyntaxError: mismatched input ':' expecting RPAREN

Describe the solution you'd like

Hopefully a support for running script containing python typing.

Describe alternatives you've considered

Remove the typing makes everything works.

Additional context Add any other context or screenshots about the feature request here.

ryanmkurtz commented 1 month ago

Are you just using Ghidra's built-in Jython support (which only supports Python 2), or are you using a 3rd party extension like Pyhidra or Ghidrathon? Typing is not supported in Python 2.

axel7083 commented 1 month ago

Are you just using Ghidra's built-in Jython support (which only supports Python 2), or are you using a 3rd party extension like Pyhidra or Ghidrathon? Typing is not supported in Python 2.

You are right, when using it, I thought I was using some kind of python 3 version, but apparently Jython 2. Closing as cannot be solved