Created Date: 2020-10-25 19:00:44
Last Upgraded Date: 2020-10-28 12:14:53
操作环境
自用腾讯云服务器
CentOS Linux release 8.0.1905 (Core)
macOS Catalina Version 10.15.5
Python 3.7
Pycharm Professional 2020.1.3
准备工作
DoS相关知识
DoS简介
In computing, a denial-of-service attack (DoS attack) is a cyber-attack in which the perpetrator seeks to make a machine or network resource unavailable to its intended users by temporarily or indefinitely disrupting services of a host connected to the Internet. Denial of service is typically accomplished by flooding the targeted machine or resource with superfluous requests in an attempt to overload systems and prevent some or all legitimate requests from being fulfilled.
DoS种类
- Distributed DoS
- Application layer attacks
- Advanced persistent DoS
- Denial-of-service as a service
DoS攻击方式
Internet Control Message Protocol (ICMP) flood
A smurf attack relies on misconfigured network devices that allow packets to be sent to all computer hosts on a particular network via the broadcast address of the network, rather than a specific machine. The attacker will send large numbers of IP packets with the source address faked to appear to be the address of the victim. Most devices on a network will, by default, respond to this by sending a reply to the source IP address. If the number of machines on the network that receive and respond to these packets is very large, the victim’s computer will be flooded with traffic. This overloads the victim computer and can even make it unusable during such attack.
Ping flood is based on sending the victim an overwhelming number of ping packets, usually using the “ping” command from Unix-like hosts (the -t flag on Windows systems is much less capable of overwhelming a target, also the -l (size) flag does not allow sent packet size greater than 65500 in Windows). It is very simple to launch, the primary requirement being access to greater bandwidth than the victim.
Ping of death is based on sending the victim a malformed ping packet, which will lead to a system crash on a vulnerable system.
(S)SYN flood
A SYN flood is a form of denial-of-service attack in which an attacker rapidly initiates a connection to a server without finalizing the connection. The server has to spend resources waiting for half-opened connections, which can consume enough resources to make the system unresponsive to legitimate traffic.
The packet that the attacker sends is the SYN packet, a part of TCP’s three-way handshake used to establish a connection.
When a client attempts to start a TCP connection to a server, the client and server exchange a series of messages which normally runs like this:
The client requests a connection by sending a SYN (synchronize) message to the server.
The server acknowledges this request by sending SYN-ACK back to the client.
The client responds with an ACK, and the connection is established.
This is called the TCP three-way handshake, and is the foundation for every connection established using the TCP protocol.
A SYN flood attack works by not responding to the server with the expected ACK code. The malicious client can either simply not send the expected ACK, or by spoofing the source IP address in the SYN, cause the server to send the SYN-ACK to a falsified IP address – which will not send an ACK because it “knows” that it never sent a SYN.
UDP flood attack
A UDP flood attack is a volumetric denial-of-service (DoS) attack using the User Datagram Protocol (UDP), a sessionless/connectionless computer networking protocol.
Using UDP for denial-of-service attacks is not as straightforward as with the Transmission Control Protocol (TCP). However, a UDP flood attack can be initiated by sending a large number of UDP packets to random ports on a remote host. As a result, the distant host will:
- Check for the application listening at that port;
- See that no application listens at that port;
- Reply with an ICMP Destination Unreachable packet.
Thus, for a large number of UDP packets, the victimized system will be forced into sending many ICMP packets, eventually leading it to be unreachable by other clients. The attacker(s) may also spoof the IP address of the UDP packets, ensuring that the excessive ICMP return packets do not reach them, and anonymizing their network location(s). Most operating systems mitigate this part of the attack by limiting the rate at which ICMP responses are sent.
HTTP flood
In an HTTP flood, the HTTP clients such as web browser interact with an application or server to send HTTP requests. The request can be either “GET” or “POST”. The aim of the attack is when to compel the server to allocate as many resources as possible to serving the attack, thus denying legitimate users access to the server’s resources.
GET flood
The GET request is used to retrieve static content like images. Typically this induces relatively low load on the server per request.
POST flood
POST requests are more likely to require the server to perform some kind of processing, such as looking up items in a database. Therefore, HTTP POST flood attacks typically impose higher load on the server per request.
实际操作
报文
伪首部(TCP/UDP)
1 | def generate_header_pseudo(self, srcaddr, dstaddr, ptcl, tslen): |
SourceAddr 32
DestinAddr 32
MustBeZero 8
Protocol 8
TransportLen
TCP头
1 | def generate_header_tcp(self, srcaddr, dstaddr, srcport, dstport): |
SourcePort 16
DestinPort 16
SeqNumber 32
AckNumber 32
HeaderLen 4
Reserved 3
Flag 1*9
Winsize 16
Checksum 16
UrgentPointer 16

UDP头
1 | def generate_header_udp(self, srcaddr, dstaddr, srcport, dstport, data): |
SourcePort 16
DestinPort 16
Len 16
Checksum 16
Data

ICMP头
1 | def generate_header_icmp(self): |
Type 8
Code 8
Checksum 16
Idenfication 16
SeqNumber 16
Data

IP头
1 | def generate_header_ip(self, srcaddr, dstaddr, segmentsize, tsprtl): |
Version 4
HeaderLen-IHL 4
TOS 8
Length 16
Identification 16
Flag 3
Offset 13
TTL 8
Protocol 8
HeaderChecksum 16
SourceAddr 32
DestinAddr 32

校验和
1 | def calc_checksum(self, header): |
封包
生成IP包
1 | def generate_ip_packet(self, trsprtl): |
攻击
进行DoS攻击
1 | def DoS_attack(self, dstaddr, dstport, dos_type): |
主函数
1 | if __name__=="__main__": |
服务器抓包
1 | tcpdump -i eth0 -w log.pcap |
tcpdump命令抓包后,使用scp下载,后使用Wireshark本地查看。
1 | scp root@MYIPADDR:/cncourse/20201028/log.pcap /Users/reneelin/Downloads |
需要解决的问题
随机IP源地址无效
在测试中间包生成过程中,可以正常输出随机IP地址,但无法在真正发送的包中查看此伪装IP,只会显示本机在局域网攻击中的IP地址,无法起到伪装的作用。
不知是操作系统限制还是网络安全限制,或者是代码问题。
UDP包长度问题
UDP包的长度进行了设置,为8个字节的Header加上数据长度,但是Wireshark获取的包的长度永远为1。
无法尝试是否真正能够进行初级DoS攻击
由于DoS攻击,特别是常用手段的DoS攻击已经被防御得比较健全,对于中小型服务器来说,是完全达不到攻击效果的,因而也没有合适的实验机可供使用,不知道实际效果如何。