PythonシミュレーションでB局にログインし、スライド検証コードを解読します.

8651 ワード

1.認証コードノード
B局の検証コードはマウスのサスペンションスライダが現れる限り、検証コードが現れた後にノードを位置決めすればよい.プロセスは煩雑で、直接貼り付けます.
img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'gt_box')))

2.座標値の取得
取得した座標値はそれぞれ左上隅と右下隅であり、フロントエンドページの座標原点は画面左上隅であり、要素ノードは一般的に相対位置であるため、座標値部分はよく理解する必要がある.例えば、B局のログインインタフェースには「ログイン」を含むdivノードがあり、その親ノードはid=「login-app」のdivであり、図のように:
3.切り欠きオフセット
画像の各座標点を巡回して対応する画素点のRGBを2枚取得し、RGBの差が閾値範囲内であれば同一とし、次の画素点を比較し続ける.しきい値を超えると、ピクセルポイントが異なり、現在の位置が切欠き位置であることを示します.
'''
    Python?Python     :984632579      ,          ,      !
'''
    def get_gap(self, image1, image2):
        """
               
        :param image1:        
        :param image2:       
        :return: None
        """
        left = 60
        #                         ,            
        for i in range(left, image1.size[0]):
            for j in range(image1.size[1]):
                if not self.is_pixel_equal(image1, image2, i, j):
                    left = i
                    return left
        return left

    def is_pixel_equal(self, image1, image2, x, y):
        """
                
        :param image1:     
        :param image2:     
        :param x:   X
        :param y:   Y
        :return:       
        """
        #          
        pixel1 = image1.load()[x, y]
        pixel2 = image2.load()[x, y]
        #   60
        threshold = 60
        #   RGB          60,         ,    
        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

4.アナログドラッグ
シミュレーションドラッグスライダは、崔大が人間の行動軌跡をシミュレートした「前段均一加速後段均一減速」を継承します.
5.点を押してスライダを押して認証コードを呼び出す
スライダを押すと、2~3秒後に認証コードが自動的に非表示になるので、遅延を追加せずに直接取得します.
完全なコード
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from utils.config import *
from time import sleep
from PIL import Image
from io import BytesIO
'''
    Python?Python     :984632579      ,          ,      !
'''
class Crack_bb(object):
    def __init__(self):
        """
           
        """
        self.url = URL
        self.browser = webdriver.Chrome()
        self.wait = WebDriverWait(self.browser, 15)
        #   、  
        self.email = EMAIL
        self.password = PASSWORD

    def __del__(self):
        """
        gc       
        """
        self.browser.close()

    def open(self):
        """
          B            
        :return: None
        """
        #   B     
        self.browser.get(self.url)
        #   
        username = self.wait.until(EC.presence_of_element_located((By.ID, 'login-username')))
        #   
        passwd = self.wait.until(EC.presence_of_element_located((By.ID, 'login-passwd')))
        #     
        username.clear()
        username.send_keys(self.email)
        sleep(3)
        #     
        passwd.clear()
        passwd.send_keys(self.password)
        sleep(3)

    def get_slider(self):
        """
            
        :return:     
        """
        slider = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[contains(@class,"gt_slider_knob")]')))
        return slider

    def get_code_position(self):
        """
               
        :return:        
        """
        # B               
        img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'gt_box')))
        sleep(1)
        #               
        location = img.location
        #       
        size = img.size
        # img   
        top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size['width']
        #             
        return [left, top, right, bottom]

    def get_geetest_image(self, name='demo.png'):
        """
               
        :return:     
        """
        #      
        left, top, right, bottom = self.get_code_position()
        print('     :({}, {}), ({}, {})'.format(left, top, right, bottom))
        #       
        screenshot = self.get_screenshot()
        #        
        captcha = screenshot.crop((left, top, right, bottom))
        #   
        captcha.save(name)
        return captcha

    def get_screenshot(self):
        """
              
        :return:     
        """
        screenshot = self.browser.get_screenshot_as_png()
        screenshot = Image.open(BytesIO(screenshot))
        return screenshot

    def get_gap(self, image1, image2):
        """
               
        :param image1:        
        :param image2:       
        :return: None
        """
        left = 60
        #                         ,            
        for i in range(left, image1.size[0]):
            for j in range(image1.size[1]):
                if not self.is_pixel_equal(image1, image2, i, j):
                    left = i
                    return left
        return left

    def is_pixel_equal(self, image1, image2, x, y):
        """
                
        :param image1:     
        :param image2:     
        :param x:   X
        :param y:   Y
        :return:       
        """
        #          
        pixel1 = image1.load()[x, y]
        pixel2 = image2.load()[x, y]
        #   60
        threshold = 60
        #   RGB          60,         ,    
        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_track(self, distance):
        """
                   
        :param distance:    
        :return:     
        """
        #     
        track = []
        #     
        current = 0
        #     
        mid = distance * 4 / 5
        #     
        t = 0.2
        #    
        v = 0

        while current < distance:
            if current < mid:
                #      2
                a = 2
            else:
                #      3
                a = -3
            #    v0
            v0 = v
            #     v = v0 + at
            v = v0 + a * t
            #     x = v0t + 1/2 * a * t^2
            move = v0 * t + 1 / 2 * a * t * t
            #     
            current += move
            #     
            track.append(round(move))
        return track

    def move_to_gap(self, slider, tracks):
        """
                 
        :param slider:   
        :param tracks:   
        :return: None
        """
        #         
        ActionChains(self.browser).click_and_hold(slider).perform()
        #     
        for x in tracks:
            ActionChains(self.browser).move_by_offset(xoffset=x, yoffset=0).perform()
        sleep(0.5)
        #     
        ActionChains(self.browser).release().perform()

    def crack_login(self):
        """
          
        :return: None
        """
        #   B     ,       
        self.open()
        #       (        )
        slider = self.get_slider()
        ActionChains(self.browser).move_to_element(slider).perform()
        #             
        image1 = self.get_geetest_image('captcha1.png')
        #       
        slider.click()
        sleep(2)
        #            
        image2 = self.get_geetest_image('captcha2.png')
        #       
        gap = self.get_gap(image1, image2)
        print('    :', gap)
        #       
        gap -= BORDER
        #       
        track = self.get_track(gap)
        print('    :', track)
        #     
        self.move_to_gap(slider, track)
        #   5 ,                
        sleep(5)

        success = self.wait.until(
            EC.text_to_be_present_in_element((By.CLASS_NAME, 't'), '    '))
        print(success)

        #     
        if not success:
            self.crack_login()

if __name__ == '__main__':
    crack = Crack_bb()
    crack.crack_login()

通常の解読率は50%以上であるべきで,主にサーバが境界(画素差)をどのように判定するかにかかっている.