1.解决DOS生产案例:根据web日志或者网络连接数,监控当某个IP并发连接数或者短时间PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔5分钟。防火墙命令:iptables -A INPUT -s IP -j REHECT

##脚本内容
[root@centos84 data]# cat nginx_dropIP.sh
#!/bin/bash
awk '{print $1}' /var/log/nginx/access.log | grep -v "^$" | sort | uniq -c > tmp.log
exec < tmp.log
while read line
do
ip=`echo $line | awk '{print $2}'`
count=`echo $line| awk '{print $1}'`
if [ $count -gt 100 ] && [ `iptables -n -L | grep "$ip" | wc -l` -lt 1 ]; then
iptables -I INPUT -s $ip -j DROP
echo "$line is dropped" >> droplist.log
fi
done
##测试端口访问
[root@centos79 ~]# curl 192.168.0.128
[root@centos84 data]# cat /var/log/nginx/access.log
192.168.0.129 - - [07/Jan/2022:03:05:05 +0800] "GET / HTTP/1.1" 200 4057 "-" "curl/7.29.0" "-"
##防火墙拦截规则查看
[root@centos84 data]# iptables -nvL
Chain INPUT (policy ACCEPT 786 packets, 61279 bytes)
pkts bytes target prot opt in out source destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 494 packets, 59548 bytes)
pkts bytes target prot opt in out source destination

Chain LIBVIRT_INP (0 references)
pkts bytes target prot opt in out source destination

Chain LIBVIRT_OUT (0 references)
pkts bytes target prot opt in out source destination

Chain LIBVIRT_FWO (0 references)
pkts bytes target prot opt in out source destination

Chain LIBVIRT_FWI (0 references)
pkts bytes target prot opt in out source destination

Chain LIBVIRT_FWX (0 references)
pkts bytes target prot opt in out source destination
##使用ab工具进行20并发和200的请求次数
[root@centos79 ~]# ab -c20 -n200 192.168.0.128/index
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.0.128 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests


Server Software: nginx/1.14.1
Server Hostname: 192.168.0.128
Server Port: 80

Document Path: /index
Document Length: 3971 bytes

Concurrency Level: 20
Time taken for tests: 0.062 seconds
Complete requests: 200
Failed requests: 0
Write errors: 0
Non-2xx responses: 200
Total transferred: 828800 bytes
HTML transferred: 794200 bytes
Requests per second: 3234.20 [#/sec] (mean)
Time per request: 6.184 [ms] (mean)
Time per request: 0.309 [ms] (mean, across all concurrent requests)
Transfer rate: 13088.42 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.8 0 3
Processing: 1 5 1.7 5 7
Waiting: 1 5 1.7 5 7
Total: 3 6 1.1 6 8
WARNING: The median and mean for the initial connection time are not within a normal deviation
These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
50% 6
66% 6
75% 7
80% 7
90% 7
95% 7
98% 8
99% 8
100% 8 (longest request)
##运行脚本检测访问log
[root@centos84 data]# sh nginx_dropIP.sh
##查看拦截效果
[root@centos84 data]# iptables -nvL
Chain INPUT (policy ACCEPT 2118 packets, 154K bytes)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * * 192.168.0.129 0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 1684 packets, 963K bytes)
pkts bytes target prot opt in out source destination

Chain LIBVIRT_INP (0 references)
pkts bytes target prot opt in out source destination

Chain LIBVIRT_OUT (0 references)
pkts bytes target prot opt in out source destination

Chain LIBVIRT_FWO (0 references)
pkts bytes target prot opt in out source destination

Chain LIBVIRT_FWI (0 references)
pkts bytes target prot opt in out source destination

Chain LIBVIRT_FWX (0 references)
pkts bytes target prot opt in out source destination
[root@centos84 data]# cat droplist.log
201 192.168.0.129 is dropped

##设置定时任务
[root@centos84 ~]#
[root@centos84 data]# crontab -l
30 1 * * 1-5 sh /root/etcbak.sh
*/5 * * * * sh /data/nginx_dropIP.sh

2.描述秘钥交换的过程

根据密钥的使用方式,将密码分为

对称加密(AES)

在对称密码中,加密、解密时使用的是同一个密钥

非对称加密(RSA)

非对称加密中,秘钥分为加密秘钥和解密秘钥两个,两者不是同一个秘钥。

加密密钥,一般是公开的,因此该密钥称为公钥(public key)

解密密钥,由消息接收者自己保管的,不能公开,因此也称为私钥(private key)

公钥和私钥是一 一对应的,是不能单独生成的,一对公钥和密钥统称为密钥对

由公钥加密的密文,必须使用与该公钥对应的私钥才能解密

由私钥加密的密文,必须使用与该私钥对应的公钥才能解密

混合密码系统

混合密码的加密:

会话密钥(session key)

为本次通信随机生成的临时密钥,作为对称密码的密钥,用于加密消息,提高速度

加密步骤(发送消息)

首先,消息发送者要拥有消息接收者的公钥,生成会话密钥,作为对称密码的密钥,加密消息,用消息接收者的公钥,加密会话密钥,将前2步生成的加密结果,一并发给消息接收者

发送出去的内容包括

用会话密钥加密的消息(加密方法:对称密码)用公钥加密的会话密钥(加密方法:公钥密码)

解密步骤(收到消息)

消息接收者用自己的私钥解密出会话密钥

再用第1步解密出来的会话密钥,解密消息

总的来说,AES加密发送者发送消息会将秘钥与秘钥加密过的明文一起发送给接受者,这种情况下无法保证秘钥传输过程中的安全配送问题;而RSA密码则是生成一对不同的公钥和私钥,是成对的。事先接受者先将公钥共享给发送方,然后发送方利用此公钥对明文内容进行加密,随后发送给接受方,接收方收到消息拿配对的私钥进行解密,得到明文,过程复杂,效率较低。

而混合加密则采取两家之长,即解决了秘钥的安全配送问题,同时也提高了加密与解密效率。混合加密先是接收方将生成的公钥共享给发送方一份,发送方使用此公钥将生成的会话秘钥进行加密,同时发送方使用会话秘钥对明文内容进行加密,完成加密后,将两份加密过的内容一起发送给接收方,接收方接收到消息,先通过配对的私钥解密出会话秘钥,然后使用会话秘钥对密文进行解密,得到明文内容。到此一个完成混合加密流程就算是完成了。

3.https的通信过程

1. 客户端发起HTTPS请求

用户在浏览器里输入一个https网址,然后连接到服务器的443端口

2. 服务端的配置

采用HTTPS协议的服务器必须要有一套数字证书,可以自己制作,也可以向组织申请。区别就是自

己颁发的证书需要客户端验证通过,才可以继续访问,而使用受信任的公司申请的证书则不会弹出

提示页面。这套证书其实就是一对公钥和私钥

3. 传送服务器的证书给客户端

证书里其实就是公钥,并且还包含了很多信息,如证书的颁发机构,过期时间等等

4. 客户端解析验证服务器证书

这部分工作是由客户端的TLS来完成的,首先会验证公钥是否有效,比如:颁发机构,过期时间等

等,如果发现异常,则会弹出一个警告框,提示证书存在问题。如果证书没有问题,那么就生成一

个随机值。然后用证书中公钥对该随机值进行非对称加密

5. 客户端将加密信息传送服务器

这部分传送的是用证书加密后的随机值,目的就是让服务端得到这个随机值,以后客户端和服务端

的通信就可以通过这个随机值来进行加密解密了

6. 服务端解密信息

服务端将客户端发送过来的加密信息用服务器私钥解密后,得到了客户端传过来的随机值

7. 服务器加密信息并发送信息

服务器将数据利用随机值进行对称加密,再发送给客户端

8. 客户端接收并解密信息

客户端用之前生成的随机值解密服务段传过来的数据,于是获取了解密后的内容。

4.使用awk以冒号分割获取/etc/password文件第一列。

[root@centos84 ~]# awk -F: '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
dbus

systemd-coredump
systemd-resolve
tss
polkitd
geoclue
rtkit
pipewire
pulse
libstoragemgmt
qemu
usbmuxd
unbound
gluster
rpc
avahi
saslauth
dnsmasq
radvd
sssd
cockpit-ws
cockpit-wsinstance
chrony
colord
rpcuser
setroubleshoot
flatpak
gdm
clevis
gnome-initial-setup
sshd
tcpdump
u01
apache
nginx