diff options
author | Leonard Kugis <leonard@kug.is> | 2024-07-22 03:58:58 +0200 |
---|---|---|
committer | Leonard Kugis <leonard@kug.is> | 2024-07-22 03:58:58 +0200 |
commit | 5ea9a8dd752c7fab4e7373c37ee79e6eaac52ffb (patch) | |
tree | 4b2214c7bb7c27a9168f5bf6510da2b9c78b6a12 /yara-compiler.py |
Initial commit
Diffstat (limited to 'yara-compiler.py')
-rw-r--r-- | yara-compiler.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/yara-compiler.py b/yara-compiler.py new file mode 100644 index 0000000..4e0c77d --- /dev/null +++ b/yara-compiler.py @@ -0,0 +1,62 @@ +import argparse +import os +import re +from yara import * + +def dir_path(string): + if os.path.isdir(string) or re.match(r"(^\/|^\.\/|^\.\.\/|^[^/])[^:*?\"<>|\r\n]*\.yac$", string): + return string + else: + raise Error(string) + +def walk(args): + logger = logging.getLogger(__name__) + logger.info("Walking files ...") + + files = [os.path.abspath(os.path.join(dp, f)) for dp, dn, filenames in os.walk(args["input_directory"]) for f in filenames] + logger.debug("Files: {}".format(files)) + logger.info("Number of files found: {}".format(len(files))) + + if args["output"].endswith(".yac"): + yd = YaraDatabase() + for file in files: + if file.endswith(".json"): + logger.info("Compiling file {}".format(file)) + yd.add_file(file) + yd.write_file(args["output"]) + else: + for file in files: + if file.endswith(".json"): + logger.info("Compiling file {}".format(file)) + yd = YaraDatabase() + yd.add_file(file) + yd.write_file(os.path.join(args["output"], os.path.splitext(os.path.basename(file))[0] + ".yac")) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Compile single or multiple yara files') + parser.add_argument('-i', '--input-directory', nargs='?', default='.', type=dir_path, help='Input directory (default: %(default)s)') + parser.add_argument('-o', '--output', nargs='?', default='.', type=dir_path, help='Output file or directory (default: %(default)s)') + parser.add_argument('-f', '--input-file', nargs='?', default='.', type=dir_path, help='Input file (default: %(default)s)') + parser.add_argument('-v', '--verbose', action="count", default=0, help="Verbosity level") + args = parser.parse_args() + + if args.verbose == 0: + log_level = logging.WARNING + elif args.verbose == 1: + log_level = logging.INFO + elif args.verbose >= 2: + log_level = logging.DEBUG + + logging.basicConfig(stream=sys.stdout, level=log_level) + logger = logging.getLogger(__name__) + + args = { + "input_directory": args.input_directory, + "output": args.output, + "input_file": args.input_file, + "verbosity": args.verbose + } + + logger.debug("args = {}".format(args)) + + walk(args)
\ No newline at end of file |