Author: rootkid
GO and try to solve this basic challenge.
FAQ: If you found the input leading to the challenge.txt you are on the right track
Attack of the Gopher
From the challenge description, and the name of the downloadable tarball gogogo.tar.gz, it's pretty apparent that this is a Go binary. In my previous experience with Go-based challenges, oftentimes the greatest difficulty is in finding the main function. Luckily, running the file command assuages my worries.
❯filebinarybinary:ELF64-bitLSBexecutable,x86-64,version1 (SYSV), statically linked, Go BuildID=OHBJFJh5S4MEkda8Q683/cMydJq6y9QbVjBCjK1KP/8R1f9ddSl9EfpM8KP2Dy/3G9-Ju3BW7WUsgoGNyvl,notstripped
So, it's not stripped. What does it do, then?
Running the binary just gives a prompt. Entering a string and hitting enter spits out an output that looks suspiciously like base64. We can test this hypothesis by checking the output of the binary if we vary the input string slightly:
We were also provided with a challenge.txt, containing the following textual data: GvVf+fHWz1tlOkHXUk3kz3bqh4UcFFwgDJmUDWxdDTTGzklgIJ+fXfHUh739+BUEbrmMzGoQOyDIFIz4GvTw+j--. We can assume that the correct input to the binary would produce the corresponding output to the contents of challenge.txt.
Going Deeper
With the help of the included debug symbols, finding the main function is trivial. Here's a snippet of the decompiled main function, retaining the important lines.
The main function doesn't do much beyond reading in the user's output, converting it to bytes and calling the main.Encode subfunction. The output of main.Encode is then printed to console. This is in line with the expected behaviour given our prior black-box testing.
Here's a snippet of main.Encode with Go-inserted error-handling snipped for easier reading:
In an attempt to understand this algorithm better, I converted it to Python, a language I'm more comfortable with. I've annotated portions of the translated code that I found interesting and indicative that my base64 hypothesis was right.
ENC_STRING ="NaRvJT1B/m6AOXL9VDFIbUGkC+sSnzh5jxQ273d4lHPg0wcEpYqruWyfZoM8itKe"str_index =0out_index =0while str_index <3* (len_input //3):'''You can observe input is split up into groups of 3 characters each.Output of 4 characters is generated.''' temp_1 =ord(in_str[str_index])<<16|ord(in_str[str_index +1])<<8|ord(in_str[str_index +2]) out_str += ENC_STRING[temp_1 >>18&0x3F] out_str += ENC_STRING[temp_1 >>12&0x3F] out_str += ENC_STRING[temp_1 >>6&0x3F] out_str += ENC_STRING[temp_1 &0x3F] str_index +=3 out_index +=4temp_2 = len_input - str_indexif temp_2 ==2: temp_3 =ord(in_str[str_index])<<16|ord(in_str[str_index +1])<<8else: temp_3 =ord(in_str[str_index])<<16out_str += ENC_STRING[temp_3 >>18&0x3F]out_str += ENC_STRING[temp_3 >>12&0x3F]'''Padding of the "-"s at the end of the string is done here'''if temp_2 ==1: out_str +=chr(0x45) out_str +=chr(0x45)elif temp_2 ==2: out_str += ENC_STRING[temp_3 >>6&0x3F] out_str +=chr(0x45)
Curiously enough, while I was doing translation, GitHub Copilot was doing an excellent job of filling in the next lines of code I would write. Along with the behaviour I observed while translating the code, this further confirmed my base64 hypothesis. A quick Google search put any remaining doubt to rest.
Reversing the base64 encryption is easy with the help of Python's base64 module. This can be accomplished in a few steps:
Extract the custom base64 alphabet from our binary
Make a string translation mapping the encrypted base64 string to the default base64 alphabet.
Profit?
import base64INFILE ="challenge.txt"STD_B64_CHARS ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="CUSTOM_ALPHABET ="NaRvJT1B/m6AOXL9VDFIbUGkC+sSnzh5jxQ273d4lHPg0wcEpYqruWyfZoM8itKe-"withopen(INFILE)as f: ENC_FLAG = f.read()defcustom_b64_decode(in_str:str) ->str: data =str(in_str).translate(str(in_str).maketrans(CUSTOM_ALPHABET, STD_B64_CHARS))return base64.b64decode(data).decode()out =custom_b64_decode(ENC_FLAG)whilenot out.startswith("grey{"): out =custom_b64_decode(out)print(out)
Running the function once spits out yet another base64-encoded string. Wrapping repeated calls to our custom base64 decode in a while loop yields the flag, eventually.