mkwinsyscall
Drop-in replacement fork of mkwinsyscall, a tool that generates windows system call bodies based on their prototypes.
The official documentation can be found there: https://pkg.go.dev/golang.org/x/sys/windows/mkwinsyscall
Why this fork ?
I made this fork to include my custom resolution library named PElib. It allows you to resolve module handles (GetModuleHandle) and function (or proc) addresses (GetProcAddress) manually, without using the Windows API. It includes the API hashing technique (using sdbm hashing algo) to hide which functions are used.
Usage
This is a drop-in replacement of mkwinsyscall, so you just need to replace the URL from the go generate
command.
For example:
//go:generate go run golang.org/x/sys/windows/mkwinsyscall -output winsyscalls_windows.go definitions.go
//sys RtlCopyMemory(dest uintptr, src uintptr, dwSize uint32) = ntdll.RtlCopyMemory
//sys HeapAlloc(hHeap windows.Handle, dwFlags uint32, dwBytes uintptr) (lpMem uintptr, err error) = kernel32.HeapAlloc
Become:
//go:generate go run github.com/atsika/mkwinsyscall -output winsyscalls_windows.go definitions.go
//sys RtlCopyMemory(dest uintptr, src uintptr, dwSize uint32) = ntdll.RtlCopyMemory
//sys HeapAlloc(hHeap windows.Handle, dwFlags uint32, dwBytes uintptr) (lpMem uintptr, err error) = kernel32.HeapAlloc
Output
The below example shows the output of the functions resolved using PElib by hash.
var (
modkernel32 = pelib.NewDLL(uint32(0x8f7ee672)) // kernel32.dll
modntdll = pelib.NewDLL(uint32(0xd22e2014)) // ntdll.dll
procHeapAlloc = pelib.NewProc(modkernel32, uint32(0xa4695109)) // HeapAlloc
procRtlCopyMemory = pelib.NewProc(modntdll, uint32(0xbe91a4e0)) // RtlCopyMemory
)
func HeapAlloc(hHeap windows.Handle, dwFlags uint32, dwBytes uintptr) (lpMem uintptr, err error) {
r0, _, e1 := syscall.SyscallN(procHeapAlloc.Addr(), uintptr(hHeap), uintptr(dwFlags), uintptr(dwBytes))
lpMem = uintptr(r0)
if lpMem == 0 {
err = errnoErr(e1)
}
return
}
func RtlCopyMemory(dest uintptr, src uintptr, dwSize uint32) {
syscall.SyscallN(procRtlCopyMemory.Addr(), uintptr(dest), uintptr(src), uintptr(dwSize))
return
}
Limitations
⚠️ This fork doesn't make the call toLoadLibrary
that is done under-the-hood byNewDLL
because of the API hashing technique. This means that if a DLL is not loaded, the functionpelib.NewDLL
will fail and returnnil
. The same will happen forpelib.NewProc
.To be sure and avoid your program crashing, check if the returned value is different from
nil
before using it.