2026-06-15 13:46:12 +03:00

116 lines
2.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Cookie Parser
Утилита `parse_netscape_cookies.py` разбирает cookies из нескольких популярных форматов и приводит их к удобному JSON или строке `Cookie` header.
## Поддерживаемые входные форматы
### Netscape HTTP Cookie File
Формат `cookies.txt`, который часто экспортируют браузерные расширения и CLI-инструменты.
```text
.example.com TRUE / TRUE 1789148954 session_id abc123
```
### JSON-массив cookies
Формат, похожий на экспорт из Chrome extensions API.
```json
[
{
"domain": ".example.com",
"expirationDate": 1789148954.129235,
"hostOnly": false,
"httpOnly": false,
"name": "session_id",
"path": "/",
"sameSite": "lax",
"secure": true,
"session": false,
"storeId": "0",
"value": "abc123"
}
]
```
Также поддерживается объект вида:
```json
{
"cookies": []
}
```
### Cookie header
Обычная строка из HTTP-заголовка `Cookie`.
```text
session_id=abc123; theme=dark;
```
У этого формата нет метаданных вроде `domain`, `expires`, `secure` и `httpOnly`, поэтому скрипт заполняет их пустыми или безопасными значениями по умолчанию.
## Использование
Вывести JSON:
```bash
python3 parse_netscape_cookies.py cookies.txt
```
Прочитать из stdin:
```bash
cat cookies.txt | python3 parse_netscape_cookies.py
```
Вывести строку `Cookie` header:
```bash
python3 parse_netscape_cookies.py cookies.json --format header
```
URL-decode значений:
```bash
python3 parse_netscape_cookies.py cookies.txt --decode
```
Явно указать входной формат:
```bash
python3 parse_netscape_cookies.py input.txt --input-format netscape
python3 parse_netscape_cookies.py input.json --input-format json
python3 parse_netscape_cookies.py header.txt --input-format header
```
По умолчанию используется `--input-format auto`.
## Выходной JSON
Каждая cookie приводится к структуре:
```json
{
"domain": ".example.com",
"include_subdomains": true,
"path": "/",
"secure": true,
"expires": 1789148954.129235,
"name": "session_id",
"value": "abc123",
"http_only": false,
"host_only": false,
"same_site": "lax",
"session": false,
"store_id": "0",
"expires_iso": "2026-09-11T17:49:14.129235+00:00"
}
```
## Важно
Cookies могут давать доступ к аккаунту пользователя. Не отправляйте их в логи, чаты или сторонние сервисы без явного согласия пользователя.