Magnus college deployed a bot to help students study programming. Unfortunately, there’s a critical vulnerability in it. Find it before some bad actors use it for malicious activities.
Jimmy bot is an automated discord bot with a dash of AI and a fatal flaw.
By giving help command, the bot will list the functionalities.
By giving the command .ls we can see the flag.txt and a python script encode.py
Using the .viewcode function, the bot displays some code blocks and the reverse function is the one with the vulnerability.
The function uses a subprocess module to execute commands on the server.
This particular line caught my attention, since it was passing the result of the decryption function into a subprocess as a variable {dec_in} via shell (shell = True)!
Therefore all the environment variables and file globs could be accessed if we can inject the payloads “1;cat flag.txt” and “1;cat encode.py”.
By using the semicolon, to escape the brackets {}, and by assigning the value 1 to the variable dec_in to close the first argument. Then execute the command cat to extract the data.
After gathering all the functions together with the help of the .viewcode function, we will be able to view most of the bots code.
The cipher text was firstly “blockified” into 4 by 4 blocks and then padded with null characters so as to match the desired size. Then with mask() the blocks were shifted horizontally using the parameters a and b. The only thing left is to reverse the shifting in the rotate_left() function. Thus by providing the plaintext it can encode it using the same process in reverse. Then we encrypt the result in base 64.
Solution from reversing the code :
The output from the solution will be a base64 string. Using the reverse function we can print the encode.py and flag.txt in reverse.
By reversing and inspecting the output we can see that encode.py is a python script to XOR strings so we have to decode the XOR using a key, so we have to find the key to reverse the flag.
While running the command .help in the output its shown “thank me later”. By typing thanks the bot will display the key.
Using the key we can decode the text from flag.txt and retrieve the flag.
Given below is a sample code from the web to decode XOR using python. You can use any language you prefer.