-
Notifications
You must be signed in to change notification settings - Fork 11
/
hinako_test.go
69 lines (61 loc) · 1.66 KB
/
hinako_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package hinako
import (
"runtime"
"syscall"
"testing"
"unsafe"
)
func TestHookArch386(t *testing.T) {
if runtime.GOARCH != "386" {
t.Skip()
}
if err := testHook(&Arch386{}); err != nil {
t.Fatal(err)
}
}
func TestHookArchAMD64(t *testing.T) {
if runtime.GOARCH != "amd64" {
t.Skip()
}
if err := testHook(&ArchAMD64{}); err != nil {
t.Fatal(err)
}
}
func testHook(arch Arch) error {
// Before hook
// Call MessageBoxW
target := syscall.NewLazyDLL("user32.dll").NewProc("MessageBoxW")
printDisas(arch, target.Addr(), int(maxTrampolineSize(arch)), "original messageboxw")
target.Call(0, wstrPtr("MessageBoxW"), wstrPtr("MessageBoxW"), 0)
// API Hooking by hinako
if err := func() error {
var originalMessageBoxW *syscall.Proc
hook, err := NewHookByName(arch, "user32.dll", "MessageBoxW", func(hWnd syscall.Handle, lpText, lpCaption *uint16, uType uint) int {
printDisas(arch, originalMessageBoxW.Addr(), int(maxTrampolineSize(arch)), "original messageboxw (tramp)")
r, _, _ := originalMessageBoxW.Call(uintptr(hWnd), wstrPtr("Hooked!"), wstrPtr("Hooked!"), uintptr(uType))
return int(r)
})
if err != nil {
return err
}
defer hook.Close()
originalMessageBoxW = hook.OriginalProc
// After hook
// Call MessageBoxW
if r, _, err := target.Call(0, wstrPtr("MessageBoxW"), wstrPtr("MessageBoxW"), 0); r == 0 {
return err
}
return nil
}(); err != nil {
return err
}
// after close hook
if r, _, err := target.Call(0, wstrPtr("MessageBoxW"), wstrPtr("MessageBoxW"), 0); r == 0 {
return err
}
return nil
}
func wstrPtr(str string) uintptr {
ptr, _ := syscall.UTF16PtrFromString(str)
return uintptr(unsafe.Pointer(ptr))
}