• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            tqsheng

            go.....
            隨筆 - 366, 文章 - 18, 評論 - 101, 引用 - 0
            數(shù)據(jù)加載中……

            TLS callback科普小知識

            TLS callback科普小知識
            2008年04月29日 星期二 下午 01:47
            前言:最初想了解TLS callback是來自于team509翻譯的一篇關于TLS callback反ida的文章。當時問了幾個朋友對TLS callback都不太了解,但是問到dummy牛,他給了一個他曾經(jīng)寫過的使用TLS callback anti-debugger的殼,于是就看了下TLS callback機制。如果你是新手,并對TLS callback機制感興趣,那么這篇基礎文章也許對你有用。不過,了解的就勿看了,沒有什么東西 :)。另外,文章沒有進行排版修改.....= =|||

            ======================我是邪惡的分割線=========================

            每個線程擁有自己的線程局部存儲,互補干擾。系統(tǒng)中線程局部存儲是存放在線程的TEB中,每個線程都有自己的TEB因此互相獨立。見下面的TEB結構中的ThreadLocalStoragePointer、TlsSlots、TlsLinksTlsExpansionSlots域。對TLS的訪問通過 TlsAlloc、TlsSetValue和TlsGetValue以及TlsFree幾個API進行。這些API也是對TEB中Tls相關域的訪問。跟蹤Tls*等API函數(shù)發(fā)現(xiàn),系統(tǒng)通過PEB中的TlsBitmap來保存Tls的使用記錄,并據(jù)此分配Tls索引,另外PEB還有 TlsExpansionCounter和TlsBitmapBits來跟蹤Tls的使用情況。

            選擇子fs所對應的段用來存儲TEB和PEB等信息,由ring3下FS選擇子如下:
            selector 3B, DATA32, BaseAddress 7FFDF000, Limit 00000FFF, DPL 3, Present, RW
            因此在ring3下是可讀寫的。fs:[0]指向_NT_TIB中的ExceptionList,fs:[18]指向_NT_TIB自身,也就是指向_TEB結構;
            mov eax, dword ptr fs:[18]
            mov esi, dword ptr [eax+30]
            此時esi指向PEB,dword ptr[esi+40]即為TlsBitmap,假設ecx為分配的索引則

            mov eax, dword ptr fs:[18]
            mov [eax+ecx*4+e10], data

            mov eax, dword ptr fs:[18]
            mov data, [eax+ecx*4+e10]

            結構如下所示(windows XP professional SP2)

                kd> dt !_NT_TIB
                +0×000 ExceptionList : Ptr32 _EXCEPTION_REGISTRATION_RECORD
                +0×004 StackBase : Ptr32 Void
                +0×008 StackLimit : Ptr32 Void
                +0×00c SubSystemTib : Ptr32 Void
                +0×010 FiberData : Ptr32 Void
                +0×010 Version : Uint4B
                +0×014 ArbitraryUserPointer : Ptr32 Void
                +0×018 Self : Ptr32 _NT_TIB
                //SEH和棧信息

                kd> dt !_TEB
                +0×000 NtTib : _NT_TIB
                +0×01c EnvironmentPointer : Ptr32 Void
                +0×020 ClientId : _CLIENT_ID
                +0×028 ActiveRpcHandle : Ptr32 Void
                +0×02c ThreadLocalStoragePointer : Ptr32 Void
                +0×030 ProcessEnvironmentBlock : Ptr32 _PEB
                +0×034 LastErrorValue : Uint4B
                +0×038 CountOfOwnedCriticalSections : Uint4B
                +0×03c CsrClientThread : Ptr32 Void
                +0×040 Win32ThreadInfo : Ptr32 Void
                +0×044 User32Reserved : [26] Uint4B
                +0×0ac UserReserved : [5] Uint4B
                +0×0c0 WOW32Reserved : Ptr32 Void
                +0×0c4 CurrentLocale : Uint4B
                +0×0c8 FpSoftwareStatusRegister : Uint4B
                +0×0cc SystemReserved1 : [54] Ptr32 Void
                +0×1a4 ExceptionCode : Int4B
                +0×1a8 ActivationContextStack : _ACTIVATION_CONTEXT_STACK
                +0×1bc SpareBytes1 : [24] UChar
                +0×1d4 GdiTebBatch : _GDI_TEB_BATCH
                +0×6b4 RealClientId : _CLIENT_ID
                +0×6bc GdiCachedProcessHandle : Ptr32 Void
                +0×6c0 GdiClientPID : Uint4B
                +0×6c4 GdiClientTID : Uint4B
                +0×6c8 GdiThreadLocalInfo : Ptr32 Void
                +0×6cc Win32ClientInfo : [62] Uint4B
                +0×7c4 glDispatchTable : [233] Ptr32 Void
                +0xb68 glReserved1 : [29] Uint4B
                +0xbdc glReserved2 : Ptr32 Void
                +0xbe0 glSectionInfo : Ptr32 Void
                +0xbe4 glSection : Ptr32 Void
                +0xbe8 glTable : Ptr32 Void
                +0xbec glCurrentRC : Ptr32 Void
                +0xbf0 glContext : Ptr32 Void
                +0xbf4 LastStatusValue : Uint4B
                +0xbf8 StaticUnicodeString : _UNICODE_STRING
                +0xc00 StaticUnicodeBuffer : [261] Uint2B
                +0xe0c DeallocationStack : Ptr32 Void
                +0xe10 TlsSlots : [64] Ptr32 Void
                +0xf10 TlsLinks : _LIST_ENTRY
                +0xf18 Vdm : Ptr32 Void
                +0xf1c ReservedForNtRpc : Ptr32 Void
                +0xf20 DbgSsReserved : [2] Ptr32 Void
                +0xf28 HardErrorsAreDisabled : Uint4B
                +0xf2c Instrumentation : [16] Ptr32 Void
                +0xf6c WinSockData : Ptr32 Void
                +0xf70 GdiBatchCount : Uint4B
                +0xf74 InDbgPrint : UChar
                +0xf75 FreeStackOnTermination : UChar
                +0xf76 HasFiberData : UChar
                +0xf77 IdealProcessor : UChar
                +0xf78 Spare3 : Uint4B
                +0xf7c ReservedForPerf : Ptr32 Void
                +0xf80 ReservedForOle : Ptr32 Void
                +0xf84 WaitingOnLoaderLock : Uint4B
                +0xf88 Wx86Thread : _Wx86ThreadState
                +0xf94 TlsExpansionSlots : Ptr32 Ptr32 Void
                +0xf98 ImpersonationLocale : Uint4B
                +0xf9c IsImpersonating : Uint4B
                +0xfa0 NlsCache : Ptr32 Void
                +0xfa4 pShimData : Ptr32 Void
                +0xfa8 HeapVirtualAffinity : Uint4B
                +0xfac CurrentTransactionHandle : Ptr32 Void
                +0xfb0 ActiveFrame : Ptr32 _TEB_ACTIVE_FRAME
                +0xfb4 SafeThunkCall : UChar
                +0xfb5 BooleanSpare : [3] UChar

                kd> dt !_PEB
                +0×000 InheritedAddressSpace : UChar
                +0×001 ReadImageFileExecOptions : UChar
                +0×002 BeingDebugged : UChar
                +0×003 SpareBool : UChar
                +0×004 Mutant : Ptr32 Void
                +0×008 ImageBaseAddress : Ptr32 Void
                +0×00c Ldr : Ptr32 _PEB_LDR_DATA
                +0×010 ProcessParameters : Ptr32 _RTL_USER_PROCESS_PARAMETERS
                +0×014 SubSystemData : Ptr32 Void
                +0×018 ProcessHeap : Ptr32 Void
                +0×01c FastPebLock : Ptr32 _RTL_CRITICAL_SECTION
                +0×020 FastPebLockRoutine : Ptr32 Void
                +0×024 FastPebUnlockRoutine : Ptr32 Void
                +0×028 EnvironmentUpdateCount : Uint4B
                +0×02c KernelCallbackTable : Ptr32 Void
                +0×030 SystemReserved : [1] Uint4B
                +0×034 AtlThunkSListPtr32 : Uint4B
                +0×038 FreeList : Ptr32 _PEB_FREE_BLOCK
                +0×03c TlsExpansionCounter : Uint4B
                +0×040 TlsBitmap : Ptr32 Void
                +0×044 TlsBitmapBits : [2] Uint4B
                +0×04c ReadOnlySharedMemoryBase : Ptr32 Void
                +0×050 ReadOnlySharedMemoryHeap : Ptr32 Void
                +0×054 ReadOnlyStaticServerData : Ptr32 Ptr32 Void
                +0×058 AnsiCodePageData : Ptr32 Void
                +0×05c OemCodePageData : Ptr32 Void
                +0×060 UnicodeCaseTableData : Ptr32 Void
                +0×064 NumberOfProcessors : Uint4B
                +0×068 NtGlobalFlag : Uint4B
                +0×070 CriticalSectionTimeout : _LARGE_INTEGER
                +0×078 HeapSegmentReserve : Uint4B
                +0×07c HeapSegmentCommit : Uint4B
                +0×080 HeapDeCommitTotalFreeThreshold : Uint4B
                +0×084 HeapDeCommitFreeBlockThreshold : Uint4B
                +0×088 NumberOfHeaps : Uint4B
                +0×08c MaximumNumberOfHeaps : Uint4B
                +0×090 ProcessHeaps : Ptr32 Ptr32 Void
                +0×094 GdiSharedHandleTable : Ptr32 Void
                +0×098 ProcessStarterHelper : Ptr32 Void
                +0×09c GdiDCAttributeList : Uint4B
                +0×0a0 LoaderLock : Ptr32 Void
                +0×0a4 OSMajorVersion : Uint4B
                +0×0a8 OSMinorVersion : Uint4B
                +0×0ac OSBuildNumber : Uint2B
                +0×0ae OSCSDVersion : Uint2B
                +0×0b0 OSPlatformId : Uint4B
                +0×0b4 ImageSubsystem : Uint4B
                +0×0b8 ImageSubsystemMajorVersion : Uint4B
                +0×0bc ImageSubsystemMinorVersion : Uint4B
                +0×0c0 ImageProcessAffinityMask : Uint4B
                +0×0c4 GdiHandleBuffer : [34] Uint4B
                +0×14c PostProcessInitRoutine : Ptr32
                +0×150 TlsExpansionBitmap : Ptr32 Void
                +0×154 TlsExpansionBitmapBits : [32] Uint4B
                +0×1d4 SessionId : Uint4B
                +0×1d8 AppCompatFlags : _ULARGE_INTEGER
                +0×1e0 AppCompatFlagsUser : _ULARGE_INTEGER
                +0×1e8 pShimData : Ptr32 Void
                +0×1ec AppCompatInfo : Ptr32 Void
                +0×1f0 CSDVersion : _UNICODE_STRING
                +0×1f8 ActivationContextData : Ptr32 Void
                +0×1fc ProcessAssemblyStorageMap : Ptr32 Void
                +0×200 SystemDefaultActivationContextData : Ptr32 Void
                +0×204 SystemAssemblyStorageMap : Ptr32 Void
                +0×208 MinimumStackCommit : Uint4B

            PE和編譯器支持:

                PE文件結構的目錄表(directory table)中有一個是IMAGE_DIRECTORY_ENTRY_TLS (9)線程級局部存儲目錄,如果此處非零的話,PE將會有一個tls節(jié),tls節(jié)以結構IMAGE_TLS_DIRECTORY(winnt.h)開始
                typedef struct _IMAGE_TLS_DIRECTORY32 {
                DWORD StartAddressOfRawData;
                DWORD EndAddressOfRawData;
                PDWORD AddressOfIndex;
                PIMAGE_TLS_CALLBACK *AddressOfCallBacks;
                DWORD SizeOfZeroFill;
                DWORD Characteristics;
                } IMAGE_TLS_DIRECTORY32;
                其中PIMAGE_TLS_CALLBACK如下:
                typedef VOID
                (NTAPI *PIMAGE_TLS_CALLBACK) (
                PVOID DllHandle,
                DWORD Reason,
                PVOID Reserved
                );
                為了方便使用TLS,編譯器支持用__declspec(thread)來修飾全局或靜態(tài)變量,這樣的變量最終存儲在TLS中,對這些變量的訪問與普通變量一樣,編譯器負責處理其中細節(jié)分配索引和初始化等。
                IMAGE_TLS_DIRECTORY32 中的StartAddressOfRawData和EndAddressOfRawData指向的區(qū)域保存__declspec(thread)變量的初始值,AddressOfIndex指向的區(qū)域保存動態(tài)分配的Tls索引。AddressOfCallBacks指向一個回調(diào)函數(shù),此函數(shù)與 DllMain有同樣的形式,它會在線程創(chuàng)建前被調(diào)用,多用來完成__declspec(thread)變量的初始化等,也被用于anti- debugger(如blacklight)。
                Visual Studio C++中對此的支持請參考Thread Local Storage - The C++ Way。
                MASM中可以將tls節(jié)中的數(shù)據(jù)放到data節(jié)中,然后使用LordPE等工具改變PE中目錄表中的IMAGE_DIRECTORY_ENTRY_TLS為對應的地址和大小即可,具體請見
                TLS-CallBack +IsDebuggerPresent() Debugger Detection。

            參考文獻
            MSDN
            Thread Local Storage: http://www.windowsitlibrary.com/Content/356/11/5.html
            Thread Local Storage - The C++ Way:http://www.codeproject.com/threads/tls.asp
            TLS-CallBack +IsDebuggerPresent() Debugger Detection:
            http://www.openrce.org/reference_library/anti_reversing_view/26/TLS-CallBack%20+IsDebuggerPresent()%20Debugger




            http://hi.baidu.com/ayarei/blog/item/1cfc9d0262c5900a4bfb511f.html

            posted on 2008-11-04 21:40 tqsheng 閱讀(549) 評論(0)  編輯 收藏 引用

            亚洲中文字幕无码久久综合网| 精品亚洲综合久久中文字幕| 久久国产精品久久国产精品| 久久这里只有精品18| 久久综合噜噜激激的五月天| 日本欧美久久久久免费播放网| 久久久久免费看成人影片| 久久96国产精品久久久| 激情综合色综合久久综合| 久久久精品久久久久久| 久久成人小视频| 精品久久久久香蕉网| 精品人妻伦一二三区久久| 热综合一本伊人久久精品| 无码人妻久久一区二区三区免费丨 | 久久精品毛片免费观看| 久久精品国产亚洲AV高清热| 国内精品久久久久| 一本久久免费视频| 老司机国内精品久久久久| 欧美久久一级内射wwwwww.| 久久久无码精品亚洲日韩蜜臀浪潮| 99久久国产综合精品网成人影院| 久久精品人妻中文系列| 99久久精品免费看国产一区二区三区| 国产69精品久久久久观看软件| 99久久国产综合精品麻豆| 亚洲人AV永久一区二区三区久久| 97久久超碰国产精品2021| 久久天天躁狠狠躁夜夜avapp | 青青草原综合久久| 久久久久久久久波多野高潮| 国产精品女同一区二区久久| 97久久精品人妻人人搡人人玩| 久久人妻AV中文字幕| 伊人久久无码精品中文字幕| 婷婷久久综合九色综合98| 国产精品一区二区久久不卡| 99久久精品免费看国产一区二区三区 | 国产精品欧美久久久久天天影视| 久久婷婷五月综合97色直播|