Click here to Skip to main content
15,881,938 members
Articles / Desktop Programming / MFC

Internet Traffic Firewall and Sniffer

Rate me:
Please Sign up or sign in to vote.
4.91/5 (26 votes)
23 Oct 2007GPL33 min read 100.8K   9.1K   133  
The article demonstrates internet packets interception with firewall capabilities based on IpFilterDriver driver and sending TCP/UDP/ICMP packets using raw sockets with IP spoofing support.
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently,
// but are changed infrequently

#pragma once

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN		// Exclude rarely-used stuff from Windows headers
#endif

// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER				// Allow use of features specific to Windows 95 and Windows NT 4 or later.
#define WINVER 0x0400		// Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#endif

#ifndef _WIN32_WINNT		// Allow use of features specific to Windows NT 4 or later.
#define _WIN32_WINNT 0x0400		// Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#endif

#ifndef _WIN32_WINDOWS		// Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif

#ifndef _WIN32_IE			// Allow use of features specific to IE 4.0 or later.
#define _WIN32_IE 0x0400	// Change this to the appropriate value to target IE 5.0 or later.
#endif

#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS	// some CString constructors will be explicit

// turns off MFC's hiding of some common and often safely ignored warning messages
#define _AFX_ALL_WARNINGS

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxcview.h>

#include <afxdtctl.h>		// MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>			// MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT


#include <Winsvc.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <afxdlgs.h>
#include <Mstcpip.h>
#include <Mmsystem.h>


#define WM_PACKET WM_APP + 1
#define WM_FILTERHOOK WM_APP + 2
#define COLUMNS 20


/*
    PVOID - Windows Data Types
      HANDLE = void *
	  HWND   = void *
*/

#include "Lib\Driver.h"

typedef struct _thread {
        void *hWnd;                      //cView handle to PostMessage(packet,size)
        void *event;                     //event to WaitForSingleEvent
        CDriver *drv;                    //user drv handle
        unsigned char stop;              //quit sign for hook thread
}threadParams;

typedef struct _ippacket {
        int length;                      //whole pack len
        int drop;                        //dropped by userdrv = 1, passed = 0
}ipPacket;

typedef struct _setings {
        char dropall;
        char dropallinbound;
        unsigned int myip;
        char dropicmp3;
        char dropicmp4;
        char dropicmp5;
        char dropicmp11;
        char dropicmp12;
        unsigned short udpmin;
        unsigned short udpmax;
        char droptcpsyn;
        char droptcprst;
}fireSetings;



#define FILE_DEVICE_USERDRV  0x8000
#define METHOD_BUFFERED 0x0000
#define FILE_ANY_ACCESS 0x0000
#define CTL_CODE( DeviceType, Function, Method, Access ) (((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) )

#define IOCTL_HOOKON CTL_CODE(FILE_DEVICE_USERDRV, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_HOOKOFF CTL_CODE(FILE_DEVICE_USERDRV, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GETPACKS CTL_CODE(FILE_DEVICE_USERDRV, 0x802, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_SETINGS CTL_CODE(FILE_DEVICE_USERDRV, 0x803, METHOD_BUFFERED, FILE_ANY_ACCESS)







/* IPv4 header */
typedef struct ip4 {
        u_char	ver_ihl;		// Version (4 bits) + Internet header length (4 bits)
        u_char	tos;			// Type of service
        u_short tlen;			// Total length
        u_short identification; // Identification
        u_short flags_fo;		// Flags (3 bits) + Fragment offset (13 bits)
        u_char	ttl;			// Time to live
        u_char	proto;			// Protocol
        u_short crc;			// Header checksum
        in_addr	saddr;		    // Source address
        in_addr	daddr;		    // Destination address
}ip4_header;

/* TCP header*/
typedef struct tcp {
        u_short sport;			// Source port
        u_short dport;			// Destination port
        u_int seqnum;           // Seq number
        u_int acknum;           // Ack number
        u_char dataoffs;        // Data offset   xxxx0000  num in 32bits
        u_char ctrlbits;        // Control bits  00xxxxxx
        u_short window;         // Window
        u_short crc;            // Header checksum
        u_short urgpnt;         // Urgent pointer
}tcp_header;

typedef struct tcpopt {
        u_char kind;
        u_char length;
        u_short data;
}tcp_options;

/* ICMP header*/
typedef struct icmp {
        u_char type;
        u_char code;
        u_short crc;
        u_short ident;
        u_short seqnum;
}icmp_header;

/* UDP header*/
typedef struct udp {
        u_short sport;			// Source port
        u_short dport;			// Destination port
        u_short len;			// Header + Datagram length
        u_short crc;			// Checksum
}udp_header;



typedef struct ippacket {
        WCHAR time[256];
        WCHAR date[256];
        WCHAR proto[256];
        WCHAR saddr[256];
        WCHAR daddr[256];
        WCHAR ident[256];
        WCHAR ttl[256];
        WCHAR foffs[256];
        WCHAR seqnum[256];
        WCHAR acknum[256];
        WCHAR ctrlbits[256];
        WCHAR window[256];
        WCHAR size[256];         // 204(138)  packet(packet data)

        WCHAR itype[256];
        WCHAR icode[256];
        WCHAR iident[256];
        WCHAR iseq[256];
        WCHAR iaddr[256];
        WCHAR iorigt[256];
        WCHAR irecvt[256];
        WCHAR itrant[256];
}ippacket;

static struct {
        WCHAR bits[2];
} BITS[] = {L"F", L"S", L"R", L"P", L"A", L"U", };



By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Engineer
Russian Federation Russian Federation
Highly skilled Engineer with 14 years of experience in academia, R&D and commercial product development supporting full software life-cycle from idea to implementation and further support. During my academic career I was able to succeed in MIT Computers in Cardiology 2006 international challenge, as a R&D and SW engineer gain CodeProject MVP, find algorithmic solutions to quickly resolve tough customer problems to pass product requirements in tight deadlines. My key areas of expertise involve Object-Oriented
Analysis and Design OOAD, OOP, machine learning, natural language processing, face recognition, computer vision and image processing, wavelet analysis, digital signal processing in cardiology.

Comments and Discussions