PicoCTF Writeup – convertme.py

# Information:

CTF Name: PicoCTF

CTF Challenge: convertme.py

Challenge Category: General Skills

Challenge Points: 100

Beginner picoMini 2022.

# Used Tools:

  • Python

# Challenge Description:

Run the Python script and convert the given number from decimal to binary to get the flag.

Download Python script

Hints:

Hint 1
Look up a decimal to binary number conversion app on the web or use your computer’s calculator!

Hint 2
The str_xor function does not need to be reverse engineered for this challenge.

Hint 3
If you have Python on your computer, you can download the script normally and run it. Otherwise, use the wget command in the webshell.

Hint 4
To use wget in the webshell, first right click on the download link and select ‘Copy Link’ or ‘Copy Link Address’

Hint 5
Type everything after the dollar sign in the webshell: $ wget , then paste the link after the space after wget and press enter. This will download the script for you in the webshell so you can run it!

Hint 6
Finally, to run the script, type everything after the dollar sign and then press enter: $ python3 convertme.py

# Writeup

This is a challenge to help learn how to run a python 3 script.

After reading the description I downloaded the python 3 script. I used the wget command to download the files.

Once downloaded, I opened the file with one text editor (in this case I used vim):

By performing a quick read of the code. We have the function str_xor that performs a xhor between a secret and a key. Then, we have the variable flag_enc, which is the flag xored.

Then, they generate a random number, and store it in the variable num. This number is between 10 and 100. Afterwards, we have a print asking us the convert the randomly generated number from decimal to binary. If our answer is correct we get the flag, otherwise we do not.

To solve this, I created my own decimal to binary conversion script in python 3:

def from_decimal_to_binary(number):
    res = ''
    while number > 0:
        res += str(number % 2)
        number = number // 2
    return change_string_order(res)
def change_string_order(string_to_change_order):
    res = ''
    for i in range(len(string_to_change_order) - 1, -1, -1):
        res += string_to_change_order[i]
    return res

decimal_number = int(input("Enter a decimal number: "))
binary_number = from_decimal_to_binary(decimal_number)
print("The binary is: " , int(binary_number))

I used one of the methods to perform the conversion. The method works by dividing the given decimal number recursively by 2.. The remainders are stored in the variable res (in my example) until we get as the final quotient the number 0. Once this is done we need to reverse the order of the numbers. Example:

For the number 6, we have:

6 % 2 = 0 and 6 // 2 = 3 remainder = 0

3 % 2 = 1 and 3 // 2 = 1 remainder = 1

1 % 2 = 1 and 1 // 2 = 0 remainder = 1 and we have a quotient of 0, so we stop here.

The remainders were:

0, 1 and 1

We now have to reverse the order and we have 6 in binary:

6 in decimal = 110 in binary

Knowing this, we now know what to do. We have to run the given script and use our script to convert the decimal to binary:

And the flag is:

Show flag
picoCTF{4ll_y0ur_b4535_722f6b39}

Code scripts:

If you want to checkout the code for the python 3 script with me solution, take a look here.


Thank you very much for reading!

Cheers,

MRegra


Leave a Reply

Your email address will not be published. Required fields are marked *