Skip to content
All articles

Security

C++ macro obfuscation: what it hides and what it can't

I ran a small experiment that makes standard C++ unrecognizable with the preprocessor and computes a hidden message at runtime. From a reverse engineering perspective, here's what this kind of obfuscation hides and what it can't.

Fahrettin Baştürk4 min read

Anyone who does reverse engineering eventually wonders: how does the other side hide its code, and how would I undo it? With that curiosity I ran a small experiment. Using the C++ preprocessor, I turned standard C++ syntax into an anime-themed, esoteric language, and generated the message inside it (yes, "I love Lain", in Turkish) at runtime without ever storing it as plain text.

It started as a joke, but it taught me serious things about code obfuscation, especially what it can hide and what it can never hide.

The preprocessor: a language that runs before the compiler

Before the C++ compiler sees your code, the preprocessor transforms it as text. Every macro you define with #define is substituted before compilation begins. That means you can map basic tokens like std, int and for to completely different words.

#define nani int
#define kawaii main
#define senpai for
#define baka std::cout

nani kawaii() {
    senpai (nani i = 0; i < 3; ++i) baka << i;
}

To the human eye, this code becomes unrecognizable. To the compiler, nothing changed; after preprocessing, it's looking at an ordinary C++ program.

Hiding the message: a tape and a single cell

One of the easiest things to find in a program is its constant strings. Run a tool like strings on an executable and every readable piece of text spills out. So I never stored the message as text at all. Using tape logic inspired by Brainfuck, a single 8-bit char cell is incremented in loops until it reaches the target ASCII value, and is written to the output at that moment. The word "Lain" appears nowhere in the executable.

So does it actually hide anything?

This is the real lesson. These techniques work against one kind of attacker and do almost nothing against the others.

  • Against someone reading the source: effective. The macro layer seriously slows down reading and understanding.
  • Against static string scanning: effective. The message isn't in the strings output.
  • Against someone analyzing the compiled binary: almost useless. Macros disappear before compilation; what you see in Ghidra or IDA is an ordinary C++ program.
  • Against someone running and watching the program: useless. The message is eventually written to memory and output; catching that moment with a debugger takes minutes.

Obfuscation in the real world

Obfuscation used in commercial software goes much further: flattening control flow, adding fake branches, turning code into bytecode that runs on its own virtual machine. These can stretch analysis from hours to days, from days to weeks. But none of them make analysis impossible. Code eventually has to run on a CPU, and everything that runs on a CPU can be observed.

I reached a similar conclusion in my post on why reverse engineering Flutter apps is hard: you can make it harder, but you can't hide it.

Practical advice for developers

  • Don't embed a secret (an API key, an encryption key) in a client app and try to hide it. Secrets belong on the server.
  • Think of obfuscation as a cost layer, not a security layer. It makes analysis expensive; it doesn't prevent it.
  • Don't leave license and integrity checks to decisions the client makes on its own.
  • Open your own app once from an attacker's point of view. Ghidra is free and very educational.

This small experiment reminded me of something I like about security: the more valuable the thing you're trying to hide, the more you should try not to ship it at all. Sometimes the best obfuscation is leaving nothing to hide.

Fahrettin Baştürk

Software Engineer & Consultant

Share
  • C++
  • Obfuscation
  • Reverse Engineering
  • Security

Working on a similar system or problem?Tell me about your project