吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4123|回复: 20
收起左侧

[C&C++ 原创] 【C语言】【最新】支持x64和x32的代码注入库

  [复制链接]
iTruth 发表于 2020-10-11 19:00

支持x64和x32的代码注入库

如果这个帖子不符合版规请版主删除   

点此获取完整的源码
这是对cheatlib的一次重大更新,加入了对x64的支持
点此查看cheatlib第一版的帖子  

快速入门

测试程序源码
cheatlib_test.c   

#include <stdio.h>
#include "cheatlib.h"

PDllInjectionInfo inject_dll_test_info = NULL;
PCodeInjectionInfo code_info = NULL;

int main()
{
  // 根据窗口标题获取句柄
  HANDLE hTarget = GetHandleByTitle("Cheatlib Target");

  if(hTarget == NULL){
    puts("Failed to get target handle");
    return EXIT_FAILURE;
  }

  // dll注入演示
  inject_dll_test_info = DllInjection(hTarget, "inject_dll_test.dll");

  if(inject_dll_test_info == NULL){
    printf("Dll injection Failed\n");
    return EXIT_FAILURE;
  }

  Sleep(1000);
  // dll注出演示
  DllOutjection(inject_dll_test_info);

#ifdef CHEATLIB_TARGET_X64

  // 代码注入演示
  code_info = CodeInjection(hTarget, (LPVOID)0x40159a,
      "add dword ptr ss:[rbp-0x4], 0xff;"
      "push 0x401574;"
      "ret;"
      );

#else

  // 代码注入演示
  code_info = CodeInjection(hTarget, (LPVOID)0x40156a,
      "add dword ptr ss:[ebp-0xC], 0xff;"
      "push 0x40153E;"
      "ret;"
      );

#endif

  if(code_info == NULL){
    printf("Code Injection Failed\n");
    return EXIT_FAILURE;
  }

  Sleep(2000);
  // 代码注出演示
  CodeOutjection(code_info);

  return EXIT_SUCCESS;
}

测试动态库代码
inject_dll_test.c  

#include <stdio.h>
#include "cheatlib.h"

PFuncHookInfo func_hook_info = NULL;
PIATHookInfo iat_hook_info = NULL;

typedef printf_type int(*)(const char * restrict, ...);

int func_hooked_printf(const char * restrict format, ...)
{
  // to do something here...
  // 现在CallOrigFunc可以直接返回函数返回值
  return CallOrigFunc(func_hook_info, "This is Func hooked printf\n");
}

int iat_hooked_printf(const char * restrict format, ...)
{
  // to do something here...
  // IAT Hook 是不能用CallOrigFunc的
  return ((printf_type)iat_hook_info->pFuncAddress)("This is IAT hooked printf\n");
}

BOOL WINAPI DllMain(
    HINSTANCE hinstDLL,  // handle to DLL module
    DWORD fdwReason,     // reason for calling function
    LPVOID lpReserved )  // reserved
{
  // Perform actions based on the reason for calling.
  switch( fdwReason ) 
  { 
    case DLL_PROCESS_ATTACH:
      {
        // 从IAT获取函数地址
        LPVOID printf_addr = GetFuncFromIAT(NULL, "printf");

        // 函数钩子演示
        func_hook_info = FuncHook(printf_addr, (LPVOID)func_hooked_printf);

        if(func_hook_info == NULL){
          printf("function hook failed\n");
        }
        Sleep(2000);

        // 函数钩子撤销演示
        FuncUnhook(func_hook_info);

        // IAT钩子演示
        iat_hook_info = IATHook(NULL, "printf", (LPVOID)iat_hooked_printf);
        Sleep(2000);

        // 撤销IAT钩子演示
        IATUnhook(iat_hook_info);
      }
      break;
  }
  return TRUE;  // Successful DLL_PROCESS_ATTACH.
}

被攻击的目标程序代码
target.c   

#include <stdio.h>
#include <windows.h>

int main()
{
    SetConsoleTitle("Cheatlib Target");
    for(int i=0;;++i)
    {
        printf("Target Program: %d printf address: %p\n", i, printf);
        Sleep(200);
    }
    return 0;
}

如何编译这些代码?

这个库可以在VS项目里使用也可以在Mingw(GCC)项目里使用,这里演示在Mingw(GCC)中的使用方法
cheatlib.h cheatlib.dll keystone.dll capstone.dll复制到你的项目目录下
编译32位运行  

gcc -shared cheatlib.dll inject_dll_test.c -o inject_dll_test.dll -m32
gcc cheatlib.dll cheatlib_test.c -o cheatlib_test.exe -m32
gcc target.c -o target.exe -m32

编译64位运行   

gcc -shared cheatlib.dll inject_dll_test.c -o inject_dll_test.dll -m64 -D CHEATLIB_TARGET_X64
gcc cheatlib.dll cheatlib_test.c -o cheatlib_test.exe -m64 -D CHEATLIB_TARGET_X64
gcc target.c -o target.exe -m64

对比上一版增加了那些内容?

  • x64支持而且接口不变,只需简单定义CHEATLIB_TARGET_X64宏即可转变成x64的版本
  • IAT Hook 支持
  • 获取IAT数据支持
  • CallOrigFunc 直接获取返回值支持.你可以直接写作 return CallOrigFunc(ptInfo, arg1, arg2);

注意

CodeInjection函数不会将跳转覆盖的指令复制到执行区执行
因此有必要知道在x32和x64下跳转需要占用多大的空间
x32下的跳转:  

jmp hook function  

共计5字节  

x64下的跳转:  

push target low address
mov dword ptr ss:[rsp], target high address
ret  

共计14字节  

最后

我写这个库是希望它能在各种场合下为你带来便利,有任何用的不爽或者不知道如何使用请让我知道

免费评分

参与人数 8威望 +1 吾爱币 +25 热心值 +6 收起 理由
tony666 + 1 + 1 谢谢@Thanks!
Eaglecad + 1 + 1 我很赞同!
netspirit + 1 鼓励转贴优秀软件安全工具和文档!
奎奎 + 1 + 1 谢谢@Thanks!
忒修斯 + 1 热心回复!
苏紫方璇 + 1 + 20 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
yanyyuan + 1 我很赞同!
huayugongju + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

  • · Aarow|主题: 991, 订阅: 304

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

lypxynok 发表于 2020-10-11 19:55
楼主厉害
lyliucn 发表于 2020-10-11 20:19
巅峰Clown 发表于 2020-10-11 20:20
Nevatu 发表于 2020-10-11 22:25
感谢分享
rainbow270118 发表于 2020-10-11 23:16
多谢分享,很强大
Eaglecad 发表于 2020-10-12 00:07
必须收藏一波
netpeng 发表于 2020-10-12 02:38
这个就厉害了,赶紧收藏。
jwpiaoi 发表于 2020-10-12 07:43
支持,学习学习。
ciker_li 发表于 2020-10-12 07:57
收藏学习
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-12 10:42

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表