0

I've heard in a lot of places that buffer overflows, illegal indexing in C like languages may compromise the security of a system. But in my experience all it does is crash the program I'm running. Can anyone explain how buffer overflows could cause security problems? An example would be nice.

I'm looking for a conceptual explanation of how something like this could work. I don't have any experience with ethical hacking.

1 Answer 1

1

First, buffer overflow (BOF) are only one of the method of gaining code execution. When they occur, the impact is that the attacker basically gain control of the process. This mean that the attacker will be able to trigger the process in executing any code with the current process privileges (depending if the process is running with a high or low privileged user on the system will respectively increase or reduce the impact of exploiting a BOF on that application). This is why it is always strongly recommended to run applications with the least needed privileges.

Basically, to understand how BOF works, you have to understand how the code you have build gets compiled into machine code (ASM) and how data managed by your software is stored in memory.

I will try to give you a basic example of a subcategory of BOF called Stack based buffer overflows :

Imagine you have an application asking the user to provide a username. This data will be read from user input and then stored in a variable called USERNAME. This variable length has been allocated as a 20 byte array of chars.

For this scenario to work, we will consider the program's do not check for the user input length.

At some point, during the data processing, the user input is copied to the USERNAME variable (20bytes) but since the user input is longer (let's say 500 bytes) data around this variable will be overwritten in memory :

Imagine such memory layout :

size in bytes     20         4          4            4  
data          [USERNAME][variable2][variable3][RETURN ADDRESS]

If you define the 3 local variables USERNAME, variable2 and variable3 the may be store in memory the way it is shown above.

Notice the RETURN ADDRESS, this 4 byte memory region will store the address of the function that has called your current function (thanks to this, when you call a function in your program and readh the end of that function, the program flow naturally go back to the next instruction just after the initial call to that function.

If your attacker provide a username with 24 x 'A' char, the memory layout would become something like this :

size in bytes     20         4          4            4  
data          [USERNAME][variable2][variable3][RETURN ADDRESS]
new data      [AAA...AA][   AAAA  ][variable3][RETURN ADDRESS]

Now, if an attacker send 50 * the 'A' char as a USERNAME, the memory layout would looks like this :

size in bytes     20         4          4            4  
data          [USERNAME][variable2][variable3][RETURN ADDRESS]
new data      [AAA...AA][   AAAA  ][   AAAA  ][[   AAAA      ][OTHER AAA...]

In this situation, at the end of the execution of the function, the program would crash because it will try to reach the address an invalid address 0x41414141 (char 'A' = 0x41) because the overwritten RETURN ADDRESS doesn't match a correct code address.

If you replace the multiple 'A' with well thought bytes, you may be able to :

  • overwrite RETURN ADDRESS to an interesting location.
  • place "executable code" in the first 20 + 4 + 4 bytes

You could for instance set RETURN ADDRESS to the address of the first byte of the USERNAME variable (this method is mostly no usable anymore thanks to many protections that have been added both to OS and to compiled programs).

I know it is quite complex to understand at first, and this explanation is a very basic one. If you want more detail please just ask.

I suggest you to have a look at great tutorials like this one which are quite advanced but more realistic

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.