Available since: Windows Packet Filter 3.6.1
Windows Packet Filter 3.6.1 introduced runtime dynamic static-filter management and driver-level flow caching. Prior to version 3.6.1, modifying static filters required allocating a full STATIC_FILTER_TABLE structure and calling SetPacketFilterTable, which completely replaced all existing active rules. With 3.6.1, developers can add, insert, and remove individual static filters at runtime without submitting a complete replacement table.
Key Concepts
- Rule Evaluation Order: Static filters in
ndisrd.sysare evaluated sequentially from index 0 to N. The first filter matching a packet determines the action (FILTER_PACKET_PASS,FILTER_PACKET_DROP,FILTER_PACKET_REDIRECT,FILTER_PACKET_PASS_RDR, orFILTER_PACKET_DROP_RDR). - Prepend vs Append: Use AddStaticFilterFront to insert high-priority rules (such as emergency IP blocks) that must take immediate precedence over existing rules. Use AddStaticFilterBack for fallback or low-priority inspection policies.
- Arbitrary Insertion: Use InsertStaticFilter to place a rule at an exact zero-based position. The public API takes a
PSTATIC_FILTERand a separate position; the SDK constructs the internal STATIC_FILTER_WITH_POSITION IOCTL payload. - Dynamic Removal: Use RemoveStaticFilter to delete a filter by index. Subsequent filters in the table automatically shift forward.
- Filter Decision Cache: When EnablePacketFilterCache is active, the packet filter cache is enabled in the driver. The public user-mode SDK does not define its cache keys or invalidation timing.
Typical Workflow Example (C++)
This example assumes coordinated, exclusive control of filter-table edits while it runs. A rule index is a position, not a stable identifier: another insertion or removal can change which rule occupies index 0. The example adds a rule, queries the table, and removes that rule immediately. Include the official SDK headers and link the matching native SDK library.
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <iostream>
#include <tchar.h>
#include "Common.h"
#include "ndisapi.h"
int main()
{
CNdisApi api;
if (!api.IsDriverLoaded()) {
std::cerr << "Driver not loaded." << std::endl;
return 1;
}
// 1. Prepare a static filter (e.g. block incoming TCP port 23 Telnet)
STATIC_FILTER filter;
ZeroMemory(&filter, sizeof(filter));
filter.m_Adapter.QuadPart = 0; // all adapters
filter.m_ValidFields = NETWORK_LAYER_VALID | TRANSPORT_LAYER_VALID;
filter.m_FilterAction = FILTER_PACKET_DROP;
filter.m_dwDirectionFlags = PACKET_FLAG_ON_RECEIVE;
// IPv4 rule: match TCP protocol
filter.m_NetworkFilter.m_dwUnionSelector = IPV4;
filter.m_NetworkFilter.m_IPv4.m_ValidFields = IP_V4_FILTER_PROTOCOL;
filter.m_NetworkFilter.m_IPv4.m_Protocol = IPPROTO_TCP;
// Transport TCP port 23
filter.m_TransportFilter.m_dwUnionSelector = TCPUDP;
filter.m_TransportFilter.m_TcpUdp.m_ValidFields = TCPUDP_DEST_PORT;
filter.m_TransportFilter.m_TcpUdp.m_DestPort.m_StartRange = 23;
filter.m_TransportFilter.m_TcpUdp.m_DestPort.m_EndRange = 23;
// 2. Prepend the block rule at the front of the filter table
if (!api.AddStaticFilterFront(&filter)) {
std::cerr << "Could not add the filter." << std::endl;
return 1;
}
std::cout << "Port 23 block filter successfully added to front." << std::endl;
// 3. Query current table size
DWORD tableSize = 0;
if (api.GetPacketFilterTableSize(&tableSize)) {
std::cout << "Active static filter rules: " << tableSize << std::endl;
}
// 4. Remove rule at index 0 when done
if (!api.RemoveStaticFilter(0)) {
std::cerr << "Could not remove the filter." << std::endl;
return 1;
}
return 0;
}
See Also
AddStaticFilterFront, AddStaticFilterBack, InsertStaticFilter, RemoveStaticFilter, STATIC_FILTER