|
| 1 | +--- |
| 2 | +title: Python Wrangling |
| 3 | +date: 2025-07-12 |
| 4 | +categories: |
| 5 | + - Capture The Flags |
| 6 | + - picoCTF |
| 7 | +tags: |
| 8 | + - ctf |
| 9 | + - picoctf |
| 10 | + - misc |
| 11 | + - writeups |
| 12 | +description: picoCTF Python Wrangling Challenge |
| 13 | +--- |
| 14 | + |
| 15 | + |
| 16 | +> Challenge description: |
| 17 | +> |
| 18 | +>Python scripts are invoked kind of like programs in the Terminal... Can you run [this Python script](https://mercury.picoctf.net/static/2ac2139344d2e734d5d638ac928f1a8d/ende.py) using [this password](https://mercury.picoctf.net/static/2ac2139344d2e734d5d638ac928f1a8d/pw.txt) to get [the flag](https://mercury.picoctf.net/static/2ac2139344d2e734d5d638ac928f1a8d/flag.txt.en)? |
| 19 | +> |
| 20 | +{: .prompt-info } |
| 21 | + |
| 22 | +For this challenge, we are given `ende.py`, `flag.txt.en`, and `pw.txt` |
| 23 | + |
| 24 | +Let's look at each file we're given. |
| 25 | + |
| 26 | +```python |
| 27 | +import sys |
| 28 | +import base64 |
| 29 | +from cryptography.fernet import Fernet |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +usage_msg = "Usage: "+ sys.argv[0] +" (-e/-d) [file]" |
| 34 | +help_msg = usage_msg + "\n" +\ |
| 35 | + "Examples:\n" +\ |
| 36 | + " To decrypt a file named 'pole.txt', do: " +\ |
| 37 | + "'$ python "+ sys.argv[0] +" -d pole.txt'\n" |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +if len(sys.argv) < 2 or len(sys.argv) > 4: |
| 42 | + print(usage_msg) |
| 43 | + sys.exit(1) |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +if sys.argv[1] == "-e": |
| 48 | + if len(sys.argv) < 4: |
| 49 | + sim_sala_bim = input("Please enter the password:") |
| 50 | + else: |
| 51 | + sim_sala_bim = sys.argv[3] |
| 52 | + |
| 53 | + ssb_b64 = base64.b64encode(sim_sala_bim.encode()) |
| 54 | + c = Fernet(ssb_b64) |
| 55 | + |
| 56 | + with open(sys.argv[2], "rb") as f: |
| 57 | + data = f.read() |
| 58 | + data_c = c.encrypt(data) |
| 59 | + sys.stdout.write(data_c.decode()) |
| 60 | + |
| 61 | + |
| 62 | +elif sys.argv[1] == "-d": |
| 63 | + if len(sys.argv) < 4: |
| 64 | + sim_sala_bim = input("Please enter the password:") |
| 65 | + else: |
| 66 | + sim_sala_bim = sys.argv[3] |
| 67 | + |
| 68 | + ssb_b64 = base64.b64encode(sim_sala_bim.encode()) |
| 69 | + c = Fernet(ssb_b64) |
| 70 | + |
| 71 | + with open(sys.argv[2], "r") as f: |
| 72 | + data = f.read() |
| 73 | + data_c = c.decrypt(data.encode()) |
| 74 | + sys.stdout.buffer.write(data_c) |
| 75 | + |
| 76 | + |
| 77 | +elif sys.argv[1] == "-h" or sys.argv[1] == "--help": |
| 78 | + print(help_msg) |
| 79 | + sys.exit(1) |
| 80 | + |
| 81 | + |
| 82 | +else: |
| 83 | + print("Unrecognized first argument: "+ sys.argv[1]) |
| 84 | + print("Please use '-e', '-d', or '-h'.") |
| 85 | + |
| 86 | +``` |
| 87 | +{: file="ende.py" } |
| 88 | + |
| 89 | +```plaintext |
| 90 | +gAAAAABgUAIV8D5MJdzgLLTkkMlbU84ARVwfX4brMt2rJQCjkpLItytfWVZe1L2dp4K8VrKgRU3axStKJEAqcM0iDaxiYE54Boh8UfAAo1RNifKnlDrFz0gLaznVSFVj2xAUa4V35180 |
| 91 | +``` |
| 92 | +{: file="flag.txt.en" } |
| 93 | + |
| 94 | +```plaintext |
| 95 | +68f88f9368f88f9368f88f9368f88f93 |
| 96 | +``` |
| 97 | +{: file="pw.txt" } |
| 98 | + |
| 99 | +Hm, looks like a python script, an encoded flag, and the password to decrypt said flag. Let's run the python script and see what the usage looks like. |
| 100 | + |
| 101 | +```terminal |
| 102 | +❯ python ende.py |
| 103 | +Usage: ende.py (-e/-d) [file] |
| 104 | +``` |
| 105 | + |
| 106 | +Okay, so since we want to decrypt, lets use the `-d` flag, along with the `flag.txt.en` file. |
| 107 | + |
| 108 | +```terminal |
| 109 | +❯ python ende.py -d flag.txt.en |
| 110 | +Please enter the password: |
| 111 | +``` |
| 112 | + |
| 113 | +Alrighty, and let's throw the password in that we got from the `pw.txt` file. |
| 114 | + |
| 115 | +```terminal |
| 116 | +❯ python ende.py -d flag.txt.en |
| 117 | +Please enter the password:68f88f9368f88f9368f88f9368f88f93 |
| 118 | +picoCTF{4p0110_1n_7h3_h0us3_68f88f93} |
| 119 | +``` |
| 120 | + |
| 121 | +FLAG: `picoCTF{4p0110_1n_7h3_h0us3_68f88f93}` |
0 commit comments