105 lines
3.2 KiB
Python
105 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def fix_malformed_cookie_file(file_path):
|
|
"""Fixes malformed cookie files where Netscape format is embedded in JSON"""
|
|
try:
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
content = f.read()
|
|
|
|
# Try to parse as JSON
|
|
try:
|
|
data = json.loads(content)
|
|
except:
|
|
print(f"Skipping {file_path.name} - not valid JSON")
|
|
return False
|
|
|
|
# Check if it's a malformed cookie file
|
|
if isinstance(data, list) and len(data) > 0:
|
|
first_item = data[0]
|
|
if 'name' in first_item and '\t' in str(first_item['name']):
|
|
# This is malformed - Netscape format embedded in name field
|
|
name_value = first_item['name']
|
|
value_field = first_item.get('value', '')
|
|
|
|
# Combine name and value to get full Netscape format
|
|
netscape_content = name_value + '\n' + value_field
|
|
|
|
# Parse Netscape format into proper JSON
|
|
cookies = []
|
|
for line in netscape_content.split('\n'):
|
|
line = line.strip()
|
|
if not line or line.startswith('#'):
|
|
continue
|
|
|
|
parts = line.split('\t')
|
|
if len(parts) >= 7:
|
|
cookie = {
|
|
"domain": parts[0],
|
|
"flag": parts[1] == "TRUE",
|
|
"path": parts[2],
|
|
"secure": parts[3] == "TRUE",
|
|
"expiration": int(parts[4]) if parts[4].isdigit() else 0,
|
|
"name": parts[5],
|
|
"value": parts[6]
|
|
}
|
|
cookies.append(cookie)
|
|
|
|
if cookies:
|
|
# Save fixed JSON
|
|
with open(file_path, 'w', encoding='utf-8') as f:
|
|
json.dump(cookies, f, indent=2)
|
|
return True
|
|
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"Error processing {file_path.name}: {e}")
|
|
return False
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) > 1:
|
|
# Process specific directory
|
|
target_dir = Path(sys.argv[1])
|
|
else:
|
|
# Process current directory
|
|
target_dir = Path.cwd()
|
|
|
|
if not target_dir.exists():
|
|
print(f"Directory not found: {target_dir}")
|
|
return
|
|
|
|
print(f"Scanning directory: {target_dir}")
|
|
print()
|
|
|
|
# Find all .txt and .json files
|
|
cookie_files = list(target_dir.glob("*.txt")) + list(target_dir.glob("*.json"))
|
|
|
|
if not cookie_files:
|
|
print("No cookie files found (.txt or .json)")
|
|
return
|
|
|
|
print(f"Found {len(cookie_files)} file(s)")
|
|
print()
|
|
|
|
fixed_count = 0
|
|
|
|
for cookie_file in cookie_files:
|
|
if fix_malformed_cookie_file(cookie_file):
|
|
print(f"✓ Fixed: {cookie_file.name}")
|
|
fixed_count += 1
|
|
|
|
print()
|
|
print("=" * 70)
|
|
print(f"Total files scanned: {len(cookie_files)}")
|
|
print(f"Files fixed: {fixed_count}")
|
|
print("=" * 70)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|