Click here to Skip to main content
6,595,854 members and growing! (21,604 online)
Email Password   helpLost your password?
General Programming » Internet / Network » General     Intermediate

IP Hole Finder and Port Scanner

By Alex Douma

Security
VC6Win2K, MFC, Dev
Posted:12 Mar 2001
Views:108,061
Bookmarked:41 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
30 votes for this article.
Popularity: 5.80 Rating: 3.93 out of 5
3 votes, 33.3%
1
1 vote, 11.1%
2
1 vote, 11.1%
3

4
4 votes, 44.4%
5
  • Download source files - 14 Kb
  • Download demo project - 60 Kb
  • Disclaimer

    The techniques discussed in this article are meant to aid Network Administrators in finding security holes in their own sysmtes in order to close potential points of weakness. These techniques must not be used to gain illegal eentry to remote servers.

    Introduction

    Internet world is full of exciting tricks and mysteries. This giant world can be more attractive when a computer programmer knows the Internet protocols and how to write codes to handle connections. We, computer programmers, know that TCP/IP protocol claims "All Hard Disks of online computers through the world are mine!" and this idea leads hackers to intrude our valuable information from any Holes on our PCs. Their hacking applications can interpret TCP/IP-based protocols such as FTP, HTTP, etc. and that is why they can do whatever with our servers. 

    What are we supposed to do to prevent their attack? If you are a network administrator, you will say that we should close unneeded ports as the first solution. In the other hand, we must find IP Holes and then close them.

    This article can give you some helpful ideas to detect these holes. The submitted code and application can be the bases of Port Scanners.

    How does it work?

    This application uses class CTheSocket inherited from class CSocket. I could use a CSocket object instead but I preferred to inherit from this class to override any desired events in the future. The member function CPortScanView::TestConnection(CString IP, UINT nPort) is the heart of port scanning. Please see the following code:

    
    BOOL CPortScanView::TestConnection(CString IP, UINT nPort)
    {
    	CTheSocket* pSocket;
    	pSocket = new CTheSocket;
    	ASSERT(pSocket);
    
    	if (!pSocket->Create())
    	{
    		delete pSocket;
    		pSocket = NULL;
    		return FALSE;
    	}
    
    	while (!pSocket->Connect(IP , nPort))
    	{
    		delete pSocket;
    		pSocket = NULL;
    		return FALSE;
    	}
    
    	pSocket->Close();
    	delete pSocket;
    	return TRUE;
    }
    
    

    In the above code if connection with the specified socket on port nPort is established, the member function will return TRUE, otherwise FALSE. This member function does not have to know Internet services protocols like HTTP, FTP, etc. to interpret them to find out whether or not the port is open. As a matter of fact the member function checks which ports are listening to establish connection. As soon as the state of socket changes from mode 'Listening' to mode 'Established' the value TRUE is reported and status 'open' is detected.

    The submitted application is a single-thread one and I used ::PeekMessage(...) at the end of the outer loop by which a range of ports can be scanned to handle windows messages in order to stop scanning process, moving windows around the screen and so on. Here, This is the code segment:

    for (m_nCounter = minPort; m_nCounter <= maxPort; m_nCounter++)
    {
    	.
    	.
    	.
    	while(nAttempt <= m_nMaxAttempts && !bIsOpen)
    	{
    		.
    		.
    	}
    	.
    	.
    	.
    	MSG message;
    	if (::PeekMessage(&message,NULL,0,0,PM_REMOVE))
    	{
    		::TranslateMessage(&message);
    		::DispatchMessage(&message);
    	}
    	.
       }
    

    To make sure that CPortScanView::OnButtonScan() is not reentered, I disabled the corresponding button CPortScanView::m_cBtnScan by invoking m_cBtnScan.EnableWindow(FALSE) member function. Also, to stop the above loop and make it exit while clicking on button CPortScanView::m_cBtnStop, I got CPortScanView::m_nCounter value to one unit more than CPortScanView::m_maxPort in message handler CPortScanView::OnButtonStop().

    Since the results of port scanning are saved in a CPtrList object member variable called CPortScanView::*m_pStatusList as a linked list, the contents of each node is not only visible in object CListCtrl::m_cResult but also accessible to save in a text file.

    The submitted code can also support UNICODE by adjusting the options and settings as described in MSDN.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Alex Douma


    Member
    - B.S. degree in Computer Engineering.
    - 10+ years experience in Turbo C, Borland C , Visual C++ and Managed C++.
    - Obssessed in OOP style design and programming.
    - Desigining and developing Network security tools.
    - Desigining and developing a client/server appliation for sharing files among users in a way other than FTP protocol.
    - Desigining and developing ISP Subscribers account management by binding to Cisco tools (NtTac+), Reporting the results on the web by ISAPI method.
    - Designing and writing code to build a search engine (Web crawler) by SQL Server 7.0 and VC++.

    - On-board programming of non-boundary scan memory devices like flash memories by boundary scan (IEEE 1149.1) protocol in C# and J#.

    - Designing and implementing GSM gateway applications and bulk messaging.

    The summary of my skills:
    C#, J#, Managed C++ code, VC++, MFC, Turbo Pascal, PL/I, SQL Server, MS Access, Windows NT administration, Web site developing, Macromedia tools, Webmastering, Cisco Routers.

    Occupation: Web Developer
    Location: Canada Canada

    Other popular Internet / Network articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
    QuestionHOW to modify it to scan ports on a IPv6 machine ? PinmemberAther Zaidi5:48 9 Apr '08  
    GeneralIs there a way to detect Network Printers' IP addresses? PinmemberJan Palmer21:43 25 Dec '06  
    GeneralWhy it can't work well on windowXP ? Pinmemberginkgo198021:49 12 Jun '05  
    GeneralThanks for the Code PinmemberIainws3:31 15 Jan '05  
    GeneralWaiting for long time! PinmemberR.selvam12:25 28 Nov '03  
    GeneralRe: Waiting for long time! PinsussAnonymous13:51 28 Nov '03  
    GeneralRe: Waiting for long time! PinmemberR.selvam14:46 28 Nov '03  
    GeneralIf you whant to see really good scanners Pinmemberwuxus9:32 9 Nov '03  
    GeneralRe: If you whant to see really good scanners PinmemberMichael Hendrickx9:19 26 Nov '04  
    Generalworst scaner ever. Pinmemberwukas8:59 17 Jul '03  
    GeneralRe: worst scaner ever. PinmemberArash Sabet15:20 17 Jul '03  
    GeneralRe: worst scaner ever. PinmemberXakep14:45 6 Aug '03  
    GeneralRe: worst scaner ever. PinmemberBearRiver5:39 18 Nov '04  
    GeneralRe: worst scaner ever. Pinmemberiainws3:10 15 Jan '05  
    GeneralRe: worst scaner ever. PinmemberTuPacMansur20:25 5 Sep '05  
    GeneralHow it works PinmemberSelevercin17:20 3 Apr '03  
    GeneralGood article, but a suggestion.. PinmemberSoliant9:18 29 Nov '02  
    GeneralRe: Good article, but a suggestion.. PinmemberArash Afifi14:13 29 Nov '02  
    GeneralRe: Good article, but a suggestion.. Pinmemberxxhimanshu18:44 16 Jan '03  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 12 Mar 2001
    Editor: Chris Maunder
    Copyright 2001 by Alex Douma
    Everything else Copyright © CodeProject, 1999-2009
    Web18 | Advertise on the Code Project