Two years ago, the article, passed over the facade.
---------------------------------------------
Production of TCP Connect Scanner under Linux
(Author: mikespook | Published: 2002-12-8 | Views: 111)
Keywords: Linux, Network, Scan, Connect () Preface: This article is just a guideline that is just the same as me. If you are a master, or is not interested in programming. Please don't waste time here. TCP Connect Scan is the most basic scanning method. Establish a reliable connection with the target host to be scanned by the connect () function. Access the specified port. If the port is listed, it is recorded. Otherwise, go to the next port for connection testing. Its advantage is that no privilege is required. As long as you are a user on Linux, you can run. And the speed is very fast. In addition to scanning one by one in this article, multiple Socket can be opened in a non-blocking manner. But TCP Connect has a maximum shortcomings. It is easy to detect, and the preferred host may also filter it. The system log will record it. The scanner's preparation principle is very simple. Below we learn its way from the original code. / *------------------------------------------------- -------------------- * // * gcc -o -o tcpscan scan.c * // * unsafe scan * // * for studing * // * mikespook * // * 2002.5.18 * / # include
#include
#include
#include
#include
#include
/ * This function is used to check the input parameters is the IP address or host name * /
INT CORRECTHOST (Const Char * Host, Struct SockAddr_in * Sock);
INT Main (int Argc, int * argv [])
{
/ * Port variable * /
INT N_PORT, N_BEGIN_PORT, N_END_PORT;
/ * Socket socket * /
INT SOCK_ID;
INT RTN_ERR;
/ * Socket structure * /
Struct SockAddr_in Remote_sock;
IF (argc! = 4) {
Printf ("USAGE: SCAN
/ N ");
Printf ("Writen By MikesPook / N");
Printf ("MikesPook@hotmail.com/twith subject: Report for scan / n");
exit (0);
}
n_begin_port = ATOI (Argv [2]);
N_END_PORT = ATOI (Argv [3]);
Remote_sock.sin_family = AF_INET;
RTN_ERR = CORRECTHOST ((char *) Argv [1], (Struct SockAddr_in *) & Remote_sock);
IF (RTN_ERR! = 0)
Exit (1);
/ * Scan the port one by one using the for loop * /
For (n_port = n_begin_port; n_port <= n_end_port; n_port ) {
/ * In order to ensure good operation on different processors, you must use the HTONS () function to process the incoming port number * /
Remote_sock.sin_port = htons (n_port);
SOCK_ID = Socket (AF_INET, SOCK_STREAM, 0); / * Initialize a Socket * /
/ * If the initialization fails to output an error, and exit the program * / if (SOCK_ID <0) {
PERROR ("/ nsocket");
EXIT (2);
}
/ * Connect to the host * /
RTN_ERR = Connect (Sock_ID, (Struct SockAddr *) & transote_sock, sizeof (transote_sock);
/ * If the connection is successful, the output port * /
IF (RTN_ERR <0) {
Fflush (stdout);
}
Else {
Printf ("% S:% D Accepted./N", Argv [1], N_PORT);
/ * Turn off the output, enter the connection * /
IF (SHUTDOWN (SOCK_ID, 2) <0) {
PERROR ("/ nshutdown");
EXIT (2);
}
}
/ * Close the socket. If you don't close your socket or put this sentence outside of the loop, you will fail because it is opened too much socket. * /
Close (SOCK_ID);
}
Printf ("scan over! / n");
exit (0);
}
/ * Check the parameters is IP or host name * /
INT CORRECTHOST (Const Char * Host, Struct SockAddr_in * Sock)
{
Struct hostent * struct_host;
/ * If the input parameter * Host is a number, it is considered an IP address, otherwise it is considered to enter the host name * /
IF (Isdigit (* Host))
SOCK-> SIN_ADDR.S_ADDR = INET_ADDR (Host);
Else {
Struct_host = gethostByName (Host);
/ * If the host name is successfully acquired, it will be incorporated into the SockAddr_in structure, otherwise the output error exits. * /
IF (struct_host! = null) {
Bcopy (struct_host-> h_addr, (char *) & SOCK-> SIN_ADDR, STRUCT_HOST-> H_LENGTH);
}
Else {
Printf ("Get Error with Host Name./N");
Return (-1);
}
}
Return (0);
}
-------------------------------------------------- -----------------------------
Finally, I still want to explain, you can also use fork () multi-process scan. Or open multiple sockets and then scan with multiple Socket's non-blocking mode. And you can get a further test through the READ () and Write () functions after obtaining the available Port list. Everything is played freely.
Since I am a rookie, maybe there is anything wrong. I may also have some details I have not considered. If you know what you want to advice.