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

时间:2014-04-04 02:55:14

标签: android memory

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

然后,它让我思考,"他们是如何做到的?" 我知道如何获取当前正在运行的应用程序列表,但挂钩到另一个进程并扫描和编辑该进程'记忆是......超出我的知识。

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

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

1)如何挂钩当前正在运行的其他应用?

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

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

inb4"您是否尝试过谷歌搜索?"

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

所以,我终于转向在这里开了一个帖子。

2 个答案:

答案 0 :(得分:20)

将此作为后代

经过一段时间的研究(阅读,连续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;
}

<强>参考文献:

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

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

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

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

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

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

答案 1 :(得分:-4)

就Linux而言,内核禁止修改属于其他进程的其他内存(顺便说一句,这就是Linux上没有病毒的原因)。 你实际在做的是编辑共享首选项。它们以纯文本形式编写,这意味着如果您可以访问它们(root),则可以对它们进行编辑。 您可以在Play商店查看CheatDroid应用程序。此外,如果您想自己开发类似的应用程序,您还可以查看此链接以创建您的第一个根应用程序。 http://www.xda-developers.com/android/how-to-build-an-android-app-part-2-writing-a-root-app-xda-tv/