基于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);
}


 

转载于:https://www.cnblogs.com/new0801/archive/2012/01/22/6177791.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值