DUMP进程内存保存文件

代码

#include <Windows.h>
#include <TlHelp32.h>
#include <stdio.h>

// Dump a process' memory to a file
void dumpProcessMemory(DWORD pid, const char* filename) {
    HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
    if (hProcess == NULL) {
        printf("Failed to open process %d\n", pid);
        return;
    }

    // Get the process information
    PROCESSENTRY32 pe = { 0 };
    pe.dwSize = sizeof(pe);
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (Process32First(hSnapshot, &pe)) {
        do {
            if (pe.th32ProcessID == pid) {
                break;
            }
        } while (Process32Next(hSnapshot, &pe));
    }
    CloseHandle(hSnapshot);

    // Create the output file
    FILE* fp = fopen(filename, "wb");
    if (fp == NULL) {
        printf("Failed to create file %s\n", filename);
        CloseHandle(hProcess);
        return;
    }

    // Loop through all memory regions in the process and write them to the file
    MEMORY_BASIC_INFORMATION mbi;
    SIZE_T bytesRead;
    for (LPVOID address = NULL; VirtualQueryEx(hProcess, address, &mbi, sizeof(mbi)) == sizeof(mbi); address = (LPBYTE)mbi.BaseAddress + mbi.RegionSize) {
        if (mbi.State != MEM_FREE) {
            // Allocate a buffer for this memory region and read the data from the process
            LPVOID buffer = VirtualAlloc(NULL, mbi.RegionSize, MEM_COMMIT, PAGE_READWRITE);
            ReadProcessMemory(hProcess, mbi.BaseAddress, buffer, mbi.RegionSize, &bytesRead);

            // Write the memory contents to the file
            fwrite(buffer, 1, mbi.RegionSize, fp);

            // Free the buffer
            VirtualFree(buffer, 0, MEM_RELEASE);
        }
    }

    fclose(fp);
    CloseHandle(hProcess);

    printf("Dumped process %s (pid=%d) memory to file %s\n", pe.szExeFile, pid, filename);
}

int main(int argc, char** argv) {
    if (argc != 2) {
        printf("Usage: dumpmem <pid>\n");
        return 1;
    }

    DWORD pid = atoi(argv[1]);
    dumpProcessMemory(pid, "memory.bin");

    return 0;
}

效果

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Core Dump文件是在程序崩溃时生成的文件,其中包含了程序运行时的内存、堆栈指针、寄存器状态和内存管理信息等。\[1\]当一个进程异常终止时,可以选择将进程的用户空间内存数据全部保存在磁盘上,文件名通常为core。\[2\]Core Dump文件可以通过sysctl/proc来设置文件名和文件路径。它通常以ELF格式保存,可以用于分析程序崩溃的原因。\[1\]通过使用gdb调试工具,可以检查Core Dump文件以查明错误的原因,这被称为事后调试。\[2\]默认情况下,系统不允许生成Core Dump文件,因为它可能包含用户密码等敏感信息。\[2\]如果需要生成Core Dump文件,可以通过设置core_pattern文件来指定生成的文件格式和存储路径。\[3\] #### 引用[.reference_title] - *1* *2* [Core Dump文件](https://blog.csdn.net/qq_37954088/article/details/79765491)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [linux中coredump文件分析](https://blog.csdn.net/weixin_44698673/article/details/126271862)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值