【MalDev-04】API Hook基础与实战

admin 2024年11月23日12:09:43评论15 views字数 4087阅读13分37秒阅读模式

04-API hook

例子:exe调用dll,后hook掉dll中的函数

dll代码:


#include <windows.h>

// Entry point of the DLL
BOOL APIENTRY DllMain(HMODULE hModule,  DWORD  dwReasonForCall, LPVOID lpReserved) {
  switch (dwReasonForCall) {
    case DLL_PROCESS_ATTACH:
      break;
    case DLL_PROCESS_DETACH:
      break;
    case DLL_THREAD_ATTACH:
      break;
    case DLL_THREAD_DETACH:
      break;
  }
  return TRUE;
}

// Exported function to display a cat message box
extern "C" {
  __declspec(dllexport) int _cdecl Cat(LPCTSTR message) {
    MessageBox(NULL, message, "=^..^=", MB_OK);
    return 1;
  }
}

// Exported function to display a mouse message box
extern "C" {
  __declspec(dllexport) int _cdecl Mouse(LPCTSTR message) {
    MessageBox(NULL, message, "<:3()~~", MB_OK);
    return 1;
  }
}

// Exported function to display a frog message box
extern "C" {
  __declspec(dllexport) int _cdecl Frog(LPCTSTR message) {
    MessageBox(NULL, message, "8)~", MB_OK);
    return 1;
  }
}

// Exported function to display a bird message box
extern "C" {
  __declspec(dllexport) int _cdecl Bird(LPCTSTR message) {
    MessageBox(NULL, message, "<(-)", MB_OK);
    return 1;
  }
}

编译

x86_64-w64-mingw32-gcc -shared -o pet.dll pet.cpp -fpermissive

exe代码

#include <windows.h>
typedef int (__cdecl *CatFunction)(LPCTSTR message);
typedef int (__cdecl *BirdFunction)(LPCTSTR message);

int main(void) {
  HINSTANCE petDll;
  CatFunction catFunction;
  BirdFunction birdFunction;
  BOOL unloadResult;
  petDll = LoadLibrary("pet.dll");

  if (petDll != NULL) {
    catFunction = (CatFunction) GetProcAddress(petDll, "Cat");
    birdFunction = (BirdFunction) GetProcAddress(petDll, "Bird");
    if ((catFunction != NULL) && (birdFunction != NULL)) {
      (catFunction)("meow-meow");
      (catFunction)("mmmmeow");
      (birdFunction)("tweet-tweet");
    }
    unloadResult = FreeLibrary(petDll);
  }
  return 0;
}

编译

86_64-w64-mingw32-g++ -O2 cat.c -o cat.exe -mconsole -I/usr/share/mingw-w64/include/ -s -ffunction-sections -fdata-sections -Wno-write-strings -fno-exceptions -fmerge-all-constants -static-libstdc++ -static-libgcc -fpermissive

运行后依次弹窗

下面hook掉cat函数

#include <windows.h>

typedef int (__cdecl *OriginalCatFunction)(LPCTSTR message);

// 保存原始5个字节
char originalBytes[5];

FARPROC hookedFunctionAddress;

// 自己自实现的cat函数
int __stdcall myModifiedCatFunction(LPCTSTR modifiedMessage) {
  HINSTANCE petDll;
  OriginalCatFunction originalCatFunc;

  // unhook还原
  WriteProcessMemory(GetCurrentProcess(), (LPVOID)hookedFunctionAddress, originalBytes, 5NULL);

  // 重新加载dll
  petDll = LoadLibrary("pet.dll");
  //获取cat函数地址
  originalCatFunc = (OriginalCatFunction)GetProcAddress(petDll, "Cat");
  //传递更改后的参数进行调用
  return (originalCatFunc)("meow-squeak-tweet!!!");
}

// 安装hook实现
void installMyHook() {
  HINSTANCE hLib;
  VOID *myModifiedFuncAddress;
  DWORD *relativeOffset;
  DWORD source;
  DWORD destination;
  CHAR patch[5] = {0};

  // 获取dll
  hLib = LoadLibraryA("pet.dll");
  //获取dll中cat函数地址
  hookedFunctionAddress = GetProcAddress(hLib, "Cat");

  // 读取前5个字节保存到originalBytes中
  ReadProcessMemory(GetCurrentProcess(), (LPCVOID)hookedFunctionAddress, originalBytes, 5NULL);

  // 获取自己实现的cat函数地址
  myModifiedFuncAddress = &myModifiedCatFunction;

  // 计算跳转的偏移
  source = (DWORD)hookedFunctionAddress + 5;
  destination = (DWORD)myModifiedFuncAddress;
  relativeOffset = (DWORD *)(destination - source);

  // xE9 跳转机器码 jmp xxxx
  memcpy(patch, "xE9"1);
  memcpy(patch + 1, &relativeOffset, 4);
  //写入跳转指令
  WriteProcessMemory(GetCurrentProcess(), (LPVOID)hookedFunctionAddress, patch, 5NULL);
}

int main() {
  HINSTANCE petDll;
  OriginalCatFunction originalCatFunc;

  // 加载dll
  petDll = LoadLibrary("pet.dll");
  // cat函数
  originalCatFunc = (OriginalCatFunction)GetProcAddress(petDll, "Cat");

  // 调用原始cat函数
  (originalCatFunc)("meow-meow");

  // 安装hook
  installMyHook();

  // 调用被hook的cat函数
  (originalCatFunc)("meow-meow");
}

编译

x86_64-w64-mingw32-g++ -O2 hack1.c -o hack1.exe -mconsole -I/usr/share/mingw-w64/include/ -s -ffunction-sections -fdata-sections -Wno-write-strings -fno-exceptions -fmerge-all-constants -static-libstdc++ -static-libgcc -fpermissive

在windows10上无法运行,需要在win7上测试

加微信拉群分享更多学习资料

【MalDev-04】API Hook基础与实战

原文始发于微信公众号(高级红队专家):【MalDev-04】API Hook基础与实战

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年11月23日12:09:43
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   【MalDev-04】API Hook基础与实战https://cn-sec.com/archives/3428087.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.

发表评论

匿名网友 填写信息