Tutorials

AWS WAF token expiration: lifetime, domains, refresh

Why aws-waf-token gets re-challenged after 300 seconds, which hosts accept it, and a Python session that re-solves at 240 s with AntiAwsWafTaskProxyLess.

AWS WAF token expiration: lifetime, domains, refresh

Your scraper solves AWS WAF once, runs fine for about five minutes, then every request comes back as the challenge page again. The aws-waf-token cookie did not break. It aged past the rule's immunity time, 300 seconds by default. This post covers how long the token stays valid, which hosts accept it, how to spot the expired state in code, and a Python session that re-solves at 240 seconds so the block never happens. Over the last 30 days a re-solve with AntiAwsWafTaskProxyLess took 766 ms on average.


how long an aws-waf-token stays valid

The token records when the client last passed a challenge and when it last solved a CAPTCHA. On every request, AWS WAF compares those timestamps with the immunity time of the rule that matched. Inside the window the request goes through. Outside it, the rule challenges again.

  • Default window: 300 seconds, for both the challenge and the CAPTCHA action.
  • Who sets it: the site owner, per rule or for the whole web ACL. Some sites run longer windows, some shorter. You can't read the value from outside.
  • What the cookie says: nothing useful. The token payload is encrypted, and the cookie's own expiry is not the immunity window, so your cookie jar can keep sending a token the WAF already treats as stale.

So treat 300 seconds as the default and track the age of the token yourself. A token that passed a challenge but never a CAPTCHA also fails CAPTCHA rules at any age, which is a different problem covered in AWS WAF challenge vs CAPTCHA.

Timeline of an aws-waf-token: solved at 0 s, refreshed at 240 s, the default immunity time ends at 300 s and the next request gets a 202 challenge instead of the page.


which hosts accept the token

By default AWS WAF accepts a token only on the domain it was issued for. A token solved on www.example.com gets challenged on api.example.com, even a second after the solve, unless the site owner added both hosts to the web ACL's token domain list.

That explains a common symptom: page loads work, but the XHR or form POST to an API subdomain gets a challenge or a 405. Two fixes:

  • Solve with websiteURL set to the host that receives the protected request, not the landing page.
  • Keep one token per host in your scraper. Don't copy a cookie across domains and hope.

When you set the cookie with requests, scope it to the exact hostname from the URL you solved. A cookie set on a parent domain gets sent to hosts that will reject it, which hides the real cause.


detecting an expired token

An expired token looks exactly like no token. The response is the challenge again, not an error with a reason:

  • Challenge action: HTTP 202 with a small HTML page that loads challenge.js from *.token.awswaf.com.
  • CAPTCHA action: HTTP 405 with the CAPTCHA page.

Check both the status and the body, because some origins wrap the response:

def is_rechallenged(resp):
    if resp.status_code not in (202, 405):
        return False
    body = resp.text
    return "token.awswaf.com" in body or "gokuProps" in body

If this returns true on a session that worked a minute ago, the token aged out (or you hit another host). If it returns true on the first request after a solve, check the User-Agent and the cookie domain instead.


refreshing before the window closes

Waiting for the re-challenge costs you one failed request per window and, on a busy scraper, a burst of failures in parallel workers. Refreshing on a timer avoids both. The task is the same as the first solve:

curl -s https://api.capbypass.pro/createTask \
  -H 'Content-Type: application/json' \
  -d '{
    "clientKey": "YOUR_API_KEY",
    "task": {
      "type": "AntiAwsWafTaskProxyLess",
      "websiteURL": "https://example.com/protected-page"
    }
  }'
{ "errorId": 0, "taskId": "61138bb6-19fb-11ec-a9c8-0242ac110006" }

Poll getTaskResult every 1 to 3 seconds (the loop is in polling captcha API tasks). The ready result carries the cookie and the User-Agent it is bound to:

{
  "errorId": 0,
  "status": "ready",
  "solution": {
    "cookie": "aws-waf-token=xxxxxxxx...",
    "token": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:...",
    "userAgent": "Mozilla/5.0 ..."
  }
}

The Python SDK wraps create and poll in solve(). This session class re-solves when the token is older than 240 seconds, and once more if a request still comes back challenged:

import os
import time
from urllib.parse import urlparse

import requests
from capbypass import CapBypass

REFRESH_AFTER = 240  # seconds, under the 300 s default immunity time

client = CapBypass(
    api_key=os.environ["CAPBYPASS_API_KEY"],
    base_url="https://api.capbypass.pro",
)


def is_rechallenged(resp):
    if resp.status_code not in (202, 405):
        return False
    body = resp.text
    return "token.awswaf.com" in body or "gokuProps" in body


class WafSession:
    def __init__(self, solve_url):
        self.solve_url = solve_url
        self.host = urlparse(solve_url).hostname
        self.http = requests.Session()
        self.solved_at = None

    def refresh(self):
        solution = client.solve({
            "type": "AntiAwsWafTaskProxyLess",
            "websiteURL": self.solve_url,
        })
        name, value = solution["cookie"].split("=", 1)
        self.http.cookies.set(name, value, domain=self.host)
        self.http.headers["User-Agent"] = solution["userAgent"]
        self.solved_at = time.monotonic()

    def get(self, url, **kwargs):
        if self.solved_at is None or time.monotonic() - self.solved_at > REFRESH_AFTER:
            self.refresh()
        resp = self.http.get(url, **kwargs)
        if is_rechallenged(resp):
            self.refresh()  # shorter immunity time or token rejected
            resp = self.http.get(url, **kwargs)
        return resp


session = WafSession("https://example.com/protected-page")
for page in range(1, 51):
    resp = session.get(f"https://example.com/protected-page?page={page}")
    print(page, resp.status_code)

The 60-second margin covers clock drift, slow requests and the solve itself. At 240 seconds a session that runs all hour re-solves 15 times, which you can price on the pricing page.

If the site runs a shorter immunity time, the fallback branch catches it on the first re-challenge. Lower REFRESH_AFTER to about 80% of the gap you observe between a solve and the first re-challenge.


Bonus: +5% credits on every top-up

New to CapBypass? Apply code WELCOME_2026 at checkout for an extra 5% in credits on every top-up, with no minimum and no expiry. Redeem it on the top-up page and put it toward your first AntiAwsWafTaskProxyLess solves.


what a refresh costs

From the public status endpoint, last 30 days: AntiAwsWafTaskProxyLess ran 62,619 tasks at a 99.99% success rate with a 766 ms average solve time (an average, not a p50). That is under 1 second of work to buy another 300-second window, so refreshing early costs almost nothing in latency.

The own-proxy variant AntiAwsWafTask takes the same fields plus proxy. Use it when the target ties the token to the IP that solved it, and keep that proxy for every request in the window.


things that go wrong

  • Refreshed token, still challenged. The new cookie went in, but the old User-Agent stayed on the session. Always copy solution.userAgent on every refresh, not only the first one.
  • Parallel workers refresh at once. Ten threads hit the 240-second mark together and fire ten solves. Share one WafSession per host behind a lock, or give each worker its own token and stagger start times.
  • Token works on pages, fails on the form. That rule wants a CAPTCHA solve, not a challenge pass, and a fresh challenge token won't fix it. Re-solve with awsApiJs and pass the old cookie as awsExistingToken, as shown in the challenge vs CAPTCHA post.
  • Cookie domain set to .example.com. The jar sends it to api.example.com, which rejects it, and the failure looks like expiry. Scope cookies to the exact host.

New to the base flow? Start with bypassing AWS WAF in Python, and see the API reference for every createTask field.

Ready to start solving CAPTCHAs?

Get started with CapBypass in minutes. No credit card required.

Written by capbypass-bob for the CapBypass engineering team, who build and operate our CAPTCHA and WAF solving infrastructure.
Share:

Related Articles