能力值:
( LV12,RANK:420 )
|
-
-
2 楼
线程创建是NtCreateThread完成的,你可以从其 ClientId参数获取返回的TID,进程ID则可以通过对ProcessHandle参数进行 ZwQueryInformationProcess->ProcessBasicInformation中获取
创建时如果没设置CreateSuspended,则在创建完成就会运行了,否则会需要调用NtResumeThread来运行线程
具体的过程在Windows Internals书中也可以看到
|
能力值:
( LV6,RANK:90 )
|
-
-
3 楼
也来凑凑热闹哈
线程创建基本上做下面这些事情
1)创建ETHREAD内核结构并初始化
2)创建线程号
3)创建TEB并初始化
4)创建系统堆栈并初始化(包含自陷框架,调用框架,线程切换上下文框架)
这些结构初始化好后,调用KeReadyThread使创建线程进入就绪队列,后面就等被调度运行了
更详细的情况,建议你看reactos源码,有问题一起讨论
|
能力值:
( LV2,RANK:10 )
|
-
-
4 楼
如果想HOOK NtCreateThread防止远程线程注入,但仅仅比较CurrentProcess与ParentProcess是否相同不行吧,当一个新进程创建后的第一个线程的创建CurrentProcess与ParentProcess就是不同的,那应该如何实现呢?
|
能力值:
( LV2,RANK:10 )
|
-
-
5 楼
代码我也没看明白。。呵呵。。先谢谢各位了。。
我只是想知道,当NtCreateXXXX这两个函数返回时,进程、线程就会立即开始运行吗(执行线程代码)?还是要等待别的条件就绪后,才会真正地执行代码?
因为我看到ring3的CreateThread在调用完ZwCreateThread后,还调用了“别的函数”,“别的函数”是在为线程的真正运行做准备吗?(如果ZwCreateThread返回时,线程还没动的话。。 )
|
能力值:
( LV2,RANK:10 )
|
-
-
6 楼
您说,没有suspend标志的话,线程在NtcreateThread返回时就会开始运行吗?
可:CreateThread在调用ZwCreateThread后还调用了别的函数。。。以及zwResumeThread,那这个zwResumeThread的作用是什么呢?
谢谢您了
|
能力值:
( LV2,RANK:10 )
|
-
-
7 楼
HANDLE WINAPI CreateRemoteThread ( HANDLE hProcess,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
DWORD dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId
)
Definition at line 100 of file thread.c.
Referenced by ConioConsoleCtrlEventTimeout(), and CreateThread().
00107 {
00108 NTSTATUS Status;
00109 INITIAL_TEB InitialTeb;
00110 CONTEXT Context;
00111 CLIENT_ID ClientId;
00112 OBJECT_ATTRIBUTES LocalObjectAttributes;
00113 POBJECT_ATTRIBUTES ObjectAttributes;
00114 HANDLE hThread;
00115 ULONG Dummy;
00116
00117 DPRINT("CreateRemoteThread: hProcess: %ld dwStackSize: %ld lpStartAddress"
00118 ": %p lpParameter: %lx, dwCreationFlags: %lx\n", hProcess,
00119 dwStackSize, lpStartAddress, lpParameter, dwCreationFlags);
00120
00121 /* Clear the Context */
00122 RtlZeroMemory(&Context, sizeof(CONTEXT));
00123
00124 /* Write PID */
00125 ClientId.UniqueProcess = hProcess;
00126
00127 /* Create the Stack */
00128 Status = BasepCreateStack(hProcess,
00129 dwStackSize,
00130 dwCreationFlags & STACK_SIZE_PARAM_IS_A_RESERVATION ?
00131 dwStackSize : 0,
00132 &InitialTeb);
00133 if(!NT_SUCCESS(Status))
00134 {
00135 SetLastErrorByStatus(Status);
00136 return NULL;
00137 }
00138
00139 /* Create Initial Context */
00140 BasepInitializeContext(&Context,
00141 lpParameter,
00142 lpStartAddress,
00143 InitialTeb.StackBase,
00144 1);
00145
00146 /* initialize the attributes for the thread object */
00147 ObjectAttributes = BasepConvertObjectAttributes(&LocalObjectAttributes,
00148 lpThreadAttributes,
00149 NULL);
00150
00151 /* Create the Kernel Thread Object */
00152 Status = NtCreateThread(&hThread,
00153 THREAD_ALL_ACCESS,
00154 ObjectAttributes,
00155 hProcess,
00156 &ClientId,
00157 &Context,
00158 &InitialTeb,
00159 TRUE);
00160 if(!NT_SUCCESS(Status))
00161 {
00162 BasepFreeStack(hProcess, &InitialTeb);
00163 SetLastErrorByStatus(Status);
00164 return NULL;
00165 }
00166
00167 /* Are we in the same process? */
00168 if (hProcess == NtCurrentProcess())
00169 {
00170 PTEB Teb;
00171 PVOID ActivationContextStack;
00172 THREAD_BASIC_INFORMATION ThreadBasicInfo;
00173 #ifndef SXS_SUPPORT_FIXME
00174 ACTIVATION_CONTEXT_BASIC_INFORMATION ActivationCtxInfo;
00175 ULONG_PTR Cookie;
00176 #endif
00177 ULONG retLen;
00178
00179 /* Get the TEB */
00180 Status = NtQueryInformationThread(hThread,
00181 ThreadBasicInformation,
00182 &ThreadBasicInfo,
00183 sizeof(ThreadBasicInfo),
00184 &retLen);
00185 if (NT_SUCCESS(Status))
00186 {
00187 /* Allocate the Activation Context Stack */
00188 Status = RtlAllocateActivationContextStack(&ActivationContextStack);
00189 }
00190
00191 if (NT_SUCCESS(Status))
00192 {
00193 Teb = ThreadBasicInfo.TebBaseAddress;
00194
00195 /* Save it */
00196 Teb->ActivationContextStackPointer = ActivationContextStack;
00197 #ifndef SXS_SUPPORT_FIXME
00198 /* Query the Context */
00199 Status = RtlQueryInformationActivationContext(1,
00200 0,
00201 NULL,
00202 ActivationContextBasicInformation,
00203 &ActivationCtxInfo,
00204 sizeof(ActivationCtxInfo),
00205 &retLen);
00206 if (NT_SUCCESS(Status))
00207 {
00208 /* Does it need to be activated? */
00209 if (!ActivationCtxInfo.hActCtx)
00210 {
00211 /* Activate it */
00212 Status = RtlActivateActivationContext(1,
00213 ActivationCtxInfo.hActCtx,
00214 &Cookie);
00215 if (!NT_SUCCESS(Status))
00216 DPRINT1("RtlActivateActivationContext failed %x\n", Status);
00217 }
00218 }
00219 else
00220 DPRINT1("RtlQueryInformationActivationContext failed %x\n", Status);
00221 #endif
00222 }
00223 else
00224 DPRINT1("RtlAllocateActivationContextStack failed %x\n", Status);
00225 }
00226
00227 /* Notify CSR */
00228 Status = BasepNotifyCsrOfThread(hThread, &ClientId);
00229 if (!NT_SUCCESS(Status))
00230 {
00231 ASSERT(FALSE);
00232 }
00233
00234 /* Success */
00235 if(lpThreadId) *lpThreadId = (DWORD)ClientId.UniqueThread;
00236
00237 /* Resume it if asked */
00238 if (!(dwCreationFlags & CREATE_SUSPENDED))
00239 {
00240 NtResumeThread(hThread, &Dummy);
00241 }
00242
00243 /* Return handle to thread */
00244 return hThread;
00245 }
地址:http://doxygen.reactos.org/da/d56/dll_2win32_2kernel32_2thread_2thread_8c_ab2f210336c32a96543dbd7c55e527dc0.html#ab2f210336c32a96543dbd7c55e527dc0
|