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 */ 

相关文章推荐

[Hacking]重新审视IPv6的漏洞扫描和渗透测试

IPv6带来了一些可喜的安全性和其它特性,但是对于IP网络的专业人士来说,可能还存在着一些潜在的问题。   随着IPv4地址逐渐耗尽,下一代IPv6协议势必闪亮登场。许多人为IPv6所提供的安全...

iOS IPv6-only 的兼容性解决方案

6月1日后所有应用必须支持IPv6-only网络 首先解释下IPv6的是什么? 维基百科的定义如下: IPv6是Internet Protocol version 6的缩写 全名为互联网通讯协议...

iOS应用兼容IPv6

在WWDC2015上苹果宣布iOS9将支持纯IPv6的网络服务。2016年初开始所有提交到App Store的应用必须支持IPv6。为确保现有的应用是兼容的,我们需要注意下面几点。 不建议使用底...

iOS——socket适配ipv6,同时兼容ipv4

前些天提交代码,被苹果打回了,苹果回复:Specifically, we were unable to access the app. We've attached screenshot for yo...

C++ IPv4与IPv6的兼容编码

这里不再对IPv6 socket相关编程的基础知识进行讲解,只提供一个IP协议无关的服务端和客户端的代码,仅供参考。 服务端代码: #include #include #include #inc...

针对苹果iOS最新审核要求为应用兼容IPv6

最新消息今天苹果推出重磅消息,6月1日后所有应用必须支持IPv6-only网络!!!当iOS开发者看到这个消息的第一反应可能就是IPv6是个什么鬼!!其实IPv6早在很早之前就已经推出,而且苹果在20...

针对苹果iOS最新审核要求为应用兼容IPv6

最新消息 今天苹果推出重磅消息,6月1日后所有应用必须支持IPv6-only网络!!!当iOS开发者看到这个消息的第一反应可能就是IPv6是个什么鬼!!其实IPv6早在很早之前就已经推出,而且苹...
  • fishmai
  • fishmai
  • 2016年05月10日 14:26
  • 867

让你的Socket应用兼容IPv6

随着互联网越来越普及,以及物联网的兴起,IPv4地址已远远不够用,IPv6的普及将是不可避免的趋势。以前,我们的大部分socket程序几乎都是针对IPv4而开发,如果不做升级重构,那么使用IPv6地址...

iOS APP 支持IPv6-only的注意事项及兼容性考虑

iOS APP 提交到AppStore要 支持IPv6-onlyIPv4 和 IPv6的区别就是 IP 地址前者是 . (dot)分割,后者是以 :(冒号)分割的。在WWDC2 015上苹果宣布iOS...

Socket如何兼容IPV6

IPV6简要: 什么是IPv6什么是IPv6?IPv6是下一版本的互联网协议,也可以说是下一代互联网的协议,它的提出最初是因为随着互联网的迅速发展,IPv4定义的有限地址空间将被耗尽,地址空间的不足...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Windows下兼容 IPv4IPv6 的扫描软件Scan6源代码
举报原因:
原因补充:

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