Why is my 'buffer overflow' program not resulting in the buffer being overflowed on Windows but does overflow when using the program on Linux?

0

I am currently learning about buffer overflows and programmed an example of one in C which uses two buffers, buffer two is deliberately supposed to have more bytes than it should be able to handle but somehow in Windows, it shows that the extra bytes are still in buffer two and buffer one still results as having its original bytes intact when it is supposed to be overwritten. The only change I noticed was when I added exactly double the amount of bytes that the buffer could take and this caused buffer one to be completely empty.

Yet when I executed the exact same program on my Linux computer, buffer one was successfully overflowed with buffer two's excess bytes. So my question is: what is going on in Windows that is preventing me from having the same output and how, if possible can I fix it?

#include <stdio.h>
#include <string.h>

[int main(int argc, char *argv\[\]){
    int value = 5;
    char buffer_one\[8\], buffer_two\[8\];
    
 strcpy(buffer_one, "one"); /* Put "one" into buffer_one. */
 strcpy(buffer_two, "two"); /* Put "two" into buffer_two. */
 
 printf("\[BEFORE\] buffer_two is at %p and contains \'%s\'\n", buffer_two, buffer_two);
 printf("\[BEFORE\] buffer_one is at %p and contains \'%s\'\n", buffer_one, buffer_one);
 printf("\[BEFORE\] value is at %p and is %d (0x%08x)\n", &value, value, value);
 
 printf("\n\[STRCPY\] copying %d bytes into buffer_two which has %d bytes \n\n", strlen(argv\[1\]), sizeof(buffer_two));
 strcpy(buffer_two, argv\[1\]); /* Copy first argument into buffer_two. */
 
 printf("SIZE OF BUFFER_TWO: %d\n", sizeof(buffer_two));
 printf("IN BUFFER 2: %d bytes\n", strlen(buffer_two));
 printf("IN BUFFER 1: %d bytes\n", strlen(buffer_one));
 
 printf("\[AFTER\] buffer_two is at %p and contains \'%s\'\n", buffer_two, buffer_two);
 printf("\[AFTER\] buffer_one is at %p and contains \'%s\'\n", buffer_one, buffer_one);
 printf("\[AFTER\] value is at %p and is %d (0x%08x)\n", &value, value, value);
}
**The Execution Commands**

overflow 123456789012345

**The output:**

[BEFORE] buffer_two is at 000000000062FE00 and contains 'two'
[BEFORE] buffer_one is at 000000000062FE10 and contains 'one'
[BEFORE] value is at 000000000062FE1C and is 5 (0x00000005)

[STRCPY] copying 15 bytes into buffer_two which has 8 bytes

SIZE OF BUFFER_TWO: 8
IN BUFFER 2: 15
IN BUFFER 1: 3
[AFTER] buffer_two is at 000000000062FE00 and contains '123456789012345'
[AFTER] buffer_one is at 000000000062FE10 and contains 'one'
[AFTER] value is at 000000000062FE1C and is 5 (0x00000005)
c
overflow
asked on Stack Overflow Oct 2, 2020 by user13055480

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0