Flag Shop
Last updated
Last updated
Easy
150
I'm only selling 1 product and that's flags! Please make sure you calculate how much money you have before coming here.
34.126.175.135:8001
Perform Integer Overflow to overflow pay_price
to be < 100(balance).
Program is pretty simple and limited with what you can do, so it is pretty straightforward.
The only option you have is to exit or provide the number of flags you want to buy, so our vulnerability is in the number of flags we provide.
We can see that our input is placed into the quantity
variable, then the program calculates pay_price = flag_price * quantity;
, and then checks if our balance is more than pay_price
.
The thing to note is that the pay_price
variable is assigned a type of unsigned int
as seen at the start of the purchase
function.
This means that the value of pay_price
cannot go into the negative (unsigned), but the problem lies in where is doesn't check for how big the value of pay_price
might get.
The int
type has a maximum value that it can store, any number above that value would cause an integer overflow
, where the number wraps around and gives a smaller number than intended.
An integer overflow
occurs when a variable tries to store a value more than it can store.
Let's say a variable can only store 1 byte, meaning 0xff (255) is the maximum value it can store.
Well, what happens if you try to store 0x100 (256) in the same variable? It will overflow and the value stored in the variable will now be 0.
And if you try to store 0x101 (257) in the same variable, it will overflow and store the value 1 instead.
You can essentially think of this as an AND operator on the Least Significant Bits of the value, where max value of 0xff = value & 0xff
(& represents bitwise AND operator).
Searching online for the maximum value of int
, we find at https://learn.microsoft.com/en-us/cpp/c-language/cpp-integer-limits?view=msvc-170:
UINT_MAX Maximum value for a variable of type unsigned int. 4294967295 (0xffffffff)
So we can essentially craft a script that loops through the numbers of flags we can purchase, making sure to account for our integer overflow, and loop through till our pay_price
<= 100.