# Include <afxext. h>
# Include <Winsock. h>
// The library to be used during compilation
# Pragma comment (Lib, "wsock32.lib ")
// Select () member Definition
# Define zero (fd_set *) 0
// Variable definition
Int maxth, scanok, scannum;
Int portip, hoststart, hoststop, startport, endport; // defines the start I and end P addresses, and the start and end ports.
Long searchnum, searched;
Void usage (char *); // define the display usage Function
Void playx (INT); // defines the function for prompting the status.
Void setip2 (char *); // defines the IP address setting function.
Void customport (char *, char *, char *); // you can customize the port scanning function.
Void portscannow (INT); // defines Port Scan
// Main program
Int main (INT argc, char * argv [])
{
Wsadata;
// Clear screen
System ("cls.exe ");
// Display version information
Printf ("/R/n =========================== huc command line portdeskv0.2 ========== ============ ");
Printf ("/R/n ======================= by lion, welcome to [url] http://www.cnhonker.net [/url] =======================/R/n ");
// Check input
If (argc <3) | (argc> 4 ))
{
// Display the help prompt
Usage (argv [0]);
Return-1;
}
// Check whether the scan is port scanning
If (! (Stricmp (strlwr (argv [1]), "-P") = 0 ))
{
Usage (argv [0]);
Return-1;
}
// Program Initialization
If (wsastartup (makeword (1,1), & wsadata )! = 0) // If Initialization is incorrect
{
Printf ("/R/nwsatartup error"); // error message
Return-1;
}
// Port scan parameter conversion
// If the parameter is set to three
If (argc = 3)
{
// Directly set the IP address
Setip2 (argv [2]);
}
// If the parameter is four
Else
If (argc = 4)
{
// Enter the custom Port Scan Process
Customport (argv [0], argv [2], argv [3]);
}
// Too many parameters show help
Else
{
Usage (argv [0]);
Return-1;
}
// Start the scan Port
Portscannow (argc );
Wsacleanup ();
Return 0;
}
// Help prompt Function
Void usage (char * prog)
{
Printf ("Usage: % S <option>", prog );
Printf ("/R/n <option> :");
Printf ("/R/n-P [port | startport-endport] Printf ("/R/n example :");
Printf ("/R/n % s-p 192.168.0.1", prog );
Printf ("/R/n % s-p 192.168.0.1-192.168.0.254", prog );
Printf ("/R/n % s-p 21-80 192.168.0.1", prog );
Printf ("/R/n % s-p 21-80 192.168.0.1-192.168.0.254/R/N", prog );
Return;
}
// Progress prompt
Void playx (INT play = 0)
{
// Progress bar
Char * plays [12] =
{
"| ",
"/",
"-",
"//",
"| ",
"/",
"-",
"//",
"| ",
"/",
"-",
"//",
};
If (searchnum! = 0)
{
For (INT I = 0; I <= 3; I ++)
{
Printf ("= % s = % d % s completed./R", plays [I], searched * 100/(searchnum + 1), "% ");
Sleep (5 );
}
}
Else
{
Printf ("= % s =/R", plays [play]); // display the progress
Sleep (10 );
}
}
// Set the IP address
Void setip2 (char * CP)
{
Int host;
Struct hostent * testhost;
Char * startip = "", * endip = "";
// Determine whether the input is in the format of 192.168.0.1-192.168.0.254
If (strstr (CP, "-") & strlen (CP)> 15 & strlen (CP) <32)
{
// Extract the end IP Address
Endip = strchr (CP, '-') + 1;
// Extract the starting IP Address
Strncpy (startip, CP, strlen (CP)-strlen (strchr (CP ,'-')));
// Assign a value to the variable controlling the IP segment to be scanned
Hoststart = ntohl (inet_addr (startip ));
Hoststop = ntohl (inet_addr (endip ));
}
Else
{
// Obtain the entered host address
Testhost = gethostbyname (startip );
// If the address does not exist
If (! Testhost)
{
Wsacleanup ();
Printf ("/R/ncan't Get IP of: % s", CP );
Exit (-1 );
}
// Assign a value to the variable controlling the IP segment to be scanned
Memcpy (& host, testhost-> h_addr, 4 );
Hoststop = hoststart = ntohl (host );
}
}
// Test whether the thread is full
Void testthread (INT thread = 200)
{
For (;;)
{
Playx ();
// Test whether the thread is full
If (maxth> thread)
Sleep (100 );
Else break;
}
Return;
}
// Wait for the thread to end the Function
Void waitthreadend ()
{
// Latency
Sleep (6000 );
// Display the waiting prompt
Printf ("/R/N ");
Printf ("Wait (% d) thread end.../R/N", maxth );
For (;;)
{
// Determine whether all threads have ended
If (maxth> 0)
{
// Wait for the end of the program
Sleep (100 );
Playx ();
Continue;
}
Else break;
}
Printf ("/R/N ");
Return;
}
// Customize port scan parameter Definitions
Void customport (char * CP, char * CP2, char * CP3)
{
Int intport;
Char * checker;
// Process the port to be scanned
// Value assigned to the variable at the start of scanning
Startport = atoi (CP2 );
// Value assigned to the scan end port variable
Endport = atoi (CP2 );
// Determine whether the 21-80 format is used
If (strstr (CP2 ,"-"))
{
Intport = atoi (checker = strchr (CP2, '-') + 1 );
If (intport> 0 & intport <65536)
// Value assigned to the scan end port variable
Endport = intport;
}
// Judge the port size
If (startport <0 | startport> 65536 | endport <0 | endport> 65535)
{
Usage (CP );
Exit (-1 );
}
// Process the IP address
Setip2 (CP3 );
}
// Port Scan Function
Uint portscan (lpvoid port)
{
Int ADDR = portip; // get the address to be scanned
Int sock;
Struct fd_set mask;
Struct timeval timeout;
Struct sockaddr_in server;
Unsigned long flag = 1;
// Create a sock
Sock = socket (af_inet, sock_stream, 0 );
// Handle sock creation failure
If (sock = invalid_socket)
{
Printf ("/R/nsock error: % s", wsagetlasterror ());
Maxth --;
Return-1;
}
// Assign values to sock members
Server. sin_family = af_inet;
Server. sin_addr.s_addr = htonl (ADDR); // address to be scanned
Server. sin_port = htons (short (port); // The port to be scanned
// Display progress
Playx ();
// Call ioctlsocket () to set the socket to non-blocking mode
If (ioctlsocket (sock, fionbio, & flag )! = 0)
{
// Handle setting failure
Printf ("/R/nsock error: % s", wsagetlasterror ());
Closesocket (sock );
Maxth --;
Return-1;
}
// Call connect () to connect to the remote host port
Connect (sock, (struct sockaddr *) & server, sizeof (server ));
Timeout. TV _sec = 18; // The timeout limit is 18 seconds.
Timeout. TV _usec = 0;
Fd_zero (& Mask); // clear the set mask
Fd_set (sock, & Mask); // put sock into the set mask
// Use select () to process scan results
Switch (select (sock + 1, zero, & Mask, zero, & timeout ))
{
Case-1:
{
Printf ("/R/nselect () error ");
Maxth --;
Return-1;
}
// Sock timeout Processing
Case 0:
{
Maxth --;
Closesocket (sock );
Return-1;
}
Default:
If (fd_isset (sock, & Mask ))
{
// Disable sock from sending and receiving data
Shutdown (sock, 0 );
// Set the output result format
Printf ("[found:] % s port: % d open./R/N", inet_ntoa (server. sin_addr), ntohs (server. sin_port ));
// Disable sock
Closesocket (sock );
Scanok ++;
Maxth --;
Return 1;
}
}
Return 0;
}
// Main function for scanning
Void portscannow (int xp)
{
Int sport;
Char * timenow, timebuf [32];
// Define the default scan Port
Char * ports [32] = {
"21 ",
"22 ",
"23 ",
"25 ",
"53 ",
"79 ",
"80 ",
"110 ",
"111 ",
"113 ",
"123 ",
"135 ",
"139 ",
"143 ",
"443 ",
"512 ",
"513 ",
"514 ",
"515 ",
"540 ",
"1080 ",
"1433 ",
"1521 ",
"1524 ",
"3306 ",
"3389 ",
"5631 ",
"6000 ",
"6112 ",
"8000 ",
"8080 ",
"12345" // You can also define the port to be scanned here
};
// Display the scanning Start Time
Timenow = _ strtime (timebuf );
Printf ("/R/nportscan Start Time: % S/R/n", timenow );
// Counter initialization.
Maxth = 0;
Scanok = 0;
Scannum = 0;
Searched = 0;
// Calculate the number of ports to be scanned
Searchnum = hoststop-hoststart + 1;
If (XP = 3)
Searchnum = searchnum * 32;
If (XP = 4)
Searchnum = searchnum * (endport-startport + 1 );
// Port scan starts
For (portip = hoststart; portip <= hoststop; portip ++, scannum ++)
{
// *. 0 and *. 255 address Processing
If (portip % 256) = 0 | (portip % 256) = 255)
{
If (XP = 3)
Searchnum = searchnum-32;
If (XP = 4)
Searchnum = searchnum-(endport-startport + 1 );
Scannum --;
Playx ();
Continue;
}
If (I> 11) I = 0;
// Default Port Scan
// Scan 192.168.0.1
// Scan 192.168.0.1-192.168.0.254
If (XP = 3)
{
For (sport = 0; sport <32; sport ++, maxth ++, searched ++)
{
// Test if the current thread is greater than 180
Test thread (180 );
// Generate a new thread to process port scanning
Cwinthread * pthread = afxbeginthread (portscan, lpvoid (atoi (char *) ports [sport]);
// Latency
Sleep (120 );
}
}
// Custom Port Scan
// Scan-P 21 192.168.0.1
// Scan-P 21-80 192.168.0.1-192.168.0.254
If (XP = 4)
{// Calculate the port to be scanned
Sport = endport-startport;
If (sport> 500)
{
// Scan the custom Port
For (sport = startport; sport <= endport; sport ++, maxth ++, searched ++)
{
Test thread (2000 );
// Generate a new thread to process port scanning
Cwinthread * pthread = afxbeginthread (portscan, lpvoid (sport ));
// Latency
Sleep (10 );
}
}
Else
{
// Scan the custom Port
For (sport = startport; sport <= endport; sport ++, maxth ++, searched ++)
{
// Test if the current thread is greater than 250
Test thread (250 );
// Generate a new thread to process port scanning
Cwinthread * pthread = afxbeginthread (portscan, lpvoid (sport ));
// Latency
Sleep (100 );
Playx ();
}
}
}
}
// Wait until all threads end
Waitthreadend ();
// Display the Port Scan End Time
Timenow = _ strtime (timebuf );
Printf ("/R/nportscan End Time: % s", timenow );
Printf ("/R/nscan % d hosts completed. Open % d ports! /R/N ", scannum, scanok );
}