A Little Sniffer that Uses WSA Sockets (Windows Sockets)


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, a sniffer pays 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; there is a complete list of all protocols that IP encapsulates.

You can download the demo project and source code at the end of this article.

Starting to Sniff

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

lsniff [TCP|ICMP]
  • TCP grabs TCP packets only (RFC 793).
  • ICMP grabs ICMP packets only (RFC 792).
  • lsniff, with no arguments, will grab TCP and ICMP packets.

Figure 1: Example of lsniff grabbing only ICMP packets

You also can redirect the output to a file: lsniff icmp >output.txt.

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, you will see the four necessary steps to start working in the promiscuous mode state:

  1. Initialize Windows Sockets (Line 107).
  2. Get a RAW socket (Line 111). RAW is a special type of socket that gives you access to packet headers, not only the data.
  3. sniff_socket = socket( AF_INET, SOCK_RAW, IPPROTO_IP );
  4. Bind the socket to the interface you want to sniff (Lines 119-127).
  5. Set the socket to promiscuous mode (Line 135).
  6. if ( WSAIoctl( sniff_socket,
                   NULL ) == SOCKET_ERROR )
       printf( "Error: WSAIoctl  = %ld\n", WSAGetLastError() );

After all four 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 )

lsniff only parses IPv4 packets, not IPv6. Furthermore, the IP header is parsed to know what protocol is encapsulated. Notice that lsniff does 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 lsniff by 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.



  • good place for practicing

    Posted by billstoneman on 02/22/2009 09:51pm

    wonderful, good place for participating in it to add some other interesting protocols.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds