轉自: http://hi.baidu.com/zzzevazzz/item/b2a9c9a4ea6805f615329ba7數據結構
這兩個Vista新增的系統服務函數原型未公開,目前網上搜索到的資料有些問題,特別是對於最後一個參數的描述不確切。函數
首先說NtCreateUserProcess。網上找到的函數原型以下:指針
DWORD newNtCreateUserProcess(PVOID pvP1,PVOID pvP2,PVOID pvP3,PVOID pvP4,PVOID pvP5,PVOID pvP6,PVOID pvP7,PVOID pvP8,PVOID pvP9,PVOID pvP10,PPROCESS_UNKNOWN ppuUnknown)調試
typedef struct {
ULONG Length;
ULONG Unknown1;
ULONG Unknown2;
PWSTR pwsImageFileName;
ULONG Unknown4;
ULONG Unknown5;
ULONG Unknown6;
PCLIENT_ID pcidClient;
ULONG Unknown8;
ULONG Unknown9;
ULONG Unknown10;
ULONG Unknown11;
ULONG Unknown12;
ULONG Unknown13;
ULONG Unknown14;
ULONG Unknown15;
ULONG Unknown16;
} PROCESS_UNKNOWN, *PPROCESS_UNKNOWN; 進程
姑且不論這麼多的Unknown,僅僅是「公開」的pwsImageFileName也不老是吻合。若是掛鉤NtCreateUserProcess並以此來獲取進程全路徑,那麼某些時候(好比註銷從新登錄時)就會失敗。而另外一個pcidClient更是不知所云,真不知道當初第一個公佈的人是怎麼判斷的。惟一始終符合的只有Length成員。 ci
另外一個NtCreateThreadEx因爲有NtCreateThread做參考,因此看起來好一點,但最後一個參數仍然是霧裏看花。 原型
typedef struct {
ULONG Length;
ULONG Unknown1;
ULONG Unknown2;
PULONG Unknown3;
ULONG Unknown4;
ULONG Unknown5;
ULONG Unknown6;
PULONG Unknown7;
ULONG Unknown8;
} UNKNOWN; it
typedef DWORD (WINAPI *NtCreateThreadEx)
(
PHANDLE ThreadHandle,
ACCESS_MASK DesiredAccess,
POBJECT_ATTRIBUTES ObjectAttributes,
HANDLE ProcessHandle,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
BOOL CreateSuspended,
DWORD dwStackSize,
DWORD dw1,
DWORD dw2,
LPVOID Unknown
); ast
又是一堆Unknown,用的時候得像這樣: 兼容性
memset (&Buffer,0,sizeof(UNKNOWN));
Buffer.Length = 36; // sizeof (UNKNOWN)
Buffer.Unknown1 = 0x10003;
Buffer.Unknown2 = 0x8;
Buffer.Unknown3 = &dw1;
Buffer.Unknown4 = 0;
Buffer.Unknown5 = 0x10004;
Buffer.Unknown6 = 4;
Buffer.Unknown7 = &dw0;
Buffer.Unknown8 = 0;
雖然能夠運行,但這樣搞內心老是沒底的。
觀察運行時的實際數據就能發現,兩個函數最後的參數指向的應該是同一種數據結構。
那麼它究竟是什麼?這裏省略調試跟蹤的細節,直接公佈答案。其實它和用戶態的LPPROC_THREAD_ATTRIBUTE_LIST是相似的東西。
隨着Vista建立進程過程變得更復雜,CreateProcess的參數也要跟着增長。爲了保持向前兼容性和擴展性,微軟搞了一個AttributeList,放在StartupInfo的擴展結構STARTUPINFOEX裏。這個AttributeList的大小和成員位置是動態決定的,因此纔出現那兩個Unknown結構「不靠譜」的狀況。
AttributeList的結構也是未公開的,只能用InitializeProcThreadAttributeList和UpdateProcThreadAttribute等API操做。經過逆向工程不可貴到結構的細節。這事有人已經作過了:
struct _ProThreadAttrItem {
DWORD dwFlags;
DWORD cbBufSize;
HANDLE* pHandleBuf;
};
struct _ProcThreadAttrHeader {
DWORD dwMark;
DWORD dwTotalNum;
DWORD dwCurNum;
DWORD dwUnknown;
_ProThreadAttrItem* lpLast;
};
struct _StartupInfoExW {
STARTUPINFOW si;
_ProcThreadAttrHeader* pEx;
};
系統服務用的AttributeList結構略有不一樣,如下是我逆向的結果:
typedef struct _NT_PROC_THREAD_ATTRIBUTE_ENTRY {
ULONG Attribute; // PROC_THREAD_ATTRIBUTE_XXX,參見MSDN中UpdateProcThreadAttribute的說明
SIZE_T Size; // Value的大小
ULONG_PTR Value; // 保存4字節數據(好比一個Handle)或數據指針
ULONG Unknown; // 老是0,多是用來返回數據給調用者
} PROC_THREAD_ATTRIBUTE_ENTRY, *PPROC_THREAD_ATTRIBUTE_ENTRY;
typedef struct _NT_PROC_THREAD_ATTRIBUTE_LIST {
ULONG Length; // 結構總大小
NT_PROC_THREAD_ATTRIBUTE_ENTRY Entry[1];
} NT_PROC_THREAD_ATTRIBUTE_LIST, *PNT_PROC_THREAD_ATTRIBUTE_LIST;
兩個系統服務大部分參數也肯定了:
NTSTATUS NtCreateUserProcess(
OUT PHANDLE ProcessHandle,
OUT PHANDLE ThreadHandle,
IN ACCESS_MASK ProcessDesiredAccess,
IN ACCESS_MASK ThreadDesiredAccess,
IN POBJECT_ATTRIBUTES ProcessObjectAttributes OPTIONAL,
IN POBJECT_ATTRIBUTES ThreadObjectAttributes OPTIONAL,
IN ULONG CreateProcessFlags,
IN ULONG CreateThreadFlags,
IN PRTL_USER_PROCESS_PARAMETERS ProcessParameters,
IN PVOID Parameter9,
IN PNT_PROC_THREAD_ATTRIBUTE_LIST AttributeList
);
NTSTATUS NtCreateThreadEx(
OUT PHANDLE ThreadHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
IN HANDLE ProcessHandle,
IN PTHREAD_START_ROUTINE StartRoutine,
IN PVOID StartContext,
IN ULONG CreateThreadFlags,
IN ULONG ZeroBits OPTIONAL,
IN ULONG StackSize OPTIONAL,
IN ULONG MaximumStackSize OPTIONAL,
IN PNT_PROC_THREAD_ATTRIBUTE_LIST AttributeList
);
CreateProcessFlags和CreateThreadFlags沒有現成的宏定義,只能逆向分析。
下面是含義較明確的經常使用定義。
#define CREATE_PROCESS_BREAKAWAY_FROM_JOB 0x0001
#define CREATE_PROCESS_INHERIT_HANDLES 0x0004
#define CREATE_PROCESS_PROTECTED 0x0040
#define CREATE_THREAD_SUSPENDED 0x0001
Parameter9是一個固定大小的結構,內容含義有待繼續分析。