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

📄 pthread.c

📁 多线程扫描匿名ftp的例程
💻 C
字号:
//eg. #./scanftp -s 127.0.0.1 -e 127.0.65.255 -o host

#include<pthread.h>
#include<sys/time.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUF_LEN 255
#define THREADNUM 1000 /*你想开的线程数,我的猫是56k的,memory是64,cpu比较惨超频的赛扬450,我开*/ /*100个线程时,cpu已全力运行,memory还有的剩,如果大家的机器比较爽,带宽*/ /* 较大,那就可以多开了。视你自己的情况而定了。*/
#define NORM "\033[0m"
#define GREEN "\033[32m"
#define RED "\033[31m"
#define BLUE "\033[34m"
#define BROWN "\033[33m"
#define time 10 
extern int errno;
uint32_t startip,endip,k;
pthread_t thread[THREADNUM];
pthread_mutex_t mut=PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t file=PTHREAD_MUTEX_INITIALIZER;
char *filename="host";

void usage(char *progname)
{
	printf(BLUE "Scananonymousftp is beta 1.0\n\n"
	RED "2001 by Tang Jing biao and cpu\n\n"
	GREEN "usage: " NORM "%s [-s startip] [-e endip] [-o filename] [-h help]\n\n",
	progname
	);

	exit(-1);
}
void filewrite(char *name,char *ip)
{
	char *p1,*p2,*p3;
	FILE *fd;
	int len1,len2;
	p1=name;
	p2=ip;
	p3="\n";
	printf("Ip is written!\n");

	if((fd=fopen(p1,"r+t"))==NULL)
	{
		printf("Reading file was failed!\n");
		exit(0);
	}
	fseek(fd,0L,SEEK_END);
	len1=strlen(p2);
	len2=strlen(p3);
	fwrite(p2,sizeof(char),len1,fd);
	fwrite(p3,sizeof(char),len2,fd);

	if(fclose(fd))
		printf("The file is not closed!\n");
}

void *scanhost()
{
	struct sockaddr_in saddr;
	int sockfd,flags,len,error,status,temp;
	char buf[BUF_LEN],*hostip;
	struct timeval timeout={time,0};

	fd_set wmask,rmask;
	saddr.sin_port=htons(21);
	saddr.sin_family=AF_INET;
	pthread_mutex_lock(&mut);
	while(k<=endip)
	{
		saddr.sin_addr.s_addr=htonl((uint32_t)k);
		pthread_mutex_unlock(&mut);
		if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
		{
			printf("Socket error!\n");
			exit(-1);
		}
		printf("scanthread%d is scanning...%s at %d\n",pthread_self(),inet_ntoa(saddr.sin_addr),sockfd);
		fflush(stdout);
		FD_ZERO(&wmask);
		FD_SET(sockfd,&wmask);
		rmask=wmask;
		timeout.tv_sec=time;
		timeout.tv_usec=0;
		status=fcntl(sockfd,F_GETFL);
		fcntl(sockfd,F_SETFL,status|O_NONBLOCK);
		temp=connect(sockfd,(struct sockaddr *)&saddr,sizeof(saddr));

		if(temp<0)
		{ 
			flags=select(sockfd+1,&rmask,&wmask,(fd_set *)NULL,&timeout);
			if(flags<=0)
			{
				close(sockfd);
				pthread_mutex_lock(&mut);
				k++;
				continue;
			}
			if(FD_ISSET(sockfd,&rmask)||FD_ISSET(sockfd,&wmask))
			{
				if(FD_ISSET(sockfd,&rmask)&&FD_ISSET(sockfd,&wmask))
				{
					len=sizeof(error);
					temp=getsockopt(sockfd,SOL_SOCKET,SO_ERROR,&error,&len);
					if((temp!=0)||(error!=0))
						{
							close(sockfd);
							pthread_mutex_lock(&mut);
							k++;
							continue;
						}
				}
			}
		}
		bzero(buf,BUF_LEN);
		fcntl(sockfd,F_SETFL,status);
		if((len=read(sockfd,buf,BUF_LEN))>=0)
		{
			if(strncmp(buf,"220",3)==0)
			{
				write(sockfd,"user anonymous\n",15);
				if((len=read(sockfd,buf,BUF_LEN))>=0)
				{
					if(strncmp(buf,"331",3)==0)
					{
						write(sockfd,"pass shit@\n",11);
						if((len=read(sockfd,buf,BUF_LEN))>=0)
						{
							if(strncmp(buf,"230",3)==0)
							{
							printf("%d HaHa! find ! Ip is %s\n",pthread_self(),inet_ntoa(saddr.sin_addr));
							hostip=inet_ntoa(saddr.sin_addr);
							pthread_mutex_lock(&file);
							filewrite(filename,hostip);
							pthread_mutex_unlock(&file);
							fflush(stdout);
							close(sockfd);
							}
						}
					}
				}
			}
		}

		close(sockfd);
		pthread_mutex_lock(&mut);
		k++;
	}
	pthread_mutex_unlock(&mut);
	pthread_exit(NULL);
}

int create_thread()
{
	int i=0,temp;
	for(i=0;i<THREADNUM;i++)
	{
		pthread_mutex_lock(&mut);
		if(k>endip)
		{
			pthread_mutex_unlock(&mut);
			break;
		}
		pthread_mutex_unlock(&mut);
		pthread_create(&thread[i],NULL,scanhost,NULL);
		pthread_mutex_lock(&mut);
		k++;
		pthread_mutex_unlock(&mut);
	}
	temp=i;
	for(i=0;i<temp;i++)
	{
		pthread_join(thread[i],NULL);
		printf("scanthread %d is closed!\n",i);
	}
	return i;
}

int main(int argc,char *argv[])
{
	char c ;
	FILE *fdmain;
	int thnum;
	if(argc<2)
	{
		printf("Please input parameter! Type -h\n");
		exit(0);
	}
	while ((c = getopt(argc, argv, "s:e:o:h")) != EOF)
	{
		switch (c)
		{
		case 's':
		startip=ntohl(inet_addr(optarg));
		break;

		case 'e':
		endip=ntohl(inet_addr(optarg));
		break;

		case 'o':
		filename = optarg;
		break;

		case 'h':
		usage(argv[0]);
		break;

		default:
		break;
		}
	}
	if(startip>endip)
	{
		k=startip;
		startip=endip;
		endip=k;
	}
	k=startip;
	if((fdmain=fopen(filename,"w+t"))==NULL)
	{
		printf("The file was not opened!!!!!\n");
		exit(0);
	}
	fclose(fdmain);
	printf("The main process created %d thread \n",THREADNUM);
	pthread_mutex_init(&mut,NULL);//初始化互斥量
	pthread_mutex_init(&file,NULL);
	thnum=create_thread();
	printf("The main process is closed.\n");
}

⌨️ 快捷键说明

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