Basic Stack Overflow attacks in Linux

Source: Internet
Author: User

1.1 Linux Stack Overflow Protection Mechanism

The basic stack overflow attack is the first buffer overflow attack method. It is the basis of all other buffer overflow attacks. However, because this attack method takes a long time, the GCC compiler and Linux operating system provide some mechanisms to prevent this attack method from harming the system. The following describes the existing stack protection mechanism and the method to disable the corresponding protection mechanism, which provides a good experimental environment for further analysis of basic stack overflow.

1. Memory Address randomization Mechanism

In Ubuntu and other Linux kernel-based systems, the memory address randomization mechanism is used to initialize the stack. This will make it very difficult to guess the specific memory address.

The method to disable the memory address randomization mechanism is:

Sysctl-w kernel. randomize_va_space = 0

2. Shielding and protection mechanism for executable programs

For Federal systems, the executable program blocking protection mechanism is implemented by default. This mechanism does not allow execution of code stored in the stack, which makes the buffer overflow attack ineffective. This mechanism is not used by default in Ubuntu.

The method to disable the protection mechanism of executable programs is:

Sysctl-w kernel.exe c-shield = 0

3. gcc compiler gs Verification Code Mechanism

The gcc compiler provides special protection measures to prevent buffer overflow. The specific method is that gcc first places a random gs verification code before the buffer is written after the buf end address and the return address, check the value at the end of the buffer write operation. Generally, buffer overflow overwrites the memory from the low address to the high address. Therefore, if you want to overwrite the return address, you need to overwrite the gs verification code. In this way, you can compare the data of the gs verification code before and after writing to determine whether overflow exists.

To disable the gcc compiler's gs Verification Code mechanism, follow these steps:

Use the-fno-stack-protector option during gcc compilation.

4. ld linker stack segment unexecutable Mechanism

When linking a program, if all. o file stack segments are marked as unexecutable, so the entire library stack segment will be marked as unexecutable; on the contrary, even if there is only one. if the stack segment of file 0 is marked as executable, the stack segment of the entire library is marked as executable. The method to check the enforceability of stack segments is:

If you are checking the ELF Library: readelf-lW $ BIN | grep GNU_STACK, check whether the E tag exists.

If you are checking the generated. o file: scanelf-e $ BIN, check whether there is an X mark.

If the ld linker marks the stack segment as unexecutable, a segment error will still occur even if the eip is controlled to generate a jump.

To disable the unexecutable mechanism of the ld linker, follow these steps:

Use the-z execstack option during gcc compilation.

1.1 Basic Stack Overflow Attack principle and Experiment

Next, we will use an example of stack overflow attack to explain in detail the detailed steps of the basic stack overflow attack.

Before testing, we should first use the method described above to disable the corresponding stack protection mechanism.

  1. Root @ linux :~ /Pentest # sysctl-w kernel. randomize_va_space = 0
  2. Kernel. randomize_va_space = 0
  3. Root @ linux :~ /Pentest # sysctl-w kernel.exe c-shield = 0
  4. Error:"Kernel.exe c-shield"Is an unknown key

The Code is as follows:

  1. Root @ linux :~ /Pentest # cat vulnerable. c
  2. # Include <stdio. h>
  3. # Include <string. h>
  4. IntMain (IntArgc,Char** Argv ){
  5. CharBuffer [500];
  6. Strcpy (buffer, argv [1]);
  7. Return0;
  8. }

Compile source code:

 Root @ linux :~ /Pentest # gcc-fno-stack-protector-z execstack-g-o vulnerable. c

Use gdb to debug the program:

  1. Root @ linux :~ /Pentest # gdb vulnerable
  2. GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2
  3. Copyright (C) 2010 Free Software Foundation, Inc.
  4. License GPLv3 +: gnu gpl version 3 or later // Gnu.org/licenses/gpl.html>
  5. This is free software: you are free to change and redistribute it.
  6. There is no warranty, to the extent permitted by law. Type"Show copying"
  7. And"Show warranty" ForDetails.
  8. This GDB was configuredI686-linux-gnu".
  9. For bug reporting instructions, please see:
  10. <Http:// Www.gnu.org/software/gdb/bugs/>...
  11. Reading symbols from/root/pentest/vulnerable... done.
  12. (Gdb) disass main
  13. Dump of worker er codeForFunction main:
  14. 0x080483c4 <+ 0>: push % ebp
  15. 0x080483c5 <+ 1>: mov % esp, % ebp
  16. 0x080483c7 <+ 3>: and {1} xfffffff0, % esp
  17. 0x080483ca <+ 6>: sub {1} x210, % esp
  18. 0x080483d0 <+ 12>: mov 0xc (% ebp), % eax
  19. 0x080483d3 <+ 15>: add {1} x4, % eax
  20. 0x080483d6 <+ 18>: mov (% eax), % eax
  21. 0x080483d8 <+ 20>: mov % eax, 0x4 (% esp)
  22. 0x080483dc <+ 24>: lea 0x1c (% esp), % eax
  23. 0x080483e0 <+ 28>: mov % eax, (% esp)
  24. 0x080483e3 <+ 31>: call 0x80482f4 <strcpy @ plt>
  25. 0x080483e8 <+ 36>: mov {1} x0, % eax
  26. 0x080483ed <+ 41>: leave
  27. 0x080483ee <+ 42>: ret
  28. End of worker er dump.
  29. (Gdb)

  • 1
  • 2
  • 3
  • Next Page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.