# HG changeset patch # User jfp <jf.pieronne@laposte.net> # Date 1701438696 -3600 # Fri Dec 01 14:51:36 2023 +0100 # Node ID f189a61c23d238d92745463c26a274bf9553d44d # Parent 9eace7214a4cb5e74113ecb7be7d1f1e1bba0169 Fix how to retreive rules diff --git a/secrules/__main__.py b/secrules/__main__.py --- a/secrules/__main__.py +++ b/secrules/__main__.py @@ -1,10 +1,11 @@ # -*- coding: iso-8859-1 -*- -import sys +import argparse +import importlib import os import re -import argparse -import importlib +import sys +from pathlib import Path DEBUG = False if DEBUG: @@ -14,11 +15,11 @@ # Unless a host and port are specified, host defaults to 127.0.0.1 debugpy.configure(subProcess=False) - debugpy.listen(('0.0.0.0', 5678), in_process_debug_adapter=True) - print('Waiting for debugger attach') + debugpy.listen(("0.0.0.0", 5678), in_process_debug_adapter=True) + print("Waiting for debugger attach") debugpy.wait_for_client() debugpy.breakpoint() - print('break on this line') + print("break on this line") all_rules = {} @@ -39,7 +40,7 @@ getattr(m, r)(fo, export) else: for n in numrule: - rname = 'rule%s%02d' % (seclass[-2:], n) + rname = "rule%s%02d" % (seclass[-2:], n) if rname in rules: if info: print(getattr(m, rname).__name__) @@ -52,12 +53,12 @@ class InflateRange(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): lst = [] - for string in values: # type: ignore - string = string.replace('(', '') - string = string.replace(')', '') - if '-' in string or ':' in string: - string = string.replace(':', '-') - m = re.match(r'(\d+)(?:-(\d+))?$', string) + for string in values: # type: ignore + string = string.replace("(", "") + string = string.replace(")", "") + if "-" in string or ":" in string: + string = string.replace(":", "-") + m = re.match(r"(\d+)(?:-(\d+))?$", string) # ^ (or use .split('-'). anyway you like.) if not m: raise argparse.ArgumentTypeError( @@ -69,8 +70,8 @@ end = m.group(2) or start lst.extend(list(range(int(start, 10), int(end, 10) + 1))) else: - string = string.replace(',', ' ') - for string in string.split(' '): + string = string.replace(",", " ") + for string in string.split(" "): if string: lst.append(int(string)) setattr(namespace, self.dest, lst) @@ -78,21 +79,23 @@ def load_rules(levels): global all_rules + + mods = [ fn[:-3] - for fn in os.listdir('./secrules') - if fn.startswith('rule') and fn[-1:].lower() == 'y' + for fn in os.listdir(Path(__file__).parent) + if fn.startswith("rule") and fn[-1:].lower() == "y" ] all_rules = {} for modn in mods: - m = importlib.import_module('.' + modn, 'secrules') + m = importlib.import_module("." + modn, "secrules") # m = __import__('secrules.' + modn, globals(), locals(), ['*'], -1) lst = [m, []] for r in dir(m): - if r.startswith('rule'): + if r.startswith("rule"): if ( levels is None - or not hasattr(getattr(m, r), 'rule_level') + or not hasattr(getattr(m, r), "rule_level") or getattr(m, r).rule_level in levels ): lst[1].append(r) @@ -102,45 +105,43 @@ def main(): global args - parser = argparse.ArgumentParser(description='security checker') + parser = argparse.ArgumentParser(description="security checker") parser.add_argument( - '--output', - type=argparse.FileType('w'), - dest='fo', - metavar='out-file', - help='output file', + "--output", + type=argparse.FileType("w"), + dest="fo", + metavar="out-file", + help="output file", default=sys.stdout, ) - parser.add_argument( - '--class', type=int, dest='seclass', help='security class' - ) + parser.add_argument("--class", type=int, dest="seclass", help="security class") parser.add_argument( - '--rule', + "--rule", action=InflateRange, - nargs='*', - dest='numrule', - help='rule number', + nargs="*", + dest="numrule", + help="rule number", ) parser.add_argument( - '--export', - action='store_true', - dest='export', + "--export", + action="store_true", + dest="export", default=False, - help='export format', + help="export format", ) parser.add_argument( - '--info', - action='store_true', - dest='info', + "--info", + action="store_true", + dest="info", default=False, - help='Rules info', + help="Rules info", ) parser.add_argument( - '--level', + "--level", action=InflateRange, - nargs='*', - dest='levels', - help='rule levels', + nargs="*", + dest="levels", + help="rule levels", ) args = parser.parse_args() @@ -149,16 +150,16 @@ if args.seclass is None: if args.numrule is not None: - raise argparse.ArgumentTypeError('missing seclass argument') + raise argparse.ArgumentTypeError("missing seclass argument") lst = list(all_rules.keys()) lst.sort() for seclass in lst: # seclass = 'rules%02d' % args.seclass rules_exec(seclass, args.numrule, args.info, args.fo, args.export) else: - seclass = 'rules%02d' % args.seclass + seclass = "rules%02d" % args.seclass rules_exec(seclass, args.numrule, args.info, args.fo, args.export) -if __name__ == '__main__': +if __name__ == "__main__": main()