<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Michal Strehovský: home &#187; Projects</title>
	<atom:link href="http://migeel.sk/blog/category/projects/feed/" rel="self" type="application/rss+xml" />
	<link>http://migeel.sk</link>
	<description>Windows development and other random stuff</description>
	<lastBuildDate>Mon, 15 Mar 2010 14:31:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Injecting code into executables with C</title>
		<link>http://migeel.sk/blog/2007/07/30/injecting-code-into-executables-with-c/</link>
		<comments>http://migeel.sk/blog/2007/07/30/injecting-code-into-executables-with-c/#comments</comments>
		<pubDate>Mon, 30 Jul 2007 11:33:31 +0000</pubDate>
		<dc:creator>Michal Strehovsky</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://beta.migeel.sk/blog/2007/07/30/injecting-code-into-executables-with-c/</guid>
		<description><![CDATA[In this article, I would like to answer a commonly asked question: is it possible to use my project &#8211; PE-inject with C? The short answer: yes.
The problem with PE-inject is that it is written in Delphi. Even though a DLL version of PE-inject is available, all the samples are written in Delphi too and [...]]]></description>
			<content:encoded><![CDATA[<p>In this article, I would like to answer a commonly asked question: is it possible to use my project &#8211; <a href="http://migeel.sk/programming/pe-inject">PE-inject</a> with C? The short answer: yes.</p>
<p>The problem with PE-inject is that it is written in Delphi. Even though a DLL version of PE-inject is available, all the samples are written in Delphi too and for a C programmer, this can be pretty confusing. So, this article will show you how easy it is to use PE-inject with C.</p>
<p>We will create a program which will modify an EXE file in such a way, that the user will be first prompted with a question asking her if she is really sure about running the program. If her answer is Yes, the program will run. Otherwise, it will terminate.</p>
<h3>Creating the injection DLL</h3>
<p>First, we need to create a DLL file containing the code to be placed in the EXE files. The <a href="http://docs.migeel.sk/PE-inject">PE-inject documentation</a> says that the library must contain a function called BeforeHandlers or AfterHandlers. We will not talk about the difference between them. For us, the only important thing to know is that when the DLL is injected using PE-inject into an executable, both functions (if present) will be run before the original executable code runs.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define WIN32_LEAN_AND_MEAN </span>
<span style="color: #339933;">#include &lt;windows.h&gt;</span>
<span style="color: #339933;">#include &lt;winuser.h&gt;</span>
&nbsp;
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> _STUB_CONFIGURATION <span style="color: #009900;">&#123;</span> 
    DWORD NewEntryRVA<span style="color: #339933;">;</span> 
    DWORD OrgEntryRVA<span style="color: #339933;">;</span> 
    DWORD ImageBase<span style="color: #339933;">;</span> 
    DWORD PrefImageBase<span style="color: #339933;">;</span> 
    DWORD OrgITRVA<span style="color: #339933;">;</span> 
    DWORD RelocRVA<span style="color: #339933;">;</span> 
    DWORD MSLRVA<span style="color: #339933;">;</span> 
    DWORD ExtraDataRVA<span style="color: #339933;">;</span> 
    DWORD RedirTableRVA<span style="color: #339933;">;</span> 
    DWORD Flags<span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> STUB_CONFIGURATION<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>PSTUB_CONFIGURATION<span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #ff0000;">&quot;C&quot;</span> __declspec<span style="color: #009900;">&#40;</span>dllexport<span style="color: #009900;">&#41;</span> <span style="color: #993333;">void</span> __stdcall 
BeforeHandlers<span style="color: #009900;">&#40;</span>PSTUB_CONFIGURATION config<span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span> 
    <span style="color: #993333;">int</span> action <span style="color: #339933;">=</span> MessageBox<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> 
        TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Do you really want to execute this program?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
        TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Confirm program execution&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
        MB_YESNO <span style="color: #339933;">|</span> MB_ICONQUESTION<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>action <span style="color: #339933;">==</span> IDNO<span style="color: #009900;">&#41;</span> ExitProcess<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Save the above source code as injectiondll.cpp. To compile this file, you will also need to create a module definition file containing the list of functions you want to export:</p>
<pre>
LIBRARY	"injectiondll"
EXPORTS
    BeforeHandlers   @1
</pre>
<p>Save it as injectiondll.def to the same directory as injectiondll.cpp. Use the following command to compile the whole code under Visual C++:</p>
<pre>
cl /MT /LD injectiondll.cpp /link /def:injectiondll.def user32.lib
</pre>
<p>Now you can use the PE-inject Frontend tool (located in the Tools directory of PE-inject) to inject this DLL into any executable file and see the result of your work.</p>
<h3>Using PE-inject programmaticaly</h3>
<p>The PE-inject Frontend tool is a nice thing for testing. In the real world however, it would be better to bypass PE-inject Frontend and to create something that would do it&#8217;s job in a more user-friendly way. The InjectFile function located in peinject.dll (part of PE-inject distribution) is exactly what we are looking for.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;windows.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define INJECT_ERR_NOERROR      0</span>
<span style="color: #339933;">#define INJECT_ERR_FILENOTFOUND 1</span>
<span style="color: #339933;">#define INJECT_ERR_NOTAPEFILE   2</span>
<span style="color: #339933;">#define INJECT_ERR_PEHEADERFULL 3</span>
&nbsp;
<span style="color: #339933;">#define INJECT_FLAG_DOIMPORTS   1</span>
<span style="color: #339933;">#define INJECT_FLAG_JUMPTOOEP   2</span>
<span style="color: #339933;">#define INJECT_FLAG_HANDLERELOC 4</span>
<span style="color: #339933;">#define INJECT_FLAG_STRIPRELOCS 8</span>
<span style="color: #339933;">#define INJECT_FLAG_COMPRESSDLL 16</span>
<span style="color: #339933;">#define INJECT_FLAG_BACKUPTLS   32</span>
&nbsp;
<span style="color: #993333;">typedef</span> DWORD <span style="color: #009900;">&#40;</span>__stdcall <span style="color: #339933;">*</span>INJECTFILEPROC<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#40;</span>LPCSTR lpInputFile<span style="color: #339933;">,</span> LPCSTR lpOutputFile<span style="color: #339933;">,</span>
    LPCSTR lpDllFile<span style="color: #339933;">,</span> LPVOID lpExtraData<span style="color: #339933;">,</span>
    DWORD dwExtraDataSize<span style="color: #339933;">,</span> DWORD dwFlags<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span><span style="color: #339933;">*</span> argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    HMODULE hPeinjectDll<span style="color: #339933;">;</span>
    INJECTFILEPROC InjectFile<span style="color: #339933;">;</span>
    DWORD result<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>argc <span style="color: #339933;">!=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Invalid arguments!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    hPeinjectDll <span style="color: #339933;">=</span> LoadLibrary<span style="color: #009900;">&#40;</span>TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;PEinject.dll&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>hPeinjectDll<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Failed to load PEinject.dll!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    InjectFile <span style="color: #339933;">=</span>
        <span style="color: #009900;">&#40;</span>INJECTFILEPROC<span style="color: #009900;">&#41;</span>GetProcAddress<span style="color: #009900;">&#40;</span>hPeinjectDll<span style="color: #339933;">,</span>
        <span style="color: #ff0000;">&quot;InjectFile&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>InjectFile<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;InjectFile() not found!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        FreeLibrary<span style="color: #009900;">&#40;</span>hPeinjectDll<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    result <span style="color: #339933;">=</span> InjectFile<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">&quot;injectiondll.dll&quot;</span><span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>
        INJECT_FLAG_DOIMPORTS
        <span style="color: #339933;">|</span> INJECT_FLAG_JUMPTOOEP
        <span style="color: #339933;">|</span> INJECT_FLAG_HANDLERELOC
        <span style="color: #339933;">|</span> INJECT_FLAG_COMPRESSDLL
        <span style="color: #339933;">|</span> INJECT_FLAG_BACKUPTLS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;File successfuly injected!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// one of the INJECT_ERR_ constants</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;An error occured!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    FreeLibrary<span style="color: #009900;">&#40;</span>hPeinjectDll<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This code will load the PEinject.dll library (must be in the same directory, or in PATH), locate the InjectFile function and use it to inject the code from injectiondll.dll into executable specified as a command line parameter.</p>
<p>To compile the above code with Visual C++ use this command:</p>
<pre>
cl injecttest.cpp
</pre>
]]></content:encoded>
			<wfw:commentRss>http://migeel.sk/blog/2007/07/30/injecting-code-into-executables-with-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
