在同一进程中从C ++程序运行可执行文件

时间:2013-01-08 14:43:51

标签: c++ process

这可能吗?我想轻松访问可执行文件的内存来编辑它。或者,当我不是管理员时,是否可以从另一个进程编辑可执行文件的内存?我已经尝试过ptrace库,如果我不是管理员,它就会失败。我在Linux上

4 个答案:

答案 0 :(得分:0)

我不完全确定你在问什么,但这可以通过共享内存实现。

见这里:http://www.kernel.org/doc/man-pages/online/pages/man7/shm_overview.7.html

答案 1 :(得分:0)

这是调试器的功能。您可以查看开源调试器的代码,例如gdb,看它是如何工作的。

答案 2 :(得分:0)

答案:

  • 是 - 它有效:您不必是管理员/ root用户,但当然您需要有权访问该流程'记忆,即同一用户。
  • 不 - 这不容易

写入/proc/pid/mem的可能性是在不久前添加到Linux内核的。因此,它取决于您使用的内核。使用内核3.2检查小程序,这是有效的,2.6.32是失败的。

该解决方案包含两个程序:

  1. A'服务器'启动后,分配一些内存,将一些模式写入此内存,并每隔三秒输出打印模式后放置的内存内容。
  2. A'客户'它通过/ proc / pid / maps和/ proc / pid / mem连接到服务器,搜索模式并将其他字符串写入服务器的内存。
  3. 实现使用堆 - 但只要权限允许 - 也可以更改其他进程的其他部分'存储器中。

    这是在C中实现的,因为它是非常低级别的' - 但它应该在C ++中工作。这是一个概念证明 - 没有生产代码 - 例如缺少一些错误检查,它有一些固定大小的缓冲区。

    memholder.c

    /*
     * Alloc memory - write in some pattern and print out the some bytes
     * after the pattern.
     *
     * Compile: gcc -Wall -Werror memholder.c -o memholder.o
     */
    
    #include <sys/types.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    
    int main() {
    
       char * m = (char*) malloc(2048);
       memset(m, '\xAA', 1024);
       strcpy(m + 1024, "Some local data.");
    
       printf("PID: %d\n", getpid());
    
       while(1) {
          printf("%s\n", m + 1024);
          sleep(3);
       }
    
       return 0;
    }
    

    memwriter.c

    /*
     * Searches for a pattern in the given PIDs memory
     * and changes some bytes after them.
     *
     * Compile: gcc -Wall -std=c99 -Werror memwriter.c -o memwriter
     */
    
    #include <sys/types.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/mman.h>
    #include <sys/ptrace.h>
    #include <sys/wait.h>
    
    int open_proc_file(pid_t other_pid, char const * const sn,
       int flags) {
       char fname[1024];
       snprintf(fname, 1023, "/proc/%d/%s", other_pid, sn);
       // Open file for reading and writing
       int const fd = open(fname, flags );
       if(fd==-1) {
          perror("Open file");
          exit(1);
       }
       return fd;
    }
    
    void get_heap(int fd_maps, size_t * heap_start, size_t * heap_end) {
       char buf[65536];
       ssize_t const r = read(fd_maps, buf, 65535);
       if(r==-1) {
          perror("Reading maps file");
          exit(1);
       }
       buf[r] = '\0';
       char * const heap = strstr(buf, "[heap]");
       if(heap==NULL) {
          printf("[heap] not found in maps file");
          exit(1);
       }
    
       // Look backward to the latest newline
       char const * hl_start;
       for(hl_start = heap; hl_start > buf && *hl_start != '\n'; 
           --hl_start) {}
       // skip \n
       ++hl_start;
    
       // Convert to beginnig and end address
       char * lhe;
       *heap_start = strtol(hl_start, &lhe, 16);
       ++lhe;
       *heap_end = strtol(lhe, &lhe, 16);
    }
    
    int main(int argc, char *argv[]) {
    
       if(argc!=2) {
          printf("Usage: memwriter <pid>\n");
          return 1;
       }
    
       pid_t const other_pid = atoi(argv[1]);
       int fd_mem = open_proc_file(other_pid, "mem", O_RDWR);
       int fd_maps = open_proc_file(other_pid, "maps", O_RDONLY);
    
       size_t other_mem_start;
       size_t other_mem_end;
       get_heap(fd_maps, &other_mem_start, &other_mem_end);
    
       ptrace(PTRACE_ATTACH, other_pid, NULL, NULL);
       waitpid(other_pid, NULL, 0);
    
       if( lseek(fd_mem, other_mem_start, SEEK_SET) == -1 ) {
          perror("lseek");
          return 1;
       }
    
       char buf[512];
    
       do {
          ssize_t const r = read(fd_mem, buf, 512);
          if(r!=512) {
             perror("read?");
             break;
          }
    
          // Check for pattern
          int pat_found = 1;
          for(int i = 0; i < 512; ++i) {
             if( buf[i] != '\xAA' ) 
                pat_found = 0;
                break;
          }
          if( ! pat_found ) continue;
    
          // Write about one k of strings
          char const * const wbuf = "REMOTE DATA - ";
          for(int i = 0; i < 70; ++i) {
             ssize_t const w = write(fd_mem, wbuf, strlen(wbuf));
             if( w == -1) {
                perror("Write");
                return 1;
             }
          }
          // Append a \0
          write(fd_mem, "\0", 1);
          break;
    
       } while(1);
    
       ptrace(PTRACE_DETACH, other_pid, NULL, NULL);   
    
       close(fd_mem);
       close(fd_maps);
    
       return 0;
    }
    

    示例输出

    $ ./memholder
    PID: 2621
    Some local data.
    Some local data.
    MOTE DATA - REMOTE DA...
    

    其他解释

    您的问题还有另一种解释(在阅读标题而非问题时),您想要替换“可执行文件”。从一个过程与另一个过程。这可以由exec()(和朋友)轻松处理:

    来自man exec

      

    exec()系列函数用新的过程映像替换当前过程映像。

答案 3 :(得分:-1)

在Windows中,用于此的方法名为ReadProcessMemory / WriteProcessMemory,但是,您需要具有管理权限。对于linux来说也是如此,正如我在评论中所说,没有理智的系统允许用户进程修改非拥有内存。

对于linux,唯一的功能是ptrace。您将需要成为管理员。

http://cboard.cprogramming.com/cplusplus-programming/92093-readprocessmemory-writeprocessmemory-linux-equivalent.html包含更详细的讨论。

你能想象允许进程修改其他进程内存而不是管理员的后果吗?

相关问题