KnightCTF 2025

Easy Path to the Grail – Reverse Engineering Challenge Writeup | KnightCTF 2025

Easy Path to the Grail - Detailed Writeup

Overview

Binary Name: grail.knight Architecture: ELF 64-bit Symbols: Not stripped

This writeup explains how to reverse the binary and recover the correct password without revealing the actual flag value.

Recon

First, inspect the binary:

file grail.knight

This confirms:

  • 64-bit ELF
  • Dynamically linked
  • Not stripped (function names available)

Run the binary:

./grail.knight

It prompts for input and prints “Wrong password!” if incorrect.

This proves:

  • The program performs a check against user input.
  • The flag is likely validated through transformation rather than direct comparison.

Dynamic Analysis

Use ltrace to monitor library calls:

ltrace ./grail.knight

Observation:

  • Each character is passed through sprintf with "%02X"
  • The program performs strcmp against a long hexadecimal string

This proves:

  1. Input is transformed before comparison.
  2. The comparison is made against a hardcoded hex string.
  3. The transformation produces byte-level hex output.

Static Analysis

Open in radare2:

r2 grail.knight
aaa
afl

Important functions discovered:

  • main
  • transform_input
  • do_fight

This confirms that input processing is separated into transformation logic.

Transformation Logic

transform_input

Disassembly shows:

  1. For each input character:
    • Call do_fight
    • Convert returned byte to hex using sprintf("%02X")
    • Append to output buffer

So the algorithm is:

transformed_byte = do_fight(original_byte)
output += HEX(transformed_byte)

do_fight

The assembly loop:

shift left accumulator
extract lowest bit from input
append bit to accumulator
shift input right
repeat 8 times

This proves:

  • The function performs bit reversal on each byte.
  • Each byte of input is reversed bitwise.

Example (demonstration only):

If a byte is: 01100001

Bit-reversed: 10000110

Reversing the Comparison

The binary compares against a fixed hexadecimal string.

Since the transformation is bit reversal:

To recover the original input:

original_byte = bit_reverse(transformed_byte)

Steps to recover password:

  1. Take the hardcoded hex string.
  2. Split into byte pairs.
  3. Convert each hex pair into a byte.
  4. Reverse the bits of each byte.
  5. Convert resulting bytes to ASCII.

Proof of Work (Verification Method)

To confirm correctness:

  1. Enter the reconstructed string into the binary.
  2. If correct:
    • The program prints a success message.
    • It reveals the flag format using the input provided.

The proof is the success message, not the flag itself.

Tools Used

  • file
  • ltrace
  • radare2

References

This writeup is original and explains the full reasoning process without revealing the actual flag value.

0 people love this