Click here to Skip to main content
15,888,351 members
Articles / Desktop Programming / MFC
Article

WinSniff :The packet capturing application for Windows

Rate me:
Please Sign up or sign in to vote.
3.36/5 (24 votes)
22 Jun 20043 min read 291.9K   11.6K   83   77
A packet capturing application for Windows.

Introduction

This article describes a sniffer for Windows. WinSniff is an application for capturing packets on the network. It displays all the packets that are transmitted on the local network and gives detailed information about each header in the packet. In order to keep it simple, I am not dealing with application level protocols. If you are interested, you can add features to support various application level protocols such as SMTP, FTP, NETBIOS etc.

Environment

Visual C++ 6.0, Win98/WinXP.

How it works?

When your machine is on the network, packets with different destinations arrive. By default (i.e., when the network adapter is in normal mode) these packets are rejected by the network adapter since they are intended to different hosts. But if you want, you can receive these packets by putting the network adapter in promiscuous mode. In this mode, it will accept all the packets irrespective of the destination address.

Hence you can analyze the packets transmitted on your network. This trick is used for network management to determine the network traffic... etc. However, there is one problem here...!!! You will receive the packets with different destinations if you are using HUB. Since, HUB uses broadcasting technique for transmitting packets to all the hosts attached to it. However, if you are using SWITCH (an intelligent device), then you won't receive any packet sent to other hosts on the network. Best place to install this application is  on the gateway where you can keep track of incoming and outgoing packets.

Implementation

Firstly, you have to get the device list and then open the device in promiscuous mode. While opening the device, you can also specify the size of the packet and time out value.

// Get all devices for capturing the packet 
pcap_findalldevs(&devlist,err); 

//Open device in promiscous mode 
hdev=pcap_open_live( devname[index], //name of the device 
65536, //size ->Capture whole packet 
1, //promiscous mode 
1000, //read timeout 
err 
);

Once you have opened the device, you will receive all packets. If you are interested in a particular packet, for example, only QUAKE packets (port 27960), ARP packets (ARP) etc., then you can specify the filter expression. For how to specify filter expression, you can refer WinPcap documentation.

//compile the filter 
pcap_compile(hdev,&fcode,filter,1,netmask); 
//now set the filter 
pcap_setfilter(hdev,&fcode);

Once you have opened the device and set the filter, now you are ready to receive the packets. Once the packet is received, header contains the length, time and other information about the packet. And pkt_data contains the exact contents of the packet starting from Ethernet header.

while(true) 
{ 
  pcap_next_ex(hdev,&header,&pkt_data); 
  // Do whatever you want.. 
}

In order to analyze the packet contents, you must be familiar with various header formats. Mainly, you must know the format of the following headers... ETHERNET, ARP, IP, TCP, UDP, ICMP and IGMP. I have included the file protocol.h which contains the format information about all these headers. If you want more details, you can refer RFCs for respective protocols.

Once you have done the job, it's time to safely close the device.

//close the device... 
pcap_close(hdev);

Requirement

You need WinPcap (Windows version of Libpcap: packet capturing library) 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 packets in detail. I advice you to go through the WinPcap documentation before going through the source code.

Running the application

When you run the application, the main window pops up. Click on the startcapture menu item to start the capture. It displays a dialog box, now select the device. Packets will be displayed in the main window. Click on the packet to see more details. You can save any packet by clicking SaveFrame menu item. Later, you can open this saved frame.

If you don't have a network adapter or you are not on the network, I have included some sample packets in SamplePackets folder in the source zip file. You can open these files and view their contents.

If you have any queries or suggestions, 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


Written By
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

 
GeneralRe: Upload / Download Pin
Matthew R. Miller4-Nov-04 3:43
Matthew R. Miller4-Nov-04 3:43 
Generalwinpcap Pin
gurneetmangat10-Sep-04 1:46
gurneetmangat10-Sep-04 1:46 
GeneralRe: winpcap Pin
Nagareshwar12-Sep-04 18:40
Nagareshwar12-Sep-04 18:40 
GeneralRe: winpcap Pin
gurneetmangat13-Sep-04 20:31
gurneetmangat13-Sep-04 20:31 
GeneralRe: winpcap Pin
Nagareshwar13-Sep-04 22:01
Nagareshwar13-Sep-04 22:01 
GeneralRe: winpcap Pin
Tadas Danielius30-Nov-06 5:31
Tadas Danielius30-Nov-06 5:31 
GeneralEthereal Pin
joegood23-Jun-04 7:40
joegood23-Jun-04 7:40 
GeneralRe: Ethereal Pin
Philippe Lhoste3-Jul-04 5:51
Philippe Lhoste3-Jul-04 5:51 
Yes, Ethereal is excellent, I used it a lot to debug and optimize IMAP4 excanges between server and (CGI) client.

The advantage of Ethereal over WinSniff is that it does support a lot of application level protocols (POP3, IMAP4, etc.), so it is easier to read results.

The drawback is that its interface is GTK+ based, so a bit heavy, not so nice (IMO) and lacking native Windows look and feel.
A WinSniff with high level protocol support would be great...

Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://phi.lho.free.fr

GeneralPacket .dll is missing in demo project Pin
Yogesh Kshatriya22-Jun-04 23:20
Yogesh Kshatriya22-Jun-04 23:20 
GeneralRe: Packet .dll is missing in demo project Pin
Aamir Butt23-Jun-04 0:40
Aamir Butt23-Jun-04 0:40 
GeneralRe: Packet .dll is missing in demo project Pin
Sam Hobbs19-Dec-05 14:58
Sam Hobbs19-Dec-05 14:58 
GeneralProblem with adapter Pin
leonardas22-Jun-04 20:02
leonardas22-Jun-04 20:02 
GeneralRe: Problem with adapter Pin
Nagareshwar23-Jun-04 10:11
Nagareshwar23-Jun-04 10:11 
GeneralRe: Problem with adapter Pin
ewasta19-Sep-05 7:00
ewasta19-Sep-05 7:00 
GeneralRe: Problem with adapter Pin
Nynaeve28-Sep-05 6:48
Nynaeve28-Sep-05 6:48 
GeneralRe: Problem with adapter Pin
Nynaeve28-Sep-05 7:38
Nynaeve28-Sep-05 7:38 
GeneralRe: Problem with adapter Pin
Sam Hobbs19-Dec-05 15:03
Sam Hobbs19-Dec-05 15:03 
GeneralRe: Problem with adapter Pin
waelahmed23-Jun-04 22:31
waelahmed23-Jun-04 22:31 
GeneralRe: Problem with adapter Pin
Nagareshwar24-Jun-04 9:49
Nagareshwar24-Jun-04 9:49 
General??? Pin
dungbkhn28-Oct-04 17:30
dungbkhn28-Oct-04 17:30 
GeneralOkey!!! Pin
dungbkhn1-Nov-04 19:03
dungbkhn1-Nov-04 19:03 
GeneralRe: Okey!!! Pin
Member 120551230-Nov-04 21:21
Member 120551230-Nov-04 21:21 
GeneralRe: Okey!!! Pin
cristitomi5-Mar-07 4:53
cristitomi5-Mar-07 4:53 
QuestionHow to revert a science Pin
Emilio Garavaglia20-Jun-04 21:07
Emilio Garavaglia20-Jun-04 21:07 
AnswerRe: How to revert a science Pin
Nagareshwar22-Jun-04 8:56
Nagareshwar22-Jun-04 8:56 

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.