selinux和防火墻的管理
一:selinux
1:selinux的基礎
1、selinux的概念
就是保護Linux系統的機制,對內的
2、三種模式
強制模式(enforcing):最嚴格的selinux模式(阻擋的操作)
允許模式(permissive):不會阻擋操作,但是會記錄操作信息
關閉模式(disabled) :不會阻擋操作,也不會記錄操作信息
其中的targeted這個策略,專門針對網路的
3、三種模式轉換
臨時修改,setenforce 0|1
通過修改內核的參數也能實現selinux切換
但是在開機的狀態下,不能實現切換到關閉模式下,只能通過修改配置文件實現,然后reboot重啟即可
4、查看selinux的標簽
使用-Z就能查看到標簽
[root@server ~]# ll -Z total 4 -rw-------. 1 root root system_u:object_r:admin_home_t:s0 1376 Feb 4 21:44 anaconda-ks.cfg drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Feb 5 13:35 Desktop drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Feb 5 13:35 Documents drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Feb 5 13:35 Downloads
2:管理文本的標簽
1、查看標簽
[root@server ~]# semanage fcontext -l |grep home /home directory system_u:object_rhome_root_t:s0 /home symbolic link system_u:object_rhome_root_t:s0 /home/(.*/)?\.snapshots(/.*)? all files system_u:object_r:snapperd_data_t:s0 /home/[^/]+ directory unconfined_u:object_r:user_home_dir_t:s0
2、臨時修改:(chcon)
[root@server mnt]# mkdir dir1 [root@server mnt]# ll -Z total 0 drwxr-xr-x. 2 root root unconfined_u:object_r:mnt_t:s0 6 Mar 16 19:28 dir1 [root@server mnt]# chcon -t httpd_sys_content_t dir1/ [root@server mnt]# ll -Z total 0 drwxr-xr-x. 2 root root unconfined_u:object_r:httpd_sys_content_t:s0 6 Mar 16 19:28 dir1 [root@server mnt]# restorecon -v dir1/ Relabeled /mnt/dir1 from unconfined_u:object_r:httpd_sys_content_t:s0 to unconfined_u:object_r:mnt_t:s0 [root@server mnt]# ll -Z total 0 drwxr-xr-x. 2 root root unconfined_u:object_r:mnt_t:s0 6 Mar 16 19:28 dir1
加上-R這個選項就能遞歸的改變了
使用restorecon刷新一下,這個就恢復到原來的樣子了
臨時修改的話,selinux的狀態發生了改變,就會恢復原樣,開啟模式到關閉模式,再到開啟模式
3、永久修改:(semanage)
#使用永久修改的話,要使用絕對路徑才行 [root@server mnt]# mkdir dir2 [root@server mnt]# ll -Z total 0 drwxr-xr-x. 2 root root unconfined_u:object_r:mnt_t:s0 6 Mar 16 19:35 dir2 [root@server mnt]# semanage fcontext -a -t httpd_sys_content_t /mnt/dir2 [root@server mnt]# restorecon -v /mnt/dir2/ Relabeled /mnt/dir2 from unconfined_u:object_r:mnt_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0 [root@server mnt]# ll -Z total 0 drwxr-xr-x. 2 root root unconfined_u:object_r:httpd_sys_content_t:s0 6 Mar 16 19:35 dir2 [root@server mnt]#
restorecon這個命令的解釋
| -R | 遞歸的修改 |
| -F | 強制的恢復上下文 |
| -v | 顯示詳細的信息 |
繼承selinux的權限
就是創建了一個文件夾,里面的文件自動繼承里面的標簽
#記住使用通配符,然后一定要使用單引號和絕對路徑即可 [root@server mnt]# mkdir dir3 [root@server mnt]# semanage fcontext -a -t httpd_sys_content_t '/mnt/dir3(/*)?' [root@server mnt]# restorecon -RFv /mnt/dir3/ Relabeled /mnt/dir3 from unconfined_u:object_r:mnt_t:s0 to system_u:object_r:httpd_sys_content_t:s0 [root@server mnt]# cd dir3/ [root@server dir3]# touch file1 [root@server dir3]# ll -Z total 0 -rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 0 Mar 16 19:41 file1
查看selinux的修改文件
使用命令修改了標簽實質就是修改了對應的配置文件
/etc/selinux/targeted/contexts/files
3:管理端口的標簽
1、查看端口的標簽
[root@server files]# semanage port -l |grep 22 freeipmi_port_t tcp 9225 freeipmi_port_t udp 9225 hplip_port_t tcp 1782, 2207, 2208, 8290, 8292, 9100, 9101, 9102, 9220, 9221, 9222, 9280, 9281, 9282, 9290, 9291, 50000, 50002
2、修改端口的標簽
[root@server files]# semanage port -a -t http_port_t -p tcp 8888 [root@server files]# semanage port -l | grep -w 8888 http_port_t tcp 8888, 80, 81, 443, 488, 8008, 8009, 8443, 9000
4:布爾值
1、查看布爾值
[root@server files]# semanage boolean -l | head -n 10 SELinux boolean State Default Description abrt_anon_write (off , off) Allow abrt to anon write abrt_handle_event (off , off) Allow abrt to handle event abrt_upload_watch_anon_write (on , on) Allow abrt to upload watch anon write antivirus_can_scan_system (off , off) Allow antivirus to can scan system antivirus_use_jit (off , off) Allow antivirus to use jit auditadm_exec_content (on , on) Allow auditadm to exec content [root@server files]# getsebool -a|head -n 10 abrt_anon_write --> off abrt_handle_event --> off abrt_upload_watch_anon_write --> on antivirus_can_scan_system --> off antivirus_use_jit --> off auditadm_exec_content --> on authlogin_nsswitch_use_ldap --> off authlogin_radius --> off authlogin_yubikey --> off awstats_purge_apache_log_files --> off [root@server files]# getsebool -a|head -n 10 abrt_anon_write --> off abrt_handle_event --> off
第一個為臨時的修改(當前的狀態),第二個為永久修改
2、修改布爾值
#不加上-P 就是臨時修改,加上了-P就是永久修改 [root@server files]# setsebool httpd_enable_homedirs on [root@server files]# semanage boolean -l | grep -w httpd_enable_homedirs httpd_enable_homedirs (on , off) Allow httpd to enable homedirs [root@server files]# setsebool -P httpd_enable_homedirs on [root@server files]# semanage boolean -l | grep -w httpd_enable_homedirs httpd_enable_homedirs (on , on) Allow httpd to enable homedirs [root@server files]#
5:selinux的排錯
審計日志:/var/log/audit
這個就是記錄selinux的信息
步驟:
安裝這個setroubleshoot包,能自動的將審計日志的消息發送到messages文件里面去,然后使用sealert-l UUID用于生成事件報告(可以在messages里面看到),就ok了
sealert -a可以查看所有的報告
當然也可以使用圖形化工具查看,安裝x86的setroubleshoot包即可
6:semanage的使用
選項:
| -a | 添加 |
| -d | 刪除 |
| -l | 查看 |
| -m | 修改 |
| -t | 類型 |
記住已經修改過的可以使用-m進行修改,不能使用-a了,初次可以使用-a
二:firewalld
1:基礎概念
內核模塊:netfilter提供防火墻,而firewalld和iptables只是管理防火墻的工具
1)linux對外的,阻止外面的主機訪問自己,控制流量的
2)是按照區域進行劃分的
2:模式
防火墻有2種模式,一個是臨時模式,一個是永久模式
臨時模式:就是重啟防火墻,或者重新加載的話,臨時模式就恢復原樣了
永久模式:加上了--permanent就能實現永久修改了,還要重新加載一下即可
3:查看防火墻
[root@client ~]# firewall-cmd --list-all public (active) ##默認區域名稱 target: default ##區域的動作 icmp-block-inversion: no ##icmp類型黑白名單開關(no|yes) interfaces: ens160 ##網卡接口 sources: ##來源ip地址,可以是mac地址 services: cockpit dhcpv6-client ssh ##放行的服務,其實就是端口 ports: ##允許放行的目標端口 protocols: ##允許通過的協議 forward: yes ##允許在此區域中轉發ip地址 masquerade: no ##偽裝ip地址 forward-ports: ##端口轉發 source-ports: ##允許的來源端口 icmp-blocks: ##可添加的icmp類型,如果上面的icmp-block-inversion為no的話,添加的都被拒絕了 rich rules: ##更為詳細的富規則,優先級最高
4:防火墻命令
關于服務,端口,源地址等的操作
|
--get-default-zone |
查訪默認的區域名稱 |
|
--set-default-zone=<區域名稱> |
設置默認的區域,使其永久生效 |
|
--list-all |
顯示當前區域的網卡配置參數、資源、端口以及服務等信息 |
|
--list-all-zones |
顯示所有區域的網卡配置參數、資源、端口以及服務等信息 |
|
--get-zones |
顯示可用的區域 |
|
--get-active-zones |
顯示當前正在使用的區域、來源地址和網卡名稱 |
|
--add-source= |
將源自此IP或子網的流量導向指定的區域 |
|
--remove-source= |
不再將源自此IP或子網的流量導向這個區域 |
|
--change-source= |
將源自此IP或子網的流量導向指定到新的區域 |
|
--add-interface=<網卡名稱> |
將源自該網卡的所有流量都導向某個指定區域 |
|
--change-interface=<網卡名稱> |
將某個網卡與區域進行關聯 |
|
--get-services |
顯示預定義的服務 |
|
--add-service=<服務名> |
設置默認區域允許該服務的流量 |
|
--add-port=<端口號/協議> |
設置默認區域允許該端口的流量 |
|
--remove-service=<服務名> |
設置默認區域不再允許該服務的流量 |
|
--remove-port=<端口號/協議> |
設置默認區域不再允許該端口的流量 |
|
--permanent |
讓配置永久生效 |
|
--reload |
讓“永久生效”的配置規則立即生效,并覆蓋當前的配置規則 |
5:區域
firewalld使用區域來管理,每個區域處理不同的事情
|
trusted |
可信區域,防火墻放行一切流量。等同于關閉防火墻功能。 |
|
home |
區域內主動發起連接的流入回程數據包允許通過,默認放行ssh,mdns,ipp-client,samba-client或dhcpv6-client服務。 |
|
internal |
與home區域相同 |
|
work |
區域內主動發起連接的流入回程數據包允許通過,放行ssh,dhcpv6-client服務 |
|
external |
區域內主動發起連接的流入回程數據包允許通過,放行ssh服務匹配,開啟地址偽裝功能 |
|
dmz |
區域內主動發起連接的流入回程數據包允許通過,放行ssh服務匹配 |
|
block |
區域內主動發起連接的流入回程數據包允許通過 |
|
drop |
對進入該區域的所有數據包丟棄,并且不進行任何回包,區域內主動發起連接的流入回程數據包允許通過 |
默認的區域是public,
6:動作
每個區域用不同的動作
四個動作default,accpet,drop,regect,
其中default,accpet,是接收,后面的drop是拒絕,不會返回消息,reject也是拒絕,但是會返回消息
設置target的策略動作:必須使用永久這個選項
[root@client ~]# firewall-cmd --list-all public (active) target: DROP icmp-block-inversion: no interfaces: ens160 sources: services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@client ~]# firewall-cmd --permanent --set-target=ACCEPT success [root@client ~]# firewall-cmd --reload success [root@client ~]# firewall-cmd --list-all public (active) target: ACCEPT icmp-block-inversion: no interfaces: ens160 sources: services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@client ~]#
7:防火墻規則
就是有優先級的存在,顯示來源地址,然后是網絡接口,最后是默認的區域,優先級的存在,決定了執行的順序
來源地址:拒絕本地的主機訪問虛擬機的設置
這個區域的動作要設置為drop策略,拒絕訪問
#虛擬機的設置 [root@client ~]# firewall-cmd --add-source=10.112.224.254 --permanent --zone=public success [root@client ~]# firewall-cmd --reload success [root@client ~]# firewall-cmd --list-all public (active) target: DROP icmp-block-inversion: no interfaces: ens160 sources: 10.112.224.254 services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: #使用win+r ping虛擬機地址 正在 Ping 192.168.20.20 具有 32 字節的數據: 請求超時。 請求超時。
網絡的接口:將ens160設置成drop區域
默認區域:
如果前面的都沒有匹配到的話,就走默認區域里面的行為
設置默認區域的
8:防火墻命令配置
1、服務
放行服務:就是將服務添加進來
[root@client /]# firewall-cmd --permanent --add-service=http success [root@client /]# firewall-cmd --reload success [root@client /]# firewall-cmd --list-all public (active) target: ACCEPT icmp-block-inversion: no interfaces: sources: 10.112.224.254 services: cockpit dhcpv6-client http ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: #拒絕服務,就是將這個服務移走 [root@client /]# firewall-cmd --permanent --remove-service=http success [root@client /]# firewall-cmd --reload success [root@client /]# firewall-cmd --list-all public (active) target: ACCEPT icmp-block-inversion: no interfaces: sources: 10.112.224.254 services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
2、端口
放行端口
[root@client /]# firewall-cmd --add-port=80/tcp --permanent success [root@client /]# firewall-cmd --reload success [root@client /]# firewall-cmd --list-all public (active) target: ACCEPT icmp-block-inversion: no interfaces: sources: 10.112.224.254 services: cockpit dhcpv6-client ssh ports: 80/tcp protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: #拒絕某個端口 [root@client /]# firewall-cmd --remove-port=80/tcp --permanent success [root@client /]# firewall-cmd --reload success [root@client /]# firewall-cmd --list-all public (active) target: ACCEPT icmp-block-inversion: no interfaces: sources: 10.112.224.254 services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
3、富規則
更加的清晰的設置規則,明確ip地址能不能訪問等
案例:
#拒絕另外一臺主機的訪問本地主機,禁止192.168.20.20/24這個ip地址訪問 [root@server ~]# firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.20.20" reject' success [root@server ~]# firewall-cmd --reload success [root@server ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens160 sources: services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: rule family="ipv4" source address="192.168.20.20" reject #使用另外一臺主機發現ping不通 [root@client ~]# ping 192.168.20.10 PING 192.168.20.10 (192.168.20.10) 56(84) bytes of data. From 192.168.20.10 icmp_seq=1 Destination Port Unreachable ^C --- 192.168.20.10 ping statistics --- 1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms #拒絕來自本地的網段訪問22端口 由于本地的主機使用通過192.168.20.1這個ip地址進行登錄的,所以只需要拒絕這個ip網段即可,使用xsehll都是通過這個ip來進行訪問的 [root@server ~]# firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.20.0/24" port protocol="tcp" port="22" reject' success [root@server ~]# firewall-cmd --reload success [root@server ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens160 sources: services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: rule family="ipv4" source address="192.168.20.0/24" port port="22" protocol="tcp" reject #遠程端口轉發
#就是將訪問本地的1000端口,然后轉發到192.168.20.20上的22端口實現連接
#當然要將偽地址轉發這個功能開啟,才能實現
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.20.0/24" forward-port port=1000 protocol=tcp to-port=22 to-addr=192.168.20.20'
#開啟偽地址轉發
firewall-cmd --add-masquerade
#退出xhsell后,實現連接
[C:\~]$ ssh root@192.168.20.10 1000
Connecting to 192.168.20.10:1000...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Activate the web console with: systemctl enable --now cockpit.socket
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Tue Mar 19 14:03:28 2024 from 192.168.20.10
[root@client ~]# ifconfig | grep netmask
inet 192.168.20.20 netmask 255.255.255.0 broadcast 192.168.20.255
inet 127.0.0.1 netmask 255.0.0.0
4、本地轉發
本地轉發:訪問本地的2222端口,實際上是轉發到本地22端口上
設置后,無需要有2222端口,只是個虛的
firewall-cmd --add-forward-port=port=2222:proto=tcp:toport=22 [root@server ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens160 sources: services: cockpit dhcpv6-client http ssh ports: protocols: forward: yes masquerade: no forward-ports: port=2222:proto=tcp:toport=22:toaddr= source-ports: icmp-blocks: rich rules: #使用xshell連接2222端口嗎,會自動的跳轉到22端口上面,實現連接 [C:\~]$ ssh root@192.168.20.10 2222 Connecting to 192.168.20.10:2222... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Activate the web console with: systemctl enable --now cockpit.socket Register this system with Red Hat Insights: insights-client --register Create an account or view all your systems at https://red.ht/insights-dashboard Last login: Mon Mar 18 13:38:11 2024 from 192.168.20.1 [root@server ~]#
5、遠程轉發
訪問本地的9999端口,能夠連接到遠程機器,這便是遠程轉發,
#注意要添加協議名
firewall-cmd --add-forward-port=port=9999:proto=tcp:toport=22:toaddr=192.168.20.20
[root@server ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client http ssh
ports:
protocols:
forward: yes
masquerade: yes
forward-ports:
port=9999:proto=tcp:toport=22:toaddr=192.168.20.20
source-ports:
icmp-blocks:
rich rules:
#使用xshell要退出去,再來連接,否則連接不上
[C:\~]$ ssh root@192.168.20.10 9999
Connecting to 192.168.20.10:9999...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Activate the web console with: systemctl enable --now cockpit.socket
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Tue Mar 19 13:50:47 2024 from 192.168.20.10
[root@client ~]# ifconfig | grep netmask
inet 192.168.20.20 netmask 255.255.255.0 broadcast 192.168.20.255
inet 127.0.0.1 netmask 255.0.0.0
9:其余修改防火墻的三種方式
1、圖形化(firewall-config)
2、web控制臺
3、自定義編寫xml配置文件
全局配置文件:/usr/lib/firewalld
用戶的配置文件:/etc/firewalld

浙公網安備 33010602011771號