首页 >后端开发 >Python教程 >如何绕过验证码

如何绕过验证码

王林
王林原创
2024-09-03 22:48:02549浏览

How to bypass recaptcha

无论人们多少次写道验证码早已过时,不再像开发者最初希望的那样有效,然而,互联网资源的所有者仍然继续用验证码保护他们的项目。但我们这个时代最流行的验证码是什么?

澄清 - 本文中介绍的所有代码都是基于验证码识别服务 2captcha 的 API 文档编写的

这是验证码。 Recaptcha V2、V3 等,由 Google 于 2007 年创建。距第一个 recaptcha 出现已经很多年了,但它仍然保持着花环,周期性地输给竞争对手,然后又赢回来。但尽管 recapcha 在神经网络面前存在诸多缺陷,但它的受欢迎程度从未达到第二位。

人们曾进行过大量创建“验证码杀手”的尝试,有些不太成功,有些看起来只是对验证码的威胁,但事实上却毫无作用。但事实仍然是,竞争对手希望做比 recapcha 更好、更可靠的事情,这表明了它的受欢迎程度。

如何使用Python绕过recaptcha(代码示例)

如果您不信任任何第三方模块,我已经准备了最通用的代码,只需稍作修改即可插入您的Python脚本中并自动解决验证码问题。这是代码本身:

导入请求
导入时间

API_KEY = 'Your_API_2Captcha_key'

def solve_recaptcha_v2(site_key, url):
    payload = {
        'key': API_KEY,
        'method': 'userrecaptcha',
        'googlekey': site_key,
        'pageurl': url,
        'json': 1
    }

    response = requests.post('https://2captcha.com/in.php', data=payload)
    result = response.json()

    if result['status'] != 1:
        raise Exception(f"Error when sending captcha: {result['request']}")

    captcha_id = result['request']

    while True:
        time.sleep(5)
        response = requests.get(f"https://2captcha.com/res.php?key={API_KEY}&action=get&id={captcha_id}&json=1")
        result = response.json()

        if result['status'] == 1:
            print("Captcha solved successfully.")
            return result['request']
        elif result['request'] == 'CAPCHA_NOT_READY':
            print("The captcha has not been solved yet, waiting...")
            continue
        else:
            raise Exception(f"Error while solving captcha: {result['request']}")

def solve_recaptcha_v3(site_key, url, action='verify', min_score=0.3):
    payload = {
        'key': API_KEY,
        'method': 'userrecaptcha',
        'googlekey': site_key,
        'pageurl': url,
        'version': 'v3',
        'action': action,
        'min_score': min_score,
        'json': 1
    }

    response = requests.post('https://2captcha.com/in.php', data=payload)
    result = response.json()

    if result['status'] != 1:
        raise Exception(f"Error when sending captcha: {result['request']}")

    captcha_id = result['request']

    while True:
        time.sleep(5)
        response = requests.get(f"https://2captcha.com/res.php?key={API_KEY}&action=get&id={captcha_id}&json=1")
        result = response.json()

        if result['status'] == 1:
            print("Captcha solved successfully.")
            return result['request']
        elif result['request'] == 'CAPCHA_NOT_READY':
            print("The captcha has not been solved yet, waiting...")
            continue
        else:
            raise Exception(f"Error while solving captcha: {result['request']}")

# Usage example for reCAPTCHA v2
site_key_v2 = 'your_site_key_v2'
url_v2 = 'https://example.com'
recaptcha_token_v2 = solve_recaptcha_v2(site_key_v2, url_v2)
print(f"Received token for reCAPTCHA v2: {recaptcha_token_v2}")

# Usage example for reCAPTCHA v3
site_key_v3 = 'your_site_key_v3'
url_v3 = 'https://example.com'
recaptcha_token_v3 = solve_recaptcha_v3(site_key_v3, url_v3)
print(f"Received token for reCAPTCHA v3: {recaptcha_token_v3}")

但是,在使用提供的脚本之前,请仔细阅读用于识别特定类型的验证码的服务的建议,以便了解此代码的工作原理。

另外,不要忘记在代码中插入您的 API 密钥,当然,还要安装必要的模块。

如何绕过node js中的recaptcha

与 Python 的情况一样,对于那些不喜欢现成解决方案的人,下面是使用 Node js 编程语言解决验证码的脚本。我提醒您不要忘记安装代码运行所需的模块,包括:
axios

您可以使用此命令安装它 –

npm 安装 axios

这是代码本身:

const axios = require('axios');
const sleep = require('util').promisify(setTimeout);

const API_KEY = 'YOUR_API_KEY_2CAPTCHA'; // Replace with your real API key

// Function for reCAPTCHA v2 solution
async function solveReCaptchaV2(siteKey, pageUrl) {
    try {
        // Sending a request for the captcha solution
        const sendCaptchaResponse = await axios.post(`http://2captcha.com/in.php`, null, {
            params: {
                key: API_KEY,
                method: 'userrecaptcha',
                googlekey: siteKey,
                pageurl: pageUrl,
                json: 1
            }
        });

        if (sendCaptchaResponse.data.status !== 1) {
            throw new Error(`Error when sending captcha: ${sendCaptchaResponse.data.request}`);
        }

        const requestId = sendCaptchaResponse.data.request;
        console.log(`Captcha sent, request ID: ${RequestId}`);

        // Waiting for the captcha solution
        while (true) {
            await sleep(5000); // Waiting 5 seconds before the next request

            const getResultResponse = await axios.get(`http://2captcha.com/res.php`, {
                params: {
                    key: API_KEY,
                    action: 'get',
                    id: requestId,
                    json: 1
                }
            });

            if (getResultResponse.data.status === 1) {
                console.log('Captcha solved successfully.');
                return getResultResponse.data.request;
            } else if (getResultResponse.data.request === 'CAPCHA_NOT_READY') {
                console.log('The captcha has not been solved yet, waiting...');
            } else {
                throw new Error(`Error while solving captcha: ${getResultResponse.data.request}`);
            }
        }
    } catch (error) {
        console.error(`An error occurred: ${error.message}`);
    }
}

// Function for reCAPTCHA v3 solution
async function solveReCaptchaV3(siteKey, pageUrl, action = 'verify', minScore = 0.3) {
    try {
        // Sending a request for the captcha solution
        const sendCaptchaResponse = await axios.post(`http://2captcha.com/in.php`, null, {
            params: {
                key: API_KEY,
                method: 'userrecaptcha',
                googlekey: siteKey,
                pageurl: pageUrl,
                version: 'v3',
                action: action,
                min_score: minScore,
                json: 1
            }
        });

        if (sendCaptchaResponse.data.status !== 1) {
            throw new Error(`Error when sending captcha: ${sendCaptchaResponse.data.request}`);
        }

        const requestId = sendCaptchaResponse.data.request;
        console.log(`Captcha sent, request ID: ${RequestId}`);

        // Waiting for the captcha solution
        while (true) {
            await sleep(5000); // Waiting 5 seconds before the next request

            const getResultResponse = await axios.get(`http://2captcha.com/res.php`, {
                params: {
                    key: API_KEY,
                    action: 'get',
                    id: requestId,
                    json: 1
                }
            });

            if (getResultResponse.data.status === 1) {
                console.log('Captcha solved successfully.');
                return getResultResponse.data.request;
            } else if (getResultResponse.data.request === 'CAPCHA_NOT_READY') {
                console.log('The captcha has not been solved yet, waiting...');
            } else {
                throw new Error(`Error while solving captcha: ${getResultResponse.data.request}`);
            }
        }
    } catch (error) {
        console.error(`An error occurred: ${error.message}`);
    }
}

// Usage example for reCAPTCHA v2
(async () => {
    const siteKeyV2 = 'YOUR_SITE_KEY_V2'; // Replace with the real site key
    const pageUrlV2 = 'https://example.com '; // Replace with the real URL of the page

    const tokenV2 = await solveReCaptchaV2(siteKeyV2, pageUrlV2);
    console.log(`Received token for reCAPTCHA v2: ${tokenV2}`);
})();

// Usage example for reCAPTCHA v3
(async () => {
    const siteKeyV3 = 'YOUR_SITE_KEY_V3'; // Replace with the real site key
    const pageUrlV3 = 'https://example.com '; // Replace with the real URL of the page
    const action = 'homepage'; // Replace with the corresponding action
    const MinScore = 0.5; // Set the minimum allowed score

    const tokenV3 = await solveReCaptchaV3(siteKeyV3, pageUrlV3, action, minScore);
    console.log(`Received token for reCAPTCHA v3: ${tokenV3}`);
})();

另外,不要忘记将您的 API 密钥插入代码中,而不是
“'YOUR_API_KEY_2CAPTCHA'”

如何在 PHP 中识别 recapcha

好了,对于那些不习惯使用现成模块的人来说,这里是直接集成的代码。该代码使用标准 PHP 函数,例如 file_get_contents 和 json_decode,以下是代码本身:

function solveRecaptchaV2($apiKey, $siteKey, $url) {
    $requestUrl = "http://2captcha.com/in.php?key={$apiKey}&method=userrecaptcha&googlekey={$siteKey}&pageurl={$url}&json=1";

    $response = file_get_contents($requestUrl);
    $result = json_decode($response, true);

    if ($result['status'] != 1) {
        throw new Exception("Error when sending captcha: " . $result['request']);
    }

    $captchaId = $result['request'];

    while (true) {
        sleep(5);
        $resultUrl = "http://2captcha.com/res.php?key={$apiKey}&action=get&id={$captchaId}&json=1";
        $response = file_get_contents($resultUrl);
        $result = json_decode($response, true);

        if ($result['status'] == 1) {
            return $result['request'];
        } elseif ($result['request'] == 'CAPCHA_NOT_READY') {
            continue;
        } else {
            throw new Exception("Error while solving captcha: " . $result['request']);
        }
    }
}

function solveRecaptchaV3($apiKey, $siteKey, $url, $action = 'verify', $minScore = 0.3) {
    $requestUrl = "http://2captcha.com/in.php?key={$apiKey}&method=userrecaptcha&googlekey={$siteKey}&pageurl={$url}&version=v3&action={$action}&min_score={$minScore}&json=1";

    $response = file_get_contents($requestUrl);
    $result = json_decode($response, true);

    if ($result['status'] != 1) {
        throw new Exception("Error when sending captcha: " . $result['request']);
    }

    $captchaId = $result['request'];

    while (true) {
        sleep(5);
        $resultUrl = "http://2captcha.com/res.php?key={$apiKey}&action=get&id={$captchaId}&json=1";
        $response = file_get_contents($resultUrl);
        $result = json_decode($response, true);

        if ($result['status'] == 1) {
            return $result['request'];
        } elseif ($result['request'] == 'CAPCHA_NOT_READY') {
            continue;
        } else {
            throw new Exception("Error while solving captcha: " . $result['request']);
        }
    }
}

// Usage example for reCAPTCHA v2
$apiKey = 'YOUR_API_KEY_2CAPTCHA';
$siteKeyV2 = 'YOUR_SITE_KEY_V2';
$urlV2 = 'https://example.com';

try {
    $tokenV2 = solveRecaptchaV2($apiKey, $siteKeyV2, $urlV2);
    echo "Received token for reCAPTCHA v2: {$tokenV2}\n";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

// Usage example for reCAPTCHA v3
$siteKeyV3 = 'YOUR_SITE_KEY_V3';
$urlV3 = 'https://example.com';
$action = 'homepage'; // Specify the appropriate action
$MinScore = 0.5; // Specify the minimum allowed score

try {
    $tokenV3 = solveRecaptchaV3($apiKey, $siteKeyV3, $urlV3, $action, $minScore);
    echo "Received token for reCAPTCHA v3: {$tokenV3}\n";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

?>

I also remind you of the need to replace some parameters in the code, in particular:
$apiKey = 'YOUR_API_KEY_2CAPTCHA';
 $siteKeyV2 = 'YOUR_SITE_KEY_V2';
$urlV2 = 'https://example.com';

因此,使用给出的示例,您可以解决与验证码识别相关的大部分问题。有问题可以在评论里提问!

以上是如何绕过验证码的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn