Network Sniffer and Connection Analyzer






4.42/5 (58 votes)
Sep 11, 2004
7 min read

418985

23142
Network sniffer and connection analyzer.
Introduction
Project is an implementation of a Packet Sniffer that captures packet in a LAN environment using Microsoft .NET environment and written in C#.
Our goal was to build a network utility tool that can be an assistant to programmers, network managers, and private users. Our Sniffer can be useful for monitoring traffic, debugging, fault analysis, network protocol analysis, network intrusion detection, traffic measuring etc.
In order to reach our goals, project supplies some useful features that can be easily used. Features can be summarized like this:
- Organize captured packets in a connection-oriented view.
- Smart real-time analyzer enables on-the-fly content viewing while capturing and analyzing.
- Parse and decode an variety of network protocol.
- Protocol definition tool to extend protocols that are decodable.
- Powerful filter provides a flexible mechanism to capture specific packets.
- Port Scanner utility.
- Finds process that uses each connection.
- Syntax highlighting for application data.
We did not use any capture library; instead we wrote our own capture library and build a GUI that serve user an easy interface. To build capture library we used .NET network classes that provide interface to native Winsock API and asynchronous sockets that brings thread based solution to socket programming.
Project Files (Sniffer)
Consts.cs |
Class “ |
DataManager.cs |
Class “ |
DnsTable.cs |
We need to resolve domain names of Ips and this process requires too much time and cause a performance bottlenecks. We overwhelm this problem with building this class that stores resolved domain names in a hash table and serve a function |
FilterManager.cs |
Supply filtering functionality. |
HeaderParser.cs |
This supply a helper class that extract field values from Ip Datagram. |
IcmpPacket.cs |
Define a class that used to set fields of an ICMP packet or extract these fields’ values from IP datagram. Icmp packets do not have a fixed format. First four bytes are same for all types of Icmp packets, so decoding these packets has some difficulties. We overwhelmed this problem by after decoding type and code values, extract other fields according to these fields.
There is no need to mention to other functions. |
IPv4Datagram.cs |
|
Protocols.cs |
This class contains functions supplied operations to read parse XML data. |
ProtocolTemplate.cs |
This class extracts fields of a packet according to protocol definitions that are loaded from XML files. First, we obtain what type of template packet will be build by looking up to Ip datagrams next header section and loads its definition from |
SnifferException.cs |
Contain exception class that will be thrown when a problem arises during sniffing. |
SnifferSocket.cs |
This file contains core class that builds our Packet Sniffer. It brings an event driven solution to sniffing. We explain it important functions below.
|
SocketPair.cs |
Class that contains a socket handle and a buffer that belongs to this socket. |
TcpPacket.cs |
Class that decodes fields of an incoming TCP Packet. |
trio.cs |
This is a helper class used to group Source IP, Destination IP, Source Port, Destination Port, Protocol Number and its string counterpart into a class. We use it to pass all these values in a single structure. |
UdpPacket.cs |
Class that decodes fields of an incoming UDP Packet. |
Project Files (SnifferUI)
AddIP.cs |
Class that holds Ip addresses in a container. We use update Ip address list that is being listening. |
AddProtocols.cs |
This class serve as an interface to add new protocols to Sniffer’s protocol list. |
AddressInput.cs |
This class is used to get an IP from user. |
CheckerForm.cs |
This form is a common interface used by ping, trace route and connection checker. Result of the operation reported is to the user in this form. |
CheckTool.cs |
This is a static class and has functions that are
|
FilterCreater.cs |
This class creates filter and adds it to Sniffer’s |
Filtering.cs |
This class contains functions that used to manage Sniffer’s |
Icmp.cs |
This is a limited version of ICMP that is used by CheckTool.cs. |
icmpView.cs |
This is a class that extends the |
NetStat.cs |
This class interfaces the
|
OtherProtocols.cs |
This is a user control class that uses |
Setting.cs |
This is the static class that holds the settings of tool that are Ping, Trace Route and Connection Checker. These settings related to the protocol type of the outgoing packets that send by mentioned tools. |
Settings.cs |
This class provides the interface to let user to change settings. |
SnifferUI.cs |
This class is the core of the interface. |
tablesForProc.cs |
This class provides two functions that are |
tcpView.cs |
This is a class that extends the |
treeViewFuncs.cs |
This class enables viewing decoded packets in tree view. Another feature that provided is, highlighting text in Hexadecimal and Binary Views when user selects a field in tree view. |
udpView.cs |
This is a class that extends the |
Project Files (PortScanner)
Configuration.cs |
Parses and extracts configuration data from XML file. This configuration includes the following. Which servers with which port will be scanned, mail server that will be used, sender of the mails, receiver that will be notified, loop count for scan process and time interval between scans. |
CreateFileForm.cs |
Creates an interface that ease making XML files configurations. |
Logging.cs |
This class enables creation of event log and writing of log. |
LogItemView.cs |
This class enables view of |
PortScanner.cs |
This is the core class that scans ports, enables user to control port scanner. |
Setting.cs |
Saves and loads PortScanner’s settings from XML file. Setting has the file name whose default configuration to be loaded. |
SettingForm.cs |
This file enables setting configuration file. |
Smtp.cs |
SMTP class has functions that enable sending mail. |
Utility.cs |
Provides two functions that are |
Here is the our implementation of asynchronous functions.
public void Sniff(String ip)
{
IP=ip;
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Raw,ProtocolType.IP);
byte[] buffer = new byte[2048];
SocketPair socketpair = new SocketPair(socket,buffer);
socket.Blocking = true;
try
{
socket.Bind(new IPEndPoint(IPAddress.Parse(ip),0));
}
catch(SocketException e)
{
throw new SnifferException("Cannot assign requested address.The
requested address is not valid in its context.",e);
}
this.SetupSocket(socket);
if ( SocketMap_.Contains(ip) )
{
throw new SnifferException("Socket already bound on that IP");
}
else
{
SocketMap_.Add(ip,socketpair);
}
try
{ socket.BeginReceive(buffer,0,buffer.Length,SocketFlags.None,new
AsyncCallback(this.ReceivePacket),socketpair);
paused=false;
}
catch ( Exception e )
{
throw new SnifferException("Could not start the Receive",e);
}
}
// Callback function for the Asynchronous Receive on a Socket.
private void ReceivePacket(IAsyncResult ar)
{
bool fired=false;
int len = 0;
SocketPair p = ar.AsyncState as SocketPair;
Socket socket = p.IPSocket;
int type = 0;
try
{
len = socket.EndReceive(ar);
}
catch ( SocketException e)
{
fired = true;
FireSnifferError(new SnifferException("Error Receiving
Packet",e));
}
if (!fired)
{
type = HeaderParser.ToInt(p.Buffer,0,4);
try
{
switch(type)
{
case 4:
HandleIPv4Datagram(p.Buffer);
break;
}
}
catch ( Exception e )
{
FireSnifferError(new
SnifferException(e.Message.ToString(),e));
}
}
if (!this.paused)
{
socket.BeginReceive(p.Buffer,0,p.Buffer.Length,SocketFlags
.None,new AsyncCallback(this.ReceivePacket),p);
}
}