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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 538|回复: 1
收起左侧

[其他原创] 对c语言函数进行反汇编获取函数大小并复制到内存里执行

[复制链接]
wangab 发表于 2024-1-1 15:39
最近想注入一个shell code,但是每次写shell code挺麻烦的,就想直接注入函数,但不知道函数的大小没法分配内存。于是想到了通过反汇编的方法获取函数占用内存的大小。
原理是从函数入口往下找到ret并统计指令的数量和大小。
关键代码:

[C++] 纯文本查看 复制代码
size_t GetFuncSIze(void * addr)
{
    ZyanU8* data = reinterpret_cast<ZyanU8*>(addr);
    // The runtime address (instruction pointer) was chosen arbitrarily here in order to better
    // visualize relative addressing. In your actual program, set this to e.g. the memory address
    // that the code being disassembled was read from.
    ZyanU64 runtime_address = reinterpret_cast<ZyanU64>(data);
    // Loop over the instructions in our buffer.
    ZyanUSize offset = 0;
    ZydisDisassembledInstruction instruction;
    while (ZYAN_SUCCESS(ZydisDisassembleIntel(
        /* machine_mode:    */ ZYDIS_MACHINE_MODE_LONG_64,
        /* runtime_address: */ runtime_address,
        /* buffer:          */ data + offset,
        /* length:          */ 15,
        /* instruction:     */ &instruction
    ))) {
        printf("%016" PRIX64 "  %s\n", runtime_address, instruction.text);
        offset += instruction.info.length; //count instruction length
        runtime_address += instruction.info.length;
        if (instruction.info.opcode == 0xc3) //find ret, then break
        {
            return offset;
        }
    }
    return 0;
}

下面的事情就简单了,把函数复制到一篇内存,执行即可。有两点注意:
1.DEBUG模式下函数有个jmp跳转,需要找到jump后的入口
2.函数调用是相对寻址,这里使用参数传入绝对地址
运行结果
无标题.png
代码:https://github.com/oakboat/GetFunctionSIze

免费评分

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

查看全部评分

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

IBinary 发表于 2024-1-2 10:47
Debug下寻找Jmp的代码可以参考我发的一篇文章 https://www.cnblogs.com/iBinary/p/14148137.html
其次想把函数当作ShellCode执行计算大小感觉不用这么麻烦.

void shellcode_fun(){.....}
void  shellCode_fun_end(){return};

将函数转为函数地址.  利用end - start就可以算出实际函数的大小了. 但是注意这两个函数要贴着.

auto fun_size = (DWORD)xxx_end - (DWORD)shellcode_fun;
这样函数大小就计算出来了.  注入 shellcode_fun 即可.

shellcode_fun 也要注意,里面不要使用到绝对地址. 要注意代码的重定位. ShellCode一定要和地址无关的代码.
您需要登录后才可以回帖 登录 | 注册[Register]

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

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

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

GMT+8, 2024-5-8 15:43

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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