joernio / joern

Open-source code analysis platform for C/C++/Java/Binary/Javascript/Python/Kotlin based on code property graphs. Discord https://discord.gg/vv4MH284Hc
https://joern.io/
Apache License 2.0
1.97k stars 267 forks source link

Ghidra CPG and renaming #2534

Open psparc82 opened 1 year ago

psparc82 commented 1 year ago

Currently, Joern allows you to create a Ghidra CPG by using the ghidra2cpg tool. This will create a CPG for the provided binary and run Ghidra analysis on the binary automatically.

This skips a vital part of the decompilation process, the user is not able to load the target binary into Ghidra, rename variables/functions and then create the CPG. This results in difficulties using Joern as you only have access to the names Ghidra automatically provided. If you are looking at a stripped binary you end up with all of the functions in the CPG being named FUN_xyz which is difficult to work with.

Allowing the user to first decompile their binary with Ghidra, then use Ghidra to rename variables/functions and then finally creating the CPG from modified decompilation would help massively and make Joern even better than what it already is. Ideally this would allow the user to use Ghidra normally and when they are ready they can convert the Ghidra project into a CPG ready to import into Joern.

One workaround is exporting decompiled code out of Ghidra after renaming what you need to. Then import the code into Joern. This makes you lose functionality but kind of works.

dotCirill commented 8 months ago

From issue in archived ghidra2cpg repo (https://github.com/joernio/ghidra2cpg/issues/19):

  try {
      projectManager = Some(new HeadlessGhidraProjectManager)

      if (inputFile.endsWith(".gpr")) {
        var projname = inputFile.substring(inputFile.lastIndexOf('/')+1, inputFile.lastIndexOf('.'))
        var projdir = inputFile.substring(0, inputFile.lastIndexOf('/'))
        println(s"Found existing Ghidra repo. Loading project $projname from $projdir")
        locator          = new ProjectLocator(projdir, projname)
        project = Some(projectManager.get.openProject(locator, true, true))

I haven't test something like this, but I think it should help.