Skip to content
KitploitKITPLOIT
工具博客
提交
工具博客
提交

黑客、渗透测试和网络安全工具,武装您的安全武器库!

Kitploit 是一个黑客、网络安全和渗透测试工具的目录。发现最新的项目更新,查找漏洞、分析系统、自动化测试并加强你的安全。

··订阅源·联系·隐私·© 2026 Kitploit

工具目录

分类

查看所有分类
Loading categories
RightHand-Persistence — COM Windows持久化技术 | Kitploit
工具/GitHubGitHub/i014n/righthand-persistence
持久化机制漏洞利用后渗透利用学习与教育红队
GitHubi014n/righthand-persistence

RightHand-Persistence

COM Windows持久化技术

查看仓库
90113个月前Kitploit 审核通过

最受欢迎

查看全部 →

发现我们社区最常用的工具。

探索所有工具

浏览我们的工具集合

查看所有工具 →
分享

RightHand Persistence

灵感来源于 CVE-2026-21509

用于持久化的 C++ COM 上下文菜单处理程序

一个使用 C++ 和 COM 实现的 Windows 上下文菜单处理程序示例,展示了隐蔽的持久化技术。通过注册自定义 COM 对象,当用户在 Windows 资源管理器中右键单击特定目标(文件、文件夹或背景)时,你的代码将执行。

免责声明: 本项目仅供教育和研究目的使用。此处演示的技术可能被用于恶意用途。作者不赞同将本代码用于任何非法活动。请负责任地使用此知识,并遵守道德规范。


POC

poc

开始使用

前提条件

  • C++ 编译器(例如 Visual Studio 中的 MSVC 或 MinGW-w64)
  • Windows SDK
  • 测试虚拟机(用于安全注册和执行)

我的环境

  • IDE: Visual Studio 2019
  • 开发虚拟机: Windows 10 x64 (Build 19045)
  • 测试虚拟机: Windows 10 x64 / Windows 11 x64

项目架构

root@kitploit:~
                                       DllMain.cpp     
                                +-----------------------+
                                |  DLL 模板项目         |
                                +-------+-------+-------+
                            ____/           |        \_____
                            |               |             |
                            v               v             v
                    +------------+  +----------------+  +--------------+
                    |   定义     |  | MyClassFactory |  |   定义       |
    clsid_defined.h |   CLSID    |  +----------------+  |   导出       | Source.def
                    +------------+     ClassFactory     |   函数       |
                                          class         +--------------+
                                            |
                                            |
                                            v
                                     +--------------+    
                                     | MyMenuHandler|
                                     +--------------+         
                                      MyMenuHandler
                                          class

1. DllMain.cpp

包含 COM 对象注册和注销 DLL 所需的标准函数。

  • DllRegisterServer(): 写入与目标文件扩展名/背景对应的注册表路径。在注册 DLL 时执行。
root@kitploit:~
STDAPI DllRegisterServer() {
    std::wstring clsidString = MyStringFromCLSID(CLSID_DecrypShellExtensionx64);
    std::wstring dllPath = MyGetModuleFilename();

    // 1. Register the COM Class (CLSID)
    std::wstring clsidBaseKey = L"SOFTWARE\\Classes\\CLSID\\" + clsidString;
    SetRegistryKey(HKEY_LOCAL_MACHINE, clsidBaseKey, L"", L"MyMenuHandler Object");

    // 2. Register the DLL Path and Threading Model
    std::wstring inprocKey = clsidBaseKey + L"\\InprocServer32";
    SetRegistryKey(HKEY_LOCAL_MACHINE, inprocKey, L"", dllPath);
    SetRegistryKey(HKEY_LOCAL_MACHINE, inprocKey, L"ThreadingModel", L"Apartment");

    // 3. Register for all Shell Contexts
    // Array of paths to cover Files, Folders, Backgrounds, and Desktop
    std::wstring handlerPaths[] = {
        L"SOFTWARE\\Classes\\*\\shellex\\ContextMenuHandlers\\",
        L"SOFTWARE\\Classes\\Directory\\shellex\\ContextMenuHandlers\\",
        L"SOFTWARE\\Classes\\Directory\\Background\\shellex\\ContextMenuHandlers\\",
        L"SOFTWARE\\Classes\\DesktopBackground\\shellex\\ContextMenuHandlers\\"
    };

    for (const auto& path : handlerPaths) {
        std::wstring fullPath = path + L"MyMenuHandler"; // Replace with your handler's name
        SetRegistryKey(HKEY_LOCAL_MACHINE, fullPath, L"", clsidString);
    }

    // 4. Register in the Approved list (Required for many Windows versions)
    std::wstring approvedKey = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved";
    SetRegistryKey(HKEY_LOCAL_MACHINE, approvedKey, clsidString, L"MyMenuHandler");

    // 5. Notify the Shell that things have changed
    SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);

    return S_OK;
}
  • DllUnregisterServer(): 清理注册表。在注销 DLL 时调用。

2. MyClassFactory

用于实例化 COM 对象。了解更多关于类工厂的信息。

3. Clsid_defined.h

包含应用程序在资源管理器中注册时所使用的 GUID。你可以使用 Visual Studio 的 guidgen.exe 或在线 GUID 生成器 生成新的 GUID。

root@kitploit:~
// {CEF1AA1B-42F7-4A54-AF46-BCEE5B3FE6BF}
DEFINE_GUID(CLSID_DecrypShellExtensionx64, 0xcef1aa1b, 0x42f7, 0x4a54, 0xaf, 0x46, 0xbc, 0xee, 0x5b, 0x3f, 0xe6, 0xbf);

4. Source.def

模块定义文件。指定了 DLL 要导出的函数。了解更多关于 .DEF 文件的信息。

root@kitploit:~
EXPORTS
    DllGetClassObject PRIVATE
    DllCanUnloadNow PRIVATE
    DllRegisterServer PRIVATE
    DllUnregisterServer PRIVATE

5. MyMenuHandler.cpp

实现 IShellExtInit 和 IContextMenu 的核心类。这是你定义自定义操作的地方。

  • MyContextMenuHandler::Initialize(PCIDLIST_ABSOLUTE pidlFolder, IDataObject* pdtobj, HKEY hkeyProgId) 每当右键单击已注册的目标时调用。用于解析点击的文件或文件夹。
root@kitploit:~
HRESULT MyContextMenuHandler::Initialize(PCIDLIST_ABSOLUTE pidlFolder, IDataObject* pdtobj, HKEY hkeyProgId)
{
    MessageBoxA(NULL, "Initialize Called!", "Debug", MB_OK);

    // 1. Check if we clicked on a FILE/FOLDER
    if (pdtobj)
    {
        STGMEDIUM medium;
        FORMATETC fe = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
        if (SUCCEEDED(pdtobj->GetData(&fe, &medium)))
        {
            DragQueryFileA((HDROP)medium.hGlobal, 0, m_szFile, MAX_PATH);
            ReleaseStgMedium(&medium);
            return S_OK; // Success!
        }
    }

    // 2. Check if we clicked the BACKGROUND (pidlFolder)
    if (pidlFolder)
    {
        if (SHGetPathFromIDListA(pidlFolder, m_szFile))
        {
            return S_OK; // Success!
        }
    }

    // 3. Fallback: If we got neither, still return S_OK to show the menu.
    // You just won't have a path populated in m_szFile.
    return S_OK;
}
  • MyContextMenuHandler::InvokeCommand(LPCMINVOKECOMMANDINFO picp) 当用户点击上下文菜单中的特定自定义选项时调用。这是你的持久化负载或自定义操作执行的地方。

测试

在目标虚拟机上,复制编译好的 DLL 并运行以下命令:

1. 注册

使用标准的 Windows 二进制文件注册 DLL。

root@kitploit:~
regsvr32.exe RightHandPersistence.dll

2. 重启资源管理器

重启 explorer.exe 进程,以确保它将新的 shell 扩展加载到内存中。

root@kitploit:~
taskkill /f /im explorer.exe & start explorer.exe

3. 注销

要移除上下文菜单处理程序并清理注册表,请使用 regsvr32.exe 的 /u 标志。这将调用你的 DllUnregisterServer 函数。

root@kitploit:~
regsvr32.exe /u RightHandPersistence.dll

自定义

1. 针对特定位置

Windows 对“空白空间”和文件的分类方式不同。要针对背景,请将你的注册表键映射到以下特定位置。

更多关于映射扩展的详细信息,请参阅 Microsoft Shell 上下文菜单文档。

2. 创建自定义菜单命令

你可以通过实现 IContextMenu::QueryContextMenu 方法来指定菜单的自定义文本(例如“自定义复制”或“运行诊断”)。单击此自定义文本会触发你在 IContextMenu::InvokeCommand 中的逻辑。


参考

  • 创建快捷菜单处理程序 (Microsoft Learn)
  • IShellExtInit 接口 (Microsoft Learn)
  • IContextMenu 接口 (Microsoft Learn)
  • 注册 Shell 扩展处理程序 (Microsoft Learn)
  • 使用 DEF 文件从 DLL 导出 (Microsoft Learn)
  • GuidGenerator.com
下载工具
右键目标注册表键路径
所有文件HKEY_CLASSES_ROOT\*\shellex\ContextMenuHandlers
文件夹(图标)HKEY_CLASSES_ROOT\Directory\shellex\ContextMenuHandlers
文件夹背景HKEY_CLASSES_ROOT\Directory\Background\shellex\ContextMenuHandlers
桌面背景HKEY_CLASSES_ROOT\DesktopBackground\shellex\ContextMenuHandlers