|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionRecently, I have been searching on how I can make filter applications using Packet Filtering API included in Windows 2000 and above. First, I decided to make a C++ class in order to encapsulate all and make easy next applications. But I thought... why don't write a .NET C++ class? Because I haven't seen any other class to do it for this platform, I answered "yes". The problem is that I am a beginner with .NET. I know C++ and I have used C# a few times, but I haven't written mix managed/unmanaged C++ code ever. Anyway, I didn't change my answer, I decided to write this class and I'll write it :P (and in this way, I can learn more about .NET). For this reason, you may see bugs in my code or probably you might think you would write code better. Feel free to tell me all you think and we will learn together. You will learn something about packet filtering ( I hope it...) and I learn something about .NET mixing managed/unmanaged code (I hope it, too). Packet Filtering APIWith Windows 2000, Microsoft included one API in order to implement packet filtering functionality in our programs. This API is included in Windows XP and Windows 2003, too. Packet Filtering API allow us to associate filters to IP adapter interfaces. We can implement a functionality similar that included in TCP/IP filter options in TCP/IP properties of a network adapter. Now I will comment the basic functions for this API, used to write this class:
If you want more information about this functions parameters, you can find it in MSDN. One reason to write this article is the few documentation (I love documentation with samples!!!). Important: You can only use this functions if you have administrative privileges. Installing our IP FiltersWe know the functions we have to use. Now we have to know the process to install filters:
In C words: // Creating the interface and associating it with
// a local ip address INTERFACE_HANDLE hInterface;
PfCreateInterface(0,
PF_ACTION_FORWARD,
PF_ACTION_FORWARD,
FALSE,
TRUE,
&hInterface);
// look this byte order for ip address!!
BYTE localIp[] = {172,29,16,2};
PfBindInterfaceToIPAddress(hInterface, PF_IPV4, localIp);
// We go to add a filter. Forbid outgoing http traffic, for example.
FILTER_HANDLE fHandle;
// Fill the filter rule data
PF_FILTER_DESCRIPTOR inFilter;
inFilter.dwFilterFlags = FD_FLAGS_NOSYN; //always this value
inFilter.dwRule = 0; //always this value
inFilter.pfatType = PF_IPV4; //using ipV4 addresses
inFilter.SrcAddr = localIp; //set local ip
inFilter.SrcMask = "\xff\xff\xff\xff"; //mask for local ip
inFilter.wSrcPort = FILTER_TCPUDP_PORT_ANY; //any source port
inFilter.wSrcPortHighRange = FILTER_TCPUDP_PORT_ANY;
inFilter.DstAddr = 0; //any destination
inFilter.DstMask = 0;
inFilter.wDstPort = 80; //destination port 80(http service)
inFilter.wDstPortHighRange = 80;
inFilter.dwProtocol = FILTER_PROTO_TCP; // Tcp protocol
// Add the filter
PfAddFiltersToInterface(hInterface, 1, &inFilter, 0, NULL, &fHandle);
//...............
//...............
// Remove the filter
PfRemoveFilterHandles(hInterface, 1, &fHandle);
// Unbind and delete interface
PfUnBindInterface(hInterface);
PfDeleteInterface(hInterface);
The .NET ClassesMy packet filtering API for .NET implements two public classes:
Sample applicationTo test the class, I wrote a simple C# application that installs two rules: forbid all incoming ICMP traffic and forbid all outgoing HTTP traffic. You can see how I use my classes, seeing the code: static void Main(string[] args)
{
TxFilterController fltCont = new TxFilterController();
TxIpFilter flt = new TxIpFilter();
// Not icmp traffic
// For icmp traffic:
// source port = icmp type
// destination port = icmp code
flt.direction = TxIpFilter.IN_DIRECTION;
flt.ipSource = IPAddress.Any;
flt.maskSource = IPAddress.Any;
flt.ipDestination = IPAddress.Any;
flt.maskDestination = IPAddress.Any;
flt.protocol = TxIpFilter.ICMP_PROTOCOL;
flt.sourcePort = TxIpFilter.ANY_ICMP_TYPES;
flt.destinationPort = TxIpFilter.ANY_ICMP_TYPES;
fltCont.AddFilter(IPAddress.Parse("172.16.0.5"), flt);
// Not allow outgoing http traffic
fltCont.AddFilter(IPAddress.Parse("172.16.0.5"),
TxIpFilter.OUT_DIRECTION,
IPAddress.Parse("172.16.0.5"),
IPAddress.Parse("255.255.255.255"),
IPAddress.Any,
IPAddress.Any,
TxIpFilter.ANY_TCPUDP_PORTS,
80,
TxIpFilter.TCP_PROTOCOL);
Console.ReadLine();
fltCont.CloseController();
}
ConclusionWhere can you use this class? You can use in applications where you can add easily, packet filtering functionality. You can use in a complete filtering application because this API is few flexible: you can only filter at IP and transport level (IPs, ports and protocol), don't filter at link level and don't filter at application level. I don't know if you can use Packet Filtering API with Wan interfaces, because I don't have a modem to test. If you test it, please tell me the result. And... that's all. I hope this class will be useful for somebody.
|
||||||||||||||||||||||