Advanced return library function Exploit code implementation (I)
Source: Internet
Author: User
Article title: advanced return library function Exploit code implementation (I ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
This article can be roughly divided into two parts. The previous section describes the advanced function technology of the returned database. Some existing ideas, or similar ones, have been published by others. However, these important technical information resources are scattered. In general, the source code that comes with the implementation of different platforms is not very educational or has no effect at all. Therefore, I decided to collect some useful resources and some of my own ideas. in this article, it should be helpful to help people with reference. According to the information published in the numerous security lists, it should be determined that such information is by no means an existing public awareness.
The second part focuses on the PaX-protected system and implements stack buffer overflow through different channels. Currently, PaX performance has been improved much more, increasing security through the method of stack random address processing and function library address ing, and challenging exploit coders with a cautious attitude. The initial method is to directly call the dynamic link flag to determine whether the program process is presented. This method is very common, and the conditions required for successful implementation are quite easy.
Since the Intel platform is slightly different under PaX protection, the general demonstration source code is designed for the linux i386 glibc system. PaX is not considered very stable by most people; however, existing technologies (described by linux i386 glibc) can be easily implemented on other systems/systems, it can be used to escape unexecutable security designs, including hardware-level protection implementation.
Assuming that the reader has the basic knowledge of exploit technology, he has already understood the article [1] [2] before further learning, [1] [2] describes the actual operations performed on ELF.
2 Traditional function library technology
The traditional function library technology is well described in article [2], so here is a simple summary. This technology is widely used to escape the unexecutable protection of stacks. Unlike locating the return code address in the stack, defective functions are returned to the memory occupied by the dynamic library. The following stack is used to construct a buffer overflow stack:
<Stack growth direction
Memory address growth direction>
Int32 will be overwritten by the return address of the defective function
(*) Buffer fillup will be overwritten by the saved $ ebp.
The function that contains buffer overflow is returned to the address of the function_in_lib library function and "executable" is restored ". By changing the pointer of the function, dummy_int32 will be used as the return address of parameters such as arg_1 and arg_2 and transferred to the address of the system calling function in the function library (libc system ()), that is, the instruction sequence of the library function, so that arg_1 points to "/bin/sh ".
3 Multiple join return function library calls
3.1 limitations of traditional methods
The technology mentioned above has two obvious limitations. First, it is impossible to request to call another parameter function after the function_in_lib function. Why? When function_in_lib is returned, it will be restored to be executable at the address of dummy_int32. In this way, it becomes another library function, and its parameters will have to occupy the same space required by the function_in_lib parameters. Sometimes, this is not a problem (see column in [3)
Note that multiple function calls are frequent. For example, a defective application temporarily enters the superuser permission state when needed (for example, a setuid application can seteuid (getuid ())), generally, before calling system (), you must call setuid (0) to restore the superuser permission status in exploit code implementation.
The second limitation is that the parameter variables in the function_in_lib function cannot contain zero bytes (a typical case is that the string processing program causes overflow. if there are zero bytes, the processing will stop ), the following two different methods are used to call multiple library functions to implement exploit.
3.2 "esp [stack pointer] growth" method
This method uses the "fomitframepointer" compilation option (this compilation parameter usually does not save the frame pointer in the function register to avoid saving, establishing, and restoring the frame pointer) designed for compiling files. under such compiling conditions, the typical function ends like this:
Eplg:
Addl $ LOCAL_VARS_SIZE, % esp
Ret
Assume that f1 and f2 are the function addresses located in the library functions. we create the following overflow string:
<Stack growth direction
Memory address growth direction>
| F1 | eplg | f1_arg1 | f1_arg2 |... | f1_argn | PAD | f2 | dmm | f2_args...
^
|
| |
|
| Int32 will be overwritten by the return address of the defective function
PAD is composed of non-zero bytes. its length increases to the space occupied by f1 and its parameter variable addresses, which should be equal to LOCAL_VARS_SIZE. (See above)
How does it work? If a defective function is returned to the address f1, f1 returns to the end of the function, and the command at the end
"Addl $ LOCAL_VARS_SIZE, % esp" will add LOCAL_VARS_SIZE to the stack pointer, so that the pointer will point to address f2 and store it. The "ret" command at the end will return the address of f2. in this way, two functions are called in one row.
Similar technologies are also described in [5. The returned results in text [5] are slightly different to the end of a standard function. some program (library function) images have the following command sequence:
Popret:
Popl any_register
Ret
This order will compile the optimized standard ending result. Elegant
Now let's build the following Stack
<Stack growth direction
Memory address growth direction>
Int32 will be overwritten by the return address of the defective function
The working principle is similar to that of the previous column sub-statement. except that the stack pointer is not increased by LOCAL_VARS_SIZE, the "popl any_register" command moves the stack pointer four bytes, all parameter variables of f1 can be transferred to the address of f1 up to four bytes.
If the command order is as follows:
Popret2:
Popl any_register_1
Popl any_register_2
Ret
In this way, we can pass each of the two parameters in four bytes to the f1 address.
The problem behind the technology is that it is impossible to use more than three pops (out-of-stack commands) in the form of "popret" at the same time. therefore, now we can only use the changes mentioned above.
I can find similar ideas in article [6], but it is a pity that it is very bad.
Note that functions of any form can be joined in this way. Note: We do not need to know the precise positioning in the stack (that is, we do not need to know the precise pointer values in the stack). of course, if you call a function to request a pointer to a variable in an array parameter and the pointer is in our stack, we need to know its exact address.
3.3 stack frame forgery (see article [4])
In this 2nd, the technology was designed to attack programs that did not use the "fomitframepointer" compilation option. Its function ends with the following:
Leaveret:
Leave
Ret
Whether or not optimization options are used, the gcc compiler always ends with "ret" and "leave. therefore, we cannot find any meaningful technology like "esp growth" in this binary file (but note the end of section 3.5)
In fact, the libgcc. a document explains that when you use the fomitframepointer compilation options to compile the target file, the compiler is connected to an executable file by default during the compilation process. Therefore, the command sequence such as "add $ imm, % esp; ret" can be found in these execution files. However, we cannot rely on these characteristics of gcc, because it depends on more factors (gcc version, compilation and usage options, etc)
Instead of "esp [frame pointer] growth", return to "leaveret" through the function ". Stack construction should be logically divided into different parts. the general exploit code should be close to "leaveret.
<Stack growth direction
Memory address growth direction>
Save base address register defect function return address
| Buffer fillup (*) | fake_ebp0 | leaveret |
|
|
++ (*) In this case, buffer fillup cannot be overwritten and written in the stack frame pointer.
|
V
| Fake_ebp1 | f1 | leaveret | f1_arg1 | f1_arg2...
|
| First stack frame
++
|
V
| Fake_ebp2 | f2 | leaveret | f2_arg1 | f2_argv2...
|
| Second stack frame
+...
Fake_ebp0 is the address of the first stack frame, fake_ebp1 is the address of the second stack frame, and so on.
Now, some ideas will be presented below
1) the end of the defective function (leave; ret) assigns the address of fake_ebp0 to the stack base pointer and returns it to leaveret.
2) the end two commands (leave; ret) put the fake_ebp1 address to the stack base pointer and return the address to f1.
3) after f1 is executed, leaveret is returned.
Repeat step 2) 3. use f1, f2 ,... Fn instead.
The techniques returned to the end of the function in article [4] are not used too much. Therefore, the author proposes that the stack should be constructed as follows to let the exploit code return to the back of the library function before the F function, do not return to the F function itself. this technology is similar to the previous one. However, we will soon face this situation. when F function only connects tables (PLT) through the process, it is impossible to add an address offset to the address of function F. Instead, it will only return to its own address.
Note: To use this technology, you must be aware of precise location of counterfeit stack frames, because fake_ebp must be set according to rules. If all stack frames are located behind buffer fillup, you must know the value of the stack pointer after overflow. However, if we know how to control a spoofed stack frame to locate it in a known memory area (static variables are more suitable), there is no need to guess the value of the stack pointer.
It is possible to attack a program that uses cheap options such as fomitframepointer. in this case, we do not need to find the leave & ret code in the program, but usually it can be found in some conventional join processes. Therefore, we need to change these zero blocks.
| Int32 will be overwritten by the return address of the defective function
Two leaverets are necessary because defective functions do not set Stack pointers when they are returned. Given the advantage of "frame forgery" teaching "stack pointer growth. In some cases, it is necessary to use this method to launch attacks.
3.4 embed zero bytes
Another problem is that the parameter passed to a function contains zero bytes. When multiple functions are effectively called, there is a simple solution: the first called function embeds zero byte to the address of the parameter of the next function to be called.
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