Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Including polyhook makes my code crash on launch #154

Closed
sbtoonz opened this issue Sep 6, 2022 · 7 comments
Closed

Including polyhook makes my code crash on launch #154

sbtoonz opened this issue Sep 6, 2022 · 7 comments

Comments

@sbtoonz
Copy link

sbtoonz commented Sep 6, 2022

Unsure how to debug this since I cant even hit the APIENTRY for my .dll

when I use PolyHook2 and its associated static libs

Zycore.lib
Zydis.lib
asmjit.lib
asmtk.lib
PolyHook_2.lib

the test no longer loads ... at all

Steps to reproduce:

  1. Make new DLL C++ project in VS/Rider/Whatever

place this code in the "main.dll"

#define _CRT_SECURE_NO_WARNINGS
#include "Main.h"
#include <windows.h>
#include <fstream>
#include <iomanip>
#include <string>
#include <iostream>
#include <psapi.h>
#include <dxgi1_4.h>
using namespace  std;

int main()
{
    DisableThreadLibraryCalls(hModule);
    AllocConsole();
    freopen("CONOUT$", "w", stdout);
    std::cout << "injected" << std::endl;
    return 0;
}
  1. Compile -> inject ... watch the console open and display your text.

  2. Simply link PolyHook (via linker or by adding it as a dependency) -> compile

  3. inject DLL from step 4.... Console no longer Allocs...

It crashes so fast I cant even get the debugger to break on it

@sbtoonz
Copy link
Author

sbtoonz commented Sep 7, 2022

Possible non issue, if I create the DLL project within the polyhook solution everything works?

@sbtoonz sbtoonz closed this as completed Sep 7, 2022
@stevemk14ebr
Copy link
Owner

stevemk14ebr commented Sep 7, 2022

it's possible your other dll has imports that aren't being resolved at load time. Inspect the library dependencies, or use a different injector that gives better output, or debug at dll load time.

@sbtoonz
Copy link
Author

sbtoonz commented Sep 7, 2022

0>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\bin\HostX64\x64\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\USERNAMEHER\RiderProjects\PolyHook_2_0\x64\Debug\TestPolyHook.dll" /INCREMENTAL /ILK:"x64\Debug\TestPolyHook.ilk" /NOLOGO /LIBPATH:"C:\Users\USERNAMEHER\RiderProjects\vcpkg\installed\x64-windows\debug\lib" /LIBPATH:"C:\Users\USERNAMEHER\RiderProjects\vcpkg\installed\x64-windows\debug\lib\manual-link" kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib "C:\Users\USERNAMEHER\RiderProjects\vcpkg\installed\x64-windows\debug\lib\*.lib" /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\USERNAMEHER\RiderProjects\PolyHook_2_0\x64\Debug\TestPolyHook.pdb" /SUBSYSTEM:WINDOWS /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\USERNAMEHER\RiderProjects\PolyHook_2_0\x64\Debug\TestPolyHook.lib" /MACHINE:X64 /DLL x64\Debug\dllmain.obj
C:\Users\USERNAMEHER\RiderProjects\PolyHook_2_0\asmtk\Debug\asmjit.lib
C:\Users\USERNAMEHER\RiderProjects\PolyHook_2_0\asmtk\Debug\asmtk.lib
C:\Users\USERNAMEHER\RiderProjects\PolyHook_2_0\zydis\dependencies\zycore\Debug\Zycore.lib
C:\Users\USERNAMEHER\RiderProjects\PolyHook_2_0\zydis\Debug\Zydis.lib
C:\Users\USERNAMEHER\RiderProjects\PolyHook_2_0\Debug\PolyHook_2.lib
0>TestPolyHook.vcxproj -> C:\Users\USERNAMEHER\RiderProjects\PolyHook_2_0\x64\Debug\TestPolyHook.dll

I was almost positive it was getting packed as the output is 1:1 from the "standalone" project using linker
the standalone project using references
and the project added to the Solution generated by polyhook Cmake

I am going to have to reopen this issue as it is present even if I build this DLL within PolyHook Solution

Steps to replicate:

git clone --recursive https://github.com/stevemk14ebr/PolyHook_2_0.git
cd PolyHook_2.0
cmake .

Method A:
Open the generated SLN
Right click -> Add new -> C++ DLL
Name it whatever its for a test
Right click "Dependencies" and add the PolyHook_2.0/asmjit/asmtk/Zycore/Zydis as project dependencies

paste code from initial issue open

Elect to add your PLH hook (x86/64 whatever it may be) in my use case it was an x64Detour* detour = new x64Detour(fnAdd, fnCallback, userTrampVar)

Build project (check build log to ensure libs are linked)
inject DLL I now no longer get console even acknowledging DLL is running

Method B:
Open the generated SLN
click build for "build all"
relaunch VS /Rider as admin and build the "INSTALL" target
PolyHook installs to ProgramFilesX86

Right click -> Add new -> C++ DLL
Name it whatever its for a test
Right click the new project
go to C++ and include the "PolyHook2_0" build dir and add the folder called "includes" as extern includes
go to linker tab and add the "PolyHook2_0" build dir folder called "lib" to the library folder
go to linker additional dependencies
add:
PolyHook_2.lib
Zydis.lib
Zycore.lib
asmtk.lib
asmjit.lib

paste code from initial issue open

Elect to add your PLH hook (x86/64 whatever it may be) in my use case it was an x64Detour* detour = new x64Detour(fnAdd, fnCallback, userTrampVar)

Build project (check build log to ensure libs are linked)
inject DLL I now no longer get console even acknowledging DLL is running

Stupid question but how to debug C++ dll at runtime when they dont produce an exe ... this is my first forray into C++ stuff so forgive me ... in C# I just attach process set breakpoint and wait for break then do my debugging from there by stepping through stuff / editing the memory in debugger etc . I can choose to launch an exe in my C# projects in the debugger tab but I dont really see anything like that for what I wanna do in the C++ projects (time for google)

i am not sure how to execute this work flow in my current scenario as this is being applied to an exe i have no control over since I didnt produce the exe I am trying to inject the dll into

@sbtoonz sbtoonz reopened this Sep 7, 2022
@sbtoonz
Copy link
Author

sbtoonz commented Sep 7, 2022

https://stevemk14ebr.github.io/PolyHook_2_0/

Wish this was easier to find putzing around the internet.. the other documentation seems centric to polyhook1.0

errr I guess none of the documentation really matches current source as even the example code wont compile without some edits... which dont translate into the x64 file.. hmm

this sm0lbrain guy will try to figure out how to make it work I suppose

@EzequielDM
Copy link

Also having issues with this, tried both vcpkg and manually building PolyHook and static linking the libraries

@stevemk14ebr
Copy link
Owner

I need someone to debug what's happening before I can triage this

@sbtoonz sbtoonz closed this as not planned Won't fix, can't repro, duplicate, stale Sep 24, 2022
@stevemk14ebr
Copy link
Owner

Lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants