Tag Archive for 'trap frame'

x64의 Trap Frame과 Register

x64 환경의 Trap Frame에 대한 정보를 찾다가 재미있는 글을 발견

OSR의 NT Insider에 보면 재미있는 글이 올라와 있군요  RCX, RDX, R8-11등의 정보를 믿지 말라는 내용 ㅎㅎ!!

http://www.osronline.com/article.cfm?id=542

[windbg]Find a Trap Frame Manually

Dump가 깨지거나 할 경우 간혹 수동으로 Trap Frame 을 찾아야하는 경우가 있습니다. 이런 경우는 조금 귀찮아지지요. 하지만 Trap Frame은 생각 보다 쉽게 발견할 수 있습니다. 그 이유는 Trap이 발생할 경우 Stack 상에는 항상 KiTrapXX와 같은 Trap Handler와 Trap Handler를 Call 하기위해서 Stack에 Trap Frame 정보가 있기 때문이지요. 

0: kd> dt _KTRAP_FRAME
nt!_KTRAP_FRAME
+0x000 DbgEbp : Uint4B
+0x004 DbgEip : Uint4B
+0x008 DbgArgMark : Uint4B
+0x00c DbgArgPointer : Uint4B
+0x010 TempSegCs : Uint4B
+0x014 TempEsp : Uint4B
+0x018 Dr0 : Uint4B
+0x01c Dr1 : Uint4B
+0x020 Dr2 : Uint4B
+0x024 Dr3 : Uint4B
+0x028 Dr6 : Uint4B
+0x02c Dr7 : Uint4B
+0x030 SegGs : Uint4B
+0×034 SegEs : Uint4B
+0×038 SegDs : Uint4B

+0×03c Edx : Uint4B
+0×040 Ecx : Uint4B
+0×044 Eax : Uint4B
+0×048 PreviousPreviousMode : Uint4B
+0×04c ExceptionList : Ptr32 _EXCEPTION_REGISTRATION_RECORD
+0×050 SegFs : Uint4B
+0×054 Edi : Uint4B
+0×058 Esi : Uint4B
+0×05c Ebx : Uint4B
+0×060 Ebp : Uint4B
+0×064 ErrCode : Uint4B
+0×068 Eip : Uint4B
+0×06c SegCs : Uint4B
+0×070 EFlags : Uint4B
+0×074 HardwareEsp : Uint4B
+0×078 HardwareSegSs : Uint4B
+0×07c V86Es : Uint4B
+0×080 V86Ds : Uint4B
+0×084 V86Fs : Uint4B
+0×088 V86Gs : Uint4B

Trap Frame을 찾을때 가장 중요한건 Ds, ES 값을 보고 찾아 간다는 점입니다. Windows 상에서 두 레지스터의 하위 바이트 값은 항상 0×0023이기 때문이죠.

0: kd> r
eax=ffdff13c ebx=00000000 ecx=00000000 edx=804e331e esi=c038ec48 edi=80701a4c
eip=8053938a esp=ad5139a8 ebp=ad5139c0 iopl=0 nv up ei ng nz na pe nc
cs=0008 ss=0010 ds=0023 es=0023 fs=0030 gs=0000 efl=00000286
nt!KeBugCheckEx+0x1b:
8053938a 5d pop ebp
0: kd> dds ad5139a8 ad5139a8+1000
... 생략
ad513a10 ad513a28
ad513a14 804e2944 nt!KiTrap0E+0xd0
ad513a18 00000000
ad513a1c e3b12000
ad513a20 00000000
ad513a24 ad513a28
ad513a28 ad513aa0
ad513a2c 80512081 nt!wcsncat+0×20
ad513a30 badb0d00
ad513a34 e38c4cda
ad513a38 e3b06800
ad513a3c e3b1c468
ad513a40 00000000
ad513a44 00000000
ad513a48 00000000
ad513a4c 00000000
ad513a50 00000000
ad513a54 00000000
ad513a58 00000000
ad513a5c e2f60023
ad513a60 00000023

ad513a64 e38c4cda
ad513a68 89da5d1e
ad513a6c 89da5c00
ad513a70 00000000
ad513a74 ad513bc4
ad513a78 00010030
ad513a7c 89da5cb0
ad513a80 e3b12000
ad513a84 805613a0 nt!CmpCallBackVector
ad513a88 ad513aa0
ad513a8c 00000000
ad513a90 80512081 nt!wcsncat+0×20
ad513a94 00000008
… 생략

위와 같이 쉽게 스택상에서 Trap Frame 정보가 위치한 곳을 찾을 수 있지요. 실제 _KTRAP_FRAME의 정보를 확인하려면 ad513a60에서 Offset인 0×38 을 뺀 Address를 가지고 _KTRAP_FRAME의 정보를 보면되겠죠 .

0: kd> dt _KTRAP_FRAME ad513a60-0x38
nt!_KTRAP_FRAME
+0x000 DbgEbp : 0xad513aa0
+0x004 DbgEip : 0x80512081
+0x008 DbgArgMark : 0xbadb0d00
+0x00c DbgArgPointer : 0xe38c4cda
+0x010 TempSegCs : 0xe3b06800
+0x014 TempEsp : 0xe3b1c468
+0x018 Dr0 : 0
+0x01c Dr1 : 0
+0x020 Dr2 : 0
+0x024 Dr3 : 0
+0x028 Dr6 : 0
+0x02c Dr7 : 0
+0x030 SegGs : 0
+0x034 SegEs : 0xe2f60023
+0x038 SegDs : 0x23
+0x03c Edx : 0xe38c4cda
+0x040 Ecx : 0x89da5d1e
+0x044 Eax : 0x89da5c00
+0x048 PreviousPreviousMode : 0
+0x04c ExceptionList : 0xad513bc4 _EXCEPTION_REGISTRATION_RECORD
+0x050 SegFs : 0x10030
+0x054 Edi : 0x89da5cb0
+0x058 Esi : 0xe3b12000
+0x05c Ebx : 0x805613a0
+0x060 Ebp : 0xad513aa0
+0x064 ErrCode : 0
+0×068 Eip : 0×80512081
+0×06c SegCs : 8
+0×070 EFlags : 0×10206
+0×074 HardwareEsp : 0xb9c33af6
+0×078 HardwareSegSs : 0xad513bd4
+0×07c V86Es : 0xb9c31db0
+0×080 V86Ds : 0×89da5c00
+0×084 V86Fs : 0xe3b11f90
+0×088 V86Gs : 0×36
0: kd> ub 0x80512081
nt!wcsncat+0x10:
80512071 85d2 test edx,edx
80512073 75f6 jne nt!wcsncat+0xa (8051206b)
80512075 49 dec ecx
80512076 49 dec ecx
80512077 837d1000 cmp dword ptr [ebp+10h],0
8051207b 56 push esi
8051207c 741b je nt!wcsncat+0×38 (80512099)
8051207e 8b750c mov esi,dword ptr [ebp+0Ch]
0: kd> u 0×80512081
nt!wcsncat+0×20:
80512081 668b16 mov dx,word ptr [esi]
80512084 ff4d10 dec dword ptr [ebp+10h]
80512087 668911 mov word ptr [ecx],dx
8051208a 41 inc ecx
8051208b 41 inc ecx
8051208c 46 inc esi
8051208d 46 inc esi
8051208e 6685d2 test dx,dx
수동으로도 멋지게 Trap Frame을 확인할 수 있죠 ??  이 Dump 같은 경우는 ebp+0Ch 의 값 즉 wcsncat의 2번째 Parameter를 확인해 보면 답이 나오겠군요 .