首页 > 其他分享 >一种inlineHook检测方案

一种inlineHook检测方案

时间:2023-01-07 15:23:09浏览次数:62  
标签:方案 handle 检测 bytes sym local hook console inlineHook

定义

inlinehook是修改内存中的机器码来实现hook的方式

我们用frida查看一个函数hook之前和hook之后的机器码,这里以open函数为例:

let bytes_count = 32
let address = Module.getExportByName("libc.so","open")

let before = ptr(address)
console.log("")
console.log("[*] before hook: ")
console.log(hexdump(before, {
    offset: 0,
    length: bytes_count,
    header: true,
    ansi: true
  }));

let isOutput = false

Interceptor.attach(address, {
	onEnter:function(args){
        if(isOutput) return;
		let after = ptr(address)
        console.log("")
		console.log("[*] after hook: ")
        console.log(hexdump(after, {
            offset: 0,
            length: bytes_count,
            header: true,
            ansi: true
        }))
        isOutput = true
	},
	onLeave:function(retv){
	}
});

hook之前:

hook之后:

可见,hook之后,函数开头的字节被修改了

思考

inlinehook只修改了内存中的机器码,而内存中的机器码是从文件加载而来的,所以我们可以将函数在内存中字节和本地对应的字节进行比较,如果不一致,那么可以认为内存中的字节被修改了,即被inlinehook了。

实现

#ifdef __LP64__
    const char *lib_path = "/system/lib64/libc.so";
#else
    const char *lib_path = "/system/lib/libc.so";
#endif
#define CMP_COUNT 8
    const char *sym_name = "open";

    struct local_dlfcn_handle *handle = static_cast<local_dlfcn_handle *>(local_dlopen(lib_path));

    off_t offset = local_dlsym(handle,sym_name);

    FILE *fp = fopen(lib_path,"rb");
    char file_bytes[CMP_COUNT] = {0};
    if(fp != NULL){
        fseek(fp,offset,SEEK_SET);
        fread(file_bytes,1,CMP_COUNT,fp);
        fclose(fp);
    }

    void *dl_handle = dlopen(lib_path,RTLD_NOW);
    void *sym = dlsym(dl_handle,sym_name);

    int is_hook = memcmp(file_bytes,sym,CMP_COUNT) != 0;

    local_dlclose(handle);
    dlclose(dl_handle);

    char text[128] = {0};
    snprintf(text,128,"Function \"%s\" is Hook: %s",sym_name,is_hook ? "true" : "false");

这里local_开头的函数是读取本地符号偏移库,库代码:https://github.com/luoyesiqiu/local_dlfcn

用frida hook测试demo:frida-trace -U -i "open" -f com.luoye.localdlfcn

标签:方案,handle,检测,bytes,sym,local,hook,console,inlineHook
From: https://www.cnblogs.com/luoyesiqiu/p/inlineHookDetect.html

相关文章

  • (首发)12-80V 2A 亮/半亮/爆闪 三功能LED车灯方案
    此方案应用领域电动车,摩托车灯照明汽车灯照明手电筒芯片特点;世微三功能降压恒流驱动器宽输入电压范围:5V~100V可设定电流范围:10mA~2200mA固定工作频率:150KHZ内置抖频电路,......
  • 基于vue+Element Table封装(纯前端解决方案,附源码)
    (文章目录)前言这个项目是拿来练手的项目,基于VUE+ElementUI,并没有做后端,所以用的是纯前端的解决方案(有更好的办法欢迎提出),主要实现的是对列表数据的查询,筛选,修改这些常......
  • GitHub车牌检测识别项目调研
    汽车车牌检测和识别实践指南,提供了算法方案和测试效果。​一,EasyOCR​​​1.1,仓库介绍​​​​1.2,使用记录​​​二,HyperLPR​​​2.1,HyperLPR概述​​​​2......
  • 分布式事务解决方案-后端分析
    一、什么是分布式事务在早期的单体架构时期,所有的数据操作都在同一个数据库里面进行,比如:A给B转100块钱,A的账户余额-100,B的账户余额+100,这两个操作放在同一个事务里面即可,......
  • Vmware Vcenter&Vmware Horizon漏洞检测与利用一条龙
    VmwareVcenter&VmwareHorizon漏洞检测与利用一条龙 文章作者:[email protected] 1、VmwareVcenter漏洞被动检测我们可以利用BurpSuite软件结合插件对VmwareVcen......
  • 内核hung检测机制(3)
    实践编写ko在内核线程上下文中触发D状态死锁代码hungtask.c#include<linux/init.h>#include<linux/sched.h>//currentmacro#include<linux/module.h>voidhu......
  • GitHub车牌检测识别项目调研
    汽车车牌检测和识别实践指南,提供了算法方案和测试效果。​一,EasyOCR​​​1.1,仓库介绍​​​​1.2,使用记录​​​二,HyperLPR​​​2.1,HyperLPR概述​​​​2......
  • GitHub车牌检测识别项目调研
    汽车车牌检测和识别实践指南,提供了算法方案和测试效果。​一,EasyOCR​​​1.1,仓库介绍​​​​1.2,使用记录​​​二,HyperLPR​​​2.1,HyperLPR概述​​​​2......
  • 内核hung检测机制(2)
    实践部分编写ko触发D状态死锁hung_task.c#include<linux/init.h>#include<linux/module.h>DEFINE_MUTEX(ckw_hung_task_mutex);staticint__inithung_task_in......
  • 【深度学习】YOLOv5快速开始自定义训练与检测(附带模板项目)
    ✨YoLov5yolov5原仓库地址https://github.com/ultralytics/yolov5更多详细信息可阅读官方文档✨项目说明项目介绍此项目主要用于构建yolov5-3.1训练及检测环境......