Search code examples
pythonwindowsdllcode-injectiondetours

Hooking Python code using Detours


I built a simple Python gui application("App.py") that I am trying to hook using detours. My understanding is that Python should use Windows dll's at some point and I am trying to hook these function calls.

For that purpose I am using detours withdll.exe :

withdll.exe /d:"myDLL.dll" "myprogram.exe"

Because withdll.exe doesn't accept running a program with arguments ("python.exe App.py"), I tried creating a bat file starter.bat as follows:

cd appdir
python App.py

And then running:

withdll.exe /d:"myDLL.dll" "starter.bat"

However this approach only hooks the background cmd process.

Is there a workaround to make detours hook the Python.exe process of my script ?


Solution

  • I looked through detours withdll.exe source code and found out that it can take command line arguments, the issue was solved using:

    withdll.exe /d:"myDLL.dll" "pathtopython/Python.exe" "pathtoscript/myscript.py"