Runtime process memory patching for restoring state

后端 未结 2 1167
伪装坚强ぢ
伪装坚强ぢ 2020-12-11 16:32

I\'m looking for a method for storing the process memory, and restore it later at certain conditions.

...

Actually I\'ve read questions about it... It seems

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 17:22

    ReadProcessMemory is for reading the memory of another process. Inside of a process, it's unnecessary -- you can just dereference a pointer to read memory within the same process.

    To find the blocks of memory in a process, you can use VirtualQuery. Each block will be tagged with a state, type, size, etc. Here's some code I wrote years ago to walk the block list for a specified process (using VirtualQueryEx). You use VirtualQuery pretty much the same way, except that you don't have to specify a process, since it always walks the process in which its running.

    #define WIN32_LEAN_AND_MEAN
    #include 
    #include 
    #include 
    
    unsigned long usage;
    
    void show_modules(HANDLE process) {
    
        unsigned char *p = NULL;
        MEMORY_BASIC_INFORMATION info;
    
        for ( p = NULL;
            VirtualQueryEx(process, p, &info, sizeof(info)) == sizeof(info);
            p += info.RegionSize ) 
        {
            printf("%#10.10x (%6uK)\t", info.BaseAddress, info.RegionSize/1024);
    
            switch (info.State) {
            case MEM_COMMIT:
                printf("Committed");
                break;
            case MEM_RESERVE:
                printf("Reserved");
                break;
            case MEM_FREE:
                printf("Free");
                break;
            }
            printf("\t");
            switch (info.Type) {
            case MEM_IMAGE:
                printf("Code Module");
                break;
            case MEM_MAPPED:
                printf("Mapped     ");
                break;
            case MEM_PRIVATE:
                printf("Private    ");
            }
            printf("\t");
    
            if ((info.State == MEM_COMMIT) && (info.Type == MEM_PRIVATE))
                usage +=info.RegionSize;
    
            int guard = 0, nocache = 0;
    
            if ( info.AllocationProtect & PAGE_NOCACHE)
                nocache = 1;
            if ( info.AllocationProtect & PAGE_GUARD )
                guard = 1;
    
            info.AllocationProtect &= ~(PAGE_GUARD | PAGE_NOCACHE);
    
            switch (info.AllocationProtect) {
            case PAGE_READONLY:
                printf("Read Only");
                break;
            case PAGE_READWRITE:
                printf("Read/Write");
                break;
            case PAGE_WRITECOPY:
                printf("Copy on Write");
                break;
            case PAGE_EXECUTE:
                printf("Execute only");
                break;
            case PAGE_EXECUTE_READ:
                printf("Execute/Read");
                break;
            case PAGE_EXECUTE_READWRITE:
                printf("Execute/Read/Write");
                break;
            case PAGE_EXECUTE_WRITECOPY:
                printf("COW Executable");
                break;
            }
    
            if (guard)
                printf("\tguard page");
            if (nocache)
                printf("\tnon-cachable");
            printf("\n");
        }
    }
    
    int main(int argc, char **argv) {
    
        int pid;
    
        if (argc != 2) {
            fprintf(stderr, "Usage: %s ", argv[0]);
            return 1;
        }
    
        sscanf(argv[1], "%i", &pid);
    
        HANDLE process = OpenProcess( 
            PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, 
            false,
            pid);
    
        show_modules(process);
        printf("Total memory used: %luKB\n", usage/1024);
        return 0;
    }        
    

提交回复
热议问题