SoFunction
Updated on 2024-11-12

Crack sliding captcha process analysis based on python implementation

Preface:

Many partners feedback, in the process of web automation, often get stuck by the login CAPTCHA, do not know how to go through the verification of the CAPTCHA. Today specifically to talk about CAPTCHA, the general situation encountered CAPTCHA we can all find the development to help solve the problem, turn off the CAPTCHA, or give a universal CAPTCHA! So if the development does not provide help, we have no way to deal with these CAPTCHA problems? The answer is of course, common CAPTCHA is generally divided into two categories, one is a graphic CAPTCHA, a slide CAPTCHA!

Today we will mainly talk about how to go about recognizing the sliding captcha to crack it.

Slide Verification Hacking Ideas

The idea about sliding CAPTCHA hack is broadly speaking the following two steps:

1, get the slider sliding distance

2. Simulate dragging the slider and pass the validation.

Sounds relatively simple, but get the slider sliding distance, most partners do not have ideas, do not know how to get. In fact, to get down is not difficult, about this sliding CAPTCHA, slider and notch background are respectively an independent picture, we can put these two pictures, download them with the help of image recognition technology, to identify the notch in the background picture of the location, and then subtract the current location of the slider, you can come up with the need to slide the distance. At this time, many partners will want to image recognition technology I will not ah, will not have nothing to do, the back will give you a good encapsulation of the slider recognition module, as long as you pass into the slider and the gap in the background image of the elements of the node will be able to calculate the slider's gap position.

case study

Without further ado, let's take a look at a case (qq space login), which uses a module slideVerfication that I encapsulated myself to recognize the sliding distance, and if you need it, you can get it by chatting with your buddies. qq space login case implementation steps are as follows:

1, create a driver object, access qq login page

2、Input the account password

3、Click to login

4. Analog sliding validation

Implementation Code

 import time
 from selenium import webdriver
 from slideVerfication import SlideVerificationCode
 ​
 # 1, create a driver object, access qq login page
 browser = ()
 ("/")
 ​
 # 2, enter the account password
 # 2.0 Click to switch to login iframe
 browser.switch_to.frame('login_frame')
 # 2.1 Click Account Password to Login
 browser.find_element_by_id('switcher_plogin').click()
 # 2.2 Locate the account number entry box and enter the account number
 browser.find_element_by_id("u").send_keys("123292678")
 # 2.3 Locate Password Entry Enter the password
 browser.find_element_by_id("p").send_keys("PYTHON01")
 # 3. Click Login
 browser.find_element_by_id('login_button').click()
 (3)
 ​
 # 4. Simulated Slide Verification
 # 4.1 Switching to the sliding captcha iframe
 tcaptcha = browser.find_element_by_id("tcaptcha_iframe")
 browser.switch_to.frame(tcaptcha)
 # 4.2 Getting slide-related elements
 # Select the node on which to drag the slider
 slide_element = browser.find_element_by_id('tcaptcha_drag_thumb')
 # Get the node of the slider image
 slideBlock_ele = browser.find_element_by_id('slideBlock')
 # Get notch background image node
 slideBg = browser.find_element_by_id('slideBg')
 # 4.3 Calculate sliding distance
 sc = SlideVerificationCode(save_image=True)
 distance = sc.get_element_slide_distance(slideBlock_ele,slideBg)
 # Sliding distance error correction, sliding distance * zoom ratio of the image displayed on the web page - initial position of the slider relative to the
 distance = distance*(280/680) - 22
 print("Corrected sliding distance",distance)
 # 4.4. Make a slide
 sc.slide_verification(browser,slide_element,distance=100)

about the sliding verification code recognition problem is solved, then the next to tell you about the encapsulation of the slideVerfication module recognition principle, in fact, about this module image recognition, but also with the help of a third-party image processing module to identify the python has a lot of ready to use to deal with the picture of the library, this side of the I use opencv - python to recognize. slideVerfication module above the two methods used in part of the reference code is as follows. slideVerfication module above the reference code is part of the following. python to recognize. slideVerfication module above the two methods used in part of the reference code is as follows:

def get_element_slide_distance(self, slider_ele, background_ele, correct=0):
     """
     Based on the nodes of the incoming slider, and the background, the distance of the slider is calculated
 
     This method only calculates the distance between the slider and the background image if the slider and the background image are both one complete image.
     If the background image is a background image stitched together by multiple small images, the
     This method is not applicable, please use get_image_slide_distance this method
     :param slider_ele: node of the slider image
     :type slider_ele: WebElement
     :param background_ele: Node for the background image
     :type background_ele:WebElement
     :param correct: slider notch screenshot correction value, default is 0, debugging the screenshot is the case of the correct value will be used
     :type: int
     :return: the x coordinate of the notch position of the background image (the left border of the notch image)
     """
     # Get the image of the CAPTCHA
     slider_url = slider_ele.get_attribute("src")
     background_url = background_ele.get_attribute("src")
     # Download Captcha background image, slide image
     slider = ""
     background = ""
     self.onload_save_img(slider_url, slider)
     self.onload_save_img(background_url, background)
     # Read the colorimetric image, convert it to array type data in numpy.
     slider_pic = (slider, 0)
     background_pic = (background, 0)
     # Get the shape of the notch map array --> width and height of the notch map
     width, height = slider_pic.shape[::-1]
     # Save the processed image as
     slider01 = ""
     background_01 = ""
     (background_01, background_pic)
     (slider01, slider_pic)
     # Read a saved slider map
     slider_pic = (slider01)
     # Perform color conversions
     slider_pic = (slider_pic, cv2.COLOR_BGR2GRAY)
     # Get the absolute value of the color difference
     slider_pic = abs(255 - slider_pic)
     # Save the picture
     (slider01, slider_pic)
     # Read the slider
     slider_pic = (slider01)
     # Read the background image
     background_pic = (background_01)
     # Compare the area of overlap between the two maps
     result = (slider_pic, background_pic, cv2.TM_CCOEFF_NORMED)
     # Get the notch position of the image
     top, left = np.unravel_index((), )
     # Image notch coordinates position in background image
     print("Current slider notch position:", (left, top, left + width, top + height))
     return left
   def slide_verification(self, driver, slide_element, distance):
     """
     Slide the slider to verify

     :param driver: driver object
     :type driver.
     :param slide_element: Tuple of sliders
     :type slider_ele: WebElement
     :param distance: distance of the slide
     :type: int
     :return.
     """
     # Get the url of the page before the slide
     start_url = driver.current_url
     print("The distance to be slid is:", distance)
     # Generate sliding trajectories based on sliding distances
     locus = self.get_slide_locus(distance)
     print("The generated sliding trajectory is:{},The sum of the distances of the trajectories is{}".format(locus, distance))
     # Press the left mouse button
     ActionChains(driver).click_and_hold(slide_element).perform()
     (0.5)
     # Traverse the trajectory to slide
     for loc in locus:
       (0.01)
       ActionChains(driver).move_by_offset(loc, (-5, 5)).perform()
       ActionChains(driver).context_click(slide_element)
     # Release the mouse
     ActionChains(driver).release(on_element=slide_element).perform()

This is the whole content of this article.