<?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[Funktion umbiegen]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe mal ein bisschen im Internet nach einer merkwürdigen Sache gesucht.<br />
Bei diassemblieren einer DLL ist mir halt mal aufgefallen, dass im Prologue eine Assembler Instruktion enthalten ist die einem 2Byte NOP sehr ähnlich sieht.</p>
<pre><code class="language-asm">mov edi, edi
</code></pre>
<p>Ich bin dann mal irgendwann dahinter gekommen, dass diese no-op während eines HotPatches gegen einen jmp zur gepatchten Funktion ersetzt wird.<br />
Jetzt hab ich also mal versucht da was nachzu basteln:</p>
<pre><code class="language-cpp">void lockUnhandledExceptionFilter() 
{
    HMODULE kernel32 = LoadLibraryA(&quot;kernel32.dll&quot;);
    assert(kernel32);

    if (FARPROC gpaSetUnhandledExceptionFilter = GetProcAddress(kernel32, &quot;SetUnhandledExceptionFilter&quot;)) 
	{
        unsigned char expected_code[] = {
											0x8B, 0xFF, // mov edi,edi
											0x55,       // push ebp
											0x8B, 0xEC, // mov ebp,esp
										 };

        // only replace code we expect
        if (memcmp(expected_code, gpaSetUnhandledExceptionFilter, sizeof(expected_code)) == 0) 
		{
            unsigned char new_code[] = {
											0x33, 0xC0,       // xor eax,eax
											0xC2, 0x04, 0x00, // ret 4
										};

            //BOOST_STATIC_ASSERT(sizeof(expected_code) == sizeof(new_code));

            DWORD old_protect;
            if (VirtualProtect(gpaSetUnhandledExceptionFilter, sizeof(new_code), PAGE_EXECUTE_READWRITE, &amp;old_protect)) 
			{
                CopyMemory(gpaSetUnhandledExceptionFilter, new_code, sizeof(new_code));

                DWORD dummy;
                VirtualProtect(gpaSetUnhandledExceptionFilter, sizeof(new_code), old_protect, &amp;dummy);

                FlushInstructionCache(GetCurrentProcess(), gpaSetUnhandledExceptionFilter, sizeof(new_code));

				SetUnhandledExceptionFilter(0);
            }
        }
    }
    FreeLibrary(kernel32);
}
</code></pre>
<p>Um ehrlich zu sein ist es auch noch copy&amp;paste gewesen;-)</p>
<p>Nun meine Frage. Wenn ich den Code rennen lasse dann wird auch, wie erwartet, die Instruktionen geändert. Wenn ic allerdings meine Applikation neu starte dann ist wieder der alte Prologue drin. Woran kann das liegen und wie kann ich es relaisieren, dass die Instruktionen permanent(also so lange das System läuft) vorhanden sind?<br />
Außerdem würde ich gerne wissen wann das wieder &quot;zurück gestellt&quot; wird. Immer wenn meine Applikation beendet wird oder ist da irgendwo ein Timer der dann als Beispiel alle 2 Sekunden überprüft ob alles ok ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/252071/funktion-umbiegen</link><generator>RSS for Node</generator><lastBuildDate>Mon, 14 Sep 2026 09:29:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/252071.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 14 Oct 2009 09:00:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Funktion umbiegen on Wed, 14 Oct 2009 09:00:15 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe mal ein bisschen im Internet nach einer merkwürdigen Sache gesucht.<br />
Bei diassemblieren einer DLL ist mir halt mal aufgefallen, dass im Prologue eine Assembler Instruktion enthalten ist die einem 2Byte NOP sehr ähnlich sieht.</p>
<pre><code class="language-asm">mov edi, edi
</code></pre>
<p>Ich bin dann mal irgendwann dahinter gekommen, dass diese no-op während eines HotPatches gegen einen jmp zur gepatchten Funktion ersetzt wird.<br />
Jetzt hab ich also mal versucht da was nachzu basteln:</p>
<pre><code class="language-cpp">void lockUnhandledExceptionFilter() 
{
    HMODULE kernel32 = LoadLibraryA(&quot;kernel32.dll&quot;);
    assert(kernel32);

    if (FARPROC gpaSetUnhandledExceptionFilter = GetProcAddress(kernel32, &quot;SetUnhandledExceptionFilter&quot;)) 
	{
        unsigned char expected_code[] = {
											0x8B, 0xFF, // mov edi,edi
											0x55,       // push ebp
											0x8B, 0xEC, // mov ebp,esp
										 };

        // only replace code we expect
        if (memcmp(expected_code, gpaSetUnhandledExceptionFilter, sizeof(expected_code)) == 0) 
		{
            unsigned char new_code[] = {
											0x33, 0xC0,       // xor eax,eax
											0xC2, 0x04, 0x00, // ret 4
										};

            //BOOST_STATIC_ASSERT(sizeof(expected_code) == sizeof(new_code));

            DWORD old_protect;
            if (VirtualProtect(gpaSetUnhandledExceptionFilter, sizeof(new_code), PAGE_EXECUTE_READWRITE, &amp;old_protect)) 
			{
                CopyMemory(gpaSetUnhandledExceptionFilter, new_code, sizeof(new_code));

                DWORD dummy;
                VirtualProtect(gpaSetUnhandledExceptionFilter, sizeof(new_code), old_protect, &amp;dummy);

                FlushInstructionCache(GetCurrentProcess(), gpaSetUnhandledExceptionFilter, sizeof(new_code));

				SetUnhandledExceptionFilter(0);
            }
        }
    }
    FreeLibrary(kernel32);
}
</code></pre>
<p>Um ehrlich zu sein ist es auch noch copy&amp;paste gewesen;-)</p>
<p>Nun meine Frage. Wenn ich den Code rennen lasse dann wird auch, wie erwartet, die Instruktionen geändert. Wenn ic allerdings meine Applikation neu starte dann ist wieder der alte Prologue drin. Woran kann das liegen und wie kann ich es relaisieren, dass die Instruktionen permanent(also so lange das System läuft) vorhanden sind?<br />
Außerdem würde ich gerne wissen wann das wieder &quot;zurück gestellt&quot; wird. Immer wenn meine Applikation beendet wird oder ist da irgendwo ein Timer der dann als Beispiel alle 2 Sekunden überprüft ob alles ok ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1792396</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1792396</guid><dc:creator><![CDATA[second sun]]></dc:creator><pubDate>Wed, 14 Oct 2009 09:00:15 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion umbiegen on Thu, 22 Oct 2009 11:16:14 GMT]]></title><description><![CDATA[<p>weiß denn keiner eine Antwort?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1796305</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1796305</guid><dc:creator><![CDATA[secondsun]]></dc:creator><pubDate>Thu, 22 Oct 2009 11:16:14 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion umbiegen on Thu, 22 Oct 2009 11:20:32 GMT]]></title><description><![CDATA[<p>secondsun schrieb:</p>
<blockquote>
<p>weiß denn keiner eine Antwort?</p>
</blockquote>
<p>Irgendjemand weiß das sicher. Du hast bloß noch nicht gemerkt, dass deine Frage rein gar nichts mit C++ zu tun hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1796308</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1796308</guid><dc:creator><![CDATA[Registrierter Troll]]></dc:creator><pubDate>Thu, 22 Oct 2009 11:20:32 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion umbiegen on Thu, 22 Oct 2009 11:37:42 GMT]]></title><description><![CDATA[<p>Ok, das kann schon stimmen. Aber in welchen Bereich ist es denn deiner Meinung nach einzuordnen?!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1796318</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1796318</guid><dc:creator><![CDATA[secondsun]]></dc:creator><pubDate>Thu, 22 Oct 2009 11:37:42 GMT</pubDate></item></channel></rss>