Ancient Encodings
1 minute to read
We are given the Python source code to encrypt the flag:
from Crypto.Util.number import bytes_to_long
from base64 import b64encode
from secret import FLAG
def encode(message):
return hex(bytes_to_long(b64encode(message)))
def main():
encoded_flag = encode(FLAG)
with open("output.txt", "w") as f:
f.write(encoded_flag)
if __name__ == "__main__":
main()
And we also have the output of the script:
0x53465243657a51784d56383361444e664d32356a4d475178626a6c664e44497a5832677a4d6a4e664e7a42664e5463306558303d
Source code analysis
The program takes the flag and uses encode
:
def encode(message):
return hex(bytes_to_long(b64encode(message)))
As can be seen, the flag is converted to Base64 encoding, then to a decimal number and finally to hexadecimal format.
So, we only need to transform the hexadecimal string to a bytes and then Base64-decode the result.
Flag
That’s it:
$ echo 53465243657a51784d56383361444e664d32356a4d475178626a6c664e44497a5832677a4d6a4e664e7a42664e5463306558303d | xxd -r -p
SFRCezQxMV83aDNfM25jMGQxbjlfNDIzX2gzMjNfNzBfNTc0eX0=
$ echo 53465243657a51784d56383361444e664d32356a4d475178626a6c664e44497a5832677a4d6a4e664e7a42664e5463306558303d | xxd -r -p | base64 -d
HTB{411_7h3_3nc0d1n9_423_h323_70_574y}