D
doliolarzz
Guest
I want to call test function in InjectDll.dll which is already injected to the process.
but It's said "The program is stopped working".
and I'm wonder how to get the return value of test function
Here is Inject function:
public void InjectDLL(IntPtr hProcess, String strDLLName)
{
IntPtr bytesout;
// Length of string containing the DLL file name +1 byte padding
Int32 LenWrite = strDLLName.Length + 1;
// Allocate memory within the virtual address space of the target process
IntPtr AllocMem = (IntPtr)VirtualAllocEx(hProcess, (IntPtr)null, (uint)LenWrite, 0x1000, 0x40); //allocation pour WriteProcessMemory
// Write DLL file name to allocated memory in target process
WriteProcessMemory(hProcess, AllocMem, strDLLName, (UIntPtr)LenWrite, out bytesout);
// Function pointer "Injector"
UIntPtr Injector = (UIntPtr)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
//UIntPtr Injector = (UIntPtr)GetProcAddress(GetModuleHandle(@"InjectDll.dll"), "test");
if (Injector == UIntPtr.Zero)
{
MessageBox.Show(" Injector Error! \n ");
// return failed
return;
}
// Create thread in target process, and store handle in hThread
//IntPtr hThread = (IntPtr)CreateRemoteThread(hProcess, (IntPtr)null, 0, Injector, AllocMem, 0, out bytesout);
IntPtr hThread = (IntPtr)CreateRemoteThread(hProcess, (IntPtr)null, 0, GetProcAddress(GetModuleHandle(@"InjectDll.dll"), "test"), AllocMem, 0, out bytesout);
Console.WriteLine("out"+bytesout);
// Make sure thread handle is valid
if (hThread == IntPtr.Zero)
{
MessageBox.Show(" hThread 1 Error! \n ");
return;
}
// Time-out is 10 seconds...
int Result = WaitForSingleObject(hThread, 10 * 1000);
// Check whether thread timed out...
if (Result == 0x00000080L || Result == 0x00000102L || Result == 0xFFFFFFFF)
{
/* Thread timed out... */
MessageBox.Show(" hThread 2 Error! \n ");
// Make sure thread handle is valid before closing... prevents crashes.
if (hThread != IntPtr.Zero)
{
//Close thread in target process
CloseHandle(hThread);
}
return;
}
// Sleep thread for 1 second
//Thread.Sleep(1000);
// Clear up allocated space ( Allocmem )
VirtualFreeEx(hProcess, AllocMem, (UIntPtr)0, 0x8000);
// Make sure thread handle is valid before closing... prevents crashes.
if (hThread != IntPtr.Zero)
{
//Close thread in target process
CloseHandle(hThread);
}
// return succeeded
return;
}
Here is InjectDll.cpp:
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
MessageBox(NULL, (LPCWSTR)L"Hello World!", (LPCWSTR)L"Dll says:", MB_OK);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
int test()
{
return 5;
}
Continue reading...
but It's said "The program is stopped working".
and I'm wonder how to get the return value of test function
Here is Inject function:
public void InjectDLL(IntPtr hProcess, String strDLLName)
{
IntPtr bytesout;
// Length of string containing the DLL file name +1 byte padding
Int32 LenWrite = strDLLName.Length + 1;
// Allocate memory within the virtual address space of the target process
IntPtr AllocMem = (IntPtr)VirtualAllocEx(hProcess, (IntPtr)null, (uint)LenWrite, 0x1000, 0x40); //allocation pour WriteProcessMemory
// Write DLL file name to allocated memory in target process
WriteProcessMemory(hProcess, AllocMem, strDLLName, (UIntPtr)LenWrite, out bytesout);
// Function pointer "Injector"
UIntPtr Injector = (UIntPtr)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
//UIntPtr Injector = (UIntPtr)GetProcAddress(GetModuleHandle(@"InjectDll.dll"), "test");
if (Injector == UIntPtr.Zero)
{
MessageBox.Show(" Injector Error! \n ");
// return failed
return;
}
// Create thread in target process, and store handle in hThread
//IntPtr hThread = (IntPtr)CreateRemoteThread(hProcess, (IntPtr)null, 0, Injector, AllocMem, 0, out bytesout);
IntPtr hThread = (IntPtr)CreateRemoteThread(hProcess, (IntPtr)null, 0, GetProcAddress(GetModuleHandle(@"InjectDll.dll"), "test"), AllocMem, 0, out bytesout);
Console.WriteLine("out"+bytesout);
// Make sure thread handle is valid
if (hThread == IntPtr.Zero)
{
MessageBox.Show(" hThread 1 Error! \n ");
return;
}
// Time-out is 10 seconds...
int Result = WaitForSingleObject(hThread, 10 * 1000);
// Check whether thread timed out...
if (Result == 0x00000080L || Result == 0x00000102L || Result == 0xFFFFFFFF)
{
/* Thread timed out... */
MessageBox.Show(" hThread 2 Error! \n ");
// Make sure thread handle is valid before closing... prevents crashes.
if (hThread != IntPtr.Zero)
{
//Close thread in target process
CloseHandle(hThread);
}
return;
}
// Sleep thread for 1 second
//Thread.Sleep(1000);
// Clear up allocated space ( Allocmem )
VirtualFreeEx(hProcess, AllocMem, (UIntPtr)0, 0x8000);
// Make sure thread handle is valid before closing... prevents crashes.
if (hThread != IntPtr.Zero)
{
//Close thread in target process
CloseHandle(hThread);
}
// return succeeded
return;
}
Here is InjectDll.cpp:
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
MessageBox(NULL, (LPCWSTR)L"Hello World!", (LPCWSTR)L"Dll says:", MB_OK);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
int test()
{
return 5;
}
Continue reading...