极验4代滑块(js逆向)

import requests
from requests.exceptions import RequestException
import json, re
import time
import execjs
import hashlib
import ddddocr

class jyHuaKuai:
    def __init__(self):
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0",
            "Referer": "https://www.geetest.com/"
        }
        self.session = requests.Session()

    def get_load(self):
        codes = ""
        with open("huakuai_js.txt", "r", encoding="utf-8") as f:
            codes = f.read()
        run = execjs.compile(codes)
        challenge = run.call("uuid")
        params = {
            "callback": "geetest_" + str(int(time.time())*1000),
            "captcha_id": "24f56dc13c40dc4a02fd0318567caef5",
            "challenge": challenge,
            "client_type": "web",
            "risk_type":"slide",
            "lang":"zh"
        }
        url = "https://gcaptcha4.geetest.com/load"
        try:
            res = self.session.get(url=url, params=params, headers=self.headers)
        except requests.exceptions.RequestException as e:
            print("请求错误",e)
        else:
            pattern = re.compile(r"\((.*)\)")
            data = json.loads(pattern.findall(res.text)[0])["data"]
            captcha_id = "24f56dc13c40dc4a02fd0318567caef5"
            return data["lot_number"], data["bg"],data["payload"], data["process_token"], data["slice"],data["pow_detail"]["datetime"],captcha_id

    def get_md5(self,pow_msg):
        md5 = hashlib.md5(pow_msg.encode("utf-8")).hexdigest()
        return md5

    def get_x(self,bg_url, slice_url):
        try:
            bg_img = self.session.get(url=bg_url, headers=self.headers).content
            slice_img = self.session.get(url=slice_url, headers=self.headers).content
            ocr = ddddocr.DdddOcr(det=False, ocr=False,show_ad=False)
            result = ocr.slide_match(slice_img, bg_img, simple_target=True)

        except requests.exceptions.RequestException as e:
            print("图片请求错误", e)
        else:
            return result.get("target")[0]


    def do_verify(self):  # slice是滑块,bg是背景图
        lot_number, bg, payload, process_token, slices, day_time, captcha_id = self.get_load()
        base_picurl = "https://static.geetest.com/"
        setLeft = self.get_x(base_picurl+bg, base_picurl + slices)
        codes = ""
        with open("huakuai_js.txt", "r", encoding="utf-8") as f:
            codes = f.read()
        run = execjs.compile(codes)
        pow_msg = run.call("get_md5",day_time, captcha_id,lot_number)
        pow_sign = self.get_md5(pow_msg)
        result = run.call("get_w",setLeft,lot_number, pow_msg, pow_sign)
        login_params = {
            "callback": "geetest_"+str(int(time.time()*1000)),
            "captcha_id": captcha_id,
            "client_type": "web",
            "lot_number":lot_number,
            "risk_type":"slide",
            "payload":payload,
            "process_token": process_token,
            "payload_protocol":1,
            "pt":1,
            "w": result
        }
        login_url = "https://gcaptcha4.geetest.com/verify"
        try:
            self.headers["Host"] = "gcaptcha4.geetest.com"
            self.headers["Connection"] = "keep-alive"
            self.headers["Cookie"] = "sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2218d9c8c39489e7-02453ffb814c9fe-26001851-921600-18d9c8c3949b02%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fcn.bing.com%2F%22%2C%22%24latest_landing_page%22%3A%22https%3A%2F%2Fwww.geetest.com%2F%22%7D%2C%22%24device_id%22%3A%2218d9c8c39489e7-02453ffb814c9fe-26001851-921600-18d9c8c3949b02%22%7D; captcha_v4_user=462c43ac9b7b42949a02f9b687e5058f; Hm_lvt_25b04a5e7a64668b9b88e2711fb5f0c4=1707728453,1707733452; Hm_lpvt_25b04a5e7a64668b9b88e2711fb5f0c4=1707735520"
            req = self.session.get(url=login_url, params=login_params, headers=self.headers)
        except requests.exceptions.RequestException as e:
            print("请求错误", e)
        else:
            print(req.status_code)
            print(req.text)



if __name__ == "__main__":
    huakuai = jyHuaKuai()
    huakuai.do_verify()

上述代码中的cookie,id等等不变量要换成本机的环境

get_w()js代码见https://github.com/KTVICTORY18/js-中的huakuai_js

js代码中有一个参数是时不时地更新,有时效性

你可能感兴趣的:(javascript,开发语言,python,前端)