Skip to content

Commit c164412

Browse files
Implemented input injection checking in hooks
1 parent b8854e9 commit c164412

File tree

5 files changed

+124
-17
lines changed

5 files changed

+124
-17
lines changed

InputHelper/AssemblyInfo.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ Imports System.Resources
55
<Assembly: AssemblyDescription("A .NET friendly library for simulating mouse and keyboard input.")>
66
<Assembly: AssemblyProduct("InputHelper")>
77
<Assembly: AssemblyCopyright("Copyright © Vincent Bengtsson 2016-2022")>
8-
<Assembly: AssemblyVersion("2.0.0.0")>
8+
<Assembly: AssemblyVersion("2.1.0.0")>
99
<Assembly: NeutralResourcesLanguage("en")>

InputHelper/EventArgs/KeyboardHookEventArgs.vb

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Namespace EventArgs
4545
Inherits System.EventArgs
4646

4747
Private _extended As Boolean
48+
Private _injected As Boolean
49+
Private _injectedAtLowerIL As Boolean
4850
Private _keyCode As Keys
4951
Private _keyState As KeyState
5052
Private _modifiers As ModifierKeys
@@ -67,6 +69,26 @@ Namespace EventArgs
6769
End Get
6870
End Property
6971

72+
''' <summary>
73+
''' Gets a boolean value indicating whether the keystroke was injected into the input stream by a process rather than an input device.
74+
''' </summary>
75+
''' <remarks>Always False for <see cref="LocalKeyboardHook"/>.</remarks>
76+
Public ReadOnly Property Injected As Boolean
77+
Get
78+
Return _injected
79+
End Get
80+
End Property
81+
82+
''' <summary>
83+
''' Gets a boolean value indicating whether the keystroke was injected into the input stream by another process running at lower integrity level.
84+
''' </summary>
85+
''' <remarks>Always False for <see cref="LocalKeyboardHook"/>.</remarks>
86+
Public ReadOnly Property InjectedAtLowerIL As Boolean
87+
Get
88+
Return _injectedAtLowerIL
89+
End Get
90+
End Property
91+
7092
''' <summary>
7193
''' Gets the keyboard code of the key that generated the keystroke.
7294
''' </summary>
@@ -108,8 +130,8 @@ Namespace EventArgs
108130
End Property
109131

110132
Public Overrides Function ToString() As String
111-
Return String.Format("{{KeyCode: {0}, ScanCode: {1}, Extended: {2}, KeyState: {3}, Modifiers: {4}}}", _
112-
Me.KeyCode, Me.ScanCode, Me.Extended, Me.KeyState, Me.Modifiers)
133+
Return String.Format("{{KeyCode: {0}, ScanCode: {1}, Extended: {2}, KeyState: {3}, Modifiers: {4}, Injected: {5}, InjectedAtLowerIL: {6}}}", _
134+
Me.KeyCode, Me.ScanCode, Me.Extended, Me.KeyState, Me.Modifiers, Me.Injected, Me.InjectedAtLowerIL)
113135
End Function
114136

115137
''' <summary>
@@ -133,5 +155,33 @@ Namespace EventArgs
133155
Me._keyState = KeyState
134156
Me._modifiers = Modifiers
135157
End Sub
158+
159+
''' <summary>
160+
''' Initializes a new instance of the KeyboardHookEventArgs class.
161+
''' </summary>
162+
''' <param name="KeyCode">The keyboard code of the key that generated the keystroke.</param>
163+
''' <param name="ScanCode">The hardware scan code of the key that generated the keystroke.</param>
164+
''' <param name="Extended">Whether the keystroke message originated from one of the additional keys on the enhanced keyboard
165+
''' (see: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646267(v=vs.85).aspx#_win32_Keystroke_Message_Flags). </param>
166+
''' <param name="KeyState">The current state of the key that generated the keystroke.</param>
167+
''' <param name="Modifiers">The modifier keys that was pressed in combination with the keystroke.</param>
168+
''' <param name="Injected">Whether the keystroke was injected into the input stream by a process rather than an input device.</param>
169+
''' <param name="InjectedAtLowerIL">Whether the keystroke was injected into the input stream by another process running at lower integrity level.</param>
170+
''' <remarks></remarks>
171+
Public Sub New(ByVal KeyCode As Keys, _
172+
ByVal ScanCode As UInteger, _
173+
ByVal Extended As Boolean, _
174+
ByVal KeyState As KeyState, _
175+
ByVal Modifiers As ModifierKeys, _
176+
ByVal Injected As Boolean, _
177+
ByVal InjectedAtLowerIL As Boolean)
178+
Me._keyCode = KeyCode
179+
Me._scanCode = ScanCode
180+
Me._extended = Extended
181+
Me._keyState = KeyState
182+
Me._modifiers = Modifiers
183+
Me._injected = Injected
184+
Me._injectedAtLowerIL = InjectedAtLowerIL
185+
End Sub
136186
End Class
137187
End Namespace

InputHelper/EventArgs/MouseEventArgs.vb

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Namespace EventArgs
4949
Private _buttonState As KeyState
5050
Private _delta As Integer
5151
Private _doubleClick As Boolean
52+
Private _injected As Boolean
53+
Private _injectedAtLowerIL As Boolean
5254
Private _location As Point
5355
Private _scrollDirection As ScrollDirection
5456

@@ -98,6 +100,26 @@ Namespace EventArgs
98100
End Get
99101
End Property
100102

103+
''' <summary>
104+
''' Gets a boolean value indicating whether the mouse event was injected into the input stream by a process rather than an input device.
105+
''' </summary>
106+
''' <remarks></remarks>
107+
Public ReadOnly Property Injected As Boolean
108+
Get
109+
Return _injected
110+
End Get
111+
End Property
112+
113+
''' <summary>
114+
''' Gets a boolean value indicating whether the mouse event was injected into the input stream by another process running at lower integrity level.
115+
''' </summary>
116+
''' <remarks></remarks>
117+
Public ReadOnly Property InjectedAtLowerIL As Boolean
118+
Get
119+
Return _injectedAtLowerIL
120+
End Get
121+
End Property
122+
101123
''' <summary>
102124
''' Gets the location of the mouse (in screen coordinates).
103125
''' </summary>
@@ -119,8 +141,8 @@ Namespace EventArgs
119141
End Property
120142

121143
Public Overrides Function ToString() As String
122-
Return String.Format("{{Button: {0}, State: {1}, DoubleClick: {2}, Location: {3}, Scroll: {4}, Delta: {5}}}", _
123-
Me.Button, Me.ButtonState, Me.DoubleClick, Me.Location, Me.ScrollDirection, Me.Delta)
144+
Return String.Format("{{Button: {0}, State: {1}, DoubleClick: {2}, Location: {3}, Scroll: {4}, Delta: {5}, Injected: {6}, InjectedAtLowerIL: {7}}}", _
145+
Me.Button, Me.ButtonState, Me.DoubleClick, Me.Location, Me.ScrollDirection, Me.Delta, Me.Injected, Me.InjectedAtLowerIL)
124146
End Function
125147

126148
''' <summary>
@@ -146,5 +168,35 @@ Namespace EventArgs
146168
Me._scrollDirection = ScrollDirection
147169
Me._delta = Delta
148170
End Sub
171+
172+
''' <summary>
173+
''' Initializes a new instance of the MouseHookEventArgs class.
174+
''' </summary>
175+
''' <param name="Button">Which mouse button was pressed or released.</param>
176+
''' <param name="ButtonState">The current state of the button that generated the mouse event.</param>
177+
''' <param name="DoubleClick">Whether the event was caused by a double click.</param>
178+
''' <param name="Location">The location of the mouse (in screen coordinates).</param>
179+
''' <param name="ScrollDirection">Which direction the mouse wheel was scrolled in.</param>
180+
''' <param name="Delta">A signed count of the number of detents the mouse wheel has rotated.</param>
181+
''' <param name="Injected">Whether the mouse event was injected into the input stream by a process rather than an input device.</param>
182+
''' <param name="InjectedAtLowerIL">Whether the mouse event was injected into the input stream by another process running at lower integrity level.</param>
183+
''' <remarks></remarks>
184+
Public Sub New(ByVal Button As MouseButtons, _
185+
ByVal ButtonState As KeyState, _
186+
ByVal DoubleClick As Boolean, _
187+
ByVal Location As Point, _
188+
ByVal ScrollDirection As ScrollDirection, _
189+
ByVal Delta As Integer, _
190+
ByVal Injected As Boolean, _
191+
ByVal InjectedAtLowerIL As Boolean)
192+
Me._button = Button
193+
Me._buttonState = ButtonState
194+
Me._doubleClick = DoubleClick
195+
Me._location = Location
196+
Me._scrollDirection = ScrollDirection
197+
Me._delta = Delta
198+
Me._injected = Injected
199+
Me._injectedAtLowerIL = InjectedAtLowerIL
200+
End Sub
149201
End Class
150202
End Namespace

InputHelper/Hooks/KeyboardHook.vb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ Namespace Hooks
117117
Dim ScanCode As UInteger = KeystrokeInfo.scanCode
118118
Dim Extended As Boolean = (KeystrokeInfo.flags And NativeMethods.LowLevelKeyboardHookFlags.LLKHF_EXTENDED) = NativeMethods.LowLevelKeyboardHookFlags.LLKHF_EXTENDED
119119
Dim AltDown As Boolean = (KeystrokeInfo.flags And NativeMethods.LowLevelKeyboardHookFlags.LLKHF_ALTDOWN) = NativeMethods.LowLevelKeyboardHookFlags.LLKHF_ALTDOWN
120+
Dim Injected As Boolean = (KeystrokeInfo.flags And NativeMethods.LowLevelKeyboardHookFlags.LLKHF_INJECTED) = NativeMethods.LowLevelKeyboardHookFlags.LLKHF_INJECTED
121+
Dim InjectedAtLowerIL As Boolean = (KeystrokeInfo.flags And NativeMethods.LowLevelKeyboardHookFlags.LLKHF_LOWER_IL_INJECTED) = NativeMethods.LowLevelKeyboardHookFlags.LLKHF_LOWER_IL_INJECTED
120122

121123
If AltDown = True _
122124
AndAlso Internal.IsModifier(KeyCode, ModifierKeys.Alt) = False _
@@ -125,7 +127,7 @@ Namespace Hooks
125127
Me.Modifiers = Me.Modifiers Or ModifierKeys.Alt
126128
End If
127129

128-
Return New KeyboardHookEventArgs(KeyCode, ScanCode, Extended, If(KeyDown, KeyState.Down, KeyState.Up), Me.Modifiers)
130+
Return New KeyboardHookEventArgs(KeyCode, ScanCode, Extended, If(KeyDown, KeyState.Down, KeyState.Up), Me.Modifiers, Injected, InjectedAtLowerIL)
129131
End Function
130132

131133
''' <summary>

InputHelper/Hooks/MouseHook.vb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,23 @@ Namespace Hooks
103103
Dim MouseEventInfo As NativeMethods.MSLLHOOKSTRUCT = _
104104
CType(Marshal.PtrToStructure(lParam, GetType(NativeMethods.MSLLHOOKSTRUCT)), NativeMethods.MSLLHOOKSTRUCT)
105105

106+
Dim Injected As Boolean = (MouseEventInfo.flags And NativeMethods.LowLevelMouseHookFlags.LLMHF_INJECTED) = NativeMethods.LowLevelMouseHookFlags.LLMHF_INJECTED
107+
Dim InjectedAtLowerIL As Boolean = (MouseEventInfo.flags And NativeMethods.LowLevelMouseHookFlags.LLMHF_LOWER_IL_INJECTED) = NativeMethods.LowLevelMouseHookFlags.LLMHF_LOWER_IL_INJECTED
108+
106109
Select Case wParam
107110
Case NativeMethods.MouseMessage.WM_LBUTTONDOWN
108111
Dim DoubleClick As Boolean = (Environment.TickCount - LeftClickTimeStamp) <= NativeMethods.GetDoubleClickTime()
109112
LeftClickTimeStamp = Environment.TickCount
110113

111114
Dim HookEventArgs As New MouseHookEventArgs(MouseButtons.Left, KeyState.Down, DoubleClick, _
112-
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0)
115+
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0, Injected, InjectedAtLowerIL)
113116
RaiseEvent MouseDown(Me, HookEventArgs)
114117
Block = HookEventArgs.Block
115118

116119

117120
Case NativeMethods.MouseMessage.WM_LBUTTONUP
118121
Dim HookEventArgs As New MouseHookEventArgs(MouseButtons.Left, KeyState.Up, False, _
119-
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0)
122+
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0, Injected, InjectedAtLowerIL)
120123
RaiseEvent MouseUp(Me, HookEventArgs)
121124
Block = HookEventArgs.Block
122125

@@ -126,14 +129,14 @@ Namespace Hooks
126129
MiddleClickTimeStamp = Environment.TickCount
127130

128131
Dim HookEventArgs As New MouseHookEventArgs(MouseButtons.Middle, KeyState.Down, DoubleClick, _
129-
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0)
132+
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0, Injected, InjectedAtLowerIL)
130133
RaiseEvent MouseDown(Me, HookEventArgs)
131134
Block = HookEventArgs.Block
132135

133136

134137
Case NativeMethods.MouseMessage.WM_MBUTTONUP
135138
Dim HookEventArgs As New MouseHookEventArgs(MouseButtons.Middle, KeyState.Up, False, _
136-
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0)
139+
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0, Injected, InjectedAtLowerIL)
137140
RaiseEvent MouseUp(Me, HookEventArgs)
138141
Block = HookEventArgs.Block
139142

@@ -143,14 +146,14 @@ Namespace Hooks
143146
RightClickTimeStamp = Environment.TickCount
144147

145148
Dim HookEventArgs As New MouseHookEventArgs(MouseButtons.Right, KeyState.Down, DoubleClick, _
146-
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0)
149+
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0, Injected, InjectedAtLowerIL)
147150
RaiseEvent MouseDown(Me, HookEventArgs)
148151
Block = HookEventArgs.Block
149152

150153

151154
Case NativeMethods.MouseMessage.WM_RBUTTONUP
152155
Dim HookEventArgs As New MouseHookEventArgs(MouseButtons.Right, KeyState.Up, False, _
153-
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0)
156+
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0, Injected, InjectedAtLowerIL)
154157
RaiseEvent MouseUp(Me, HookEventArgs)
155158
Block = HookEventArgs.Block
156159

@@ -163,38 +166,38 @@ Namespace Hooks
163166
Else X1ClickTimeStamp = Environment.TickCount
164167

165168
Dim HookEventArgs As New MouseHookEventArgs(If(IsXButton2, MouseButtons.XButton2, MouseButtons.XButton1), KeyState.Down, DoubleClick, _
166-
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0)
169+
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0, Injected, InjectedAtLowerIL)
167170
RaiseEvent MouseDown(Me, HookEventArgs)
168171
Block = HookEventArgs.Block
169172

170173

171174
Case NativeMethods.MouseMessage.WM_XBUTTONUP
172175
Dim IsXButton2 As Boolean = (New NativeMethods.DWORD(MouseEventInfo.mouseData).High = 2)
173176
Dim HookEventArgs As New MouseHookEventArgs(If(IsXButton2, MouseButtons.XButton2, MouseButtons.XButton1), KeyState.Up, False, _
174-
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0)
177+
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0, Injected, InjectedAtLowerIL)
175178
RaiseEvent MouseUp(Me, HookEventArgs)
176179
Block = HookEventArgs.Block
177180

178181

179182
Case NativeMethods.MouseMessage.WM_MOUSEWHEEL
180183
Dim Delta As Integer = New NativeMethods.DWORD(MouseEventInfo.mouseData).SignedHigh
181184
Dim HookEventArgs As New MouseHookEventArgs(MouseButtons.None, KeyState.Up, False, _
182-
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.Vertical, Delta)
185+
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.Vertical, Delta, Injected, InjectedAtLowerIL)
183186
RaiseEvent MouseWheel(Me, HookEventArgs)
184187
Block = HookEventArgs.Block
185188

186189

187190
Case NativeMethods.MouseMessage.WM_MOUSEHWHEEL
188191
Dim Delta As Integer = New NativeMethods.DWORD(MouseEventInfo.mouseData).SignedHigh
189192
Dim HookEventArgs As New MouseHookEventArgs(MouseButtons.None, KeyState.Up, False, _
190-
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.Horizontal, Delta)
193+
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.Horizontal, Delta, Injected, InjectedAtLowerIL)
191194
RaiseEvent MouseWheel(Me, HookEventArgs)
192195
Block = HookEventArgs.Block
193196

194197

195198
Case NativeMethods.MouseMessage.WM_MOUSEMOVE
196199
Dim HookEventArgs As New MouseHookEventArgs(MouseButtons.None, KeyState.Up, False, _
197-
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0)
200+
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.None, 0, Injected, InjectedAtLowerIL)
198201
RaiseEvent MouseMove(Me, HookEventArgs)
199202
Block = HookEventArgs.Block
200203

0 commit comments

Comments
 (0)