目录
编辑
一、在CentOS上安装Python3
1.下载Python3.10源代码文件
2.运行以下命令行完成安装
3.确认是否安装成功
二、理解各个命令的含义
1.uptime
2.netstat
3.ss
4.firewall-cmd
5.sysctl
三、利用Python实现DDOS入侵检测
1.采集TCP连接数据
一、在CentOS上安装Python3
1.下载Python3.10源代码文件
下载地址:https://www.python.org/downloads/source/
2.运行以下命令行完成安装
mkdir /usr/local/python3
tar -zxvf Python-3.10.0.tgz
cd Python-3.10.0
./configure --prefix=/usr/local/python3
make
make install
3.确认是否安装成功
/usr/local/python3/bin/python3
import time
print(time.strftime("%Y-%m-%d %H:%M:%S"))
import random
print(random.randint(100,200))
exit()
4.设置环境变量
vi ~/.bashrc
#添加该语句
alias python3=/usr/local/python3/bin/python3
alias pip3=/usr/local/python3/bin/pip3
alias python3=/usr/local/python3/bin/python3
alias pip3=/usr/local/pyuthon3/bin/pip3
source ~/.bashrc
5.配置pip国内镜像源
[global]
index-url=https://pypi.douban.com/simple
trusted-host = pypi.douban.com
二、理解各个命令的含义
1.uptime
2.netstat
3.ss
4.firewall-cmd
打开防火墙80端口:firewall-cmd --add-port=80/tcp
5.sysctl
三、利用Python实现DDOS入侵检测
1.采集TCP连接数据
# 4、采集连接数量最多的IP地址
def get_most_ip():
result = os.popen('netstat -ant | grep :80').read()
line_list = result.split('\n')
ip_list = []
for line in line_list:
try:
temp_list = line.split()
ip = temp_list[4].split(':')[0]
ip_list.append(ip)
except:
pass
dict = Counter(ip_list)
most_ip = dict.most_common(1)
return most_ip[0][0]
# 5、调用firewall-cmd防火墙命令封锁攻击源IP地址
def firewall_ip(ip):
result = os.popen(f"firewall-cmd --add-rich-rule='rule family=ipv4 source address={ip} port port=80 protocol=tcp reject'").read()
if 'success' in result:
print(f"已经成功将可疑攻击源 {ip} 进行封锁,流量将不再进入.")
else:
print(f"对可疑攻击源 {ip} 进行封锁时失败,转为人工处理.")