SoFunction
Updated on 2024-11-15

selenium+Chrome Sliding CAPTCHA Hack II (a certain website)

See the code for specific details, study the site, enter a random cell phone number and click to get a verification code

In their own code before the reference to a number of blogs, is to intercept all the CAPTCHA images all CAPTCHA images saved locally, and then compare, feel the method does not work, so they wrote a crack method, modify the css through the js to directly capture the complete picture, because the last one wrote the B station, here will not be analyzed one by one, directly on the code.

Crack Success Screen

Full code.

# -*- coding:utf-8 -*-
'''
Research site: /NonRegistrations-Regist
There are two types of slider CAPTCHA.
  1. directly to the gap image, first slide to the gap to find the full CAPTCHA image, compare the CAPTCHA image with the gap, then calculate the coordinates of the gap, and then use selenium to move the button to the specified location
  2. Directly to the original image, the gap needs to be clicked to appear, directly save the original image, and then compare the
'''
from selenium import webdriver
from  import WebDriverWait
from  import expected_conditions as EC
from  import By
from  import ActionChains
from  import etree
from PIL import Image
from time import sleep
import re, requests
from  import urlretrieve
from bs4 import BeautifulSoup
 
class SliderVerificationCode(object):
  def __init__(self): # Initialize some information
     = 60 # Define a starting point on the left # The gap is generally a certain distance from the left side of the picture # There's a slider #
     = '/NonRegistrations-Regist'
     = "C:\Program Files (x86)\Google\Chrome\Application\"
     = (executable_path=)
     = WebDriverWait(, 20) # Set wait time 20 seconds
     = "18516544488"
 
  def input_name_password(self): # Enter cell phone number
    ()
    .maximize_window()
     = (EC.presence_of_element_located((, 'phoneNumberInput')))
    .send_keys()
 
 
  def click_login_button(self): # Click on the login button and the CAPTCHA image will appear.
    login_button = (EC.element_to_be_clickable((, 'getDynamicPwd')))
    login_button.click()
    sleep(1)
 
  def get_geetest_image(self): # Get CAPTCHA image
    # print(.page_source)
    gapimg = (EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_bg')))
    sleep(2)
    (r'./')
    # Modify tag style by js code Show image 2
    js = 'var change = ("geetest_canvas_fullbg");change[0].style = "display:block;"'
    .execute_script(js)
    sleep(2)
    fullimg = (
      EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_fullbg')))
    (r'./')
 
  def is_similar(self, image1, image2, x, y):
    '''Determine if two images have the same pixels in each position.
    #image1: notched image
    :param image2: notched image
    :param x: position x
    :param y: position y
    :return: (x,y) are the same pixel at each position
    '''
    # Get pixel points at specified locations in two images
    pixel1 = ()[x, y]
    pixel2 = ()[x, y]
    # Set a threshold to allow for error #
    threshold = 60
    # Color map Three channels for each pixel point at each position
    if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs(
        pixel1[2] - pixel2[2]) < threshold:
      return True
    else:
      return False
 
  def get_diff_location(self): # Get the start of the gap chart
    captcha1 = ('')
    captcha2 = ('')
    for x in range(, [0]): # Left to right x-direction
      for y in range([1]): # From top to bottom y direction
        if not self.is_similar(captcha1, captcha2, x, y):
          return x # Find the left boundary of the gap in the x-direction #
 
  def get_move_track(self, gap):
    track = [] # Moving trajectory
    current = 0 # Current displacement
    # Deceleration threshold
    mid = gap * 4 / 5 # Accelerate in the first 4/5, decelerate in the last 1/5.
    t = 0.2 # Calculation interval
    v = 0 # Initial velocity
    while current < gap:
      if current < mid:
        a = 3 # Acceleration is +3
      else:
        a = -3 # Acceleration of -3
      v0 = v # Initial velocity v0
      v = v0 + a * t # Current speed
      move = v0 * t + 1 / 2 * a * t * t # Distance traveled
      current += move # Current displacement
      (round(move)) # Join the track
    return track
 
  def move_slider(self, track):
    slider = (EC.presence_of_element_located((By.CSS_SELECTOR, '.geetest_slider_button')))
    ActionChains().click_and_hold(slider).perform()
    for x in track: # There's movement only in the horizontal direction Move on a trajectory #
      ActionChains().move_by_offset(xoffset=x, yoffset=0).perform()
    sleep(1)
    ActionChains().release().perform() # Release the mouse
 
  def main(self):
    self.input_name_password()
    self.click_login_button()
    self.get_geetest_image()
    gap = self.get_diff_location() # Where the gap starts left
    gap = gap - 6 # Subtract the distance in the x-direction that the left side of the slider is from the left side of the picture, and that's how far the slider will actually move.
    track = self.get_move_track(gap)
    print("Moving Tracks", track)
    self.move_slider(track)
 
 
if __name__ == "__main__":
  springAutumn = SliderVerificationCode()
  ()

This is the whole content of this article.