Click here to Skip to main content
Click here to Skip to main content

PortScanner : Efficient TCP port scanner based on popular TCP Half Open scanning method

, 17 Jun 2004
Rate this:
Please Sign up or sign in to vote.
A TCP port scanner

Introduction

There are various port scanners which uses simple method of scanning. These applications work at application level and are quite slow. This scanner is faster than normal scanner. It is based on the TCP Half Open Scanning or TCP SYN scanning technique. This method is less detectable than the simple port scanner.

What is Half Open Scanning ?

When any two hosts wants to communicate together connection must be established between them. In case of TCP , three way handshake takes place before any communication begins. This is called Full connection and the process is described below.

  1. First the host A sends the SYN packet (TCP packet with SYN flag set) to host B.
  2. If the port is open then host B responds by sending SYN+ACK packet. else it sends the RST+ACK packet to host B.
  3. Now host A sends the ACK packet to host B. (if SYN+ACK packet is received).

Once the connection is established- both machines can transmit data packet until one of them ends the connection by sending FIN packet. Some of the simple port scanners use this technique. It can be implemented by creating socket and calling Connect method on each port.This is simple to implement but quite slow and moreover it can be easily detected.

Half scanning is more fast and efficient than full scanning technique. Half open connection is explained below.

  1. First the host A sends the SYN packet (TCP packet with SYN flag set) to host B.
  2. If the port is open then host B responds by sending SYN+ACK packet. else it sends the RST+ACK packet to host B.

Since the host A does not send any additional ACK packet , it is called half open connection. Now the host can easily find out if the target port is open or closed. It it receives TCP packet with SYN+ACK flag set, then it means that target port is open. If it receives RST+ACK packet ,it implies that target port is closed.

In this method full handshake does not take place , hence it is quite faster than full scanning method. Since the implementation has to be done at the protocol level , knowledge of TCP/IP protocol suite is essential.

Implementation

Core part of the implementation is sending the TCP packet and ARP packet. This involves building the raw packet by filling all headers. For this we must know MAC address of the source and destination machine. MAC address also called Ethernet address ,is the address associated with Ethernet adapter.

Find source MAC address

There are various methods for obtaining the source MAC address. This method is simple.
IP_ADAPTER_INFO adapter[5]; 
DWORD buflen=sizeof(adapter); 
DWORD status=GetAdaptersInfo(adapter,&buflen); 

Now the adapter structure contain the source MAC address.

Find destination MAC address

This is done by sending ARP packet. ARP packet is used to determine the host's MAC address when its IP address is known. First ARP request packet is sent by specifying the source MAC address, source IP address and destination IP Address. The ARP reply packet contains the destination MAC address. This method also prevents the target host from sending arp packet to source host when the source host sends the first SYN packet during scanning process. From the ARP request packet that we have sent , target host will come to know about the MAC address of the source host.

Scanning process

Scanning process involves building TCP packet.For this one has to prepare the Ethernet Header, IP header and TCP header. Header file packet.h contains the format details for each of these headers. You can refer RFC for details regarding these formats.

Each time during scanning TCP SYN packet is sent with different port numbers. Then the corresponding reply packet is checked for the flag RST+ACK or SYN+ACK. Based upon this flag target port status is determined.

Requirement

You need Winpcap ( Windows version of Libpcap) to run this application. It can be downloaded from this location. It contains the setup file along with good documentation that explains capturing and sending packet in detail. I advice to you to go through the WinPcap documentation before going through the source code.

Running the application

First make sure that you have installed WinPcap , then run the application. Port Scanner dialog box get displayed. Select the capture device, specify the target host and range of port number to be scanned. On starting scan port numbers and their status will be displayed.

Acknowledgement

I am thankful to ( Hacker ) Hr.Ankit Fadia for his great book "Unofficial guide to Ethical Hacking". Most of the technical details that I have mentioned here ,I learnt from this book. If you want more details or you have any doubts , please feel free to drop a mail at nsry2002@yahoo.co.in

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

Nagareshwar
Web Developer
India India
Nagareshwar is a security enthusiastic person involved in reverse engineering, vulnerability research, coding security tools etc. He spend most of the time in uncovering the secrets of computer world.
 
He holds 'Bachelor of Engineering' degree from National Institute of Technology of Karnataka, India. He had professional experience of 2.5 years in Novell. At Novell he was working on various security products including 'Novell Secure Login' and CASA.
 
For more details visit his website http://securityxploded.com

Comments and Discussions

 
Generalcomplie error Pinmemberylfeng8616-Feb-09 2:07 
GeneralRe: complie error Pinmembermchanglee27-Dec-13 13:49 
Generalcompiler error !!! Pinmemberjiaruey20-Sep-08 22:22 
Generalcompiler errors Pinmemberjm.alkema17-Feb-07 5:29 
GeneralRe: compiler errors PinmemberMember 409551528-Jun-08 6:22 
GeneralRe: compiler errors PinmemberMember 403423610-Jul-08 1:10 
GeneralRe: compiler errors Pinmemberspicture16-Feb-10 13:14 
QuestionIs there a way to detect Network Printers' IP addresse PinmemberJan Palmer25-Dec-06 20:47 
Generaluuid.lib(objidl_i.obj) : fatal error LNK1103 Pinmemberzcxzcx4-Sep-06 19:45 
GeneralRe: uuid.lib(objidl_i.obj) : fatal error LNK1103 Pinmemberkihong7-Dec-06 17:58 
GeneralRe: uuid.lib(objidl_i.obj) : fatal error LNK1103 Pinmembernebulon8-Oct-07 22:15 
GeneralRe: uuid.lib(objidl_i.obj) : fatal error LNK1103 PinmemberDuoc T12-Mar-08 2:21 
GeneralRe: uuid.lib(objidl_i.obj) : fatal error LNK1103 PinmemberJun Du12-Mar-08 11:19 
GeneralNo network adapters are present... error Pinmembervikramchiruvolu9-Aug-05 5:57 
Generalports PinsussAnonymous20-Mar-05 0:49 
GeneralRe: ports PinmemberCeri6-Apr-05 5:13 
To see what local ports and established connections you active on your machine I would recommend you try TCPView by sysinternals.
 
To see what ports a remote machine has opened then this article basicly explains how it's done and there is a myriad of free port scanner on the web. Just google it and im sure you will find one
GeneralNot bad but.... PinmemberCeri25-Jan-05 4:09 
QuestionRe: Not bad but.... PinmemberPrashanth Gedde N27-Feb-06 7:41 
AnswerRe: Not bad but.... PinmemberCeri27-Feb-06 22:07 
GeneralRe: Not bad but.... PinmemberPrashanth Gedde N27-Feb-06 22:43 
Generalwondering why the rating is low Pinmembersudhir mangla21-Dec-04 1:38 
GeneralRe: wondering why the rating is low Pinmemberspicture16-Feb-10 13:17 
GeneralAsk for source code Pinmemberthong_lam18-Nov-04 16:36 
GeneralRe: Ask for source code PinmemberNagareshwar18-Nov-04 18:19 
GeneralRe: Ask for source code Pinmemberthong_lam22-Nov-04 0:19 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 18 Jun 2004
Article Copyright 2004 by Nagareshwar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid