{"id":505,"date":"2012-06-08T17:20:14","date_gmt":"2012-06-08T09:20:14","guid":{"rendered":"http:\/\/newbiecoder.0ginr.com\/blog\/?p=505"},"modified":"2012-06-08T17:21:30","modified_gmt":"2012-06-08T09:21:30","slug":"%e4%bd%bf%e7%94%a8-native-api-%e5%88%9b%e5%bb%ba%e8%bf%9b%e7%a8%8b-vista","status":"publish","type":"post","link":"https:\/\/newbiecoder.0ginr.com\/blog\/?p=505","title":{"rendered":"\u4f7f\u7528 Native API \u521b\u5efa\u8fdb\u7a0b (Vista+)"},"content":{"rendered":"<p>Vista+ \u628a\u5f88\u591a\u4e8b\u60c5\u90fd\u632a\u5230\u5185\u6838\u91cc\u9762\u53bb\u4e86\uff0c\u597d\u50cf\u662f\u56e0\u4e3a\u201c\u53d7\u4fdd\u62a4\u7684\u8fdb\u7a0b\u201d\u7684\u539f\u56e0\u3002<\/p>\n<p><code>#include <Windows.h><\/p>\n<p>\/\/<br \/>\n\/\/ \u7528\u5230\u7684\u5404\u79cd\u5b8f\u3001\u7c7b\u578b\u3001\u51fd\u6570\u7684\u58f0\u660e\u3002<br \/>\n\/\/<\/p>\n<p>#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)<br \/>\n#define STATUS_INSUFFICIENT_RESOURCES ((NTSTATUS)0xC000009AL)<\/p>\n<p>typedef __success(return >= 0) LONG NTSTATUS;<br \/>\ntypedef NTSTATUS *PNTSTATUS;<\/p>\n<p>#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)<\/p>\n<p>typedef struct _STRING<br \/>\n{<br \/>\n    USHORT Length;<br \/>\n    USHORT MaximumLength;<br \/>\n    __field_bcount_part_opt(MaximumLength, Length) PCHAR Buffer;<br \/>\n} STRING, *PSTRING, ANSI_STRING, *PANSI_STRING, OEM_STRING, *POEM_STRING;<\/p>\n<p>typedef struct _UNICODE_STRING<br \/>\n{<br \/>\n    USHORT Length;<br \/>\n    USHORT MaximumLength;<br \/>\n    __field_bcount_part(MaximumLength, Length) PWCH Buffer;<br \/>\n} UNICODE_STRING, *PUNICODE_STRING;<\/p>\n<p>#define RTL_CONSTANT_STRING(s) { sizeof(s) - sizeof((s)[0]), sizeof(s), s }<\/p>\n<p>typedef struct _OBJECT_ATTRIBUTES<br \/>\n{<br \/>\n    ULONG Length;<br \/>\n    HANDLE RootDirectory;<br \/>\n    PUNICODE_STRING ObjectName;<br \/>\n    ULONG Attributes;<br \/>\n    PVOID SecurityDescriptor;<br \/>\n    PVOID SecurityQualityOfService;<br \/>\n} OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES;<\/p>\n<p>#define DOS_MAX_COMPONENT_LENGTH 255<br \/>\n#define DOS_MAX_PATH_LENGTH (DOS_MAX_COMPONENT_LENGTH + 5)<\/p>\n<p>typedef struct _CURDIR<br \/>\n{<br \/>\n    UNICODE_STRING DosPath;<br \/>\n    HANDLE Handle;<br \/>\n} CURDIR, *PCURDIR;<\/p>\n<p>#define RTL_USER_PROC_CURDIR_CLOSE 0x00000002<br \/>\n#define RTL_USER_PROC_CURDIR_INHERIT 0x00000003<\/p>\n<p>typedef struct _RTL_DRIVE_LETTER_CURDIR<br \/>\n{<br \/>\n    USHORT Flags;<br \/>\n    USHORT Length;<br \/>\n    ULONG TimeStamp;<br \/>\n    STRING DosPath;<br \/>\n} RTL_DRIVE_LETTER_CURDIR, *PRTL_DRIVE_LETTER_CURDIR;<\/p>\n<p>#define RTL_MAX_DRIVE_LETTERS 32<br \/>\n#define RTL_DRIVE_LETTER_VALID (USHORT)0x0001<\/p>\n<p>typedef struct _RTL_USER_PROCESS_PARAMETERS<br \/>\n{<br \/>\n    ULONG MaximumLength;<br \/>\n    ULONG Length;<\/p>\n<p>    ULONG Flags;<br \/>\n    ULONG DebugFlags;<\/p>\n<p>    HANDLE ConsoleHandle;<br \/>\n    ULONG ConsoleFlags;<br \/>\n    HANDLE StandardInput;<br \/>\n    HANDLE StandardOutput;<br \/>\n    HANDLE StandardError;<\/p>\n<p>    CURDIR CurrentDirectory;<br \/>\n    UNICODE_STRING DllPath;<br \/>\n    UNICODE_STRING ImagePathName;<br \/>\n    UNICODE_STRING CommandLine;<br \/>\n    PVOID Environment;<\/p>\n<p>    ULONG StartingX;<br \/>\n    ULONG StartingY;<br \/>\n    ULONG CountX;<br \/>\n    ULONG CountY;<br \/>\n    ULONG CountCharsX;<br \/>\n    ULONG CountCharsY;<br \/>\n    ULONG FillAttribute;<\/p>\n<p>    ULONG WindowFlags;<br \/>\n    ULONG ShowWindowFlags;<br \/>\n    UNICODE_STRING WindowTitle;<br \/>\n    UNICODE_STRING DesktopInfo;<br \/>\n    UNICODE_STRING ShellInfo;<br \/>\n    UNICODE_STRING RuntimeData;<br \/>\n    RTL_DRIVE_LETTER_CURDIR CurrentDirectories[RTL_MAX_DRIVE_LETTERS];<\/p>\n<p>    ULONG EnvironmentSize;<br \/>\n    ULONG EnvironmentVersion;<br \/>\n} RTL_USER_PROCESS_PARAMETERS, *PRTL_USER_PROCESS_PARAMETERS;<\/p>\n<p>#define RTL_USER_PROC_PARAMS_NORMALIZED 0x00000001<\/p>\n<p>\/\/ begin_rev<br \/>\n#define PS_ATTRIBUTE_NUMBER_MASK 0x0000ffff<br \/>\n#define PS_ATTRIBUTE_THREAD 0x00010000 \/\/ can be used with threads<br \/>\n#define PS_ATTRIBUTE_INPUT 0x00020000 \/\/ input only<br \/>\n#define PS_ATTRIBUTE_UNKNOWN 0x00040000<br \/>\n\/\/ end_rev<\/p>\n<p>\/\/ private<br \/>\ntypedef enum _PS_ATTRIBUTE_NUM<br \/>\n{<br \/>\n    PsAttributeParentProcess, \/\/ in HANDLE<br \/>\n    PsAttributeDebugPort, \/\/ in HANDLE<br \/>\n    PsAttributeToken, \/\/ in HANDLE<br \/>\n    PsAttributeClientId, \/\/ out PCLIENT_ID<br \/>\n    PsAttributeTebAddress, \/\/ out PTEB *<br \/>\n    PsAttributeImageName, \/\/ in PWSTR<br \/>\n    PsAttributeImageInfo, \/\/ out PSECTION_IMAGE_INFORMATION<br \/>\n    PsAttributeMemoryReserve, \/\/ in PPS_MEMORY_RESERVE<br \/>\n    PsAttributePriorityClass, \/\/ in UCHAR<br \/>\n    PsAttributeErrorMode, \/\/ in ULONG<br \/>\n    PsAttributeStdHandleInfo, \/\/ 10, in PPS_STD_HANDLE_INFO<br \/>\n    PsAttributeHandleList, \/\/ in PHANDLE<br \/>\n    PsAttributeGroupAffinity, \/\/ in PGROUP_AFFINITY<br \/>\n    PsAttributePreferredNode, \/\/ in PUSHORT<br \/>\n    PsAttributeIdealProcessor, \/\/ in PPROCESSOR_NUMBER<br \/>\n    PsAttributeUmsThread, \/\/ ? in PUMS_CREATE_THREAD_ATTRIBUTES<br \/>\n    PsAttributeMitigationOptions, \/\/ in UCHAR<br \/>\n    PsAttributeMax<br \/>\n} PS_ATTRIBUTE_NUM;<\/p>\n<p>\/\/ begin_rev<\/p>\n<p>#define PsAttributeValue(Number, Thread, Input, Unknown) \\<br \/>\n    (((Number) & PS_ATTRIBUTE_NUMBER_MASK) | \\<br \/>\n    ((Thread) ? PS_ATTRIBUTE_THREAD : 0) | \\<br \/>\n    ((Input) ? PS_ATTRIBUTE_INPUT : 0) | \\<br \/>\n    ((Unknown) ? PS_ATTRIBUTE_UNKNOWN : 0))<\/p>\n<p>#define PS_ATTRIBUTE_PARENT_PROCESS \\<br \/>\n    PsAttributeValue(PsAttributeParentProcess, FALSE, TRUE, TRUE)<br \/>\n#define PS_ATTRIBUTE_DEBUG_PORT \\<br \/>\n    PsAttributeValue(PsAttributeDebugPort, FALSE, TRUE, TRUE)<br \/>\n#define PS_ATTRIBUTE_TOKEN \\<br \/>\n    PsAttributeValue(PsAttributeToken, FALSE, TRUE, TRUE)<br \/>\n#define PS_ATTRIBUTE_CLIENT_ID \\<br \/>\n    PsAttributeValue(PsAttributeClientId, TRUE, FALSE, FALSE)<br \/>\n#define PS_ATTRIBUTE_TEB_ADDRESS \\<br \/>\n    PsAttributeValue(PsAttributeTebAddress, TRUE, FALSE, FALSE)<br \/>\n#define PS_ATTRIBUTE_IMAGE_NAME \\<br \/>\n    PsAttributeValue(PsAttributeImageName, FALSE, TRUE, FALSE)<br \/>\n#define PS_ATTRIBUTE_IMAGE_INFO \\<br \/>\n    PsAttributeValue(PsAttributeImageInfo, FALSE, FALSE, FALSE)<br \/>\n#define PS_ATTRIBUTE_MEMORY_RESERVE \\<br \/>\n    PsAttributeValue(PsAttributeMemoryReserve, FALSE, TRUE, FALSE)<br \/>\n#define PS_ATTRIBUTE_PRIORITY_CLASS \\<br \/>\n    PsAttributeValue(PsAttributePriorityClass, FALSE, TRUE, FALSE)<br \/>\n#define PS_ATTRIBUTE_ERROR_MODE \\<br \/>\n    PsAttributeValue(PsAttributeErrorMode, FALSE, TRUE, FALSE)<br \/>\n#define PS_ATTRIBUTE_STD_HANDLE_INFO \\<br \/>\n    PsAttributeValue(PsAttributeStdHandleInfo, FALSE, TRUE, FALSE)<br \/>\n#define PS_ATTRIBUTE_HANDLE_LIST \\<br \/>\n    PsAttributeValue(PsAttributeHandleList, FALSE, TRUE, FALSE)<br \/>\n#define PS_ATTRIBUTE_GROUP_AFFINITY \\<br \/>\n    PsAttributeValue(PsAttributeGroupAffinity, TRUE, TRUE, FALSE)<br \/>\n#define PS_ATTRIBUTE_PREFERRED_NODE \\<br \/>\n    PsAttributeValue(PsAttributePreferredNode, FALSE, TRUE, FALSE)<br \/>\n#define PS_ATTRIBUTE_IDEAL_PROCESSOR \\<br \/>\n    PsAttributeValue(PsAttributeIdealProcessor, TRUE, TRUE, FALSE)<br \/>\n#define PS_ATTRIBUTE_MITIGATION_OPTIONS \\<br \/>\n    PsAttributeValue(PsAttributeMitigationOptions, FALSE, TRUE, TRUE)<\/p>\n<p>\/\/ end_rev<\/p>\n<p>\/\/ begin_private<\/p>\n<p>typedef struct _PS_ATTRIBUTE<br \/>\n{<br \/>\n    ULONG Attribute;<br \/>\n    SIZE_T Size;<br \/>\n    union<br \/>\n    {<br \/>\n        ULONG Value;<br \/>\n        PVOID ValuePtr;<br \/>\n    };<br \/>\n    PSIZE_T ReturnLength;<br \/>\n} PS_ATTRIBUTE, *PPS_ATTRIBUTE;<\/p>\n<p>typedef struct _PS_ATTRIBUTE_LIST<br \/>\n{<br \/>\n    SIZE_T TotalLength;<br \/>\n    PS_ATTRIBUTE Attributes[1];<br \/>\n} PS_ATTRIBUTE_LIST, *PPS_ATTRIBUTE_LIST;<\/p>\n<p>typedef struct _PS_MEMORY_RESERVE<br \/>\n{<br \/>\n    PVOID ReserveAddress;<br \/>\n    SIZE_T ReserveSize;<br \/>\n} PS_MEMORY_RESERVE, *PPS_MEMORY_RESERVE;<\/p>\n<p>typedef enum _PS_STD_HANDLE_STATE<br \/>\n{<br \/>\n    PsNeverDuplicate,<br \/>\n    PsRequestDuplicate, \/\/ duplicate standard handles specified by PseudoHandleMask, and only if StdHandleSubsystemType matches the image subsystem<br \/>\n    PsAlwaysDuplicate, \/\/ always duplicate standard handles<br \/>\n    PsMaxStdHandleStates<br \/>\n} PS_STD_HANDLE_STATE;<\/p>\n<p>\/\/ begin_rev<br \/>\n#define PS_STD_INPUT_HANDLE 0x1<br \/>\n#define PS_STD_OUTPUT_HANDLE 0x2<br \/>\n#define PS_STD_ERROR_HANDLE 0x4<br \/>\n\/\/ end_rev<\/p>\n<p>typedef struct _PS_STD_HANDLE_INFO<br \/>\n{<br \/>\n    union<br \/>\n    {<br \/>\n        ULONG Flags;<br \/>\n        struct<br \/>\n        {<br \/>\n            ULONG StdHandleState : 2; \/\/ PS_STD_HANDLE_STATE<br \/>\n            ULONG PseudoHandleMask : 3; \/\/ PS_STD_*<br \/>\n        };<br \/>\n    };<br \/>\n    ULONG StdHandleSubsystemType;<br \/>\n} PS_STD_HANDLE_INFO, *PPS_STD_HANDLE_INFO;<\/p>\n<p>\/\/ windows-internals-book:\"Chapter 5\"<br \/>\ntypedef enum _PS_CREATE_STATE<br \/>\n{<br \/>\n    PsCreateInitialState,<br \/>\n    PsCreateFailOnFileOpen,<br \/>\n    PsCreateFailOnSectionCreate,<br \/>\n    PsCreateFailExeFormat,<br \/>\n    PsCreateFailMachineMismatch,<br \/>\n    PsCreateFailExeName, \/\/ Debugger specified<br \/>\n    PsCreateSuccess,<br \/>\n    PsCreateMaximumStates<br \/>\n} PS_CREATE_STATE;<\/p>\n<p>typedef enum _PS_IFEO_KEY_STATE<br \/>\n{<br \/>\n    PsReadIFEOAllValues,<br \/>\n    PsSkipIFEODebugger,<br \/>\n    PsSkipAllIFEO,<br \/>\n    PsMaxIFEOKeyStates<br \/>\n} PS_IFEO_KEY_STATE, *PPS_IFEO_KEY_STATE;<\/p>\n<p>typedef struct _PS_CREATE_INFO<br \/>\n{<br \/>\n    SIZE_T Size;<br \/>\n    PS_CREATE_STATE State;<br \/>\n    union<br \/>\n    {<br \/>\n        \/\/ PsCreateInitialState<br \/>\n        struct<br \/>\n        {<br \/>\n            union<br \/>\n            {<br \/>\n                ULONG InitFlags;<br \/>\n                struct<br \/>\n                {<br \/>\n                    UCHAR WriteOutputOnExit : 1;<br \/>\n                    UCHAR DetectManifest : 1;<br \/>\n                    UCHAR SpareBits1 : 6;<br \/>\n                    UCHAR IFEOKeyState : 2; \/\/ PS_IFEO_KEY_STATE<br \/>\n                    UCHAR SpareBits2 : 6;<br \/>\n                    USHORT ProhibitedImageCharacteristics : 16;<br \/>\n                };<br \/>\n            };<br \/>\n            ACCESS_MASK AdditionalFileAccess;<br \/>\n        } InitState;<\/p>\n<p>        \/\/ PsCreateFailOnSectionCreate<br \/>\n        struct<br \/>\n        {<br \/>\n            HANDLE FileHandle;<br \/>\n        } FailSection;<\/p>\n<p>        \/\/ PsCreateFailExeName<br \/>\n        struct<br \/>\n        {<br \/>\n            HANDLE IFEOKey;<br \/>\n        } ExeName;<\/p>\n<p>        \/\/ PsCreateSuccess<br \/>\n        struct<br \/>\n        {<br \/>\n            union<br \/>\n            {<br \/>\n                ULONG OutputFlags;<br \/>\n                struct<br \/>\n                {<br \/>\n                    UCHAR ProtectedProcess : 1;<br \/>\n                    UCHAR AddressSpaceOverride : 1;<br \/>\n                    UCHAR DevOverrideEnabled : 1; \/\/ from Image File Execution Options<br \/>\n                    UCHAR ManifestDetected : 1;<br \/>\n                    UCHAR SpareBits1 : 4;<br \/>\n                    UCHAR SpareBits2 : 8;<br \/>\n                    USHORT SpareBits3 : 16;<br \/>\n                };<br \/>\n            };<br \/>\n            HANDLE FileHandle;<br \/>\n            HANDLE SectionHandle;<br \/>\n            ULONGLONG UserProcessParametersNative;<br \/>\n            ULONG UserProcessParametersWow64;<br \/>\n            ULONG CurrentParameterFlags;<br \/>\n            ULONGLONG PebAddressNative;<br \/>\n            ULONG PebAddressWow64;<br \/>\n            ULONGLONG ManifestAddress;<br \/>\n            ULONG ManifestSize;<br \/>\n        } SuccessState;<br \/>\n    };<br \/>\n} PS_CREATE_INFO, *PPS_CREATE_INFO;<\/p>\n<p>extern \"C\"<br \/>\nNTSYSCALLAPI<br \/>\nNTSTATUS<br \/>\nNTAPI<br \/>\nZwCreateUserProcess(<br \/>\n    __out PHANDLE ProcessHandle,<br \/>\n    __out PHANDLE ThreadHandle,<br \/>\n    __in ACCESS_MASK ProcessDesiredAccess,<br \/>\n    __in ACCESS_MASK ThreadDesiredAccess,<br \/>\n    __in_opt POBJECT_ATTRIBUTES ProcessObjectAttributes,<br \/>\n    __in_opt POBJECT_ATTRIBUTES ThreadObjectAttributes,<br \/>\n    __in ULONG ProcessFlags,<br \/>\n    __in ULONG ThreadFlags,<br \/>\n    __in_opt PRTL_USER_PROCESS_PARAMETERS ProcessParameters,<br \/>\n    __inout PPS_CREATE_INFO CreateInfo,<br \/>\n    __in_opt PPS_ATTRIBUTE_LIST AttributeList<br \/>\n    );;<\/p>\n<p>#define IMAGE_PATH L\"\\\\??\\\\C:\\\\Windows\\\\System32\\\\cmd.exe\"<\/p>\n<p>#define PointerAdd(Pointer, Increment, ResultType) ((ResultType)((ULONG_PTR)(Pointer) \\<br \/>\n\t+ (ULONG_PTR)(Increment)))<\/p>\n<p>#define RoundUp(Value, Alignment) (((Value) + (Alignment) - 1) \/ (Alignment) * (Alignment))<\/p>\n<p>\/\/<br \/>\n\/\/ \u521b\u5efa\u7528\u6237\u8fdb\u7a0b\u53c2\u6570\u3002<br \/>\n\/\/<\/p>\n<p>static NTSTATUS PsCreateProcessParameters(<br \/>\n\tOUT PRTL_USER_PROCESS_PARAMETERS *ProcessParameters,<br \/>\n\tIN PUNICODE_STRING ImagePath,<br \/>\n\tIN PUNICODE_STRING CurrentDirectory,<br \/>\n\tIN PWSTR Environment,<br \/>\n\tIN ULONG EnvironmentSize<br \/>\n\t)<br \/>\n{<br \/>\n\tstatic UNICODE_STRING EmptyUnicodeString = RTL_CONSTANT_STRING(L\"\");<br \/>\n\tstatic UNICODE_STRING DefaultDesktop = RTL_CONSTANT_STRING(L\"Winsta0\\\\Default\");<\/p>\n<p>\tPRTL_USER_PROCESS_PARAMETERS LocalProcessParameters;<br \/>\n\tPUNICODE_STRING CommandLine, WindowTitle, DesktopInfo, ShellInfo, RuntimeData, DllPath;<br \/>\n\tULONG ProcessParametersSize;<br \/>\n\tPWSTR CurrentString;<\/p>\n<p>\tCommandLine = ImagePath;<br \/>\n\tDllPath = &EmptyUnicodeString;<br \/>\n\tWindowTitle = &EmptyUnicodeString;<br \/>\n\tDesktopInfo = &DefaultDesktop;<br \/>\n\tShellInfo = &EmptyUnicodeString;<br \/>\n\tRuntimeData = &EmptyUnicodeString;<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u8ba1\u7b97\u7528\u6237\u8fdb\u7a0b\u53c2\u6570\u7684\u5927\u5c0f\u3002<br \/>\n\t\/\/<\/p>\n<p>\tProcessParametersSize = sizeof(RTL_USER_PROCESS_PARAMETERS) +<br \/>\n\t\tRoundUp(ImagePath->Length + sizeof(UNICODE_NULL), sizeof(ULONG)) +<br \/>\n\t\tRoundUp(CommandLine->Length + sizeof(UNICODE_NULL), sizeof(ULONG)) +<br \/>\n\t\tRoundUp(DllPath->MaximumLength, sizeof(ULONG)) +<br \/>\n\t\tRoundUp(WindowTitle->MaximumLength, sizeof(ULONG)) +<br \/>\n\t\tRoundUp(DesktopInfo->MaximumLength, sizeof(ULONG)) +<br \/>\n\t\tRoundUp(ShellInfo->MaximumLength, sizeof(ULONG)) +<br \/>\n\t\tRoundUp(RuntimeData->MaximumLength, sizeof(ULONG)) +<br \/>\n\t\tRoundUp(DOS_MAX_PATH_LENGTH \/ sizeof(CHAR) * sizeof(WCHAR), sizeof(ULONG));<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u4e3a\u7528\u6237\u8fdb\u7a0b\u53c2\u6570\u5206\u914d\u5185\u5b58\u3002<br \/>\n\t\/\/<\/p>\n<p>\tLocalProcessParameters = (PRTL_USER_PROCESS_PARAMETERS)malloc(ProcessParametersSize);<\/p>\n<p>\tif (!LocalProcessParameters) {<br \/>\n\t\treturn STATUS_INSUFFICIENT_RESOURCES;<br \/>\n\t}<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u521d\u59cb\u5316\u7528\u6237\u8fdb\u7a0b\u53c2\u6570\u3002<br \/>\n\t\/\/<\/p>\n<p>\tRtlZeroMemory(LocalProcessParameters, ProcessParametersSize);<\/p>\n<p>\tLocalProcessParameters->Length = LocalProcessParameters->MaximumLength =<br \/>\n\t\tProcessParametersSize;<br \/>\n\tLocalProcessParameters->Flags = RTL_USER_PROC_PARAMS_NORMALIZED;<br \/>\n\tLocalProcessParameters->DebugFlags = 0;<br \/>\n\tLocalProcessParameters->Environment = (PVOID)Environment;<br \/>\n\tLocalProcessParameters->EnvironmentSize = EnvironmentSize;<br \/>\n\tLocalProcessParameters->CurrentDirectory.Handle = 0;<br \/>\n\tLocalProcessParameters->ConsoleFlags = 0;<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u590d\u5236\u5b57\u7b26\u4e32\u3002<br \/>\n\t\/\/<\/p>\n<p>#define PspCopyUnicodeString(BufferPointer, Destination, Source, AllocationSize) \\<br \/>\n\t(Destination)->Buffer = BufferPointer; \\<br \/>\n\t(Destination)->Length = (Source)->Length; \\<br \/>\n\t(Destination)->MaximumLength = AllocationSize; \\<br \/>\n\tRtlCopyMemory(BufferPointer, (PVOID)(Source)->Buffer, Source->Length); \\<br \/>\n\tBufferPointer = PointerAdd(BufferPointer, RoundUp( \\<br \/>\n\t\tAllocationSize, sizeof(ULONG)), PWSTR);<\/p>\n<p>\tCurrentString = (PWCHAR)(LocalProcessParameters + 1);<\/p>\n<p>\tPspCopyUnicodeString(CurrentString, &LocalProcessParameters->ImagePathName,<br \/>\n\t\tImagePath, ImagePath->Length + sizeof(UNICODE_NULL));<\/p>\n<p>\tPspCopyUnicodeString(CurrentString, &LocalProcessParameters->CommandLine,<br \/>\n\t\tCommandLine, CommandLine->Length + sizeof(UNICODE_NULL));<\/p>\n<p>\tPspCopyUnicodeString(CurrentString, &LocalProcessParameters->DllPath,<br \/>\n\t\tDllPath, DllPath->MaximumLength);<\/p>\n<p>\tPspCopyUnicodeString(CurrentString, &LocalProcessParameters->WindowTitle,<br \/>\n\t\tWindowTitle, WindowTitle->MaximumLength);<\/p>\n<p>\tPspCopyUnicodeString(CurrentString, &LocalProcessParameters->DesktopInfo,<br \/>\n\t\tDesktopInfo, DesktopInfo->MaximumLength);<\/p>\n<p>\tPspCopyUnicodeString(CurrentString, &LocalProcessParameters->ShellInfo,<br \/>\n\t\tShellInfo, ShellInfo->MaximumLength);<\/p>\n<p>\tPspCopyUnicodeString(CurrentString, &LocalProcessParameters->RuntimeData,<br \/>\n\t\tRuntimeData, RuntimeData->MaximumLength);<\/p>\n<p>\tPspCopyUnicodeString(CurrentString, &LocalProcessParameters->CurrentDirectory.DosPath,<br \/>\n\t\tCurrentDirectory, DOS_MAX_PATH_LENGTH \/ sizeof(CHAR) * sizeof(WCHAR));<\/p>\n<p>#undef PspCopyUnicodeString<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u8fd4\u56de\u7ed3\u679c\u7ed9\u8c03\u7528\u65b9\u3002<br \/>\n\t\/\/<\/p>\n<p>\t*ProcessParameters = LocalProcessParameters;<\/p>\n<p>\treturn STATUS_SUCCESS;<br \/>\n}<\/p>\n<p>\/\/<br \/>\n\/\/ \u91ca\u653e\u7528\u6237\u8fdb\u7a0b\u53c2\u6570\u3002<br \/>\n\/\/<\/p>\n<p>static VOID PsFreeProcessParameters(<br \/>\n\tIN PRTL_USER_PROCESS_PARAMETERS ProcessParameters<br \/>\n\t)<br \/>\n{<br \/>\n\tfree((PVOID)ProcessParameters);<\/p>\n<p>\treturn;<br \/>\n}<\/p>\n<p>\/\/<br \/>\n\/\/ \u521b\u5efa\u73af\u5883\u53d8\u91cf\u4fe1\u606f\u3002<br \/>\n\/\/<\/p>\n<p>static NTSTATUS PsCreateEnvironment(<br \/>\n\tOUT PWSTR *Environment,<br \/>\n\tOUT ULONG *EnvironmentSize<br \/>\n\t)<br \/>\n{<br \/>\n\tstatic PWSTR LocalEnvironment = L\"\\0\";<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ FIX: \u76ee\u524d\u53ea\u662f\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684\u73af\u5883\u53d8\u91cf\u4fe1\u606f\u3002\u5e94\u6539\u4e3a\u4ece\u6ce8\u518c\u8868\uff08<br \/>\n\t\/\/ Registry\\Machine\\System\\CurrentControlSet\\Control\\Session Manager\\Environment\uff09<br \/>\n\t\/\/ \u67e5\u8be2\u3002<br \/>\n\t\/\/<br \/>\n\t\/\/ \u73af\u5883\u53d8\u91cf\u4fe1\u606f\u4e5f\u53ef\u4ee5\u4ece PEB \u4e2d\u83b7\u53d6\u3002<br \/>\n\t\/\/<\/p>\n<p>\t*Environment = LocalEnvironment;<br \/>\n\t*EnvironmentSize = sizeof(LocalEnvironment);<\/p>\n<p>\treturn STATUS_SUCCESS;<br \/>\n}<\/p>\n<p>\/\/<br \/>\n\/\/ \u91ca\u653e\u73af\u5883\u53d8\u91cf\u4fe1\u606f\u3002<br \/>\n\/\/<\/p>\n<p>static VOID PsFreeEnvironment(<br \/>\n\tIN PWSTR Environment<br \/>\n\t)<br \/>\n{<br \/>\n\t\/\/<br \/>\n\t\/\/ \u56e0\u4e3a\u76ee\u524d\u73af\u5883\u53d8\u91cf\u4fe1\u606f\u662f\u4e00\u4e2a\u9759\u6001\u53d8\u91cf\uff0c\u56e0\u6b64\u4e0d\u9700\u91ca\u653e\u3002<br \/>\n\t\/\/<\/p>\n<p>\tUNREFERENCED_PARAMETER(Environment);<\/p>\n<p>\treturn;<br \/>\n}<\/p>\n<p>\/\/<br \/>\n\/\/ \u521b\u5efa\u8fdb\u7a0b\u3002<br \/>\n\/\/<\/p>\n<p>NTSTATUS PsCreateProcess(<br \/>\n\tOUT HANDLE *ProcessHandle,<br \/>\n\tOUT HANDLE *ThreadHandle,<br \/>\n\tIN PUNICODE_STRING ImagePath<br \/>\n\t)<br \/>\n{<br \/>\n\tstatic UNICODE_STRING CurrentDirectory = RTL_CONSTANT_STRING(L\"C:\\\\\");<\/p>\n<p>\tPRTL_USER_PROCESS_PARAMETERS ProcessParameters;<br \/>\n\tPWSTR Environment;<br \/>\n\tULONG EnvironmentSize;<br \/>\n\tPS_CREATE_INFO CreateInfo = {0};<br \/>\n\tPS_ATTRIBUTE_LIST AttributeList = {0};<br \/>\n\tNTSTATUS Status;<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u521b\u5efa\u73af\u5883\u53d8\u91cf\u3002<br \/>\n\t\/\/<\/p>\n<p>\tStatus = PsCreateEnvironment(&Environment, &EnvironmentSize);<\/p>\n<p>\tif (!NT_SUCCESS(Status)) {<br \/>\n\t\treturn Status;<br \/>\n\t}<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u521b\u5efa\u8fdb\u7a0b\u53c2\u6570\u3002<br \/>\n\t\/\/<\/p>\n<p>\tStatus = PsCreateProcessParameters(&ProcessParameters, ImagePath, &CurrentDirectory,<br \/>\n\t\tEnvironment, EnvironmentSize);<\/p>\n<p>\tif (!NT_SUCCESS(Status)) {<br \/>\n\t\tPsFreeEnvironment(Environment);<\/p>\n<p>\t\treturn Status;<br \/>\n\t}<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u521d\u59cb\u5316 PS_CREATE_INFO \u7ed3\u6784\u4f53\u3002<br \/>\n\t\/\/<\/p>\n<p>\tCreateInfo.Size = sizeof(PS_CREATE_INFO);<br \/>\n\tCreateInfo.State = PsCreateInitialState;<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u5c06\u6587\u4ef6\u540d\u901a\u8fc7 PS_ATTRIBUTE_LIST \u4f20\u7ed9 ZwCreateUserProcess\u3002<br \/>\n\t\/\/<\/p>\n<p>\tAttributeList.TotalLength = sizeof(PS_ATTRIBUTE_LIST);<br \/>\n\tAttributeList.Attributes[0].Attribute = PS_ATTRIBUTE_IMAGE_NAME;<br \/>\n\tAttributeList.Attributes[0].Size = ImagePath->Length;<br \/>\n\tAttributeList.Attributes[0].ValuePtr = (PVOID)ImagePath->Buffer;<\/p>\n<p>\t\/\/<br \/>\n\t\/\/ \u521b\u5efa\u8fdb\u7a0b\u3002<br \/>\n\t\/\/<\/p>\n<p>\tStatus = ZwCreateUserProcess(ProcessHandle, ThreadHandle, MAXIMUM_ALLOWED, MAXIMUM_ALLOWED,<br \/>\n\t\tNULL, NULL, 0, 0, ProcessParameters, &CreateInfo, &AttributeList);<\/p>\n<p>\tPsFreeProcessParameters(ProcessParameters);<br \/>\n\tPsFreeEnvironment(Environment);<\/p>\n<p>\tif (!NT_SUCCESS(Status)) {<br \/>\n\t\treturn Status;<br \/>\n\t}<\/p>\n<p>\treturn Status;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n\tstatic UNICODE_STRING strImagePath = RTL_CONSTANT_STRING(IMAGE_PATH);<\/p>\n<p>\tHANDLE hProcess, hThread;<br \/>\n\tNTSTATUS Status;<\/p>\n<p>\tStatus = PsCreateProcess(&hThread, &hProcess, &strImagePath);<\/p>\n<p>\tif (NT_SUCCESS(Status)) {<br \/>\n\t\tCloseHandle(hThread);<br \/>\n\t\tCloseHandle(hProcess);<br \/>\n\t}<\/p>\n<p>\treturn 0;<br \/>\n}<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vista+ \u628a\u5f88\u591a\u4e8b\u60c5\u90fd\u632a\u5230\u5185\u6838\u91cc\u9762\u53bb\u4e86\uff0c\u597d\u50cf\u662f\u56e0\u4e3a\u201c\u53d7\u4fdd\u62a4\u7684\u8fdb\u7a0b\u201d\u7684\u539f\u56e0\u3002 #include \/\/ \/\/  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-505","post","type-post","status-publish","format-standard","hentry","category-1"],"_links":{"self":[{"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=505"}],"version-history":[{"count":2,"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/505\/revisions"}],"predecessor-version":[{"id":510,"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/505\/revisions\/510"}],"wp:attachment":[{"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/newbiecoder.0ginr.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}