只是想尝试反射 dll 加载,所以我写了一个简单的消息框:
package main import "c" import ( "unsafe" "syscall" ) //export onprocessattach func onprocessattach() { const ( null = 0 mb_ok = 0 ) caption := "hola" title := "desdegoo" ret, _, _ := syscall.newlazydll("user32.dll").newproc("messageboxw").call( uintptr(null), uintptr(unsafe.pointer(syscall.stringtoutf16ptr(caption))), uintptr(unsafe.pointer(syscall.stringtoutf16ptr(title))), uintptr(mb_ok)) if ret != 1 { return } return } func main() {}
我使用以下命令生成了一个 dll(只是一个带有 cgo/golang 的消息框) go build --buildmode=c-shared main.go
当使用 loadlibrary() 加载 dll 并运行导出函数 onprocessattach 时,它可以工作(弹出消息框),但是当尝试实现 dll 反射加载时,通过解析重定位和解析 iat,它就不起作用。 似乎执行基本重定位并将 iat 设置为 .rdata 上的空部分,用于初始化 go 运行时(在 nt 标头的入口点中初始化) 这是我用来解决导入问题的代码片段:
// resolve base relocations IMAGE_DATA_DIRECTORY relocations = ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; DWORD_PTR relocationTable = relocations.VirtualAddress + (DWORD_PTR)dllBase; DWORD relocationsProcessed = 0; while (relocationsProcessed < relocations.Size) { PBASE_RELOCATION_BLOCK relocationBlock = (PBASE_RELOCATION_BLOCK)(relocationTable + relocationsProcessed); relocationsProcessed += sizeof(BASE_RELOCATION_BLOCK); DWORD relocationsCount = (relocationBlock->BlockSize - sizeof(BASE_RELOCATION_BLOCK)) / sizeof(BASE_RELOCATION_ENTRY); PBASE_RELOCATION_ENTRY relocationEntries = (PBASE_RELOCATION_ENTRY)(relocationTable + relocationsProcessed); for (DWORD i = 0; i < relocationsCount; i++) { relocationsProcessed += sizeof(BASE_RELOCATION_ENTRY); if (relocationEntries[i].Type == 0) { continue; } DWORD_PTR relocationRVA = relocationBlock->PageAddress + relocationEntries[i].Offset; DWORD_PTR addressToPatch = 0; ReadProcessMemory(GetCurrentProcess(), (LPCVOID)((DWORD_PTR)dllBase, relocationRVA), &addressToPatch, sizeof(DWORD_PTR), NULL); addressToPatch += deltaImageBase; memcpy((PVOID)((DWORD_PTR)dllBase + relocationRVA), &addressToPatch, sizeof(DWORD_PTR)); } } // resolve IAT PIMAGE_IMPORT_DESCRIPTOR importDescriptor = NULL; IMAGE_DATA_DIRECTORY importsDirectory = ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; importDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)(importsDirectory.VirtualAddress + (DWORD_PTR)dllBase); LPCSTR libraryName = ""; HMODULE library = NULL; while (importDescriptor->Name != NULL) { libraryName = (LPCSTR)importDescriptor->Name + (DWORD_PTR)dllBase; library = LoadLibraryA(libraryName); if (library) { PIMAGE_THUNK_DATA thunk = NULL; thunk = (PIMAGE_THUNK_DATA)((DWORD_PTR)dllBase + importDescriptor->FirstThunk); while (thunk->u1.AddressOfData != NULL) { if (IMAGE_SNAP_BY_ORDINAL(thunk->u1.Ordinal)) { LPCSTR functionOrdinal = (LPCSTR)IMAGE_ORDINAL(thunk->u1.Ordinal); thunk->u1.Function = (DWORD_PTR)GetProcAddress(library, functionOrdinal); } else { PIMAGE_IMPORT_BY_NAME functionName = (PIMAGE_IMPORT_BY_NAME)((DWORD_PTR)dllBase + thunk->u1.AddressOfData); DWORD_PTR functionAddress = (DWORD_PTR)GetProcAddress(library, functionName->Name); thunk->u1.Function = functionAddress; } ++thunk; } } importDescriptor++; }
这样做之后,我解决了 eat 寻找 onprocessattach 函数的问题,直接运行它显然不起作用,因为 go 运行时未初始化,但尝试初始化它会导致程序崩溃,因为如上所述。它会给出 exception_access_violation,因为尝试读取无效的字节块。
入口点的反汇编: mov rax, qdword ptr ds:[地址] mov dword ptr ds:[rax]
按照转储中的地址,它看起来为空 00 00 00 00 00 00 00 00 00 [..]
虽然原始dll确实有值 90 2b c5 ea 01 [...]
我知道我将 .rdata 上的这些字节设置为 null,但无法弄清楚为什么在执行重定位时会发生这种情况,也许 go 运行时不适合我想要做的事情?还是别的什么?
解决后我只是忘了在这里发布解决方案 错误出现在以下行
ReadProcessMemory(GetCurrentProcess(), (LPCVOID)((DWORD_PTR)dllBase, relocationRVA), &addressToPatch, sizeof(DWORD_PTR), NULL);
这只是一个手指错误,应该是添加符号而不是逗号,这就是它使地址无效的原因。
以上是反射加载 CGO 生成的 DLL的详细内容。更多信息请关注PHP中文网其他相关文章!