我正在尝试使用 WFP 设置一些过滤器来阻止与本地服务器的入站连接(例如,侦听端口 8080 的网络服务器)。
我有一个可以基于远程端口阻止的过滤器,因此我可以阻止我的机器上的进程建立与端口 8080 的任何连接,但我不知道如何根据本地阻止来自另一台机器的传入连接8080端口?
这是我的代码,它可以基于远程端口进行阻塞:(它是使用 P/invoke 的 C#,但它与用 C++ 编写的几乎相同)
var RemotePort = 8080 # port to block
// connect to engine
var session = new Fwpm.FWPM_SESSION0 { flags = Fwpm.FWPM_SESSION_FLAG_DYNAMIC };
UInt32 engineHandle;
UnsafeNativeMethods.FwpmEngineOpen0(null, Fwpm.RPC_C_AUTHN_WINNT, IntPtr.Zero, session, out engineHandle
// create a subLayer to attach filters to
var subLayerGuid = Guid.NewGuid();
var subLayer = new Fwpm.FWPM_SUBLAYER0();
subLayer.subLayerKey = subLayerGuid;
subLayer.displayData.name = DisplayName;
subLayer.displayData.description = DisplayName;
subLayer.flags = 0;
subLayer.weight = 0x100;
UnsafeNativeMethods.FwpmSubLayerAdd0(engineHandle, subLayer, IntPtr.Zero)
var condition = new Fwpm.FWPM_FILTER_CONDITION0 {
fieldKey = Fwpm.FWPM_CONDITION_IP_REMOTE_PORT,
matchType = Fwpm.FWP_MATCH_TYPE.FWP_MATCH_EQUAL,
conditionValue = {
type = Fwpm.FWP_DATA_TYPE.FWP_UINT16,
uint16 = RemotePort
}
}
// create the filter itself
var fwpFilter = new Fwpm.FWPM_FILTER0();
fwpFilter.layerKey = Fwpm.FWPM_LAYER_ALE_AUTH_CONNECT_V4;
fwpFilter.action.type = Fwpm.FWP_ACTION_BLOCK;
fwpFilter.subLayerKey = subLayerGuid;
fwpFilter.weight.type = Fwpm.FWP_DATA_TYPE.FWP_EMPTY; // auto-weight.
fwpFilter.numFilterConditions = (uint)1;
var condsArray = new[]{ condition };
var condsPtr = SafeNativeMethods.MarshalArray(condsArray); // helper to create a native array from a C# one
fwpFilter.filterCondition = condsPtr;
fwpFilter.displayData.name = DisplayName;
fwpFilter.displayData.description = DisplayName;
// add the filter
UInt64 filterId = 0L;
UnsafeNativeMethods.FwpmFilterAdd0(engineHandle, ref fwpFilter, IntPtr.Zero, out filterId));
如上所述,此代码确实可以阻止与远程端口 8080 的连接。为了阻止与本地端口 8080 的连接,我将代码修改如下:
var LocalPort = 8080;
var condition = new Fwpm.FWPM_FILTER_CONDITION0 {
fieldKey = Fwpm.FWPM_CONDITION_IP_LOCAL_PORT,
matchType = Fwpm.FWP_MATCH_TYPE.FWP_MATCH_EQUAL,
conditionValue = {
type = Fwpm.FWP_DATA_TYPE.FWP_UINT16,
uint16 = LocalPort
}
}
// create the filter itself
var fwpFilter = new Fwpm.FWPM_FILTER0();
fwpFilter.layerKey = Fwpm.FWPM_LAYER_ALE_AUTH_RECV_ACCEPT_V4;
MSDN 暗示这FWPM_LAYER_ALE_AUTH_RECV_ACCEPT_V4
是阻止入站连接的正确位置,但这根本不起作用。我已经尝试FWPM_LAYER_ALE_RESOURCE_ASSIGNMENT_V4
了其他一些层,但无论我尝试了什么,我总是能够建立从另一台机器到我机器上端口 8080 上的服务器的连接。
任何帮助将非常感激