Windows WFP驱动程序:在ClassifyFn回调中处理数据包时获取BSOD

问题描述 投票:0回答:1

我正在尝试编写一个简单的防火墙应用程序,该应用程序可以允许或阻止用户级进程进行的网络连接尝试。

为此,在WFPStarterKit教程之后,我创建了一个WFP驱动程序,该驱动程序设置为在FWPM_LAYER_OUTBOUND_TRANSPORT_V4层上拦截数据。

ClassifyFn回调函数负责拦截连接尝试,并允许或拒绝它。

一旦ClassifyFn回调被点击,数据包的ProcessID就会与其他一些信息一起通过FltSendMessage函数发送到用户级进程。

用户级进程接收到消息,检查ProcessID,并向驱动程序回复布尔允许/拒绝命令。

虽然此方法在阻止第一个数据包时有效,但在某些情况下(尤其是在允许多个数据包时),代码会生成带有INVALID_PROCESS_ATTACH_ATTEMPT错误代码的BSOD。该错误是在调用FltSendMessage时触发的。

虽然我仍然无法查明确切的问题,在某些情况下,使标注线程等待(通过FltSendMessage)等待用户级别的答复可能会生成此BSOD错误。

如果您能指出正确的方向,我将不胜感激。

这里是我注册标注的功能:

    NTSTATUS register_example_callout(DEVICE_OBJECT * wdm_device)
{
    NTSTATUS status = STATUS_SUCCESS;
    FWPS_CALLOUT s_callout = { 0 };
    FWPM_CALLOUT m_callout = { 0 };
    FWPM_DISPLAY_DATA display_data = { 0 };

    if (filter_engine_handle == NULL)
        return STATUS_INVALID_HANDLE;

    display_data.name = EXAMPLE_CALLOUT_NAME;
    display_data.description = EXAMPLE_CALLOUT_DESCRIPTION;

    // Register a new Callout with the Filter Engine using the provided callout functions
    s_callout.calloutKey = EXAMPLE_CALLOUT_GUID;
    s_callout.classifyFn = example_classify;
    s_callout.notifyFn = example_notify;
    s_callout.flowDeleteFn = example_flow_delete;
    status = FwpsCalloutRegister((void *)wdm_device, &s_callout, &example_callout_id);
    if (!NT_SUCCESS(status)) {
        DbgPrint("Failed to register callout functions for example callout, status 0x%08x", status);
        goto Exit;
    }

    // Setup a FWPM_CALLOUT structure to store/track the state associated with the FWPS_CALLOUT
    m_callout.calloutKey = EXAMPLE_CALLOUT_GUID;
    m_callout.displayData = display_data;
    m_callout.applicableLayer = FWPM_LAYER_OUTBOUND_TRANSPORT_V4;
    m_callout.flags = 0;
    status = FwpmCalloutAdd(filter_engine_handle, &m_callout, NULL, NULL);
    if (!NT_SUCCESS(status)) {
        DbgPrint("Failed to register example callout, status 0x%08x", status);
    }
    else {
        DbgPrint("Example Callout Registered");
    }

Exit:
    return status;
}

这里是标注功能:

    /*************************
ClassifyFn Function
**************************/
void example_classify(
    const FWPS_INCOMING_VALUES * inFixedValues,
    const FWPS_INCOMING_METADATA_VALUES * inMetaValues,
    void * layerData,
    const void * classifyContext,
    const FWPS_FILTER * filter,
    UINT64 flowContext,
    FWPS_CLASSIFY_OUT * classifyOut)
{
    UNREFERENCED_PARAMETER(layerData);
    UNREFERENCED_PARAMETER(classifyContext);
    UNREFERENCED_PARAMETER(flowContext);
    UNREFERENCED_PARAMETER(filter);
    UNREFERENCED_PARAMETER(inMetaValues);

    NETWORK_ACCESS_QUERY AccessQuery;
    BOOLEAN SafeToOpen = TRUE;
    classifyOut->actionType = FWP_ACTION_PERMIT;

    AccessQuery.remote_address = inFixedValues->incomingValue[FWPS_FIELD_OUTBOUND_TRANSPORT_V4_IP_REMOTE_ADDRESS].value.uint32;
    AccessQuery.remote_port = inFixedValues->incomingValue[FWPS_FIELD_OUTBOUND_TRANSPORT_V4_IP_REMOTE_PORT].value.uint16;

    // Get Process ID
    AccessQuery.ProcessId = (UINT64)PsGetCurrentProcessId();

    if (!AccessQuery.ProcessId) 
    {
        return;
    }

    // Here we connect to our userlevel application using FltSendMessage.
    // Some checks are done and the SafeToOpen variable is populated with a BOOLEAN which indicates if to allow or block the packet.
    // However, sometimes, a BSOD is generated with an INVALID_PROCESS_ATTACH_ATTEMPT error on the FltSendMessage call
    QueryUserLevel(QUERY_NETWORK, &AccessQuery, sizeof(NETWORK_ACCESS_QUERY), &SafeToOpen, NULL, 0);

    if (!SafeToOpen) {
        classifyOut->actionType = FWP_ACTION_BLOCK;
    }

    return;
}
c windows driver wfp
1个回答
0
投票

WFP驱动程序使用inverted call model与用户模式应用程序通信。通过这种方法,您可以使内核模式驱动程序实例中的用户模式下的IRP处于挂起状态,并且每当要将数据发送回用户模式时,您都需要完成IRP以及要发送回的数据。

© www.soinside.com 2019 - 2024. All rights reserved.