Ghidra Scripting: Annotating Linux system calls
Tags: reverseengineering, security
Published: 2019-10-13 14:24:35

---
title: Ghidra Scripting: Annotating Linux system calls
published: true 
tags: reverseengineering, security
---

I had some fun this weekend messing around with [Ghidra](https://ghidra-sre.org/). Having such a powerful tool for free is truly a game changer.

To start scripting in Ghidra, I downloaded the latest Eclipse for Java Developers [Version: 2019-09 R (4.13.0)](https://www.eclipse.org/downloads/packages/release/2019-09/r/eclipse-ide-java-developers), Ghidra and [Open JDK](https://jdk.java.net/), I believe any JDK version 11+ will work.

After downloading the JDK, extract the zip, put it somewhere and modify your PATH and JAVA_HOME environment variables to point to it:

```bash
export JAVA_HOME="/home/denis/jdk-13"
export PATH="/home/denis/jdk-13/bin:$PATH"
```

Next, you should start Ghidra in order to associate the JDK with it, now close it, then start Eclipse. In Eclipse, install the GhidraDev extension from the archive which is found in ghidra_9.1-BETA_DEV/Extensions/Eclipse.

After installing GhidraDev, you may now create a new Ghidra Scripts project.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/n97uqsbrar7b64kskn9d.png)
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/aghnmg30yx7obcpqblb9.png)
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/coz5v69ksqtwrq0a2666.png)
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/mniv1483rc2xg1it2hxz.png)
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/f0chxrf4cpkk92tsrirb.png)
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/nosw6yqlxbf9dnqzv8uz.png)

This is cool because all your scripts are stored in ~/ghidra_scripts and linked to the project, if you change the JDK you may safely delete the project and recreate it without losing files.

If you click Run or Debug, Eclipse will be prompted to start a new Ghidra or Ghidra Headless instance, by clicking Debug, you can set breakpoints in the scripts you want to Debug. Run the scripts by opening Ghidra’s Script Manager and clicking Run. When the code reaches the breakpoints, the script’s execution will stop and you can debug it in Eclipse.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/7gkfw98o6scctqm2x0sf.png)

You can then resume the script, cancel it from Ghidra or modify the script without having to restart Ghidra. If you hit the stop button Ghidra will shut down, I’ve encountered some popups complaining that hot code replacement won’t work when I tried to save a script, I ignored it and it worked just fine.

By opening Ghidra Script Manager and right clicking on a script then clicking on Ghidra API Help will open a web browser with the scripting documentation.

---

To make my script I started with InstructionSearchScript.java as a base, iterated through all instructions, saved the value of EAX, searched for int 0x80 and added a plate comment with the system call’s name.

The final script:

[https://github.com/dnutiu/GhidraScripts/blob/master/CommentLinuxSysCall.java](https://github.com/dnutiu/GhidraScripts/blob/master/CommentLinuxSysCall.java)

Please note the ugly `systemCallNames.put(1, "sys_exit");`, I haven’t used Java that much and I couldn’t find a good way to initialize maps in an inline statement. If you have a suggestion please write it in the comments.

To get the system calls names I used a reference table from [Shell Storm](http://shell-storm.org/shellcode/files/syscalls.html) and some JavaScript to parse the table.

```javascript
table = document.getElementsByTagName("table")[0];
tableRows = table.children[0].children;
 
// for each tableRows starting from 1 skip header
for (index = 1; index < tableRows.length; index++) {
    rowData = tableRows[index].children;
    syscallValue = rowData[0].innerText;
    syscallName = rowData[1].innerText;
    console.log(`systemCallNames.put(${syscallValue}, "${syscallName}");`)
}
```

---

Running the script on a binary will annotate the system calls it finds by adding a plate comment.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/c0s26g5eyv66rp4l54wz.png)
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/njksdozfo7konivjuppq.png)

In conclusion, creating my first Ghidra script wasn’t that hard and once I’ve figured out how to setup Eclipse and link it properly the development experience was a bliss. I hope more and more people will adopt Ghidra and contribute to it. Some Ghidra trainings and exercises can be found in `ghidra_9.1-BETA_DEV/docs`.

Thanks for reading and have a great day!

---

Originally published [on my blog](https://dnutiu.wordpress.com/2019/10/13/ghidra-scripting-annotating-linux-system-calls/).