Skip to content

Commit

Permalink
smarter way of attaching using mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandromrc committed Aug 2, 2023
1 parent 65cf404 commit bf526a7
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x64\Release
x64\Debug
.vs
37 changes: 37 additions & 0 deletions LibraryHijacker/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,34 @@ bool installHooks()
return true;
}

const char* mutexDetect = "hijackmutex1337";
HANDLE hMutex;

bool IsAlreadyInjected()
{
hMutex = OpenMutexA(MUTEX_ALL_ACCESS, FALSE, mutexDetect);
if (hMutex)
{
CloseHandle(hMutex);
return true;
}

// The mutex does not exist, indicating the DLL is not injected yet
// Create the mutex to prevent further injections
HANDLE hNewMutex = CreateMutexA(NULL, TRUE, mutexDetect);
if (!hNewMutex)
{
// Failed to create mutex
// Handle the error appropriately
}

if (hNewMutex != 0)
CloseHandle(hNewMutex);

return false;
}


BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
Expand All @@ -68,6 +96,12 @@ BOOL APIENTRY DllMain(HMODULE hModule,
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:

if (IsAlreadyInjected())
{
MessageBoxA(NULL, "Do you want to crash your game? Seems like you like desktop!", "Injection Error", MB_ICONERROR);
return FALSE;
}
installHooks();
std::thread([]() {
if (hijacked)
Expand All @@ -76,14 +110,17 @@ BOOL APIENTRY DllMain(HMODULE hModule,
{
MH_Uninitialize();
HMODULE hModule = GetModuleHandle(NULL);
ReleaseMutex(hMutex);
FreeLibraryAndExitThread(hModule, 0);
}
}
}).detach();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
ReleaseMutex(hMutex);
case DLL_PROCESS_DETACH:
ReleaseMutex(hMutex);
break;
}
return TRUE;
Expand Down
2 changes: 1 addition & 1 deletion LibraryHijacker/x64/Release/LibraryHijacker.dll.recipe
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<ProjectOutputs>
<ProjectOutput>
<FullPath>C:\Users\righe\Desktop\LibraryHijacker\x64\Release\LibraryHijacker.dll</FullPath>
<FullPath>C:\Users\user\Documents\GitHub\D3D-HIjacker\x64\Release\LibraryHijacker.dll</FullPath>
</ProjectOutput>
</ProjectOutputs>
<ContentFiles />
Expand Down
Binary file modified LibraryHijacker/x64/Release/LibraryHijacker.iobj
Binary file not shown.
Binary file modified LibraryHijacker/x64/Release/LibraryHijacker.ipdb
Binary file not shown.
14 changes: 7 additions & 7 deletions LibraryHijacker/x64/Release/LibraryHijacker.log
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
 dllmain.cpp
Generating code
36 of 148 functions (24.3%) were compiled, the rest were copied from previous compilation.
33 functions were new in current compilation
0 functions had inline decision re-evaluated but remain unchanged
Finished generating code
LibraryHijacker.vcxproj -> C:\Users\righe\Desktop\LibraryHijacker\x64\Release\LibraryHijacker.dll
 pch.cpp
dllmain.cpp
Generazione codice in corso...
Previous IPDB was built with incompatible compiler, fall back to full compilation.
All 148 functions were compiled because no usable IPDB/IOBJ from previous compilation was found.
Generazione codice terminata
LibraryHijacker.vcxproj -> C:\Users\user\Documents\GitHub\D3D-HIjacker\x64\Release\LibraryHijacker.dll
Binary file modified LibraryHijacker/x64/Release/LibraryHijacker.pch
Binary file not shown.
Binary file not shown.
Binary file modified LibraryHijacker/x64/Release/LibraryHijacker.tlog/CL.read.1.tlog
Binary file not shown.
Binary file modified LibraryHijacker/x64/Release/LibraryHijacker.tlog/CL.write.1.tlog
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
C:\Users\user\Documents\GitHub\D3D-HIjacker\LibraryHijacker\dllmain.cpp;C:\Users\user\Documents\GitHub\D3D-HIjacker\LibraryHijacker\x64\Release\dllmain.obj
C:\Users\user\Documents\GitHub\D3D-HIjacker\LibraryHijacker\pch.cpp;C:\Users\user\Documents\GitHub\D3D-HIjacker\LibraryHijacker\x64\Release\pch.obj
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.34.31933:TargetPlatformVersion=10.0.22621.0:VcpkgTriplet=x64-windows:
Release|x64|C:\Users\righe\Desktop\LibraryHijacker\|
PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.36.32532:TargetPlatformVersion=10.0.22000.0:
Release|x64|C:\Users\user\Documents\GitHub\D3D-HIjacker\|
Binary file not shown.
Binary file not shown.
Binary file modified LibraryHijacker/x64/Release/LibraryHijacker.tlog/link.write.1.tlog
Binary file not shown.
Binary file modified LibraryHijacker/x64/Release/dllmain.obj
Binary file not shown.
Binary file modified LibraryHijacker/x64/Release/pch.obj
Binary file not shown.
Binary file modified LibraryHijacker/x64/Release/vc143.pdb
Binary file not shown.

0 comments on commit bf526a7

Please sign in to comment.