-
Learn about OVAL.
-
Learn how
ComplianceAsCodefacilitates creation of new OVAL content. -
Learn how to test OVAL checks.
-
Learn how to use tests and remediations to safely and gradually improve an OVAL check.
-
The
ComplianceAsCoderepository was cloned to thelab5_ovaldirectory. -
The following dependencies required for the content build were installed using
yum install:-
Generic build utilities:
cmakeandmake, -
Utilities for generating SCAP content:
openscap-scanner -
Python dependencies for putting content together:
python3-pyyamlandpython3-jinja2
-
-
A
podmanssg_test_suiteimage was built using theDockerfiles/test_suite-rhelfiles. The SSH keys forrootare authorized by the container’s root user. The steps for how to accomplish this are in thetests/README.mdfile of theComplianceAsCodeproject. -
The OVAL check for
accounts_tmoutwas modified so you can improve it.
|
Important
|
Content used in this lab has been altered to increase its educative potential, and is therefore different from the content in ComplianceAsCode upstream repository or the content in the scap-security-guide package shipped in Red Hat® products. |
-
Log in to the VM using the text console if you have not done so already.
-
Open the terminal window on the lab laptop.
-
Execute the following command, replacing
GUIDwith your lab-provided GUID. Type r3dh4t1! for the password.[... ~]$ ssh lab-user@workstation-GUID.rhpds.opentlc.com [lab-user@workstation-GUID ~]$
-
For the best experience, open a second terminal window on the laptop:
-
Click
Activities, then right-click theTerminalicon and chooseNew Windowto open one more terminal window. -
Connect to the VM in this terminal window as well:
[... ~]$ ssh lab-user@workstation-GUID.rhpds.opentlc.com [lab-user@workstation-GUID ~]$
-
OVAL stands for Open Vulnerability and Assessment Language. In a nutshell, it is an XML-based declarative language that is part of the SCAP standard. This lab focuses on its ability to query and evaluate the state of a system. Quoting from the OVAL FAQ:
The language standardizes the three main steps of the assessment process: representing configuration information of systems for testing; analyzing the system for the presence of the specified machine state (vulnerability, configuration, patch state, etc.); and reporting the results of this assessment.
The ComplianceAsCode project supports OVAL as the language for writing automated configurable checks.
It compiles OVAL snippets into checks that are understood by OVAL interpreters—for example, the OpenSCAP scanner.
The scanner evaluates the check, and determines whether the system passes.
In this lab exercise, you go through the OVAL snippet of the accounts_tmout rule.
You see how even simple checks can rapidly become complicated, and what you can do about it.
Finally, you discover that the check was written incorrectly and you fix it.
There is already a built HTML guide in the build directory.
-
To examine it, you navigate to it, and then open the guide in the browser:
-
Click
Activities, then click the "file cabinet" icon to open the file explorer. -
Just to make sure, click the
Homeicon in the upper left portion of the file explorer window. -
Navigate to the location of the exercise by double-clicking the
labsfolder, followed by double-clicking thelab5_oval,build, andguidesfolders. -
Finally, double-click the
ssg-rhel7-guide-ospp.htmlfile.
-
-
In this lab exercise, you focus on the
accounts_tmoutrule. To find the rule entry in the guide, pressCtrl+For use theEdit → Find in this pagemenu item, and search for theSet Interactive Session Timeoutstring, which is the rule title.The description says:
Setting the TMOUT option in /etc/profile ensures that all user sessions will terminate based on inactivity. The TMOUT setting in /etc/profile should read as follows: TMOUT=600
When dealing with the rule check, there are additional aspects to keep in mind:
-
Because the timeout is supposed to be set to 600 seconds, what is the consequence if the timeout value is set to 100? Is it more or less secure?
Having a shorter time interval between inactivity and logout is more bothersome for the user, but it is a stricter requirement. Therefore, you need to make sure that if the rule requires
TMOUT=600, havingTMOUT=100is also evaluated as correct. -
The rule description that the
TMOUT=…statement is in a config file is accurate, but guides on the Internet often recommend that you haveexport TMOUT=…there. The assignment form with theexportkeyword ensures that the variable is available to other programs. Environmental variables such asPATHandHOMEare commonly exported, so this probably is where the confusion comes from thatexportis needed forTMOUTto work.In this case, you want to make sure that the rule’s check allows both forms—with and without
export, even though theexportkeyword is not required.
-
-
Examine the Bash remediation by opening the following file in the text editor:
[... ~]$ cd /home/lab-user/labs/lab5_oval [... lab5_oval]$ nano linux_os/guide/system/accounts/accounts-session/accounts_tmout/bash/shared.sh
The remediation body looks like this:
NoteThe header of the remediation is processed by the build system, so the actual file contents and the remediation displayed in the HTML guide are different. if grep --silent ^TMOUT /etc/profile ; then sed -i "s/^TMOUT.*/TMOUT=$var_accounts_tmout/g" /etc/profile else echo -e "\n# Set TMOUT to $var_accounts_tmout per security requirements" >> /etc/profile echo "TMOUT=$var_accounts_tmout" >> /etc/profile fi
You do not need to make any changes to the file.
-
After you are finished looking, press
Ctrl+Xto bring up the "save and exit" option. If you are asked about saving any changes, you probably do not want that, so entern.You can see that the remediation is in sync with the description—it handles the
/etc/profilefile, and it does one of the following:-
Adds the
TMOUTassignment to the file if it is missing -
Modifies the
TMOUTassignment so that the correct value is used if an assignment already exists
-
In this section, you move on to the OVAL check.
-
In the text editor, open the file that defines the check:
[... lab5_oval]$ nano linux_os/guide/system/accounts/accounts-session/accounts_tmout/oval/shared.xml
-
This file is much more complicated, so examine it piece by piece:
-
Note the leading
definitionelement:<definition class="compliance" id="accounts_tmout" version="2"> <metadata> <title>Set Interactive Session Timeout</title> <affected family="unix"> <platform>multi_platform_rhel</platform> <platform>multi_platform_fedora</platform> <platform>multi_platform_ol</platform> </affected> <description>Checks interactive shell timeout</description> </metadata> <criteria operator="OR"> <criterion comment="TMOUT value in /etc/profile >= var_accounts_tmout" test_ref="test_etc_profile_tmout" /> <criterion comment="TMOUT value in /etc/profile.d/*.sh >= var_accounts_tmout" test_ref="test_etc_profiled_tmout" /> </criteria> </definition> ...
The
definitionspecifies acriteriaelement. Here is a close-up of those criteria:... <criteria operator="OR"> <criterion comment="TMOUT value in /etc/profile >= var_accounts_tmout" test_ref="test_etc_profile_tmout" /> <criterion comment="TMOUT value in /etc/profile.d/*.sh >= var_accounts_tmout" test_ref="test_etc_profiled_tmout" /> </criteria> </definition> ...You can see that each criterion references a test. The first test checks for the
TMOUTsetting in the/etc/profilefile, the other one checks all files in/etc/profile.d/that have theshfile extension. If either test passes, the whole test passes as well, as theoperator="OR"attribute of thecriteriaelement imposes.A test is typically composed of an object and state definitions. The object defines what should be gathered on the tested system, the state defines expected properties of the object. In order for the test to pass, the object has to exist, and it has to conform to the specified state.
-
-
Now examine the test for the
/etc/profilecriterion and its dependencies:... <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="TMOUT in /etc/profile" id="test_etc_profile_tmout" version="1"> <ind:object object_ref="object_etc_profile_tmout" /> <ind:state state_ref="state_etc_profile_tmout" /> </ind:textfilecontent54_test> ...
The object definition associates a filename with a regular expression. The filename is checked for the regular expression, and if there is a match, contents of the regular expression group become the object.
-
Note the
instanceelement that equals1. This indicates that it is the first match of the regular expression that defines the object:... <ind:textfilecontent54_object id="object_etc_profile_tmout" version="1"> <ind:filepath>/etc/profile</ind:filepath> <ind:pattern operation="pattern match">^[\s]*TMOUT[\s]*=[\s]*(.*)[\s]*$</ind:pattern> <ind:instance datatype="int">1</ind:instance> </ind:textfilecontent54_object>
-
The state is a specification that the object (the matched substring) should be an integer that equals the value of the
var_accounts_tmoutvariable:<ind:textfilecontent54_state id="state_etc_profile_tmout" version="1"> <ind:subexpression datatype="int" operation="equals" var_check="all" var_ref="var_accounts_tmout" /> </ind:textfilecontent54_state> <external_variable comment="external variable for TMOUT" datatype="int" id="var_accounts_tmout" version="1" /> ...
There are two regular expressions that check for
TMOUT=…in theshared.xmlfile: one for theprofiletest and one for theprofile.d/*.shtest. As there are two types of locations that need to be examined, (the single/etc/profilefile and*.shfiles in the/etc/profile.ddirectory), there have to be two objects. Theobject_etc_profile_tmoutandobject_etc_profiled_tmoutobjects have different file/path specifications, but the regular expression is the same. The alternative form of the assignmentexport TMOUT=…is not handled in either of them.Moreover, there is the
equalsoperation used to perform the match. As stated in the previous section, this looks wrong, as shorter timeouts are more secure, and therefore should be allowed. -
Now you can close the file. As a reminder, you do not need to make any changes at this point. Therefore, press
Ctrl+Xto bring up the "save and exit" option. If you are asked about saving any changes, you probably do not want that, so entern.
The ComplianceAsCode project features a test suite that is useful for defining which scenarios the check and remediation are supposed to handle.
It sets up a system to a certain state and runs the scan and possibly remediations.
Results are reported in the form of console output, and detailed reports are saved to a log directory.
Regarding scenarios, consider, for example, the accounts_tmout rule—the two simplest cases are handled using the following scenarios:
-
TMOUT=600is present in/etc/profile. This test scenario should pass. -
TMOUT=600is not present in/etc/profileor/etc/profile.d/*.sh. This is more complicated because remediations become involved:-
This test scenario should fail the initial scan.
-
If there is a remediation for the rule, it should apply without errors.
-
The final scan after the remediation should pass.
-
The test suite has to prepare a system, scan it, and report results.
Due to practical considerations, the system under test should be isolated from the system running the test.
The test suite supports libvirt VMs, and docker or podman containers that satisfy this isolation requirement.
In this exercise, you are going to use a podman container with the Red Hat® Enterprise Linux® 7 (RHEL 7) image.
-
We need the RHEL 7 content to test the RHEL 7 image. As we have already seen earlier, the initial build of the content including build of the guide has already been done for us.
-
You test the
accounts_tmoutrule included in theosppprofile of the RHEL 7 datastream. You need to run the test suite as a superuser, because it involves spinning up a container that exposes an SSH port. With that in mind, execute the test suite:[... lab5_oval]$ sudo python3 tests/test_suite.py rule --container ssg_test_suite --datastream build/ssg-rhel7-ds.xml accounts_tmout INFO - The DataStream contains 2 Benchmarks INFO - 0 - scap_org.open-scap_cref_ssg-rhel7-xccdf-1.2.xml INFO - 1 - scap_org.open-scap_cref_ssg-rhel7-pcidss-xccdf-1.2.xml INFO - Selected Benchmark is 0 INFO - To select a different Benchmark, use --xccdf-id-number option. INFO - The base image option has been specified, choosing Podman-based test environment. INFO - Logging into /home/lab-user/labs/lab5_oval/logs/... INFO - xccdf_org.ssgproject.content_rule_accounts_tmout INFO - Script comment.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script line_not_there.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script correct_value.pass.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script wrong_value.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK
NoteThe test suite is a Python script
tests/test_suite.py. You supplied the following arguments to it:-
You want to use the test suite in
rulemode—you want to test a rule under all available rule test scenarios.The alternative mode is
profilemode, which is simpler—there are no test scenarios and the system is scanned. -
You want to use
podmanwith thessg_test_suiteimage as the back end, so you supply the--container ssg_test_suitearguments. -
Of course you have to specify which datastream to use for testing—you use the built one, so you specify
--datastream build/ssg-rhel7-ds.xmlarguments. -
Finally, you specify what to test—a rule regular expression:
accounts_tmoutor^accounts_tmout$.
-
The output tells you the following:
-
The rule with full ID
xccdf_org.ssgproject.content_rule_accounts_tmoutwas tested in theOSPPprofile context. -
There were four test scenarios:
comment.fail.sh,line_not_there.fail.sh,correct_value.pass.shandwrong_value.fail.sh, all of which passed. These scenarios test whether the rule can handle various situations correctly. You examine these test scenarios later in this lab exercise. For now, it is important to realize that all of the scenarios should still pass after you make any changes in the OVAL. -
More information about the test run is available in the respective log directory. This is useful when a test breaks unexpectedly or the test suite suffers from internal issues.
Now when you have a reasonable amount of certainty about your rules, you can improve the OVAL content.
|
Tip
|
You repeat the (re)build of the content and subsequent test suite execution multiple times.
Therefore, it may be practical to dedicate a terminal window for this purpose.
You can browse the command history using Up and Down keyboard arrow keys, so if you want to rebuild after the test run finishes, tap the Up key until the build_product command shows up (typically you have to tap twice), and confirm the execution of the build command by pressing Enter.
|
In this section, you analyze the OVAL check for the accounts_tmout rule and perform the following steps:
-
Analyze the OVAL and identify duplicated elements.
-
Design a Jinja2 macro that deduplicates test definitions.
-
Test changes.
-
Design a Jinja2 macro that deduplicates test objects.
-
Test changes again.
The OVAL test repeats itself a bit—there are checks for the /etc/profile file as well as for other /etc/profile.d/*.sh files, but the tests and respective objects are very similar.
This makes editing tedious and prone to copy-paste errors.
Luckily, ComplianceAsCode supports the Jinja2 macro language that can be used to introduce templating, thus removing the duplication.
-
Analyze the difference between the two tests:
There is a difference in name and comment, and test objects are also different.
-
Compare the following two excerpts:
<ind:textfilecontent54_test check="all" check_existence="all_exist" comment="TMOUT in /etc/profile" id="test_etc_profile_tmout" version="1"> <ind:object object_ref="object_etc_profile_tmout" /> <ind:state state_ref="state_etc_profile_tmout" /> </ind:textfilecontent54_test> ... <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="TMOUT in /etc/profile.d/*.sh" id="test_etc_profiled_tmout" version="1"> <ind:object object_ref="object_etc_profiled_tmout" /> <ind:state state_ref="state_etc_profile_tmout" /> </ind:textfilecontent54_test> ...
-
You have etc_profile_tmout and etc_profiled_tmout (note the extra d) in the test ID and in the object reference.
Luckily, the Jinja2 language enables you to define macros that can help you to remove the duplication. You are going to define a macro that accepts the filename comment and the test stem as arguments.
Therefore, you remove both tests and add the new macro and its new invocations.
|
Tip
|
Next you edit files in the rule directory, and build and test the content. Therefore, it is advantageous to reserve one terminal window for builds and tests, and the other one for edits. |
|
Tip
|
To delete a text section in nano, move the cursor to the start of the text you want to select.
Press Alt+A to mark the start, then move the cursor to the end of the section you want to select.
Finally, press Ctrl+K to erase the selection.
Undo by pressing Alt+U, redo by pressing Alt+E.
Also remember that if you paste to the terminal, you have to press Ctrl+Shift+V.
|
-
Open the
oval/shared.xmlfile in the editor:[... lab5_oval]$ cd linux_os/guide/system/accounts/accounts-session/accounts_tmout [... accounts_tmout]$ nano oval/shared.xml
-
Now, delete the two
textfilecontent54_testXML elements, and then copy and paste the following content to replace it (between thedefinitionand the first of thetextfilecontent54_objectelements):{{% macro test_tmout(test_stem, files) %}} <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="TMOUT in {{{ files }}}" id="test_{{{ test_stem }}}" version="1"> <ind:object object_ref="object_{{{ test_stem }}}" /> <ind:state state_ref="state_etc_profile_tmout" /> </ind:textfilecontent54_test> {{% endmacro %}} {{{ test_tmout( test_stem="etc_profile_tmout", files="/etc/profile") }}} {{{ test_tmout( test_stem="etc_profiled_tmout", files="/etc/profile.d/*.sh") }}} -
Finish your edits as usual by pressing
Ctrl+Xand then enteringyto save and exit.NoteThe delimiters are different than the Jinja2 website shows—that is, instead of {% macro … %}, you use the{{% macro … %}}form and so on. There is always one curly bracket more than the website documentation shows.
So, did you do everything correctly?
-
Rebuild the datastream and execute the test suite again—the result should be exactly the same.
TIP:You can use the
Uparrow key to browse the command history so you do not have to retype them every time.[... accounts_tmout]$ cd /home/lab-user/labs/lab5_oval [... lab5_oval]$ ./build_product rhel7 ... [... lab5_oval]$ sudo python3 tests/test_suite.py rule --container ssg_test_suite --datastream build/ssg-rhel7-ds.xml accounts_tmout ... INFO - Logging into /home/lab-user/labs/lab5_oval/logs/... INFO - xccdf_org.ssgproject.content_rule_accounts_tmout INFO - Script comment.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script line_not_there.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script correct_value.pass.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script wrong_value.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK
Next, the test objects are very similar, as well—the only thing that differs is their name, and path + filename/filepath attributes.
So you define a macro that accepts the test name stem and path, filename, or filepath attributes.
You use the if-statement here—if, for example, filepath is not supplied, {{% if filepath %}} evaluates to False and the body of the condition is ignored.
Conversely, if the filepath is supplied, the textfilecontent54_object definition created by the macro includes the ind:filepath child element holding the respective value.
-
Open the
oval/shared.xmlfile in the editor, if it is not already open:[... lab5_oval]$ cd linux_os/guide/system/accounts/accounts-session/accounts_tmout [... accounts_tmout]$ nano oval/shared.xml
-
Remove the two
textfilecontent54_objectXML elements and then copy and paste the following block as a replacement (between the test creation and thetextfilecontent54_stateXML elements):{{% macro object_tmout(test_stem, path, filename, filepath) %}} <ind:textfilecontent54_object id="object_{{{ test_stem }}}" version="1"> {{% if path %}} <ind:path>{{{ path }}}</ind:path> {{% endif %}} {{% if filename %}} <ind:filename operation="pattern match">{{{ filename }}}</ind:filename> {{% endif %}} {{% if filepath %}} <ind:filepath>{{{ filepath }}}</ind:filepath> {{% endif %}} <ind:pattern operation="pattern match">^[\s]*TMOUT[\s]*=[\s]*(.*)[\s]*$</ind:pattern> <ind:instance datatype="int">1</ind:instance> </ind:textfilecontent54_object> {{% endmacro %}} {{{ object_tmout(test_stem="etc_profile_tmout", filepath="/etc/profile") }}} {{{ object_tmout(test_stem="etc_profiled_tmout", path="/etc/profile.d", filename="^.*\.sh$") }}} -
Finish your edits as usual by pressing
Ctrl+Xand then enteringy. -
To actually create tests and objects, macros have to be called. Therefore, do it and place the macro calls close to each other. Doing this emphasizes that there are two tests:
etc_profile_tmoutthat examines the single file andetc_profiled_tmoutthat goes through the whole directory. -
If you get errors during the build or during the tests and you do not know how to fix them, you are covered. The snippet below represents the OVAL file after performing the deduplication described in the previous section. To get back on track, copy and paste the text below to the
linux_os/guide/system/accounts/accounts-session/accounts_tmout/oval/shared.xmlfile.<def-group> <definition class="compliance" id="accounts_tmout" version="2"> <metadata> <title>Set Interactive Session Timeout</title> <affected family="unix"> <platform>multi_platform_rhel</platform> <platform>multi_platform_fedora</platform> <platform>multi_platform_ol</platform> </affected> <description>Checks interactive shell timeout</description> </metadata> <criteria operator="OR"> <criterion comment="TMOUT value in /etc/profile >= var_accounts_tmout" test_ref="test_etc_profile_tmout" /> <criterion comment="TMOUT value in /etc/profile.d/*.sh >= var_accounts_tmout" test_ref="test_etc_profiled_tmout" /> </criteria> </definition> {{% macro test_tmout(test_stem, files) %}} <ind:textfilecontent54_test check="all" check_existence="all_exist" comment="TMOUT in {{{ files }}}" id="test_{{{ test_stem }}}" version="1"> <ind:object object_ref="object_{{{ test_stem }}}" /> <ind:state state_ref="state_etc_profile_tmout" /> </ind:textfilecontent54_test> {{% endmacro %}} {{{ test_tmout( test_stem="etc_profile_tmout", files="/etc/profile") }}} {{{ test_tmout( test_stem="etc_profiled_tmout", files="/etc/profile.d/*.sh") }}} {{% macro object_tmout(test_stem, path, filename, filepath) %}} <ind:textfilecontent54_object id="object_{{{ test_stem }}}" version="1"> {{% if path %}} <ind:path>{{{ path }}}</ind:path> {{% endif %}} {{% if filename %}} <ind:filename operation="pattern match">{{{ filename }}}</ind:filename> {{% endif %}} {{% if filepath %}} <ind:filepath>{{{ filepath }}}</ind:filepath> {{% endif %}} <ind:pattern operation="pattern match">^[\s]*TMOUT[\s]*=[\s]*(.*)[\s]*$</ind:pattern> <ind:instance datatype="int">1</ind:instance> </ind:textfilecontent54_object> {{% endmacro %}} {{{ object_tmout(test_stem="etc_profile_tmout", filepath="/etc/profile") }}} {{{ object_tmout(test_stem="etc_profiled_tmout", path="/etc/profile.d", filename="^.*\.sh$") }}} <ind:textfilecontent54_state id="state_etc_profile_tmout" version="1"> <ind:subexpression datatype="int" operation="equals" var_check="all" var_ref="var_accounts_tmout" /> </ind:textfilecontent54_state> <external_variable comment="external variable for TMOUT" datatype="int" id="var_accounts_tmout" version="1" /> </def-group>
This way, you do not have to worry about possibly introducing those copy-paste errors.
-
Finally, run the rule’s test again—it may be that a typo was introduced, so the OVAL is not actually correct:
|
Tip
|
You do not need to specify the parameter --datastream when there is only one datastream built, so our command this time is shorter.
|
+
[... accounts_tmout]$ cd /home/lab-user/labs/lab5_oval [... lab5_oval]$ ./build_product rhel7 ... [... lab5_oval]$ sudo python3 tests/test_suite.py rule --container ssg_test_suite accounts_tmout ... INFO - Logging into /home/lab-user/labs/lab5_oval/logs/... INFO - xccdf_org.ssgproject.content_rule_accounts_tmout INFO - Script comment.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script line_not_there.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script correct_value.pass.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script wrong_value.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK
+ As there are no errors, this proves that your check-remediation combination works as expected.
-
Examine the test scenarios—for example, the
wrong_value.fail.shscenario.-
Open a new terminal window, and change to the test definitions directory. Tests reside within the same directory as the
rule.yml:[... lab5_oval]$ cd linux_os/guide/system/accounts/accounts-session/accounts_tmout/tests
-
Open the
wrong_value.fail.shfile:[... tests]$ nano wrong_value.fail.sh
As you can see, the test sets the
TMOUTvalue to 1234. The value is correctly considered to be noncompliant—the timeout should be 600, and 1234 is longer and therefore less secure. -
After you finish looking, press
Ctrl+Xto bring up the "save and exit" option. If you are asked about saving any changes, you probably do not want that, so entern. -
What about the
correct_value.pass.shscenario? Open it in the editor, as well:[... tests]$ nano correct_value.pass.sh
As you can see, this one sets the
TMOUTvalue to 600, which is the value defined by the profile. -
After you finish looking, press
Ctrl+Xto bring up the "save and exit" option. If you are asked about saving any changes, you probably do not want that, so entern.
-
-
Add another check for a correct value—check for a timeout of 100. In the case of a timeout, 100 seconds is more secure than 600 seconds. Therefore, the scenario represents a supercompliant case, that is, the setting is stricter than necessary, but it is within the area of allowed values.
-
Copy that one, and make a new test scenario out of it. Run this command in the terminal in the
testsdirectory:[... tests]$ cp correct_value.pass.sh supercompliant.pass.sh
-
Then, open it in the
nanoeditor, and change the value from 600 to 100.[... tests]$ nano supercompliant.pass.sh
-
After you finish editing, press
Ctrl+X, then enteryto save and exit. For reference, thesupercompliant.pass.shfile now looks like this:#!/bin/bash # # profiles = xccdf_org.ssgproject.content_profile_ospp if grep -q "TMOUT" /etc/profile; then sed -i "s/.*TMOUT.*/TMOUT=100/" /etc/profile else echo "TMOUT=100" >> /etc/profile fi
-
-
Now go back to the tests and run them:
[... tests]$ cd /home/lab-user/labs/lab5_oval [... lab5_oval]$ ./build_product rhel7 ... [... lab5_oval]$ sudo python3 tests/test_suite.py rule --container ssg_test_suite accounts_tmout ... INFO - Logging into /home/lab-user/labs/lab5_oval/logs/... INFO - xccdf_org.ssgproject.content_rule_accounts_tmout INFO - Script correct_value.pass.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script comment.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK ERROR - Script supercompliant.pass.sh using profile xccdf_org.ssgproject.content_profile_ospp found issue: ERROR - Rule evaluation resulted in fail, instead of expected pass during initial stage ERROR - The initial scan failed for rule 'xccdf_org.ssgproject.content_rule_accounts_tmout'. INFO - Script line_not_there.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script wrong_value.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK
The test output tells you that the
supercompliant.pass.shscenario has failed, which was not expected. -
Modify the OVAL snippet, so timeouts shorter than the threshold are allowed:
[... lab5_oval]$ cd linux_os/guide/system/accounts/accounts-session/accounts_tmout [... accounts_tmout]$ nano oval/shared.xml
-
The modification should be easy—instead of checking that the timeout value
equalsthe threshold, you use theless than or equalcheck as per the OVAL specification. So just replaceequalswithless than or equalin the definition of thetextfilecontent54_statelike this:<ind:textfilecontent54_state id="state_etc_profile_tmout" version="1"> <ind:subexpression datatype="int" operation="less than or equal" var_check="all" var_ref="var_accounts_tmout" /> </ind:textfilecontent54_state>
-
After you are finished editing, press
Ctrl+X, then enteryto save and exit. This time, when rebuilt and executed again, the tests pass:[... accounts_tmout]$ cd /home/lab-user/labs/lab5_oval [... lab5_oval]$ ./build_product rhel7 ... [... lab5_oval]$ sudo python3 tests/test_suite.py rule --container ssg_test_suite accounts_tmout INFO - The base image option has been specified, choosing Podman-based test environment. INFO - Logging into /home/lab-user/labs/lab5_oval/logs/... INFO - xccdf_org.ssgproject.content_rule_accounts_tmout INFO - Script comment.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script line_not_there.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script correct_value.pass.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script supercompliant.pass.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script wrong_value.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK
As discussed at the beginning of this exercise, the TMOUT variable can be prefixed by the export keyword—this is allowed, but not required.
-
Modify the passing
correct_value.pass.shtest scenario to test a correct value in addition to the usage of theexportkeyword:[... lab5_oval]$ nano linux_os/guide/system/accounts/accounts-session/accounts_tmout/tests/correct_value.pass.sh
#!/bin/bash # # profiles = xccdf_org.ssgproject.content_profile_ospp if grep -q "TMOUT" /etc/profile; then sed -i "s/.*TMOUT.*/export TMOUT=600/" /etc/profile else echo "export TMOUT=600" >> /etc/profile fi
-
After you are finished editing, press
Ctrl+X, then enteryto save and exit. -
It is time to rerun those tests. You do not have to rebuild the product, as you have changed only the test definition, and you can rerun the test suite without the prior rebuild. Execute the test suite again and expect the
Script correct_value.pass.sh using profile xccdf_org.ssgproject.content_profile_ospp found issue:line to appear in the output.[... lab5_oval]$ sudo python3 tests/test_suite.py rule --container ssg_test_suite accounts_tmout ...
This confirms the theory that OVAL does not allow this configuration, although it is valid. Therefore, in order to make tests pass, you have to edit the OVAL so that the occurrence of
exportis allowed. Thanks to the OVAL optimization that you performed earlier, there is only one place that needs to be changed—the definition of the test object. -
Open the OVAL file again:
[... lab5_oval]$ cd linux_os/guide/system/accounts/accounts-session/accounts_tmout [... accounts_tmout]$ nano oval/shared.xml
-
Note that the current test object specifies the following:
<ind:pattern operation="pattern match">^[\s]*TMOUT[\s]*=[\s]*(.*)[\s]*$</ind:pattern> <ind:instance datatype="int">1</ind:instance>
It needs to be changed to ignore the
exportkeyword followed by at least one whitespace. -
The best approach is to make this an optional group. This means adding
(export[\s])?` to the regular expression, but as you do not want that group to be registered (stored in memory or captured), you have to link:https://oval.mitre.org/language/about/re_support_5.6.html[add some special syntax^]. Add `(?:export[\s])and the section becomes this:<ind:pattern operation="pattern match">^[\s]*(?:export[\s]+)?TMOUT[\s]*=[\s]*(.*)[\s]*$</ind:pattern> <ind:instance datatype="int">1</ind:instance>
The non-capturing group that consists of
exportfollowed by at least one whitespace can be either absent or present exactly once. -
It is time to save the OVAL. Press
Ctrl+X, then enteryto save and exit, and then rebuild the product and run the tests again:[... accounts_tmout]$ cd /home/lab-user/labs/lab5_oval [... lab5_oval]$ ./build_product rhel7 ... [... lab5_oval]$ sudo python3 tests/test_suite.py rule --container ssg_test_suite accounts_tmout INFO - The base image option has been specified, choosing Podman-based test environment. INFO - Logging into /home/lab-user/labs/lab5_oval/logs/... INFO - xccdf_org.ssgproject.content_rule_accounts_tmout INFO - Script comment.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script line_not_there.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script correct_value.pass.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script wrong_value.fail.sh using profile xccdf_org.ssgproject.content_profile_ospp OK INFO - Script supercompliant.pass.sh using profile xccdf_org.ssgproject.content_profile_ospp OK
Everything passes, which means that your check can now handle a range of compliant values and it does not produce false positives when the
exportkeyword is involved.
Congratulations—now you know how to use the ComplianceAsCode project to make OVAL creation less error-prone and how to make sure that OVAL checks are working according to expectations.
