TIPS & TRICKS
To search the contents of the man pages
# man -k searchterm |
SELinux task examples
To keep in mind.
Context:
# cat /etc/selinux/config // Edit context - permanent |
Web site:
# vim /etc/httpd/conf/httpd.conf // Change document root if needed |
# man semanage-fcontext |
Users directories (non standard directories):
# vim /etc/httpd/conf/httpd.conf // Uncomment "UserDir public_html" and comment "UserDir disabled" |
$ mkdir /home/aline/public_html |
# getsebool -a | grep homedirs |
Specific problem:
# ausearch -m AVC -ts today p // Récentes violations d’accès |
See: http://n0tes.fr/2024/10/30/RH-Gestion-de-la-securite-avec-SELinux/
Configure SELinux 1
| Enable SELinux in enforcing mode
Checks status of SELinux:
# getenforce Checks status of SELinux |
Add it permanemtly by modify the value in ‘/etc/selinux/config’:
# vim /etc/selinux/config |
# systemctl reboot |
After reboot, rechecks status of SELinux:
# getenforce (checks status of SELinux) |
Configure SELinux 2
| Assurez-vous que SELinux est en mode enforcing
| Configurez un contexte SELinux pour permettre à Apache d’accéder à un répertoire /webdata
.
# setenforce 1 |
# vim /etc/selinux/config |
Accès pour apache au répertoire:
# chown -R apache:apache /webdata |
Modifier le contexte pour Apache (qui utilise httpd_sys_content_t):
# semanage fcontext -a -t httpd_sys_content_t "/webdata(/.*)?" |
Note : # chcon -R -t httpd_sys_content_t /webdata
est temporaire
Voir MAN semanage-fcontext
Configure SELinux 2
| Assign the same SELinux contexts used by the home directories to the /xfs directory permanently.
Check the home directory context with the z option:
# ls -Z /home |
# semanage fcontext -a -t user_home_dir_t "/xfs(/.*)?" |
Voir MAN semanage-fcontext
Configure SELinux 3
| Your webcontent has been configured at port 82 in the /var/www/html directory.
| (Don’t alter or remove any files in this directory)
| Make the content accessible.
# yum install httpd -y // These first 3 steps are to set up the question |
# cat /var/www/html/index.html // Hello World! |
# systemctl status httpd |
Check whether port 82 is enabled or if not, use the command below to add it:
# semanage port -l | grep http // indicate http_port_t for other web port |
# systemctl enable httpd |
You need to configure firewal:
# firewall-cmd --add-port=82/tcp --permanent |
# curl http://servera.lab.example.com:82 |
Configure SELinux 4
| A HTTP web server running on non standard port 82 is having issues serving content.
| Debug and fix the issues.
| - The web server on your system can server all the existing HTML files from /var/www/html
| ( NOTE: Do not make any changes to these files ).
| - Web service should automatically start at boot time.
Find the sealert messages
# grep -i sealert /var/log/messages |
Copy the command from the logs
# seleart -l <HASH_CODE> |
Run the suggested command given in the message of the above command, for example:
# setsebool -P httpd_unified 1 |
Finally enable httpd service
# systemctl enable --now httpd |
Configure SELinux 5
| Adjust the context of port 2332 to xen_port_t using the tcp protocol.
# semanage port -l | grep 2332 |
Configure SELinux 6
| Install Apache and allow it to get documents from NFS mounted folder
First step is to install Apache using DNF. It does not require special treatment - just type the
# dnf install -y httpd |
Always when installing service/software which interacts with network it is crucial to keep in mind configuring firewall to enable incoming connections for this service. Therefore the commands used:
notice the ‘-permament’ option (in order to save rule to survive during reboots)
# firewall-cmd –permanent –add-service=http |
Besides firewall configuration for network-interacting services for all services being installed in the system remember to enable it (to autostart after reboot) and also starting it up right after the installation (services usually does not autostart as a part of installation process):
# systemctl enable httpd |
SELinux at the end of the exam should be enabled and set to enforcing mode. Therefore always pay attention to this aspect of system configuration. The usual problem for using SELinux is to find out what rule should be used.
One easy way to tell which SELinux related configuration has to be done, is through sealert command. This command is used to diagnose SELinux denials and attempts to provide user friendly explanations for a SELinux denial and recommendations for how one might adjust the system to prevent the denial in the future.
So use this command to analyze Selinux denials log /var/log/audit/audit.log
# sealert -a /var/log/audit/audit.log |
The output suggest to adjust the following boolean setting:
***** Plugin catchall_boolean (47.5 confidence) suggests ****************** |
After executing setsebool -P httpd_use_nfs 1 Apache will be allowed to get documents from NFS mounted folder
Configure SELinux 7
| Create a directory hierarchy /dir1/dir2/dir3/dir4
| Apply SELinux contexts for /etc on it recursively.
# mkdir -p /dir1/dir2/dir3/dir4 |
Configure SELinux 8
| Create a directory sedir1 under /tmp and a file sefile1 under sedir1.
| Check the context on the directory and file.
| Change the SELinux user and type to user_u and public_content_t on both and verify
# mkdir /tmp/sedir1 |
Configure SELinux 10
| Add a non-standard port 8010 to the SELinux policy database for the httpd service and confirm the addition.
| Remove the port from the policy and verify the deletion
# semanage port -a -t http_port_t -p tcp 8010 |
Configure SELinux 11
| Display the current state of the Boolean nfs_export_all_rw.
| Toggle its value persistently after the system has been reboot
# getsebool -a | grep nfs_export_all_rw |
Configure SELinux 12
| A web server running on a non-standard port 82 is having trouble serving content.
| Debug and resolve the issue as necessary so that the following conditions are met:
| - The web server on the system is able to serve all existing HTML files in /var/www/html
| (note: do not delete or otherwise alter the contents of existing files)
| - The web server serves this content on port 82
| - The web server starts automatically at system startup
View httpd service status
# systemctl status httpd |
View the security context of the HTML file (prints the security context of the file):
# ls -laZ /var/www/html/* |
Modify the security context of the original /var/www/html/
directory:
# man semange fcontext |
Refresh the security context
# restorecon -R -v /var/www/html/ |
Use semanage to release port 82
# man semanage port |
Check whether port 82 is allowed
# semanage port -l | grep http |
Restart the httpd service, set the boot to start automatically, and check whether the service is enabled
# systemctl restart httpd |
Access verification
# curl http://172.25.250.100:82/file{1..3} |
Documentations
Internet
Git
ChatGPT