| 1 |
|
| 2 |
|
| 3 |
import xml.etree.ElementTree as etree |
| 4 |
import subprocess |
| 5 |
import sys |
| 6 |
import os |
| 7 |
import argparse |
| 8 |
|
| 9 |
xccdf_ns = "http://checklists.nist.gov/xccdf/1.2" |
| 10 |
xccdf_1_1_ns = "http://checklists.nist.gov/xccdf/1.1" |
| 11 |
oval_result_ns = "http://oval.mitre.org/XMLSchema/oval-results-5" |
| 12 |
|
| 13 |
def get_test_property(test, property_name): |
| 14 |
property_element = test.find(property_name) |
| 15 |
if property_element is not None: |
| 16 |
return property_element.text |
| 17 |
else: |
| 18 |
return None |
| 19 |
|
| 20 |
def get_datastream_file_name(test): |
| 21 |
return get_test_property(test, "datastreamFile") |
| 22 |
|
| 23 |
def get_profile_name(test): |
| 24 |
return get_test_property(test, "xccdfProfile") |
| 25 |
|
| 26 |
def get_datastream_id(test): |
| 27 |
return get_test_property(test, "datastreamID") |
| 28 |
|
| 29 |
def get_tailoring_id(test): |
| 30 |
return get_test_property(test, "tailoringID") |
| 31 |
|
| 32 |
def get_benchmark_id(test): |
| 33 |
return get_test_property(test, "benchmarkID") |
| 34 |
|
| 35 |
def get_schema(test): |
| 36 |
return get_test_property(test, "schema") |
| 37 |
|
| 38 |
def get_oval_file_name(test): |
| 39 |
return get_test_property(test, "ovalFilename") |
| 40 |
|
| 41 |
def get_oval_variables_file_name(test): |
| 42 |
return get_test_property(test, "ovalVariables") |
| 43 |
|
| 44 |
def are_remote_resources_allowed(test): |
| 45 |
remote_resources = get_test_property(test, "remoteResources") |
| 46 |
return remote_resources == "true" |
| 47 |
|
| 48 |
def run_test(test, scanner, output_dir): |
| 49 |
test_id = test.get("suiteId") |
| 50 |
datastream = get_datastream_file_name(test) |
| 51 |
oval = get_oval_file_name(test) |
| 52 |
oval_variables = get_oval_variables_file_name(test) |
| 53 |
datastream_id = get_datastream_id(test) |
| 54 |
tailoring_id = get_tailoring_id(test) |
| 55 |
benchmark_id = get_benchmark_id(test) |
| 56 |
profile = get_profile_name(test) |
| 57 |
command = scanner |
| 58 |
if datastream: |
| 59 |
results_file = os.path.join(output_dir, test_id + ".results_arf.xml") |
| 60 |
command += " xccdf eval" |
| 61 |
if are_remote_resources_allowed(test): |
| 62 |
command += " --fetch-remote-resources" |
| 63 |
if datastream_id: |
| 64 |
command += " --datastream-id " + datastream_id |
| 65 |
if tailoring_id: |
| 66 |
command += " --tailoring-id " + tailoring_id |
| 67 |
if profile: |
| 68 |
command += " --profile " + profile |
| 69 |
if benchmark_id: |
| 70 |
command += " --benchmark-id " + benchmark_id |
| 71 |
command += " --results-arf " + results_file + " " + datastream |
| 72 |
elif oval: |
| 73 |
results_file = test_id + ".results_oval.xml" |
| 74 |
command += " oval eval" |
| 75 |
if oval_variables: |
| 76 |
command += " --variables " + oval_variables |
| 77 |
command += " --results " + results_file + " " + oval |
| 78 |
else: |
| 79 |
sys.stderr.write("Unable to run the test! Check the catalog.xml.\n") |
| 80 |
return None |
| 81 |
print(command) |
| 82 |
with open(os.path.join(output_dir, test_id + ".stdout"), "w") as stdout_file: |
| 83 |
oscap_return_code = subprocess.call(["bash", "-c", command], |
| 84 |
stdout=stdout_file) |
| 85 |
|
| 86 |
if oscap_return_code not in [0, 2]: |
| 87 |
sys.stderr.write( |
| 88 |
"oscap returned an invalid exit code '%i'" % (oscap_return_code) |
| 89 |
) |
| 90 |
return None |
| 91 |
|
| 92 |
|
| 93 |
def find_actual_result_in_arf(arf_root, rule_id): |
| 94 |
rule_results = arf_root.findall(".//{%s}rule-result" % xccdf_ns) |
| 95 |
rule_results.extend(arf_root.findall(".//{%s}rule-result" % xccdf_1_1_ns)) |
| 96 |
|
| 97 |
for rule_result in rule_results: |
| 98 |
if rule_result.get("idref") == rule_id: |
| 99 |
result = rule_result.find("{%s}result" % xccdf_ns) |
| 100 |
if result is None: |
| 101 |
result = rule_result.find("{%s}result" % xccdf_1_1_ns) |
| 102 |
if result is not None: |
| 103 |
return result.text |
| 104 |
else: |
| 105 |
return None |
| 106 |
return None |
| 107 |
|
| 108 |
def find_actual_result_in_oval(oval_root, definition_id): |
| 109 |
definition_results = oval_root.findall("{" + oval_result_ns + "}results/{" + |
| 110 |
oval_result_ns + "}system/{" + oval_result_ns + "}definitions/{" + |
| 111 |
oval_result_ns + "}definition") |
| 112 |
for definition_result in definition_results: |
| 113 |
if definition_result.get("definition_id") == definition_id: |
| 114 |
result = definition_result.get("result") |
| 115 |
return result |
| 116 |
return None |
| 117 |
|
| 118 |
def check_results(test, output_dir): |
| 119 |
test_id = test.get("suiteId") |
| 120 |
all_expected_results_elements = test.findall("expectedResults") |
| 121 |
expected_results = None |
| 122 |
report_type = None |
| 123 |
for x in all_expected_results_elements: |
| 124 |
expected_type = x.get("reportType") |
| 125 |
if expected_type == "arf" or expected_type == "oval": |
| 126 |
expected_results = x |
| 127 |
report_type = expected_type |
| 128 |
if expected_results is None: |
| 129 |
print("Catalog doesn't provide expected results.") |
| 130 |
return False |
| 131 |
if report_type == "arf": |
| 132 |
results_file = test_id + ".results_arf.xml" |
| 133 |
elif report_type == "oval": |
| 134 |
results_file = test_id + ".results_oval.xml" |
| 135 |
try: |
| 136 |
actual_results_xml_tree = etree.parse( |
| 137 |
os.path.join(output_dir, results_file) |
| 138 |
) |
| 139 |
except IOError: |
| 140 |
print("ERROR: No results were generated in test " + test_id) |
| 141 |
return False |
| 142 |
actual_results_root = actual_results_xml_tree.getroot() |
| 143 |
rules = expected_results.findall("result") |
| 144 |
test_result = True |
| 145 |
for rule in rules: |
| 146 |
rule_id = rule.get("definition") |
| 147 |
expected_result = rule.get("expectedResult") |
| 148 |
if report_type == "arf": |
| 149 |
actual_result = find_actual_result_in_arf(actual_results_root, rule_id) |
| 150 |
if report_type == "oval": |
| 151 |
actual_result = find_actual_result_in_oval(actual_results_root, rule_id) |
| 152 |
if actual_result is None: |
| 153 |
test_result = False |
| 154 |
actual_result = "<None>" |
| 155 |
if actual_result != expected_result: |
| 156 |
print("Failing rule: rule_id: " + rule_id + ", expected: " + expected_result + ", actual: " + actual_result) |
| 157 |
test_result = False |
| 158 |
return test_result |
| 159 |
|
| 160 |
if __name__ == "__main__": |
| 161 |
parser = argparse.ArgumentParser() |
| 162 |
parser.add_argument("--scanner", default="oscap", |
| 163 |
help="Specifies a custom path to oscap scanner.") |
| 164 |
parser.add_argument("--outputdir", |
| 165 |
help="Specifies a custom path where any output artifacts will be " |
| 166 |
"written. By default the same directory as testdir is used.") |
| 167 |
parser.add_argument("testdir", |
| 168 |
help="Specifies a directory containing test case, its SCAP content and catalog.") |
| 169 |
args = parser.parse_args() |
| 170 |
test_dir = os.path.abspath(args.testdir) |
| 171 |
output_dir = os.path.abspath(args.outputdir) \ |
| 172 |
if args.outputdir is not None else test_dir |
| 173 |
scanner_path = args.scanner |
| 174 |
|
| 175 |
os.chdir(test_dir) |
| 176 |
|
| 177 |
catalog_xml_tree = etree.parse("catalog.xml") |
| 178 |
catalog = catalog_xml_tree.getroot() |
| 179 |
return_value = 0 |
| 180 |
for test in catalog.findall("testSuite"): |
| 181 |
test_id = test.get("suiteId") |
| 182 |
print("# " + test_id) |
| 183 |
schema = get_schema(test) |
| 184 |
if schema == "windows": |
| 185 |
print("SKIPPED") |
| 186 |
continue |
| 187 |
if "remote" in test_id: |
| 188 |
print("SKIPPED") |
| 189 |
continue |
| 190 |
run_test(test, scanner_path, output_dir) |
| 191 |
if check_results(test, output_dir): |
| 192 |
print("PASS") |
| 193 |
else: |
| 194 |
print("FAIL") |
| 195 |
return_value = 1 |
| 196 |
print("") |
| 197 |
|
| 198 |
sys.exit(return_value) |