分类: |
  • 1

Keyboard Hook

Hook is a mechanism, by which a function can intercept events before they reach an application. The function can act on events, modify or discard them. Functions which receive the events are called Filter Functions, every Filter Function is classified by its type. Hooks provide powerful capabilities: Procces or modify every message; Record or play back keyboard and mouse events; Prevent another filter from being called; And many more capabilities... Generally, there are two types of hooks: System-wide, and Thread-specific. The System-wide hook is used for filtering messages of all applications(IE: when writing a key logger). And the Thread-specific hook is used for filtering messages of a specific thread. In this tutorial, I'll cover just System-wide keyboard hook. To set a System-wide hook we need a DLL.

A DLL is an indirectly executable which doesn't have a message loop to receive user input. DLLs are seperate files containing functions(not only) that can be called by programs and other DLLs. To the end-user a DLL is a program that can't be executed directly from the Program Manger(or other Shells), but from the system's point of view, there are two differences between DLLs and applications: DLLs cannot have multiple running instances loaded in memory. and DLLs attach themselves to processes, only application instances are processes. DLL stands for Dynamic-Link Library. Dynamic-Link is a mechanism to link libraries to applications at run time. These libraries(DLLs) reside in their own executable files(.dll) and are not copied into applications' executable files(.exe) as with Static-Link libraries. It's important to understand that a .DLL is loaded into the address space of the specified linking application and not into the global heap! The advantages of using dynamic linking method are:
They can be updated without requiring applications to be recompiled or relinked.
When several applications use the same .DLL, the .DLL is just loaded once for all applications(reducing memory and disk space).

查看更多...

Tags: c语言 代码 windows api vc hook

分类:c/c++ | 固定链接 | 评论: 0 | 查看次数: 8816

api hook和pe格式的关系

api hook技术的难点,并不在于hook技术,初学者借助于资料“照葫芦画瓢”能够很容易就掌握hook的基本使用技术。但是如何修改api函数的入口地址?这就需要学习pe可执行文件(.exe,.dll等)如何被系统映射到进程空间中,这就需要学习pe格式的基本知识。windows已经提供了很多数据结构struct帮助我们访问pe格式,借助它们,我们就不要自己计算格式的具体字节位置这些繁琐的细节。但是从api hook的实现来看,pe格式的访问部分仍然是整个编程实现中最复杂的一部分,对于经常crack的朋友不在此列。
假设我们已经了解了pe格式,那么我们在哪里修改api的函数入口点比较合适呢?这个就是输入符号表imported symbols table(间接)指向的输入符号地址。
   下面对于pe格式的介绍这一部分,对于没有接触过pe格式学习的朋友应该是看不太明白的,但我已经把精华部分提取出来了,学习了pe格式后再看这些就很容易了。

pe格式的基本组成
+-------------------+
    | DOS-stub          |    --DOS-头
    +-------------------+
    | file-header       |    --文件头
    +-------------------+
    | optional header   |    --可选头
    |- - - - - - - - - -|
    |                   |
    | data directories |    --(可选头尾的)数据目录
    |                   |
    +-------------------+
    |                   |
    | section headers   |     --节头
    |                   |
    +-------------------+
    |                   |
    | section 1        |     --节1
    |                   |
    +-------------------+
    |                   |
    | section 2        |     --节2
    |                   |
    +-------------------+
    |                   |
    | ...               |
    |                   |
    +-------------------+
    |                   |
    | section n        |     --节n
    |                   |
    +-------------------+
    在上图中,我们需要从“可选头”尾的“数据目录”数组中的第二个元素——输入符号表的位置,它是一个IMAGE_DATA_DIRECTORY结构,从它中的VirtualAddress地址,“顺藤摸瓜”找到api函数的入口地点。
    下图的简单说明如下:
OriginalFirstThunk 指向IMAGE_THUNK_DATA结构数组,为方便只画了数组的一个元素,AddressOfData 指向IMAGE_IMPORT_BY_NAME结构。
IMAGE_IMPORT_DESCRIPTOR数组:每个引入的dll文件都对应数组中的一个元素,以全0的元素(20个bytes的0)表示数组的结束
IMAGE_THUNK_DATA32数组:同一组的以全0的元素(4个bytes的0)表示数组的结束,每个元素对应一个 IMAGE_IMPORT_BY_NAME结构
IMAGE_IMPORT_BY_NAME:如..@Consts@initialization$qqrv. 表示
Unmangled Borland C++ Function: qualified function __fastcall Consts::initialization() 
 
    为了减少这个图的大小,不得已将汇编和c++的结构都用上了。这个图是输入符号表初始化的情形,此时两个IMAGE_THUNK_DATA结构数组的对应元素都指向同一个IMAGE_IMPORT_BY_NAME结构。
    程序加载到进程空间后,两个IMAGE_THUNK_DATA结构数组指向有所不同了。看下图: 


// 本文转自 C++Builder研究 - http://www.ccrun.com/article.asp?i=1036&d=cf6de2
始化的,“两个结构都指向同一个IMAGE_IMPORT_BY_NAME”,此时还没有api函数地址 


当PE文件准备执行时,前图已转换成上图。一个结构指向不变,另一个出现api函数地址

    如果PE文件从kernel32.dll中引入10个函数,那么IMAGE_IMPORT_DESCRIPTOR 结构的 Name1域包含指向字符串"kernel32.dll"的RVA,同时每个IMAGE_THUNK_DATA 数组有10个元素。(RVA是指相对地址,每一个可执行文件在加载到内存空间前,都以一个基址作为起点,其他地址以基址为准,均以相对地址表示。这样系统加载程序到不同的内存空间时,都可以方便的算出地址)
    上述这些结构可以在winnt.h头文件里查到。

查看更多...

Tags: pe 格式 汇编 导入表 api 原理 hook

分类:c/c++ | 固定链接 | 评论: 0 | 查看次数: 9570

inline hook NtQuerySystemInformation 保护进程

      inline hook & NtQuerySystemInformation & ring0
本文呢,是介绍一种保护进程不被结束的方法,这个方法不算新了,好旧好旧的了,其实呢,也是 hook 了某个函数来实现的,不过没有 hook NtOpenProcess 、NtTerminateProcess、KiInsertQueuApc 等函数,而是 hook 了 NtQuerySystemInformation 来保护我们的进程,NtQuerySystemInformation 是用来查询系统信息的,可以查询的系统信息有 54 种这么多,其中 ID 是 5 的话呢,就会返回一个链表,这个链表中包括了当前系统中的所以的进程名、进程 ID,也就是说这个函数可以获取系统的进程列表,我们可以 hook 这个函数来隐藏进程,不过这次是保护进程,不是隐藏哦
在 ring3 中,列举进程的方法是调用 Tool32 或者 psapi 中的 EnumProcess,可是这些函数都调用了 ZwQuerySystemInformation 然后在NtQuerySystemInformation,也就是我们 hook NtQuerySystemInformation 就可以保护&隐藏进程了哇
hookNtQuerySystemInformation 的方法有好的,可以修改 SSDT,也可以修改函数前 5 个字节,也就是inline hook了,本文采用后者哦
在函数开始的5个字节中,改为一个 Jmp 指令,让她在调用这个函数的时候跳转到我们的函数中,我们在经过一些处理后再调用原来的****

查看更多...

Tags: inline hook api hook

分类:破解调试 | 固定链接 | 评论: 0 | 查看次数: 12484

apihook原理分析

跨进程[b]API Hook[/b](初稿)

什么是“跨进程 API Hook”?
众所周知Windows应用程序的各种系统功能是通过调用API函数来实现。API Hook就是给系统的API附加上一段小程序,它能监视甚至控制应用程序对API函数的调用。所谓跨进程也就是让自己的程序来控制别人程序的API调用了。

查看更多...

Tags: api 原理 分析 hook

分类:c/c++ | 固定链接 | 评论: 0 | 查看次数: 7390
  • 1