Problem in creating remote thread
PostPosted:Thu Oct 18, 2012 5:14 pm
Hi
I am using the following code to inject my dll in every process that is started
I am creating "InjectDLLThread" thread after I get a callback for a newly created process
The problem I am having is that the "MyCreateRemoteThread" call succeeds for every process but the dll is not injected into all the processes.
Thanks
I am using the following code to inject my dll in every process that is started
Code: Select all
I am testing this on Windows 7 32 bit OSHANDLE NtCreateThreadEx(HANDLE hProcess, LPVOID lpRemoteThreadStart, LPVOID lpRemoteCallback)
{
typedef struct
{
ULONG Length;
ULONG Unknown1;
ULONG Unknown2;
PULONG Unknown3;
ULONG Unknown4;
ULONG Unknown5;
ULONG Unknown6;
PULONG Unknown7;
ULONG Unknown8;
} UNKNOWN;
typedef DWORD WINAPI NtCreateThreadEx_PROC
(
PHANDLE ThreadHandle,
ACCESS_MASK DesiredAccess,
LPVOID ObjectAttributes,
HANDLE ProcessHandle,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
BOOL CreateSuspended,
DWORD dwStackSize,
DWORD Unknown1,
DWORD Unknown2,
LPVOID Unknown3
);
UNKNOWN Buffer;
DWORD dw0 = 0;
DWORD dw1 = 0;
memset(&Buffer, 0, sizeof(UNKNOWN));
Buffer.Length = sizeof (UNKNOWN);
Buffer.Unknown1 = 0x10003;
Buffer.Unknown2 = 0x8;
Buffer.Unknown3 = &dw1;
Buffer.Unknown4 = 0;
Buffer.Unknown5 = 0x10004;
Buffer.Unknown6 = 4;
Buffer.Unknown7 = &dw0;
NtCreateThreadEx_PROC* VistaCreateThread = (NtCreateThreadEx_PROC*) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtCreateThreadEx");
if(VistaCreateThread == NULL)
{
return (NULL);
}
HANDLE hRemoteThread = NULL;
HRESULT hRes = 0;
hRes = VistaCreateThread(&hRemoteThread,
0x1FFFFF, // all access
NULL,
hProcess,
(LPTHREAD_START_ROUTINE)lpRemoteThreadStart,
lpRemoteCallback,
FALSE,
NULL,
NULL,
NULL,
&Buffer);
if(!SUCCEEDED(hRes))
{
return (NULL);
}
return (hRemoteThread);
}
HANDLE MyCreateRemoteThread(HANDLE hProcess, LPVOID lpRemoteThreadStart, LPVOID lpRemoteCallback)
{
if (GetProcAddress(GetModuleHandle("ntdll.dll"), "NtCreateThreadEx"))
{
return NtCreateThreadEx(hProcess, lpRemoteThreadStart, lpRemoteCallback);
}
else
{
return CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)lpRemoteThreadStart, lpRemoteCallback, 0, 0);
}
return NULL;
}
VOID InjectDLLThread(PVOID dummy)
{
DWORD Pid = (DWORD)dummy;
HANDLE hProcess = NULL;
LPVOID Memory = NULL;
LPVOID LoadLibraryx = NULL;
CHAR dll[MAX_PATH] = "AppinitHook.dll";
//Sleep(1000);
HANDLE ht = NULL;
hProcess = MyOpenProcess(PROCESS_ALL_ACCESS, FALSE, Pid);
if(hProcess == NULL || hProcess == INVALID_HANDLE_VALUE)
{
return;
}
LoadLibraryx = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
if (LoadLibraryx == NULL)
{
goto CleanUp;
}
Memory = (LPVOID)VirtualAllocEx(Process, NULL, strlen(dll) + 1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (Memory == NULL)
{
goto CleanUp;
}
if (!WriteProcessMemory(Process, (LPVOID)Memory, dll, strlen(dll) + 1, NULL))
{
goto CleanUp;
}
ht = MyCreateRemoteThread(Process, LoadLibraryx, (LPVOID)Memory);
if (ht)
{
WaitForSingleObject(ht, INFINITE);
}
CleanUp:
if(Memory)
{
VirtualFreeEx(hProcess, Memory, 0, MEM_RELEASE);
Memory = NULL;
}
if(ht)
{
CloseHandle(ht);
ht = NULL;
}
if(hProcess)
{
CloseHandle(hProcess);
hProcess = NULL;
}
}
I am creating "InjectDLLThread" thread after I get a callback for a newly created process
The problem I am having is that the "MyCreateRemoteThread" call succeeds for every process but the dll is not injected into all the processes.
Thanks