拒絕網(wǎng)絡攻擊–nginx和linux的安全加固
nginx安全
隱藏nginx版本號
- 安裝啟服務
[root@node1 lnmp_soft]# tar xf nginx-1.12.2.tar.gz
[root@node1 lnmp_soft]# cd nginx-1.12.2/
[root@node1 nginx-1.12.2]# ./configure && make && make install
[root@node1 ~]# /usr/local/nginx/sbin/nginx
-
訪問不存在的路徑
如果訪問頁面不存在就會暴露nginx版本號,進一步提高了被攻擊的可能性
-
命令行訪問:
[root@node1 ~]# curl -I http://192.168.88.11/ # -I 只顯示頭部
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Fri, 10 Dec 2021 07:51:08 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 10 Dec 2021 07:46:16 GMT
Connection: keep-alive
ETag: "61b305c8-264"
Accept-Ranges: bytes
- 隱藏版本信息
[root@node1 ~]# vim /usr/local/nginx/conf/nginx.conf
... ...
17 http {
18 server_tokens off;
... ...
[root@node1 ~]# /usr/local/nginx/sbin/nginx -s reload
再次訪問不存在的路徑,版本號消失文章來源:http://www.zghlxwxcb.cn/news/detail-426031.html
防止DOS、DDOS攻擊
- DDOS:分布式拒絕服務
# 壓力測試,每批次發(fā)送100個請求給web服務器,一共發(fā)200個
[root@zzgrhel8 ~]# yum install -y httpd-tools
[root@zzgrhel8 ~]# ab -c 100 -n 200 http://192.168.88.11/
... ...
Benchmarking 192.168.88.11 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests # 發(fā)送200個請求完成
... ...
Complete requests: 200 # 完成了200個請求
Failed requests: 0 # 0個失敗
... ...
- 配置nginx連接共享內存為10M,每秒鐘只接收一個請求,最多有5個請求排隊,多余的拒絕
[root@node1 ~]# vim /usr/local/nginx/conf/nginx.conf
17 http {
18 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; # 添加
... ...
37 server {
38 listen 80;
39 server_name localhost;
40 limit_req zone=one burst=5; # 添加
[root@node1 ~]# /usr/local/nginx/sbin/nginx -s reload
# 再次測試
[root@zzgrhel8 ~]# ab -c 100 -n 200 http://192.168.88.11/
... ...
Benchmarking 192.168.88.11 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests
... ...
Complete requests: 200
Failed requests: 194 # 失敗了194個
... ...
附:取出nginx.conf中注釋和空行以外的行文章來源地址http://www.zghlxwxcb.cn/news/detail-426031.html
# -v是取反。^ *#表示開頭有0到多個空格,然后是#。^$表示空行 [root@node1 ~]# egrep -v '^ *#|^$' /usr/local/nginx/conf/nginx.conf
防止緩沖區(qū)溢出
- 緩沖區(qū)溢出定義:程序企圖在預分配的緩沖區(qū)之外寫數(shù)據(jù)。
- 漏洞危害:用于更改程序執(zhí)行流,控制函數(shù)返回值,執(zhí)行任意代碼。
# 配置nginx緩沖區(qū)大小,防止緩沖區(qū)溢出
[root@node1 ~]# vim /usr/local/nginx/conf/nginx.conf
... ...
17 http {
18 client_body_buffer_size 1k;
19 client_header_buffer_size 1k;
20 client_max_body_size 1k;
21 large_client_header_buffers 2 1k;
... ...
[root@node1 ~]# /usr/local/nginx/sbin/nginx -s reload
Linux加固
用戶賬號參數(shù)修改
- 設置tom賬號,有效期為2022-1-1
# 查看tom的賬號信息
[root@node1 ~]# chage -l tom
最近一次密碼修改時間 :10月 12, 2021
密碼過期時間 :從不
密碼失效時間 :從不
帳戶過期時間 :從不
兩次改變密碼之間相距的最小天數(shù) :0
兩次改變密碼之間相距的最大天數(shù) :99999
在密碼過期之前警告的天數(shù) :7
[root@node1 ~]# chage -E 2022-1-1 tom
[root@node1 ~]# chage -l tom
最近一次密碼修改時間 :10月 12, 2021
密碼過期時間 :從不
密碼失效時間 :從不
帳戶過期時間 :1月 01, 2022
兩次改變密碼之間相距的最小天數(shù) :0
兩次改變密碼之間相距的最大天數(shù) :99999
在密碼過期之前警告的天數(shù) :7
# 設置賬號永不過期,注意-E后面是數(shù)字-1,不是字母l
[root@node1 ~]# chage -E -1 tom
[root@node1 ~]# chage -l tom
最近一次密碼修改時間 :10月 12, 2021
密碼過期時間 :從不
密碼失效時間 :從不
帳戶過期時間 :從不
兩次改變密碼之間相距的最小天數(shù) :0
兩次改變密碼之間相距的最大天數(shù) :99999
在密碼過期之前警告的天數(shù) :7
# 設置新建用戶的密碼策略
[root@node1 ~]# vim /etc/login.defs
25 PASS_MAX_DAYS 99999 # 密碼永不過期,設置最長有效期
26 PASS_MIN_DAYS 0 # 密碼最短使用時間,0表示隨時可改密碼
27 PASS_MIN_LEN 5 # 密碼最短長度
28 PASS_WARN_AGE 7 # 密碼過期前7天發(fā)警告
33 UID_MIN 1000 # 新建用戶最小的UID
34 UID_MAX 60000 # 新建用戶最大的UID
用戶安全設置
# 鎖定tom賬號
[root@node1 ~]# passwd -l tom
鎖定用戶 tom 的密碼 。
passwd: 操作成功
[root@node1 ~]# passwd -S tom # 查看狀態(tài)
tom LK 2021-10-12 0 99999 7 -1 (密碼已被鎖定。)
# 解鎖tom賬號
[root@node1 ~]# passwd -u tom
解鎖用戶 tom 的密碼。
passwd: 操作成功
[root@node1 ~]# passwd -S tom
tom PS 2021-10-12 0 99999 7 -1 (密碼已設置,使用 SHA512 算法。)
保護文件
# 查看文件的特殊屬性
[root@node1 ~]# lsattr /etc/passwd
---------------- /etc/passwd # 沒有特殊屬性
# 修改屬性
chattr +i 文件 # 不允許對文件做任何操作,只能看
chattr -i 文件 # 去除i屬性
chattr +a 文件 # 文件只允許追加
chattr -a 文件 # 去除a屬性
[root@node1 ~]# chattr +i /etc/passwd
[root@node1 ~]# lsattr /etc/passwd
----i----------- /etc/passwd
[root@node1 ~]# useradd zhangsan
useradd:無法打開 /etc/passwd
[root@node1 ~]# rm -f /etc/passwd
rm: 無法刪除"/etc/passwd": 不允許的操作
[root@node1 ~]# chattr -i /etc/passwd
[root@node1 ~]# rm -f /etc/passwd # 可以刪除
[root@node1 ~]# ls /etc/passwd
ls: 無法訪問/etc/passwd: 沒有那個文件或目錄
# 恢復passwd文件
[root@node1 ~]# cp /etc/passwd- /etc/passwd
# 追加
[root@node1 ~]# chattr +a /etc/hosts
[root@node1 ~]# echo 'hello world' >> /etc/hosts
[root@node1 ~]# rm -f /etc/hosts
rm: 無法刪除"/etc/hosts": 不允許的操作
到了這里,關于拒絕網(wǎng)絡攻擊--nginx和linux的安全加固的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!