Scan the port

Source: Internet
Author: User
Tags htons
A few days ago, I started to learn socket. I wrote a single-threaded single-host port scanner for one of the great help of yunshu. Today I want to change it to multi-host scanning, (The thread is still single and slow, but it can be used for learning. It doesn't matter if it is slow), but it won't be changed. Please give me some advice.

/*
Name: netxport
Copyright: netxfly
Author: netxfly
Date: 12-10-04
Description: A simple port restart
*/

# Include <stdio. h>
# Include <string. h>
# Include <winsock2.h>

# Pragma comment (Lib, "ws2_32.lib ")

Void usage (char *); // helper Function
Void scan (char *); // common Port Scan
Void scan1 (char *, char *, char *); // custom Port Scan
Void scan2 (char *, char * 2); // common port scanning for multiple hosts
Void scan3 (char *, char *);
// Multi-host custom Port Scan

//////////////////////////////////////// //////////////////////////
// Main Function
//////////////////////////////////////// //////////////////////////
Int main (INT argc, char * argv [])
{
If (argc = 2)
{
Scan (argv [1]); // common Port Scan
}
Else if (argc = 3) // common port scanning for multiple hosts
{
Scan2 (argv [1], argv [2]);
}
Else if (argc = 4)
{
Scan1 (argv [1], argv [2], argv [3]); // custom Port Scan
}
Else if (argc = 5)
{
Scan3 (argv [1], argv [2], argv [3], argv [4]); // multi-host custom Port Scan
}
Else
{
Usage (argv [0]);
}

}
//////////////////////////////////////// /////////////////////////////////
// Help function void usage (char * Help)
//////////////////////////////////////// /////////////////////////////////

Void usage (char * Help)
{
Printf ("========================================== =======================================/N ");
Printf ("netxport version 1.0 by netxfly 12-10-2004/N ");
Printf ("welcome to visit http://netxfly.126.com/N ");
Printf ("Usage:/N ");
Printf ("% S <target>/T common port scan/N", help );
Printf ("% S <target> <port1> <port2>/T custom Port Scan/N", help );
Printf ("% S <target1> <TARGET2>/T common port scanning for multiple hosts/N", help );
Printf ("% S <target1> <TARGET2> <port1> <port2>/t multi-host custom Port Scan/N", help );
Printf ("========================================== =======================================/N ");
Return;
}
//////////////////////////////////////// ////////////////////////////////////
// Common port scan void scan (char * target)
//////////////////////////////////////// ////////////////////////////////////
Void scan (char * target)
{
Wsadata;
Socket netxfly;
Int I;
Struct sockaddr_in server;
Int ports [12] = {80,110,135,139,445,143, 3389 };
If (wsastartup (makeword (2, 2), & wsadata )! = 0)
{
Printf ("Startup Winsock failed.../N ");
Exit (-1 );
}
For (I = 0; I <12; I ++)
{
If (netxfly = socket (af_inet, sock_stream, 0) = invalid_socket)
{
Printf ("create socket err.../R/N ");
Exit (-1 );
}

Server. sin_family = af_inet;
Server. sin_addr.s_addr = inet_addr (target );
Server. sin_port = htons (ports [I]);
If (connect (netxfly, (struct sockaddr *) & server, sizeof (server ))! = Socket_error)
{
Printf ("port % d is open at host: % S/R/N", ports [I], target );
Closesocket (netxfly );
}
Else
{
Closesocket (netxfly );
}
}
Wsacleanup ();
}
//////////////////////////////////////// ////////////////////////////////////
// Custom Port Scan void scan1 (char * target, char * Start, char * end)
//////////////////////////////////////// ////////////////////////////////////

Void scan1 (char * target, char * Start, char * end)
{
Wsadata;
Socket netxfly;
Int I;
Int port1;
Int port2;
Struct sockaddr_in server;
If (wsastartup (makeword (2, 2), & wsadata )! = 0)
{
Printf ("Startup Winsock failed.../N ");
Exit (-1 );
}
Port1 = atoi (start );
Port2 = atoi (end );
For (I = port1; I <= port2; I ++)
{
If (netxfly = socket (af_inet, sock_stream, 0) = invalid_socket)
{
Printf ("create socket err.../R/N ");
Exit (-1 );
}

Server. sin_family = af_inet;
Server. sin_addr.s_addr = inet_addr (target );
Server. sin_port = htons (I );
If (connect (netxfly, (struct sockaddr *) & server, sizeof (server ))! = Socket_error)
{
Printf ("port % d is open at host: % S/R/N", I, target );
Closesocket (netxfly );
}
Else
{
Closesocket (netxfly );
}
}
Wsacleanup ();
}

//////////////////////////////////////// ///////////////////////////////////
// Common port scan for multiple hosts void scan2 (char * target1, char * TARGET2)
//////////////////////////////////////// ///////////////////////////////////
Void scan2 (char * target1, char * TARGET2)
{

Unsigned long I;
Unsigned long start1;
Unsigned long start2;
Char * target;
Start1 = ntohl (inet_addr (target1); // ascll-> network-> host
Start2 = ntohl (inet_addr (TARGET2); // ascll-> network-> host
For (I = start1; I <= start2; I ++)
{
Target = inet_ntoa (htonl (I); // host-> network-> ASCII
// The host order-> network order-> character point format for scan () to call
Scan (target );
}
}

//////////////////////////////////////// ////////////////////////////////////
// Multi-host custom Port Scan
// Void scan3 (char * target, char * TARGET2, char * Start, char * end)
//////////////////////////////////////// ///////////////////////////////////

Void scan3 (char * target, char * TARGET2, char * Start, char * end)
{

}

I can better understand the code in my blog
Http://www.blogbus.com/blogbus/blog ...? Diaryid = 536479
PS: I wrote it at an Internet cafe, without VC, using Dev C ++ and

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.