Writing shell code is perceived as a black art by many. The good news is that it is far from that. Anyone with a basic knowledge of programming and a desire to catch up on some basic assembly programming and CPU architecture can churn out shell code in less than an hour.
Lots of people have asked me for clarifications and tips for writing shell code. To this purpose, I will try to introduce how to quickly write your first shell code program. Believe me you will feel awesome. I remember I did!
Lets try to look at a basic loop in assembly:
start: xor ecx,ecx mov ecx,5 loop start
XORing the ecx register with itself is a standard way to set it as zero. Every time the loop progresses through the code, the value in the ecx register is decremented by 1, until it is zero.
Lets write a small hello world program:
#include <stdio.h>
int main(){
write(1,"hello world: \n",15);
exit(0);
}
Now lets turn this into assembly:
gcc -O2 -S -c foo.c
Now we have:
section .text global _start _start: mov edx,len ; mov ecx,msg ; mov ebx,1 ; mov eax,4 ; int 0x80 ; ;and exit mov ebx,0 ; mov eax,1 ; int 0x80 ; section .data msg db "Hello, world!",0xa len equ $ - msg
Following this, we use:
nasm -f elf test.asm ld -s -o hello test.o
Voila! You have an executable! Moreover if you type in ls -all, you will notice the size of the assembly is only 1/9th of the size of the plain text code. Now lets use the tool available at www.safemode.org and type in:
s-proc -p test.o
To get… awesome shiny shell code.
char shellcode[] = "\x7f\x45\x4c\x46\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x01\x00\x03\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x34\x00\x00\x00\x00" "\x00\x28\x00\x08\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" "\x00\x00\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00" "\x80\x01\x00\x00\x22\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x10\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x01\x00" "\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\xb0\x01\x00\x00\x0e" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00" "\x00\x00\x00\x00\x0d\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\xc0\x01\x00\x00\x1f\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x16" "\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\xe0\x01\x00\x00\x3a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x01\x00\x00\x00\x00\x00\x00\x00\x20\x00\x00\x00\x02\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x02\x00\x00\x70" "\x00\x00\x00\x06\x00\x00\x00\x06\x00\x00\x00\x04\x00\x00\x00" "\x10\x00\x00\x00\x28\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x90\x02\x00\x00\x1a\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x30" "\x00\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\xb0\x02\x00\x00\x08\x00\x00\x00\x05\x00\x00\x00\x01\x00\x00" "\x00\x04\x00\x00\x00\x08\x00\x00\x00\xba\x0e\x00\x00\x00\xb9" "\x00\x00\x00\x00\xbb\x01\x00\x00\x00\xb8\x04\x00\x00\x00\xcd" "\x80\xbb\x00\x00\x00\x00\xb8\x01\x00\x00\x00\xcd\x80\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x48\x65\x6c" "\x6c\x6f\x2c\x20\x77\x6f\x72\x6c\x64\x21\x0a\x00\x00\x00\x54" "\x68\x65\x20\x4e\x65\x74\x77\x69\x64\x65\x20\x41\x73\x73\x65" "\x6d\x62\x6c\x65\x72\x20\x32\x2e\x30\x33\x2e\x30\x31\x00\x00" "\x00\x2e\x74\x65\x78\x74\x00\x2e\x64\x61\x74\x61\x00\x2e\x63" "\x6f\x6d\x6d\x65\x6e\x74\x00\x2e\x73\x68\x73\x74\x72\x74\x61" "\x62\x00\x2e\x73\x79\x6d\x74\x61\x62\x00\x2e\x73\x74\x72\x74" "\x61\x62\x00\x2e\x72\x65\x6c\x2e\x74\x65\x78\x74\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x04\x00\xf1\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x03\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x03\x00\x02\x00\x12\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16\x00\x00\x00\x0e\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\xf1\xff\x0b\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x10\x00\x01\x00\x00\x74\x65\x73" "\x74\x2e\x61\x73\x6d\x31\x00\x5f\x73\x74\x61\x72\x74\x00\x6d" "\x73\x67\x00\x6c\x65\x6e\x00\x00\x00\x00\x00\x00\x00\x06\x00" "\x00\x00\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
Till next time, adios!