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

📄 wscan.c

📁 linux下的一个多线程扫描工具
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * Wscan Multi thread command line port scanner V 0.06
 *
 * Copyright (c) 2007   wzt
 *
 * http://www.xsec.org
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <ctype.h>
#include "socket.h"
#include "wscan.h"

/* function in socket.c */
extern unsigned int make_network_ip(char *host);
extern int tcp_connect(unsigned int remote_ip,
                unsigned int remote_port,int timeout);
extern int tcp_connect_fast(unsigned int remote_ip,
                unsigned int remote_port,int timeout);

char local_ip[20];

pthread_t t;
pthread_attr_t attr;
pthread_mutex_t thread_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t total_ports_lock = PTHREAD_MUTEX_INITIALIZER;

long start_ip = 0,end_ip = 0;

unsigned long thread_num = 0;
unsigned long max_thread = 0;
unsigned int timeout = 0;
unsigned long total_ports;
unsigned long search_num;

unsigned int long_ports[MAXFLAG2][2] = {{0,0}},long_ports_num = 0;
unsigned int short_ports[MAXPORTS],short_ports_num = 0;

void usage(char *program);
void setup(pthread_attr_t *attr);
int my_sleep(unsigned int micro_second);
void get_ctrl_c();
void set_ip(char *hosts);
void check_port(int port);
void check_ports(char port[]);
int abstract_ports(char ports[]);
void wait_thread_end(void);
int scan_port_array(unsigned int remote_ip,unsigned int ports[],
                int port_num,unsigned int timeout);
int scan_port(unsigned int remote_ip,unsigned int s_port,
        unsigned int e_port,unsigned int timeout);
void *tcp_thread_connect(void *sock);

void usage(char *program)
{
        sprintf(banner,"wscan %2.2f %s",
                VERSION,"linux multi port scanner  (c) wzt#xsec.org\n\n");
        fprintf(stdout,"%s",banner);
        fprintf(stdout,"usage : %s <host|startip[-endip]> [options]\n",program);
        fprintf(stdout,"\n[options]:\n");
        fprintf(stdout,"-p <port{,-}>\t-- scan port between {,-},see examples\n");
        fprintf(stdout,"-n <thread_num>\t-- scan thread number,between 1-1000,default is 200\n");
        fprintf(stdout,"-t <timeout>\t-- set time out,between 1-20,default is 5\n");
        fprintf(stdout,"\n[example]:\n");
        fprintf(stdout,"%s http://www.xxx.com -p 21,22,25,80-1024,1433,3306-3389 -n 150 -t 10\n",program);
        exit(0);
}

/* init mutli thread function */
void setup(pthread_attr_t *attr)
{
	pthread_attr_init(attr);
	pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED);
}

int my_sleep(unsigned int micro_second)
{
	struct timeval  t_timeval;

        t_timeval.tv_sec = 0;
        t_timeval.tv_usec = micro_second;

        select( 0, NULL, NULL, NULL, &t_timeval );

        return 0;
}

void get_ctrl_c()
{
	printf("\r\n[-] Received Ctrl + C.\r\n");
        printf("\r\n[+] Wait threads exit ...\r\n");
        
	sleep(WAIT_TIME1);
	exit(0);
}

void test_thread(void)
{
        while (1) {
                if (thread_num > max_thread)
                        my_sleep(THREAD_TIME);
                else
                        break;
        }
        return;
}

void wait_thread_end(void)
{
        sleep(WAIT_TIME1);
        while (1) {
                if (thread_num > 0) {
                        printf("[+] wait threads %d end ...\n",thread_num);
                        my_sleep(WAIT_TIME);
                        continue;
                }
                else
                        break;
        }
        return ;
}

void set_ip(char *hosts)
{
        char startip[20],endip[20];
        int i = 0,j = 0;;

        if (!hosts)
                goto err;
                
        for (; hosts[i] != '-'; i++)
                startip[j++] = hosts[i];
        startip[j] = '\0';
        i++;
        j = 0;
        
        for (; i < strlen(hosts); i++)
                endip[j++] = hosts[i];
        endip[j] = '\0';
#ifdef DEBUG
        printf("%s\n%s\n.......\n",startip, endip);
#endif
        start_ip = ntohl(inet_addr(startip));
        end_ip = ntohl(inet_addr(endip));
#ifdef DEBUG
        printf("%d, %d\n",start_ip, end_ip);
#endif
        if (start_ip ==0 || end_ip == 0)
                goto err;
        else
                return;

        err:
        printf("[-] hosts error.check it out\n");
        exit(-1);
}

/* check single port. */
void check_port(int port)
{
        if (port < 0 || port > 65535) {
                printf("[-] port error,must > 0 && < 65535.\n");
                exit(-1);
        }
}

void check_ports(char port[])
{
        int i = 0;

        if (port[0] == FLAG1 || port[0] == FLAG2 ||
                port[strlen(port) - 1] == FLAG1 ||
                port[strlen(port) - 1] == FLAG2)
                goto err;
                
        for (; i < strlen(port); i++) {
                if (port[i] < '0' || port[i] > '9')
                        if (port[i] != FLAG1 && port[i] != FLAG2)
                                goto err;
                if (port[i] == FLAG1 && port[i + 1] == FLAG1)
                        goto err;
                if (port[i] == FLAG2 && port[i + 1] == FLAG2)
                        goto err;
        }

        if (i >= strlen(port))
                return ;
                
        err:
        printf("[-] -p bad parameter,check it out.\n");
        exit(-1);
}

/**
 * abstract_ports - extract all the ports from the parameters given in the main
 * funtion.
 *
 * @ports[] the ports form maybe like follows:
 *          21,22,23,80
 *          21-1024
 *          21,22,80-1024
 *          21-1024,1433-3306
 *          21-1024,1234,12345,1433-3306
 *          21-1024,1234,12345,1433-3306,8080
 *          21,22,25,80-1024,1433,3306-3389
 */
int abstract_ports(char ports[])
{
        int i = 0,j = 0,k = 0,m = 0,n = 0;
        char temp[6];
        int port;

        if (strchr(ports,FLAG1) != NULL && strchr(ports,FLAG2) != NULL) {
#ifdef DEBUG
                printf("mode 3.\n");
#endif
                for (; i < strlen(ports); i++) {
                        if (ports[i] >= '0' && ports[i] <= '9') {
                                temp[j++] = ports[i];
                        }
                        if (ports[i] == FLAG1) {
                                temp[j] = '\0';
                                port = atoi(temp);
                                check_port(port);
                                short_ports[k++] = port;
#ifdef DEBUG
                                printf("abstract port %d ok.\n",short_ports[k - 1]);
#endif
                                temp[0] = '\0';
                                j = 0;
                        }
                        if (ports[i] == FLAG2) {
                                temp[j] = '\0';
                                long_ports[m++][0] = atoi(temp);
#ifdef DEBUG
                                printf("abstract port %d ok.\n",long_ports[m - 1][0]);
#endif
                                temp[0] = '\0';
                                j = 0;
                                i++;
                                for (; i < strlen(ports); i++) {
                                        if (ports[i] == FLAG1 || ports[i] == FLAG2)
                                                break;
                                        temp[j++] = ports[i];
                                }
                                if (i > strlen(ports))
                                        break;
                                temp[j] = '\0';
                                port = atoi(temp);
                                check_port(port);
                                long_ports[m - 1][1] = port;
#ifdef DEBUG
                                printf("abstract port %d ok.\n",long_ports[m - 1][1]);
#endif
                                temp[0] = '\0';
                                j = 0;
                        }
                }
                temp[j] = '\0';
                port = atoi(temp);
                check_port(port);
                if (port != 0)
                        short_ports[k++] = port;

#ifdef DEBUG
                printf("abstract port %d ok.\n",short_ports[k - 1]);
#endif
                short_ports_num = k;
                long_ports_num = m;
                return PORT_MODE3;
        }
        else if (strchr(ports,FLAG1) != NULL) {
#ifdef DEBUG
                printf("mode 1.\n");
#endif
                for (; i < strlen(ports); i++) {
                        if (ports[i] >= '0' && ports[i] <= '9') {
                                temp[j++] = ports[i];
                        }
                        if (ports[i] == FLAG1) {
                                temp[j] = '\0';
                                port = atoi(temp);
                                check_port(port);
                                short_ports[k++] = port;

⌨️ 快捷键说明

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