A simple port scanning program

Source: Internet
Author: User

I. TCP Scanning Technology
There are many common port scanning technologies, such as TCP connect () scanning, tcp syn scanning, tcp fin scanning, and many other articles on the network, for example, on the http://www.antai-genecon.com/suml/zhishiyy/jingong/duankougj.htm, I introduced a lot of the most basic scanning technology used by my program: TCP scanning.
The connect () System Call provided by the operating system is used to connect to the port of each target computer of interest. If the port is in the listening status, connect () will be successful. Otherwise, this port is unavailable, that is, it does not provide services. One of the biggest advantages of this technology is that you do not need any permissions. Any user in the system has the right to use this call. Another benefit is speed. If you use a separate connect () call to each target port in a linear way, it will take a long time. You can open multiple sockets at the same time to accelerate scanning. The use of non-blocking I/O allows you to set a low time consumption cycle, while observing multiple sockets. However, the disadvantage of this method is that it is easy to detect and filtered out. The logs file of the target computer displays a series of connection and connection error service messages, and can be quickly disabled.
The author suggested that scanning others' computer ports without permission is illegal. This program only shows a port scanning technology. Please do not use it for illegal purposes; otherwise, the consequences will be borne by you.
Ii. Program Introduction
To improve scanning speed, this program uses multithreading and non-blocking I/O technologies. The main interface of the program is a dialog box. The following is the program framework:

1. Global variables:
The following are definitions of all global variables:
HWND g_hWnd = NULL; // handle of the window for processing messages
Unsigned long g_ulAddr = INADDR_NONE; // scan host address
DWORD g_dwTimeOut = 1000; // connection timeout, in ms
Bool g_bTerminate = false; // specifies whether the user sends a flag to end the scan.
Short g_nMaxThread = 200; // The maximum number of scan threads allowed. The number of scan threads cannot exceed 200.
Short g_nThreadCount = 0; // number of processes currently being scanned
2. StartScan thread:
The task completed by this thread is to start a specific scan DoScanPort thread to scan a port. The maximum number of DoScanPort threads started by this process is within the set value range. If the maximum number of threads has been reached, it will wait for some threads to end before starting a new thread. If the user sends an end scan message, a new thread is no longer started, and a result is returned after the started thread ends. The following is the execution body code of this thread:
Dword winapi StartScan (LPVOID lpParam)
{
Tag_PORTS * pScanParam = (tag_PORTS *) lpParam;

DWORD dwThreadId;
Unsigned short I;
If (pScanParam-> bSepecifiedPort)
{
For (I = 0; I <= pScanParam-> nCount; I ++)
{
If (g_bTerminate)
{
Break; // The user has issued an end scan command
}
While (g_nThreadCount> = g_nMaxThread)
{
Sleep (10 );
}

If (CreateThread (NULL,
0,
DoScanPort,
(Lpvoid) New short (pscanparam-> narrofports ),
0,
& Dwthreadid )! = NULL)
{
G_nthreadcount ++;
}
}
}
Else
{
For (I = pscanparam-> istartport; I <= pscanparam-> iendport; I ++)
{
If (g_bterminate)
{
Break; // The user has issued an end scan command
}
While (g_nthreadcount> = g_nmaxthread)
{
Sleep (10 );
}
If (createthread (null, 0, doscanport, (lpvoid) New short (I), 0, & dwthreadid )! = NULL)
{
G_nthreadcount ++;
}
}
}

// Wait until the scanning thread of each port ends
While (g_nthreadcount> 0)
{
Sleep (50 );
}
: SendMessage (g_hWnd, SCAN_THREAD, STARTSCAN_COMPLETE, 0 );
Delete pScanParam;
Return ERROR_SUCCESS;
}
3. DoScanPort thread:
This thread is responsible for scanning the specified port and sending the result SendMessage to the main dialog box. The following is the code:
Dword winapi DoScanPort (LPVOID lpParam)
{
DWORD dwRet;
Short nPort = * (short *) lpParam;
Delete lpParam;

SOCKET sock = socket (AF_INET, SOCK_STREAM, 0 );
If (sock = INVALID_SOCKET)
{
AfxMessageBox ("failed to create socket !");
DwRet = ERROR_CREATE_SOCKET;
}
Else
{
Unsigned long flag = 1;
If (ioctlsocket (sock, FIONBIO, & flag )! = 0 ))
{
AfxMessageBox ("failed to change to non-blocking mode !");
DwRet = ERROR_MODIFY_FIONBIO;
}
Else
{
Sockaddr_in severAddr;
SeverAddr. sin_family = AF_INET;
SeverAddr. sin_port = htons (nPort );
SeverAddr. sin_addr.S_un.S_addr = g_ulAddr;
Connect (sock, (sockaddr *) & severAddr, sizeof (severAddr ));

Struct fd_set mask;
FD_ZERO (& mask );
FD_SET (sock, & mask );

Struct timeval timeout;
Timeout. TV _sec = g_dwTimeOut/1000;
Timeout. TV _usec = g_dwTimeOut % 1000;

Switch (select (0, NULL, & mask, NULL, & timeout ))
{
Case-1:
DwRet = ERROR_SELECT;
Break;

Case 0:
DwRet = ERROR_SELECT_TIMEOUT;
Break;

Default:
DwRet = ERROR_SUCCESS;
};
}
Closesocket (sock );
}
G_nthreadcount -;
If (dwret = error_success)
{
: Sendmessage (g_hwnd, scan_thread, doscan_find_port, nport );
}
Else
{
: Sendmessage (g_hwnd, scan_thread, doscan_end_port, nport );
}
Return dwret;
}
Iii. Running results
This program is compiled and debugged in vc6 + WINXP and runs correctly in Win98.
Scan Port 1-1000 on my computer and set the timeout value to 200 ms. the maximum number of threads is, which takes about 45 seconds. When the timeout setting is shorter, the speed can reach 150 ports per second.
Iv. Conclusion
In fact, other methods may be required to increase the speed. If the number of threads is too large, the scheduling overhead of the thread will decrease the speed. If the timeout setting is too short, the scan result may be incorrect (depending on the Network), and because it is multi-thread, the timeout wait time may be less than the waiting time due to thread scheduling, so the speed cannot be increased.
In addition, I also found a problem: when scanning the local machine, if the IP address 127.0.0.1 is used, port 139 # is not open; if it is set to the actual IP address of the Local Machine, port 139 # is open. I do not know why, but I still hope to have a high finger point.
This program also needs to be improved, that is, unlike most port scanners that can scan specified IP segments, it is designed to scan a specified host.

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.