7oSkaaa

This Roxs

Mar 20th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | Source Code | 0 0
  1. import string
  2. from base64 import b64decode
  3.  
  4. # placeholder for the flag
  5. flag = "NOONCTF{flag_placeholder}"
  6.  
  7.  
  8. # Given encrypted flag
  9. given = ['', '00111111', '01010010', '01101001', '00000100', '00111000', '01010101', '01111101', '00010010', '00010011', '00011111', '01101101', '01010101', '00001000', '00101000', '01110001', '01101011', '01101011', '01011000', '00011100', '00010011', '00110010', '01101110', '00001001', '01111100', '00110111', '00110100', '00101000', '00000100', '00110110', '00011010', '00101000', '00000100', '00001000', '00101000', '01110001', '01101011', '01101011', '01011000', '00011100', '00010011', '00110010', '01101111', '01010010', '00100110', '00001010', '01110011', '00000111', '01101011', '00100010', '00111000', '00111011', '00100001', '01101010', '00000010', '00000000', '01010101', '00110010', '00011110', '00110011', '00100010', '01101011', '01011000', '00011100', '00010011', '00110010', '01101110', '00001001', '01111111', '00110100', '00111100', '00110101', '00010001', '00100100', '01111111', '01010010', '00100110', '00000101', '01111000', '00001101']
  10.  
  11.  
  12. # Get the first letter of the flag
  13. def get_first_letter():
  14.     first_letter_guess = list(
  15.         string.ascii_lowercase + string.ascii_uppercase + string.digits + '{}'
  16.     )
  17.     return first_letter_guess
  18.  
  19.  
  20. # Encrypt the flag
  21. def encrypt(flag):
  22.     arr = []
  23.  
  24.     for i in range(len(flag) - 1):
  25.  
  26.         c = format(ord(flag[i]), 'b')
  27.         cn = format(ord(flag[i + 1]), 'b')
  28.  
  29.         if len(c) < 8:
  30.             c = ((8 - len(c)) * "0") + c
  31.         if len(cn) < 8:
  32.             cn = ((8 - len(cn)) * "0") + cn
  33.  
  34.         item = []
  35.         for l in range(len(c)):
  36.             item.append(str(int(c[l]) ^ int(cn[l])))
  37.  
  38.         arr.append("".join([str(e) for e in item]))
  39.     return arr
  40.  
  41.  
  42.  
  43. # Decrypt the flag after adding the first letter to the encrypted flag
  44. def decrypt(given_copy, first_letter):
  45.     # Convert the first letter to binary
  46.     first_letter_byte = format(ord(first_letter), 'b')
  47.    
  48.     # Add padding to the first letter if it is less than 8 bits
  49.     if len(first_letter_byte) < 8:
  50.         first_letter_byte = ((8 - len(first_letter_byte)) * "0") + first_letter_byte
  51.        
  52.        
  53.     # Copy the given flag to avoid changing the original flag
  54.     tmp = given_copy.copy()
  55.    
  56.     # Add the first letter to the beginning of the flag
  57.     tmp[0] = first_letter_byte
  58.    
  59.     for i in range(len(tmp) - 1):
  60.         item = []
  61.         for l in range(len(tmp[i])):
  62.             item.append(str(int(tmp[i][l]) ^ int(tmp[i + 1][l])))
  63.  
  64.         tmp[i] = "".join([str(e) for e in item])
  65.    
  66.     return "".join([chr(int(tmp[i], 2)) for i in range(len(tmp))])
  67.  
  68.  
  69. # Print the flag to a file named "CTF_Match.txt" to match the CTF pattern
  70. def print_flag(flag):
  71.     with open("CTF_Match.txt", "a") as file:
  72.         file.write(flag + "\n")
  73.  
  74.    
  75. def main():
  76.     # Get the first letter of the flag
  77.     first_letter_guess = get_first_letter()
  78.    
  79.     # Decrypt the flag after adding the first letter to the encrypted flag
  80.     for c in first_letter_guess:
  81.         try:
  82.             decrypted = decrypt(given, c)
  83.             decrypted = b64decode(decrypted).decode("utf-8")
  84.             print_flag(decrypted)
  85.         except:
  86.             pass
  87.        
  88.  
  89. if __name__ == "__main__":
  90.     main()
Add Comment
Please, Sign In to add comment