File size: 5,493 Bytes
223ef32 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
#!/usr/bin/env python3
"""
Script to rename response analysis files to shorter, more readable names.
This script renames all JSON and MD files in the final_response directory
from long names like:
- covenant_dcsync_dcerpc_drsuapi_DsGetNCChanges_2020-08-05020926_response_analysis.json
- covenant_dcsync_dcerpc_drsuapi_DsGetNCChanges_2020-08-05020926_threat_report.md
To shorter names:
- response_analysis.json
- threat_report.md
"""
import os
import sys
from pathlib import Path
from typing import List, Tuple
def find_response_files(base_dir: str) -> List[Tuple[str, str, str]]:
"""
Find all response analysis files that need to be renamed.
Args:
base_dir: Base directory to search (e.g., 'final_response')
Returns:
List of tuples: (file_path, new_json_name, new_md_name)
"""
files_to_rename = []
base_path = Path(base_dir)
if not base_path.exists():
print(f"[ERROR] Base directory '{base_dir}' does not exist!")
return files_to_rename
# Walk through all subdirectories
for root, dirs, files in os.walk(base_path):
root_path = Path(root)
# Look for JSON and MD files with the old naming pattern
json_files = [f for f in files if f.endswith('_response_analysis.json')]
md_files = [f for f in files if f.endswith('_threat_report.md')]
# Process JSON files
for json_file in json_files:
json_path = root_path / json_file
new_json_name = "response_analysis.json"
new_md_name = "threat_report.md"
files_to_rename.append((str(json_path), new_json_name, new_md_name))
# Process MD files
for md_file in md_files:
md_path = root_path / md_file
new_json_name = "response_analysis.json"
new_md_name = "threat_report.md"
files_to_rename.append((str(md_path), new_json_name, new_md_name))
return files_to_rename
def rename_files(files_to_rename: List[Tuple[str, str, str]], dry_run: bool = True) -> None:
"""
Rename the files to shorter names.
Args:
files_to_rename: List of files to rename
dry_run: If True, only show what would be renamed without actually doing it
"""
if not files_to_rename:
print("[INFO] No files found that need renaming.")
return
print(f"[INFO] Found {len(files_to_rename)} files to rename.")
if dry_run:
print("\n[DRY RUN] Files that would be renamed:")
else:
print("\n[RENAMING] Renaming files:")
success_count = 0
error_count = 0
for file_path, new_json_name, new_md_name in files_to_rename:
try:
old_path = Path(file_path)
new_name = new_json_name if file_path.endswith('.json') else new_md_name
new_path = old_path.parent / new_name
if dry_run:
print(f" {old_path.name} -> {new_name}")
else:
# Check if target file already exists
if new_path.exists():
print(f" [SKIP] {old_path.name} -> {new_name} (target already exists)")
continue
# Rename the file
old_path.rename(new_path)
print(f" [OK] {old_path.name} -> {new_name}")
success_count += 1
except Exception as e:
print(f" [ERROR] Failed to rename {file_path}: {e}")
error_count += 1
if not dry_run:
print(f"\n[SUMMARY] Renamed {success_count} files successfully, {error_count} errors.")
def main():
"""Main function to handle command line arguments and execute renaming."""
import argparse
parser = argparse.ArgumentParser(
description="Rename response analysis files to shorter names",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python rename_response_files.py # Dry run (show what would be renamed)
python rename_response_files.py --execute # Actually rename the files
python rename_response_files.py --dir custom_dir # Use custom directory
"""
)
parser.add_argument(
'--dir',
default='final_response',
help='Base directory to search for files (default: final_response)'
)
parser.add_argument(
'--execute',
action='store_true',
help='Actually rename files (default is dry run)'
)
args = parser.parse_args()
print(f"[INFO] Searching for response files in: {args.dir}")
# Find files to rename
files_to_rename = find_response_files(args.dir)
if not files_to_rename:
print("[INFO] No files found that need renaming.")
return
# Show what we found
print(f"[INFO] Found {len(files_to_rename)} files that match the old naming pattern.")
# Rename files (dry run or actual)
rename_files(files_to_rename, dry_run=not args.execute)
if not args.execute:
print("\n[INFO] This was a dry run. Use --execute to actually rename the files.")
print("Example: python rename_response_files.py --execute")
if __name__ == "__main__":
main()
|