In this blog series, I will be putting the spotlight on some useful Ghidra features you might have missed. Each post will look at a different feature and show how it helps you save time and be more effective in your reverse engineering workflows. Ghidra is an incredibly powerful tool, but much of this power comes from knowing how to use it effectively.

In this post, I’ll be discussing how to use the Ghidra Decompiler to identify strings being constructed on the stack at runtime. This stack string technique is an easy way for a programmer to obscure string data within a program by blending it as opaque operand instructions. A typical pattern for constructing a stack string would be a series of MOV instructions transferring constant values into adjacent locations on the stack as shown in these basic examples:

Cloud Native Now
single-byte-copies stack string
Single Byte Copies
DWORD-copies
DWORD Copies

The above examples as seen in the Decompiler:

decompiler-single-byte-copies
Single Byte Copies
decompiler-DWORD-copies
DWORD Copies

Decoding Stack Strings

Some stack strings can be identified or deciphered with simple operations in the Decompiler view. In the case of the single byte copies, it may be possible to read out stack string values by updating Ghidra’s stack frame description so that the individual opaque bytes are interpreted as elements in a character array. The easiest way to do this is to retype the earliest element from undefined to char[n] where n is the length of the reconstructed string.

retype-variable
data-type-chooser-dialog

The resulting changes in the Decompiler reveal the string clearly:

Stack String in Decompiler

DWORD Value Previews

This is an easy way to evaluate that simple example, but it is not so trivial to convert multibyte stack string construction. In this case, it is possible to glimpse the data through the data type preview which appears when mousing over the constants.

Preview of 0x72636553
Preview of 0x72636553