# Payload rauskopieren:
# Größe richtig einstellen (Y und byte[0xbf4])
# Doppelt drauf klicken
# auf den Namen klicken, eine Zeile runter ziehe, Maustaste loslassen
# "Copy Special" / Shift-E
# Byte String (No Spaces)
# Resultat in payload.hex speichern

# 1. payload.hex von der Festplatte lesen
payload_hex = open("payload.hex").read().strip()
import binascii

# 2. hex decoden (um die tatsächlichen Bytes zu bekommen)
payload = binascii.unhexlify(payload_hex)
#print(payload)

# 3. in 4-er chunks durchgehen
decoded = bytes()
for i in range(0, len(payload), 4):
    chunk = payload[i:i + 4]
    #print(chunk)
    # 4. auf jedem chunk das hier machen:
    """
    XorKey = 0x10a1
    Decrypted = Input ^ XorKey

    return (Decrypted << 4 | Decrypted >> 28) + 2004318072;
    """

    xor_key = 0x10a1
    decrypted_chunk = int.from_bytes(chunk, "little") ^ xor_key

    shifted_chunk = (decrypted_chunk << 4 | decrypted_chunk >> 28) + 2004318072
    shifted_chunk &= 0xffffffff
    decoded += shifted_chunk.to_bytes(4, "little")

# 5. resultat in decrypted.bin schreiben (sollte mit 55 8b ec 81 ec ac 00 00 anfangen)
#print(binascii.hexlify(decoded[:20]))
open("decrypted.bin", "wb").write(decoded)

# Import as:
# x64, default, 32, little, Visual Studio
