aesencrypt.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # Red Team Operator course code template
  2. # payload encryption with AES
  3. #
  4. # author: reenz0h (twitter: @sektor7net)
  5. import sys
  6. from Crypto.Cipher import AES
  7. from os import urandom
  8. import hashlib
  9. KEY = urandom(16)
  10. def pad(s):
  11. # o = lambda x: x if isinstance(x, int) else ord(x) # handle data being bytes not string
  12. return s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size)
  13. def aesenc(plaintext, key):
  14. k = hashlib.sha256(key).digest()
  15. iv = 16 * '\x00'
  16. plaintext = pad(plaintext)
  17. cipher = AES.new(k, AES.MODE_CBC, iv)
  18. # o = lambda x: x if isinstance(x, int) else ord(x) # handle data being bytes not string
  19. return cipher.encrypt(bytes(plaintext))
  20. try:
  21. o = lambda x: x if isinstance(x, int) else ord(x) # handle data being bytes not string
  22. plaintext = open(sys.argv[1], "rb").read()
  23. except:
  24. print("File argument needed! %s <raw payload file>" % sys.argv[0])
  25. sys.exit()
  26. ciphertext = aesenc(plaintext, KEY)
  27. print('AESkey[] = { 0x' + ', 0x'.join(hex(ord(x))[2:] for x in KEY) + ' };')
  28. print('payload[] = { 0x' + ', 0x'.join(hex(ord(x))[2:] for x in ciphertext) + ' };')