<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Frage zu Api Hooking]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich versuche seit einiger Zeit eine Funktion zu hooken indem ich die IAT überschreibe. Das klappt auch ganz gut wenn ich es wie folgt mache.</p>
<p>Wenn ich allerdings vor dem Aufruf von set_hook die Funktion strncpy aufrufe oder nur die Adresse ausgeben lasse, funktioniert set_hook nicht mehr und die Adresse in der IAT wird nicht überschrieben.<br />
Dabei werden aber keine Fehler ausgegeben und GetLastError() enthält auch keine Fehler.</p>
<p>Woran liegt das?</p>
<pre><code class="language-cpp">#include &quot;stdafx.h&quot;
#include &lt;windows.h&gt;
#include &lt;imagehlp.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

HANDLE hModule = INVALID_HANDLE_VALUE;
PROC orig_strncpy = 0;

typedef char * __cdecl strncpy_t(char *, char const *, size_t);

char * __cdecl my_strncpy(char * dest, char const * src, size_t len) {
	char * ReturnValue;

	strncpy_t * fn = (strncpy_t *) orig_strncpy;

	ReturnValue = (*fn)(dest, &quot;Hallo Geld&quot;, len);

	return ReturnValue;
}

BOOL set_hook(HMODULE hModuleOfCaller, LPSTR LibraryName, PROC OldFunctionPointer, PROC NewFunctionPointer);

int main(int argc, char **argv) {
	char src[] = &quot;Hallo Welt&quot;;
	char dest[11];	

	HMODULE mod = GetModuleHandle(&quot;Application.exe&quot;);
	HMODULE lib = LoadLibrary(&quot;MSVCR80.DLL&quot;);
	PROC old_fun = GetProcAddress(lib, &quot;strncpy&quot;);

	set_hook(mod, &quot;MSVCR80.DLL&quot;, old_fun, (PROC) my_strncpy);

	strncpy(dest, src, 11);
	printf(&quot;src:  %s\n&quot;, src);
	printf(&quot;dest: %s\n&quot;, dest);

	fgetc(stdin);

	return 0;
}
</code></pre>
<pre><code class="language-cpp">BOOL set_hook(HMODULE hModuleOfCaller, LPSTR LibraryName, PROC OldFunctionPointer, PROC NewFunctionPointer) {
	if(hModuleOfCaller == hModule)
		return FALSE;
	if(hModuleOfCaller == 0)
		return FALSE;

	ULONG ulSize;

	// Get the address of the module’s import section
	PIMAGE_IMPORT_DESCRIPTOR pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR) 
		ImageDirectoryEntryToData(hModuleOfCaller, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &amp;ulSize);

	// Does this module have an import section ?
	if (pImportDesc == NULL)
		return FALSE;

	// Loop through all descriptors and find the 
	// import descriptor containing references to callee’s functions
	while(pImportDesc-&gt;Name) {
		PSTR pszModName = (PSTR)((PBYTE) hModuleOfCaller + pImportDesc-&gt;Name);

		if(_stricmp(pszModName, LibraryName) == 0) 
			break; // Found

		pImportDesc++;
	} // while

	if (pImportDesc-&gt;Name == 0)
		return FALSE;

	//Get caller’s IAT 
	PIMAGE_THUNK_DATA pThunk = (PIMAGE_THUNK_DATA)( (PBYTE) hModuleOfCaller + pImportDesc-&gt;FirstThunk );

	PROC pfnCurrent = OldFunctionPointer;

	// Replace current function address with new one
	while(pThunk-&gt;u1.Function) {
		// Get the address of the function address
		PROC* ppfn = (PROC*) &amp;pThunk-&gt;u1.Function;

		// Is this the function we’re looking for?
		BOOL bFound = (*ppfn == pfnCurrent);

		if(bFound) {
			MEMORY_BASIC_INFORMATION mbi;

			::VirtualQuery(ppfn, &amp;mbi, sizeof(MEMORY_BASIC_INFORMATION));

			// In order to provide writable access to this part of the 
			// memory we need to change the memory protection

			if (FALSE == ::VirtualProtect(mbi.BaseAddress,mbi.RegionSize,PAGE_READWRITE,&amp;mbi.Protect))
				return FALSE;

			if(orig_strncpy == 0)
				orig_strncpy = *ppfn;

			*ppfn = *NewFunctionPointer;

			BOOL bResult = TRUE;

			// Restore the protection back
			DWORD dwOldProtect;

			::VirtualProtect(mbi.BaseAddress, mbi.RegionSize, mbi.Protect, &amp;dwOldProtect);

			break;
		}

		pThunk++;
	}

	return TRUE;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/185762/frage-zu-api-hooking</link><generator>RSS for Node</generator><lastBuildDate>Sun, 05 Jul 2026 05:33:52 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/185762.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 30 Jun 2007 21:45:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zu Api Hooking on Sat, 30 Jun 2007 21:45:05 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich versuche seit einiger Zeit eine Funktion zu hooken indem ich die IAT überschreibe. Das klappt auch ganz gut wenn ich es wie folgt mache.</p>
<p>Wenn ich allerdings vor dem Aufruf von set_hook die Funktion strncpy aufrufe oder nur die Adresse ausgeben lasse, funktioniert set_hook nicht mehr und die Adresse in der IAT wird nicht überschrieben.<br />
Dabei werden aber keine Fehler ausgegeben und GetLastError() enthält auch keine Fehler.</p>
<p>Woran liegt das?</p>
<pre><code class="language-cpp">#include &quot;stdafx.h&quot;
#include &lt;windows.h&gt;
#include &lt;imagehlp.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

HANDLE hModule = INVALID_HANDLE_VALUE;
PROC orig_strncpy = 0;

typedef char * __cdecl strncpy_t(char *, char const *, size_t);

char * __cdecl my_strncpy(char * dest, char const * src, size_t len) {
	char * ReturnValue;

	strncpy_t * fn = (strncpy_t *) orig_strncpy;

	ReturnValue = (*fn)(dest, &quot;Hallo Geld&quot;, len);

	return ReturnValue;
}

BOOL set_hook(HMODULE hModuleOfCaller, LPSTR LibraryName, PROC OldFunctionPointer, PROC NewFunctionPointer);

int main(int argc, char **argv) {
	char src[] = &quot;Hallo Welt&quot;;
	char dest[11];	

	HMODULE mod = GetModuleHandle(&quot;Application.exe&quot;);
	HMODULE lib = LoadLibrary(&quot;MSVCR80.DLL&quot;);
	PROC old_fun = GetProcAddress(lib, &quot;strncpy&quot;);

	set_hook(mod, &quot;MSVCR80.DLL&quot;, old_fun, (PROC) my_strncpy);

	strncpy(dest, src, 11);
	printf(&quot;src:  %s\n&quot;, src);
	printf(&quot;dest: %s\n&quot;, dest);

	fgetc(stdin);

	return 0;
}
</code></pre>
<pre><code class="language-cpp">BOOL set_hook(HMODULE hModuleOfCaller, LPSTR LibraryName, PROC OldFunctionPointer, PROC NewFunctionPointer) {
	if(hModuleOfCaller == hModule)
		return FALSE;
	if(hModuleOfCaller == 0)
		return FALSE;

	ULONG ulSize;

	// Get the address of the module’s import section
	PIMAGE_IMPORT_DESCRIPTOR pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR) 
		ImageDirectoryEntryToData(hModuleOfCaller, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &amp;ulSize);

	// Does this module have an import section ?
	if (pImportDesc == NULL)
		return FALSE;

	// Loop through all descriptors and find the 
	// import descriptor containing references to callee’s functions
	while(pImportDesc-&gt;Name) {
		PSTR pszModName = (PSTR)((PBYTE) hModuleOfCaller + pImportDesc-&gt;Name);

		if(_stricmp(pszModName, LibraryName) == 0) 
			break; // Found

		pImportDesc++;
	} // while

	if (pImportDesc-&gt;Name == 0)
		return FALSE;

	//Get caller’s IAT 
	PIMAGE_THUNK_DATA pThunk = (PIMAGE_THUNK_DATA)( (PBYTE) hModuleOfCaller + pImportDesc-&gt;FirstThunk );

	PROC pfnCurrent = OldFunctionPointer;

	// Replace current function address with new one
	while(pThunk-&gt;u1.Function) {
		// Get the address of the function address
		PROC* ppfn = (PROC*) &amp;pThunk-&gt;u1.Function;

		// Is this the function we’re looking for?
		BOOL bFound = (*ppfn == pfnCurrent);

		if(bFound) {
			MEMORY_BASIC_INFORMATION mbi;

			::VirtualQuery(ppfn, &amp;mbi, sizeof(MEMORY_BASIC_INFORMATION));

			// In order to provide writable access to this part of the 
			// memory we need to change the memory protection

			if (FALSE == ::VirtualProtect(mbi.BaseAddress,mbi.RegionSize,PAGE_READWRITE,&amp;mbi.Protect))
				return FALSE;

			if(orig_strncpy == 0)
				orig_strncpy = *ppfn;

			*ppfn = *NewFunctionPointer;

			BOOL bResult = TRUE;

			// Restore the protection back
			DWORD dwOldProtect;

			::VirtualProtect(mbi.BaseAddress, mbi.RegionSize, mbi.Protect, &amp;dwOldProtect);

			break;
		}

		pThunk++;
	}

	return TRUE;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1315900</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1315900</guid><dc:creator><![CDATA[Konrad]]></dc:creator><pubDate>Sat, 30 Jun 2007 21:45:05 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Api Hooking on Sun, 01 Jul 2007 15:47:18 GMT]]></title><description><![CDATA[<p>Konrad schrieb:</p>
<blockquote>
<p>Wenn ich allerdings vor dem Aufruf von set_hook die Funktion strncpy aufrufe oder nur die Adresse ausgeben lasse,<br />
funktioniert set_hook nicht mehr und die Adresse in der IAT wird nicht überschrieben.</p>
</blockquote>
<p>Poste mal was &quot;VirtualQuery ()&quot; in &quot;set_hook ()&quot; in beiden Fällen liefert. Könnte ev. ein Kompiler / Linker - Problem sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1316231</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1316231</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 01 Jul 2007 15:47:18 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Api Hooking on Sun, 01 Jul 2007 18:04:50 GMT]]></title><description><![CDATA[<p>In beiden Fällen 28.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1316318</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1316318</guid><dc:creator><![CDATA[Konrad]]></dc:creator><pubDate>Sun, 01 Jul 2007 18:04:50 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Api Hooking on Sun, 01 Jul 2007 18:40:55 GMT]]></title><description><![CDATA[<p>Offenbar kann &quot;VirtualProtect ()&quot; &quot;PAGE_READWRITE&quot; nicht setzen, wenn vorher lesend auf die IAT zugegriffen wurde (warum auch immer).</p>
<p>Was steht denn in beiden Fällen in &quot;mbi.Protect&quot; ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1316332</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1316332</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 01 Jul 2007 18:40:55 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Api Hooking on Sun, 01 Jul 2007 19:17:16 GMT]]></title><description><![CDATA[<p>In beiden Fällen wird aus PAGE_READONLY PAGE_READWRITE</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1316363</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1316363</guid><dc:creator><![CDATA[Konrad]]></dc:creator><pubDate>Sun, 01 Jul 2007 19:17:16 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Api Hooking on Sun, 01 Jul 2007 19:36:24 GMT]]></title><description><![CDATA[<p>Wenn in beiden Fällen PAGE_READWRITE gesetzt werden konnte, dann wird in beiden Fällen auch die Adresse in der IAT überschrieben.<br />
Wie testest Du, ob die Adresse überschrieben wurde ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1316380</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1316380</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 01 Jul 2007 19:36:24 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Api Hooking on Sun, 01 Jul 2007 19:43:35 GMT]]></title><description><![CDATA[<p>Testen tue ich das wie folgt:</p>
<pre><code class="language-cpp">strncpy(dest, src, 11);
    printf(&quot;src:  %s\n&quot;, src);
    printf(&quot;dest: %s\n&quot;, dest);
</code></pre>
<p>Wenn die Adresse überschrieben worden wäre, müsste im 2. String &quot;Hallo Geld&quot; anstatt &quot;Hallo Welt&quot; stehen.</p>
<p>Wenn es geklappt hat ist &amp;strncpy und &amp;my_strncpy == 00401080.<br />
Wenn es wegen dem vorherigen Zugriff nicht geklappt hat ist die Adresse von &amp;strncpy 781803F0.</p>
<p>Die Protect Werte habe ich über mbi.Protect und über dwOldProtect ausgelesen..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1316385</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1316385</guid><dc:creator><![CDATA[Konrad]]></dc:creator><pubDate>Sun, 01 Jul 2007 19:43:35 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Api Hooking on Sun, 01 Jul 2007 20:33:41 GMT]]></title><description><![CDATA[<p>Eigentlich alles ok soweit. Tritt der Fehler auch im &quot;Release-Mode&quot; auf ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1316439</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1316439</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 01 Jul 2007 20:33:41 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Api Hooking on Sun, 01 Jul 2007 21:03:25 GMT]]></title><description><![CDATA[<p>Jo, hab die ganze Zeit im Release Mode kompiliert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1316463</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1316463</guid><dc:creator><![CDATA[Konrad]]></dc:creator><pubDate>Sun, 01 Jul 2007 21:03:25 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Api Hooking on Mon, 02 Jul 2007 19:50:06 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>wäre vielleicht jemand so nett und würde das Programm mal bei sich testen? Das ganze ist ein Standard Konsolen Programm und dbghelp.lib wird dazugelinkt.</p>
<p>Output müsste folgendes sein:</p>
<blockquote>
<p>src: Hallo Welt<br />
dest: Hallo Geld</p>
</blockquote>
<p>Einmal zusätzlich mit folgendem Code _vor_ dem set_hook.</p>
<pre><code class="language-cpp">strncpy(dest, src, 11);
    printf(&quot;src:  %s\n&quot;, src);
    printf(&quot;dest: %s\n&quot;, dest);
</code></pre>
<p>Danke <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1317150</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1317150</guid><dc:creator><![CDATA[Konrad]]></dc:creator><pubDate>Mon, 02 Jul 2007 19:50:06 GMT</pubDate></item></channel></rss>