Windows下兼容 IPv4IPv6 的扫描软件Scan6源代码

原创 2007年09月14日 01:45:00
/*
scan6
written by Marco Del Percio
09/11/2004

DESCRIPTION: Scan6 is a simple IPv4/IPv6 single host portscanner.
It can check for ANY open port on a single IPv4/IPv6 host or you
can specify a startport and an endport if you don't want to check
all the 65535 ports. Scan6 requires a mandatory option in order
to specify if you are going to scan an IPv4 or an IPv6 address.
Scan6 is a high-multithreading console application.

REQUIRES: This application is designed to work on Windows 2000/XP however
it was only tested on Windows XP SP1. To use it with IPv6 addresses you
MUST have an IPv6 address (so you should have Microsoft IPv6 Tech. Preview
if you are using Windows 2k).
To compile this you need:
1)Platform SDK (updated)
2)Microsoft VC++6.0
3)IPv6 installed

NOTE: This application was only tested on Windows XP SP1!
This application uses a lot of threads if you specify "big" port-range.
So checking all 65535 ports will take much time, DO NOT TERMINATE the program ;-P
just wait ;)
IMPORTANT: If you don't specify startport and endport (65535 ports), I have decided
to use 257 threads with every thread checking for 255 ports using a single socket.
On the other side, if you specify a startport and an endport (smaller range) I have
decided to use X thread (where X can be max 1285) with every thread checking for
51 ports using a single socket, this way if the range is small there are more threads
that means more sockets and "generally" means more speed in small port ranges.

*/
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#ifndef IPPROTO_IPV6
#include <tpipv6.h> // For IPv6 Tech Preview.
#endif
#include <ws2tcpip.h>
#include <stdio.h>
#include <process.h>

#pragma comment(lib, "ws2_32")

//Global Variables
HANDLE TID[257];
HANDLE *TIARRAY;
WSADATA wsaData;
char *server;
int stport, edport, avanzo, totalthreads;
short family;

//Threads declaration
void Scan(void *p);
void ScanPort(void *q);

int main(int argc, char *argv[]) {

int RetVal, i, q, totalports;

if(!(argc == 3 || argc == 5)) {
printf("/nERROR! Usage: %s <-4|-6> <ipv4/ipv6_host_to_scan> [startport] [endport]/n/nNOTE: If you want to check /"not so big/" port range/nspecify a startport and an endport, the scanner will go faster/n/n", argv[0]);
exit(-1);
}
//family protocol (mandatory)
if((RetVal = atoi(argv[1])) == -4) {
family = AF_INET;
}
else if((RetVal = atoi(argv[1])) == -6) {
family = AF_INET6;
}
else {
fprintf(stderr, "/nERROR! The first option must be -4 or -6/n");
exit(-1);
}
//IPv4/IPv6 host or IP address (mandatory)
server = argv[2];

if ((RetVal = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0) {
    fprintf(stderr, "/nWSAStartup failed with error %d/n", RetVal);
    WSACleanup();
    exit(-1);
}

//we are checking a specific port range from a startport to an endport
if(argc == 5) {
stport = atoi(argv[3]);
edport = atoi(argv[4]);
if(stport >= edport) {
fprintf(stderr, "/nERROR! Endport must be higher than startport.../n");
WSACleanup();
exit(-1);
}
if(stport < 1 || stport > 65535 || edport < 1 || edport > 65535) {
fprintf(stderr, "/nERROR! Ports must be values between 1 and 65535/n");
WSACleanup();
exit(-1);
}
//1 single thread will check 51 open ports
totalports = edport - stport + 1; //total number of ports
totalthreads = (int)(totalports / 51); //total number of necessary threads
avanzo = totalports % 51; //missing ports to be added to the last thread
if(avanzo > 0) {
totalthreads++;
}
printf("/n/n----IPv4/IPv6 PortScanner for Windows 2k/XP----/n/nBy LeVante^/n/n----Scan6----/nHost/IP Address: %s/nStart Port: %d/nEnd Port: %d/n----PortScanning just started. Please wait----/n/n", server, stport, edport);
TIARRAY = (HANDLE) malloc(totalthreads*sizeof(HANDLE));

for(i=0;i<totalthreads;i++) {
TIARRAY[i] = CreateMutex(NULL, FALSE, NULL);
_beginthread( ScanPort, 0, (void *) i);
}
Sleep(100);
//waits for all threads to end
WaitForMultipleObjects(totalthreads, TIARRAY, TRUE, INFINITE);

for(q=0;q<totalthreads;q++)
CloseHandle(TIARRAY[q]);

}
//we are checking ALL the 65535 ports
else {
stport = 1;
edport = 65535;

printf("/n/n----IPv4/IPv6 PortScanner for Windows 2k/XP----/n/nBy LeVante^/n/n----Scan6----/nHost/IP Address: %s/nStart Port: %d/nEnd Port: %d/n/nNOTE: If you want to check /"not so big/" port range/nspecify a startport and an endport, the scanner will go faster/n/n----PortScanning just started. Please wait----/n/n", server, stport, edport);
//1 single thread uses 1 socket to check 255 ports. I use 257 threads. So 255*257 = 65535 ports
for(i=0;i<257;i++) {
TID[i] = CreateMutex(NULL, FALSE, NULL);
_beginthread( Scan, 0, (void *) i);
}
Sleep(100);
//waits for all threads to end
WaitForMultipleObjects(257, TID, TRUE, INFINITE);

for(q=0;q<257;q++)
CloseHandle(TID[q]);
}

printf("/n/n----Port Scan terminated----/n");
WSACleanup();
exit(0);
}

void Scan(void *p) {

int id = (int) p;
int connfd, Ret, startport, endport, r;
ADDRINFO Hints, *AddrInfo;
char portstr[6];
SOCKET sock;
struct sockaddr_in servaddr;
struct sockaddr_in6 *servaddr6 = NULL;

WaitForSingleObject(TID[id], INFINITE);

startport = id * 255 + 1;
endport = startport + 255 - 1;

//one socket per thread
if((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET) {
fprintf(stderr, "Thread with id: %d couldn't create Socket/n", id);
ReleaseMutex(TID[id]);
_endthread();
}
if(family == AF_INET) {
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_addr.s_addr = inet_addr(server);
servaddr.sin_family = family;

for(r=startport;r<=endport;r++) {
servaddr.sin_port = htons(r); //we only change the port, this way struct reuse is maximized

if((connfd = connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr))) == 0) {
  printf("Port %d OPEN/n", r);
}

}
}
else if(family == AF_INET6) {
memset(&Hints, 0, sizeof(Hints));
Hints.ai_family = (int) family;
Hints.ai_socktype = SOCK_STREAM;
sprintf(portstr, "%d", startport);
//getaddrinfo is called only once just to fill the AddrInfo->ai_addr structure
if((Ret = getaddrinfo(server, portstr, &Hints, &AddrInfo)) < 0) {
fprintf(stderr, "Unable to resolve %s/n", server);
fflush(stderr);
ReleaseMutex(TID[id]);
_endthread();
}

servaddr6 = (struct sockaddr_in6 *)AddrInfo->ai_addr;


for(r=startport;r<=endport;r++) {
servaddr6->sin6_port = htons(r); //we only change the port, this way struct reuse is maximized

if((connfd = connect(sock, (struct sockaddr *) servaddr6, sizeof(struct sockaddr_in6))) == 0) {
  printf("Port %d OPEN/n", r);
}

}
}

closesocket(sock);
ReleaseMutex(TID[id]);
_endthread();
}

void ScanPort(void *q) {

int id = (int) q;
int connfd, Ret, startport, endport, r;
ADDRINFO Hints, *AddrInfo;
char portstr[6];
SOCKET sock;
struct sockaddr_in servaddr;
struct sockaddr_in6 *servaddr6 = NULL;

WaitForSingleObject(TIARRAY[id], INFINITE);

startport = id * 51 + stport;
if(id == (totalthreads - 1) && avanzo > 0) {
endport = startport + (avanzo - 1);
}
else {
endport = startport + 50;
}
//one socket per thread
if((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET) {
fprintf(stderr, "Thread with id: %d couldn't create Socket/n", id);
ReleaseMutex(TIARRAY[id]);
_endthread();
}

if(family == AF_INET) {
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_addr.s_addr = inet_addr(server);
servaddr.sin_family = family;

for(r=startport;r<=endport;r++) {
servaddr.sin_port = htons(r); //we only change the port, this way struct reuse is maximized

if((connfd = connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr))) == 0) {
  printf("Port %d OPEN/n", r);
}

}
}
else if(family == AF_INET6) {
memset(&Hints, 0, sizeof(Hints));
Hints.ai_family = (int) family;
Hints.ai_socktype = SOCK_STREAM;
sprintf(portstr, "%d", startport);
//getaddrinfo is called only once just to fill the AddrInfo->ai_addr structure
if((Ret = getaddrinfo(server, portstr, &Hints, &AddrInfo)) < 0) {
fprintf(stderr, "Unable to resolve %s/n", server);
fflush(stderr);
ReleaseMutex(TIARRAY[id]);
_endthread();
}

servaddr6 = (struct sockaddr_in6 *)AddrInfo->ai_addr;


for(r=startport;r<=endport;r++) {
servaddr6->sin6_port = htons(r); //we only change the port, this way struct reuse is maximized

if((connfd = connect(sock, (struct sockaddr *) servaddr6, sizeof(struct sockaddr_in6))) == 0) {
  printf("Port %d OPEN/n", r);
}

}
}

closesocket(sock);
ReleaseMutex(TIARRAY[id]);
_endthread();
}

/* EOF */ 

Ciclop开源3D扫描仪软件---Horus源码分析之laser_segmentation.py

/****************************************************************************/  *  *                ...
  • zouyu1746430162
  • zouyu1746430162
  • 2017年12月10日 10:44
  • 107

Windows下安装Ubuntu16.04 及 Ubuntu常用软件配置 完全版 ~吐血整理!

前言大家好,由于我项目需要,需要装Ubuntu系统,本以为很简单,结果花费了一周的时间来熟悉系统安装,环境配置和软件安装。由于是新手,在学习过程中遇到了很多问题,所以查阅了大量的资料,感谢网络给我们带...
  • xuluohongshang
  • xuluohongshang
  • 2017年07月23日 20:35
  • 3381

C/C++ Windows移植到Linux兼容库移植

近期写了有关Socket的程序,需要从windows移植到linux。现把有用的东东收集整理记录下来。   1.头文件 windows下winsock.h或winsock2.h; li...
  • u010871058
  • u010871058
  • 2017年12月07日 15:08
  • 91

windows下编译postgresql源码

原文:http://blog.csdn.net/wangxingxing2006/article/details/7185926         本文主要是对postgreSQL数据库在wind...
  • xiaoxinyu316
  • xiaoxinyu316
  • 2015年02月03日 22:39
  • 784

国内的windows硬件检测软件算是很牛逼的吧!!!?

这几天的实践表明,免费的,windows操作系统上硬件驱动检测软件,国内做的挺牛逼的。虽然这些软件有些流氓,比如鲁大师,比如驱动精灵。但是,在硬件检测方面确实挺厉害的,客官的说。缘由: 最近在搞op...
  • wangshubo1989
  • wangshubo1989
  • 2016年07月07日 01:19
  • 12774

两款WiFi无线网络扫描工具软件:WirelessMon、Xirrus WiFi Inspector

两款WiFi无线网络扫描工具软件:WirelessMon、Xirrus WiFi Inspector         WirelessMon是一款早期经典的无线网络扫描工具,它最大的特点就是简单易...
  • e_wsq
  • e_wsq
  • 2011年12月01日 13:16
  • 4590

windows上更好的命令行工具软件

windows自带的console cmd用起来太不爽了,尝试了一些其他的:看下来是powercmd和cmder比较好,而且两个都可以和total commander结合起来,很不错。 相比之下, ...
  • ccanan
  • ccanan
  • 2016年04月27日 10:01
  • 5999

使用Windows API实现SOCKET Ping 范例源代码

http://www.okbase.net/file/item/34093
  • aasmfox
  • aasmfox
  • 2017年08月20日 16:18
  • 213

Android自由行之走进zxing,轻松实现二维码扫描

现在很多App都集成了扫一扫功能,最常用的微信、QQ、手机助手等。二维码也使得生活变得更加简洁,扫一扫订餐、扫一扫下载等等。那么,说到二维码,我们不得不提Google一个开源的扫码框架:zxing。 ...
  • bingqingsuimeng
  • bingqingsuimeng
  • 2016年05月17日 14:55
  • 5706

(编译原理)C++源代码单词扫描程序(词法分析)

实验内容   (1)C++源代码扫描程序识别C++记号。 C++语言包含了几种类型的记号:标识符,关键字,数(包括整数、浮点数),字符串、注释、特殊符号(分界符)和运算符号等。  (2)打开一个C...
  • lonely_Quan
  • lonely_Quan
  • 2015年03月06日 23:55
  • 2270
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Windows下兼容 IPv4IPv6 的扫描软件Scan6源代码
举报原因:
原因补充:

(最多只允许输入30个字)