Trust me, I’m not a bot!

Avishek Paul
2 min readSep 13, 2024

--

The other day, my manager told me that the application we work on is going to implement an authentication mechanism that uses more scary than CAPTCHA, mouse behavior detection.
So the application would track the mouse movement to determine if a user is a human or a bot. Now our Selenium script would not be able to bypass it because the mouse movements are instant and in a straight line.
I did a bit of digging around to find an algorithm that uses some crazy high school physics: WindMouse, an algorithm for generating human-like mouse motion | ben.land

The WindMouse algorithm is designed to simulate human-like mouse movements. It was developed to make automated mouse movements appear more natural and less detectable by anti-bot systems. The algorithm models the cursor as an object influenced by forces such as gravity and wind, creating smooth and realistic paths.

How WindMouse Works

  1. Gravity: This force constantly pulls the cursor towards the target destination.
  2. Wind: This introduces random variations in the cursor’s path, mimicking the small, unpredictable movements of a human hand.

These forces are combined to create a movement that is both direct and slightly erratic, much like how a human would move a mouse.

Java Implementation

Here’s a basic implementation of the WindMouse algorithm in Java:

import java.awt.Point;
import java.util.Random;

public class WindMouse {
private static final Random random = new Random();

public static void windMouse(int startX, int startY, int endX, int endY) {
double wind = 0.1;
double gravity = 9;
double minWait = 10;
double maxWait = 15;
double maxStep = 10;
double targetArea = 10;

double sqrt2 = Math.sqrt(2);
double sqrt3 = Math.sqrt(3);
double sqrt5 = Math.sqrt(5);

double currentX = startX;
double currentY = startY;

while (Math.hypot(currentX - endX, currentY - endY) > 1) {
double randomDist = Math.min(maxStep, Math.hypot(currentX - endX, currentY - endY));
double step = randomDist / 2.0;

double windX = wind * (random.nextDouble() * 2 - 1);
double windY = wind * (random.nextDouble() * 2 - 1);

double velocityX = (endX - currentX) / randomDist;
double velocityY = (endY - currentY) / randomDist;

currentX += velocityX * step + windX;
currentY += velocityY * step + windY;

try {
Thread.sleep((long) (minWait + random.nextDouble() * (maxWait - minWait)));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
windMouse(0, 0, 100, 100);
}
}

Explanation

  • Gravity: Pulls the cursor towards the target.
  • Wind: Adds randomness to the movement.
  • Velocity: Determines the direction and speed of the cursor.

This code simulates a human-like mouse movement from the starting point (startX, startY) to the destination (endX, endY).

Feel free to tweak the parameters like wind, gravity, minWait, maxWait, and maxStep to better suit your needs. If you have any questions or need further customization, let me know!

--

--

Avishek Paul
Avishek Paul

Written by Avishek Paul

0 Followers

I write about anything and everything that baffles me!

No responses yet