<?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[Problem mit dll injection [ERLEDIGT]]]></title><description><![CDATA[<p>Hallo ich versuche gerade ein Programm zu schreiben, dass beliebige dlls in beliebige Prozesse injected. Das Problem ist, dass der betreffenden Prozess abstürzt nachdem die dll ihre Aufgabe erfüllt hat. Ich nehme an das es an VirtualFreeEx liegt, aus irgendeinem Grund schlägt die Funtion bei mir fehl. Bitte um Hilfe.</p>
<p>PS: In der dll wird nichts besonderes gemacht, auch mit einer &quot;leeren&quot; dll krieg ich das Problem mit dem Absturz.</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;cstdio&gt;
#include &lt;tlhelp32.h&gt;
#include &lt;iostream&gt;
#include &lt;conio.h&gt;

using namespace std;

typedef HINSTANCE (*fpLoadLibrary)(char*);
typedef LPVOID (*fpGetProcAddress)(HINSTANCE, char*);
typedef void (*fpFunktion)(void);

struct INJECTSTRUCT
{
      fpLoadLibrary LoadLibrary;
      fpGetProcAddress GetProcAddress;
      char path[255];
      char func[255];
};

DWORD WINAPI threadstart(LPVOID addr)
{
	HINSTANCE hDll;
	fpFunktion funktion;
	INJECTSTRUCT * is = (INJECTSTRUCT*)addr;       
	hDll = is-&gt;LoadLibrary(is-&gt;path);
	funktion = (fpFunktion)is-&gt;GetProcAddress(hDll, is-&gt;func);
	funktion();
	return 0;
}
void threadend()
{
}

//...Debug_Privileg setzen

void listproc()
{
		HANDLE hSnap, hTemp;
	PROCESSENTRY32 pe;
	pe.dwSize = sizeof(PROCESSENTRY32);

	//EnableDebugPrivilege();
	hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

	if(Process32First(hSnap, &amp;pe))
	{
		do
		{
			hTemp = OpenProcess(PROCESS_ALL_ACCESS, 0, pe.th32ProcessID);
			if(hTemp)
			{
				printf(&quot;%4d\t%s\n&quot;, pe.th32ProcessID, pe.szExeFile);
				CloseHandle(hTemp);
			}
		}
		while(Process32Next(hSnap, &amp;pe));
	}
}

int main()
{

   HANDLE hProc;
   LPVOID start, thread;
   DWORD funcsize, written;
   HINSTANCE hDll;
   INJECTSTRUCT is;
   DWORD id;

   EnableDebugPrivilege();
   listproc();

   hDll = LoadLibrary(&quot;KERNEL32&quot;);
   is.LoadLibrary = (fpLoadLibrary)GetProcAddress(hDll, &quot;LoadLibraryA&quot;);
   is.GetProcAddress = (fpGetProcAddress)GetProcAddress(hDll, &quot;GetProcAddress&quot;);
   strcpy(is.path, &quot;DLL.dll&quot;);
   strcpy(is.func, &quot;Funktion&quot;);

   funcsize = (DWORD)threadend-(DWORD)threadstart;

   printf(&quot;Process ID: &quot;);
   scanf(&quot;%d&quot;, &amp;id);

   hProc = OpenProcess(PROCESS_ALL_ACCESS, false, id);
   if(!hProc)
	   MessageBox ( NULL, &quot;Unable to open process&quot;, &quot;DLL Injector&quot;, MB_OK);

   printf(&quot;Prozess Handle:       %x&quot;, hProc);

   //start = VirtualAllocEx(hProc, 0, funcsize+sizeof(INJECTSTRUCT), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
   if(!(start = VirtualAllocEx(hProc, 0, funcsize+sizeof(INJECTSTRUCT), MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE)))
	   MessageBox ( NULL, &quot;Failed to allocate memory&quot;, &quot;DLL Injector&quot;, MB_OK);

   printf(&quot;Memory:               %x\n&quot;, start);

   if(!(WriteProcessMemory(hProc, start, (LPVOID)&amp;is, sizeof(INJECTSTRUCT), NULL)))
		MessageBox ( NULL, &quot;WriteProcessMemory(hProc, start, (LPVOID)&amp;is, sizeof(INJECTSTRUCT), NULL) failed&quot;, &quot;DLL Injector&quot;, MB_OK);

   thread = (LPVOID)((DWORD)start+sizeof(INJECTSTRUCT));

  if(!(WriteProcessMemory(hProc, thread, (LPVOID)threadstart, funcsize, NULL)))
		MessageBox ( NULL, &quot;WriteProcessMemory(hProc, thread, (LPVOID)threadstart, funcsize, NULL) failed&quot;, &quot;DLL Injector&quot;, MB_OK);

   if(!(CreateRemoteThread(hProc, NULL, 0, (LPTHREAD_START_ROUTINE)thread, start, 0, NULL)))
	   MessageBox ( NULL, &quot;Failed to Create remote thread&quot;, &quot;DLL Injector&quot;, MB_OK);

   if(!(VirtualFreeEx(hProc,NULL,funcsize+sizeof(INJECTSTRUCT),MEM_RESERVE|MEM_COMMIT)))

		MessageBox ( NULL, &quot;Failed to release allocated memory&quot;, &quot;DLL Injector&quot;, MB_OK);

    CloseHandle(hProc);
	return 0;
}
</code></pre>
<p>Wie gesagt, die DLL macht sogar was sie soll, aber danach stürzt der Prozess in den injected wurde ab.</p>
<p>Danke schonmal.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/227146/problem-mit-dll-injection-erledigt</link><generator>RSS for Node</generator><lastBuildDate>Thu, 09 Apr 2026 13:15:17 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/227146.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 11 Nov 2008 17:48:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Wed, 12 Nov 2008 13:45:45 GMT]]></title><description><![CDATA[<p>Hallo ich versuche gerade ein Programm zu schreiben, dass beliebige dlls in beliebige Prozesse injected. Das Problem ist, dass der betreffenden Prozess abstürzt nachdem die dll ihre Aufgabe erfüllt hat. Ich nehme an das es an VirtualFreeEx liegt, aus irgendeinem Grund schlägt die Funtion bei mir fehl. Bitte um Hilfe.</p>
<p>PS: In der dll wird nichts besonderes gemacht, auch mit einer &quot;leeren&quot; dll krieg ich das Problem mit dem Absturz.</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;cstdio&gt;
#include &lt;tlhelp32.h&gt;
#include &lt;iostream&gt;
#include &lt;conio.h&gt;

using namespace std;

typedef HINSTANCE (*fpLoadLibrary)(char*);
typedef LPVOID (*fpGetProcAddress)(HINSTANCE, char*);
typedef void (*fpFunktion)(void);

struct INJECTSTRUCT
{
      fpLoadLibrary LoadLibrary;
      fpGetProcAddress GetProcAddress;
      char path[255];
      char func[255];
};

DWORD WINAPI threadstart(LPVOID addr)
{
	HINSTANCE hDll;
	fpFunktion funktion;
	INJECTSTRUCT * is = (INJECTSTRUCT*)addr;       
	hDll = is-&gt;LoadLibrary(is-&gt;path);
	funktion = (fpFunktion)is-&gt;GetProcAddress(hDll, is-&gt;func);
	funktion();
	return 0;
}
void threadend()
{
}

//...Debug_Privileg setzen

void listproc()
{
		HANDLE hSnap, hTemp;
	PROCESSENTRY32 pe;
	pe.dwSize = sizeof(PROCESSENTRY32);

	//EnableDebugPrivilege();
	hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

	if(Process32First(hSnap, &amp;pe))
	{
		do
		{
			hTemp = OpenProcess(PROCESS_ALL_ACCESS, 0, pe.th32ProcessID);
			if(hTemp)
			{
				printf(&quot;%4d\t%s\n&quot;, pe.th32ProcessID, pe.szExeFile);
				CloseHandle(hTemp);
			}
		}
		while(Process32Next(hSnap, &amp;pe));
	}
}

int main()
{

   HANDLE hProc;
   LPVOID start, thread;
   DWORD funcsize, written;
   HINSTANCE hDll;
   INJECTSTRUCT is;
   DWORD id;

   EnableDebugPrivilege();
   listproc();

   hDll = LoadLibrary(&quot;KERNEL32&quot;);
   is.LoadLibrary = (fpLoadLibrary)GetProcAddress(hDll, &quot;LoadLibraryA&quot;);
   is.GetProcAddress = (fpGetProcAddress)GetProcAddress(hDll, &quot;GetProcAddress&quot;);
   strcpy(is.path, &quot;DLL.dll&quot;);
   strcpy(is.func, &quot;Funktion&quot;);

   funcsize = (DWORD)threadend-(DWORD)threadstart;

   printf(&quot;Process ID: &quot;);
   scanf(&quot;%d&quot;, &amp;id);

   hProc = OpenProcess(PROCESS_ALL_ACCESS, false, id);
   if(!hProc)
	   MessageBox ( NULL, &quot;Unable to open process&quot;, &quot;DLL Injector&quot;, MB_OK);

   printf(&quot;Prozess Handle:       %x&quot;, hProc);

   //start = VirtualAllocEx(hProc, 0, funcsize+sizeof(INJECTSTRUCT), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
   if(!(start = VirtualAllocEx(hProc, 0, funcsize+sizeof(INJECTSTRUCT), MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE)))
	   MessageBox ( NULL, &quot;Failed to allocate memory&quot;, &quot;DLL Injector&quot;, MB_OK);

   printf(&quot;Memory:               %x\n&quot;, start);

   if(!(WriteProcessMemory(hProc, start, (LPVOID)&amp;is, sizeof(INJECTSTRUCT), NULL)))
		MessageBox ( NULL, &quot;WriteProcessMemory(hProc, start, (LPVOID)&amp;is, sizeof(INJECTSTRUCT), NULL) failed&quot;, &quot;DLL Injector&quot;, MB_OK);

   thread = (LPVOID)((DWORD)start+sizeof(INJECTSTRUCT));

  if(!(WriteProcessMemory(hProc, thread, (LPVOID)threadstart, funcsize, NULL)))
		MessageBox ( NULL, &quot;WriteProcessMemory(hProc, thread, (LPVOID)threadstart, funcsize, NULL) failed&quot;, &quot;DLL Injector&quot;, MB_OK);

   if(!(CreateRemoteThread(hProc, NULL, 0, (LPTHREAD_START_ROUTINE)thread, start, 0, NULL)))
	   MessageBox ( NULL, &quot;Failed to Create remote thread&quot;, &quot;DLL Injector&quot;, MB_OK);

   if(!(VirtualFreeEx(hProc,NULL,funcsize+sizeof(INJECTSTRUCT),MEM_RESERVE|MEM_COMMIT)))

		MessageBox ( NULL, &quot;Failed to release allocated memory&quot;, &quot;DLL Injector&quot;, MB_OK);

    CloseHandle(hProc);
	return 0;
}
</code></pre>
<p>Wie gesagt, die DLL macht sogar was sie soll, aber danach stürzt der Prozess in den injected wurde ab.</p>
<p>Danke schonmal.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1613253</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1613253</guid><dc:creator><![CDATA[Grabgewalt]]></dc:creator><pubDate>Wed, 12 Nov 2008 13:45:45 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Tue, 11 Nov 2008 23:57:44 GMT]]></title><description><![CDATA[<p>Prüf bei Gelegenheit mal, <em>was</em> Du mit VirtualFreeEx eigentlich freigibst. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1613344</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1613344</guid><dc:creator><![CDATA[schaufelsoftie]]></dc:creator><pubDate>Tue, 11 Nov 2008 23:57:44 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Wed, 12 Nov 2008 00:22:00 GMT]]></title><description><![CDATA[<p>Here you are:</p>
<pre><code class="language-cpp">// INJECTION
// std::string moduleFullPath = Voller Pfad zur DLL, zB. C:\\mydll.dll

LPVOID remoteMemoryHandle = VirtualAllocEx(processHandle, 0, moduleFullPath.size() * sizeof(char), MEM_COMMIT, PAGE_READWRITE);
WriteProcessMemory(processHandle, remoteMemoryHandle, moduleFullPath.c_str(), moduleFullPath.size() * sizeof(char), 0);
FARPROC loadLibraryAddress = GetProcAddress(GetModuleHandle(&quot;KERNEL32&quot;), &quot;LoadLibraryA&quot;);
HANDLE remoteThreadHandle = CreateRemoteThread(processHandle, 0, 0, reinterpret_cast&lt;LPTHREAD_START_ROUTINE&gt;(loadLibraryAddress), remoteMemoryHandle, 0, 0);
WaitForSingleObject(remoteThreadHandle, INFINITE);
CloseHandle(remoteThreadHandle);
VirtualFreeEx(processHandle, remoteMemoryHandle, 0, MEM_RELEASE);
</code></pre>
<pre><code class="language-cpp">// EJECTION
// HMODULE moduleHandle = Holen mit Module32First(), Module32Next() ...

FARPROC freeLibraryAddress = GetProcAddress(GetModuleHandle(&quot;KERNEL32&quot;), &quot;FreeLibrary&quot;);
HANDLE remoteThreadHandle = CreateRemoteThread(processHandle, 0, 0, reinterpret_cast&lt;LPTHREAD_START_ROUTINE&gt;(freeLibraryAddress), reinterpret_cast&lt;void*&gt;(moduleHandle), 0, 0);	
WaitForSingleObject(remoteThreadHandle, INFINITE);
CloseHandle(remoteThreadHandle);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1613351</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1613351</guid><dc:creator><![CDATA[Crocker]]></dc:creator><pubDate>Wed, 12 Nov 2008 00:22:00 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Wed, 12 Nov 2008 07:05:18 GMT]]></title><description><![CDATA[<p>Man darf in dem Startprozess natürlich nicht den Speicher freigeben in dem die Argumente und der Threadcode selbst liegen, bevor der Thread diese auch nicht mehr braucht, sprich terminiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1613381</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1613381</guid><dc:creator><![CDATA[Martin Richter]]></dc:creator><pubDate>Wed, 12 Nov 2008 07:05:18 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Wed, 12 Nov 2008 11:42:33 GMT]]></title><description><![CDATA[<p>Ja, die erste Version des Programms war recht unüberlegt, bzw hauptsächlich abkopiert aus nem Tutorial. Hier mal die neue Version:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;cstdio&gt;
#include &lt;tlhelp32.h&gt;
#include &lt;iostream&gt;
#include &lt;conio.h&gt;
#include &lt;string.h&gt;

using namespace std;

typedef HINSTANCE (*fpLoadLibrary)(char*);
typedef LPVOID (*fpGetProcAddress)(HINSTANCE, char*);
typedef void (*fpFunktion)(void);

struct INJECTSTRUCT
{
      fpLoadLibrary LoadLibrary;
      fpGetProcAddress GetProcAddress;
      char path[255];
      char func[255];
};

DWORD WINAPI threadstart(LPVOID addr)
{
	HINSTANCE hDll;
	fpFunktion funktion;
	INJECTSTRUCT * is = (INJECTSTRUCT*)addr;       
	hDll = is-&gt;LoadLibrary(is-&gt;path);
	funktion = (fpFunktion)is-&gt;GetProcAddress(hDll, is-&gt;func);
	funktion();
	return 0;
}
void threadend()
{
}

//EnableDebugPrivilege()[...]

void listproc()
{
	HANDLE hSnap, hTemp;
	PROCESSENTRY32 pe;
	pe.dwSize = sizeof(PROCESSENTRY32);

	EnableDebugPrivilege();

	hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

	if(Process32First(hSnap, &amp;pe))
	{
		do
		{
			hTemp = OpenProcess(PROCESS_ALL_ACCESS, 0, pe.th32ProcessID);
			if(hTemp)
			{
				printf(&quot;%4d\t%s\n&quot;, pe.th32ProcessID, pe.szExeFile);
				CloseHandle(hTemp);
			}
			if(!hTemp)
			{
				printf(&quot;%4d\t%s NO ACCESS\n&quot;, pe.th32ProcessID, pe.szExeFile);
				CloseHandle(hTemp);
			}
		}
		while(Process32Next(hSnap, &amp;pe));
	}
}

int main()
{

   HANDLE hProc,hThread;
   LPVOID start, thread;
   DWORD funcsize, written;
   HINSTANCE hDll;
   INJECTSTRUCT is;
   DWORD id;

   listproc();

   hDll = LoadLibrary(&quot;KERNEL32&quot;);

   is.LoadLibrary = (fpLoadLibrary)GetProcAddress(hDll, &quot;LoadLibraryA&quot;);
   is.GetProcAddress = (fpGetProcAddress)GetProcAddress(hDll, &quot;GetProcAddress&quot;);
   strcpy(is.path, &quot;J:\\Dokumente\\Programmierung\\C++\\DLL\\DLL\\Release\\DLL.dll&quot;);
   strcpy(is.func, &quot;Funktion&quot;);

   funcsize = (DWORD)threadend-(DWORD)threadstart;

   printf(&quot;Process ID: &quot;);
   scanf(&quot;%d&quot;, &amp;id);

   hProc = OpenProcess(PROCESS_ALL_ACCESS, false, id);
   if(!hProc)
	   MessageBox ( NULL, &quot;Unable to open process&quot;, &quot;DLL Injector&quot;, MB_OK);

   printf(&quot;Prozess Handle:       %x&quot;, hProc);

   if(!(start = VirtualAllocEx(hProc, 0, funcsize+sizeof(INJECTSTRUCT), MEM_COMMIT, PAGE_READWRITE)))
	   MessageBox ( NULL, &quot;Failed to allocate memory&quot;, &quot;DLL Injector&quot;, MB_OK);
   printf(&quot;Memory:               %x\n&quot;, start);

   if(!(WriteProcessMemory(hProc, start, (LPVOID)&amp;is, sizeof(INJECTSTRUCT), 0)))
		MessageBox ( NULL, &quot;WriteProcessMemory failed&quot;, &quot;DLL Injector&quot;, MB_OK);

   thread = (LPVOID)((DWORD)start+sizeof(INJECTSTRUCT));

  if(!(WriteProcessMemory(hProc, thread, (LPVOID)threadstart, funcsize, NULL)))
		MessageBox ( NULL, &quot;WriteProcessMemory(hProc, thread, (LPVOID)threadstart, funcsize, NULL) failed&quot;, &quot;DLL Injector&quot;, MB_OK);

   if(!(hThread = CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)thread, start, 0, 0)))
	   MessageBox ( NULL, &quot;Failed to Create remote thread&quot;, &quot;DLL Injector&quot;, MB_OK);

if((WaitForSingleObject(hThread, INFINITE)) == WAIT_OBJECT_0) //Bin mir nich sicher ob man das so machen darf, aber auch wenn ich nur WaitForSingleObject(...) schreibe ist der Fehler der Gleiche.
{
CloseHandle(hThread);
  if(!(VirtualFreeEx(hProc,start,0,MEM_RELEASE)))
		MessageBox ( NULL, &quot;Failed to release allocated memory&quot;, &quot;DLL Injector&quot;, MB_OK);

CloseHandle(hProc);

	return 0;
}
}
</code></pre>
<p>So finde ich ergibt das eigentlich Sinn. Meine dll enthält eine message box mit der Nachricht &quot;Message from dll&quot;. Wenn ich nun mit meinem Programm versuche die dll in Minesweeper zu injecten passiert folgendes: Als erstes kommt die Nachricht aus der dll. Klicke ich &quot;Ok&quot; meldet die winmine.exe &quot;winmine.exe hat ein Problem festgestellt und muss beendet werden&quot;. Klicke ich nun auf schließen kommt &quot;Failed to release allocated memory&quot;. Das das memory releasen nicht klappt ist ja auch klar, da der Prozess ja schon vorher abstürzt. Das Problem muss also irgendwo zwischen CreateRemoteThread und WaitForSingleObject auftreten.</p>
<p>GetLastError() nach CreateRemoteThread gibt übrigens Fehlercode 18 = ERROR_NO_MORE_FILES. Laut msdn dürfte das aber aus der Funktion listproc() stammen bzw aus der Funktion Process32Next().</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1613498</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1613498</guid><dc:creator><![CDATA[Grabgewalt]]></dc:creator><pubDate>Wed, 12 Nov 2008 11:42:33 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Wed, 12 Nov 2008 12:41:34 GMT]]></title><description><![CDATA[<p>Sorry für Doppelpost aber ich hab das alles nochmal bisschen umgestaltet. Hab nun auch ne uninject funktion was aber an meinem Problem nichts ändert, da es wie gesagt schon davor auftritt.</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;cstdio&gt;
#include &lt;tlhelp32.h&gt;
#include &lt;iostream&gt;
#include &lt;conio.h&gt;
#include &lt;string.h&gt;

using namespace std;

typedef HINSTANCE (*fpLoadLibrary)(char*);
typedef LPVOID (*fpGetProcAddress)(HINSTANCE, char*);
typedef void (*fpFunktion)(void);

BOOL Inject();
BOOL Uninject(DWORD id,HMODULE ah_ModuleHandle);

DWORD  hLibModule = 0;
DWORD id;

struct INJECTSTRUCT
{
      fpLoadLibrary LoadLibrary;
      fpGetProcAddress GetProcAddress;
      char path[255];
      char func[255];
};

DWORD WINAPI threadstart(LPVOID addr)
{
	HINSTANCE hDll;
	fpFunktion funktion;
	INJECTSTRUCT * is = (INJECTSTRUCT*)addr;       
	hDll = is-&gt;LoadLibrary(is-&gt;path);
	funktion = (fpFunktion)is-&gt;GetProcAddress(hDll, is-&gt;func);
	funktion();
	return 0;
}
void threadend()
{
}

bool EnableDebugPrivilege()
{
	TOKEN_PRIVILEGES priv;
	HANDLE hThis, hToken;
	LUID luid;

	hThis = GetCurrentProcess();

	OpenProcessToken(hThis, TOKEN_ADJUST_PRIVILEGES, &amp;hToken);

	LookupPrivilegeValue(0, &quot;seDebugPrivilege&quot;, &amp;luid);

	priv.PrivilegeCount = 1;
	priv.Privileges[0].Luid = luid;
	priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

	AdjustTokenPrivileges(hToken, false, &amp;priv, 0, 0, 0);

	CloseHandle(hToken);
	CloseHandle(hThis);

	return true;

}

void listproc()
{
	HANDLE hSnap, hTemp;
	PROCESSENTRY32 pe;
	pe.dwSize = sizeof(PROCESSENTRY32);

	EnableDebugPrivilege();

	hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

	if(Process32First(hSnap, &amp;pe))
	{
		do
		{
			hTemp = OpenProcess(PROCESS_ALL_ACCESS, 0, pe.th32ProcessID);
			if(hTemp)
			{
				printf(&quot;%4d\t%s\n&quot;, pe.th32ProcessID, pe.szExeFile);
				CloseHandle(hTemp);
			}
			if(!hTemp)
			{
				printf(&quot;%4d\t%s NO ACCESS\n&quot;, pe.th32ProcessID, pe.szExeFile);
				CloseHandle(hTemp);
			}
		}
		while(Process32Next(hSnap, &amp;pe));
	}
}

int main()
{
   listproc();
   if(!(Inject()))
   {
	   MessageBox ( NULL, &quot;Inject failed&quot;, &quot;DLL Injector&quot;, MB_OK);
	   return 0;
   }
   else
		MessageBox ( NULL, &quot;DLL injected&quot;, &quot;DLL Injector&quot;, MB_OK);

if(!(Uninject(id,(HMODULE)hLibModule)))
{
	MessageBox ( NULL, &quot;Uninject failed&quot;, &quot;DLL Injector&quot;, MB_OK);
	   return 0;
   }
else
	MessageBox ( NULL, &quot;DLL uninjected&quot;, &quot;DLL Injector&quot;, MB_OK);

return 1;
}

BOOL Inject()
{
   HANDLE hProc,hThread ;
   LPVOID start, thread;
   DWORD funcsize;
   HINSTANCE hDll;
   INJECTSTRUCT is;

    hDll = LoadLibrary(&quot;KERNEL32&quot;);

   is.LoadLibrary = (fpLoadLibrary)GetProcAddress(hDll, &quot;LoadLibraryA&quot;);
   is.GetProcAddress = (fpGetProcAddress)GetProcAddress(hDll, &quot;GetProcAddress&quot;);
   strcpy_s(is.path, &quot;J:\\Dokumente\\Programmierung\\C++\\DLL\\DLL\\Release\\DLL.dll&quot;);
   strcpy_s(is.func, &quot;Funktion&quot;);

   funcsize = (DWORD)threadend-(DWORD)threadstart;

   printf(&quot;\nProcess ID: &quot;);
   scanf_s(&quot;%d&quot;, &amp;id);

   hProc = OpenProcess(PROCESS_ALL_ACCESS, false, id);
   if(!hProc)
   {
	   MessageBox ( NULL, &quot;Unable to open process&quot;, &quot;DLL Injector&quot;, MB_OK);
	   return 0;
   }

   if(!(start = VirtualAllocEx(hProc, 0, funcsize+sizeof(INJECTSTRUCT), MEM_COMMIT, PAGE_READWRITE)))
   {
	   MessageBox ( NULL, &quot;Failed to allocate memory&quot;, &quot;DLL Injector&quot;, MB_OK);
	   return 0;
   }

   if(!(WriteProcessMemory(hProc, start, (LPVOID)&amp;is, sizeof(INJECTSTRUCT), 0)))
   {
		MessageBox ( NULL, &quot;WriteProcessMemory failed&quot;, &quot;DLL Injector&quot;, MB_OK);
		return 0;
   }

   thread = (LPVOID)((DWORD)start+sizeof(INJECTSTRUCT));

  if(!(WriteProcessMemory(hProc, thread, (LPVOID)threadstart, funcsize, NULL)))
  {
		MessageBox ( NULL, &quot;WriteProcessMemory(hProc, thread, (LPVOID)threadstart, funcsize, NULL) failed&quot;, &quot;DLL Injector&quot;, MB_OK);
			return 0;
   }

   if(!(hThread = CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)thread, start, 0, 0)))
   {
	   MessageBox ( NULL, &quot;Failed to Create remote thread&quot;, &quot;DLL Injector&quot;, MB_OK);
	   	return 0;
   }

WaitForSingleObject(hThread, INFINITE);

GetExitCodeThread( hThread, &amp;hLibModule );

  if(!(VirtualFreeEx(hProc,start,0,MEM_RELEASE)))
  {
		MessageBox ( NULL, &quot;Failed to release allocated memory&quot;, &quot;DLL Injector&quot;, MB_OK);
		return 0;
   }

CloseHandle(hProc);
return 1;
}

BOOL Uninject(DWORD id,HMODULE ah_ModuleHandle)
{
    HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, id);
    if (hProc == NULL)
        return false;

    bool lb_ReturnValue = false;

    HMODULE hLocKernel32 = GetModuleHandleW(L&quot;KERNEL32&quot;);
    FARPROC hLocLoadLibrary = GetProcAddress(hLocKernel32, &quot;FreeLibrary&quot;);

    if(ah_ModuleHandle != NULL)
    {
        HANDLE hThread = CreateRemoteThread(hProc, NULL, 0, (LPTHREAD_START_ROUTINE)hLocLoadLibrary, (void*)ah_ModuleHandle, 0, NULL );

        if( hThread != NULL )
        {
            DWORD ldw_ReturnCode;
            WaitForSingleObject( hThread, INFINITE );
            GetExitCodeThread( hThread, &amp;ldw_ReturnCode );
            CloseHandle( hThread );

            lb_ReturnValue = ldw_ReturnCode != 0;
        }
    }

    CloseHandle(hProc);

    return lb_ReturnValue; 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1613542</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1613542</guid><dc:creator><![CDATA[Grabgewalt]]></dc:creator><pubDate>Wed, 12 Nov 2008 12:41:34 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Wed, 12 Nov 2008 13:45:27 GMT]]></title><description><![CDATA[<p>Habs hingekriegt.</p>
<p>ERLEDIGT</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1613581</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1613581</guid><dc:creator><![CDATA[Grabgewalt]]></dc:creator><pubDate>Wed, 12 Nov 2008 13:45:27 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Wed, 21 Jan 2009 17:34:47 GMT]]></title><description><![CDATA[<p>Wie hast du es denn hinbekommen? (Hab das Selbe Problem)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1649724</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1649724</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Wed, 21 Jan 2009 17:34:47 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Wed, 21 Jan 2009 18:22:49 GMT]]></title><description><![CDATA[<p>Wie schön, dass ihr mein Posting ignoriert. Naja, wenn euch der Frickelweg lieber ist als 4 und 7 Zeilen...</p>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1649745</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1649745</guid><dc:creator><![CDATA[Crocker]]></dc:creator><pubDate>Wed, 21 Jan 2009 18:22:49 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Wed, 21 Jan 2009 18:50:55 GMT]]></title><description><![CDATA[<p>Danke Croker, jetzt gehts. (Ich hatte deinen Post glatt übersehen)</p>
<p>Ist es normal, das wenn das zu infizierende Programm in einer Schleife läuft, der eingeschläußte code nur einmal ausgeführt wird?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1649768</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1649768</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Wed, 21 Jan 2009 18:50:55 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Wed, 21 Jan 2009 19:18:06 GMT]]></title><description><![CDATA[<p>Lt. Code soll er das doch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1649793</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1649793</guid><dc:creator><![CDATA[schläußer]]></dc:creator><pubDate>Wed, 21 Jan 2009 19:18:06 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Wed, 21 Jan 2009 19:37:11 GMT]]></title><description><![CDATA[<p>Und wie muß ich das abändern, damit das jedesmal duchlaufenwird?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1649814</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1649814</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Wed, 21 Jan 2009 19:37:11 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Wed, 21 Jan 2009 23:05:37 GMT]]></title><description><![CDATA[<p>Was meinst du?<br />
Sobald die DLL in den Prozess gemapped wurde, wird DllMain mit &quot;reason == DLL_PROCESS_ATTACH&quot; aufgerufen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1649921</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1649921</guid><dc:creator><![CDATA[Crocker]]></dc:creator><pubDate>Wed, 21 Jan 2009 23:05:37 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Thu, 22 Jan 2009 07:11:50 GMT]]></title><description><![CDATA[<p>Hi Crocker,</p>
<p>ich weiß nicht so genau ob das überhaupt gehr was ich möchte (Ich bin ja schon froh das ich es jetzt hinbekommen habe. Auch den Frickelweg habe ich zum laufen gebracht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> )</p>
<p>Also ich möchte, dass die dll so eingeladen wird, das sie im infizierten Programm immer wieder aufgerufen wird.</p>
<p>Also wenn das zu infizierende Programm so aussieht.</p>
<pre><code class="language-cpp">int main () 
{
	while (true)
	{
		Sleep(6000);
		cout &lt;&lt; &quot;alles ok&quot;;
	}
       return 0;
}
</code></pre>
<p>und die DLL so:</p>
<pre><code class="language-cpp">BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	    return TRUE;
}

void Funktion()
{
	cout &lt;&lt; &quot;infiziert&quot;;   
}
</code></pre>
<p>Dann passiert bisher folgendes:<br />
Ausgabe: alles ok alles ok alles ok infiziert alles ok alles ok alles ok...</p>
<p>Das infiziert kommt genau einmal zu dem zeitpunkt zu dem ich das injection Programm aufrufe.</p>
<p>Nun hätte ich aber gerne, das Folgendes passiert:<br />
Ausgabe: alles ok infiziert alles ok infiziert alles ok infiziert...</p>
<p>Und zwar soll dies möglichst mit nur einem Aufruf des injection Programms möglich sein.<br />
(Wenn das überhaupt geht)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1649970</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1649970</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Thu, 22 Jan 2009 07:11:50 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Thu, 22 Jan 2009 12:04:07 GMT]]></title><description><![CDATA[<p>DllMain wird <strong>einmalig</strong> mit DLL_PROCESS_ATTACH aufgerufen wenn die DLL injiziert wird, was du darin machst ist doch dir überlassen. Allerdings darf man darin nicht alles tun (google dazu am besten mal (<strong>Ich glaube</strong>, wenn du in der DllMain eine eigene Funktion aufrufst, darfst du in dieser aber alles tun).<br />
Probier halt mal rum, mach ne Schleife mit cout rein oder sonstwas.</p>
<pre><code class="language-cpp">bool __stdcall DllMain(HMODULE, unsigned long reason, void*)
{
	if(reason == DLL_PROCESS_ATTACH)
	{
		; // Rufe zB. eine Funktion auf ...
	}
	else if(reason == DLL_PROCESS_DETACH)
	{
		;
	}

    return true;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1650114</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1650114</guid><dc:creator><![CDATA[Crocker]]></dc:creator><pubDate>Thu, 22 Jan 2009 12:04:07 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit dll injection [ERLEDIGT] on Fri, 23 Jan 2009 13:30:32 GMT]]></title><description><![CDATA[<p>Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1650849</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1650849</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Fri, 23 Jan 2009 13:30:32 GMT</pubDate></item></channel></rss>