Blog
How to solve AWS Captcha / Challenge with Python

How to solve AWS Captcha / Challenge with Python

Logo of Capsolver

CapSolver Blogger

How to use capsolver

20-Sep-2023

Bypassing aws captcha, aws challenge, aws-waf-token with Python, aws captcha python solver

⚙️ Prerequisites

  • A working proxy
  • Python installed
  • Capsolver API key

🤖 Step 1: Install Necessary Packages

Execute the following commands to install the required packages:

pip install capsolver
pip install requests
pip install BeautifulSoup

👨‍💻Python Code for bypass aws captcha / challenge

Here's a Python sample script to accomplish the task:

import capsolver
import requests
from bs4 import BeautifulSoup
import re
from urllib.parse import urlparse

PROXY = ""
PAGE_URL = ""
capsolver.api_key = ""


def solve_aws_captcha(websiteURL, awsKey, awsIv, awsContext, awsChallengeJS):
    solution = capsolver.solve({
        "type": "AntiAwsWafTask",
        "websiteURL":websiteURL,
        "awsKey":awsKey,
        "awsIv":awsIv,
        "awsContext":awsContext,
        "awsChallengeJS":awsChallengeJS,
        "proxy": PROXY
    })
    
    return solution

def solve_aws_challenge(awsChallengeJS):
    solution = capsolver.solve({
        "type": "AntiAwsWafTask",
        "awsKey":"",
        "awsIv":"",
        "awsContext":"",
        "awsChallengeJS":awsChallengeJS,
        "websiteURL": PAGE_URL,
        "proxy": PROXY
    })
    
    return solution

def main():
    session = requests.Session()
    
    session.proxies = { 
       "http"  : PROXY, 
       "https" : PROXY, 
    }
    
    headers = {
        "cache-control": "max-age=0",
        "sec-ch-ua": '"Not/A)Brand";v="99", "Google Chrome";v="107", "Chromium";v="107"',
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "Windows",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36/9uiP7EnX-09",
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "sec-fetch-site": "same-origin",
        "sec-fetch-mode": "navigate",
        "sec-fetch-user": "?1",
        "sec-fetch-dest": "document",
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "en,fr-FR;q=0.9,fr;q=0.8,en-US;q=0.7"
    }
    response = session.get(
        headers=headers,
        url=PAGE_URL
    )
    print(response.status_code)
    ## Handling AWS Challenge
    if(response.status_code == 202):
        print("AWS Challenge Solve required")
        # Parse HTML content
        soup = BeautifulSoup(response.content, 'html.parser')

        # Find all script tags
        script_tags = soup.find_all('script')

        # Filter based on src attribute
        for script in script_tags:
            src = script.get('src')
            if src and 'token.awswaf.com' in src:
                print(f'Found AWS Challenge JS URL: {src}')  
                print("Solving AWS Challenge")
                
                solution = solve_aws_challenge(src)
                print("Received AWS Cookie: " + str(solution))
    ## Handling AWS Captcha + Challenge
    if response.status_code == 405:
        soup = BeautifulSoup(response.content, 'html.parser')

        # Find the script tag that includes the source URL for "token.awswaf.com"
        script_tags = soup.find_all('script', {'src': re.compile(r'token\.awswaf\.com')})
        for script in script_tags:
            src = script.get('src')
            print(f'Found URL: {src}')

        # Extract JavaScript code and search for key, iv, and context values
        script_texts = soup.find_all('script', string=re.compile('.*key.*'))
        if script_texts:
            script_text = script_texts[0].string
            key_search = re.search('"key":"(.*?)"', script_text)
            iv_search = re.search('"iv":"(.*?)"', script_text)
            context_search = re.search('"context":"(.*?)"', script_text)
            
            key = key_search.group(1) if key_search else "Key not found"
            iv = iv_search.group(1) if iv_search else "IV not found"
            context = context_search.group(1) if context_search else "Context not found"

            print("Key:"+key)
            print("IV:"+iv)
            print("Context:"+context)
            
            print("Solving AWS Captcha")
            solution = solve_aws_captcha(PAGE_URL,key, iv, context, src)
            print("Received AWS Cookie: " + str(solution))
            # Extract the domain
    
    parsed_url = urlparse(PAGE_URL)
    domain = parsed_url.netloc
    formatted_domain = f".{domain}"
        
    session.cookies.set(
            "aws-waf-token",
            solution.get("cookie"),
            domain=formatted_domain
        )
    
    headers = {
        "cache-control": "max-age=0",
        "sec-ch-ua": '"Not/A)Brand";v="99", "Google Chrome";v="107", "Chromium";v="107"',
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "Windows",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36/9uiP7EnX-09",
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "sec-fetch-site": "same-origin",
        "sec-fetch-mode": "navigate",
        "sec-fetch-user": "?1",
        "sec-fetch-dest": "document",
        "referer": PAGE_URL,
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "en,fr-FR;q=0.9,fr;q=0.8,en-US;q=0.7"
    }
    response = session.get(
        headers=headers,
        cookies = session.cookies,
        url=PAGE_URL
    )
    print(response.status_code)
    print(response.content)
    
main()

⚠️ Change these variables

  • PROXY: Update with your proxy details. The format should be http://username:password@ip:port.
  • capsolver.api_key: Obtain your API key from the Capsolver Dashboard.
  • PAGE_URL: Replace with the URL of the website for which you wish to bypass aws captcha

👀 More information

More