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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4274|回复: 7
收起左侧

[原创] frida hook 中间函数,并打印寄存器

[复制链接]
AsmJit 发表于 2021-3-30 14:55
frIDA 支持全平台,不想傻傻的在 C++ 写 inline hook,还是脚本快。AOB 签名第一个 hook 位置,中间 aob 模式 6A 00 8B CE E8 ?? ?? ?? ?? EB,不够 5 字节,还调用了一个 CALL,需要找到目的地址
2021-03-30 14-19-38.png

目的地址 aob 模式 6A FF 68 ?? ?? ?? ?? 64 A1 00 00 00 00
2021-03-30 14-21-44.png

蹦床结果蹦床代码,下面使用 frida 写入字节码和反汇编。
2021-03-30 14-23-14.png

注意分配的页面内存不能在闭包里,不然会被释放掉 hook 多处,复制粘贴,闭包实现。

frida hook

[JavaScript] 纯文本查看 复制代码
send('init hook!');

var exeModule = Process.getModuleByName('hexin_001.exe');

let thunkPage = null;
let thunkOffset = 0;

const thunkRelocators = {
    ia32: global.X86Relocator,
    x64: global.X86Relocator,
    arm: global.ThumbRelocator,
    arm64: global.Arm64Relocator
};

const thunkWriters = {
    ia32: global.X86Writer,
    x64: global.X86Writer,
    arm: global.ThumbWriter,
    arm64: global.Arm64Writer
};

function makeThunk(size, write) {
    if (thunkPage === null) {
        thunkPage = Memory.alloc(Process.pageSize);
    }

    const thunk = thunkPage.add(thunkOffset);

    const arch = Process.arch;

    const Writer = thunkWriters[arch];
    Memory.patchCode(thunk, size, code => {
        const writer = new Writer(code, {
            pc: thunk
        });
        write(writer);
        writer.flush();
        if (writer.offset > size) {
            throw new Error(`Wrote ${writer.offset}, exceeding maximum of ${size}`);
        }
    });

    thunkOffset += size;

    return (arch === 'arm') ? thunk.or(1) : thunk;
}



const print_ecx = new NativeCallback(value => {
    console.log('ecx value: ' + value);
}, 'void', ['pointer']);


// 0x008E089E

// 这个必须在全局分配内存,不能在闭包分配,不然会被释放掉
//var trampoline = Memory.alloc(Process.pageSize);

((module, hook_addpress_pattern) => {



    var other_function_address = Memory.scanSync(module.base, module.size, "6A FF 68 ?? ?? ?? ?? 64 A1 00 00 00 00 50 64 89 25 00 00 00 00 83 EC 34 53 55 56 57 8B F9 8D 4F 3C 89 7C 24 1C E8 ?? ?? ?? ?? 8B D8 85 DB 0F 84 ?? ?? ?? ?? 8B CB E8 ?? ?? ?? ?? 8B F0 85 F6")[0].address;
    console.log('other_function_address:' + other_function_address);

    var hook_address = Memory.scanSync(module.base, module.size, hook_addpress_pattern)[0].address;
    var ret_address = hook_address.add(9);


    let trampoline = makeThunk(64, cw => {
        cw.putBytes([0x6A, 0x00, 0x8B, 0xCE]);
        cw.putPushfx();
        cw.putPushax();
        cw.putCallAddressWithAlignedArguments(print_ecx, ['ecx']);
        cw.putPopax();
        cw.putPopfx();
        cw.putCallAddress(other_function_address);
        cw.putJmpAddress(ret_address);
        cw.flush();
    });

    console.log('trampoline address: ' + trampoline);

    Memory.patchCode(hook_address, 9, code => {
        var cw = new X86Writer(code, {
            pc: hook_address
        });

        cw.putJmpAddress(trampoline);
        cw.putNopPadding(4);
        cw.flush();
    });

})(exeModule, "6A 00 8B CE E8 ?? ?? ?? ?? EB 0B 8D 8E 68 04 00 00 E8 ?? ?? ?? ?? 8B CE E8 ?? ?? ?? ?? 85 C0 75 0E 50 FF 15 ?? ?? ?? ?? B8 01 00 00 00 EB 62 8B 40 1C 50 FF 15 ?? ?? ?? ?? B8 01 00 00 00");


JavaScript下一步应该写一个 x64dbg 插件,自动生成 js 代码,就像 CE 的 AOB injection。

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
Hmily + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

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

云在天 发表于 2021-3-31 01:36
重新排一下版吧
xiaolongv5 发表于 2021-3-31 08:23
IBinary 发表于 2021-3-31 09:04
 楼主| AsmJit 发表于 2021-3-31 13:27
帖子需要审核,来不及排版,已经修正过,等待吧
zhuyanxiang 发表于 2021-4-1 09:33
非常棒的学习知识,谢谢大佬
sunny_shaw 发表于 2021-5-18 19:04
正在学习用frida hook windows程序,写的很好,学习了
yaorong 发表于 2021-5-20 15:36
dsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
您需要登录后才可以回帖 登录 | 注册[Register]

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

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

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

GMT+8, 2024-4-26 06:04

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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