Using APIs to write firewalls

Source: Internet
Author: User
Tags include remove filter

/*
Use the API provided by the operating system to write firewalls.
The program involves API instructions please visit Microsoft's MSDN Library
Code is compiled in C + + Builder 5 through
If you want to communicate with me, please email:zzwinner@163.com.
*/
#pragma hdrstop
#include "Windows.h"
#include "Fltdefs.h"
Need to load "iphlpapi.lib"
//---------------------------------------------------------------------------
#pragma argsused
int main (int argc, char* argv[])
{
A Create network packet filter interface
Interface_handle Hinterface;
Pfcreateinterface (0,
Pf_action_drop,//pf_action_forward,
Pf_action_drop,//pf_action_forward,
FALSE,
TRUE,
&hinterface);
Bindings require IP addresses for network packet filtering
BYTE localip[] = {192,168,0,2};
Pfbindinterfacetoipaddress (Hinterface, Pf_ipv4, Localip);
Now let's start filtering the interfaces of the HTTP protocol
Filter_handle Fhandle;
Rule structure for filling filter packs
Pf_filter_descriptor Infilter;
Infilter.dwfilterflags = Fd_flags_nosyn; Always add this value
Infilter.dwrule = 0; Always add this value
Infilter.pfattype = Pf_ipv4; Use ipV4 Address
INFILTER.SRCADDR = Localip; Set Local IP Address
Infilter.srcmask = "\xff\xff\xff\xff"; Set Local subnet mask
Infilter.wsrcport = Filter_tcpudp_port_any; Any source port
Infilter.wsrcporthighrange = Filter_tcpudp_port_any;
infilter.dstaddr = 0; Any destination address
Infilter.dstmask = 0;
Infilter.wdstport = 80; Destination port (HTTP service)
Infilter.wdstporthighrange = 80;
Infilter.dwprotocol = filter_proto_tcp; Filtered Protocols
Add a filter interface
Pfaddfilterstointerface (Hinterface, 1, &infilter, 0, NULL, &fhandle);
Set a debug breakpoint here and see if your IE doesn't have access to the Web page. :)
Remove Filter Interface
Pfremovefilterhandles (Hinterface, 1, &fhandle);
Pfunbindinterface (Hinterface);
Pfdeleteinterface (Hinterface);
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.