首页 > 技术点滴 > Reverse Engineering with LD_PRELOAD

Reverse Engineering with LD_PRELOAD

2015年12月20日 baoz 阅读评论

This paper is about the LD_PRELOAD feature, and how it can be useful for reverse engineering dynamically linked executables. This technique allows you to hijack functions/inject code and manipulate the application flow.

Compiling Methods


Generally there are two ways of producing an executable. The first method is the static compilation. During the compilation process the executable that will be produced, will be independent and will include all it needs to function properly. The advantage of this method is mainly portability as the user is not required to install anything in order to use the application. The disadvantage is a relatively oversized executable, also bugs that originate from an outside component will not be fixed, since the executable is not linked. The other method is the dynamically produced executable. It is dependent on shared libraries to function, and corresponding to changes in the shared libraries. For better or worse, this can be an advantage and a disadvantage. Dynamically linked executables by their nature are more lightweight than the static produced executables.

The gcc compiler will by default produce a dynamically linked executable, unless you specify the ‘-static’ parameter to gcc.

You can browse through the dynamically linked executable dependencies list, using the ldd utility. Following this:

Each dependency presenting the library it depends on, while the adress we see, is the one it would be mapped to during runtime. This article revolves around dynamically linked executables as they can be manipulated by the LD_PRELOAD.

Runtime Linker


Runtime linker’s purpose is to bind the dynamically linked executables to its dependencies by all means. Taking care of resolving the symbols and invoking the initialization functions of shared libraries as well as the finalization functions, if any. It also provides a set of functions to allow an application to load and unload libraries at runtime. This is often refereed to as Application Plug-in feature.

LD_PRELOAD is an environment variable that affects the runtime linker. It allows you to put a dynamic object, that will create some sort of a buffer layer, between the application references and the dependencies. It also grants you the possibility of linking with the application and relocating symbols/references.

To simplify the situation. This is a man-in-the middle attack between the program and the needed libraries ;) Limitations


The LD_PRELOAD option does not effect applications that have been chmod’ed with +s (setgid/setuid) flag. Unless the user that runs the program already has a root privileges. Also on some platforms, to be able to use the LD_PRELOAD option the user should already have root privileges. All the examples brought up in this paper assume that you will use your own machine as a part of your reverse engineering framework. But it would work regardless, if you have root privileges elsewhere.

Hack in a box


The simplest thing that LD_PRELOAD allows is to hijack a function. In order to do it, we will create a shared library that will include the implementation of the function that we wish to hijack. But first let’s take a look at our challenge:

In this example we got a simple password-matching challenge. It uses the strcmp() function for comparing the two strings. Lets attack it by hijacking the strcmp() function to see who’s running against who and to ensure it always returns equal strings!

Now let’s compile our strcmp-hijack.c snippet as a shared library, by doing this:

Before attacking, let’s see if our challenge does what we expect it to do, by doing this:

Now let’s attack it using LD_PRELOAD, by doing this:

Our evil shared library has done its job. We hijacked the strcmp() function and made it return a fixed value. Also we put a debug print that shows what the two arguments are, that have been sent to the function. Now we know what the real password is as well.

Notice I am using bash shell. And LD_PRELOAD is an environment variable, which means it is up to your shell to set this variable. If you have troubles to set it, you should refer to your shell man page, in order to find out how setting environment variable can be done.

Gatekeeper


Hijacking functions is fun. But becoming a wrapper to a function is more powerful. As a wrapper function we will accept the arguments that have been sent to the original function, pass ’em along to the original function and examine the result. Then we could decide if we want to return the original value, or fix the return value. Calling the original function would be done by using a pointer to the original function address. We will then use the runtime linker api, thus the dl*() function family.

This is our new challenge, which is a little bit more complex:

This challenge principle is quite simple. It performs a MD5 hash on the given filename then buffers up the same amount of bytes as the filename length itself, and performs the same MD5 function on the payload. Then it compares the two, without explicitly using the strcmp() function. This forces us to find a weak spot. This challenge’s weak spot is the crypt() function which can be hijacked. The plan is to buffer up the 1st hash returned by the 1st crypt() call, then fix up the 2nd crypt() return value so it will match the 1st hash, and pass the comparing part smoothly.

This is our new evil shared library:

We will start by simply testing the challenge, doing this:

Now let’s try it again with our evil shared library, by doing this:

Again using LD_PRELOAD, we just bypassed the mechanism and went straight on.

Cerberus


After we did a few high level tricks. It is time to get our hands a little dirty. And this means involving Assembly in the mix. The next challenge might look a little bit odd, check it out:

As you can see in this challenge our input doesn’t count. The statement will always be incorrect. Regardless of any parameters that will be passed to main(). But there is a way out! To understand this trick, let’s first disassemble the main function, following this:

The IF statement takes place in <main+36>, the ‘je’ is not evaluated. Therefore we do not jump to <main+63> but rather continue to <main+38>. There it pushes the string into the stack and calls printf() — Hold on! Are you thinking what I’m thinking? *RET HIJACK* ;-)

As always before attacking, we test the challenge, by doing this:

It is a pretty straight forward challenge. Now let’s attack this challenge using our evil shared library, doing this:

Our attack succeeds. Manipulating the printf() function return address has made the impossible possible. I will not get into details about this attack. As it alone deserved a paper about it. I would say that I’ve cheated a little bit, as this code has created a stack corruption, and I have used the exit() function to do the some dirty work for me. Notice that the printf() family functions are unusual in the way they meant to accept unlimited number of parameters. The code above is meant to be a proof of concept to how it is possible to manipulate the program flow dynamically.

To conclude this paper I would say that LD_PRELOAD is a powerful feature, and can be used on many purposes and Reverse Engineering is only one.

Contact


Izik <izik@tty64.org> [or] http://www.tty64.org

  1. 本文目前尚无任何评论.