-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
223 lines (182 loc) · 7.09 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"fmt"
"log"
"strings"
"syscall"
"unicode/utf16"
"unsafe"
"github.com/nanitefactory/gominhook"
"github.com/nanitefactory/winmb"
"github.com/zetamatta/go-outputdebug"
)
// ----------------------------------------------------------------------------
/*
#include <Windows.h>
// Due to lack of my knowledge in reversing I literally have no idea what the return type of these functions would be though.
// Arguments could be guessed; 64-bit integers because they always pass in R8, RDX, RCX in order.
// Gateway functions in C.
DWORD64 OnDisplayUpdate(DWORD64, DWORD64, DWORD64);
DWORD64 OnNumberUpdate(DWORD64, DWORD64, DWORD64);
*/
import "C"
var isInMiddleOfOnDisplayUpdate bool
var fpDisplayUpdate *func(arg1, arg2, arg3 uintptr) (ret uintptr)
var fpNumberUpdate *func(arg1, arg2, arg3 uintptr) (ret uintptr)
//export OnDisplayUpdate
func OnDisplayUpdate(arg1, arg2, arg3 uintptr) (ret uintptr) {
isInMiddleOfOnDisplayUpdate = true
ret, _, _ = syscall.Syscall6(uintptr(unsafe.Pointer(fpDisplayUpdate)), 3, arg1, arg2, arg3, 0, 0, 0)
return
}
//export OnNumberUpdate
func OnNumberUpdate(arg1, arg2, arg3 uintptr) (ret uintptr) {
// See if it's hooked well. // arg3 points to the stack where our string is stored.
outputdebug.String("OnNumberUpdate(): " + fmt.Sprintf("Arguments passed: 0x%X 0x%X 0x%X", arg1, arg2, arg3))
// If this OnNumberUpdate() is not called from OnDisplayUpdate(), don't do nothing and fallthrough.
if !isInMiddleOfOnDisplayUpdate {
ret, _, _ = syscall.Syscall6(uintptr(unsafe.Pointer(fpNumberUpdate)), 3, arg1, arg2, arg3, 0, 0, 0)
return
}
isInMiddleOfOnDisplayUpdate = false
// Convert our UTF16 string (WSTR) to which our arg3 points to a plain Go string.
strArg3 := lpwstrToString((C.LPCWSTR)(unsafe.Pointer(arg3)))
outputdebug.String("original text: " + strArg3)
// Make some changes to this copied string.
strArg3 = strings.Replace(strArg3, "0", "空", -1)
strArg3 = strings.Replace(strArg3, "1", "一", -1)
strArg3 = strings.Replace(strArg3, "2", "二", -1)
strArg3 = strings.Replace(strArg3, "3", "三", -1)
strArg3 = strings.Replace(strArg3, "4", "四", -1)
strArg3 = strings.Replace(strArg3, "5", "五", -1)
strArg3 = strings.Replace(strArg3, "6", "六", -1)
strArg3 = strings.Replace(strArg3, "7", "七", -1)
strArg3 = strings.Replace(strArg3, "8", "八", -1)
strArg3 = strings.Replace(strArg3, "9", "九", -1)
// strArg3 = "야옹, 멍멍, 귀여워! <" + strArg3 + "> by 코코넛 xD 냠냠"
outputdebug.String("modified text: " + strArg3)
// Get another copy of that modified string with a ptr to it.
newArg3 := syscall.StringToUTF16Ptr(strArg3)
sizeNewArg3 := len(syscall.StringToUTF16(strArg3)) * 2 // size as byte array
outputdebug.String("arg3: " + fmt.Sprintf("0x%X -> 0x%X -> 0x%X", arg3, strArg3, newArg3))
// ----------------------------------------------------------------------------
// Copy this new string to where arg3 points to.
// Going to read & write memory with kernel32 API.
kernel32 := syscall.NewLazyDLL("kernel32.dll")
hProcess, _, _ := syscall.Syscall(
kernel32.NewProc("GetCurrentProcess").Addr(),
0, 0, 0, 0,
)
var oldProtect C.DWORD
ret, _, err := syscall.Syscall6(
kernel32.NewProc("VirtualProtectEx").Addr(),
5, hProcess, arg3, uintptr(sizeNewArg3), C.PAGE_EXECUTE_READWRITE, uintptr(unsafe.Pointer(&oldProtect)), 0,
)
outputdebug.String(fmt.Sprint(ret, err)) // ret: return code. err: return detail.
ret, _, err = syscall.Syscall6(
kernel32.NewProc("WriteProcessMemory").Addr(),
4, hProcess, arg3, uintptr(unsafe.Pointer(newArg3)), uintptr(sizeNewArg3), 0, 0,
)
outputdebug.String(fmt.Sprint(ret, err)) // ret: return code. err: return detail.
// ----------------------------------------------------------------------------
// Call the original function.
ret, _, _ = syscall.Syscall6(uintptr(unsafe.Pointer(fpNumberUpdate)), 3, arg1, arg2, arg3, 0, 0, 0)
return
}
func lpwstrToString(cwstr C.LPCWSTR) string {
const maxRunes = 1<<30 - 1
ptr := unsafe.Pointer(cwstr)
sz := C.wcslen((*C.wchar_t)(ptr))
wstr := (*[maxRunes]uint16)(ptr)[:sz:sz]
return string(utf16.Decode(wstr))
}
// ----------------------------------------------------------------------------
// GetProcessModuleHandle returns the base address of .exe module.
//export GetProcessModuleHandle
func GetProcessModuleHandle() (hModule uintptr) {
hModule, _, _ = syscall.Syscall( // (HANDLE)GetModuleHandle(NULL);
syscall.NewLazyDLL("kernel32.dll").NewProc("GetModuleHandleW").Addr(),
1, 0, 0, 0,
)
outputdebug.String(fmt.Sprintf("GetProcessModuleHandle(): 0x%X", hModule))
return
}
// GetHookPoint in our process.
//export GetHookPoint
func GetHookPoint(offset uintptr) (hookPoint uintptr) {
base := GetProcessModuleHandle()
hookPoint = base + offset
outputdebug.String(fmt.Sprintf("GetHookPoint(): 0x%X = 0x%X + 0x%X", hookPoint, base, offset))
return
}
// OnProcessAttach is an async callback (hook).
//export OnProcessAttach
func OnProcessAttach(
hinstDLL unsafe.Pointer, // handle to DLL module
fdwReason uint32, // reason for calling function
lpReserved unsafe.Pointer, // reserved
) {
// Initialize minhook
err := gominhook.Initialize()
if err != nil {
outputdebug.String(err.Error())
log.Fatalln(err)
}
// Clean-up minhook
defer func() {
// Unhook
err := gominhook.DisableHook(gominhook.AllHooks)
if err != nil {
outputdebug.String(err.Error())
log.Println(err)
}
// Uninitialize
err = gominhook.Uninitialize()
if err != nil {
outputdebug.String(err.Error())
log.Println(err)
}
}()
// ----------------------------------------------------------------------------
// Create a hook for OnDisplayUpdate(). // Calculator.exe+2BBC8 - 48 89 5C 24 08 - mov [rsp+08],rbx
err = gominhook.CreateHook(GetHookPoint(0x2BBC8), uintptr(C.OnDisplayUpdate), uintptr(unsafe.Pointer(&fpDisplayUpdate)))
if err != nil {
outputdebug.String(err.Error())
log.Fatalln(err)
}
// Create a hook for OnNumberUpdate(). // Calculator.exe+2BC94 - 48 89 5C 24 08 - mov [rsp+08],rbx
err = gominhook.CreateHook(GetHookPoint(0x2BC94), uintptr(C.OnNumberUpdate), uintptr(unsafe.Pointer(&fpNumberUpdate)))
if err != nil {
outputdebug.String(err.Error())
log.Fatalln(err)
}
// Enable the hook.
err = gominhook.EnableHook(gominhook.AllHooks)
if err != nil {
outputdebug.String(err.Error())
log.Fatalln(err)
}
// ----------------------------------------------------------------------------
// Block this routine.
<-ch
outputdebug.String("OnProcessAttach(): Exit")
}
var ch = make(chan int)
// Unhook everything. This will restore the target process to its original state.
//export Unhook
func Unhook() {
ch <- 1
}
// ----------------------------------------------------------------------------
//export MessageBoxTest
func MessageBoxTest() {
winmb.MessageBoxPlain("export Test", "export Test")
}
//export Test
func Test() {
}
const title = "TITLE"
var version = "undefined"
func main() {
// nothing really. xD
}