简体   繁体   English

更改另一个应用程序的地址指针

[英]change a pointer of address of another application

I need somebody to edit the title, I can't find better title. 我需要有人来编辑标题,我找不到更好的标题。


Assume a have this simple program called source.exe : 假设有一个名为source.exe简单程序:

#include <stdio.h>

int main()
{
   int a = 5;
   printf("%p", &a);
   return 0;
}

I want to write another application, change.exe , that changes a in the above. 我想编写另一个应用程序, change.exe ,它在上面改变a

I tried something like this: 我试过这样的事情:

int main()
{
   int * p = (int*) xxx; // xxx is what have printed above
   *p = 1;
   printf("%d", *p);
   return 0;
}

It doesn't work. 它不起作用。 assuming I have Administrator rights, is there a way to do what I've tried above? 假设我拥有管理员权限,有没有办法做我上面尝试过的事情? thanks. 谢谢。

In first place, when you run the second program, the a in the first will be long gone (or loaded in a different position). 首先,当你运行第二个程序时,第a程序中的a将很久(或加载到不同的位置)。 In second place, many OS's protect programs by loading them in separate spaces. 第二,许多操作系统通过将程序加载到不同的空间来保护程序。

What you really seem to be looking for is Inter-Process Communication (IPC) mechanisms, specifically shared memory or memory-mapped files. 您真正需要的是进程间通信(IPC)机制,特别是共享内存或内存映射文件。

On most traditional computers that people deal with, the operating system makes use of virtual memory. 在人们处理的大多数传统计算机上,操作系统使用虚拟内存。 This means that two processes can both use address 0x12340000 and it can refer to two different pieces of memory. 这意味着两个进程都可以使用地址0x12340000 ,它可以引用两个不同的内存。

This is helpful for a number of reasons, including memory fragmentation, and allowing multiple applications to start and stop at random times. 这有很多原因,包括内存碎片,允许多个应用程序随机启动和停止。

On some systems, like TI DSPs for example, there is no MMU, and thus no virtual memory. 在某些系统上,例如TI DSP,没有MMU,因此没有虚拟内存。 On these systems, something like your demo application could work. 在这些系统上,像演示应用程序这样的东西可以工作。

I was feeling a bit adventurous, so I thought about writing something like this under Windows, using the WinAPI, of course. 我感觉有点冒险,所以我想在Windows下使用WinAPI编写类似的东西,当然。 Like Linux's ptrace , the calls used by this code should only be used by debuggers and aren't normally seen in any normal application code. 与Linux的ptrace一样,此代码使用的调用只能由调试器使用,通常不会出现在任何普通的应用程序代码中。

Furthermore, opening another process' memory for writing requires you to open the process handle with PROCESS_VM_WRITE and PROCESS_VM_OPERATION privileges. 此外,打开另一个进程的内存进行写入需要您使用PROCESS_VM_WRITEPROCESS_VM_OPERATION权限打开进程句柄。 This, however, is only possible if the application opening the process has the SeDebugPriviledge priviledge enabled. 但是,只有在打开进程的应用程序启用了SeDebugPriviledge特权时才能执行此操作。 I ran the application in elevated mode with administrator privileges, however I don't really know if that has any effect on the SeDebugPriviledge . 我使用管理员权限以提升模式运行应用程序,但是我真的不知道这是否对SeDebugPriviledge有任何影响。

Anyhow, here's the code that I used for this. 无论如何,这是我用于此的代码。 It was compiled with VS2008. 它是用VS2008编译的。

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char cmd[2048];
    int a = 5;
    printf("%p %d\n", &a, a);

    sprintf(cmd, "MemChange.exe %lu %x", GetCurrentProcessId(), &a);
    system(cmd);

    printf("%p %d\n", &a, a);

    return 0;
}

And here's the code for MemChange.exe that this code calls. 这是此代码调用的MemChange.exe代码。

#include <windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    DWORD pId;
    LPVOID pAddr;
    HANDLE pHandle;
    SIZE_T bytesWritten;
    int newValue = 666;

    sscanf(argv[1], "%lu", &pId);
    sscanf(argv[2], "%x", &pAddr);

    pHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pId);
    WriteProcessMemory(pHandle, pAddr, &newValue, sizeof(newValue), &bytesWritten);
    CloseHandle(pHandle);

    fprintf(stderr, "Written %u bytes to process %u.\n", bytesWritten, pId);
    return 0;
}

But please don't use this code. 但请不要使用此代码。 It is horrible, has no error checks and probably leaks like holy hell. 它太可怕了,没有错误检查,可能像圣地狱一样泄漏。 It was created only to illustrate what can be done with WriteProcessMemory . 它的创建只是为了说明WriteProcessMemory可以做些什么。 Hope it helps. 希望能帮助到你。

  1. Why do you think that this is possible - debuggers can only read? 为什么你认为这是可能的 - 调试器只能读取?
  2. If it was possible then all sorts of mayhem could happen! 如果有可能那么各种各样的混乱都可能发生!
  3. Shared memory springs to mind. 记住共享记忆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM