The Challenge
- Name: Python Wrangling
- Description: Python scripts are invoked kind of like programs in the Terminal... Can you run this Python script using this password to get the flag?
The Solution
- With python installed, use the following command in a terminal
> cat pw.txt | python ende.py -d flag.txt.en
The Steps
The Source Code
-
Having some knowledge of Python helps to review the source code. Let's interpret it in chunks:
import sys import base64 from cryptography.fernet import Fernet- We're importing the
syslibrary which looks like it's used to handle the CLI arguments. - The
base64library, which looks to encode some sort of password - And
Fernetwhich seems to be an easy to use symmetric encryption class.
usage_msg = "Usage: "+ sys.argv[0] +" (-e/-d) [file]" help_msg = usage_msg + "\n" +\ "Examples:\n" +\ " To decrypt a file named 'pole.txt', do: " +\ "'$ python "+ sys.argv[0] +" -d pole.txt'\n" if len(sys.argv) < 2 or len(sys.argv) > 4: print(usage_msg) sys.exit(1)- We have a usage message and a help message. The help message looks... helpful, and provides instruction to decrypt what I assume is the encrypted
flag.txt.enflag. - It looks like the usage message prints when either too few or too many CLI arguments are passed to the program.
if sys.argv[1] == "-e": ... data_c = c.encrypt(data) sys.stdout.write(data_c.decode()) elif sys.argv[1] == "-d": ... data_c = c.decrypt(data.encode()) sys.stdout.buffer.write(data_c)- The
-eoption encrypts the data, the-doption decrypts.
- We're importing the
The Terminal Bits
-
After verifying the source code is magically harmless let's run it and see what happens.
> python ende.py Usage: ende.py (-e/-d) [file]- As expected the usage string prints.
-
Let's try following the help message. We can read it better by passing in the flag
-h.> python ende.py -h Usage: ende.py (-e/-d) [file] Examples: To decrypt a file named 'pole.txt', do: '$ python ende.py -d pole.txt' -
Now let's try passing in our encrypted flag file
> python ende.py -d flag.txt.en Please enter the password: -
Now we can copy and paste the password from the
pw.txtfile or:> cat pw.txt | python ende.py -d flag.txt.en- The flag prints out to the terminal.