Security Forem

Hitanshu Gedam
Hitanshu Gedam

Posted on

picoCTF bloat.py writeup

We are given two files and are askedd to run them in the same directory.
I create a ~/tmp directory on pico webshell and wget those two files in it. First, I open the python file to try to understand the code.

code

This code is obfuscated which makes it difficult for a human to read.

The variable a is given a long string.

I head over to Programiz to find what the first condition is:

programiz

It checks for the argument to be equal to the string "happychance", if it is, then it returns True, else it returns "That password is incorrect" and exits with code 0.
I re-wrote python code in a readable format:

a = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"+ \
            "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ "
def check(pwd):
  if pwd == "happychance":
    return True
  else:
    print("The password is incorrect")

def decoder(arg444):
  return join_flag(arg444.decode(), "rapscallion")

def getinput():
  return input("Please enter correct password for flag: ")

def open_flag():
  return open('flag.txt.enc', 'rb').read()

def welc():
  print("Welcome back... your flag, user: ")


def join_flag(first_string, second_string):
    second_string_copy = second_string
    i = 0
    while len(second_string_copy) < len(first_string):
        second_string_copy = second_string_copy + second_string[i]
        i = (i + 1) % len(second_string)        
    return "".join([chr(ord(first_string_char) ^ ord(second_string_char)) for (first_string_char,second_string_char) in zip(first_string,second_string_copy)])


opened_flag_binary = open_flag()
pwd = getinput()
check(pwd)
welc()
decoded_flag = decoder(opened_flag_binary)
print(decoded_flag)
Enter fullscreen mode Exit fullscreen mode

I decoded this much and after a while, I thought it was enough since later in the code the functions are being called and the values are getting stored in the variables.

I ran the python file and gave "happychance" as the input, and there I had my flag!

gotcha

Top comments (0)