【问题标题】:How To hook time function如何挂钩时间功能
【发布时间】:2016-10-08 07:11:41
【问题描述】:

每个人。我有一个项目调用一些函数来获取时间,例如

time_t t = time(NULL);

#ifndef _WIN32
    timespec ts;
    if( -1 == clock_gettime(CLOCK_MONOTONIC,&ts) )
        GenErrnoErr()
    return uint64( ( ((uint64)ts.tv_sec*1000 + (uint64)ts.tv_nsec/1000000) - m_uBaseTime ) * ms_dTimeRatio ) ;
#else
    LARGE_INTEGER uTime;
    QueryPerformanceCounter(&uTime);
    return uint64(  ( uint64(uTime.QuadPart/CFrequency::Instance().Get().QuadPart) - m_uBaseTime ) * ms_dTimeRatio );
#endif

`

我想要的是一直挂钩 func,而无需更改代码存在。当它调用 time(NULL) 或其他 func 时,它会返回我伪造的时间。

【问题讨论】:

  • 您使用的是哪个编译器和链接器? gcc?

标签: c++ linux windows api


【解决方案1】:

完成这种事情的通常方法是使用链接器的--wrap 选项。它的工作原理是这样的:

  1. 编写替换函数,但不要将其命名为 time(...),而是将其命名为 __wrap_time(...)
  2. 在你的替换函数中,如果需要调用原来的time()函数,实际调用__real_time()
  3. 链接程序时,添加以下选项:--wrap=time。这将使链接器将任何其他模块对time() 的调用链接到__wrap_time(),但仍允许通过__real_time() 调用原始time() 函数。

因此:

// Need this to satisfy the compiler
extern time_t __real_time(time_t *seconds);

time_t __wrap_time(time_t *seconds) {
    if (seconds==NULL) {
        return 0;
    } // if
    return __real_time(seconds)
} // __wrap_time(seconds)

【讨论】:

    【解决方案2】:

    将过程中的函数绕道到你的钩子。您需要注入一个 dll 来挂钩它,并在原始函数尾声中将 jmp 添加到您的挂钩函数地址。更多信息也会有所帮助...

    【讨论】:

      猜你喜欢
      • 2011-12-26
      • 1970-01-01
      • 2022-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多