Hướng dẫn kỹ thuật

Auth Token va ct0 tren X (Twitter): kien truc phien, yeu cau HTTP va tiem cookie vao trinh duyet

Huong dan ky thuat day du ve xac thuc bang auth_token va ct0 tren X (Twitter). Bao gom header bat buoc, vi du Python va cURL, tiem cookie trong Playwright va xu ly loi 32, 353, 429.

Tác giả: Accira Security Research Team (Ky su truong Ha tang & Tu dong hoa) · Xuất bản: · 7 phút đọc

Mục lục

  1. 01. Kien truc cap cookie auth_token + ct0
  2. 02. Cac HTTP Header bat buoc
  3. 03. Vi du thuc te bang Python va cURL
  4. 04. Tiem cookie vao trinh duyet va Playwright
  5. 05. Bang ma loi HTTP va cach xu ly

Kien truc cap cookie auth_token + ct0

Tren nen tang X (truoc day la Twitter), phien dang nhap web hoat dong dua tren hai cookie dong bo: auth_token va ct0. Khi su dung dung cap token nay ket hop proxy on dinh, he thong tu dong hoa co the khoi phuc trang thai da xac thuc ma khong can nhap mat khau hay ma xac minh email lap lai.

  • auth_token: chuoi hex 40 ky tu dinh danh phien dang nhap, mang co HttpOnly va Secure de ngan JavaScript doc trom.
  • ct0: ma chong gia mao CSRF dai toi 160 ky tu hex, can duoc sao chep vao header X-Csrf-Token trong moi yeu cau API.

De tich hop tai khoan san sang cho API, xem danh sach tai khoan X co auth_token hoac toan bo danh muc X (Twitter).

Cac HTTP Header bat buoc

  1. Authorization: ma Bearer cong khai cua ung dung web (Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA).
  2. Cookie: chua auth_token va ct0 hop le cho ten mien .x.com.
  3. X-Csrf-Token: phai trung khop tuyet doi voi cookie ct0.
  4. X-Twitter-Auth-Type: dat thanh OAuth2Session.

Vi du thuc te bang Python va cURL

curl -s "https://x.com/i/api/1.1/account/verify_credentials.json"   -H "Authorization: Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"   -H "X-Twitter-Auth-Type: OAuth2Session"   -H "X-Csrf-Token: CT0_CUA_BAN"   -H "Cookie: auth_token=AUTH_TOKEN_CUA_BAN; ct0=CT0_CUA_BAN"
import requests

s = requests.Session()
s.cookies.set("auth_token", "auth_token_40_hex", domain=".x.com")
s.cookies.set("ct0", "ct0_token", domain=".x.com")
s.headers.update({
    "Authorization": "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
    "X-Twitter-Auth-Type": "OAuth2Session",
    "X-Csrf-Token": "ct0_token",
})
print(s.get("https://x.com/i/api/1.1/account/settings.json", timeout=15).status_code)

Tiem cookie vao trinh duyet va Playwright

await context.addCookies([
  { name: 'auth_token', value: AUTH_TOKEN, domain: '.x.com', path: '/', httpOnly: true, secure: true, sameSite: 'None' },
  { name: 'ct0', value: CT0_TOKEN, domain: '.x.com', path: '/', httpOnly: false, secure: true, sameSite: 'Lax' },
]);
await page.goto('https://x.com/home', { waitUntil: 'domcontentloaded' });

Bang ma loi HTTP va cach xu ly

Ma loi Nguyen nhan Huong khac phuc
32 (401) auth_token da het han hoac bi thu hoi. Thay the token moi hoac dang nhap lai tai khoan.
353 (403) Header X-Csrf-Token lech voi cookie ct0. Lay gia tri ct0 moi tu header Set-Cookie va gui lai.
64 / 326 (403) Tai khoan bi khoa tam thoi hoac yeu cau xac minh. Mo trinh duyet that voi proxy rieng de kiem tra mo khoa.
429 Vuot gioi han tan suat hoac IP dung chung bi chan. Tang thoi gian nghi va chuyen sang proxy sach rieng biet.
Quay lại danh mục Tất cả hướng dẫn