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.
- First the host A sends the SYN packet (TCP packet with SYN flag set) to host B.
- If the port is open then host B responds by sending SYN+ACK packet.
else it sends the RST+ACK packet to host B.
- 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.
- First the host A sends the SYN packet (TCP packet with SYN flag set) to host B.
- 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