blabberer
September 23rd, 2011, 03:00
if the thread function didnt have a return it wouldnt compile
if it had a return it would return back to the callee
and the callee is not any code in main but system code
the flow of CreateThread Is like this
your code calls CreateThread which is in kernel32.dll
it is a wrapper to
7c8106f0 e8d7fdffff call kernel32!CreateRemoteThread (7c8104cc)
Code:
0:000> kbn1
# ChildEBP RetAddr Args to Child
00 0013fe30 00401172 00000000 00000000 004011bd kernel32!CreateThread
0:000> ub 00401172 l1
image00400000+0x116d:
0040116d e8d4000000 call image00400000+0x1246 (00401246)
0:000> u 00401246 l1
image00400000+0x1246:
00401246 ff2508204000 jmp dword ptr [image00400000+0x2008 (00402008)]
0:000> dps 00402008 l1
00402008 7c8106d7 kernel32!CreateThread
0:000> tc
kernel32!CreateThread+0x19:
7c8106f0 e8d7fdffff call kernel32!CreateRemoteThread (7c8104cc)
which then calls into kernel (Ring 0) via sysenter using NtcreateThread in ntdll.dll
Code:
0:000> tc 10000
kernel32!CreateThread+0x19:
7c8106f0 e8d7fdffff call kernel32!CreateRemoteThread (7c8104cc)
kernel32!CreateRemoteThread+0xa:
7c8104d6 e8fb1fffff call kernel32!_SEH_prolog (7c8024d6)
kernel32!CreateRemoteThread+0x61:
7c81052b e88cfdffff call kernel32!BaseCreateStack (7c8102bc)
kernel32!BaseCreateStack+0x2f:
7c8102eb ff15c012807c call dword ptr [kernel32!_imp__RtlImageNtHeader (7c8012c0)]
ntdll!RtlpImageNtHeader+0x7:
7c9102e0 e8e6e5ffff call ntdll!_SEH_prolog (7c90e8cb)
ntdll!RtlpImageNtHeader+0x51:
7c91031e e8e3e5ffff call ntdll!_SEH_epilog (7c90e906)
kernel32!BaseCreateStack+0x105:
7c8103a5 ffd3 call ebx
ntdll!NtAllocateVirtualMemory+0xa:
7c90cf78 ff12 call dword ptr [edx]
ntdll!KiFastSystemCall+0x2:
7c90e512 0f34 sysenter
kernel32!BaseCreateStack+0x158:
7c8103f6 ffd3 call ebx
ntdll!NtAllocateVirtualMemory+0xa:
7c90cf78 ff12 call dword ptr [edx]
ntdll!KiFastSystemCall+0x2:
7c90e512 0f34 sysenter
kernel32!BaseCreateStack+0x1a1:
7c810425 ff15c412807c call dword ptr [kernel32!_imp__NtProtectVirtualMemory (7c8012c4)]
ntdll!NtProtectVirtualMemory+0xa:
7c90d6f8 ff12 call dword ptr [edx]
ntdll!KiFastSystemCall+0x2:
7c90e512 0f34 sysenter
kernel32!CreateRemoteThread+0x85:
7c810550 e8eefeffff call kernel32!BaseInitializeContext (7c810443)
kernel32!CreateRemoteThread+0x95:
7c810560 e8338effff call kernel32!BaseFormatObjectAttributes (7c809398)
kernel32!CreateRemoteThread+0xc4:
7c81058f ff155014807c call dword ptr [kernel32!_imp__NtCreateThread (7c801450)]
Breakpoint 0 hit
ntdll!NtCreateThread:
7c90d1ae b835000000 mov eax,35h
0:000> tc
ntdll!NtCreateThread+0xa:
7c90d1b8 ff12 call dword ptr [edx]
0:000> ln edx
(7ffe0300) SharedUserData!SystemCallStub
Exact matches:
SharedUserData!SystemCallStub = <no type information>
0:000> tc
ntdll!KiFastSystemCall+0x2:
7c90e512 0f34 sysenter
there inside the system your ThreadFunc is Called Via Despatch Mechanism
usually depending on the function if it is gui it most probably would be DispatchClientmessage()
Code:
ChildEBP RetAddr Args to Child
0013fe18 7e418816 00401130 0010023e 00000500 user32!InternalCallWinProc+0x28
0013fe80 7e428ea0 00000000 00401130 0010023e user32!UserCallWinProcCheckWow+0x150
0013fed4 7e428eec 006687e8 00000500 00000000 user32!DispatchClientMessage+0xa3
0013fefc 7c90e473 0013ff0c 00000018 006687e8 user32!__fnDWORD+0x24
0013ff20 7e4191be 7e42776b 0013ff60 00000000 ntdll!KiUserCallbackDispatcher+0x13
0013ff48 00401111 0013ff60 00000000 00000000 user32!NtUserGetMessage+0xc
WARNING: Stack unwind information not available. Following frames may be wrong.
0013ffac 0040102b 00400000 00000000 00161ee0 image00400000+0x1111
0013fff0 00000000 00401000 00000000 78746341 image00400000+0x102b
and once your threadfunction ends with a return it will go back to system via some callbackreturns
Code:
0:000> tc
user32!UserCallWinProcCheckWow+0x109:
7e41881d e81f000000 call user32!UserCallWinProcCheckWow+0x157 (7e418841)
0:000>
user32!UserCallWinProcCheckWow+0x161:
7e41884b e878030000 call user32!_EndUserApiHook (7e418bc8)
0:000>
user32!_EndUserApiHook+0xb:
7e418bd3 ff152013417e call dword ptr [user32!_imp__InterlockedDecrement (7e411320)]
0:000>
user32!UserCallWinProcCheckWow+0x16a:
7e418854 ff153414417e call dword ptr [user32!_imp__RtlDeactivateActivationContextUnsafeFast (7e411434)]
0:000>
user32!UserCallWinProcCheckWow+0x111:
7e418825 e8d6fdffff call user32!_SEH_epilog (7e418600)
0:000>
user32!DispatchClientMessage+0xa9:
7e428ea6 e855f7feff call user32!_SEH_epilog (7e418600)
0:000>
user32!__fnDWORD+0x2f:
7e428ef7 e8a805ffff call user32!XyCallbackReturn (7e4194a4)
0:000>
kernel32!WaitForSingleObjectEx+0xcb:
7c8025f3 e828000000 call kernel32!WaitForSingleObjectEx+0xdb (7c802620)
if you want the thread to wait you use Wait Functions and act upon the event signals
if you dont want the the thread you call Exit thread like you call ExitProcess in your main Code
or simply return back
on simply returning back system will Act as a Garbage Cleaner will call your ThreadExit proc if you have set it up
and then will Terminate the thread and will send the execution into an infinite msgpump loop
and once a pertinent messgae that is meant for your app is recieved the system will pass it to your app
it may be another create thread
a user mode debugger has no way of intercepting the flow that happens inside system so it appears that the user mode debugger is hung