1. 程式人生 > >security 02: 加密與解密 、 掃描與抓包 、 總結和答疑 、 SELinux安全防護

security 02: 加密與解密 、 掃描與抓包 、 總結和答疑 、 SELinux安全防護

一段 數字簽名 def 接口 數據流 5.1 調用 message systemctl

day01
一、selinux安全防護
二、數據 加密 解密
三、抓包與掃描
++++++++++++++++++++++++++++++
一、selinux安全防護
1.1 selinux 介紹
1.2 linux安全保護模型?
1.3 查看當前系統selinux的狀態
[root@svr7 ~]# sestatus

1.4 selinux配置文件
[root@host50 ~]# cat /etc/selinux/config

1.5 查看安全上下文
查看進程 ps aux -Z
ps aux -Z | grep -i 進程名

查看文件 ls -lZ 文件名

查看目錄 ls -ldZ 目錄名

1.6 安全上下文的組成?
system_u:object_r:passwd_file_t:s0
system_u:object_r:net_conf_t:s0
用戶:角色:訪問類型:參數

1.7 常見的訪問類型?

1.8 SELinux啟用後的一般操作規律?
創建新文件時,新文件繼承父目錄的安全上下文。
移動文件時,保持原有的安全上下文不變
拷貝文件時,繼承目標目錄的安全上下文
1.9 修改訪問類型?
]# chcon -R -t 訪問類型 目錄名 //遞歸修改
]# chcon -t 訪問類型 文件名
]# chcon -t httpd_sys_content_t /var/www/html/test1.html

1.10 恢復文件訪問類型?
]# restorecon -R 目錄名 //遞歸恢復
]# restorecon 文件名
]# restorecon /var/www/html/test2.html
練習文件selinux 訪問類型的修改 (以網站服務文件為例)

1.11 selinux布爾值 (功能開關)
查看bool值
#getsebool -a
#getsebool -a | grep -i ftp

修改bool值
#setsebool -P 選項=1|0
#setsebool -P 選項 on|off
環境準備,啟動vsftpd服務,配置允許匿名ftp用戶, 可以上傳和下載文件到/var/ftp/shardir目錄裏。

[root@host50 ~]# sed -n ‘29p‘ /etc/vsftpd/vsftpd.conf
anon_upload_enable=YES
[root@host50 ~]#
[root@host50 ~]# mkdir /var/ftp/shardir
[root@host50 ~]# chmod o+w /var/ftp/shardir
[root@host50 ~]# systemctl restart vsftpd
592 setsebool -P ftpd_anon_write=1
593 setsebool -P ftpd_full_access on
594 getenforce -a | grep -i ftp
597 setenforce 1

客戶端:
yum -y install ftp

lcd 切換到本機目錄
get 下載
put 上傳
+++++++++++++++++++++++++++++++++++++
啟用selinux日誌程序記錄報錯信息:
啟用selinux 後,修改網站服務使用的端口號為8090。

[root@host50 ~]# cat /var/log/messages | grep -n -i setroubleshoot | tail -1
37272:Jan 28 22:38:31 host50 setroubleshoot: SELinux is preventing /usr/sbin/httpd from name_bind access on the tcp_socket port 8090. For complete SELinux messages. run sealert -l a9db74ee-ff0d-45c7-8a4d-34d4bf5bc3c4

[root@host50 ~]# sealert -l a9db74ee-ff0d-45c7-8a4d-34d4bf5bc3c4

]# semanage port -a -t http_port_t -p tcp 8090

+++++++++++++++++++++++++++++++
二、數據 加密 解密
2.1 為什麽要加密?
2.2 什麽加密? 什麽是解密?
發送方 加密 接收方 解密
2.3 什麽是算法? 加/解密計算規則
秘鑰(公鑰加密 私鑰解密)
|——> 可以是指令 或 一段代碼

2.4 加密方式?
對稱加密: 加密和解密使用相同的計算規則
非對稱加密:加密和解密使用不相同的計算規則

對稱加密算法: DES AES
非對稱加密算法: RSA DSA

Hash散列技術,保證數據的完整性。 md5 sha
++++++++++++++++++++++++++++++

gpg 對稱加密 -c -d

發 收
usera -------------- > userb

usera 加密 gpg -c 文件名

userb 解密 gpg -d 文件名

++++++++++++++++++++++++++
gpg 非對稱加密(使用密鑰對)
公鑰 加密
私鑰 解密
發 收
usera -------------- > userb
userb(解密 )
1 創建密鑰對 ~/.gnupg/
#gpg --gen-key
#ls ~/.gnupg/
2 導出公鑰 並把公鑰文件傳給usera
[userb@room9pc17 ~]$ gpg --export -a > /tmp/userb.pub
[userb@room9pc17 ~]$ cat /tmp/userb.pub
usera(加密)
1 導入公鑰文件~/.gnupg
[usera@room9pc17 ~]$ gpg --import /tmp/userb.pub
[usera@room9pc17 ~]$ ls ~/.gnupg
2 使用公鑰加密文件 ,把加密後的文件給userb
[usera@room9pc17 ~]$ gpg -e -r usera b.txt
[usera@room9pc17 ~]$ mv b.txt.gpg /tmp/
3 userb解密
調用私鑰解密 解密文件
[userb@room9pc17 ~]$ gpg -d /tmp/b.txt.gpg > u.txt
cat u.txt
++++++++++++++++++++++++++++++++++
gpg數字簽名 功能驗證數據完整性
-b --verify --fingerprint
userb
私鑰簽名
26 vim c.txt
28 gpg -b c.txt
ls c.
30 cp c.
/tmp/
33 gpg --fingerprint
usera
公鑰驗證簽名
#gpg --verify /tmp/c.txt.sig
#cat /tmp/c.txt
root# sed -i ‘1s/^/aaa/‘ /tmp/c.txt
usera
#cat /tmp/c.txt
#gpg --verify /tmp/c.txt.sig
+++++++++++++++++++++++++++++++
三、抓包與掃描
3.1 掃描nmap
3.2 抓包tcpdump
3.3 協議分析軟件的使用wireshark

3.1 掃描nmap
180 which nmap
181 rpm -qf /bin/nmap
182 rpm -q nmap

man nmap

語法格式
#nmap [掃描類型] [選項] <ip地址>

掃描類型有哪些? -sS -sT -sU -sP
選項有哪些? -A -n -p

ip地址表示方式?
192.168.4.53
192.168.4.100-200
192.168.4.53,57,68

#nmap -sP 172.40.55.122
185 nmap -sP 172.40.55.122
186 nmap -sP 172.40.55.180
187 nmap 172.40.55.180
188 nmap -n -sT -p 80 172.40.55.180
189 nmap -n -sT -p 25,80 172.40.55.180
190 nmap -n -sT -p 21-100,3306 172.40.55.180
191 nmap -n -A 172.40.55.180

nmap -n -sP 172.40.55.100-200 --exclude 172.40.55.143,172.40.55.158

#vim /root/ip.txt
172.40.55.143
172.40.55.158
172.40.55.180
:wq

nmap -n -sP 172.40.55.100-200 --excludefile /root/ip.txt

vim /root/web.sh
#/bin/bash
for ip in 180 143 158
do
nmap -n -sS -p 80 172.40.55.$ip | grep -q open
if [ $? -eq 0 ];then
echo " 172.40.55.$ip 80 open"
else
echo " 172.40.55.$ip 80 closed"
fi
done
:wq
+++++++++++++++++++++++++++++
#mkdir /myself
#mv /root/web.sh /myself/checkweb
#chmod +x /myself/checkweb

#vim /etc/profile
....
export PATH=/myself:$PATH
:wq

#resource /etc/profile
#echo $PATH
#cd /usr/local/
#checkweb

++++++++++++++++++++++++++++++++++++
#checkweb 3306 112 130 129
#checkweb 11211 112 130 129

3.2 抓包tcpdump
tcpdump [選項] [過濾條件]

選項
-i 網絡接口名 //不指定接口名時,默認抓eth0 接口進出的包
-c 數字 //指定抓包個數,不指定的話會一直抓包
-A //以可閱讀的方式抓取數據包
-w 文件名.cap //把抓到的數據信息存儲到文件裏。默認會輸出到屏幕上。
-r 文件名.cap //讀取抓包文件的內容

110 tcpdump
112 tcpdump -i br1
113 tcpdump -i br1 -c 2
114 tcpdump -i br1 -c 2 -A
115 tcpdump -i br1 -c 2 -A -w /tmp/tcpdump.cap
116 tcpdump -A -r /tmp/tcpdump.cap

過濾條件: 抓包時,不加過濾條件,會抓所有到達的數據包。反之,只抓復合條件的數據包。

[root@host50 ~]# tcpdump -i eth0 -A

[root@host50 ~]# tcpdump -i eth0 -A tcp port 8090

[root@host50 ~]# tcpdump -i eth0 -A tcp port 8090 and host 192.168.4.53

[root@host50 ~]#tcpdump -i eth0 -A tcp port 22 and net 192.168.4.0/24

[root@host50 ~]#tcpdump -i eth0 -A tcp port 22 and not host 192.168.4.53

[root@host50 ~]#tcpdump -i eth0 -A
tcp port 8090 and host 192.168.4.53 or host 192.168.4.54

[root@host50 ~]#tcpdump -i eth0 -A
tcp port 8090 and (host 192.168.4.53 or host 192.168.4.54)

[root@host52 ~]#tcpdump -i eth0 -A tcp port 25 -w /tmp/mail2.cap

[root@host52 ~]#tcpdump -A -r /tmp/mail2.cap

[root@host52 ~]# scp /tmp/mail2.cap 192.168.4.254:/root/

3.3 協議分析軟件的使用wireshark (宿主機)
安裝軟件包

yum -y install wireshark wireshark-gnome

rpm -q wireshark wireshark-gnome

打開圖形界面
應用程序->互聯網->軟件名 ---> 文件菜單->打開文件/root/mail2.cap

tcp 傳輸協議 標記位
SYN 新連接
ACK 確認連接
FIN 斷開連接
push( P) 傳輸數據
RST 重新建立連接

應用層 http smtp ftp
傳輸層 tcp upd
網絡層 ip包
物理層 數據流 (0101)

security 02: 加密與解密 、 掃描與抓包 、 總結和答疑 、 SELinux安全防護