基于visual c++之windows核心编程代码分析(33)实现防火墙模型

防火墙(英文:firewall)是一项协助确保信息安全的设备,会依照特定的规则,允许或是限制传输的数据通过。防火墙可以是一台专属的硬件也可以是架设在一般硬件上的一套软件。
所谓防火墙指的是一个由软件和硬件设备组合而成、在内部网和外部网之间、专用网与公共网之间的界面上构造的保护屏障.是一种获取安全性方法的形象说法,它是一种计算机硬件和软件的结合,使Interne防火墙
t与Intranet之间建立起一个安全网关(Security Gateway),从而保护内部网免受非法用户的侵入,防火墙主要由服务访问规则、验证工具、包过滤和应用网关4个部分组成,   防火墙就是一个位于计算机和它所连接的网络之间的软件或硬件。该计算机流入流出的所有网络通信和数据包均要经过此防火墙。  

 在网络中,所谓“防火墙”,是指一种将内部网和公众访问网(如Internet)分开的方法,它实际上是一种隔离技术。防火墙是在两个网络通讯时执行的一种访问控制尺度,它能允许你“同意”的人和数据进入你的网络,同时将你“不同意”的人和数据拒之门外,最大限度地阻止网络中的黑客来访问你的网络。换句话说,如果不通过防火墙,公司内部的人就无法访问Internet,Internet上的人也无法和公司内部的人进行通信。

下列代码为防火墙的简单规则实现,过滤相关信息

void CxpktfilterDlg::OnBnClickedOk()
{
	PfCreateInterface(0, 
                     PF_ACTION_DROP,//PF_ACTION_FORWARD,
                     PF_ACTION_DROP,//PF_ACTION_FORWARD, 
                     FALSE, 
                     TRUE, 
                     &hInterface);

    // 绑定需要网络包过滤的IP地址
	
	BYTE localIp[]={127,0,0,1};
	
    PfBindInterfaceToIPAddress(hInterface, PF_IPV4, localIp);
    // 现在我们开始过滤HTTP协议的的接口
    
    // 填充过滤包的规则结构
    PF_FILTER_DESCRIPTOR inFilter;
    inFilter.dwFilterFlags = FD_FLAGS_NOSYN; //一直添这个值
    inFilter.dwRule = 0; //一直添这个值
    inFilter.pfatType = PF_IPV4; //用 ipV4 地址
    inFilter.SrcAddr = localIp; //设置本地IP地址
  
    inFilter.wSrcPort = FILTER_TCPUDP_PORT_ANY; //任意来源端口
    inFilter.wSrcPortHighRange = FILTER_TCPUDP_PORT_ANY;
    inFilter.DstAddr = 0; //任意目标地址
    inFilter.DstMask = 0;
    inFilter.wDstPort = FILTER_TCPUDP_PORT_ANY; //任意目标端口
    inFilter.wDstPortHighRange =FILTER_TCPUDP_PORT_ANY;
    inFilter.dwProtocol = FILTER_PROTO_TCP; // 过滤的协议,可选FILTER_PROTO_ICMP
    // 加入一个过滤接口
    PfAddFiltersToInterface(hInterface, 1, &inFilter, 0, NULL, &fHandle);
}

void CxpktfilterDlg::OnBnClickedButton1()
{
PfRemoveFilterHandles(hInterface, 1, &fHandle);
PfUnBindInterface(hInterface);
PfDeleteInterface(hInterface);
}


 

Xfilter代码完整的文件列表 ------------------------------------------------------ .\Common .\Lib .\Property .\Release .\TcpIpDog .\Xfilter.dsw .\readme.txt .\filelist.txt .\Common\XLogFile.h .\Common\XLogFile.cpp .\Common\XInstall.cpp .\Common\XFile.h .\Common\XInstall.h .\Common\XFile.cpp .\Common\Debug.h .\Common\XFileRes.h .\Lib\htmlhelp.h .\Lib\htmlhelp.lib .\Property\Property.opt .\Property\Property.aps .\Property\Property.dsp .\Property\Property.rc .\Property\Property.clw .\Property\ReadMe.txt .\Property\resource.h .\Property\NetIPAria.h .\Property\GuiRes.h .\Property\AclSet.cpp .\Property\SetTime.h .\Property\Acl.cpp .\Property\SetNet.h .\Property\MainSheet.h .\Property\AclSet.h .\Property\Register.h .\Property\SystemSet.cpp .\Property\Splash.h .\Property\Property.dsw .\Property\Acl.h .\Property\Property.ncb .\Property\Splash.cpp .\Property\Property.h .\Property\LogQuery.h .\Property\SetTime.cpp .\Property\MainSheet.cpp .\Property\SetNet.cpp .\Property\NetIPAria.cpp .\Property\About.cpp .\Property\StdAfx.h .\Property\StdAfx.cpp .\Property\SystemSet.h .\Property\Register.cpp .\Property\About.h .\Property\LogQuery.cpp .\Property\Property.cpp .\Property\NetTimeSheet.h .\Property\PacketMonitor.h .\Property\Property.plg .\Property\NetTimeSheet.cpp .\Property\PacketMonitor.cpp .\Property\Internet .\Property\MainFrame .\Property\SystemTray .\Property\res .\Property\HyperLink .\Property\Internet\Internet.cpp .\Property\Internet\Internet.h .\Property\MainFrame\MainFrame.cpp .\Property\MainFrame\mainframe.h .\Property\SystemTray\SystemTray.cpp .\Property\SystemTray\SystemTray.h .\Property\res\Property.rc2 .\Property\res\NULL.ico .\Property\res\Property.ico .\Property\res\about.bmp .\Property\res\Alert.ico .\Property\res\DenyEx1.ico .\Property\res\PassEx1.ico .\Property\res\QueryEx1.ico .\Property\res\splash.bmp .\Property\res\MEMO.ICO .\Property\res\ALERTSET.ICO .\Property\res\APPSET.ICO .\Property\res\BASESET.ICO .\Property\res\COMMONSET.ICO .\Property\res\Monitor.ico .\Property\res\NETSET.ICO .\Property\res\SUPERSET.ICO .\Property\res\TIMESET.ICO .\Property\res\Xfilter.ico .\Property\res\IPSET.ICO .\Property\res\Email.ico .\Property\res\QueryResult.ico .\Property\res\QuerySet.ICO .\Property\res\UserInfo.ico .\Property\res\ACLSET.ICO .\Property\res\Message.ico .\Property\HyperLink\HyperLink.cpp .\Property\HyperLink\HyperLink.h .\Release\xacl.cfg .\Release\Xfilter.chm .\Release\Xfilter.exe .\Release\Xfilter.dll .\Release\xlog.dat .\TcpIpDog\StdAfx.cpp .\TcpIpDog\TcpIpDog.dsp .\TcpIpDog\LspServ.def .\TcpIpDog\ReadMe.txt .\TcpIpDog\CheckAcl.cpp .\TcpIpDog\TcpIpdog.cpp .\TcpIpDog\Codes.h .\TcpIpDog\TcpIpDog.h .\TcpIpDog\ProtocolInfo.h .\TcpIpDog\CheckAcl.h .\TcpIpDog\StdAfx.h .\TcpIpDog\ProtocolInfo.cpp .\TcpIpDog\TcpIpDog.plg
// fire.cpp : Defines the class behaviors for the application. // #include "StarWarsCtrl.h" // Added by ClassView #include "stdafx.h" #include "fire.h" #include "MainFrm.h" #include "fireDoc.h" #include "fireView.h" #include <afxsock.h> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CFireApp BEGIN_MESSAGE_MAP(CFireApp, CWinApp) //{{AFX_MSG_MAP(CFireApp) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP // Standard file based document commands ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CFireApp construction CFireApp::CFireApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CFireApp object CFireApp theApp; ///////////////////////////////////////////////////////////////////////////// // CFireApp initialization BOOL CFireApp::InitInstance() { // CG: The following block was added by the Windows Sockets component. { if (!AfxSocketInit()) { AfxMessageBox(CG_IDS_SOCKETS_INIT_FAILED); return FALSE; } } AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif // Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CFireDoc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CFireView)); AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; // The one and only window has been initialized, so show and update it. HICON hIcon = ::LoadIcon (AfxGetResourceHandle (), MAKEINTRESOURCE(IDI_MAINFRAME)); m_pMainWnd->SetIcon(hIcon,FALSE); m_pMainWnd->SetWindowText("NetDefender"); m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; CStarWarsCtrl m_StarWarsCtrl; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) // No message handlers //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) // DDX_Control(pDX, IDC_STARWARS, m_StarWarsCtrl); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() // App command to run the dialog void CFireApp::OnAppAbout() { CAboutDlg aboutDlg; aboutDlg.DoModal(); } ///////////////////////////////////////////////////////////////////////////// // CFireApp message handlers
一款改自IPHook的IP数据包监控过滤程序 By ccc 2009-6-13 完成(ch2zh1@tom.com) IP包过滤程序采用IPHook驱动器程序,在内核层用钩子回调函数(IpHookFilter)勾住系统自带的IP过滤驱动器(System32\Drivers\IpFltDrv.sys),从而返回过滤驱动器传输的所有IP包数据。可以通过回调函数的返回值(PF_DROP、PF_FORWARD、PF_PASS),指示滤波驱动器对包的处理,是抛弃还是通过。 应用启动驱动器过程可以使用驱动器的安装,或使用系统的服务控制器(ServiceControl)直接启动和停止驱动程序。 服务控制器利用OpenSCManager等服务控制函数控制驱动程序的加载与卸载。这就使得应用程序可以在运行时自动加载和卸载需要的驱动程序。而不必设置驱动程序的安装过程。 初始的IPHook.sys只提供了IP包的监视过程即,仅仅返回IP头信息。在应用中解释并显示这个头信息。经过写改的IPHook增加了对IP包的过滤处理,有几个设置命令: START_IP_HOOK :启动钩子过滤命令,建立连接IP滤波器的回调函数(原始命令) STOP_IP_HOOK :停止钩子过滤命令,撤销IP滤波器回调函数(原始命令) ADD_IP_HOOKADDR :添加过滤IP地址,回调函数判断IP包,以决定是放弃还是通过(新命令) DEL_IP_HOOKADDR :删除过滤IP地址(新命令) ADD_IP_HOOKPORT :添加过滤端口号(新命令) DEL_IP_HOOKPORT :删除过滤端口号(新命令) ADD_IP_OVER_HOOK :添加置换的IP地址,用于对指定目的的IP包置换源地址和端口号(New) DEL_IP_OVER_HOOK :取消IP地址置换。(New) 应用程序可一次设置和删除对个滤波地址和端口号。对于重复设置,IPHook可以正确识别并加以剔除。这是一个演示程序主要想说明windows2000驱动程序编程过程,及应用自动加载和卸载驱动的方式。 驱动程序的源码在DrvChecked.rar中,要想正确编译,必须安装windows2000 DDK,并在VS2003下设置包含目录指向DDK的目录,库目录也要指向DDK的库目录: C:\NTDDK\inc C:\NTDDK\inc\ddk C:\NTDDK\libchk\i386 并且在排列上一定要排在头两个位置上。 这个驱动程序在VC++下是可以正确编译通过的。驱动程序名:DrvChecked.sys。使用时应该改为:iphook.sys。delphi下的应用程序为:IPPacketMonitor.exe,它使用IPHook.sys。 任何人可以自己编译和修改这个驱动程序,使之能够按照一定的方式运行,这里给出的只是本人的一个想法,希望能给各位带来一点帮助。任何应用程序都可以想打开文件一样使用这个驱动程序,关于使用方法请参考Release Notes.htm提供的线索。 有什么问题和建议请电邮:ch2zh1@tom.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值