WIN32 API Hooking

Hallo, dieses Thema interessiert mich brennend.
Das will ich erreichen:

image.axd


mrgreen.gif
... die DrawText(Ex) Funktionen hooken. Nur bin ich noch nicht ganz durchgestiegen. Ich habe dazu einige Fragen:

-Mauss ich dazu zwingend DLL-Injection beherrschen?
-Brauche ich Detours.h zwingend dazu? Oder bietet die Windows.h da etwas in der Richtung?
-Kennt ihr ein gutes Tutorial?
-Wenn nicht, ihr aber bescheid wisst wärt ihr bereit mich einzulernen?
-Hat jemand von euch sich schon mal damit beschäftigt und zufällig etwas Codegeschnipsel auf der Platte?
 
-Mauss ich dazu zwingend DLL-Injection beherrschen?
Ja, im Userland hookt man, indem man jeden Prozess manipuliert.

-Brauche ich Detours.h zwingend dazu? Oder bietet die Windows.h da etwas in der Richtung?
Es geht auch manuell in dem man die IAT manipuliert oder den Detour-Hook manuell anbringt, also ein paar Bytes im Funktionsanfang mit einem Jump zum eigenen Code überschreibt.

-Kennt ihr ein gutes Tutorial?
Suche bei Google nach Stichwort wie DLL-Injection, IAT-Hook, Detour-Hook, Userland-Hook...

--Wenn nicht, ihr aber bescheid wisst wärt ihr bereit mich einzulernen?
Nein, das sind Grundlagen, die ausreichend im Internet dokumentiert sind und die sich jeder selbst beibringt.
 
Grundlagen XD du bist gut.

Danke für deine Hilfe. Hör nexte Woche ein Seminar zu Codeinjection. Dann will ich mich mal ranwagen.

greets
 
Hi

also das Seminar ist leider komplett ausgefallen. Ich wollte das Thema, hab es leider nicht zugeteilt bekommen, und der Arsch, der es bekommen hat, ist abgesprungen.
Naja ich habe mich jetzt reingelesen und gleich das erste Problem:

Code:
#include "Windows.h"
#include <iostream>
#include <tchar.h>

#define TEST_DLL
#include "test_dll.h"
#include "detours.h"

#pragma comment(lib, "detours")

HANDLE _DllHandle;
HHOOK  _hInjectionHook; 
LRESULT CALLBACK WinHookCallBack(int nCode, WPARAM wParam, LPARAM lParam);


//=============================================================================
int (WINAPI *Real_DrawTextExA)(HDC hdc, LPSTR lpchText, int cchText, LPRECT lprc, UINT dwDTFormat, LPDRAWTEXTPARAMS lpDTParams) = DrawTextExA;
int  WINAPI  Mine_DrawTextExA (HDC hdc, LPSTR lpchText, int cchText, LPRECT lprc, UINT dwDTFormat, LPDRAWTEXTPARAMS lpDTParams){

  char   dllstrA[100] = "Mine_DrawTextExA";
  return Real_DrawTextExA(hdc, dllstrA, -1, lprc, dwDTFormat, NULL);

}
int (WINAPI *Real_DrawTextExW)(HDC hdc, LPWSTR lpchText, int cchText, LPRECT lprc, UINT dwDTFormat, LPDRAWTEXTPARAMS lpDTParams) = DrawTextExW;
int  WINAPI  Mine_DrawTextExW (HDC hdc, LPWSTR lpchText, int cchText, LPRECT lprc, UINT dwDTFormat, LPDRAWTEXTPARAMS lpDTParams){

  wchar_t dllstrW[100]=L"Mine_DrawTextExW";
  return  Real_DrawTextExW(hdc, dllstrW, -1, lprc, dwDTFormat, NULL);
}
//=============================================================================
BOOL (WINAPI *Real_ExtTextOutA)(HDC hdc, int X, int Y, UINT fuOptions, const RECT *lprc, LPCSTR lpString, UINT cbCount, const INT *lpDx)= ExtTextOutA;
BOOL  WINAPI  Mine_ExtTextOutA (HDC hdc, int X, int Y, UINT fuOptions, const RECT *lprc, LPCSTR lpString, UINT cbCount, const INT *lpDx){

  char dllstrA[100] = "Mine_ExtTextOutA";
  int slen = strnlen(dllstrA,maxlinelen);
  return Real_ExtTextOutA( hdc, X, Y, fuOptions, lprc, dllstrA, slen, lpDx );
}
BOOL (WINAPI *Real_ExtTextOutW)(HDC hdc, int X, int Y, UINT fuOptions, const RECT *lprc, LPCWSTR lpString, UINT cbCount, const INT *lpDx)= ExtTextOutW;
BOOL  WINAPI  Mine_ExtTextOutW (HDC hdc, int X, int Y, UINT fuOptions, const RECT *lprc, LPCWSTR lpString, UINT cbCount, const INT *lpDx){
  
  wchar_t dllstrW[100] = L"Mine_ExtTextOutW";
  int slen = wcsnlen(dllstrW,maxlinelen);
  return Real_ExtTextOutW( hdc, X, Y, fuOptions, lprc, dllstrW, slen, lpDx );
}
//=============================================================================
BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved){
  switch (ul_reason_for_call){
    case DLL_PROCESS_ATTACH:
    {
      _DllHandle = hModule;
      DetourTransactionBegin();
      DetourUpdateThread(::GetCurrentThread());
      DetourAttach(&(PVOID&)Real_DrawTextExA, Mine_DrawTextExA);
      DetourAttach(&(PVOID&)Real_DrawTextExW, Mine_DrawTextExW);
      DetourAttach(&(PVOID&)Real_ExtTextOutA, Mine_ExtTextOutA);
      DetourAttach(&(PVOID&)Real_ExtTextOutW, Mine_ExtTextOutW);
      DetourTransactionCommit();
      break;
    }
    case DLL_THREAD_ATTACH:break;
    case DLL_THREAD_DETACH:break;
    case DLL_PROCESS_DETACH:
    {
      DetourTransactionBegin();
      DetourUpdateThread(::GetCurrentThread());
      DetourDetach(&(PVOID&)Real_DrawTextExA, Mine_DrawTextExA);
      DetourDetach(&(PVOID&)Real_DrawTextExW, Mine_DrawTextExW);
      DetourDetach(&(PVOID&)Real_ExtTextOutA, Mine_ExtTextOutA);
      DetourDetach(&(PVOID&)Real_ExtTextOutW, Mine_ExtTextOutW);
      DetourTransactionCommit();
      break;
    }
  }
  return TRUE;
}
//=============================================================================
LRESULT CALLBACK WinHookCallBack(int nCode, WPARAM wParam, LPARAM lParam)
{  
  if (nCode < 0)
    return (CallNextHookEx (_hInjectionHook,nCode,wParam,lParam));
  return 0;
}
//=============================================================================
int SetHook(DWORD dwThreadId){
  _hInjectionHook=SetWindowsHookEx(WH_GETMESSAGE, WinHookCallBack, (HINSTANCE)_DllHandle, 0);
  return _hInjectionHook ? 1 : 0;
}
//=============================================================================


So siehts dann aus:
Vorher/Nachher:
vorher.PNG
nachher.PNG


Kann mir einer sagen warum es die DrawTextEx so verhaut?

EDIT:

Also ich habe herausgefunden wenn ich die ExtTextOut(...) auskommentiere, dann funktioniert der DrawTextEx(...) Hook.
Daraus habe ich vermuted dass die DrawTextEx(...) intern die ExtTextOut(...) aufruft. Obv falsch, denn wenn ich nur die ExtTextOut(...) hooke dann sieht alles was mit DrawTextEx(...) geprinted wird wieder so beknackt aus. Warum stört der ExtTextOut(...)hook die DrawTextEx(...)ausgabe?
 
Zuletzt bearbeitet:
Die Strings sind nur lokale Variablen.
Und wieso übergibst du in der Parameterliste einmal fest -1 oder NULL? Übergebe doch alle Parameter.
 
Na, das hat so seine Gründe.
cchText [in] The length of the string pointed to by lpchText. If cchText is -1, then the lpchText parameter is assumed to be a pointer to a null-terminated string and DrawTextEx computes the character count automatically.

da ich die strings ändere haben sie auch eine neue länge.


lpDTParams [in] A pointer to a DRAWTEXTPARAMS structure that specifies additional formatting options. This parameter can be NULL

keine faxxen soll er machen. einfach unformatiert ausgeben.





Mir ist schon klar dass die Variablen lokal sind. Aber die Funktion ist ja im selben Scope, also kann DrawTextEx (insofern es in der Funktion nicht auch einen Variable namens dllstrW gibt) auch nutzen. Erklär mir bitte ob ich falsch liege und warum. Aber bisher hat das so immer funktioniert.

Vorschläge?
 
Zurück
Oben