Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 试图从内存中打印出有用的数据_C_Windows_Memory_Hexdump - Fatal编程技术网

C 试图从内存中打印出有用的数据

C 试图从内存中打印出有用的数据,c,windows,memory,hexdump,C,Windows,Memory,Hexdump,这是我的当前代码,我最初使用ReadProcessMemory读取另一个进程的内存。现在我将临时数据存储在tempbuf中。我能够以十六进制形式从tempbuf输出数据。但我计划如图所示显示它,我在这里面临的另一个复杂性是,如果bytes_left>sizeof(tempbuf),我只读取足够的数据,相当于tempbuf的大小。由于我定义的数组只能支持相同数量的数据,如何读取更多数据 存储的数据不能超过分配的空间。如果需要存储更多数据,则需要在某处分配更多空间(更多RAM、磁盘文件等)。压缩将允

这是我的当前代码,我最初使用
ReadProcessMemory
读取另一个进程的内存。现在我将临时数据存储在
tempbuf
中。我能够以十六进制形式从
tempbuf
输出数据。但我计划如图所示显示它,我在这里面临的另一个复杂性是,如果bytes_left>sizeof(tempbuf),我只读取足够的数据,相当于tempbuf的大小。由于我定义的数组只能支持相同数量的数据,如何读取更多数据


存储的数据不能超过分配的空间。如果需要存储更多数据,则需要在某处分配更多空间(更多RAM、磁盘文件等)。压缩将允许您在分配的空间中存储更多的数据,但您不会获得那么多。对于几乎无限的存储,您将需要写入磁盘


或者,如果您只需要显示一次,然后就可以忘记,读入16个字节,显示行,然后将接下来的16个字节读入同一内存。

如果我理解正确,您的主要问题是如何模拟十六进制编辑器的输出

这可以分为三个部分:

  • 打印地址
  • 打印每个字节的十六进制值
  • 打印每个字节的ASCII值
  • 用十六进制打印地址很容易。我们可以使用
    %p
    打印指针的地址,如下所示:

    void update_memblock(MEMBLOCK *mb)
    {
        static unsigned char tempbuf[128 * 1024];
        SIZE_T bytes_left;
        SIZE_T total_read;
        SIZE_T bytes_to_read;
        SIZE_T bytes_read;
    
        bytes_left = mb->size;
        total_read = 0;
    
        while (bytes_left)
        {
            bytes_to_read = (bytes_left > sizeof(tempbuf)) ? 
    sizeof(tempbuf) : bytes_left;
            ReadProcessMemory(mb->hProc, mb->addr + total_read, 
    tempbuf, bytes_to_read, &bytes_read);
            if (bytes_read != bytes_to_read) break;
    
            memcpy(mb->buffer + total_read, tempbuf, bytes_read);
    
            bytes_left -= bytes_read;
            total_read += bytes_read;
        }
        mb->size = total_read;
    }
    
    接下来,您可以使用字符(1字节)上的
    %02x
    打印字节的十六进制值。
    02
    指定零填充字段宽度为2。在这种情况下,只需将
    0
    打印为
    00
    ,即可使事物排列整齐并看起来漂亮

    char* mem = malloc(99);
    printf("%p\n", (void*)mem);
    // output: 0xc17080
    
    最后,打印ASCII是最简单的。。。几乎。我们可以使用
    %c
    为某些ASCII值打印一个字节,但不希望打印
    \n
    \t
    之类的内容。为了解决这个问题,我们可以将
    %c
    的使用限制在的字符/符号区域,并将
    打印到其他所有内容

    printf("%02x", mem[0]);
    // output: 0A
    

    您需要做更多的工作来检查错误,并使一切看起来很好,但希望这能让您对可以做的事情有一个很好的了解。

    “…如十六进制编辑器所示”-请提供更详细的说明。“收集有用的信息”也是含糊不清的;这是什么有用的信息?刚刚添加了一个图像值,我希望能够以UNICODE和ASCII格式显示它,并具有与上述相同的表示形式。还面临如上所述的缓冲区大小问题。请提供。这意味着可以编译一个示例,并显示在调用
    update\u MEMBLOCK
    之前如何声明和填充
    MEMBLOCK*mb
    。也就是说,您的输出问题只需要一次输出16个字节,首先是以空格分隔的2位十六进制值,然后是以ASCII分隔的
    “|”
    。您遇到了什么问题?
    ReadProcessMemory
    并不是那么简单。看起来你用错了,而且它会输出随机字节。显示其余的代码。也读
    char c = mem[0];
    if ( ' ' <= c && c <= '~' ) {
         printf("%c", c);
    }
    else {
         printf(".");
    }
    //output: .
    
    void display_mem(void* mem, int mem_size, int line_len) {
       /*
            mem         - pointer to beggining of memory region to be printed
            mem_size    - number of bytes mem points to
            line_len    - number of bytyes to display per line
       */
    
        unsigned char* data = mem;
        int full_lines = mem_size / line_len;
        unsigned char* addr = mem;
    
        for (int linno = 0; linno < full_lines; linno++) {
            // Print Address
            printf("0x%x\t", addr);
    
            // Print Hex
            for (int i = 0; i < line_len; i++) {
                printf(" %02x", data[linno*line_len + i]);
            }
            printf("\t");
    
            // Print Ascii
            for (int i = 0; i < line_len; i++) {
                char c = data[linno*line_len + i];
                if ( 32 < c && c < 125) {
                    printf(" %c", c);
                }
                else {
                    printf(" .");
                }
            }
            printf("\n");
    
            // Incremement addr by number of bytes printed
            addr += line_len;
        }
    
        // Print any remaining bytes that couldn't make a full line
        int remaining = mem_size % line_len;
        if (remaining > 0) {
            // Print Address
            printf("0x%x\t", addr);
    
            // Print Hex
            for (int i = 0; i < remaining; i++) {
                printf(" %02x", data[line_len*full_lines + i]);
            }
            for (int i = 0; i < line_len - remaining; i++) {
                printf("  ");
            }
            printf("\t");
    
            // Print Hex
            for (int i = 0; i < remaining; i++) {
                char c = data[line_len*full_lines + i];
                if ( 32 < c && c < 125) {
                    printf(" %c", c);
                }
                else {
                    printf(" .");
                }
            }
            printf("\n");
         }
     }
    
    0x1e79010        74 65 73 74 79 2a 6e    t e s t y * n
    0x1e79017        0c 3e 24 45 5e 33 27    . > $ E ^ 3 '
    0x1e7901e        18 51 09 2d 76 7e 4a    . Q . - v . J
    0x1e79025        12 53 0f 6e 0b 1a 6d    . S . n . . m
    0x1e7902c        31 6e 03 2b 01 2f 2c    1 n . + . / ,
    0x1e79033        72 59 1c 76 18 38 3c    r Y . v . 8 <
    0x1e7903a        6e 6b 5b 00 36 64 25    n k [ . 6 d %
    0x1e79041        2d 5c 6f 38 30 00 27    - \ o 8 0 . '
    0x1e79048        33 12 15 5c 01 18 09    3 . . \ . . .
    0x1e7904f        02 40 2d 6c 1a 41 63    . @ - l . A c
    0x1e79056        2b 72 18 1a 5e 74 12    + r . . ^ t .
    0x1e7905d        0d 51 38 33 26 28 6b    . Q 8 3 & ( k
    0x1e79064        56 20 0b 0b 32 20 67    V . . . 2 . g
    0x1e7906b        34 30 68 2e 70 0f 1c    4 0 h . p . .
    0x1e79072        04 50                   . P
    
    while (bytes_left) {
        ReadProcessMemory(mb->hProc, mb->addr + total_read, tempbuf, bytes_to_read, &bytes_read);
        // Get new chunk
        memcpy(mb->buffer + total_read, tempbuf, bytes_read);
    
        // Display chunk
        display_mem(tempbuf, bytes_read);
    
        bytes_left -= bytes_read;
    }