二.seh实现单步自跟踪.
有时如果你对SICE,TRW或者其他调试器显示的信息有所怀疑的话,你可以用seh显示一些信息作为简单的调试手段,当然,单步跟踪的用途远不止于此.首先回忆一下我们以前了解的单步的概念,当EFLAGS的TF位为1的话执行完某条指令后CPU将产生单步异常,与执行软指令int1类似.注意产生单步陷阱后eip已经指向下一条指令.但进入单步异常处理程序后cpu自动清除TF,以便下条指令正常执行.
我们要作的就是在seh例程中继续置TF位为1,以便下一条指令执行完毕后继续产生单步陷阱实现跟踪功能,直到遇到popfd指令为止,当然你也可以随便检测其他指令或者用记数器来终止单步.
下面例子中如果没有单步跟踪eax的最后结果是3,由于有了单步自跟踪,在seh处理例程中我们每中断一次要加1,所以最后的结果是7,呵呵.请看下面的例子.
;-----------------------------------------
;Ex7,演示利用seh单步自跟踪 by Hume,2002
;humewen@21cn.com
;hume.longcity.net
;-----------------------------------------
.586
.model flat, stdcall
option casemap :none ; case sensitive
include hd.h
include mac.h
singlestep_xHandler proto C :DWORD,:DWORD,:DWORD,:DWORD
;;--------------
.data
count dd 0
Msg0 db "Eax=="
DispEAX dd 0,0
;;-----------------------------------------
.CODE
_Start:
assume fs:nothing
push offset singlestep_xHandler
push fs:[0]
mov fs:[0],esp
;------------------
xor eax,eax
pushfd
pushfd
or dword ptr[esp],100h
popfd ;置TF标志进入单步状态
nop ; nop执行完后单步异常引发
inc eax ; eip指向,nop后面的指令,就是这里
inc eax ; 单步执行
inc eax ; normal eax==3,but infact eax==7
popfd
;------------------
add eax,30h ;convert to ASCIIZ
mov DispEAX,eax
invoke MessageBox,0,addr Msg0,ddd("The Eax equal to..."),0
pop fs:[0]
add esp,4
invoke ExitProcess,0
;-----------------------------------------
singlestep_xHandler PROC C pExcept:DWORD,pFrame:DWORD,pContext:DWORD,pDispatch:DWORD
pushad
assume edi:ptr CONTEXT
assume esi:ptr EXCEPTION_RECORD
mov esi,pExcept
mov edi,pContext
test dword ptr[esi+4],1 ;Exception flags test common stuff
jnz @f
test dword ptr[esi+4],6
jnz @f
cmp dword ptr[esi],80000004h ;是否为单步异常标志
jnz @f
inc [edi].regEax
mov ebx,[edi].regEip
cmp byte ptr[ebx],9Dh ;是否是popfd,因为目的是取消
jz @finish_singlestep ;单步状态,所以这时就不应该重置TF
or [edi].regFlag,100h ;否则,重置TF
;每单步中断一次,eax加1
;所以eax最后不等于3,而是
;中断4次后,eax==7
@finish_singlestep:
mov dword ptr[esp+7*4],0 ;eax==0 hehe...
popad ;研究一下pushad指令就明白了
ret
@@:
mov dword ptr[esp+7*4],1 ;eax==1
popad
ret
singlestep_xHandler ENDP
;-----------------------------------------
END _Start
humewen@21cn.com
humewen@263.net
humeasm.yeah.net
[the way of Hume]
2002.1写毕
2002.2 整理
|