思路

众所周知传统的反沙盒思路是调用函数查询系统的一些配置,如时区、内存、CPU等,但是这种行为是被标记为高敏感度行为,如果写入反沙盒会适得其反。所以我们需要一种没有任何威胁的行为去把这事办了,截图是很正常的一个行为,如果我对桌面进行局部截图,检查特征进行匹配,那么就可以完成0行为的反沙盒了。

CODE

#include <windows.h>
#include <vector>
#include <string>
#include <cmath>

struct Color {
    unsigned char b;
    unsigned char g;
    unsigned char r;
    unsigned char a;
};

void DebugLog(const std::string& message) {
    OutputDebugStringA((message + "\n").c_str());
}

bool IsTargetBlue(const Color& c) {
    if (c.b < c.r + 50) return false;
    if (c.b < c.g + 20) return false;
    if (c.b < 140) return false;
    if (c.r > 100) return false;

    return true;
}

bool IsWhite(const Color& c) {
    return (c.r > 200 && c.g > 200 && c.b > 200);
}

bool CaptureScreen(int& width, int& height, std::vector<Color>& pixels) {
    width = GetSystemMetrics(SM_CXSCREEN);
    height = GetSystemMetrics(SM_CYSCREEN);

    HDC hScreenDC = GetDC(NULL);
    HDC hMemoryDC = CreateCompatibleDC(hScreenDC);

    HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
    SelectObject(hMemoryDC, hBitmap);

    if (!BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY)) {
        DeleteObject(hBitmap);
        DeleteDC(hMemoryDC);
        ReleaseDC(NULL, hScreenDC);
        return false;
    }

    BITMAPINFOHEADER bi = { 0 };
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = width;
    bi.biHeight = -height;
    bi.biPlanes = 1;
    bi.biBitCount = 32;
    bi.biCompression = BI_RGB;

    pixels.resize(width * height);

    if (!GetDIBits(hMemoryDC, hBitmap, 0, height, pixels.data(), (BITMAPINFO*)&bi, DIB_RGB_COLORS)) {
        DeleteObject(hBitmap);
        DeleteDC(hMemoryDC);
        ReleaseDC(NULL, hScreenDC);
        return false;
    }

    DeleteObject(hBitmap);
    DeleteDC(hMemoryDC);
    ReleaseDC(NULL, hScreenDC);

    return true;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    (void)hInstance;
    (void)hPrevInstance;
    (void)lpCmdLine;
    (void)nCmdShow;

    DebugLog("Analyzing screen features...");

    int width = 0;
    int height = 0;
    std::vector<Color> pixels;

    if (!CaptureScreen(width, height, pixels)) {
        DebugLog("Screen capture failed!");
        MessageBoxW(NULL, L"Screen capture failed!", L"Error", MB_OK | MB_ICONERROR);
        return 1;
    }

    int blueCount = 0;
    int sampleCount = 0;

    for (int y = 0; y < height * 0.2; ++y) {
        for (int x = width * 0.5; x < width; ++x) {
            int index = y * width + x;
            if (IsTargetBlue(pixels[index])) {
                blueCount++;
            }
            sampleCount++;
        }
    }

    double blueRatio = (double)blueCount / sampleCount;
    DebugLog("Background Blue Ratio: " + std::to_string(blueRatio * 100) + "%");

    if (blueRatio < 0.8) {
        DebugLog("Detection Failed: Background is not target blue.");

        std::wstring msg = L"\u68c0\u6d4b\u5931\u8d25\uff1a\u80cc\u666f\u989c\u8272\u4e0d\u5339\u914d\n";
        msg += L"Blue Ratio: " + std::to_wstring((int)(blueRatio * 100)) + L"%";

        MessageBoxW(NULL, msg.c_str(), L"\u68c0\u6d4b\u7ed3\u679c", MB_OK | MB_ICONWARNING);
        return 0;
    }

    int startY = height * 0.45;
    int endY = height * 0.55;
    int startX = width * 0.3;
    int endX = width * 0.7;

    int whitePixelCount = 0;
    int centerTotalPixels = (endY - startY) * (endX - startX);

    for (int y = startY; y < endY; ++y) {
        for (int x = startX; x < endX; ++x) {
            int index = y * width + x;
            if (IsWhite(pixels[index])) {
                whitePixelCount++;
            }
        }
    }

    double whiteRatio = (double)whitePixelCount / centerTotalPixels;
    DebugLog("Center White Ratio: " + std::to_string(whiteRatio * 100) + "%");

    if (whiteRatio > 0.01 && whiteRatio < 0.20) {
        DebugLog("Feature Match Success!");
        MessageBoxW(NULL, L"\u5fae\u6b65\u4e91", L"\u68c0\u6d4b\u7ed3\u679c", MB_OK | MB_ICONINFORMATION);
    }
    else {
        DebugLog("Detection Failed: Center text feature not found.");

        std::wstring msg = L"\u68c0\u6d4b\u5931\u8d25\uff1a\u672a\u53d1\u73b0\u4e2d\u5fc3\u7279\u5f81\u6587\u5b57\n";
        msg += L"White Ratio: " + std::to_wstring(whiteRatio * 100) + L"%";

        MessageBoxW(NULL, msg.c_str(), L"\u68c0\u6d4b\u7ed3\u679c", MB_OK | MB_ICONWARNING);
    }

    return 0;
}

分析

本文以微步云沙箱为案例,其他的思路大同小异

这是一个纯净的桌面壁纸图,可以自取

我实现了一种非常隐蔽且高级的**“环境键控”(Environmental Keying)** 反沙盒技术。它不依赖传统的检测虚拟机文件、注册表或 CPU 指令的方法,而是通过 **“视觉验证”**来判断自己是否运行在目标机器上。

以下是对代码的详细解读

1. 核心原理:视觉环境键控 (Visual Environmental Keying)

为什么能反沙盒? 沙盒和自动化分析环境(如 Cuckoo, Any.Run, 微步云沙箱等)通常使用的是 标准、纯净的 Windows 虚拟机 。这些虚拟机的桌面壁纸通常是:

  • Windows 默认壁纸(蓝色 Logo)。
  • 纯色背景。
  • 没有任何杂乱图标或特定文字的干净桌面。只有在屏幕上**“看到”**特定的特征(特定的蓝色背景 + 中间有特定的白色文字)时,才会认为自己处于沙盒中,从而执行一个messagebox)。

2. API 截屏 (无外部库)

代码完全使用了 windows.h 中的 GDI (Graphics Device Interface) 接口,这是 Windows 95 时代就存在的底层绘图 API, 所有 Windows 系统都内置,无需安装任何库 。

  • CaptureScreen 函数 (L30-69) :
  • GetDC(NULL) / CreateCompatibleDC :获取整个屏幕的画布。
  • BitBlt :这是最核心的函数,将屏幕图像“拷贝”到内存中。这是一个非常通用的系统调用,截图工具、远程桌面、录屏软件都在用, 杀毒软件很难仅仅因为调用了这个 API 就报毒 ,因为它太常见了。
  • GetDIBits :将内存中的图片转换为原始的 RGB 像素数组 ( std::vector ),方便后续直接操作内存。 B. 手写像素级分析 (替代 OpenCV) 通常图像识别需要 OpenCV 这样庞大的库(几十 MB),容易暴露特征。但这里用了最原始的 C++ 指针操作:
  • IsTargetBlue / IsWhite (L17-28) :
  • 直接判断 RGB 数值。不需要复杂的计算机视觉算法,只需要简单的 if (b > 140 && r < 100) 就能区分出特定的蓝色。
  • 特征提取逻辑 (L89-146) :
  • 分区检测 :代码没有扫描全屏(全屏太慢且容易受干扰),而是极具针对性:
  • L92 :只扫 顶部 20% (通常是纯背景,避开任务栏)。
  • L115 :只扫 屏幕正中心 (检测特定 LOGO 或文字)。
  • 统计学验证 :不要求 100% 匹配(如OpenCV做视觉识别),而是计算“占比” ( BLUE:WITHE )。只要蓝色像素超过 80%,或者白色像素在 1%-20% 之间,就认为匹配。

3.体积小

我只使用了windows内置的库,没有大费周章调用OpenCV或者OCR等,更加适配不同环境

微步云报告

https://s.threatbook.com/report/file/948a1a58e856cc43898a2ea78810b1dc73ea7f25d1d7efc166682c438ca38f25

运行截图

行为截图