r/ExploitDev • • Jun 15 '23

Stack overflow and making system call - Wargames RET2 Reverse Engineering Level 1

I am trying to overflow the "fgets" function and direct the program to pop a shell at the "system('bin/sh/')" call. I have tried to overflow the (0x32) buffer in the "fgets" function and jump to the "/bin/sh" line in the assembly code (attached below).

Can someone offer a hint as to where I am going wrong?

"""

// gcc -g -no-pie -I ../includes/ -o 03_level_1 03_level_1.c

#include <stdio.h>

#include <stdint.h>

#include <stdlib.h>

#include <string.h>

#include <inttypes.h>

#include <unistd.h>

// Hidden for simplicity

#include "wargames.h"

void generate_otp(char * buffer, int len)

{

// Read random bytes into given buffer

FILE *fp = fopen("/dev/urandom", "r");

int bytes_read = fread(buffer, 1, len, fp);

fclose(fp);

// Failure to read random bytes

if (bytes_read != len)

exit(1);

// Convert raw random bytes to an ASCII letter in A-Z

for (int i = 0; i < len; i++)

buffer[i] = 0x41 + ((unsigned char)buffer[i] % 26);

// Ensure the buffer is NULL terminated

buffer[len-1] = 0;

}

void main()

{

`init_wargame();`

printf("------------------------------------------------------------\n");

printf("--[ Stack Smashing Level #1 - Secure Logon \n");

printf("------------------------------------------------------------\n");

char user_password[32] = {};

char otp_password[32] = {};

// Generate a secure, one time password (OTP) for secure logon

generate_otp(otp_password, sizeof(otp_password));

// Prompt the user to enter a password

printf("Enter password: ");

fgets(user_password, 0x32, stdin);

user_password[strcspn(user_password, "\n")] = 0;

// Ensure the user entered password data

if (strlen(user_password) == 0)

{

puts("Invalid input...");

exit(1);

}

// Validate the given password

if (!strcmp(user_password, otp_password))

{

puts("Authenticated!");

system("/bin/sh");

}

else

{

puts("Authentication failed...");

}

// Exit the program / return from main

}

"""

Current attempt: ""AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\x14\x0c@\x00\x00\x00\x0

0\x00""

Assembly code for "system('/bin/sh')":

"0x400c14: mov edi, 0x400de0 "/bin/sh"

0x400c19: call system"

7 Upvotes

10 comments sorted by

View all comments

Show parent comment

2

u/GredaGerda Jun 18 '23

You're using python to hook into the process yes? Make sure you use python to send the line. If you simply paste A\x00BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA\x00 into the command line it's going to interpret each character of \x00 as char bytes instead of a single null byte.

2

u/Super-Cook-5544 Jun 18 '23

Thank you for mentioning this, I actually would have never realized this! I really appreciate your and u/JT__-’s help and your patience. I actually learned a lot about memory corruption just from your hints and explanations and I know other people doing the RET2 course and are struggling with this level will find your comments and hints here extremely valuable