如果其他进程通过代码更改了光标位置,并且不是由用户来移动,我认为这不是警告,除非某些WinAPI通过代码移动它具有可以注册的事件。
我测试了如果代码更改了光标位置,则不会在WinForm上或使用全局钩子引发MouseMove。
因此,使用GetLastInputInfo无效,并且似乎无法检测到进程是否更改了移动位置。
也就是说,您可以在此MouseMove全局挂钩中使用计时器。
https://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
很重但是可以用。
如果用户在一个过程中同时移动鼠标,则只会得到移动的用户。
使用WinForms计时器
public partial class FormTest : Form
{
Point PreviousMousePosition;
bool UserMovedMouse;
public FormTest()
{
InitializeComponent();
PreviousMousePosition = Cursor.Position;
TimerCheckMouseMove.Start();
TimerMoveMouse.Start();
HookManager.MouseMove += HookManager_MouseMove;
}
private void HookManager_MouseMove(object sender,MouseEventArgs e)
{
PreviousMousePosition = Cursor.Position;
UserMovedMouse = true;
}
private void TimerCheckMouseMove_Tick(object sender,EventArgs e)
{
TimerCheckMouseMove.Enabled = false;
try
{
var pos = Cursor.Position;
if ( !UserMovedMouse && pos != PreviousMousePosition)
MessageBox.Show("Mouse moved by a process");
PreviousMousePosition = Cursor.Position;
UserMovedMouse = false;
}
finally
{
TimerCheckMouseMove.Enabled = true;
}
}
private void TimerMoveMouse_Tick(object sender,EventArgs e)
{
Cursor.Position = new Point(100,100);
}
}
使用线程计时器
System.Threading.Timer TimerCheckMouseMove;
TimerCheckMouseMove = new System.Threading.Timer(TimerCheckMouseMove_Tick,null,100);
private bool TimerCheckMouseMoveMutex;
private void TimerCheckMouseMove_Tick(object state)
{
if ( TimerCheckMouseMoveMutex ) return;
TimerCheckMouseMoveMutex = true;
try
{
var pos = Cursor.Position;
if ( !UserMovedMouse && pos != PreviousMousePosition)
MessageBox.Show("Mouse moved by a process");
PreviousMousePosition = Cursor.Position;
UserMovedMouse = false;
}
finally
{
TimerCheckMouseMoveMutex = false;
}
}
,
可以使用API调用SendInput()
或mouse_event()
检测鼠标输入是由代码生成的。为此,请使用SetWindowsHookEx()
来设置low-level mouse hook。这个钩子过程传递了一个MSLLHOOKSTRUCT,其flags
成员指示鼠标事件是代码注入的结果还是用户输入的结果。
万一攻击者向您的应用程序发送了鼠标消息,则可以从鼠标消息处理程序中调用InSendMessage。真正的鼠标消息将发布到消息队列中。通常会发送假鼠标消息。如果InSendMessage
返回TRUE
,则可以确保获得了虚假的输入消息。如果攻击者确实发布了消息,则必须依靠识别实际输入设备状态与预期状态之间的不匹配。 GetKeyState可用于此目的。
无法立即检测到对SetCursorPos
的呼叫。孤立地,这些调用不会生成用户输入,也不能用于自动执行应用程序。它们总是伴随着其他方案以注入输入,并且您知道如何识别这些输入。
本文链接:https://www.f2er.com/3133887.html