跳转到主要内容
指南

Akamai Bot Manager 与 _abck Cookie:如何在 Python 和 Node.js 中解决 "Access Denied"

NSLSolver 团队发布于 8 分钟阅读

Akamai Bot Manager 保护着大量零售、航空、银行和票务网站。它在浏览器中运行一个 JavaScript 传感器,把采集到的数据回传给 Akamai,并把判定结果保存在名为 _abck 的 Cookie 中。没有有效 _abck(或 _abck 来自另一个客户端)的请求,会被 Akamai 边缘以 403 Access Denied、428 拒绝,或者让你真正想调用的 API 静默失败。

普通 HTTP 客户端从不运行这个传感器,所以 Python requests、Node fetch 和 curl 会在 Chrome 中能正常打开的 Akamai 站点上被拦截。本文介绍如何识别 Akamai、哪些 Cookie 是关键、如何通过 NSLSolver API 获取有效 Cookie,以及如何在 Python 和 Node.js 中复用与刷新它们。

简而言之: 把页面 URL、你将使用的完整 user agent 和一个固定 IP 代理发送给 API,然后通过同一个代理、使用同一个 user agent 回放返回的所有 Cookie;当 Akamai 边缘返回 403 Access Denied 时重新求解。 NSLSolver Akamai API(beta)一次调用即可返回经校验的 _abck 与会话 Cookie,每 1,000 次成功求解 $2.00。

延伸阅读:另一种主流的传感器类防护请看 Kasada x-kpsdk 请求头指南。下文的 Cookie 复用方式与我们在 cf_clearance Cookie 详解中介绍的 Cloudflare 方案相同。

如何判断网站使用了 Akamai Bot Manager?

Akamai 很少在页面上显式标识自己,但有几个可靠的信号。在写代码之前,先在浏览器开发者工具(Application 和 Network 面板)中检查:

  • 站点域名下设置了名为 _abck 和 bm_sz 的 Cookie,常常还伴随 ak_bmsc 和 bm_sv。Bot Manager 检查的是 _abck。
  • 被拦截的请求返回一个简短的 "Access Denied" 页面,提示你无权访问该资源,并以 "Reference #" 编号结尾。响应的 Server 头通常是 AkamaiGHost。
  • 页面从同域名下一个看似随机的路径加载第一方脚本,并在页面加载后不久向同一路径 POST 数据。这就是传感器。
  • HTML 在浏览器中正常,但其背后的 JSON 接口(搜索、价格、登录、加入购物车)在脚本中即使带上相同的请求头也会失败。

_abck 和 bm_sz Cookie 有什么作用?

Akamai 将这些 Cookie 列为 Bot Manager 的一部分,但没有公开其内部格式。实际中需要关注的是:

Cookie作用对你的代码意味着什么
_abck保存 Bot Manager 对该客户端的判定;每次传感器提交后更新把守受保护接口的 Cookie。它必须处于已校验状态,并随每个请求发送
bm_sz首次访问时设置的会话 Cookie,与 _abck 配合使用与 _abck 一起发送;不匹配的组合会被视为新的、不受信任的客户端
ak_bmsc边缘设置的 Bot Manager 会话 Cookie如果站点设置了它就原样回传;它不能替代 _abck
bm_sv部分站点在会话后期设置保留站点设置的所有 Cookie;Cookie jar 会自动处理

请把所有值都当作不透明的数据。这些 Cookie 不是可以手动解码、修改或生成的令牌,其具体行为也因站点配置而异。

为什么 Python requests 和 curl 在 Akamai 上会得到 403?

Bot Manager 评估的不只是 Cookie。一个请求通常会从以下方面被判断:

  • 是否带有有效的 _abck,这要求传感器已经运行并被接受。
  • 客户端身份是否与获得 Cookie 的身份一致:相同的出口 IP、相同的 User-Agent、一致的请求头。
  • 客户端的 TLS 与 HTTP/2 指纹。Python、Node 和 Go 的默认客户端与 Chrome 完全不同。
  • IP 地址的信誉。数据中心 IP 段的评分比住宅或移动 IP 更严格。
  • 随时间推移的行为:请求频率、访问接口的顺序,以及跳过真实访客会先加载的页面直接请求接口。

如何以编程方式获取有效的 _abck Cookie?

你需要一个能运行传感器并让它被接受的客户端,然后把得到的 Cookie 交给你的 HTTP 客户端。NSLSolver 替你完成这一步:把加载 Akamai 脚本的页面 URL、你将用于回放的 user agent 和你的代理,以 type "akamai" POST 到 /solve,即可拿回 Cookie(_abck、bm_sz 以及站点设置的其他 Cookie)。Akamai 支持目前处于 beta 阶段:平均求解约 15 秒,每 1,000 次成功求解 $2.00,失败的求解不计费。

三个字段都是必填项。代理之所以重要,是因为 Cookie 是从它的出口 IP 获得的;user agent 之所以重要,是因为 Akamai 会在之后的每个请求中检查它。请使用固定会话代理,确保求解和后续请求之间 IP 不变。

请仅在你自己的网站、预发布与测试环境,以及条款允许自动化访问的网站上使用。

get-akamai-cookies.sh
# 1) Ask NSLSolver for Akamai cookies minted through YOUR proxy and user agent
curl -s https://api.nslsolver.com/solve \
  -H "X-API-Key: nsl_YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "akamai",
    "url": "https://protected-site.example.com/login",
    "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36",
    "proxy": "http://user:pass@sticky-proxy.example.com:8080"
  }'

# Response (200 OK)
# {
#   "success": true,
#   "type": "akamai",
#   "cookies": { "_abck": "7A12...", "bm_sz": "D41F..." },
#   "cost": 0.002
# }

# 2) Replay ALL returned cookies with the SAME proxy and the SAME user agent
curl -s https://protected-site.example.com/api/products \
  -x "http://user:pass@sticky-proxy.example.com:8080" \
  -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36" \
  -b "_abck=7A12...; bm_sz=D41F..."

在 Python 中复用 Akamai Cookie(curl_cffi)

一个代理 IP、一次求解、多次请求。下面的会话只设置一次 user agent,把返回的所有 Cookie 放进 Cookie jar,只在 Akamai 边缘拦截请求时才重新求解。curl_cffi 能复现 Chrome 的 TLS 与 HTTP/2 指纹;请让 user agent 与模拟目标使用相同的 Chrome 版本,避免两者相互矛盾。

注意这里有两个 URL:在加载 Akamai 脚本的页面(通常是首页或登录页)上求解,再调用你需要的接口。让 Cookie jar 保留站点在会话中更新的 Cookie,比反复发送一份固定副本更好。

akamai_session.py
# akamai_session.py  (pip install curl_cffi)
# Solve once per proxy IP, replay the cookies, re-solve only when Akamai blocks.
import threading
from curl_cffi import requests

API_KEY = "nsl_YOUR_API_KEY"
PROXY = "http://user:pass@sticky-proxy.example.com:8080"  # sticky exit IP
# Must be the exact UA you send afterwards; keep it on the same Chrome
# version as the curl_cffi impersonation target.
USER_AGENT = (
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
    "(KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36"
)


def is_akamai_block(resp) -> bool:
    """True when the Akamai edge denied the request instead of serving it."""
    if resp.status_code not in (403, 428, 429):
        return False
    server = resp.headers.get("server", "")
    body = resp.text[:3000]
    return "AkamaiGHost" in server or ("Access Denied" in body and "Reference #" in body)


class AkamaiSession:
    def __init__(self, proxy: str, user_agent: str):
        self.proxy = proxy
        self.generation = 0
        self.lock = threading.Lock()
        self.s = requests.Session(impersonate="chrome")
        self.s.proxies = {"http": proxy, "https": proxy}
        self.s.headers["User-Agent"] = user_agent
        self.user_agent = user_agent

    def solve(self, url: str) -> None:
        r = requests.post(
            "https://api.nslsolver.com/solve",
            headers={"X-API-Key": API_KEY},
            json={"type": "akamai", "url": url, "user_agent": self.user_agent, "proxy": self.proxy},
            timeout=120,
        )
        data = r.json()
        if r.status_code != 200 or not data.get("success"):
            raise RuntimeError(f"solve failed: {r.status_code} {data}")
        for name, value in data["cookies"].items():  # _abck, bm_sz, ...
            self.s.cookies.set(name, value)
        self.generation += 1

    def get(self, url: str, solve_url: str):
        seen = self.generation
        resp = self.s.get(url, timeout=30)
        if is_akamai_block(resp):
            with self.lock:  # one solve per proxy IP, not one per thread
                if self.generation == seen:
                    self.solve(solve_url)
            resp = self.s.get(url, timeout=30)
        return resp


ak = AkamaiSession(PROXY, USER_AGENT)
for page in range(1, 21):
    r = ak.get(
        f"https://protected-site.example.com/api/products?page={page}",
        solve_url="https://protected-site.example.com/login",  # page that loads the Akamai script
    )
    print(page, r.status_code)

在 Node.js 中复用 Akamai Cookie

Node.js 中的流程相同,使用 undici 的 ProxyAgent,确保求解和所有请求都从同一个 IP 发出。Node 的 TLS 指纹并不是浏览器的指纹;如果 IP 和 user agent 都正确,有效的 Cookie 仍被拒绝,请把这些调用改用能模拟浏览器的客户端。

akamai-session.mjs
// akamai-session.mjs  (Node 18+, npm i undici)
import { fetch, ProxyAgent } from "undici";

const API_KEY = "nsl_YOUR_API_KEY";
const PROXY = "http://user:pass@sticky-proxy.example.com:8080";
const USER_AGENT =
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36";
const dispatcher = new ProxyAgent(PROXY); // same exit IP for the solve and every request

let cookieHeader = "";

async function solve(url) {
  const res = await fetch("https://api.nslsolver.com/solve", {
    method: "POST",
    headers: { "X-API-Key": API_KEY, "Content-Type": "application/json" },
    body: JSON.stringify({ type: "akamai", url, user_agent: USER_AGENT, proxy: PROXY }),
  });
  const data = await res.json();
  if (!res.ok || !data.success) {
    throw new Error("solve failed: " + res.status + " " + JSON.stringify(data));
  }
  cookieHeader = Object.entries(data.cookies).map(([k, v]) => k + "=" + v).join("; ");
}

function isAkamaiBlock(res, body) {
  if (![403, 428, 429].includes(res.status)) return false;
  return (res.headers.get("server") || "").includes("AkamaiGHost") ||
    (body.includes("Access Denied") && body.includes("Reference #"));
}

async function fetchOnce(url) {
  const headers = { "User-Agent": USER_AGENT, ...(cookieHeader ? { Cookie: cookieHeader } : {}) };
  const res = await fetch(url, { dispatcher, headers });
  return { res, body: await res.text() };
}

export async function get(url, solveUrl) {
  let { res, body } = await fetchOnce(url);
  if (isAkamaiBlock(res, body)) {
    await solve(solveUrl);
    ({ res, body } = await fetchOnce(url));
  }
  return { status: res.status, body };
}

const { status } = await get(
  "https://protected-site.example.com/api/products",
  "https://protected-site.example.com/login",
);
console.log(status);

完整 API 文档

Akamai Cookie 什么时候会失效?

Akamai 没有公布固定的有效期,而且因站点而异。与其预测过期时间,不如做好重新求解的准备:

  • 在每个响应上检测拦截:403(有时是 428 或 429),并带有 Server: AkamaiGHost 或 "Access Denied ... Reference #" 页面。
  • 遇到拦截时,求解一次并重试一次。如果重试仍被拦截,就停下来检查代理、user agent 和客户端指纹,而不是无限循环。
  • 每个代理 IP 保留一组 Cookie,并在多个 worker 之间加锁共享,这样十个线程同时被拦截时只会触发一次求解。
  • 实际中,更换出口 IP 或 user agent 都会使会话失效。请同时轮换两者,并为新的身份重新求解。
  • 即使 Cookie 有效,在一组 Cookie 上的请求频率过高也会被标记。请把负载分散到多个代理身份上。

Akamai 拦截问题排查

现象可能原因解决方法
求解成功后立即 403 Access Denied请求走了不同的 IP,或没有走代理所有请求都通过传给 /solve 的那个代理发送
已设置 _abck 仍被拦截User-Agent 与传给 /solve 的不同,或没有发送 bm_sz复用完全相同的 user_agent,并发送返回的所有 Cookie
curl_cffi 可用,requests 或 Node 不行TLS / HTTP/2 指纹不像浏览器使用与 user agent 版本一致的 Chrome 目标的 curl_cffi
用了一段时间后再次 403站点刷新或作废了会话检测到拦截后重新求解
使用轮换代理时随机 403请求之间出口 IP 发生变化使用固定会话代理,每个 IP 一组 Cookie
HTML 能加载但 JSON 接口失败接口的防护比页面更严格在加载脚本的页面上求解,再用同一会话调用接口
/solve 返回 400url、user_agent 或 proxy 缺失或无效akamai 类型三者都是必填;检查 protocol://user:pass@host:port 代理格式
/solve 返回 503求解器未能完成本次请求重试即可;503 不计费。Akamai 支持处于 beta 阶段

只对成功的求解计费,因此重试失败的求解不会产生额外费用。

常见问题

Akamai _abck Cookie 是什么?

_abck 是 Akamai Bot Manager 用来保存对客户端判定结果的 Cookie。它会在 Akamai 传感器脚本回传数据后更新,只有当请求带着有效的 _abck、且客户端身份与获得它的身份一致时,受保护的接口才会放行。

如何判断网站是否使用了 Akamai Bot Manager?

查看站点上是否有 _abck 和 bm_sz Cookie;被拦截的响应是否显示 "Access Denied" 并带有 "Reference #" 编号、Server 头为 AkamaiGHost;以及页面是否从随机路径加载第一方脚本并向该路径 POST 数据。

能在另一个 IP 或 user agent 上复用 _abck 吗?

并不可靠。实际中,这些 Cookie 只在相同的出口 IP、相同的 user agent 和一致的客户端指纹下持续有效。请使用固定代理,并发送生成 Cookie 时传入的完全相同的 user agent。

为什么 Python requests 在 Akamai 站点上得到 403?

requests 不执行 JavaScript,因此 Akamai 传感器从未运行,也就得不到有效的 _abck;它的 TLS 指纹也与浏览器不同。请先获取有效的 Cookie,再通过 curl_cffi 等能模拟浏览器的客户端,使用相同的代理和 user agent 回放。

Akamai Cookie 的有效期有多长?

Akamai 没有公布固定的有效期,且因站点而异。请检测拦截(带 AkamaiGHost 的 403 或 "Access Denied"),一旦出现就重新求解,而不是试图预测过期时间。

NSLSolver Akamai API 的价格是多少?

每 1,000 次成功求解 $2.00,平均求解时间约 15 秒。Akamai 支持处于 beta 阶段,需要提供代理和 user agent,失败的求解不计费,新账户可获得 100 次免费求解,无需信用卡。

一次 API 调用获取有效的 Akamai Cookie

发送 URL、你的 user agent 和代理,即可拿回 _abck 及其配套 Cookie。Beta 阶段,每 1,000 次成功求解 $2.00,注册即送 100 次免费求解,无需信用卡。