Tuesday, March 16, 2021

Ghidra Dark Theme (VS Dark color scheme)

I've been using Ghidra for awhile now, but its default color scheme annoys me, especially when all of my other development tools and editors are running dark themes. While there is some support for recoloring certain aspects of the UI, not everything can be modified, leading to a rather inconsistent UI experience. The best guide I was able to find was this one: http://enigmatrix.me/blog/2019/11/30/dark-ghidra/ but even it left several dockable panes with the default white background, or even worse, black text in areas that did apply the dark theme to the background colors, but not the text for some reason. I have been able to extend that guide a bit further, and in the process I also modified the color scheme to my preferred one, to try and match the default dark theme from Visual Studio.

As with the enigmatrix tutorial, I chose to use FlatLaf as the base for my dark theme. You can find the latest version on Maven. Once you select a version, the download link will be in the top-right, and you'll want to select "jar" from the drop-down menu. Or, you can just grab the 1.0 jar here.  Once you download the jar file, I suggest placing it into your %USERPROFILE%\.ghidra folder.  Next, you'll need to modify a couple of Ghidra's launch files in order for Ghidra to be able to find it.  First up, you'll want to add the full jar path to the CPATH lines in <GHIDRA_PATH>/support/launch.sh (launch.bat on Windows).  The CPATH variable is a colon-delimited list (in the .sh file, the .bat file is semicolon-delimited), so just type a colon at the end, followed by the full path to the flatlaf jar file.

Next, set FlatLaf as the systemlaf in <GHIDRA_PATH>/support/launch.properties.  Just copy this at the bottom of the file:


Now open Ghidra and click Edit>Tool Options and select Tool.  Under Swing Look And Feel, select System, and check the box for Use Inverted Colors.  Click OK and close Ghidra.  Using inverted colors is an important step for converting some of the hard-coded elements that might otherwise use a white background or black text on our new dark themed backgrounds.  Next up is the color assignment.  As I said, I'm using the Visual Studio dark theme as the basis for my theme.  You're free to edit these as you like, but keep in mind that because Use Inverted Colors is enabled, you'll have to invert the RGB values that you actually want displayed (at least in most cases, some colors don't get inverted, it's really inconsistent and annoying).

First of all, you'll want to edit %USERPROFILE%\.ghidra\<GHIDRA_VERSION>\tools\_code_browser.tcd.  There are a lot of values to edit here.  This file doesn't seem to support hex values, so if you want to change any of these from what I have here, you first have to invert the hex code, and then convert the result to a 32-bit signed integer.  e.g. to get hex color #343A40, you would first invert it to #FFCBC5BF (the first FF is because you have to expand to 32-bits, so 00 inverts to FF), and then convert to signed 32-bit integer -3422785.  You can do this using the Windows 10 calculator by switching to Programmer mode and clicking the word size button above the keypad until it displays "DWORD".  Select Hex, enter your desired hex color value, and then XOR FFFFFFFF.  The value displayed next to DEC should be the value to use.

Now, we're going to edit the FlatLaf theme itself.  First of all, open up a text editor and copy the following contents into it.  Note that unlike the _code_browser.tcd edits above, this is the full contents of the file, so you don't have to go through and edit a bunch of separate sections.

Save this file as FlatDarkLaf.properties (if on Windows, you may need to save the file using Unix LF line endings instead of the default CRLF).  Next, rename the jar file you downloaded to .zip, or just open it directly with an archive tool like 7-zip.  Browse to com\formdev\flatlaf and replace FlatDarkLaf.properties with the one you just created.  Save the archive and rename back to .jar if necessary.

Now you're done!  Reopen Ghidra, and you should be greeted with a nice dark theme.

There are some weird orange and yellow colors, especially in dialog header, those are the result of inverting the hard-coded original blue colors.  Aside from that, it should all be themed.  I will say, there were a few colors in the properties files that I couldn't manage to match up to a UI element in the actual application.  It's possible Ghidra doesn't use those theme elements, or it's possible that I just missed them.  In most cases, if I couldn't find them, I left them as bright magenta (which may show up as bright green, depending on the inversion, it's not the darker purple like the selection color in the disassembly pane shown in the screenshot above), so if you happen to find something in the UI that is showing up as magenta or green, please let me know in the comments, and I'll see if I can track it down.