Skip to content

Instantly share code, notes, and snippets.

@caueb
Created July 14, 2023 08:59
Show Gist options
  • Save caueb/6fa10a0c95fb2ac7b694771354822eff to your computer and use it in GitHub Desktop.
Save caueb/6fa10a0c95fb2ac7b694771354822eff to your computer and use it in GitHub Desktop.
AES encrypt binary and save the encrypted shellcode to a file.
import sys
from base64 import b64encode
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Random import get_random_bytes
import hashlib
KEY = get_random_bytes(16)
iv = 16 * b'\x00'
cipher = AES.new(hashlib.sha256(KEY).digest(), AES.MODE_CBC, iv)
try:
plaintext = open(sys.argv[1], "rb").read()
except:
print("File argument needed! %s <raw payload file>" % sys.argv[0])
sys.exit()
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
# Save the encrypted bytes to a file
encrypted_file = "caue.bin"
with open(encrypted_file, "wb") as file:
file.write(ciphertext)
print('AESkey[] = { 0x' + ', 0x'.join(hex(x)[2:] for x in KEY) + ' };')
#print('payload[] = { 0x' + ', 0x'.join(hex(x)[2:] for x in ciphertext) + ' };')
hex_bytes = [f'0x{x:02X}' for x in ciphertext]
num_bytes = len(ciphertext)
num_rows = (num_bytes + 15) // 16
# Print the hex bytes format with a maximum of 16 bytes per line
print(f'Shellcode in hex bytes format:')
print('payload[] = {')
for i in range(num_rows):
row_start = i * 16
row_end = min(row_start + 16, num_bytes)
row_hex = ', '.join(hex_bytes[row_start:row_end])
if i == num_rows - 1:
# Remove the last comma for the last row
print(f' {row_hex}')
else:
print(f' {row_hex},')
print('};')
print('[+] Saved encrypted file as caue.bin')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment