Okay so I made a DLL injector in VB.net a while ago. It works fine with any DLL except for mine. So I know the problem is with the DLL. Here is the injector's code:
Private Function Inject(ByVal pID As Integer, ByVal dllLocation As String) As Boolean
Dim hProcess As Integer = OpenProcess(&H1F0FFF, 1, pID)
If hProcess = 0 Then
Return False
MessageBox.Show("Could not open process!")
End If
Dim dllBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(dllLocation)
Dim allocAddress As Integer = VirtualAllocEx(hProcess, 0, dllBytes.Length, &H1000, &H4)
If allocAddress = Nothing Then
Return False
MessageBox.Show("Could not allocate the address!")
End If
Dim kernelMod As Integer = GetModuleHandle("kernel32.dll")
Dim loadLibAddr = GetProcAddress(kernelMod, "LoadLibraryA")
If (kernelMod = 0) Then
MessageBox.Show("Could not get the Module")
Return False
End If
If (loadLibAddr = 0) Then
MessageBox.Show("get the Process address!")
Return False
End If
WriteProcessMemory(hProcess, allocAddress, dllBytes, dllBytes.Length, 0)
Dim libThread As Integer = CreateRemoteThread(hProcess, 0, 0, loadLibAddr, allocAddress, 0, 0)
If libThread = 0 Then
Return False
MessageBox.Show("Error Creating thread!")
Else
WaitForSingleObject(libThread, 5000)
CloseHandle(libThread)
End If
CloseHandle(hProcess)
Threading.Thread.Sleep(1000)
Return True
End Function
This writes the process memory and creates a remote thread.
Now my project has two files: the header and the CPP File.
Header:
#ifdef MAINLIB_EXPORTS
#define MAINLIB_API __declspec(dllexport)
#else
#define MAINLIB_API __declspec(dllexport)
#endif
extern "C" MAINLIB_API DWORD TestFunction();
And the CPP:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include "dll.h"
#include "Urlmon.h"
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
hModule;
lpReserved;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
DWORD TestFunction()
{
MessageBox(0, TEXT("LOL"), TEXT("LMAO"), MB_OK);
return 1;
}
From what I understand is that this should run TestFunction on injection. But it doesn't. Any solutions/helpful pages I could use?
There is nothing in your code that specifies TestFunction needs to be called. Once the DLL is attached to the process only DllMain and global objects needing initialization are called. You need to call TestFunction when processing DLL_PROCESS_ATTACH.
DWORD TestFunction();
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
hModule;
lpReserved;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
TestFunction(); // < call TestFunction ONCE when dll is loaded
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
DWORD TestFunction()
{
MessageBox(0, TEXT("LOL"), TEXT("LMAO"), MB_OK);
return 1;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With