From 0a1557034032ce8e8f6d957e873ed15e893c7b55 Mon Sep 17 00:00:00 2001 From: aaron Date: Mon, 20 Jun 2022 21:43:53 +0200 Subject: [PATCH] update --- README.md | 2 +- crypto/XMASSpirit/decrypt.py | 49 ++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 4ba426e..abce83e 100644 --- a/README.md +++ b/README.md @@ -37,4 +37,4 @@ Hack the box december [CTF](https://ctf.hackthebox.com/ctf/249) ## Writeups -- Each challenge subfolder contains a small README.md +- Each challenge subfolder contains a README.md diff --git a/crypto/XMASSpirit/decrypt.py b/crypto/XMASSpirit/decrypt.py index 89ee380..6fa33fa 100755 --- a/crypto/XMASSpirit/decrypt.py +++ b/crypto/XMASSpirit/decrypt.py @@ -6,45 +6,50 @@ from math import gcd # the modulus for the lcg prng n = 256 # the header of the pdf v1.5 file -header = [ 0x25, 0x50, 0x44, 0x46, 0x2d, 0x31, 0x2e ] +header = [0x25, 0x50, 0x44, 0x46, 0x2D, 0x31, 0x2E] -def get_factors(ct:bytes, n:int=256) -> (int, int): - ''' find a and b for n and ct ''' + +def get_factors(ct: bytes, n: int = 256) -> (int, int): + """find a and b for n and ct""" # first generate a list of all numbers without common divisor with 256 - nogcds = [ x for x in range(1, n) if gcd(x, n) == 1 ] + nogcds = [x for x in range(1, n) if gcd(x, n) == 1] for b in range(1, n): for a in nogcds: for i, enc in enumerate(header): candidate = (a * enc + b) % n - if candidate != ct[i]: break - if(i+1 == len(header)): - return (a,b) - print('[-] No solution found.') - return (0,0) + if candidate != ct[i]: + break + if i + 1 == len(header): + return (a, b) + print("[-] No solution found.") + return (0, 0) -def generate_lookuptable(a:int, b:int) -> dict[int,int]: - ''' generate a lookuptable for the translation of the enc file''' + +def generate_lookuptable(a: int, b: int) -> dict[int, int]: + """generate a lookuptable for the translation of the enc file""" out = {} for i in range(0, 256): lt = (i * a + b) % 256 out[lt] = i return out -def decrypt(ct:bytes, lut:dict) -> bytes: - ''' decrypt the file using the lookup table ''' - res = b'' + +def decrypt(ct: bytes, lut: dict) -> bytes: + """decrypt the file using the lookup table""" + res = b"" for byte in ct: dec = lut[byte] res += bytes([dec]) return res -if __name__ == '__main__': - ct = open('encrypted.bin', 'rb').read() + +if __name__ == "__main__": + ct = open("encrypted.bin", "rb").read() a, b = get_factors(ct) - print(f'[+] Factors solved: a={a}, b={b}') + print(f"[+] Factors solved: a={a}, b={b}") lut = generate_lookuptable(a, b) - print(f'[+] Lookuptable generated: \nlookup = {lut}') - dec = decrypt(ct,lut) - print(f'[+] Encrypted file decrypted') - with open('letter.pdf', 'wb') as f: - f.write(dec) + print(f"[+] Lookuptable generated: \nlookup = {lut}") + dec = decrypt(ct, lut) + print(f"[+] Encrypted file decrypted") + with open("letter.pdf", "wb") as f: + f.write(dec)