IC00AJ74 · Week 4 · activity 1

Task 1: Reverse engineering an x86_64 native binary

Reverse-engineer a small native executable and recover its original input.
120minutes
1point

In this task, we examine a binary executable that acts like a simulated ransomware payload. It is an executable and linkable format (ELF) binary, the standard executable format on Linux systems.

The executable is not actual malware. It is a benign program that takes a string as a command-line argument and writes an encrypted version to a specified file, mimicking how ransomware encrypts files on disk. We want to analyse this behavior from a reverse engineering perspective. In the final task of this lab, we will examine an authentic sample. In this exercise, your goal is to find out how the program encrypts the string and then reverse its logic to recover the original text.

You can find the example binary and encrypted_output.txt from the shared files on the right side. It will be different, when you are doing it with the automated system!

Recommended tools:

  • Standard Linux utilities:
    • strings
    • hexdump
    • objdump
    • ldd
  • Ghidra (software reverse engineering suite)
  • An LLM (such as ChatGPT) to help analyse decompiled code snippets
  • CyberChef for data conversions and cryptographic operations

Basic Linux commands for analysis

First, let’s review a few commands that can be used in Linux to inspect binary executables.

strings extracts and prints printable character sequences from the binary. Piping into less lets you scroll through the output conveniently:

strings <FILENAME> | less

You can exit less by pressing q.

Using strings, you might find the encryption key used to encrypt the file, along with other interesting indicators.

However, if you do not find it immediately, do not worry: Ghidra will make it easier to locate. In real malware, keys are rarely stored in plain text.

Similarly, you can inspect both raw bytes and hexadecimal representations using hexdump:

hexdump -C <FILENAME> | less

objdump disassembles the binary, displaying its raw machine instructions. While disassembly shows exactly what the program executes, reading assembly directly can be slow and demanding:

objdump -D -z <FILENAME>

You can inspect dynamic library dependencies using ldd. While not strictly required for this task, it helps identify the shared libraries on which a binary depends:

ldd /bin/ls
        libcap.so.2 => /usr/lib/libcap.so.2 (0x0000004002861000)
        libc.so.6 => /usr/lib/libc.so.6 (0x000000400286f000)
        /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x0000004000000000)

Because reading raw assembly is time-consuming, decompilers help translate machine instructions into readable pseudocode.

Ghidra analysis

We will now analyse the file using Ghidra, an open-source reverse engineering framework developed by the National Security Agency (NSA) for analysing compiled code. If you are familiar with another reverse engineering tool, you are welcome to use that instead. Some alternative tools are commercial and quite expensive. If you are interested in comparing decompilation outputs from different tools on small binaries, you can explore Dogbolt.

You can install Ghidra on Debian-based systems with:

sudo apt install ghidra

On Arch Linux:

sudo pacman -S ghidra
Setup guide for Ghidra file analysis (click to expand)

Dependencies for Ghidra:

  • libc6
  • libgcc-s1
  • libstdc++6
  • openjdk-17-jdk

Launch Ghidra after installing it:

ghidra

Two windows may open; keep the main project manager window open.

Ghidra project manager

flowchart LR
    A[Click File] --> B[New project]
    B --> C[Non-shared project]
    C -->  D[Choose a directory for the project and a name]

Creating a new Ghidra project

Drag the binary file into Ghidra and import it into the project.

Import information shows up. Close it since it doesn’t have much new information.

Double-click the executable in Ghidra to open the CodeBrowser, and allow it to analyse the file when prompted (the default analysis settings are sufficient).

Now we can begin reverse engineering the binary.


We can locate all the functions in the binary by exploring the symbol tree. The symbol tree provides a hierarchical view of the symbols in the program, such as functions, classes, and variables. The one we are most interested in is the main function.

flowchart LR
    A[Symbol tree] --> B[Functions]
    B --> C[Main]

Ghidra symbol tree with the main function

To find the main function, click the plus icon next to Functions and select main.

Ghidra displays the machine instructions (disassembly listing) on the left side, and the decompiled C/C++ pseudocode on the right side.

Ghidra disassembly and decompiled main function

Examining the decompiled code reveals the string encrypted_output.txt and our encryption key. We can also see that the program takes a string argument from the command line. This is the flag you are tasked with recovering.

In the decompiled view, we can see that our key is passed as a parameter to an initial helper function, which we will call “function A”.

A second non-standard function follows function_A in main; we will call this “function B”.

Let’s examine what happens inside function A and function B.

You are welcome to use an LLM to help interpret decompiled snippets. LLMs are particularly effective at identifying standard library idioms from decompiled pseudocode.

The main function

Input argument

  • Description: The main function expects one command-line argument, which serves as the flag to be encrypted.

Function A

This function processes a key string in hexadecimal format, transforming it into a sequence of raw bytes. Note that an operation is performed on the key before it is passed to Function A. What operation is it?

Operations performed in the loop

  1. Length check (length)

    • Description: The function checks the length of the key string to determine how many iterations are needed to process the entire string.
    • Equivalent C++ method: std::string::length
  2. Substring extraction (substr)

    • Description: In each iteration, the function extracts a two-character substring from the key string representing a hexadecimal byte.
    • Equivalent C++ method: std::string::substr
  3. String-to-integer conversion (stoi)

    • Description: The extracted two-character hexadecimal substring is converted into its corresponding byte value.
    • Equivalent C++ method: std::stoi
  4. Appending the byte (push_back)

    • Description: The byte obtained from the conversion is appended to the output string, building a sequence of bytes.
    • Equivalent C++ method: std::string::push_back

The two primary clues for understanding Function A:

  1. The function processes pairs of characters from a string representing hexadecimal values, converting them into raw byte values.

  2. Base 0x10: Passing base 16 (0x10) to stoi indicates that each pair of characters is parsed as a hexadecimal integer. Why is this useful in the context of key processing?

Based on these observations, you can determine the overall purpose of Function A.

Function B

Within the main function, the flag and the processed key from function_A are passed as parameters to Function B.

  1. Size check (size)

    • Description: The function checks the size of the input string (flag) to determine how many iterations the loop will perform.
    • Equivalent C++ method: std::string::size()
  2. Character access (operator[])

    • Description: The function accesses individual characters of both the flag and the processed key, pairing each character of the flag with a corresponding byte from the key.
    • Equivalent C++ method: std::string::operator[]
  3. Character processing

    • Description: In each iteration, these characters undergo an operation that transforms the flag character based on the key byte.
  4. Appending the result (push_back)

    • Description: The resulting character from the operation is appended to the output string.
    • Equivalent C++ method: std::string::push_back

While decompiled C++ template code can look cluttered, inspecting the corresponding assembly instructions makes the underlying operation much clearer.

Selecting the key’s operator[] in the listing highlights the corresponding assembly instructions.

The two primary clues for understanding Function B:

  1. Key indexing with operator[]: The machine code shows the key being accessed character by character alongside a specific bitwise operation. This gives a direct indication of the function’s purpose.

  2. The transformed character is appended to the output string. How does this transformation occur?

Reversing the output text

At this stage, you have identified both the encryption key and the transformation algorithm.

Using this information, you can reverse encrypted_output.txt back into the original flag.

There are two common ways to accomplish this:

  1. Use a tool like CyberChef to reverse the transformation steps and decrypt the output file (straightforward).

  2. Write a short program (in Python, C, or Rust) that reverses the logic to convert encrypted_output.txt back into the original input string (or prompt an LLM to help build the script).

Below, you get a dynamic version of the descriped binary with own secret and flag. Reverse-engineer it and submit the flag!

If you want to explore Ghidra further, check out the awesome-ghidra repository for a curated list of plugins and resources.

Sign in required

Sign in with the GitHub account linked to your university email.
Sign in to continue

Shared files

  • encrypted_output.txttxt
  • reversable.binBinary
    Download

Shared files

  • encrypted_output.txttxt
  • reversable.binBinary
    Download

encrypted_output.txt

Loading preview…

Download