Writeup from InCTFi 2019 bartender
tl;dr Windows 32-bit SEH exploitation
Category: Pwn
Challenge Points: 1000
Solves: 1
The challenge is a 32-bit windows executable. This is a simple windows exploiation challenge based on the Structured Exception Handling. Download the files here
Write-up
1 | PS D:\Downloads\Win_BE\bartender> .\bartender.exe |
A setting.xml
was given with the handout
1 |
|
This file was given to inform that SEHOP was disabled in remote Windows server.
This is a menu driven program, you can create a drink, modify a drink and so on. The drinks and ingredients are stored as an global array.
1 | struct ingredient{ |
When you make a drink, you get to add ingredients with a key that is a unique and prime number. The ingredient->key of all the ingredients you select are multiplied to from drink->value. Since the keys are prime numbers, the ingredients can be indentified by checking if the value is completely divisible by the key.
You can also see that a catFlag
function is given to make exploitation easy.
Vulnerabilities
Stack Bufferoverflow
in theadd Ingredient
function.Out of Bounds
read inmake drink
functionality.
Exploitaiton
First thing you would notice is the stack bufferflow
that can be seen in the add ingredient
option. You cannot exploit this bug by straightaway as stack cookies are enabled and also the main function doesn’t return as it is an while loop that terminates only when exit
is called.
Also when you are overflowing, a stack buffer overrun error is returned in the addIngredient
function call. This is because strncpy_s
is used to copy name of the ingredient to heap.
1 | errno_t strncpy_s( |
strncpy_s
checks if the len of the src is greater than the count. Check out the docs for strncpy_s to know more.
To bypass this, all you have to do is put a \x00
in your exploit, such that the the len of the src is less than count.
You are able to overwrite the SEH handler with this overflow now, but you still have to trigger an exception to redirect to execution flow to overwritten handler.
/SAFESEH:NO
compiler flag is also enabled, you check this by looking for the __safe_seh_table
which will not be present if safeSEH is disabled. If enabled, the last two bytes of the exception handlers in the exception chain are stored in the table, and validity is checked before a exception call.
There is a division by zero
bug in the change drink
functionality when you give 0 as an option when removing an ingredient. You can use this bug to trigger the overwritten exception handler.
Now all you have to do is overwrite the SEH handler with address of catFlag
.ASLR
is enabled though. In Windows ASLR randomizes the image base everytime it is loaded into memory and this includes the executable image. You can use the .data section out of bound
read vulnerability in the make drink
functionality. The index read from the user is not validated. When you select an ingredient the key of that indredient->key([ingredient+4]) is used to form the drink->value. Provide an index such that the address+4 points to a image address. This address gets multiplied with drink->value and hence the image leak is obtained.
Finally you can overwrite the SE handler with catFlag
and trigger the exception to get the flag.
1 | from pwn import * |