注册表的监控(一) (转)

注册表的监控(一) (转)[@more@]

 

CSDN上的好帖子比较多,但关于注册表 监控方面的似乎少见,于是兄弟便来
补缺了。

 一、windows9X 部分

  目前有关注册表监控的例子大多需要VTOOLSD的支持,在没有VTOOLSD的
情况下,编此类程序就需要一点汇编知识了,本文就没有使用VTOOLSD,本人也不太
喜欢使用它。

  监控注册表实际上就是拦截如下几个系统服务:(具体参数见DDK DOCUMENTS)。
Begin_Hook_table:
  RegOpenKey
  RegCloseKey
  RegCreateKey
  RegDeleteKey
  RegEnumKey
  RegEnumValue
  RegFlushKey
  RegQueryInfoKey
  RegQueryValue
  RegQueryValueEx
  RegSetValue
  RegSetValueEx
  RegRemapPreDefKey
  RegQueryMultipleValues
  RegCreateDynKey
End_Hook_table:

  微软编译器提供了一套接管VMM服务例程的标准, 例如接管RegOpenKey,:

1、首先声明准备接管函数HookRegOpenKey
BeginProc HookRegOpenKey, service, hook_proc, RealRegOpenKey, locked
  ArgVar hkey, Dword
  ArgVar lpszSubKey, DWORD
  ArgVar phkResult, DWORD

  EnterProc

  push dword ptr phkResult ;
  push dword ptr lpszSubKey ;
  push dword ptr hkey ;= invoke RealRegOpenKey,  hkey, lpszSubKey, phkResult
  call [RealRegOpenKey] ;
  ;;  add  esp, 12
 
  LeaveProc
  Return

EndProc HookRegOpenKey

2、用VMM服务Hook_Device_Service来联上我们的函数

  GetVxdServiceOrdinal eax, _RegOpenKey
  mov  esi, OFFSET32 HookRegOpenKey  ; points to the hook procedure to install
  VMMCall Hook_Device_Service
  jc  @F  ;;fail
  mov  RealRegOpenKey, esi  ;for safe
@@:

3、用VMM服务Unhook_Device_Service来卸载我们的函数
  getvxdserviceordinal eax, _RegOpenKey
  mov  esi, OFFSET32 HookRegOpenKey  ; points to the hook procedure to install
  VMMCall Unhook_Device_Service


一、工程文件
  与标准工程没有太多差别,采用C与汇编混合编程, DEF文件略;

#  Requires:
#  VC++ 5.0以上的编译器
#  98ddk
#  VXDWRAPS.CLB (from Beta-3 DDK or newer), 如不用sprintf之类的函数则不需。

DEVICE  = RegMon
OBJS  = devctl.obj regmon.obj hook.obj msg.obj

CVXDFLAGS = -Zdp -Gs -Zp -c -DIS_32 -Zl -DDEBLEVEL=1 -DDEbug
ASM  = ml
AFLAGS = -coff -dbLD_COFF -DIS_32 -nologo -W3 -Zd -c -Cx -DMASM6 -DINITLOG -DDEBLEVEL=1 -DDEBUG
ASMENV = ML

all: $(DEVICE).vxd

regmon.obj: regmon.c
  cl $(CVXDFLAGS) %s

.asm.obj:
  set $(ASMENV)=$(AFLAGS)
  $(ASM) -Fo$*.obj $<

$(DEVICE).sym: $(DEVICE).map
  mapsym -s $(DEVICE).map

$(DEVICE).map: $(DEVICE).vxd

$(DEVICE).vxd: $(OBJS)
  link @</VXD /NOD
/OUT:$(DEVICE).vxd
/MAP:$(DEVICE).map
$(OBJS) vxdwraps.clb

二、hook.asm, 由于较多,只列举几个:
BeginProc HookRegOpenKey, service, hook_proc, RealRegOpenKey, locked
  ArgVar hkey, DWORD
  ArgVar lpszSubKey, DWORD
  ArgVar phkResult, DWORD

  EnterProc
  push dword ptr phkResult
  push dword ptr lpszSubKey
  push dword ptr hkey
  call [RealRegOpenKey]

  LeaveProc
  Return

EndProc HookRegOpenKey

BeginProc HookRegCloseKey, service, hook_proc, RealRegCloseKey, locked
  ArgVar hKey, DWORD

  EnterProc
  push dword ptr hKey
  call [RealRegCloseKey]

  LeaveProc
  Return

EndProc HookRegCloseKey

BeginProc HookRegCreateKey, service, hook_proc, RealRegCreateKey, locked

  jmp [RealRegCreateKey]

EndProc HookRegCreateKey

BeginProc HookRegDeleteKey, service, hook_proc, RealRegDeleteKey, locked

  jmp [RealRegDeleteKey]

EndProc HookRegDeleteKey

 ........
 ........

starthook proc public C uses ebx ecx edx
  getvxdserviceordinal eax, _RegOpenKey
  mov  esi, OFFSET32 HookRegOpenKey  ; points to the hook procedure to install
  VMMCall Hook_Device_Service
  jc  @F  ;;fail
  mov  RealRegOpenKey, esi
@@:
  getvxdserviceordinal eax, _RegCloseKey
  mov  esi, OFFSET32 HookRegCloseKey  ; points to the hook procedure to install
  VMMCall Hook_Device_Service
  jc  @F  ;;fail
  mov  RealRegCloseKey, esi
@@:
  getvxdserviceordinal eax, _RegCreateKey
  mov  esi, OFFSET32 HookRegCreateKey  ; points to the hook procedure to install
  VMMCall Hook_Device_Service
  jc  @F  ;;fail
  mov  RealRegCreateKey, esi

 ..............
 ..............

@@:
  getvxdserviceordinal eax, _RegCreateDynKey
  mov  esi, OFFSET32 HookRegCreateDynKey  ; points to the hook procedure to install
  VMMCall Hook_Device_Service
  jc  @F
  mov  RealRegCreateDynKey, esi
@@:

 ret
starthook endp

stophook proc public C uses ebx ecx edx

  .if RealRegOpenKey != 0
  getvxdserviceordinal eax, _RegOpenKey
  mov  esi, OFFSET32 HookRegOpenKey  ; points to the hook procedure to install
  VMMCall Unhook_Device_Service
  .endif

  .if RealRegCloseKey != 0
  getvxdserviceordinal eax, _RegCloseKey
  mov  esi, OFFSET32 HookRegCloseKey  ; points to the hook procedure to install
  VMMCall Unhook_Device_Service
  .endif

 ...... 
 ......
  ret

stophook endp

三、regmon.c, 部分:

DWORD OnDeviceIoControl(PDIOCPARAMETERS p)
{
  DWORD retc=0;
 
  switch (p->dwIoControlCode)
  {
  case CMD_GET_VERSION: 
  {
  break;
  }
  case CMD_START_HOOK:
  {
  starthook();
  break;
  }
  case CMD_STOP_HOOK:
 {
  stophook();
  break;
 } 
  default:
  break;
  }
  return 0;
}

OnSysDynamicDeviceInit()
{
  return TRUE;
}

OnSysDynamicDeviceExit()
{
  stophook();
  return TRUE;
}

其它文件略。写文章较累,就不多加注释了,希望可以看的懂。
至于NT部分改天补上。


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-990025/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-990025/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值