Files
qxzb/script/extract_cookie.py
2026-03-01 10:12:57 +08:00

147 lines
3.8 KiB
Python
Executable File
Raw Permalink 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.
#!/usr/bin/env python3
"""
从抓包文件中提取 Cookie 并保存
"""
import re
import sys
import os
def extract_cookie_from_capture(capture_file):
"""从抓包文件中提取 JSESSIONID Cookie"""
print(f"📄 读取抓包文件: {capture_file}")
try:
with open(capture_file, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
# 查找 JSESSIONID
# 格式1: Cookie: JSESSIONID=xxxxx
pattern1 = r'Cookie:\s*JSESSIONID=([A-Z0-9]+)'
matches1 = re.findall(pattern1, content)
# 格式2: Set-Cookie: JSESSIONID=xxxxx
pattern2 = r'Set-Cookie:\s*JSESSIONID=([A-Z0-9]+)'
matches2 = re.findall(pattern2, content)
# 合并所有匹配
all_cookies = matches1 + matches2
if all_cookies:
# 使用最后一个最新的Cookie
cookie = all_cookies[-1]
print(f"✅ 找到 {len(all_cookies)} 个 Cookie")
print(f"🔑 最新 Cookie: JSESSIONID={cookie}")
return f"JSESSIONID={cookie}"
else:
print("❌ 未找到 Cookie")
return None
except FileNotFoundError:
print(f"❌ 文件不存在: {capture_file}")
return None
except Exception as e:
print(f"❌ 读取失败: {e}")
return None
def save_cookie(cookie, output_file):
"""保存 Cookie 到文件"""
try:
# 确保目录存在
os.makedirs(os.path.dirname(output_file), exist_ok=True)
with open(output_file, 'w') as f:
f.write(cookie)
print(f"💾 Cookie 已保存到: {output_file}")
return True
except Exception as e:
print(f"❌ 保存失败: {e}")
return False
def extract_apis(capture_file):
"""提取所有 API 调用"""
print(f"\n📋 提取 API 调用...")
try:
with open(capture_file, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
# 查找 Target URI
pattern = r'Target URI:\s*([^\s]+)'
apis = re.findall(pattern, content)
if apis:
print(f"✅ 找到 {len(apis)} 个 API 调用:")
# 去重并排序
unique_apis = sorted(set(apis))
for api in unique_apis:
count = apis.count(api)
print(f" - {api} (调用 {count} 次)")
return unique_apis
else:
print("❌ 未找到 API 调用")
return []
except Exception as e:
print(f"❌ 提取失败: {e}")
return []
def main():
print("🔍 Cookie 提取工具")
print("="*60)
# 获取抓包文件路径
if len(sys.argv) >= 2:
capture_file = sys.argv[1]
else:
capture_file = "/Users/amos/Downloads/game/7q/capture_20260301_100105.txt"
print(f"⚠️ 使用默认文件: {capture_file}")
# 输出文件
if len(sys.argv) >= 3:
output_file = sys.argv[2]
else:
output_file = "/Users/amos/Downloads/game/7q/latest_cookie.txt"
print(f"📁 输出文件: {output_file}")
print("="*60)
print()
# 提取 Cookie
cookie = extract_cookie_from_capture(capture_file)
if cookie:
# 保存 Cookie
if save_cookie(cookie, output_file):
print()
print("="*60)
print("✅ 完成")
print("="*60)
print()
print("💡 使用方法:")
print(f" python3 exchange_item.py 9 10")
print()
else:
sys.exit(1)
else:
sys.exit(1)
# 提取 API 列表
extract_apis(capture_file)
print()
print("="*60)
if __name__ == "__main__":
main()