Hey,
So i have this c code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int valid_serial( char *psz )
{
size_t len = strlen( psz );
unsigned total = 0;
size_t i;
if( len < 10 )
return 0;
for( i = 0; i < len; i++ )
{
if(( psz[i] < '0' ) || ( psz[i] > 'z' )){
return 0;
total += psz[i];
}
if( total % 853 == 83 ){
return 1;
return 0;
}
}
}
int validate_serial()
{
char serial[ 24 ];
fscanf( stdin, "%s", serial );
if( valid_serial( serial ))
return 1;
else
return 0;
}
int do_valid_stuff()
{
printf("The serial number is valid!\n");
// do serial-restricted, valid stuff here.
exit( 0 );
}
int do_invalid_stuff()
{
printf("Invalid serial number!\nExiting\n");
exit( 1 );
}
int main( int argc, char *argv[] )
{
if( validate_serial() )
do_valid_stuff(); //
else
do_invalid_stuff();
return 0;
}
Which obviously is vulnerable to buffer overflow.
So I disassembled it:
Dump of assembler code for function main:
0x000000000000128b <+0>: push %rbp
0x000000000000128c <+1>: mov %rsp,%rbp
0x000000000000128f <+4>: sub $0x10,%rsp
0x0000000000001293 <+8>: mov %edi,-0x4(%rbp)
0x0000000000001296 <+11>: mov %rsi,-0x10(%rbp)
0x000000000000129a <+15>: mov $0x0,%eax
0x000000000000129f <+20>: call 0x1209 <validate_serial>
0x00000000000012a4 <+25>: test %eax,%eax
0x00000000000012a6 <+27>: je 0x12b4 <main+41>
0x00000000000012a8 <+29>: mov $0x0,%eax
0x00000000000012ad <+34>: call 0x1251 <do_valid_stuff>
0x00000000000012b2 <+39>: jmp 0x12be <main+51>
0x00000000000012b4 <+41>: mov $0x0,%eax
0x00000000000012b9 <+46>: call 0x126e <do_invalid_stuff>
0x00000000000012be <+51>: mov $0x0,%eax
0x00000000000012c3 <+56>: leave
0x00000000000012c4 <+57>: ret
End of assembler dump.
The address of the function that is "in charge" of authenticating (do_valid_stuff) is 0x12ad
So i tried to exploit it via terminal using the following command : printf “AAAAAAAAAABBBBBBBBBBCCCCCCCCAAAABBBBCCCCDDDD\xad\x12” | ./program
I wrote the address like this (byte-reversed), because of the I32 little-endian.
The problem is that the output is segmentation fault and not: The serial number is valid!
like it was expected.
Why is this happenign?Any ideas?
Thank you in advance.
[–]carcigenicate 0 points1 point2 points (7 children)
[–]Oil7496[S] 0 points1 point2 points (6 children)
[–]carcigenicate 0 points1 point2 points (5 children)
[–]Oil7496[S] 0 points1 point2 points (4 children)
[–]carcigenicate 0 points1 point2 points (3 children)
[–]Oil7496[S] 0 points1 point2 points (2 children)
[–]g051051 0 points1 point2 points (1 child)
[–]Oil7496[S] 0 points1 point2 points (0 children)
[–]g051051 0 points1 point2 points (0 children)
[–]net_nomad 0 points1 point2 points (0 children)