Coronacaptcha

Coronacaptcha

By Michał Szopiński

Goal

The goal of this challenge was to create a script capable of solving a simple graphical captcha. The script was to find the specified icon among a set of five and select it in a form.

Strategy

A simple yet effective heuristic strategy was to count the number of black pixels within each of the icons. This number could be used to programmatically identify each icon with a sufficient accuracy.

Implementation

The following JavaScript script is to be run as soon as the captcha is loaded:

var img = document.getElementsByTagName("img")[0];
var canvas = document.createElement("canvas");
canvas.width = 480;
canvas.height = 96;
var context = canvas.getContext("2d");
context.drawImage(img, 0, 0);

function countBlacks(index) {
  var output = 0;
  var data = context.getImageData(index * 96, 0, 96, 96).data;

  for (var i = 0; i < data.length; i += 4) {
    //smile
    if (data[i] == 255)
      return -1;

    if (data[i + 3] >= 128)
      output++;
  }

  return output;
}

function fillCaptcha() {
  var needle = document.getElementsByTagName("strong")[0].innerText;

  var AREAS = {
    "smile": -1,
    "sneeze": 2597,
    "hospital": 3390,
    "coronavirus": 3648,
    "ambulance": 3678,
    "icu": 3872,
    "pills": 3437,
    "elderly": 2695,
    "nurse": 3718,
    "lungs": 3388,
    "nose": 2848
  }

  var closestIndex = -1;
  var closestIndexDifference = 9216;
  for (var i = 0; i < 5; i++) {
    var diff = Math.abs(countBlacks(i) - AREAS[needle]);

    if (diff < closestIndexDifference) {
      closestIndex = i;
      closestIndexDifference = diff;
    }

    console.log(diff);
  }

  document.querySelectorAll("input[type=radio]")[closestIndex].checked = true;
  document.querySelector("input[type=text]").value = needle;

  document.querySelector("form").submit();
}

fillCaptcha();

The script can be typed in the browser's JavaScript console and reentered by pressing the Arrow Up key.


Revision #5
Created Sun, Mar 22, 2020 10:32 PM by Lachcim
Updated Sun, Mar 22, 2020 10:53 PM by 3na10