【问题标题】:linux ptrace() get function informationlinux ptrace() 获取函数信息
【发布时间】:2012-10-25 17:47:51
【问题描述】:

我想使用 ptrace() 调用从用户定义的函数中捕获信息。

但函数地址不稳定(因为 ASLR)。

如何以编程方式获取其他程序的功能信息,如 gdb?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <dlfcn.h>
#include <errno.h>

void error(char *msg)
{
    perror(msg);
    exit(-1);
}

int main(int argc, char **argv)
{
    long ret = 0;
    void *handle;
    pid_t pid = 0;
    struct user_regs_struct regs;
    int *hackme_addr = 0;

    pid = atoi(argv[1]);

    ret = ptrace(PTRACE_ATTACH, pid, NULL, NULL);
    if(ret<0)
    {
        error("ptrace() error");
    }

    ret = waitpid(pid, NULL, WUNTRACED);
    if(ret<0)
    {
        error("waitpid ()");
    }

    ret = ptrace(PTRACE_GETREGS, pid, NULL, &regs);
    if(ret<0)
    {
        error("GETREGS error");
    }

    printf("EIP : 0x%x\n", (int)regs.eip);

    ptrace(PTRACE_DETACH, pid, NULL, NULL);

    return 0;
}

【问题讨论】:

    标签: linux debugging hook ptrace api-hook


    【解决方案1】:

    ptrace 有点难看,但是很有用。

    这是一个 ptrace 示例程序;它用于使与 I/O 相关的系统调用暂停。 http://stromberg.dnsalias.org/~strombrg/slowdown/

    你当然也可以学习 gdb,但是 ISTR 非常庞大。

    您还可以查看 strace 和 ltrace,尤其是 ltrace,因为它列出了符号。

    HTH

    【讨论】:

      【解决方案2】:

      您可能想要调用驻留在特定可执行文件(可能是共享对象)中的函数。因此,首先,您必须使用

      找到此可执行文件所映射的基地址
      /proc/pid/maps
      

      之后,你需要找到你感兴趣的函数的本地偏移量,你可以通过两种方式做到这一点:

      1. 了解ELF文件格式(Linux本机可执行格式),并使用映射文件搜索所需功能(这需要一些专业知识)
      2. 使用现成的elfparser(可能是readelf工具)获取可执行文件下的函数偏移量。请注意,您必须计算出真正的本地偏移量,因为该工具通常会为您提供地址,就好像可执行文件已映射到特定地址一样

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-26
        • 2011-01-29
        • 2022-07-18
        • 1970-01-01
        • 2019-10-02
        • 2014-06-12
        • 1970-01-01
        相关资源
        最近更新 更多