hacks_windows.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package winloader
  2. import (
  3. "errors"
  4. "fmt"
  5. "unsafe"
  6. "golang.org/x/sys/windows"
  7. )
  8. var (
  9. ntdllModule = windows.NewLazySystemDLL("ntdll")
  10. ntdllNtQueryInformationProcess = ntdllModule.NewProc("NtQueryInformationProcess")
  11. )
  12. type _ProcessBasicInformation struct {
  13. Reserved1 uintptr
  14. PebBaseAddress uintptr
  15. Reserved2 [2]uintptr
  16. UniqueProcessID uintptr
  17. Reserved3 uintptr
  18. }
  19. type _PEB struct {
  20. Reserved1 [2]byte
  21. BeingDebugged byte
  22. Reserved2 [1]byte
  23. Reserved3 [2]uintptr
  24. Ldr uintptr
  25. }
  26. type _List struct {
  27. Front uintptr
  28. Back uintptr
  29. }
  30. type _PEBLoaderData struct {
  31. Length uint32
  32. Initialized uint8
  33. Padding [3]uint8
  34. SsHandle uintptr
  35. InLoadOrderModuleList _List
  36. InMemoryOrderModuleList _List
  37. InInitializationOrderModuleList _List
  38. }
  39. // MakePEBEntryForModule is a hack that inserts an entry for the loaded module
  40. // into the PEB loader data to make it appear to Windows functions.
  41. func MakePEBEntryForModule() error {
  42. process := -1
  43. sizeNeeded := uint32(0)
  44. pbi := _ProcessBasicInformation{}
  45. status, _, _ := ntdllNtQueryInformationProcess.Call(
  46. uintptr(process),
  47. 0,
  48. uintptr(unsafe.Pointer(&pbi)),
  49. unsafe.Sizeof(pbi),
  50. uintptr(unsafe.Pointer(&sizeNeeded)),
  51. )
  52. if status != 0 {
  53. return fmt.Errorf("NtQueryInformationProcess failed: %08x", status)
  54. }
  55. // TODO: Implement attempt to insert module entry.
  56. return errors.New("unimplemented")
  57. }
  58. // GetProcessHInstance gets the HINSTANCE for the current process.
  59. func GetProcessHInstance() (uintptr, error) {
  60. var hinstance windows.Handle
  61. windows.GetModuleHandleEx(0, nil, &hinstance)
  62. return uintptr(hinstance), nil
  63. }