Skip to content

Commit df5dd5e

Browse files
committed
Add helper functions for work with controls
1 parent 552403f commit df5dd5e

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

ctf/DiffStruct.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,42 @@ def find_rule_profiles(self, rule):
6161
if find_rule.search(line):
6262
yield profile_file
6363

64+
def find_rule_controls(self, rule):
65+
controls = []
66+
find_rule = re.compile(r"^\s*-\s*" + rule + r"\s*$", re.MULTILINE)
67+
control_folder = git_wrapper.repo_path + "/" + "controls/"
68+
# Check all yaml files in controls/
69+
for control in os.listdir(control_folder):
70+
if not control.endswith(".yml"):
71+
continue
72+
control_path = control_folder + control
73+
with open(control_path) as f:
74+
control_content = f.read()
75+
# If controls in separate directory, merge them to one string
76+
controls_dir = re.search(r"controls_dir:\s*(\w+)", control_content)
77+
if controls_dir:
78+
controls_dir = controls_dir.group(1)
79+
for c in os.listdir(control_folder + controls_dir):
80+
with open(control_folder + controls_dir + "/" + c) as cf:
81+
control_content += cf.read()
82+
# Search for rule in control content
83+
if find_rule.search(control_content):
84+
yield control.rstrip(".yml")
85+
86+
def find_control_products(self, control):
87+
products_folder = git_wrapper.repo_path + "/" + "products"
88+
find_control = re.compile(r"^\s*-\s*" + control + r":", re.MULTILINE)
89+
# Find dirs with profile files
90+
for dir_path, _, files in os.walk(products_folder):
91+
for file in files:
92+
if not file.endswith(".profile"):
93+
continue
94+
# Search if desired control is used and if so, return product
95+
with open(dir_path + "/" + file) as f:
96+
for line in f:
97+
if find_control.search(line):
98+
yield re.match(r".*/products/([^/]+)", dir_path).group(1)
99+
64100
def get_rule_ruleyml(self, rule):
65101
# Find a directory with a rule name and check if it has rule.yml file
66102
for root, dirs, files in os.walk(git_wrapper.repo_path):

0 commit comments

Comments
 (0)