Skip to content

Instantly share code, notes, and snippets.

@nick3499
Last active January 19, 2022 18:34
Show Gist options
  • Save nick3499/7eb6f732e05d9d23850df0afaeadb9a5 to your computer and use it in GitHub Desktop.
Save nick3499/7eb6f732e05d9d23850df0afaeadb9a5 to your computer and use it in GitHub Desktop.
crackme-py
#!/bin/python3
'''Encode or decode ROT47'''
# Any spaces are left unchanged.
def rot47(string):
'''Encode/decode string.'''
enc_dec = '' # string encoded/decoded to/from ROT47
for char in string:
ascii_code = ord(char) # convert character to ASCII code number
if 33 <= ascii_code <= 79:
enc_dec += chr(ascii_code + 47)
elif 80 <= ascii_code <= 126:
enc_dec += chr(ascii_code - 47)
else:
enc_dec += char
return enc_dec
if __name__ == '__main__':
print(rot47(input('Enter string: ')))
@nick3499
Copy link
Author

nick3499 commented Jan 19, 2022

picoCTF: crackme-py

Get Python Script

$ curl https://mercury.picoctf.net/static/be2ba466c6154e42c756bf737ddcecc3/crackme.py -O

Decode a Variable

In order to find the picoCTF flag, decode the value of the bezos_cc_secret from crackme.py using the ROT47 cipher.

Encoder/Decoder Script

rot47(s) both encodes and decodes ROT47, while any spaces will be left unchanged.

def rot47(string):
    enc_dec = ''
    for char in string:
        ascii_code = ord(char)
        if 33 <= ascii_code <= 79:
            enc_dec += chr(ascii_code + 47)
        elif 80 <= ascii_code <= 126:
            enc_dec += chr(ascii_code - 47)
        else:
            enc_dec += char
    return enc_dec


if __name__ == '__main__':
    print(rot47(input('Enter string: ')))
$ python3 encode_decode_rot47.py
Enter string: A:4@r%uL`M-^M0c0AbcM-MFE0cdhb52g2N
picoCTF{1|\/|_4_p34|\|ut_4593da8a}

Simplified Chained Comparisons

pylint suggests the use of arbitrarily chained comparisons.

Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

33 <= ascii_code <= 79
80 <= ascii_code <= 126

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment