This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
129 lines (113 loc) · 5.51 KB
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from common.audit_rule import AuditRule
from common.config_data import ConfigData
from common.expression import Expression
from common.override_rule import OverrideRule
from common.server_type import ServerType
from pathlib import Path
import json
import re
def parse_audit_rules(rule_file_name):
"""Parse the JSON rules into a list of AuditRule objects.
:param rule_file_name: The name of the file to open to get the JSON.
:return: A list of AuditRule objects created from the given file.
"""
try:
with open(rule_file_name, "r") as rule_file:
content = rule_file.read()
except FileNotFoundError:
print(f"File {rule_file_name}: Not found")
raise FileNotFoundError
json_rules_list = json.loads(content)
rules_list = []
for json_rule in json_rules_list:
tmp_expressions = []
for json_expression in json_rule["audit_expressions"]:
expression = Expression.unserializer(json_expression)
tmp_expressions.append(expression)
json_rule["audit_expressions"] = tmp_expressions
tmp_override = []
for json_override in json_rule["override_rules"]:
override = OverrideRule.unserializer(json_override)
tmp_override.append(override)
json_rule["override_rules"] = tmp_override
rule = AuditRule.unserializer(json_rule)
rules_list.append(rule)
return rules_list
def parse_config_data_apache(config_file_name, audit_rules):
"""Parse an Apache config file and return its content without the comments.
:param config_file_name: The name of the config file to parse.
:param audit_rules: A list of AuditRule objects that can be obtained by calling the parse_audit_rules function.
:return: A ConfigData object containing the active configuration of the Apache.
"""
with open(config_file_name) as config_file:
lines = config_file.readlines()
content = ""
for line in lines:
if line[0] == "#":
continue
content += line
config_data = ConfigData(ServerType.APACHE, content, config_file_name, audit_rules)
return config_data
def parse_config_data_tomcat(config_file_name, audit_rules):
"""Parse a Tomcat config file and return its content without the comments.
:param config_file_name: The name of the config file to parse.
:param audit_rules: A list of AuditRule objects that can be obtained by calling the parse_audit_rules function.
:return: A ConfigData object containing the active configuration of the Apache.
"""
end_comment = re.compile("-->")
with open(config_file_name) as config_file:
tmp_content = config_file.read()
split_content = tmp_content.split("<!--")
content = ""
for element in split_content:
if end_comment.search(element):
content += element.split("-->")[1]
else:
content += element
config_data = ConfigData(ServerType.TOMCAT, content, config_file_name, audit_rules)
return config_data
def parse_config_data_iis(config_file_name, audit_rules):
"""Parse a IIS file generated by the config extraction script and return its content.
:param config_file_name: The name of the config file to parse.
:param audit_rules: A list of AuditRule objects that can be obtained by calling the parse_audit_rules function.
:return: A ConfigData object containing the active configuration of the IIS.
"""
# The configuration will be formated using one line by CIS point in order to made easier to reach data via regexes
content = ""
# The file is a JSON one so parse it
with open(config_file_name, "r") as f:
data_raw = f.read()
iis_config = json.loads(data_raw)
# Replace references to environment variables and reload the JSON
systemDrive = iis_config["Export-DataContext"]["SystemDrive"]
systemRoot = iis_config["Export-DataContext"]["SystemRoot"]
data_raw = data_raw.replace("%SystemDrive%", systemDrive).replace("%SystemRoot%", systemRoot)
iis_config = json.loads(data_raw)
# If there is several site then the content will be stored in a JSON array of JSON objects
# If there is a single site then the content will be stored in a single JSON object
for cis_point_data_entry in iis_config:
# skip non CIS data
if cis_point_data_entry in ["InternalFunctionsInError", "Export-DataContext"]:
continue
# Get data
cis_point_datas = iis_config[cis_point_data_entry]
# Format data
cis_point_content = f"{cis_point_data_entry}="
if isinstance(cis_point_datas, list):
# List of JSON objects
for cis_point_data in cis_point_datas:
cis_point_content += str(cis_point_data).replace(": ", ":").replace(", ", ",") + ";"
else:
# JSON object
cis_point_content += str(cis_point_datas).replace(": ", ":").replace(", ", ",")
content += f"{cis_point_content}\n"
config_data = ConfigData(ServerType.IIS, content, config_file_name, audit_rules)
return config_data
def multi_file_reader(folder_path):
"""List all the files in a given folder and its subdirectories.
:param folder_path: the path to the folder to explore.
:return: A list of string representing the path to each file in the folder_path folder.
"""
root = Path(folder_path)
files = [str(f) for f in root.resolve().glob("**/*") if f.is_file()]
return files