创建远程线程失败。从user32.dll调用函数时访问被拒绝

时间:2018-08-22 04:20:41

标签: c windows winapi

我正在尝试将exe有效负载注入远程进程。 我在远程进程中分配了内存,我使用RVA寻址转换了原始有效负载。我申请了搬迁和进口表格。

当我在Windows 10环境中测试我的解决方案时,它可以正常工作。 Exe被注入并正常运行,并显示“我”消息框。

但是当我尝试在Windows 7 64位上执行相同操作(加载程序,有效负载和目标以0x86模式编译)时,出现错误:

  

访问冲突执行位置0x7698FD1E

我检查了一下,这个地址是来自user32.dll库的MessageBoxA函数的地址

对于所有正在运行的应用程序,user32.dll的MessageBoxA函数的地址应相同,因为kernel32.dll和user32.dll始终具有相同的地址(据我所知)。

在调用MessageBoxA之前,看起来注入的程序正常运行。

这是我将pe注入远程进程的主要代码:

char* target_n = "InjectTarget.exe";
    char* payload_path  = "C:\\Users\\pb\\source\\repos\\pe-dumper\\Debug\\DummyApp.exe";

    FILE* raw_payload = get_file_buffer(payload_path);
    PIMAGE_NT_HEADERS inth = get_nt_headers(raw_payload);

    DWORD kImageSize = inth->OptionalHeader.SizeOfImage;
    DWORD kTargetProcId = get_process_id(target_n);

    HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, NULL, kTargetProcId);
    if (hProcess == NULL) {
        printf("Error: Process handle is NULL\n");
    }

    LPVOID imageBaseRemote = VirtualAllocEx(hProcess, NULL, kImageSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    if (imageBaseRemote == NULL) {
        printf("Error: Image base remote is NULL\n");
    }

    LPVOID imageBaseLocal = VirtualAlloc(NULL, kImageSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    copy_raw_to_image_local(imageBaseLocal, raw_payload);
    adjust_relocations(imageBaseRemote, imageBaseLocal);
    adjust_imports(imageBaseLocal);

    DWORD bytesWritten;
    if (!WriteProcessMemory(hProcess, imageBaseRemote, imageBaseLocal, kImageSize, &bytesWritten)) {
        printf("Cannot write to remote process!\n");
    }

    LPTHREAD_START_ROUTINE routine = ((ULONG_PTR)imageBaseRemote + inth->OptionalHeader.AddressOfEntryPoint);

    DWORD threadId;
    HANDLE hThread = CreateRemoteThread(hProcess, NULL, NULL, routine, NULL, NULL, &threadId);

    if (hThread == NULL) {
        printf("%d", GetLastError());
    }

    VirtualFree(imageBaseLocal, kImageSize, MEM_RELEASE);
    fclose(raw_payload);

为什么Windows 10和Windows 7之间会出现这些差异?

0 个答案:

没有答案