""" Copyright (C) 2024 Mandiant, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: [package root]/LICENSE.txt Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ import sys import random import logging from pathlib import Path import capa.rules logger = logging.getLogger(__name__) start_dir = Path(sys.argv[1]) txt_file_path = Path(sys.argv[2]) out_dir = Path(sys.argv[3]) output_html_path = out_dir / "index.html" assert start_dir.exists(), "input directory must exist" assert txt_file_path.exists(), "file-modification txt file must exist" assert out_dir.exists(), "output directory must exist" predefined_colors = [ "#9CAFAA", "#577590", "#a98467", "#D6DAC8", "#adc178", "#f4d35e", "#85182a", "#d6c399", "#dde5b6", "#8da9c4", "#fcd5ce", "#706993", "#FBF3D5", "#1a659e", "#c71f37", "#EFBC9B", "#7e7f9a", ] def read_file_paths(txt_file_path: Path): categorized_files: dict[str, list[Path]] = { "modified in the last day": [], "modified in the last week": [], "modified in the last month": [], "modified in the last three months": [], "modified in the last year": [], "older": [], } lines = txt_file_path.read_text(encoding="utf-8").splitlines() current_category = None for line in lines: line = line.strip() if not line: continue if "===" in line: category = line.strip("=").strip() if category in categorized_files: current_category = category else: logger.warning("Unrecognized category '%s'", category) current_category = None elif current_category: parts = line.split(" ", 1) if len(parts) == 2: file_path, last_modified_date_str = parts categorized_files[current_category].append(Path(file_path)) else: logger.warning("Skipping line due to unexpected format: %s", line) return categorized_files def parse_rule(file_path: Path): rule = capa.rules.Rule.from_yaml_file(file_path) return { "name": rule.name, "namespace": rule.meta.get("namespace", ""), "authors": rule.meta.get("authors", []), "path": file_path, "filename": file_path.name, } def generate_color(): return "#{:06x}".format(random.randint(0, 0xFFFFFF)) def get_first_word(namespace): return namespace.split("/")[0] if "/" in namespace else namespace def generate_html(categories_data, color_map): html_content = """