Introduction

Many people have used a sniffer at some time. What is a sniffer? A sniffer is an application that catches all network traffic from or to computers attached to a network.Basically, what a sniffer really does is pay attention to all traffic by putting a network interface in the promiscuous mode state. Promiscuous mode puts a selected network interface to listening to all packets passing through it.

This article demonstrates how an application can configure a socket connection to pay attention to all network packets, instead of only those addressed to it. It shows how to grab protocols encapsulated by IP (Internet Protocol: network layer protocol), specifically, TCP and ICMP. IP encapsulates up to 100 different protocols. I advise you to take a look at the RFC 1700, as there is a complete list of all protocols that IP encapsulates.

Let Us Start to Sniff

The demo project contains a single executable named lsniff.exe,which is a console application. The syntax is:

Understanding the Source Code

lsniff is a C/C++ application coded using Visual Studio 2005. It will compile in older compilers, too. It is really simple. In fact, the difficult part is to analyze the packets because you must know the packet structure that depends upon the protocol. Windows Socket API (WSA) offers the tools (functions) to create a simple sniffer.

It is worth mentioning that lsniff will run only if the you are logged on with administrative privileges. By reading lsniff_main.cpp, we will see the 4 necessary steps to start working in the promiscuous mode state:

Initialize Windows Sockets (line 107).

Get aRAWsocket (line 111). RAWis a special type of socket that gives you access to packet headers, not only the data.

After all those 4 steps have been successfully performed, it is time to start reading the packets. Notice that you should provide a buffer big enough (LS_MAX_PACKET_SIZE) to the recv function. The first check made after a packet has been read is the IP version (line 161):

if ( LS_HI_PART(ip_header->ver_ihl) != 4 )
continue;

lsniff only parses IPv4 packets, not IPv6. Furthermore, the IP header is parsed to know what protocol is encapsulated. Notice that lsniffdoes not care about packet data, only packet headers. The rest of the code depends upon your knowledge about the protocol you want to parse. Of course, you can extend lsniffby adding more protocol-parsing routines. It is good practice to use a professional sniffer (like Ethereal) to help you to parse packets:

Figure 2: parsing packets

Enjoy. I hope this helps.

History

5 November, 2007: First version

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.