What is Ghidra? Reverse engineering explained, with cats
How Ghidra, the NSA's free reverse engineering tool, turns a compiled program back into readable code, and how to track down a hidden feature inside Notepad.
Ghidra, explained by cats. Kitty opens Notepad. Every day. For years. A blank page. Nothing else. One day she wonders. How does it work inside? But Notepad is just a file. Machine code. Numbers for computers. Not for cats. So Kitty opens Ghidra.
A free reverse engineering tool. Made by the NSA. Yes, that NSA. She drops Notepad in. Ghidra doesn't run it. It turns it back into readable code. But the names are gone. Every function is FUN, and a number. Not very fun.
So Kitty follows the clues. She lists the text inside. Menu words. Error messages. And one odd word. .LOG. She jumps to the code that uses it. Presses L. Renames it: log trick. It checks a file's first line. If it says.log, Notepad adds the date and time.
Every time it opens. Kitty tries it. types.log. Saves. Opens it again. There's the time. That's Ghidra. It turns programs back into code cats can read. Now Kitty knows how her Notepad works. Notepad still has no idea how Kitty works.
Ghidra is a free, open-source reverse engineering tool made by the NSA. It takes a compiled program, such as notepad.exe, and turns its machine code back into C-like code a person can read.
Reverse engineering means working out how a program works from the program alone, with no source code and nobody to ask. Security researchers use it to pull malware apart and to hunt for bugs. It is also how people keep old software running after its source is lost, and how they make new programs talk to file formats nobody documented.
Why a compiled program is hard to read
When a program is compiled, the compiler turns source code into machine code: the numbers a processor runs directly. A lot is thrown away on the way: comments and most names go. Loops and if statements become jumps between addresses, and the compiler is free to reorder, merge and inline code to make it faster.
Open notepad.exe in a hex viewer and that is what you get: page after page of bytes. Every one of them matters to the processor, and none of them explains itself. If the gap between source code and machine code is new to you, the compiler vs interpreter video covers it.
What Ghidra does with a file
Ghidra never runs the program. It reads the file, which is called static analysis, so you can look inside a program without letting it do anything.
Import: you create a project and drop the file in. Ghidra recognises the format (a Windows .exe is a PE file) and the processor it was built for.
Analyse: Ghidra offers to analyse the file, and the defaults are fine. It disassembles the bytes into instructions, finds where functions start and end, follows the calls between them and picks out text and other data.
Ghidra, explained by cats. Kitty opens Notepad. Every day. For years. A blank page. Nothing else. One day she wonders. How does it work inside? But Notepad is just a file. Machine code. Numbers for computers. Not for cats. So Kitty opens Ghidra.
A free reverse engineering tool. Made by the NSA. Yes, that NSA. She drops Notepad in. Ghidra doesn't run it. It turns it back into readable code. But the names are gone. Every function is FUN, and a number. Not very fun.
So Kitty follows the clues. She lists the text inside. Menu words. Error messages. And one odd word. .LOG. She jumps to the code that uses it. Presses L. Renames it: log trick. It checks a file's first line. If it says.log, Notepad adds the date and time.
Every time it opens. Kitty tries it. types.log. Saves. Opens it again. There's the time. That's Ghidra. It turns programs back into code cats can read. Now Kitty knows how her Notepad works. Notepad still has no idea how Kitty works.
Read: the CodeBrowser opens. The Listing shows the assembly, each instruction with its address and bytes. Beside it, the Decompiler shows the function you are in as C-like code.
Most of the time you will read the Decompiler rather than the Listing. Here is one small function from notepad.exe after analysis:
A function from notepad.exe: the Windows calls keep their names, Notepad's own function does not.
You can follow it without knowing assembly: it gets the window's system menu, removes four entries and shows the menu as a pop-up. That is far quicker to read than the instructions it came from.
Why every function is called FUN_
Look at that function's name: FUN_14007c030. When Ghidra finds a function with no name attached, it calls it FUN_ plus the address where the function starts. Most of Notepad's functions have no name, so the Symbol Tree is a long list of FUN_ entries. The same goes for data (DAT_), jump targets (LAB_), parameters (param_1) and local variables (local_48, uVar1).
Some names do survive. GetSystemMenu and RemoveMenu live in Windows, not in Notepad, and the program asks Windows for them by name when it loads. So every call into the operating system shows up with a readable label, and those calls are your best clues to what a function is for.
Microsoft also publishes symbol files (PDBs) for many Windows programs, and Ghidra offers to download them. Say yes and many FUN_ names turn into Microsoft's own. The video said no on purpose, because most programs you would want to study don't come with symbols.
Following the clues: the .LOG hunt
With so many unnamed functions, you don't read from the top. You start from something you can recognise and work backwards. Text is the easiest place to start.
Search the strings
Search > For Strings lists every piece of text Ghidra can find, from menu labels to error messages. In Notepad that is more than 40,000 entries. Type into the filter and the list shrinks to the matches. Filter for .LOG and four are left:
Filtering the string search for .LOG leaves four hits out of 40,668.
The top hit is not stored text at all. It sits inside an instruction, MOV RCX,0x47004f.... The compiler packed the four characters straight into a number in the code, and Ghidra's search still spotted them.
Jump to the code that uses it
Double-click the hit, then use References > Show References to. Ghidra lists every place that uses the address, and one more click takes you there. These links are called cross-references, and following them is a large part of reverse engineering: from a string to the function that uses it, from a function to the functions that call it.
Rename as you go
Once you know what a function does, give it a name. Click the name in the Decompiler, press L, type a better one, and press Enter:
The new name shows up everywhere the function is used, in the Listing, the Decompiler and every caller. Each rename makes the next function easier to read, which is how a list of FUN_ entries slowly turns into a map of the program.
What log_trick does
Inside log_trick, Notepad takes the start of the text it has just loaded and compares it with one number:
Line 278 compares the start of the text with 0x47004f004c002e, which is .LOG in UTF-16.
That number is .LOG. Windows programs hold text in memory as UTF-16, two bytes per character, so .LOG is the eight bytes 2E 00 4C 00 4F 00 47 00. An x86 processor stores the lowest byte of a number first, so reading those eight bytes as one number gives 0x0047004F004C002E. Checking one 8-byte number takes a couple of instructions, which is cheaper than looping over four characters.
If the file's first line is .LOG, Notepad adds the current date and time at the end. The timestamp comes from Windows functions that the decompiler shows by name: GetLocalTime reads the clock, and GetDateFormatW and GetTimeFormatW format it for your region.
You can try it in any Notepad. Type .LOG on the first line, press Enter, save, close and open the file again:
File Explorer says the file is 6 bytes: .LOG and a line break. The timestamp is added to the open document, not to the file, until you save it. Microsoft documents the trick in Notepad's help. On Windows 11, Notepad may restore your last session instead of opening the file fresh; the video set 'When Notepad starts' to start a new session so the trick fires.
What decompiled code can't tell you
The Decompiler's output is a reconstruction, not the original source. Expect a few gaps:
Names and comments are gone unless symbols survive. local_380 tells you nothing until you rename it.
Types are guesses. Ghidra marks what it can't work out as undefined8 or undefined4, and the code gets clearer as you correct them.
Optimised code looks odd. The compiler's shortcuts, like the .LOG comparison, show up in the output, and inlined functions appear mixed into the code that calls them.
Some programs fight back. Malware is often packed or obfuscated, so its code only makes sense after it unpacks itself in memory.
Programs written for .NET or Java keep far more of their structure, and tools built for them, such as ILSpy for .NET, give much cleaner results there. Ghidra suits native code: C, C++, Rust, Go and anything else compiled to machine code.
When you may use it
Ghidra itself is free to use. It is released under the Apache 2.0 licence, and anyone can read its source on GitHub (the open source video explains what that licence lets you do). What you point it at is a separate question. Many software licences forbid reverse engineering, and the law differs from country to country. Practise on your own programs, open-source ones, programs built as reverse engineering exercises ('crackmes'), or work you are authorised to do. Don't publish decompiled code from someone else's product.
Ghidra is not the only tool for this. IDA Pro and Binary Ninja are commercial, and radare2 is another free option. Ghidra's draw is that it costs nothing, runs on Windows, Linux and macOS, and comes with a decompiler that handles many processor types.
Key takeaways
Ghidra is the NSA's free, open-source reverse engineering suite. It reads a compiled program without running it and shows it as assembly and C-like code.
Compiling throws away names and comments, so Ghidra calls unnamed functions FUN_ plus their address. Calls into Windows keep their names and make good clues.
Start from something recognisable, such as a string, follow cross-references to the code that uses it, and rename functions with L as you work them out.
Decompiled code is a reconstruction. Types are guesses and optimised code looks odd.
Only reverse engineer software you are allowed to, and check the licence first.