以编程方式扫描和编辑Android App内存值

Jus*_*tep 14 memory android

我一直在使用一些Android应用程序挂钩到另一个进程,扫描其分配的内存并进行编辑.显然,我用它来搞乱一些游戏.

然后,它让我想到,"他们是怎么做到的?" 我知道如何获取当前正在运行的应用程序列表,但挂钩到另一个进程,扫描和编辑进程的内存是..超出我的知识.

似乎我需要某种"root"权限来执行这样的代码,但我不介意.我只是想知道这些应用开发者是如何做到这一点来满足我的好奇心的.

所以..假设启用了root权限..

1)如何挂钩当前运行的不同应用程序?

2)如何扫描其内存区域?

3)如何编辑其内存区域?

inb4"你试过谷歌吗?"

我考虑过它并做了大量的谷歌搜索(1小时以上),但没有结果,因为"RAM"和"内存"这两个词只是给我一些东西,比如如何跟踪当前应用程序的内存分配等等.换句话说,不是我想要的.

所以,我终于转向在这里打开一个主题.

Jus*_*tep 21

把这个放在后代

经过一段时间的研究(阅读,连续5天),就Linux而言,可以通过简单地执行此操作来附加到进程,读取其内存并分离:

对于像我这样的新手,如果你更好的话,取消注释以及其他任何东西都会发表评论

#include <sys/ptrace.h> //For ptrace()
#include <sys/wait.h>   //For waitpid()

int main () {
    int pid     = 1337; //The process id you wish to attach to
    int address = 0x13371337; //The address you wish to read in the process

    //First, attach to the process
    //All ptrace() operations that fail return -1, the exceptions are
    //PTRACE_PEEK* operations
    if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1) {
        //Read the value of errno for details.
        //To get a human readable, call strerror()
        //strerror(errno) <-- Returns a human readable version of the
        //error that occurred
        return 0;
    }

    //Now, attaching doesn't mean we can read the value straight away
    //We have to wait for the process to stop
    int status;
    //waitpid() returns -1 on failure
    //W.I.F, not W.T.F
    //WIFSTOPPED() returns true if the process was stopped when we attached to it
    if (waitpid(pid, &status, 0) == -1 || !WIFSTOPPED(status)) {
        //Failed, read the value of errno or strerror(errno)
        return 0;
    }

    errno = 0; //Set errno to zero
    //We are about to perform a PTRACE_PEEK* operation, it is possible that the value
    //we read at the address is -1, if so, ptrace() will return -1 EVEN THOUGH it succeeded!
    //This is why we need to 'clear' the value of errno.
    int value = ptrace(PTRACE_PEEKDATA, pid, (void*)addr, NULL);
    if (value == -1 && errno != 0) {
        //Failed, read the value of errno or strerror(errno)
        return 0;
    } else {
        //Success! Read the value
    }

    //Now, we have to detach from the process
    ptrace(PTRACE_DETACH, pid, NULL, NULL);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

参考文献:

http://linux.die.net/man/2/ptrace

http://linux.die.net/man/2/waitpid

这与编辑Android应用内存值有何关系?

好吧,ptrace和wait的标头存在于Android NDK中.因此,要读取/写入应用程序的RAM,您需要在应用程序中使用本机代码.

此外,ptrace()需要root权限.

为什么这么长时间? 我以前从未写过这种代码.