From 418bba1706408bb00711c61f9629d4cc09739ca0 Mon Sep 17 00:00:00 2001 From: Robert Fisk Date: Wed, 17 Jan 2018 18:36:39 +1300 Subject: [PATCH] Constant acceleration detector now blocks mouse movement well before causing a full lockout --- Upstream/Inc/build_config.h | 20 +++++++++----------- Upstream/Src/upstream_hid_botdetect.c | 24 +++++++++++++++++------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/Upstream/Inc/build_config.h b/Upstream/Inc/build_config.h index 889eec5..c753591 100644 --- a/Upstream/Inc/build_config.h +++ b/Upstream/Inc/build_config.h @@ -41,30 +41,28 @@ //Configure mouse bot detection here: #ifdef CONFIG_MOUSE_BOT_DETECT_ENABLED - //----------------------------------------------------------- - //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 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 - //----------------------------------------------------------- - #define MOUSE_BOTDETECT_VELOCITY_MULTIPLIER 10 - #define MOUSE_BOTDETECT_MOVEMENT_STOP_PERIODS 5 //Jump detection stuff - #define MOUSE_BOTDETECT_JUMP_MINIMUM_PERIODS 4 + #define MOUSE_BOTDETECT_JUMP_VELOCITY_THRESHOLD 20 //Varies by mouse. Most short jumps are <= 10 velocity + #define MOUSE_BOTDETECT_JUMP_PERIODS 4 //Constant acceleration detection stuff - #define MOUSE_BOTDETECT_VELOCITY_RESET_TIMEOUT_MS 4000 + #define MOUSE_BOTDETECT_CONSTANT_ACCEL_LOCKOUT 100 //Lock when constant acceleration counter reaches this number + #define MOUSE_BOTDETECT_CONSTANT_ACCEL_STOP 10 //Block mouse movements when counter is above this value + #define MOUSE_BOTDETECT_CONSTANT_ACCEL_CREDIT 100 //Non-constant-acceleration movements can build a credit that will be used before hitting the limits above. Handy for mice or users that exhibit constant velocity characteristics mid-movement. + #define MOUSE_BOTDETECT_VELOCITY_RESET_TIMEOUT_MS 3000 //Reset constant acceleration counter when mouse stops for this time + #define MOUSE_BOTDETECT_VELOCITY_HISTORY_SIZE 12 #define MOUSE_BOTDETECT_VELOCITY_MATCH_BASE 256 #define MOUSE_BOTDETECT_VELOCITY_MATCH_ERROR 6 //Jiggle detection stuff + #define MOUSE_BOTDETECT_JIGGLE_STOP_PERIODS 10 #define MOUSE_BOTDETECT_JIGGLE_BIN_WIDTH_MS 20 //20ms per bin #define MOUSE_BOTDETECT_JIGGLE_BIN_COUNT 50 //50 bins at 20ms = 1 sec coverage, wrapped #define MOUSE_BOTDETECT_JIGGLE_BIN_DIVIDER 4 + #define MOUSE_BOTDETECT_LOCKOUT_JIGGLE_BIN_THRESHOLD 4 #endif diff --git a/Upstream/Src/upstream_hid_botdetect.c b/Upstream/Src/upstream_hid_botdetect.c index 684ff9f..f723a9a 100644 --- a/Upstream/Src/upstream_hid_botdetect.c +++ b/Upstream/Src/upstream_hid_botdetect.c @@ -64,7 +64,7 @@ volatile LockoutStateTypeDef LockoutState = LOCKOUT_STATE_INACTIVE; //Constant acceleration detection stuff uint16_t MouseVelocityHistory[MOUSE_BOTDETECT_VELOCITY_HISTORY_SIZE] = {0}; int32_t PreviousSmoothedAcceleration = 0; - int8_t ConstantAccelerationCounter = 0; + int32_t ConstantAccelerationCounter = 0; //Jiggle detection stuff uint8_t MouseStopIntervalBinDrainDivideCount = 0; @@ -427,8 +427,8 @@ void Upstream_HID_BotDetectMouse(uint8_t* mouseInData) } - //Did the mouse stop moving briefly? - if (moveDelay > ((MOUSE_BOTDETECT_MOVEMENT_STOP_PERIODS * HID_FS_BINTERVAL) - (HID_FS_BINTERVAL / 2))) + //Jiggle detection: did the mouse stop moving? + if (moveDelay > ((MOUSE_BOTDETECT_JIGGLE_STOP_PERIODS * HID_FS_BINTERVAL) - (HID_FS_BINTERVAL / 2))) { //Is this the start of a new movement? if (velocity != 0) @@ -458,12 +458,17 @@ void Upstream_HID_BotDetectMouse(uint8_t* mouseInData) } } } + } + - //Jump detection: was a movement in progress? + //Jump detection: did the mouse stop moving briefly? + if (moveDelay > ((MOUSE_BOTDETECT_JUMP_PERIODS * HID_FS_BINTERVAL) - (HID_FS_BINTERVAL / 2))) + { + //Was a movement in progress? if (JumpMouseIsMoving) { JumpMouseIsMoving = 0; - if ((LastMouseMoveTime - JumpLastMouseMoveBeginTime) < ((MOUSE_BOTDETECT_JUMP_MINIMUM_PERIODS * HID_FS_BINTERVAL) - (HID_FS_BINTERVAL / 2))) + if ((LastMouseMoveTime - JumpLastMouseMoveBeginTime) < ((MOUSE_BOTDETECT_JUMP_PERIODS * HID_FS_BINTERVAL) - (HID_FS_BINTERVAL / 2))) { if (ConstantAccelerationCounter >= 0) //Ignore jumps if ConstantAccelerationCounter is negative -> a human is using the mouse { @@ -513,14 +518,19 @@ void Upstream_HID_BotDetectMouse(uint8_t* mouseInData) ((PreviousSmoothedAcceleration - smoothedAccelerationMatchError) <= newSmoothedAcceleration)) { ConstantAccelerationCounter++; - if (ConstantAccelerationCounter > MOUSE_BOTDETECT_LOCKOUT_CONSTANT_ACCEL_LIMIT) + if (ConstantAccelerationCounter > MOUSE_BOTDETECT_CONSTANT_ACCEL_LOCKOUT) { Upstream_HID_BotDetectMouse_DoLockout(); } + else if (ConstantAccelerationCounter >= MOUSE_BOTDETECT_CONSTANT_ACCEL_STOP) //Stop mouse movement if it looks suspiciously constant + { + mouseInData[1] = 0; + mouseInData[2] = 0; + } } else { - if (ConstantAccelerationCounter > -MOUSE_BOTDETECT_LOCKOUT_CONSTANT_ACCEL_CREDIT) ConstantAccelerationCounter--; + if (ConstantAccelerationCounter > -MOUSE_BOTDETECT_CONSTANT_ACCEL_CREDIT) ConstantAccelerationCounter--; } PreviousSmoothedAcceleration = newSmoothedAcceleration;