# Copyright 2024 Google LLC # # 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 # # http://www.apache.org/licenses/LICENSE-2.0 # # 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 logging import urllib.parse from pathlib import Path import pygments from pygments.lexers import YamlLexer from pygments.formatters import HtmlFormatter import capa.rules logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) input_directory = Path(sys.argv[1]) txt_file_path = Path(sys.argv[2]) output_directory = Path(sys.argv[3]) assert input_directory.exists(), "input directory must exist" assert txt_file_path.exists(), "file-modification txt file must exist" assert output_directory.exists(), "output directory must exist" def render_rule(timestamps, path: Path) -> str: rule_content = path.read_text(encoding="utf-8") rule = capa.rules.Rule.from_yaml(rule_content, use_ruamel=True) filename = path.with_suffix("").name namespace = rule.meta.get("namespace", "") timestamp = timestamps[path.as_posix()] rendered_rule = pygments.highlight( rule_content, YamlLexer(), HtmlFormatter( style="xcode", noclasses=True, wrapcode=True, nobackground=True, ), ) gh_link = f"https://github.com/mandiant/capa-rules/tree/master/{namespace}/{filename}.yml" vt_query = 'behavior_signature:"' + rule.name + '"' vt_fragment = urllib.parse.quote(urllib.parse.quote(vt_query)) vt_link = f"https://www.virustotal.com/gui/search/{vt_fragment}/files" ns_query = f'"namespace: {namespace} "' ns_link = f"../?{urllib.parse.urlencode({'q': ns_query})}" html_content = f"""
last edited: {timestamp}