<?xml version="1.0"?>
<rss version="2.0">
    <channel>
        <title>OpenRCE: Blog</title>
        <link>http://www.openrce.org/rss/feeds/blog</link>
        <description>OpenRCE: The Open Reverse Code Engineering Community</description>
                <item>
            <title>DLL Injection on Linux using Hotpatch</title>
                            <pubDate>Sun, 16 Oct 2011 12:23:26 -0500</pubDate>
                                        <link>https://www.openrce.org/blog/view/1761/DLL_Injection_on_Linux_using_Hotpatch</link>
                                        <author>bihariking &lt;email-suppressed@example.com&gt;</author>
                                                    <description>Introduction to Hotpatch&lt;br /&gt;
=========================&lt;br /&gt;
Hotpatch is a library that can be used to dynamically load a shared library&lt;br /&gt;
(.so) file on Linux from one process into another already running process,&lt;br /&gt;
without affecting the execution of the target process. The API is a C API, but&lt;br /&gt;
also supported in C++.&lt;br /&gt;
&lt;br /&gt;
The current version is 0.1.&lt;br /&gt;
Source code can be found on http://bit.ly/pi8fq7&lt;br /&gt;
&lt;br /&gt;
The limitations, directions on how to use, and possible uses of hotpatch will be&lt;br /&gt;
explained in this document.&lt;br /&gt;
&lt;br /&gt;
The main idea of hotpatch stems from the fact that in Linux, it is not easy to&lt;br /&gt;
load a library into another already running process. In Windows, there is an API&lt;br /&gt;
called CreateRemoteThread() that can load a library into another process very&lt;br /&gt;
easily with a couple of API calls. Hotpatch makes this functionality available&lt;br /&gt;
to Linux users and developers, with a single API call. Unlike other available&lt;br /&gt;
injection libraries, hotpatch restores the execution of the process to its&lt;br /&gt;
original state.&lt;br /&gt;
&lt;br /&gt;
The user can do the following with hotpatch:&lt;br /&gt;
- load his/her own .so file into an already running process&lt;br /&gt;
- invoke a custom symbol/function in that .so file&lt;br /&gt;
- pass arguments to that function as long as it is serialized to the form of a&lt;br /&gt;
&amp;nbsp;&amp;nbsp;byte buffer and length of the buffer. This shall be explained more later.&lt;br /&gt;
&lt;br /&gt;
Hotpatch is available as an API with a header file called &amp;quot;hotpatch.h&amp;quot; and a&lt;br /&gt;
.so file called &amp;quot;libhotpatch.so&amp;quot;, and also a commandline application called&lt;br /&gt;
&amp;quot;hotpatcher&amp;quot; which can inject .so files into processes via the commandline&lt;br /&gt;
itself. Hotpatch also comes with a test .so called &amp;quot;libhotpatchtest.so&amp;quot;&lt;br /&gt;
which can be used via the commandline &amp;quot;hotpatcher&amp;quot; application to test out&lt;br /&gt;
the working of hotpatch on any system. The &amp;quot;libhotpatchtest.so&amp;quot; has a symbol&lt;br /&gt;
&amp;quot;mysym&amp;quot; that can be invoked, and it writes to the &amp;quot;/tmp/hotpatchtest.log&amp;quot; file&lt;br /&gt;
with the timestamp at which the .so file was injected and anything else.&lt;br /&gt;
&lt;br /&gt;
Limitations&lt;br /&gt;
============&lt;br /&gt;
NOTE: Currently if hotpatch is compiled in 64-bit mode, it can inject libraries&lt;br /&gt;
only in 64-bit processes, and if compiled in 32-bit mode can inject libraries&lt;br /&gt;
only in 32-bit processes. It cannot inject from a 64-bit to a 32-bit process or&lt;br /&gt;
from a 32-bit to a 64-bit process.&lt;br /&gt;
&lt;br /&gt;
There are some limitations, the main being that the user can inject a library&lt;br /&gt;
.so file only in a process on which the user has privileges over. For example,&lt;br /&gt;
as the root user, hotpatch can inject libraries into any process, but as a&lt;br /&gt;
regular non-root user, hotpatch can inject libraries into only those processes&lt;br /&gt;
that hotpatch has access to, i.e. the user's processes and any other via sudo&lt;br /&gt;
privileges.&lt;br /&gt;
&lt;br /&gt;
The other limitation is that if the user needs to compile his shared library&lt;br /&gt;
with the linker options &amp;quot;-fPIC -nostartfiles&amp;quot; so that hotpatch can reliably load&lt;br /&gt;
the .so file.&lt;br /&gt;
&lt;br /&gt;
Another limitation is that injection for a particular .so file can happen only&lt;br /&gt;
once in the target process. Each library that is injected can be injected only&lt;br /&gt;
once into the target process.&lt;br /&gt;
&lt;br /&gt;
Usage: API&lt;br /&gt;
===========&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;hotpatch.h&amp;quot; header file needs to be included by the user. There are 3 main&lt;br /&gt;
API calls that matter. Each of them have to be called in the order as shown&lt;br /&gt;
below in the sample program.&lt;br /&gt;
&lt;br /&gt;
- hotpatch_t *hotpatch_create(pid_t pid, int verbose);&lt;br /&gt;
&lt;br /&gt;
This function takes a PID of the target process, and the verbosity level&lt;br /&gt;
(between 0 to 6), and returns an opaque object which contains further intimate&lt;br /&gt;
details about the process such as current library mappings, and locations of the&lt;br /&gt;
important functions needed for hotpatch to do its work.&lt;br /&gt;
&lt;br /&gt;
- int hotpatch_inject_library(hotpatch_t *hp,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;const char *sofile,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;const char *symbol,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;const unsigned char *data,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;size_t datalen,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;uintptr_t *out_addr,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;uintptr_t *out_result);&lt;br /&gt;
&lt;br /&gt;
This function takes the newly created hotpatch object, along with a path to the&lt;br /&gt;
shared library in the variable &amp;quot;sofile&amp;quot;, the optional function &amp;quot;symbol&amp;quot; to invoke,&lt;br /&gt;
along with the serialized arguments to the function provided in &amp;quot;data&amp;quot; and&lt;br /&gt;
&amp;quot;datalen&amp;quot; which are also optional. The return address of where the library was&lt;br /&gt;
loaded is returned in &amp;quot;out_addr&amp;quot; and the return value of the invocation of&lt;br /&gt;
&amp;quot;symbol&amp;quot; is returned in &amp;quot;out_result&amp;quot;. On success this returns 0 and on failure&lt;br /&gt;
returns -1.&lt;br /&gt;
&lt;br /&gt;
The verbosity levels can be adjusted accordingly from 0 to 6 to see debugging&lt;br /&gt;
information for investigating errors.&lt;br /&gt;
&lt;br /&gt;
The usefulness of the &amp;quot;data&amp;quot; and &amp;quot;datalen&amp;quot; parameters is extremely high. Suppose&lt;br /&gt;
the user has a custom function they want to invoke, and the arguments of the&lt;br /&gt;
function is a big struct or a class. The user can then write a wrapper function&lt;br /&gt;
that takes a serialized buffer of this struct/class along with the length of the&lt;br /&gt;
buffer and invoke that wrapper function. This wrapper function can then&lt;br /&gt;
deserialize this buffer into the struct/class as needed and call the actual&lt;br /&gt;
function that the user really wanted to invoke. This functionality is only&lt;br /&gt;
available by the API and not by the &amp;quot;hotpatcher&amp;quot; executable.&lt;br /&gt;
&lt;br /&gt;
- void hotpatch_destroy(hotpatch_t *hp);&lt;br /&gt;
&lt;br /&gt;
This function cleans up memory and resources used by the hotpatch opaque object.&lt;br /&gt;
&lt;br /&gt;
Sample Program&lt;br /&gt;
==============&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;hotpatch.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;pid_t pid = argc &amp;gt; 1 ? atoi(argv[1]) : 0;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;hotpatch_t *hp = hotpatch_create(pid, 1);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (hp) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;unsigned char *data = (unsigned char *)&amp;quot;my custom serialized data&amp;quot;;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;size_t datalen = strlen((char *)data) + 1;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;uintptr_t result1, result2;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;hotpatch_inject_library(hp, &amp;quot;libhotpatchtest.so&amp;quot;, &amp;quot;mysym&amp;quot;,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;data, datalen, &amp;amp;result1, &amp;amp;result2);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;hotpatch_destroy(hp);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Usage: Hotpatcher&lt;br /&gt;
==================&lt;br /&gt;
&lt;br /&gt;
The commandline &amp;quot;hotpatcher&amp;quot; can be executed with the &amp;quot;-h&amp;quot; option to see the&lt;br /&gt;
various options that are supported.&lt;br /&gt;
&lt;br /&gt;
A sample execution of &amp;quot;hotpatcher&amp;quot; into the current running shell can be done as&lt;br /&gt;
below:&lt;br /&gt;
&lt;br /&gt;
Let's say the library libhotpatchtest.so is in the current directory.&lt;br /&gt;
&lt;br /&gt;
bash&amp;gt; ./hotpatcher -l ./libhotpatchtest.so -s mysym -v1 $$&lt;br /&gt;
&lt;br /&gt;
On success the &amp;quot;/tmp/hotpatchtest.log&amp;quot; file can be checked if it has the&lt;br /&gt;
timestamp of the injection.&lt;br /&gt;
&lt;br /&gt;
Uses of Hotpatch&lt;br /&gt;
=================&lt;br /&gt;
Most uses of hotpatch are related to custom modifications of processes for which&lt;br /&gt;
the users do not have source code available.&lt;br /&gt;
&lt;br /&gt;
- System administrators can use hotpatch to inject their own custom libraries in&lt;br /&gt;
&amp;nbsp;&amp;nbsp;already running processes and change behavior as per requirement. Some such&lt;br /&gt;
behavior could be adding a library that creates a thread and heartbeats to a&lt;br /&gt;
monitoring system.&lt;br /&gt;
&lt;br /&gt;
- Many software applications, that are not mission critical, are not built with&lt;br /&gt;
&amp;nbsp;&amp;nbsp;mechanisms to update their software without having to stop the application and&lt;br /&gt;
restarting it. Hotpatch can help modify applications to restart and do other&lt;br /&gt;
fancy tricks without losing the PID and the other states such as file handles of&lt;br /&gt;
the applications that might be very useful or too risky to let go.&lt;br /&gt;
&lt;br /&gt;
- Users can inject a library and then set up RPC service calls for the target&lt;br /&gt;
&amp;nbsp;&amp;nbsp;application without changing any code.&lt;br /&gt;
&lt;br /&gt;
- Users can inject a library and with import table modifications can instrument&lt;br /&gt;
&amp;nbsp;&amp;nbsp;the target application for things like profiling, reverse engineering and also&lt;br /&gt;
debugging. This is useful as it does not necessarily need the application to be&lt;br /&gt;
recompiled and performance numbers can be extracted. The code to do import table&lt;br /&gt;
modifications is currently outside the scope of hotpatch.&lt;br /&gt;
&lt;br /&gt;
- Users can create threads in other processes and make them work like a cluster&lt;br /&gt;
&amp;nbsp;&amp;nbsp;of processes that they control.&lt;br /&gt;
&lt;br /&gt;
- Users can modify another application and make it perform better by doing&lt;br /&gt;
&amp;nbsp;&amp;nbsp;tricks in the injected code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
</description>
                    </item>
            </channel>
</rss>
