【问题标题】:Windows Filtering Platform - How can I block incoming connections based on local port?Windows 过滤平台 - 如何阻止基于本地端口的传入连接?
【发布时间】:2014-09-29 23:25:16
【问题描述】:

我正在尝试使用 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 上的服务器的连接。

任何帮助将不胜感激

【问题讨论】:

    标签: c# wfp


    【解决方案1】:

    您应该能够在任何支持 FWPM_CONDITION_IP_LOCAL_PORT 条件的 INBOUND 或 RECV 层上创建该过滤器,要搜索的资源是:

    http://msdn.microsoft.com/en-us/library/windows/hardware/ff549939%28v=vs.85%29.aspx

    但是,并非所有流量都通过每一层,我绝不是专家,但一种方法是在每个适用层(大约六个过滤器)中添加一个类似的过滤器,看看是否有效。如果是这样,则一次删除一个过滤器,直到找到实际需要的那组。在最近的一个项目中,我需要 4 层来阻止我感兴趣的所有流量。

    可能值得注意的一个重要警告是 localhost 上的流量可能不会通过任何 WFP 层(或者它可能只是它跳过的入站层,我不记得了)。因此,您可以使用 WFP 阻止与端口的远程连接,但本地连接可能仍会通过。

    【讨论】:

      猜你喜欢
      • 2015-08-15
      • 2014-07-20
      • 2019-09-04
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多