From f1bb58e8c2e4b665dbf1540646b4a7bfcc43c482 Mon Sep 17 00:00:00 2001 From: Robert Fisk Date: Tue, 16 Jan 2018 02:04:36 +1300 Subject: [PATCH] Maintain constant acceleration counter for several seconds, on the assumption that a mouse bot would wait until the human stops using the mouse before starting to do anything bad. --- Upstream/Inc/build_config.h | 7 +++-- Upstream/Src/upstream_hid_botdetect.c | 40 +++++++++++++++++---------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/Upstream/Inc/build_config.h b/Upstream/Inc/build_config.h index 99e3caf..889eec5 100644 --- a/Upstream/Inc/build_config.h +++ b/Upstream/Inc/build_config.h @@ -44,8 +44,8 @@ //----------------------------------------------------------- //Adjust these thresholds first to tune mouse bot detection. Lower values = more sensitive #define MOUSE_BOTDETECT_JUMP_VELOCITY_THRESHOLD 20 //Varies by mouse. Most short jumps are <= 10 velocity - #define MOUSE_BOTDETECT_LOCKOUT_CONSTANT_ACCEL_LIMIT 20 //10 is ok for most mice. But some mice (or users!) generate longer sequences. - #define MOUSE_BOTDETECT_LOCKOUT_CONSTANT_ACCEL_CREDIT 60 //Non-constant-acceleration movements can build a credit that will be used before hitting the ACCEL_COUNT limit above. Handy for mice or users that exhibit constant velocity characteristics mid-movement. + #define MOUSE_BOTDETECT_LOCKOUT_CONSTANT_ACCEL_LIMIT 30 //10 is ok for most mice. But some mice (or users!) generate longer sequences. + #define MOUSE_BOTDETECT_LOCKOUT_CONSTANT_ACCEL_CREDIT 80 //Non-constant-acceleration movements can build a credit that will be used before hitting the ACCEL_COUNT limit above. Handy for mice or users that exhibit constant velocity characteristics mid-movement. #define MOUSE_BOTDETECT_LOCKOUT_JIGGLE_BIN_THRESHOLD 4 //----------------------------------------------------------- @@ -53,9 +53,10 @@ #define MOUSE_BOTDETECT_MOVEMENT_STOP_PERIODS 5 //Jump detection stuff - #define MOUSE_BOTDETECT_JUMP_MINIMUM_TIME 4 + #define MOUSE_BOTDETECT_JUMP_MINIMUM_PERIODS 4 //Constant acceleration detection stuff + #define MOUSE_BOTDETECT_VELOCITY_RESET_TIMEOUT_MS 4000 #define MOUSE_BOTDETECT_VELOCITY_HISTORY_SIZE 12 #define MOUSE_BOTDETECT_VELOCITY_MATCH_BASE 256 #define MOUSE_BOTDETECT_VELOCITY_MATCH_ERROR 6 diff --git a/Upstream/Src/upstream_hid_botdetect.c b/Upstream/Src/upstream_hid_botdetect.c index b358c71..684ff9f 100644 --- a/Upstream/Src/upstream_hid_botdetect.c +++ b/Upstream/Src/upstream_hid_botdetect.c @@ -55,10 +55,11 @@ volatile LockoutStateTypeDef LockoutState = LOCKOUT_STATE_INACTIVE; //Variables specific to mouse bot detection: #if defined (CONFIG_MOUSE_ENABLED) && defined (CONFIG_MOUSE_BOT_DETECT_ENABLED) - //Jump detection stuff uint32_t LastMouseMoveTime = 0; - uint32_t LastMouseMoveBeginTime; - uint8_t MouseIsMoving = 0; + + //Jump detection stuff + uint32_t JumpLastMouseMoveBeginTime; + uint8_t JumpMouseIsMoving = 0; //Constant acceleration detection stuff uint16_t MouseVelocityHistory[MOUSE_BOTDETECT_VELOCITY_HISTORY_SIZE] = {0}; @@ -411,20 +412,27 @@ void Upstream_HID_BotDetectMouse(uint8_t* mouseInData) moveDelay = now - LastMouseMoveTime; - //Did the mouse stop moving? - if (moveDelay > ((MOUSE_BOTDETECT_MOVEMENT_STOP_PERIODS * HID_FS_BINTERVAL) - (HID_FS_BINTERVAL / 2))) + //Reset constant acceleration detection state after a few seconds of inactivity + if (moveDelay > MOUSE_BOTDETECT_VELOCITY_RESET_TIMEOUT_MS) { //Is this the start of a new movement? if (velocity != 0) { - //Reset constant acceleration detection state for (i = 0; i < MOUSE_BOTDETECT_VELOCITY_HISTORY_SIZE; i++) { MouseVelocityHistory[i] = 0; } ConstantAccelerationCounter = 0; + } + } + //Did the mouse stop moving briefly? + if (moveDelay > ((MOUSE_BOTDETECT_MOVEMENT_STOP_PERIODS * HID_FS_BINTERVAL) - (HID_FS_BINTERVAL / 2))) + { + //Is this the start of a new movement? + if (velocity != 0) + { //Jiggle detection: add stopped time to jiggle bins moveDelay = moveDelay % (MOUSE_BOTDETECT_JIGGLE_BIN_WIDTH_MS * MOUSE_BOTDETECT_JIGGLE_BIN_COUNT); //Wrap stopped time into the array MouseStopIntervalBinArray[(moveDelay / MOUSE_BOTDETECT_JIGGLE_BIN_WIDTH_MS)]++; @@ -451,14 +459,16 @@ void Upstream_HID_BotDetectMouse(uint8_t* mouseInData) } } - - //Jump detection - if (MouseIsMoving) + //Jump detection: was a movement in progress? + if (JumpMouseIsMoving) { - MouseIsMoving = 0; - if ((LastMouseMoveTime - LastMouseMoveBeginTime) < ((MOUSE_BOTDETECT_JUMP_MINIMUM_TIME * HID_FS_BINTERVAL) - (HID_FS_BINTERVAL / 2))) + JumpMouseIsMoving = 0; + if ((LastMouseMoveTime - JumpLastMouseMoveBeginTime) < ((MOUSE_BOTDETECT_JUMP_MINIMUM_PERIODS * HID_FS_BINTERVAL) - (HID_FS_BINTERVAL / 2))) { - Upstream_HID_BotDetectMouse_DoLockout(); + if (ConstantAccelerationCounter >= 0) //Ignore jumps if ConstantAccelerationCounter is negative -> a human is using the mouse + { + Upstream_HID_BotDetectMouse_DoLockout(); + } } } } @@ -468,10 +478,10 @@ void Upstream_HID_BotDetectMouse(uint8_t* mouseInData) { //Jump detection LastMouseMoveTime = now; - if ((MouseIsMoving == 0) && (velocity > (MOUSE_BOTDETECT_JUMP_VELOCITY_THRESHOLD * MOUSE_BOTDETECT_VELOCITY_MULTIPLIER))) + if ((JumpMouseIsMoving == 0) && (velocity > (MOUSE_BOTDETECT_JUMP_VELOCITY_THRESHOLD * MOUSE_BOTDETECT_VELOCITY_MULTIPLIER))) { - MouseIsMoving = 1; - LastMouseMoveBeginTime = now; + JumpMouseIsMoving = 1; + JumpLastMouseMoveBeginTime = now; } //Constant acceleration detection