Red Hat Security: Linux in Physical, Virtual and Cloud (RH415)
Red Hat Security: Linux in Physical, Virtual and Cloud (RH415)
# SECURING SERVICES:
ss -tlw # open ports in the listening state
# CUSTOMIZING YOUR SSH SERVICE CONFIGURATION:
vi /etc/ssh/sshd_config
PermitRootLogin no # do not allow root to SSH to this machine
PasswordAuthentication no # force only key-based authentication
# ALLOW/DENY USERS & GROUPS:
# The allow/deny directives are processed in the following order: DenyUsers, AllowUsers, DenyGroups, and
finally AllowGroups
AllowUsers root@192.168.22.* # this would need PermitRootLogin yes
AllowUsers James@host1.example.com
AllowUsers root@192.168.22.0/24 James@host1.example.com
# If all of the criteria on the Match line are satisfied, the keywords on the following lines override
those set in the global section of the config file, until either another Match line or the end
of the file. The available criteria are User, Group, Host, LocalAddress, LocalPort, and Address:
Match Address 192.168.0.? # 192.168.0.[0-9] network range
PermitRootLogin yes
systemctl reload sshd
# SUDO:
su # switches to the target user (which is root by
default), but provides a normal shell with the same environment as the user who invoked the su command
su - # switches to the target user and invokes a
login shell based on the target user’s environment. A login shell resets most environment variables,
including the target user’s PATH
visudo
vi /etc/sudoers
Defaults timestamp_timeout = 1 # require password every minute (0 = every time
it’s used)
User_Alias FULLTIMERS = millert, mikef, dowdy
Runas_Alias OP = root, operator
Host_Alias SERVERS = master, mail, www, ns
Cmnd_Alias REBOOT = /usr/sbin/reboot
root ALL = (ALL) ALL # who where = (as_whom) what
%wheel ALL = (ALL) ALL # we let any user in group wheel run any
command on any host as any user
FULLTIMERS ALL = NOPASSWD: ALL
lisa SERVERS = ALL
bob SERVERS = (OP) ALL : 128.138.242.0 = (OP) REBOOT # the user bob may run anything on the SERVERS
and can run reboot on 128.138.242.0 machines as any user listed in the OP Runas_Alias (root and
operator)
sudo # resets the PATH variable based on the
’secure_path’ directive in the /etc/sudoers file
sudo -i # changes to the root user’s home directory and
opens an interactive login shell based on the root user’s environment variables
2. Automating Configuration & Remediation with Ansible
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
yum install clevis clevis-luks clevis-dracut # Clevis clients reach out to tang servers
clevis luks bind -d /dev/vda1 tang ’{"url":"http://demotang.lab.example.com"}’
luksmeta show -d /dev/vda1 # verify that Clevis key was placed in LUKS
header
dracut -f # enable Dracut to unlock encrypted partitions
using NBDS
systemctl enable clevis-luks-askpass.path # when decrypting non-root file system
# SSS policy which defines three Tang servers, and requires at least two of them to be available for
automatic decryption to occur
cfg=$’{"t":2,"pins":{"tang":[\n
> {"url":"http://demotang1.lab.example.com"},\n
> {"url":"http://demotang2.lab.example.com"},\n
> {"url":"http://demotang3.lab.example.com"}]}}’
clevis luks bind -d /dev/vdb1 sss "$cfg"
# USBGUARD:
yum -y install usbguard
yum -y install usbutils udisks2 # provides lsusb, udisksctl
usbguard <list-devices|allow-device id|block-device id|reject-device id|list-rules|append-rule
rule|remove-rule id|generate-policy>
systemctl enable usbguard --now
usbguard generate-policy > /etc/usbguard/rules.conf # authorizes the currently connected USB devices
systemctl restart usbguard
usbguard list-rules
# Rule output example:
1: allow id 1d6b:0002 serial "0000:00:04.7" name "EHCI Host Controller"
hash "CsKOZ6IY8v3eojsc1fqKDW84V+MMhD6HsjjojcZBjSg=" parent-hash
"qiR4Ubbd7AIXLCz201hJYzaO9KIrOvqqRgqs2vM2NOY=" with-interface 09:00:00
usbguard list-devices
usbguard list-rules
usbguard watch # watch terminal for IPC activity
# PREVENTING A DEVICE FROM INTERACTING WITH THE SYSTEM, WHITE/BLACKLISTING:
usbguard block-device <ID> # set its rule target to block
usbguard list-devices --blocked
usbguard reject-device <ID> # set its rule target to reject
usbguard generate-policy -X -t reject \
> /etc/usbguard/rules.conf # generate a new base policy with a reject rule
target that will ignore any additional USB devices that’ll try to interact with the system
grep usbguard /etc/group # ’groupadd usbguard’ & ’usermod -aG usbguard
richard’ if needed
vi /etc/usbguard/usbguard-daemon.conf
RuleFile=/etc/usbguard/rules.conf # do not edit this file directly, but rather
elsewhere and then move it here
IPCAccessControlFiles=/etc/usbguard/IPCAccessControl.d/
IPCAllowedGroups=usbguard
usbguard add-user -g usbguard --devices=modify,list,listen --policy=list --exceptions=listen
# RULE OPTIONS:
allow/reject name <DEVICE_NAME> serial <SER_NUM> via-port <PORT_ID> hash <HASH> with-interface
<INTERFACE_TYPE>
# RULE OPERATORS (via-port <OPERATOR> {...}, with-interface <OPERATOR> {...}):
all-of # must contain all specified values to match
one-of # must contain at least one
none-of # must not contain any
equals # must contain exactly the same
equals-ordered # must contain exactly the same also in the
same order
# RULE CONDITIONS:
localtime(time_range) # true if local time is in the range
allowed-matches(query) # true if device matches query
rule-applied # true if rule currently being evaluated ever
matched device before
rule-applied(past_duration) # same as above, but if it matched devce in the
past duration of time
rule-evaluated # true if was ever evaluated before
rule-evaluated(past_duration) # same as above, but if it was evaluated in the
past duration of time
random # probability is 0.5 by default, can be changed
by p_true
true
false
# SETTING SYSTEM CALL RULES: # when Audit starts, it assigns an Audit UID of
4294967295 to any existing process (-F auid!=4294967295)
auditctl -l # list the current rules
auditctl -s # current status of audit
auditctl -a exit,always -F arch=b32 -F auid>=500 -S rename\ # audit the 32-bit version of both the rename
and renameat system call for all users whose original Audit user ID is equal to or greater than 500
-S renameat -F subj_type!=mysqld_t -k rename # do not trigger the Audit rule if the process
is under the mysqld_t SELinux domain, and add the rename key to the logs
auditctl -a exit,always -F dir=/home/ -F uid=0\ # recursively audit every file system access by
the root user under the /home directory to files or directories not owned by the original user that is
now working as root
-C auid!=obj_uid
auditctl -e 2 # set the currently loaded rules to be
immutable, the rules cannot be changed again until the system is rebooted, must be last rule
# SELECTION LINES:
/etc PERMS # regular, regular expression recursively
=/testidr PERMS # equals, regular expression non-recursively
!/etc/mtab # negative, regular expression of what files or
directories not to monitor
# MACRO LINES:
@@define DBDIR /var/lib/aide # variable definition
database=file:@@{DBDIR}/aide.db.gz # variable expansion, sets the database
parameter to the value file:/var/lib/aide/aide.db.gz
# CREATING CUSTOM POLICY MODULES: # before using audit2allow, and to collect all
the denials in one operation, put SELinux in permissive mode
audit2allow -a # generate a policy module for you by analyzing
the denials in the audit.log file, print the rules to allow the access
audit2allow -a -M mymodule # generate a new SELinux policy module, add the
-M modulename option to the previous command
semodule -i mymodule.pp # persistently load the new module in SELinux
# SCAP WORKBENCH:
yum install scap-workbench # GUI tool, it also installs scap-security-guide
# GENERATE A REMEDIATION ANSIBLE PLAYBOOK: # it only includes the tasks to remediate the
failed checks
oscap xccdf generate fix --profile xccdf_org.ssgproject.content_profile_pci-dss \
--fix-type ansible /usr/share/xml/scap/ssg/content/ssg-rhel7-ds.xml > pci-dss.yml # remediation from
profile
oscap xccdf generate fix --profile xccdf_org.ssgproject.content_profile_pci-dss \
--fix-type ansible --result-id "" /root/results.xml > remediation-playbook.yml # remediation from
results
# The ’bootstrap.py’ script provided by the Satellite Server is used to register a system as both a host
and a content host
wget https://satellite.lab.example.com/pub/bootstrap.py --no-check-certificate
chmod a+x bootstrap.py
./bootstrap.py -l admin -s satellite.lab.example.com -o ’org-example’ -L ’Default Location’ -a serverkey -g
org-hostgroup1 --force
vi /etc/foreman_scap_client/config.yaml # contains policy information to be applied on
the host
:server: ’satellite.lab.example.com’
:port: 9090
1:
:profile: ’xccdf_org.ssgproject.content_profile_common’
rpm -qa | grep -E ’foreman_scap|openscap’
vi /etc/cron.d/foreman_scap_client_cron
puppet agent --test --verbose # Puppet agent ensures that the compliance
policy is correctly configured on the host
# RUNNING AN OPENSCAP SCAN ON A CLIENT: # assumes the Puppet is configured with the
above module
foreman_scap_client 1 # scan, archives the scan results, and uploads
the results to the Satellite Server