cn:ccr:formazione:centos7:2018-11:selinux-soluzioni
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cn:ccr:formazione:centos7:2018-11:selinux-soluzioni [2018/11/28 19:58] – brunengo@infn.it | cn:ccr:formazione:centos7:2018-11:selinux-soluzioni [2018/11/29 14:04] (current) – brunengo@infn.it | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== SELinux - soluzioni ====== | ||
+ | |||
+ | ==== Esercitazione 1 ==== | ||
+ | |||
+ | - Visualizzazione dello stato di SELinux< | ||
+ | - Visualizzazione dello stato di SELinux < | ||
+ | [root@statichostname ~]# sestatus | ||
+ | SELinux status: | ||
+ | SELinuxfs mount: | ||
+ | SELinux root directory: | ||
+ | Loaded policy name: | ||
+ | Current mode: | ||
+ | Mode from config file: enforcing | ||
+ | Policy MLS status: | ||
+ | Policy deny_unknown status: | ||
+ | Max kernel policy version: | ||
+ | [root@statichostname ~]# getenforce | ||
+ | Enforcing | ||
+ | </ | ||
+ | - Visualizzazione del context dei processi di systemd e cron< | ||
+ | [root@statichostname ~]# ps -e -Z | grep -e systemd -e cron | ||
+ | system_u: | ||
+ | system_u: | ||
+ | system_u: | ||
+ | system_u: | ||
+ | system_u: | ||
+ | system_u: | ||
+ | </ | ||
+ | - Visualizzazione del context dei file / | ||
+ | [root@statichostname ~]# ls -Z -d /etc/passwd /etc/shadow /etc/cron.d / | ||
+ | drwxr-xr-x. root root system_u: | ||
+ | -rw-------. root root system_u: | ||
+ | -rw-r--r--. root root system_u: | ||
+ | ----------. root root system_u: | ||
+ | [root@statichostname ~]# getfattr -d -m " | ||
+ | getfattr: Removing leading '/' | ||
+ | # file: etc/passwd | ||
+ | security.selinux=" | ||
+ | |||
+ | # file: etc/shadow | ||
+ | security.selinux=" | ||
+ | |||
+ | </ | ||
+ | - Visualizzazione degli SELinux user e associazione con role e level< | ||
+ | [root@statichostname ~]# semanage user -l | ||
+ | |||
+ | Labeling | ||
+ | SELinux User Prefix | ||
+ | |||
+ | guest_u | ||
+ | root user | ||
+ | staff_u | ||
+ | sysadm_u | ||
+ | system_u | ||
+ | unconfined_u | ||
+ | user_u | ||
+ | xguest_u | ||
+ | </ | ||
+ | - Visualizzazione della associazione degli user agli SELinux user< | ||
+ | [root@statichostname ~]# semanage login -l | ||
+ | |||
+ | Login Name | ||
+ | |||
+ | __default__ | ||
+ | root | ||
+ | system_u | ||
+ | </ | ||
+ | - Visualizzare i context di httpd e della sua DocumentRoot ed identificare la rule che permette l' | ||
+ | |||
+ | # Far partire httpd e visualizzare il context del processo | ||
+ | |||
+ | [root@statichostname ~]# systemctl start httpd | ||
+ | [root@statichostname ~]# ps -e -Z | grep httpd | ||
+ | system_u: | ||
+ | system_u: | ||
+ | ... | ||
+ | |||
+ | # visualizzare il context della attuale document root (creare anche una index.html vuota) | ||
+ | |||
+ | [root@statichostname ~]# ls -lZd / | ||
+ | drwxr-xr-x. root root system_u: | ||
+ | [root@statichostname ~]# touch / | ||
+ | [root@statichostname ~]# ls -lZd / | ||
+ | -rw-r--r--. root root unconfined_u: | ||
+ | |||
+ | # cercare con sesearch la regola che permette a httpd di leggere i file della DocumentRoot | ||
+ | |||
+ | [root@statichostname ~]# sesearch --allow --source httpd_t --target httpd_sys_content_t --class file | ||
+ | Found 5 semantic av rules: | ||
+ | allow httpd_t httpd_content_type : file { ioctl read getattr lock open } ; | ||
+ | -> allow httpd_t httpd_sys_content_t : file { ioctl read getattr lock open } ; | ||
+ | allow httpd_t httpdcontent : file { ioctl read write create getattr setattr lock append unlink link rename open } ; | ||
+ | allow httpd_t httpdcontent : file { read getattr execute open } ; | ||
+ | allow httpd_t httpd_content_type : file { ioctl read getattr lock open } ; | ||
+ | |||
+ | </ | ||
+ | - Verificare che cambiando il context type di / | ||
+ | [root@statichostname ~]# chcon -t var_t / | ||
+ | |||
+ | [root@statichostname ~]# ls -Z / | ||
+ | -rw-r--r--. root root unconfined_u: | ||
+ | |||
+ | [root@statichostname ~]# wget http:// | ||
+ | --2018-11-28 05: | ||
+ | Connecting to 127.0.0.1: | ||
+ | HTTP request sent, awaiting response... 403 Forbidden | ||
+ | 2018-11-28 05:49:52 ERROR 403: Forbidden. | ||
+ | |||
+ | [root@statichostname ~]# restorecon / | ||
+ | [root@statichostname ~]# ls -Z / | ||
+ | -rw-r--r--. root root unconfined_u: | ||
+ | [root@statichostname ~]# wget http:// | ||
+ | --2018-11-28 06: | ||
+ | Connecting to 127.0.0.1: | ||
+ | HTTP request sent, awaiting response... 200 OK | ||
+ | Length: 0 [text/html] | ||
+ | Saving to: ‘index.html’ | ||
+ | |||
+ | [ < | ||
+ | |||
+ | 2018-11-28 06:00:52 (0.00 B/s) - ‘index.html’ saved [0/0] | ||
+ | </ | ||
+ | - Spostare la DocumentRoot di httpd in /www/html\\ Per fare questo si deve: modificare la configurazione di httpd, creare la nuova document root, verificare che non funziona, assegnare il default context (semanage fcontext), e modificare il context (restorecon)< | ||
+ | |||
+ | # creare le directory | ||
+ | |||
+ | [root@statichostname ~]# mkdir -p /www/html | ||
+ | [root@statichostname ~]# ls -lZd /www/html/ | ||
+ | drwxr-xr-x. root root unconfined_u: | ||
+ | |||
+ | # default_t e' il default type se non ci sono configurazioni apposite | ||
+ | |||
+ | [root@statichostname ~]# cp / | ||
+ | [root@statichostname ~]# ls -Z / | ||
+ | -rw-r--r--. root root unconfined_u: | ||
+ | |||
+ | # modificare la configurazione di httpd | ||
+ | |||
+ | [root@statichostname ~]# vi / | ||
+ | [root@statichostname ~]# grep ^DocumentRoot -A15 / | ||
+ | DocumentRoot "/ | ||
+ | |||
+ | # | ||
+ | # Relax access to content within /var/www. | ||
+ | # | ||
+ | #< | ||
+ | # AllowOverride None | ||
+ | # # Allow open access: | ||
+ | # Require all granted | ||
+ | #</ | ||
+ | < | ||
+ | AllowOverride None | ||
+ | # Allow open access: | ||
+ | Require all granted | ||
+ | </ | ||
+ | |||
+ | [root@statichostname ~]# systemctl reload httpd | ||
+ | |||
+ | # verificare che l' | ||
+ | |||
+ | [root@statichostname ~]# wget http:// | ||
+ | --2018-11-28 06: | ||
+ | Connecting to 127.0.0.1: | ||
+ | HTTP request sent, awaiting response... 403 Forbidden | ||
+ | 2018-11-28 06:14:37 ERROR 403: Forbidden. | ||
+ | |||
+ | # identificare tramite matchpathconf quale type devono avere i file della DocumentRoot | ||
+ | |||
+ | [root@statichostname ~]# matchpathcon -m file / | ||
+ | / | ||
+ | |||
+ | # utilizzare semanage per modificare il context di configurazione della nuova document root | ||
+ | |||
+ | [root@statichostname ~]# semanage fcontext --add --type httpd_sys_content_t "/ | ||
+ | [root@statichostname ~]# semanage fcontext --add --type httpd_sys_content_t "/ | ||
+ | |||
+ | # verificare che le entry aggiunte compaiono nel file di configuraizone (locale) file_context.local | ||
+ | |||
+ | [root@statichostname ~]# cat / | ||
+ | # This file is auto-generated by libsemanage | ||
+ | # Do not edit directly. | ||
+ | |||
+ | / | ||
+ | / | ||
+ | |||
+ | # restorare il context della nuova documento root al type della configurazione | ||
+ | |||
+ | [root@statichostname ~]# restorecon -Rv /www | ||
+ | restorecon reset /www context unconfined_u: | ||
+ | restorecon reset /www/html context unconfined_u: | ||
+ | restorecon reset / | ||
+ | |||
+ | [root@statichostname ~]# ls -Z / | ||
+ | -rw-r--r--. root root unconfined_u: | ||
+ | |||
+ | # verificare che l' | ||
+ | |||
+ | [root@statichostname ~]# wget http:// | ||
+ | --2018-11-28 06: | ||
+ | Connecting to 127.0.0.1: | ||
+ | HTTP request sent, awaiting response... 200 OK | ||
+ | Length: 0 [text/html] | ||
+ | Saving to: ‘index.html.1’ | ||
+ | |||
+ | [ < | ||
+ | |||
+ | 2018-11-28 06:42:31 (0.00 B/s) - ‘index.html.1’ saved [0/0] | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Esercitazione 2 ==== | ||
+ | |||
+ | - Utilizzare seinfo per visualizzare l' | ||
+ | [root@statichostname ~]# seinfo -t | grep init_t | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | -> | ||
+ | | ||
+ | [root@statichostname ~]# seinfo -tinit_t -x | ||
+ | | ||
+ | ... | ||
+ | domain | ||
+ | ... | ||
+ | </ | ||
+ | - Utilizzare seinfo per visualizzare l' | ||
+ | [root@statichostname ~]# seinfo -adomain | ||
+ | | ||
+ | [root@statichostname ~]# seinfo -adomain -x | grep init_t | ||
+ | cloud_init_t | ||
+ | -> init_t | ||
+ | namespace_init_t | ||
+ | run_init_t | ||
+ | </ | ||
+ | - Domain transition: identificare le policy che permettono la domain transition da systemd a httpd | ||
+ | - Visualizzare domain e type di systemd, httpd, / | ||
+ | root@statichostname ~]# ps -eZ | grep systemd$ | ||
+ | system_u: | ||
+ | [root@statichostname ~]# ls -Z / | ||
+ | -rwxr-xr-x. root root system_u: | ||
+ | [root@statichostname ~]# ps -eZ | grep httpd | head -n 1 | ||
+ | system_u: | ||
+ | </ | ||
+ | - Verificare l' | ||
+ | [root@statichostname ~]# sesearch -s init_t -t httpd_exec_t -c file -A | ||
+ | Found 3 semantic av rules: | ||
+ | ... | ||
+ | allow initrc_domain direct_init_entry : file { read getattr execute open } ; | ||
+ | ... | ||
+ | |||
+ | # La regola e' permessa perche' | ||
+ | |||
+ | [root@statichostname ~]# seinfo -ainitrc_domain -x | grep init_t | ||
+ | init_t | ||
+ | e perche' | ||
+ | [root@statichostname ~]# seinfo -adirect_init_entry -x | grep http | ||
+ | httpd_exec_t | ||
+ | httpd_rotatelogs_exec_t | ||
+ | </ | ||
+ | - Verificare che esista una rule che definisca che il type dell' | ||
+ | [root@statichostname ~]# sesearch -s httpd_t -p entrypoint -A | ||
+ | Found 3 semantic av rules: | ||
+ | allow sepgsql_client_type sepgsql_temp_object_t : db_procedure { create drop getattr setattr execute entrypoint install } ; | ||
+ | allow sepgsql_client_type sepgsql_trusted_procedure_type : db_procedure { getattr execute entrypoint } ; | ||
+ | -> allow httpd_t httpd_exec_t : file { ioctl read getattr lock execute execute_no_trans entrypoint open } ; | ||
+ | </ | ||
+ | - Verificare che esista una rule che permette al parent domain (init_t) una transizione verso il destination domain (httpd_t)< | ||
+ | [root@statichostname ~]# sesearch -s init_t -t httpd_t -p transition -A | ||
+ | Found 1 semantic av rules: | ||
+ | allow initrc_domain daemon : process transition ; | ||
+ | |||
+ | # initrc_domain e' attributo di init_t (gia' visto), e daemon e' attributo di httpd_t. | ||
+ | |||
+ | [root@statichostname ~]# seinfo -adaemon -x | grep httpd_t | ||
+ | httpd_t | ||
+ | </ | ||
+ | - Verificare l' | ||
+ | [root@statichostname ~]# sesearch -T -s init_t -t httpd_exec_t | ||
+ | Found 1 semantic te rules: | ||
+ | | ||
+ | </ | ||
+ | |||
+ | ==== Esercitazione 3 ==== | ||
+ | |||
+ | - Confinamento di un utente: confinare user1 a user_u (verifica impossibilita' | ||
+ | - Visualizzazione context del processo di login dell' | ||
+ | [user1@statichostname ~]$ id -Z | ||
+ | unconfined_u: | ||
+ | [user1@statichostname ~]$ su - user2 | ||
+ | Password: | ||
+ | Attempting to create directory / | ||
+ | [user2@statichostname ~]$ logout | ||
+ | [user1@statichostname ~]$ logout | ||
+ | Connection to 192.168.56.102 closed. | ||
+ | </ | ||
+ | - Confinare il linux user user1 associandolo all' | ||
+ | [root@statichostname ~]# semanage login -a -s user_u user1 | ||
+ | [root@statichostname ~]# semanage login -l | ||
+ | |||
+ | Login Name | ||
+ | |||
+ | __default__ | ||
+ | root | ||
+ | system_u | ||
+ | user1 user_u | ||
+ | </ | ||
+ | - Verificare che user1, il cui dominio attuale e' user_t, non puo' fare su e diventare user2< | ||
+ | # ssh user1@192.168.56.102 | ||
+ | user1@192.168.56.102' | ||
+ | Warning: No xauth data; using fake authentication data for X11 forwarding. | ||
+ | Last login: Wed Nov 28 18:45:06 2018 from 192.168.56.1 | ||
+ | [user1@statichostname ~]$ id -Z | ||
+ | user_u: | ||
+ | [user1@statichostname ~]$ su - user2 | ||
+ | Password: | ||
+ | su: Authentication failure | ||
+ | </ | ||
+ | - Visualizzazione del policy module apache< | ||
+ | [root@statichostname ~]# semodule -l | grep apache | ||
+ | apache 2.7.2 | ||
+ | [root@statichostname ~]# semodule -lfull | grep apache | ||
+ | 100 apache | ||
+ | [root@statichostname ~]# ls / | ||
+ | cil hll lang_ext | ||
+ | [root@statichostname ~]# cat / | ||
+ | [root@statichostname ~]# wc -l / | ||
+ | 4116 / | ||
+ | </ | ||
+ | - Abilitare tramite boolean httpd a leggere le home directory degli utenti< | ||
+ | [root@statichostname ~]# semanage boolean -l | grep http | grep user | ||
+ | httpd_read_user_content | ||
+ | [root@statichostname ~]# sesearch -b httpd_read_user_content -AC | ||
+ | Found 40 semantic av rules: | ||
+ | DT allow httpd_user_script_t user_home_type : dir { getattr search open } ; [ httpd_read_user_content ] | ||
+ | DT allow httpd_user_script_t user_home_type : dir { ioctl read getattr lock search open } ; [ httpd_read_user_content ] | ||
+ | ... | ||
+ | [root@statichostname ~]# semanage boolean --modify --on httpd_read_user_content | ||
+ | [root@statichostname ~]# semanage boolean -l httpd_read_user_content | ||
+ | list option can not be used with --boolean | ||
+ | [root@statichostname ~]# semanage boolean -l | grep httpd_read_user_content | ||
+ | httpd_read_user_content | ||
+ | </ | ||
+ | [root@statichostname ~]# getsebool httpd_read_user_content | ||
+ | httpd_read_user_content --> off | ||
+ | [root@statichostname ~]# setsebool -P httpd_read_user_content on | ||
+ | [root@statichostname ~]# getsebool httpd_read_user_content | ||
+ | httpd_read_user_content --> on | ||
+ | [root@statichostname ~]# semanage boolean -l | grep httpd_read_user_content | ||
+ | httpd_read_user_content | ||
+ | [root@statichostname ~]# | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Esercitazione 4 ==== | ||
+ | |||
+ | - Generare un messaggio di errore eseguendo il comando **su** come user **user1**< | ||
+ | [root@statichostname ~]# semanage login -lC | ||
+ | |||
+ | Login Name | ||
+ | |||
+ | user1 user_u | ||
+ | |||
+ | |||
+ | [user1@statichostname ~]$ su - user2 | ||
+ | Password: | ||
+ | su: Authentication failure | ||
+ | </ | ||
+ | - Analizzare l' | ||
+ | [root@statichostname ~]# ausearch -m avc --start recent | ||
+ | ---- | ||
+ | time-> | ||
+ | type=PROCTITLE msg=audit(1543498686.922: | ||
+ | type=SYSCALL msg=audit(1543498686.922: | ||
+ | type=AVC msg=audit(1543498686.922: | ||
+ | ---- | ||
+ | time-> | ||
+ | type=PROCTITLE msg=audit(1543498691.746: | ||
+ | type=SYSCALL msg=audit(1543498691.746: | ||
+ | type=AVC msg=audit(1543498691.746: | ||
+ | ---- | ||
+ | time-> | ||
+ | type=PROCTITLE msg=audit(1543498689.416: | ||
+ | type=SYSCALL msg=audit(1543498689.416: | ||
+ | type=AVC msg=audit(1543498689.416: | ||
+ | |||
+ | [root@statichostname ~]# ausearch -m avc --start recent | audit2allow | ||
+ | |||
+ | |||
+ | # | ||
+ | allow user_t faillog_t: | ||
+ | |||
+ | #!!!! This avc can be allowed using the boolean ' | ||
+ | allow user_t self: | ||
+ | |||
+ | |||
+ | [root@statichostname ~]# ausearch -m avc --start recent | audit2allow -M mylocalmodule | ||
+ | ******************** IMPORTANT *********************** | ||
+ | To make this policy package active, execute: | ||
+ | |||
+ | semodule -i mylocalmodule.pp | ||
+ | |||
+ | [root@statichostname ~]# ls -l mylocalmodule.* | ||
+ | -rw-r--r--. 1 root root 1120 Nov 29 14:47 mylocalmodule.pp | ||
+ | -rw-r--r--. 1 root root 304 Nov 29 14:47 mylocalmodule.te | ||
+ | [root@statichostname ~]# | ||
+ | |||
+ | </ | ||
+ | - Caricare il modulo generato e verificare che l' | ||
+ | [root@statichostname ~]# semodule -i mylocalmodule.pp | ||
+ | [root@statichostname ~]# semodule -lfull | grep mylocalmodule | ||
+ | 400 mylocalmodule | ||
+ | [root@statichostname ~]# ls / | ||
+ | 100/ 400/ disabled/ | ||
+ | [root@statichostname ~]# ls / | ||
+ | mylocalmodule | ||
+ | [root@statichostname ~]# ls / | ||
+ | cil hll lang_ext | ||
+ | [root@statichostname ~]# | ||
+ | |||
+ | [user1@statichostname ~]$ su - user2 | ||
+ | Password: | ||
+ | [user2@statichostname ~]$ | ||
+ | |||
+ | </ | ||
+ | - Disabilitare e rimuovere il modulo, verificando che l' | ||
+ | [root@statichostname ~]# semodule -d mylocalmodule | ||
+ | [root@statichostname ~]# semodule -lfull | grep mylocalmodule | ||
+ | 400 mylocalmodule | ||
+ | [root@statichostname ~]# semodule -r mylocalmodule | ||
+ | libsemanage.semanage_direct_remove_key: | ||
+ | [root@statichostname ~]# semodule -lfull | grep mylocalmodule | ||
+ | |||
+ | [user1@statichostname ~]$ su - user2 | ||
+ | Password: | ||
+ | su: Authentication failure | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||