Click here to Skip to main content
15,392,679 members
Articles / Desktop Programming / MFC
Posted 12 Mar 2001


52 bookmarked

IP Hole Finder and Port Scanner

Rate me:
Please Sign up or sign in to vote.
3.21/5 (10 votes)
12 Mar 2001
  • 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.


    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;
    	if (!pSocket->Create())
    		delete pSocket;
    		pSocket = NULL;
    		return FALSE;
    	while (!pSocket->Connect(IP , nPort))
    		delete pSocket;
    		pSocket = NULL;
    		return FALSE;
    	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))

    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.


    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

    Arash Sabet
    Web Developer
    Canada Canada
    - 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.

    Comments and Discussions

    QuestionHOW to modify it to scan ports on a IPv6 machine ? Pin
    Ather Zaidi9-Apr-08 4:48
    MemberAther Zaidi9-Apr-08 4:48 
    QuestionIs there a way to detect Network Printers' IP addresses? Pin
    Jan Palmer25-Dec-06 20:43
    MemberJan Palmer25-Dec-06 20:43 
    QuestionWhy it can't work well on windowXP ? Pin
    ginkgo198012-Jun-05 20:49
    Memberginkgo198012-Jun-05 20:49 
    GeneralThanks for the Code Pin
    Iainws15-Jan-05 2:31
    MemberIainws15-Jan-05 2:31 
    GeneralWaiting for long time! Pin
    Selvam R28-Nov-03 11:25
    professionalSelvam R28-Nov-03 11:25 

    It take more time to scan port.

    GeneralRe: Waiting for long time! Pin
    Anonymous28-Nov-03 12:51
    MemberAnonymous28-Nov-03 12:51 
    GeneralRe: Waiting for long time! Pin
    Selvam R28-Nov-03 13:46
    professionalSelvam R28-Nov-03 13:46 
    GeneralIf you whant to see really good scanners Pin
    wuxus9-Nov-03 8:32
    Memberwuxus9-Nov-03 8:32 
    GeneralRe: If you whant to see really good scanners Pin
    Michael Hendrickx26-Nov-04 8:19
    MemberMichael Hendrickx26-Nov-04 8:19 
    Generalworst scaner ever. Pin
    Andrius Mudinas17-Jul-03 7:59
    MemberAndrius Mudinas17-Jul-03 7:59 
    GeneralRe: worst scaner ever. Pin
    Arash Sabet17-Jul-03 14:20
    MemberArash Sabet17-Jul-03 14:20 
    GeneralRe: worst scaner ever. Pin
    Xakep6-Aug-03 13:45
    MemberXakep6-Aug-03 13:45 
    GeneralRe: worst scaner ever. Pin
    BearRiver18-Nov-04 4:39
    MemberBearRiver18-Nov-04 4:39 
    GeneralRe: worst scaner ever. Pin
    Iainws15-Jan-05 2:10
    MemberIainws15-Jan-05 2:10 
    GeneralRe: worst scaner ever. Pin
    TuPacMansur5-Sep-05 19:25
    MemberTuPacMansur5-Sep-05 19:25 
    QuestionHow it works Pin
    Selevercin3-Apr-03 16:20
    MemberSelevercin3-Apr-03 16:20 
    GeneralGood article, but a suggestion.. Pin
    Soliant29-Nov-02 8:18
    MemberSoliant29-Nov-02 8:18 
    GeneralRe: Good article, but a suggestion.. Pin
    Arash Sabet29-Nov-02 13:13
    MemberArash Sabet29-Nov-02 13:13 
    GeneralRe: Good article, but a suggestion.. Pin
    xxhimanshu16-Jan-03 17:44
    Memberxxhimanshu16-Jan-03 17:44 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.