Skip to main content

Python implementation for generating Twitter/X authentication headers (twitter-generator and X-XP-Forwarded-For)

Project description

Twitter / X Headers Generator

Twitter headers

Status: Complete Type: Research License: MIT npm version GitHub stars GitHub repo

Check the Python version Read the full article on Medium Buy me a coffee

Python module with an attached reverse-engineering study of the X (former Twitter) x-client-transaction-id and x-xp-forwarded-for request headers.

Series Context

This repository represents Episode 1 of a multi-part analysis of Twitter/X login-flow protections.

Be sure to follow me on Medium and Github in order to prevent loosing next episodes.

Next one is going to be about the first antibot challenge.

Learning

Be sure to check out LEARN.md in order to understand how this has been made as well as the full article on Medium

Table of Contents


Installation

pip install twitter-generator

Or from source:

git clone https://github.com/GlizzyKingDreko/twitter-generator.git
cd twitter-generator
pip install -e .

Quick Start

X-Client-Transaction-Id

from twitter_generator import ClientTransactionGenerator

generator = ClientTransactionGenerator(
    ondemand_file=ondemand_js_content, # Dynamic js file content
    home_page=home_page_html # Loaded homepage content
)

transaction_id = generator.generate(
    "GET", # Method you are going to use
    "/api/1.1/statuses/user_timeline.json" # Route of the request
)

print(transaction_id)

X-XP-Forwarded-For

from twitter_generator import XPForwardedForGenerator

generator = XPForwardedForGenerator(
    guest_id="v1%3A176824413470818950" # Your guest_id cookie
)

env = {
    'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36',
    'hasBeenActive': False,
    'webdriver': False
}

token = generator.generate(env)
print(token)


# Or decrypting it via
generator.decode(
    token, 
    generator._derive_key_from_guest_id(guest_id)
)

API Reference

ClientTransactionGenerator

class ClientTransactionGenerator:
    def __init__(self, ondemand_file: str, home_page: str)
    def generate(self, method: str, path: str) -> str

XPForwardedForGenerator

class XPForwardedForGenerator:
    def __init__(self, guest_id: Optional[str] = None)
    def generate(self, env: Dict) -> str
    def decode(self, token: str, key: Optional[bytes] = None) -> Dict
    def decode_with_key(self, token: str, key_hex: str) -> Dict
    def decode_with_guest_id(self, token: str, guest_id: str) -> Dict

    @staticmethod
    def extract_guest_id_from_cookie(
        cookie_string: str
    ) -> Optional[str]

Implementations in Other Languages

Be sure to check the

License

MIT License. See LICENSE for details.


Author

If you found this project helpful or interesting, consider starring the repo and following me for more security research and tools, or buy me a coffee to keep me up.

GitHub Twitter Medium Discord Email Buy Me a Coffee

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

twitter_generator-1.0.0.tar.gz (25.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

twitter_generator-1.0.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file twitter_generator-1.0.0.tar.gz.

File metadata

  • Download URL: twitter_generator-1.0.0.tar.gz
  • Upload date:
  • Size: 25.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for twitter_generator-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a8a930a172377e1ad0af4df137517d9fe12db4c889cd49078685f223b9c44bae
MD5 aa239b6d79450f80e9303b3224134fe0
BLAKE2b-256 0df5fef71f8a436bbba55eb0cf9b1ea180f7a1d71969c8e976d37d75adae5e25

See more details on using hashes here.

File details

Details for the file twitter_generator-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for twitter_generator-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d39d87d752963d7becdf310598958b55c3c6ec37430182b03e3c54d6e5b5e029
MD5 91da4607ae5dc4cd413cadd40865791f
BLAKE2b-256 69cabf999c3c62a40574c803f0f9020768cbe160181b886376f8c12c9ff4555c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page