windows – 如何使用写入地址捕获内存写入和调用函数

我想捕获特定内存范围的内存写入,并使用写入的内存位置的地址调用函数.优选地,在已经发生对存储器的写入之后.

我知道这可以通过操作系统来填充页表条目来完成.但是,如何在想要这样做的应用程序中完成类似的操作呢?

好吧,你可以这样做:
// compile with Open Watcom 1.9: wcl386 wrtrap.c

#include <windows.h>
#include <stdio.h>

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif


UINT_PTR RangeStart = 0;
SIZE_T RangeSize = 0;

UINT_PTR AlignedRangeStart = 0;
SIZE_T AlignedRangeSize = 0;


void MonitorRange(void* Start,size_t Size)
{
  DWORD dummy;

  if (Start &&
      Size &&
      (AlignedRangeStart == 0) &&
      (AlignedRangeSize == 0))
  {
    RangeStart = (UINT_PTR)Start;
    RangeSize = Size;

    // Page-align the range address and size

    AlignedRangeStart = RangeStart & ~(UINT_PTR)(PAGE_SIZE - 1);

    AlignedRangeSize = ((RangeStart + RangeSize - 1 + PAGE_SIZE) &
                        ~(UINT_PTR)(PAGE_SIZE - 1)) -
                       AlignedRangeStart;

    // Make the page range read-only
    VirtualProtect((LPVOID)AlignedRangeStart,AlignedRangeSize,PAGE_READONLY,&dummy);
  }
  else if (((Start == NULL) || (Size == 0)) &&
           AlignedRangeStart &&
           AlignedRangeSize)
  {
    // Restore the original setting
    // Make the page range read-write
    VirtualProtect((LPVOID)AlignedRangeStart,PAGE_READWRITE,&dummy);

    RangeStart = 0;
    RangeSize = 0;

    AlignedRangeStart = 0;
    AlignedRangeSize = 0;
  }
}

// This is where the magic happens...
int ExceptionFilter(LPEXCEPTION_POINTERS pEp,void (*pMonitorFxn)(LPEXCEPTION_POINTERS,void*))
{
  CONTEXT* ctx = pEp->ContextRecord;
  ULONG_PTR* info = pEp->ExceptionRecord->Exceptioninformation;
  UINT_PTR addr = info[1];
  DWORD dummy;

  switch (pEp->ExceptionRecord->ExceptionCode)
  {
  case STATUS_ACCESS_VIOLATION:
    // If it's a write to read-only memory,// to the pages that we made read-only...
    if ((info[0] == 1) &&
        (addr >= AlignedRangeStart) &&
        (addr < AlignedRangeStart + AlignedRangeSize))
    {
      // Restore the original setting
      // Make the page range read-write
      VirtualProtect((LPVOID)AlignedRangeStart,&dummy);

      // If the write is exactly within the requested range,// call our monitoring callback function
      if ((addr >= RangeStart) && (addr < RangeStart + RangeSize))
      {
        pMonitorFxn(pEp,(void*)addr);
      }

      // Set FLAGS.TF to trigger a single-step trap after the
      // next instruction,which is the instruction that has caused
      // this page fault (AKA access violation)
      ctx->EFlags |= (1 << 8);

      // Execute the faulted instruction again
      return EXCEPTION_CONTINUE_EXECUTION;
    }

    // Don't handle other AVs
    goto ContinueSearch;

  case STATUS_SINGLE_STEP:
    // The instruction that caused the page fault
    // has Now succeeded writing to memory.
    // Make the page range read-only again
    VirtualProtect((LPVOID)AlignedRangeStart,&dummy);

    // Continue executing as usual until the next page fault
    return EXCEPTION_CONTINUE_EXECUTION;

  default:
  ContinueSearch:
    // Don't handle other exceptions
    return EXCEPTION_CONTINUE_SEARCH;
  }
}


// We'll monitor writes to blah[1].
// volatile is to ensure the memory writes aren't
// optimized away by the compiler.
volatile int blah[3] = { 3,2,1 };

void WritetoMonitoredMemory(void)
{
  blah[0] = 5;
  blah[0] = 6;
  blah[0] = 7;
  blah[0] = 8;

  blah[1] = 1;
  blah[1] = 2;
  blah[1] = 3;
  blah[1] = 4;

  blah[2] = 10;
  blah[2] = 20;
  blah[2] = 30;
  blah[2] = 40;
}

// This pointer is an attempt to ensure that the function's code isn't
// inlined. We want to see it's this function's code that modifies the
// monitored memory.
void (* volatile pWritetoMonitoredMemory)(void) = &WritetoMonitoredMemory;

void WriteMonitor(LPEXCEPTION_POINTERS pEp,void* Mem)
{
  printf("We're about to write to 0x%X from EIP=0x%X...\n",Mem,pEp->ContextRecord->Eip);
}

int main(void)
{
  printf("&WritetoMonitoredMemory() = 0x%X\n",pWritetoMonitoredMemory);
  printf("&blah[1] = 0x%X\n",&blah[1]);

  printf("\nstart\n\n");

  __try
  {
    printf("blah[0] = %d\n",blah[0]);
    printf("blah[1] = %d\n",blah[1]);
    printf("blah[2] = %d\n",blah[2]);

    // Start monitoring memory writes
    MonitorRange((void*)&blah[1],sizeof(blah[1]));

    // Write to monitored memory
    pWritetoMonitoredMemory();

    // Stop monitoring memory writes
    MonitorRange(NULL,0);

    printf("blah[0] = %d\n",blah[2]);
  }
  __except(ExceptionFilter(GetExceptioninformation(),&WriteMonitor)) // write monitor callback function
  {
    // never executed
  }

  printf("\nstop\n");
  return 0;
}

输出(在Windows XP上运行):

&WritetoMonitoredMemory() = 0x401179
&blah[1] = 0x4080DC

start

blah[0] = 3
blah[1] = 2
blah[2] = 1
We're about to write to 0x4080DC from EIP=0x4011AB...
We're about to write to 0x4080DC from EIP=0x4011B5...
We're about to write to 0x4080DC from EIP=0x4011BF...
We're about to write to 0x4080DC from EIP=0x4011C9...
blah[0] = 8
blah[1] = 4
blah[2] = 40

stop

这就是主意.

您可能需要更改内容以使代码在多个线程中正常工作,使其与其他SEH代码(如果有)一起使用,具有C异常(如果适用).

当然,如果你真的想要它,你可以在写完成后调用write监视回调函数.为此,您需要在某处保存STATUS_ACCESS_VIOLATION案例中的内存地址(TLS?),以便STATUS_SINGLE_STEP案例可以在以后获取并传递给该函数.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Windows注册表操作基础代码 Windows下对注册表进行操作使用的一段基础代码Reg.h:#pragmaonce#include&lt;assert.h&gt;#include&lt;windows.h&gt;classReg{HKEYhkey;public:voidopen(HKEYroot
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的内容,在Windows环境下的黑客必须熟练掌握底层API编程。为了使读者对黑客常用的Windows API有个更全面的了解以及方便日后使用API方法的查询,特将这些常用的API按照7大分类进行整理如下,希望对大家的学习有所帮助。一
一个简单的Windows Socket可复用框架说起网络编程,无非是建立连接,发送数据,接收数据,关闭连接。曾经学习网络编程的时候用Java写了一些小的聊天程序,Java对网络接口函数的封装还是很简单实用的,但是在Windows下网络编程使用的Socket就显得稍微有点繁琐。这里介绍一个自己封装的一
Windows文件操作基础代码 Windows下对文件进行操作使用的一段基础代码File.h,首先是File类定义:#pragmaonce#include&lt;Windows.h&gt;#include&lt;assert.h&gt;classFile{HANDLEhFile;//文件句柄publ
Winpcap基础代码 使用Winpcap进行网络数据的截获和发送都需要的一段代码:#include&lt;PCAP.H&gt;#pragmacomment(lib,&quot;wpcap.lib&quot;)//#pragmacomment(lib,&quot;ws2_32.lib&quot;)#
使用vbs脚本进行批量编码转换 最近需要使用SourceInsight查看分析在Linux系统下开发的项目代码,我们知道Linux系统中文本文件默认编码格式是UTF-8,而Windows中文系统中的默认编码格式是Gb2312。系统内的编码格式有所区别倒无伤大雅,关键的是SourceInsigh...
缓冲区溢出攻击缓冲区溢出(Buffer Overflow)是计算机安全领域内既经典而又古老的话题。随着计算机系统安全性的加强,传统的缓冲区溢出攻击方式可能变得不再奏效,相应的介绍缓冲区溢出原理的资料也变得“大众化”起来。其中看雪的《0day安全:软件漏洞分析技术》一书将缓冲区溢出攻击的原理阐述得简洁
Windows字符集的统一与转换一、字符集的历史渊源在Windows编程时经常会遇到编码转换的问题,一直以来让刚接触的人摸不着头脑。其实只要弄清Win32程序使用的字符编码方式就清楚了,图1展示了一个Win32控制台项目的属性中的字符集选项。这里有两个不同的字符集:一个是Unicode字符集,另一个
远程线程注入引出的问题一、远程线程注入基本原理远程线程注入——相信对Windows底层编程和系统安全熟悉的人并不陌生,其主要核心在于一个Windows API函数CreateRemoteThread,通过它可以在另外一个进程中注入一个线程并执行。在提供便利的同时,正是因为如此,使得系统内部出现了安全
windows系统启动项怎么打开
win10系统文件夹的只读属性去不掉怎么办
windows.old可以删掉吗?
windows的网络功能主要通过什么来实现?
win10系统以太网不见了怎么办
win10安装cad缺少net组件怎么办
win10系统鼠标移动方向相反怎么办
如何ping局域网内所有IP
windows10的系统保留分区有什么用
win10系统无法删除账户怎么办
win10系统音频服务未响应怎么办