📄 wscan.c
字号:
#ifdef DEBUG
printf("abstract port %d ok.\n",short_ports[k - 1]);
#endif
temp[0] = '\0';
j = 0;
}
}
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
short_ports_num = k;
return PORT_MODE1;
}
else if (strchr(ports,FLAG2) != NULL) {
#ifdef DEBUG
printf("mode 2.\n");
#endif
for (; i < strlen(ports); i++) {
if (ports[i] >= '0' && ports[i] <= '9') {
temp[j++] = ports[i];
}
if (ports[i] == FLAG2) {
temp[j] = '\0';
port = atoi(temp);
check_port(port);
long_ports[m++][0] = port;
#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];
}
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
if (long_ports[m - 1][0] > long_ports[m - 1][1]) {
printf("[-] bad ports,check it out.\n");
exit(-1);
}
temp[0] = '\0';
j = 0;
}
}
long_ports_num = long_ports[0][1] - long_ports[0][0] + 1;
return PORT_MODE2;
}
else {
#ifdef DEBUG
printf("mode4.\n");
#endif
port = atoi(ports);
check_port(port);
short_ports[k] = port;
short_ports_num = 1;
return PORT_MODE4;
}
return 0;
}
void *tcp_thread_connect(void *sock)
{
struct remote_sock *socks = sock;
unsigned int remote_ip,remote_port,timeout;
remote_ip = socks->ip;
remote_port = socks->port;
timeout = socks->timeout;
if (!remote_ip) {
printf("[-] remote ip error.\n");
return ;
}
if (!remote_port) {
printf("[-] remote port error.\n");
return ;
}
if (tcp_connect_fast(remote_ip,remote_port,timeout)) {
pthread_mutex_lock(&total_ports_lock);
total_ports++;
pthread_mutex_unlock(&total_ports_lock);
printf("%-5d\t\t\t\t[+]\n",ntohs(remote_port));
}
}
int scan_port_array(unsigned int remote_ip,unsigned int ports[],
int port_num,unsigned int timeout)
{
struct remote_sock sock;
int i = 0;
for (; i < port_num; i++) {
sock.ip = remote_ip;
sock.port = htons(ports[i]);
sock.timeout = timeout;
test_thread();
if (pthread_create(&t, &attr, tcp_thread_connect, (void *)&sock) != 0) {
printf("[-] create thread failed.\n");
continue;
}
#ifdef DEBUG
printf("[+] create thread ok.\n");
#endif
pthread_mutex_lock(&thread_lock);
thread_num++;
pthread_mutex_unlock(&thread_lock);
my_sleep(SLEEP_TIME);
}
return 1;
}
int scan_port(unsigned int remote_ip,unsigned int s_port,
unsigned int e_port,unsigned int timeout)
{
struct remote_sock sock;
int i = 0;
int port_num;
printf("scan port from %d to %d ...\n",s_port, e_port);
i = s_port;
for (; i <= e_port; i++) {
sock.ip = remote_ip;
sock.port = htons(i);
sock.timeout = timeout;
test_thread();
if (pthread_create(&t, &attr, tcp_thread_connect, (void *)&sock) != 0) {
printf("[-] create thread failed.\n");
continue;
}
#ifdef DEBUG
printf("[+] create thread ok.\n");
#endif
pthread_mutex_lock(&thread_lock);
thread_num++;
pthread_mutex_unlock(&thread_lock);
my_sleep(SLEEP_TIME);
}
return 1;
}
int main(int argc,char **argv)
{
time_t t_start,t_end;
unsigned int remote_ip;
int flag_p = 0;
int i = 0;
int ret,port_num = 0;
if (argc == 1 || !strcmp(argv[1],"-h"))
usage(argv[0]);
while (i < argc - 1) {
if (!strcmp(argv[i],"-p")) {
flag_p = 1;
check_ports(argv[i + 1]);
ret = abstract_ports(argv[i + 1]);
i++;
continue;
}
if (!strcmp(argv[i],"-n")) {
max_thread = atoi(argv[i + 1]);
i++;
continue;
}
if (!strcmp(argv[i],"-t")) {
timeout = atoi(argv[i +1]);
i++;
continue;
}
i++;
}
signal(SIGINT, &get_ctrl_c);
setup(&attr);
if (max_thread == 0)
max_thread = DEFAULT_THREAD;
if (timeout == 0)
timeout = TIMEOUT_FAST;
if (strchr(argv[1],'-') != NULL) {
set_ip(argv[1]);
printf("Start scaning host from %u.%u.%u.%u to %u.%u.%u.%u at %s",
NIPQUAD(start_ip),NIPQUAD(end_ip),ctime(&t_start) + 4);
}
else {
remote_ip = make_network_ip(argv[1]);
if (!remote_ip) {
printf("[-] host error.\n");
exit(-1);
}
start_ip = end_ip = remote_ip;
}
time(&t_start);
printf("Start scaning host %s at %s",argv[1],ctime(&t_start) + 4);
printf("thread : %d | timeout : %d | ",max_thread,timeout);
if (flag_p == 0) {
port_num = (end_ip - start_ip + 1) * PORT_NUM;
printf("total_ports : %d\n",port_num);
for (remote_ip = start_ip; remote_ip <= end_ip; remote_ip++){
if (remote_ip % 256 == 0 || remote_ip %256 == 255)
continue;
scan_port_array(remote_ip,common_ports,PORT_NUM,timeout);
}
goto out;
}
if (flag_p == 1) {
switch(ret) {
case PORT_MODE1:
printf("total_ports : %d\n",short_ports_num);
scan_port_array(remote_ip,short_ports,
short_ports_num,timeout);
break;
case PORT_MODE2:
printf("total_ports : %d\n",
long_ports[0][1] - long_ports[0][0] + 1);
scan_port(remote_ip,long_ports[0][0],
long_ports[0][1],timeout);
break;
case PORT_MODE3:
for (i = 0; i < long_ports_num; i++)
port_num += long_ports[i][1] - long_ports[i][0];
printf("total_ports : %d\n",port_num + short_ports_num);
scan_port_array(remote_ip,short_ports,
short_ports_num,timeout);
for (i = 0; i < long_ports_num; i++)
scan_port(remote_ip,long_ports[i][0],
long_ports[i][1],timeout);
break;
case PORT_MODE4:
printf("total_ports : %d\n",short_ports_num);
short_ports[1] = short_ports[0] + 1;
scan_port_array(remote_ip,short_ports,2,timeout);
break;
default:
printf("[-] -p bad parameters.\n");
exit(-1);
}
}
out:
//wait_thread_end();
my_sleep(1);
time(&t_end);
printf("End scan host %s at %s",argv[1],ctime(&t_end) + 4);
printf("%ld ports opened.\n",total_ports);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -