远程执行时,短LdrLoadDll存根崩溃

时间:2018-06-08 20:16:26

标签: windows multithreading crash stub

所以我编写了一个小程序,将一些shellcode复制到一个调用LdrLoadDll的指定进程中(就像一个存根)。问题是,它只有在我指定要使用的程序与我编写的程序相同时才有效。如果我选择任何其他程序,该程序将崩溃。可能是我的功能原型吗?

这是我的代码:

#include <Windows.h>
#pragma comment(lib, "ntdll.lib")

typedef struct _LSA_UNICODE_STRING {
    USHORT Length;
    USHORT MaximumLength;
    PWSTR Buffer;
} LSA_UNICODE_STRING, *PLSA_UNICODE_STRING, UNICODE_STRING, *PUNICODE_STRING;
using f_LdrLoadDll = NTSTATUS(NTAPI*)(IN PWCHAR PathToFile OPTIONAL, IN ULONG Flags OPTIONAL, IN PUNICODE_STRING ModuleFileName, OUT PHANDLE ModuleHandle);


typedef NTSTATUS(NTAPI *pdef_LdrLoadDll)(IN PWCHAR PathToFile OPTIONAL, IN ULONG Flags OPTIONAL, IN PUNICODE_STRING ModuleFileName, OUT PHANDLE ModuleHandle);
EXTERN_C NTSYSAPI VOID WINAPI RtlInitUnicodeString(PUNICODE_STRING, PCWSTR);
struct LOADER_STUB_INFO
{
    pdef_LdrLoadDll LdrLoadDllDef;
    UNICODE_STRING filename;
    f_LdrLoadDll LdrLoadDll = LdrLoadDllDef;
};
void __stdcall ldrstub(LOADER_STUB_INFO * ldrInfo);
int main()
{                                 //only works with GetCurrentProcessId();
    HANDLE proc = OpenProcess(GENERIC_ALL, 0, GetCurrentProcessId()); 
    LOADER_STUB_INFO loaderInfo;
    LPVOID ldrFuncAddr = GetProcAddress(GetModuleHandle("ntdll.dll"), "LdrLoadDll");
    pdef_LdrLoadDll LdrLoadDll = (pdef_LdrLoadDll)ldrFuncAddr;
    loaderInfo.LdrLoadDll = LdrLoadDll;
    UNICODE_STRING file;
    RtlInitUnicodeString(&file, L"C:\\Users\\Arush\\Desktop\\test.dll");
    loaderInfo.filename = file;
    LPVOID structAddr = VirtualAllocEx(proc, nullptr, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    WriteProcessMemory(proc, structAddr, &loaderInfo, sizeof(loaderInfo), nullptr);
    LPVOID codeAddr = VirtualAllocEx(proc, nullptr, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    WriteProcessMemory(proc, codeAddr, ldrstub, 0x1000, nullptr);
    CreateRemoteThread(proc, nullptr, 0, (LPTHREAD_START_ROUTINE)codeAddr, reinterpret_cast<LOADER_STUB_INFO*>(structAddr), 0, nullptr);
    system("pause");
    return 0;
}

void __stdcall ldrstub(LOADER_STUB_INFO * ldrInfo)
{
    auto _LdrLoadDll = ldrInfo->LdrLoadDll;
    HANDLE handee;
    _LdrLoadDll(nullptr, 0, &ldrInfo->filename, &handee);
}

1 个答案:

答案 0 :(得分:1)

包含指针UNICODE_STRING filename;的{​​{1}}内的

LOADER_STUB_INFO。您将此指针初始化为Buffer并按原样复制到远程进程。但在远程过程中,L"C:\\Users\\Arush\\Desktop\\test.dll"当然无效。你需要分配和复制你想加载到远程进程的dll名称,而不是从本地进程指向它的那个