-
-
未解决 [求助]关于调用NtCreateUserProcess函数成功创建进程并执行命令后如何取回的结果的问题
-
发表于: 2023-11-3 16:41 2247
-
最近在学习如何调用NtCreateUserProcess函数创建新的子进程,实现了创建一个cmd.exe并执行命令,但我想把执行结果从新建的子进程中取回到父进程。
最初的思路是想对子进程的标准输出句柄重定向到匿名管道的写入端,在父进程中读取管道的读出端。
在调用CreateProcess函数时我们可以利用SECURITY_ATTRIBUTES结构中的bInheritHandle成员,将其设置为 TRUE实现继承,并在STARTUPINFO结构体中指定hStdOutput和hStdError重定向到匿名管道的写入端。
但是在调用NtCreateUserProcess函数创建子进程时,我不知道该在哪个地方对标准输出进行重定向的指定。我尝试在AttributeList属性列表中添加标准句柄的信息,但是我发现该属性似乎并不能重定向标准输出句柄,可能只是起辅助作用。而且我发现PRTL_USER_PROCESS_PARAMETERS结构体中存在StandardInput、StandardOutput、StandardError相关字段,包括ConsoleFlags标志位可能是设置句柄继承的标志,但在调用RtlCreateProcessParametersEx填充PRTL_USER_PROCESS_PARAMETERS结构体时,这些字段并未被填充。
我应该在如何在调用NtCreateUserProcess函数创建经常时,重定向子进程的标准输出句柄?请各位老师不吝赐教,愿意付费,万分感谢!
代码如下:
头文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | #pragma once #pragma comment( lib, "ntdll" ) #include <Windows.h> #ifdef __cplusplus //指示编译器使用C++标准库和C++语言的特性来处理代码 extern "C" { #endif typedef enum _PS_ATTRIBUTE_NUM { PsAttributeParentProcess, / / in HANDLE PsAttributeDebugObject, / / in HANDLE PsAttributeToken, / / in HANDLE PsAttributeClientId, / / out PCLIENT_ID PsAttributeTebAddress, / / out PTEB * PsAttributeImageName, / / in PWSTR PsAttributeImageInfo, / / out PSECTION_IMAGE_INFORMATION PsAttributeMemoryReserve, / / in PPS_MEMORY_RESERVE PsAttributePriorityClass, / / in UCHAR PsAttributeErrorMode, / / in ULONG PsAttributeStdHandleInfo, / / 10 , in PPS_STD_HANDLE_INFO PsAttributeHandleList, / / in HANDLE[] PsAttributeGroupAffinity, / / in PGROUP_AFFINITY PsAttributePreferredNode, / / in PUSHORT PsAttributeIdealProcessor, / / in PPROCESSOR_NUMBER PsAttributeUmsThread, / / ? in PUMS_CREATE_THREAD_ATTRIBUTES PsAttributeMitigationOptions, / / in PPS_MITIGATION_OPTIONS_MAP (PROCESS_CREATION_MITIGATION_POLICY_ * ) / / since WIN8 PsAttributeProtectionLevel, / / in PS_PROTECTION / / since WINBLUE PsAttributeSecureProcess, / / in PPS_TRUSTLET_CREATE_ATTRIBUTES, since THRESHOLD PsAttributeJobList, / / in HANDLE[] PsAttributeChildProcessPolicy, / / 20 , in PULONG (PROCESS_CREATION_CHILD_PROCESS_ * ) / / since THRESHOLD2 PsAttributeAllApplicationPackagesPolicy, / / in PULONG (PROCESS_CREATION_ALL_APPLICATION_PACKAGES_ * ) / / since REDSTONE PsAttributeWin32kFilter, / / in PWIN32K_SYSCALL_FILTER PsAttributeSafeOpenPromptOriginClaim, / / in PsAttributeBnoIsolation, / / in PPS_BNO_ISOLATION_PARAMETERS / / since REDSTONE2 PsAttributeDesktopAppPolicy, / / in PULONG (PROCESS_CREATION_DESKTOP_APP_ * ) PsAttributeChpe, / / in BOOLEAN / / since REDSTONE3 PsAttributeMitigationAuditOptions, / / in PPS_MITIGATION_AUDIT_OPTIONS_MAP (PROCESS_CREATION_MITIGATION_AUDIT_POLICY_ * ) / / since 21H1 PsAttributeMachineType, / / in WORD / / since 21H2 PsAttributeComponentFilter, PsAttributeEnableOptionalXStateFeatures, / / since WIN11 PsAttributeMax } PS_ATTRIBUTE_NUM; #define RTL_USER_PROCESS_PARAMETERS_NORMALIZED 0x01 #define PS_ATTRIBUTE_NUMBER_MASK 0x0000ffff #define PS_ATTRIBUTE_THREAD 0x00010000 // Attribute may be used with thread creation #define PS_ATTRIBUTE_INPUT 0x00020000 // Attribute is input only #define PS_ATTRIBUTE_ADDITIVE 0x00040000 // Attribute may be "accumulated", e.g. bitmasks, counters, etc. #define PsAttributeValue(Number, Thread, Input, Additive) \ (((Number) & PS_ATTRIBUTE_NUMBER_MASK) | \ ((Thread) ? PS_ATTRIBUTE_THREAD : 0 ) | \ (( Input ) ? PS_ATTRIBUTE_INPUT : 0 ) | \ ((Additive) ? PS_ATTRIBUTE_ADDITIVE : 0 )) #define PS_ATTRIBUTE_PARENT_PROCESS \ PsAttributeValue(PsAttributeParentProcess, FALSE, TRUE, TRUE) / / 0x60000 #define PS_ATTRIBUTE_DEBUG_OBJECT \ PsAttributeValue(PsAttributeDebugObject, FALSE, TRUE, TRUE) / / 0x60001 #define PS_ATTRIBUTE_TOKEN \ PsAttributeValue(PsAttributeToken, FALSE, TRUE, TRUE) / / 0x60002 #define PS_ATTRIBUTE_CLIENT_ID \ PsAttributeValue(PsAttributeClientId, TRUE, FALSE, FALSE) / / 0x10003 #define PS_ATTRIBUTE_TEB_ADDRESS \ PsAttributeValue(PsAttributeTebAddress, TRUE, FALSE, FALSE) / / 0x10004 #define PS_ATTRIBUTE_IMAGE_NAME \ PsAttributeValue(PsAttributeImageName, FALSE, TRUE, FALSE) / / 0x20005 #define PS_ATTRIBUTE_IMAGE_INFO \ PsAttributeValue(PsAttributeImageInfo, FALSE, FALSE, FALSE) / / 0x6 #define PS_ATTRIBUTE_MEMORY_RESERVE \ PsAttributeValue(PsAttributeMemoryReserve, FALSE, TRUE, FALSE) / / 0x20007 #define PS_ATTRIBUTE_PRIORITY_CLASS \ PsAttributeValue(PsAttributePriorityClass, FALSE, TRUE, FALSE) / / 0x20008 #define PS_ATTRIBUTE_ERROR_MODE \ PsAttributeValue(PsAttributeErrorMode, FALSE, TRUE, FALSE) / / 0x20009 #define PS_ATTRIBUTE_STD_HANDLE_INFO \ PsAttributeValue(PsAttributeStdHandleInfo, FALSE, TRUE, FALSE) / / 0x2000A #define PS_ATTRIBUTE_HANDLE_LIST \ PsAttributeValue(PsAttributeHandleList, FALSE, TRUE, FALSE) / / 0x2000B #define PS_ATTRIBUTE_GROUP_AFFINITY \ PsAttributeValue(PsAttributeGroupAffinity, TRUE, TRUE, FALSE) / / 0x2000C #define PS_ATTRIBUTE_PREFERRED_NODE \ PsAttributeValue(PsAttributePreferredNode, FALSE, TRUE, FALSE) / / 0x2000D #define PS_ATTRIBUTE_IDEAL_PROCESSOR \ PsAttributeValue(PsAttributeIdealProcessor, TRUE, TRUE, FALSE) / / 0x2000E #define PS_ATTRIBUTE_MITIGATION_OPTIONS \ PsAttributeValue(PsAttributeMitigationOptions, FALSE, TRUE, FALSE) / / 0x60010 #define PS_ATTRIBUTE_PROTECTION_LEVEL \ PsAttributeValue(PsAttributeProtectionLevel, FALSE, TRUE, FALSE) / / 0x20011 #define PS_ATTRIBUTE_SECURE_PROCESS \ PsAttributeValue(PsAttributeSecureProcess, FALSE, TRUE, FALSE) / / 0x20012 #define PS_ATTRIBUTE_JOB_LIST \ PsAttributeValue(PsAttributeJobList, FALSE, TRUE, FALSE) / / 0x20013 #define PS_ATTRIBUTE_CHILD_PROCESS_POLICY \ PsAttributeValue(PsAttributeChildProcessPolicy, FALSE, TRUE, FALSE) / / 0x20014 #define PS_ATTRIBUTE_ALL_APPLICATION_PACKAGES_POLICY \ PsAttributeValue(PsAttributeAllApplicationPackagesPolicy, FALSE, TRUE, FALSE) / / 0x20015 #define PS_ATTRIBUTE_WIN32K_FILTER \ PsAttributeValue(PsAttributeWin32kFilter, FALSE, TRUE, FALSE) / / 0x20016 #define PS_ATTRIBUTE_SAFE_OPEN_PROMPT_ORIGIN_CLAIM \ PsAttributeValue(PsAttributeSafeOpenPromptOriginClaim, FALSE, TRUE, FALSE) / / 0x20017 #define PS_ATTRIBUTE_BNO_ISOLATION \ PsAttributeValue(PsAttributeBnoIsolation, FALSE, TRUE, FALSE) / / 0x20018 #define PS_ATTRIBUTE_DESKTOP_APP_POLICY \ PsAttributeValue(PsAttributeDesktopAppPolicy, FALSE, TRUE, FALSE) / / 0x20019 #define PS_ATTRIBUTE_CHPE \ PsAttributeValue(PsAttributeChpe, FALSE, TRUE, TRUE) / / 0x6001A #define PS_ATTRIBUTE_MITIGATION_AUDIT_OPTIONS \ PsAttributeValue(PsAttributeMitigationAuditOptions, FALSE, TRUE, FALSE) / / 0x2001B #define PS_ATTRIBUTE_MACHINE_TYPE \ PsAttributeValue(PsAttributeMachineType, FALSE, TRUE, TRUE) / / 0x6001C #define PS_ATTRIBUTE_COMPONENT_FILTER \ PsAttributeValue(PsAttributeComponentFilter, FALSE, TRUE, FALSE) / / 0x2001D #define PS_ATTRIBUTE_ENABLE_OPTIONAL_XSTATE_FEATURES \ PsAttributeValue(PsAttributeEnableOptionalXStateFeatures, TRUE, TRUE, FALSE) / / 0x3001E typedef struct _PS_ATTRIBUTE { ULONG_PTR Attribute; / / PROC_THREAD_ATTRIBUTE_XXX | PROC_THREAD_ATTRIBUTE_XXX modifiers, see ProcThreadAttributeValue macro and Windows Internals 6 ( 372 ) SIZE_T Size; / / Size of Value or * ValuePtr union { ULONG_PTR Value; / / Reserve 8 bytes for data (such as a Handle or a data pointer) PVOID ValuePtr; / / data pointer }; PSIZE_T ReturnLength; / / Either 0 or specifies size of data returned to caller via "ValuePtr" } PS_ATTRIBUTE, * PPS_ATTRIBUTE; typedef enum _PS_IFEO_KEY_STATE { PsReadIFEOAllValues, PsSkipIFEODebugger, PsSkipAllIFEO, PsMaxIFEOKeyStates } PS_IFEO_KEY_STATE, * PPS_IFEO_KEY_STATE; typedef enum _PS_CREATE_STATE { PsCreateInitialState, PsCreateFailOnFileOpen, PsCreateFailOnSectionCreate, PsCreateFailExeFormat, PsCreateFailMachineMismatch, PsCreateFailExeName, / / Debugger specified PsCreateSuccess, PsCreateMaximumStates } PS_CREATE_STATE; typedef struct _PS_CREATE_INFO { SIZE_T Size; PS_CREATE_STATE State; union { / / PsCreateInitialState struct { union { ULONG InitFlags; struct { UCHAR WriteOutputOnExit : 1 ; UCHAR DetectManifest : 1 ; UCHAR IFEOSkipDebugger : 1 ; UCHAR IFEODoNotPropagateKeyState : 1 ; UCHAR SpareBits1 : 4 ; UCHAR SpareBits2 : 8 ; USHORT ProhibitedImageCharacteristics : 16 ; } s1; } u1; ACCESS_MASK AdditionalFileAccess; } InitState; / / PsCreateFailOnSectionCreate struct { HANDLE FileHandle; } FailSection; / / PsCreateFailExeFormat struct { USHORT DllCharacteristics; } ExeFormat; / / PsCreateFailExeName struct { HANDLE IFEOKey; } ExeName; / / PsCreateSuccess struct { union { ULONG OutputFlags; struct { UCHAR ProtectedProcess : 1 ; UCHAR AddressSpaceOverride : 1 ; UCHAR DevOverrideEnabled : 1 ; / / From Image File Execution Options UCHAR ManifestDetected : 1 ; UCHAR ProtectedProcessLight : 1 ; UCHAR SpareBits1 : 3 ; UCHAR SpareBits2 : 8 ; USHORT SpareBits3 : 16 ; } s2; } u2; HANDLE FileHandle; HANDLE SectionHandle; ULONGLONG UserProcessParametersNative; ULONG UserProcessParametersWow64; ULONG CurrentParameterFlags; ULONGLONG PebAddressNative; ULONG PebAddressWow64; ULONGLONG ManifestAddress; ULONG ManifestSize; } SuccessState; }; } PS_CREATE_INFO, * PPS_CREATE_INFO; typedef struct _UNICODE_STRING { USHORT Length; USHORT MaximumLength; PWSTR Buffer ; } UNICODE_STRING, * PUNICODE_STRING; typedef const UNICODE_STRING * PCUNICODE_STRING; typedef struct _PS_ATTRIBUTE_LIST { SIZE_T TotalLength; / / sizeof(PS_ATTRIBUTE_LIST) PS_ATTRIBUTE Attributes[ 6 ]; / / Depends on how many attribute entries should be supplied to NtCreateUserProcess } PS_ATTRIBUTE_LIST, * PPS_ATTRIBUTE_LIST; typedef struct _CURDIR { UNICODE_STRING DosPath; HANDLE Handle; } CURDIR, * PCURDIR; typedef struct _RTL_DRIVE_LETTER_CURDIR { USHORT Flags; USHORT Length; ULONG TimeStamp; UNICODE_STRING DosPath; } RTL_DRIVE_LETTER_CURDIR, * PRTL_DRIVE_LETTER_CURDIR; #define RTL_MAX_DRIVE_LETTERS 32 typedef struct _RTL_USER_PROCESS_PARAMETERS { ULONG MaximumLength; ULONG Length; ULONG Flags; ULONG DebugFlags; HANDLE ConsoleHandle; ULONG ConsoleFlags; HANDLE StandardInput; HANDLE StandardOutput; HANDLE StandardError; CURDIR CurrentDirectory; UNICODE_STRING DllPath; UNICODE_STRING ImagePathName; UNICODE_STRING CommandLine; PWCHAR Environment; ULONG StartingX; ULONG StartingY; ULONG CountX; ULONG CountY; ULONG CountCharsX; ULONG CountCharsY; ULONG FillAttribute; ULONG WindowFlags; ULONG ShowWindowFlags; UNICODE_STRING WindowTitle; UNICODE_STRING DesktopInfo; UNICODE_STRING ShellInfo; UNICODE_STRING RuntimeData; RTL_DRIVE_LETTER_CURDIR CurrentDirectories[RTL_MAX_DRIVE_LETTERS]; ULONG_PTR EnvironmentSize; ULONG_PTR EnvironmentVersion; PVOID PackageDependencyData; ULONG ProcessGroupId; ULONG LoaderThreads; } RTL_USER_PROCESS_PARAMETERS, * PRTL_USER_PROCESS_PARAMETERS; typedef struct _OBJECT_ATTRIBUTES { ULONG Length; HANDLE RootDirectory; PUNICODE_STRING ObjectName; ULONG Attributes; PVOID SecurityDescriptor; PVOID SecurityQualityOfService; } OBJECT_ATTRIBUTES, * POBJECT_ATTRIBUTES; NTSYSAPI NTSTATUS NTAPI RtlDestroyProcessParameters(PRTL_USER_PROCESS_PARAMETERS ProcessParameters); NTSYSAPI BOOLEAN NTAPI RtlFreeHeap(PVOID HeapHandle, ULONG Flags, PVOID BaseAddress); NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PWSTR SourceString); NTSYSAPI NTSTATUS NTAPI RtlCreateProcessParametersEx( PRTL_USER_PROCESS_PARAMETERS * pProcessParameters, PUNICODE_STRING ImagePathName, PUNICODE_STRING DllPath, PUNICODE_STRING CurrentDirectory, PUNICODE_STRING CommandLine, PVOID Environment, PUNICODE_STRING WindowTitle, PUNICODE_STRING DesktopInfo, PUNICODE_STRING ShellInfo, PUNICODE_STRING RuntimeData, ULONG Flags ); NTSYSCALLAPI NTSTATUS NTAPI NtCreateUserProcess( _Out_ PHANDLE ProcessHandle, _Out_ PHANDLE ThreadHandle, _In_ ACCESS_MASK ProcessDesiredAccess, _In_ ACCESS_MASK ThreadDesiredAccess, _In_opt_ POBJECT_ATTRIBUTES ProcessObjectAttributes, _In_opt_ POBJECT_ATTRIBUTES ThreadObjectAttributes, _In_ ULONG ProcessFlags, _In_ ULONG ThreadFlags, _In_ PRTL_USER_PROCESS_PARAMETERS ProcessParameters, _Inout_ PPS_CREATE_INFO CreateInfo, _In_ PPS_ATTRIBUTE_LIST AttributeList ); NTSYSAPI PVOID NTAPI RtlAllocateHeap( _In_ PVOID HeapHandle, _In_opt_ ULONG Flags, _In_ SIZE_T Size ); typedef enum _PS_STD_HANDLE_STATE { PsNeverDuplicate, PsRequestDuplicate, / / duplicate standard handles specified by PseudoHandleMask, and only if StdHandleSubsystemType matches the image subsystem PsAlwaysDuplicate, / / always duplicate standard handles PsMaxStdHandleStates } PS_STD_HANDLE_STATE; / / begin_rev PS_STD_HANDLE_INFO Flags #define PS_STD_INPUT_HANDLE 0x1 #define PS_STD_OUTPUT_HANDLE 0x2 #define PS_STD_ERROR_HANDLE 0x4 / / end_rev typedef struct _PS_STD_HANDLE_INFO { union { ULONG Flags; struct { ULONG StdHandleState : 2 ; / / PS_STD_HANDLE_STATE ULONG PseudoHandleMask : 3 ; / / PS_STD_ * } s; }; ULONG StdHandleSubsystemType; } PS_STD_HANDLE_INFO, * PPS_STD_HANDLE_INFO; typedef struct _CLIENT_ID { HANDLE UniqueProcess; HANDLE UniqueThread; } CLIENT_ID, * PCLIENT_ID; typedef struct _SECTION_IMAGE_INFORMATION { PVOID TransferAddress; / / Entry point ULONG ZeroBits; SIZE_T MaximumStackSize; SIZE_T CommittedStackSize; ULONG SubSystemType; union { struct { USHORT SubSystemMinorVersion; USHORT SubSystemMajorVersion; } s1; ULONG SubSystemVersion; } u1; union { struct { USHORT MajorOperatingSystemVersion; USHORT MinorOperatingSystemVersion; } s2; ULONG OperatingSystemVersion; } u2; USHORT ImageCharacteristics; USHORT DllCharacteristics; USHORT Machine; BOOLEAN ImageContainsCode; union { UCHAR ImageFlags; struct { UCHAR ComPlusNativeReady : 1 ; UCHAR ComPlusILOnly : 1 ; UCHAR ImageDynamicallyRelocated : 1 ; UCHAR ImageMappedFlat : 1 ; UCHAR BaseBelow4gb : 1 ; UCHAR ComPlusPrefer32bit : 1 ; UCHAR Reserved : 2 ; } s3; } u3; ULONG LoaderFlags; ULONG ImageFileSize; ULONG CheckSum; } SECTION_IMAGE_INFORMATION, * PSECTION_IMAGE_INFORMATION; typedef struct _PEB_LDR_DATA { ULONG Length; BOOLEAN Initialized; HANDLE SsHandle; LIST_ENTRY InLoadOrderModuleList; LIST_ENTRY InMemoryOrderModuleList; LIST_ENTRY InInitializationOrderModuleList; PVOID EntryInProgress; BOOLEAN ShutdownInProgress; HANDLE ShutdownThreadId; } PEB_LDR_DATA, * PPEB_LDR_DATA; typedef struct _PEB { BOOLEAN InheritedAddressSpace; BOOLEAN ReadImageFileExecOptions; BOOLEAN BeingDebugged; union { BOOLEAN BitField; struct { BOOLEAN ImageUsesLargePages : 1 ; BOOLEAN IsProtectedProcess : 1 ; BOOLEAN IsImageDynamicallyRelocated : 1 ; BOOLEAN SkipPatchingUser32Forwarders : 1 ; BOOLEAN IsPackagedProcess : 1 ; BOOLEAN IsAppContainer : 1 ; BOOLEAN IsProtectedProcessLight : 1 ; BOOLEAN IsLongPathAwareProcess : 1 ; } s1; } u1; HANDLE Mutant; PVOID ImageBaseAddress; PPEB_LDR_DATA Ldr; PRTL_USER_PROCESS_PARAMETERS ProcessParameters; PVOID SubSystemData; PVOID ProcessHeap; } PEB, * PPEB; typedef struct _TEB { NT_TIB NtTib; PVOID EnvironmentPointer; CLIENT_ID ClientId; PVOID ActiveRpcHandle; PVOID ThreadLocalStoragePointer; PPEB ProcessEnvironmentBlock; } TEB, * PTEB; #define NtCurrentPeb() (NtCurrentTeb()->ProcessEnvironmentBlock) #define RtlProcessHeap() (NtCurrentPeb()->ProcessHeap) #ifdef __cplusplus } #endif |
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include <Windows.h> #include "ntdll.h" int main() { / / 创建进程的图像文件的路径以及命令 UNICODE_STRING NtImagePath, Params, ImagePath; RtlInitUnicodeString(&ImagePath, (PWSTR)L "C:\\Windows\\System32\\cmd.exe" ); RtlInitUnicodeString(&NtImagePath, (PWSTR)L "\\??\\C:\\Windows\\System32\\cmd.exe" ); / / RtlInitUnicodeString(&Params, (PWSTR)L "\"C:\\WINDOWS\\SYSTEM32\\cmd.exe\" /k echo Hello world!" ); RtlInitUnicodeString(&Params, (PWSTR)L " /k whoami" ); / / Create the process parameters PRTL_USER_PROCESS_PARAMETERS ProcessParameters = NULL; RtlCreateProcessParametersEx(&ProcessParameters, &ImagePath, NULL, NULL, &Params, NULL, NULL, NULL, NULL, NULL, RTL_USER_PROCESS_PARAMETERS_NORMALIZED); / / 初始化PS_CREATE_INFO结构 PS_CREATE_INFO CreateInfo = { 0 }; CreateInfo.Size = sizeof(CreateInfo); CreateInfo.State = PsCreateInitialState; / / 跳过图像文件执行选项调试器 CreateInfo.InitState.u1.InitFlags = PsSkipIFEODebugger; OBJECT_ATTRIBUTES objAttr = { sizeof(OBJECT_ATTRIBUTES) }; PPS_STD_HANDLE_INFO stdHandleInfo = (PPS_STD_HANDLE_INFO)RtlAllocateHeap(RtlProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PS_STD_HANDLE_INFO)); PCLIENT_ID clientId = (PCLIENT_ID)RtlAllocateHeap(RtlProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PS_ATTRIBUTE)); PSECTION_IMAGE_INFORMATION SecImgInfo = (PSECTION_IMAGE_INFORMATION)RtlAllocateHeap(RtlProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SECTION_IMAGE_INFORMATION)); PPS_ATTRIBUTE_LIST AttributeList = (PS_ATTRIBUTE_LIST * )RtlAllocateHeap(RtlProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PS_ATTRIBUTE_LIST)); / / 设置stdHandle属性 stdHandleInfo - >Flags = PS_STD_OUTPUT_HANDLE; stdHandleInfo - >StdHandleSubsystemType = PsMaxStdHandleStates; / / 创建必要的属性 AttributeList - >TotalLength = sizeof(PS_ATTRIBUTE_LIST); AttributeList - >Attributes[ 0 ].Attribute = PS_ATTRIBUTE_CLIENT_ID; AttributeList - >Attributes[ 0 ].Size = sizeof(CLIENT_ID); AttributeList - >Attributes[ 0 ].ValuePtr = clientId; AttributeList - >Attributes[ 1 ].Attribute = PS_ATTRIBUTE_IMAGE_INFO; AttributeList - >Attributes[ 1 ].Size = sizeof(SECTION_IMAGE_INFORMATION); AttributeList - >Attributes[ 1 ].ValuePtr = SecImgInfo; AttributeList - >Attributes[ 2 ].Attribute = PS_ATTRIBUTE_IMAGE_NAME; AttributeList - >Attributes[ 2 ].Size = NtImagePath.Length; AttributeList - >Attributes[ 2 ].ValuePtr = NtImagePath. Buffer ; AttributeList - >Attributes[ 3 ].Attribute = PS_ATTRIBUTE_STD_HANDLE_INFO; AttributeList - >Attributes[ 3 ].Size = sizeof(PS_STD_HANDLE_INFO); AttributeList - >Attributes[ 3 ].ValuePtr = stdHandleInfo; DWORD64 policy = PROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON; / / 添加进程缓解属性 AttributeList - >Attributes[ 4 ].Attribute = PS_ATTRIBUTE_MITIGATION_OPTIONS; AttributeList - >Attributes[ 4 ].Size = sizeof(DWORD64); AttributeList - >Attributes[ 4 ].ValuePtr = &policy; / / 欺骗父进程 ID 为explorer.exe DWORD trayPID; HWND trayWnd = FindWindowW(L "Shell_TrayWnd" , NULL); GetWindowThreadProcessId(trayWnd, &trayPID); HANDLE hParent = OpenProcess(PROCESS_ALL_ACCESS, false, trayPID); if (hParent) { AttributeList - >Attributes[ 5 ].Attribute = PS_ATTRIBUTE_PARENT_PROCESS; AttributeList - >Attributes[ 5 ].Size = sizeof(HANDLE); AttributeList - >Attributes[ 5 ].ValuePtr = hParent; } else { AttributeList - >TotalLength - = sizeof(PS_ATTRIBUTE); } / / Create the process HANDLE hProcess = NULL, hThread = NULL; NtCreateUserProcess(&hProcess, &hThread, MAXIMUM_ALLOWED, MAXIMUM_ALLOWED, &objAttr, &objAttr, 0 , 0 , ProcessParameters, &CreateInfo, AttributeList); / / Clean up if (hParent) CloseHandle(hParent); RtlFreeHeap(RtlProcessHeap(), 0 , AttributeList); RtlFreeHeap(RtlProcessHeap(), 0 , stdHandleInfo); RtlFreeHeap(RtlProcessHeap(), 0 , clientId); RtlFreeHeap(RtlProcessHeap(), 0 , SecImgInfo); RtlDestroyProcessParameters(ProcessParameters); } |
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!
赞赏
他的文章
看原图
赞赏
雪币:
留言: