-
Notifications
You must be signed in to change notification settings - Fork 0
/
iat_hooking.html
390 lines (366 loc) · 24.8 KB
/
iat_hooking.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="F3real" />
<meta name="keywords" content="windows,dll,IAT,hook" />
<meta name="description" content="How to do simple IAT hook" />
<title>IAT hooking - EnSec blog</title>
<link href="https://f3real.github.io/theme/css/combined.css" rel="stylesheet" />
<!-- Feeds -->
</head>
<body data-spy="scroll" data-target="#scrollspy">
<div id="wrapper">
<!-- Sidebar -->
<nav id="sidebar-wrapper-small" class="twitchy-background">
<ul id="accordion-small" class="sidebar-nav sidebar-nav-small">
<li>
<a href="https://f3real.github.io" title="EnSec blog" class="collapsed">
<span class="fas fa-home"></span>
</a>
</li>
<li class="nav-divider"></li>
<li>
<a href="https://f3real.github.io/archives.html" title="Recent Articles" class="collapsed">
<span class="fas fa-th-list"></span>
</a>
</li>
<li class="nav-divider"></li>
<li>
<a data-toggle="collapse" data-parent="#accordion-small" href="#collapse-social-small" title="Social" class="collapsed">
<i class="fas fa-users padding-small"></i>
</a>
</li>
<li class="panel anti-panel"><ul id="collapse-social-small" class="collapse ">
<li>
<a href="https://github.com/F3real" title="Github"><i class="fab fa-github-square padding-small"></i></a>
</li>
<li>
<a href="https://www.linkedin.com/in/stefan-ili%C4%87-61a004111" title="Linkedin"><i class="fab fa-linkedin padding-small"></i></a>
</li>
</ul></li>
<li class="nav-divider"></li>
<li>
<a href="#" title="Back to top" class="collapsed">
<span class="fas fa-arrow-up"></span>
</a>
</li>
</ul>
</nav>
<nav id="sidebar-wrapper" class="twitchy-background">
<ul id="accordion" class="sidebar-nav">
<li class="sidebar-brand">
<a href="https://f3real.github.io/">
<span class="fas fa-home padding-small"></span>
EnSec blog
</a>
</li>
<li>
<a href="https://f3real.github.io/archives.html">
<span class="fas fa-th-list padding-small"></span>
Archives
</a>
</li>
<li class="nav-divider"></li>
<li>
<a data-toggle="collapse" data-parent="#accordion" href="#collapse-social">
<i class="fas fa-users padding-small"></i>
Contact
</a>
</li>
<li class="panel anti-panel"><ul id="collapse-social" class="sidebar_submenu collapse ">
<li>
<a href="https://github.com/F3real" title="Github">
<i class="fab fa-github-square padding-small"></i>
Github
</a>
</li>
<li>
<a href="https://www.linkedin.com/in/stefan-ili%C4%87-61a004111" title="Linkedin">
<i class="fab fa-linkedin padding-small"></i>
Linkedin
</a>
</li>
</ul></li>
<li class="nav-divider"></li>
<li class="panel anti-panel"><ul id="collapse-pages" class="sidebar_submenu collapse ">
</ul></li>
<li class="nav-divider"></li>
<li>
<a data-toggle="collapse" data-parent="#accordion" href="#collapse-categories">
<i class="fas fa-folder-open padding-small"></i>
Categories
</a>
</li>
<li class="panel anti-panel"><ul id="collapse-categories" class="sidebar_submenu collapse ">
<li >
<a href="https://f3real.github.io/category/ctf.html">
<i class="fas fa-folder-open padding-small"></i>
ctf
<span class="badge badge-secondary float-right categorybadge">28</span>
</a>
</li>
<li >
<a href="https://f3real.github.io/category/misc.html">
<i class="fas fa-folder-open padding-small"></i>
misc
<span class="badge badge-secondary float-right categorybadge">15</span>
</a>
</li>
<li >
<a href="https://f3real.github.io/category/reversing.html">
<i class="fas fa-folder-open padding-small"></i>
reversing
<span class="badge badge-secondary float-right categorybadge">6</span>
</a>
</li>
<li class="active">
<a href="https://f3real.github.io/category/tutorial.html">
<i class="fas fa-folder-open padding-small"></i>
tutorial
<span class="badge badge-secondary float-right categorybadge">5</span>
</a>
</li>
</ul></li>
</ul>
</nav>
<!-- /#sidebar-wrapper -->
<!-- open/close sidebar -->
<button onclick="toggleMenu();return false;" class="btn btn-primary" id="menu-toggle">
<span id="right-arrow" class="fas fa-chevron-right" title="expand sidebar"></span>
<span id="left-arrow" class="fas fa-chevron-left" title="minimize sidebar"></span>
</button>
<!-- /open/close sidebar -->
<!-- Page Content -->
<div id="page-content-wrapper">
<div class="container-fluid">
<section id="content">
<article>
<div class="row">
<div class="col-lg-10">
<header class="page-header">
<h1>
<a href="https://f3real.github.io/iat_hooking.html"
rel="bookmark"
title="Permalink to IAT hooking">
IAT hooking
</a>
<small>
<div class="post-info">
<div class="publish-info-block">
<small>
<span class="published">
<i class="fa fa-calendar padding-small"></i><time datetime="2019-04-17T10:01:00+02:00"> Wed 17 April 2019</time>
</span>
<span class="category">
<i class="fa fa-folder-open padding-small"></i><a href="https://f3real.github.io/category/tutorial.html">tutorial</a>
</span>
<span class="tags">
<i class="fa fa-tags padding-small"></i>
<a href="https://f3real.github.io/tag/windows.html">windows</a> / <a href="https://f3real.github.io/tag/dll.html">dll</a> / <a href="https://f3real.github.io/tag/iat.html">IAT</a> / <a href="https://f3real.github.io/tag/hook.html">hook</a> </span>
</small>
</div>
</div><!-- /.post-info --> </small>
</h1>
</header>
</div>
</div>
<div class="row">
<div class="col-lg-10">
<div class="entry-content">
<p>There are few different userland API hooking techniques in Windows, but in this post, we will take a look at IAT (Import Address Table) hooking.</p>
<p>IAT is a lookup table of function pointers for functions imported from modules (executables or dlls). At compile time addresses of these functions are unknown so dynamic linker/loader has to fill IAT with real function addresses at runtime.</p>
<p>IAT hooking relies on replacing real function address in IAT table with address we control. IAT doesn't work with functions obtained from dlls by <code>LoadLibrary</code>/<code>GetProcAddress</code> directly (but we can overwrite <code>GetProcAddress</code> to give different result).</p>
<p>Standard function call using IAT table looks something like:</p>
<div class="highlight"><pre><span></span><code> Application mydll
+-------------------+ +--------------------+
| | | func1 |
| | +------------>---------------------+
| call mydll!func1+ | IAT | | .... |
| | | +-------------------+ | code |
+-------------------+ | | | | .... |
+----------> jmp func1 + | +--------------------+
| | | |
+-------------------+ +--------------------+
</code></pre></div>
<p>The first step is to locate IAT in memory, for this we can parse PE optional header which contains <code>Data directory</code> entry with IAT address.</p>
<p>Here is pretty nice illustration of PE structure (taken from pentest.blog):</p>
<p><img alt="PE structure" class="img-fluid centerimage" src="https://f3real.github.io/images/2019_4_17_location-of-the-IAT.png"></p>
<p><code>DataDirectory</code> is an array of <code>IMAGE_DATA_DIRECTORY</code> structures:</p>
<div class="highlight"><pre><span></span><code><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">_IMAGE_DATA_DIRECTORY</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">DWORD</span><span class="w"> </span><span class="n">VirtualAddress</span><span class="p">;</span><span class="w"> </span><span class="c1">// RVA of data</span>
<span class="w"> </span><span class="n">DWORD</span><span class="w"> </span><span class="n">Size</span><span class="p">;</span><span class="w"> </span><span class="c1">// Size of the data in bytes</span>
<span class="p">}</span><span class="n">IMAGE_OPTIONAL_HEADERS32</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">PIMAGE_OPTIONAL_HEADERS32</span><span class="p">;</span>
</code></pre></div>
<p>Pointer IAT is found at <code>DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT]</code> entry.</p>
<p>But finding the IAT is not enough for hooking an API function. It contains only API addreses and in order to replace an API function address we need to know which entry belongs to the API function that we want to hook. For this, we have to look at IDT (pointer to IDT is in <code>DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]</code>).</p>
<p>IDT (Import Directory Table) table contains IMAGE_IMPORT_DESCRIPTOR entries with the following structure:</p>
<div class="highlight"><pre><span></span><code><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">_IMAGE_IMPORT_DESCRIPTOR</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">union</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">DWORD</span><span class="w"> </span><span class="n">Characteristics</span><span class="p">;</span><span class="w"> </span><span class="c1">// 0 for terminating null import descriptor</span>
<span class="w"> </span><span class="n">PIMAGE_THUNK_DATA</span><span class="w"> </span><span class="n">OriginalFirstThunk</span><span class="p">;</span><span class="w"> </span><span class="c1">// The RVA of the import lookup table</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="n">DWORD</span><span class="w"> </span><span class="n">TimeDateStamp</span><span class="p">;</span><span class="w"> </span><span class="c1">// 0 if not bound,</span>
<span class="w"> </span><span class="c1">// -1 if bound, and real date\time stamp</span>
<span class="w"> </span><span class="c1">// in IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT (new BIND)</span>
<span class="w"> </span><span class="c1">// O.W. date/time stamp of DLL bound to (Old BIND)</span>
<span class="w"> </span><span class="n">DWORD</span><span class="w"> </span><span class="n">ForwarderChain</span><span class="p">;</span><span class="w"> </span><span class="c1">// -1 if no forwarders</span>
<span class="w"> </span><span class="n">DWORD</span><span class="w"> </span><span class="n">Name</span><span class="p">;</span><span class="w"> </span><span class="c1">// address of dll name string</span>
<span class="w"> </span><span class="n">PIMAGE_THUNK_DATA</span><span class="w"> </span><span class="n">FirstThunk</span><span class="p">;</span><span class="w"> </span><span class="c1">// same as OriginalFirstThunk or, if bound, the RVA of the IAT. </span>
<span class="p">}</span><span class="w"> </span><span class="n">IMAGE_IMPORT_DESCRIPTOR</span><span class="p">;</span>
<span class="k">typedef</span><span class="w"> </span><span class="n">IMAGE_IMPORT_DESCRIPTOR</span><span class="w"> </span><span class="n">UNALIGNED</span><span class="w"> </span><span class="o">*</span><span class="n">PIMAGE_IMPORT_DESCRIPTOR</span><span class="p">;</span>
</code></pre></div>
<p>IDT contains entries for all dlls loaded by executable and is used by the loader to fill entries in IAT with real function addresses.</p>
<p>ILT (Import Lookup Table) contains a list of function names imported from the specified DLL. Entries of ILT are IMAGE_THUNK_DATA32 structs.</p>
<div class="highlight"><pre><span></span><code><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">_IMAGE_THUNK_DATA32</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">union</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">LPBYTE</span><span class="w"> </span><span class="n">ForwarderString</span><span class="p">;</span><span class="w"> </span>
<span class="w"> </span><span class="n">PDWORD</span><span class="w"> </span><span class="n">Function</span><span class="p">;</span>
<span class="w"> </span><span class="n">DWORD</span><span class="w"> </span><span class="n">Ordinal</span><span class="p">;</span>
<span class="w"> </span><span class="n">PIMAGE_IMPORT_BY_NAME</span><span class="w"> </span><span class="n">AddressOfData</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="k">typedef</span><span class="w"> </span><span class="n">_IMAGE_THUNK_DATA32</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">PIMAGE_THUNK_DATA</span><span class="p">;</span>
<span class="k">typedef</span><span class="w"> </span><span class="n">_IMAGE_THUNK_DATA32</span><span class="w"> </span><span class="n">IMAGE_THUNK_DATA32</span><span class="p">;</span>
</code></pre></div>
<p>Functions can be imported by name or by ordinal. For ordinal imports, the <code>Ordinal</code> field of the union in <code>IMAGE_THUNK_DATA</code> structure will have the most significant bit set to 1 and the ordinal number can be extracted from the least significant bits.</p>
<p>In the case of import by name, the structure holds a pointer to <code>IMAGE_IMPORT_BY_NAME</code> structure.</p>
<div class="highlight"><pre><span></span><code><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">_IMAGE_IMPORT_BY_NAME</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">WORD</span><span class="w"> </span><span class="n">Hint</span><span class="p">;</span><span class="w"> </span><span class="c1">//used to find location of function faster</span>
<span class="w"> </span><span class="n">BYTE</span><span class="w"> </span><span class="n">Name</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span><span class="w"> </span><span class="c1">//pointer to the function name</span>
<span class="p">}</span><span class="w"> </span><span class="n">IMAGE_IMPORT_BY_NAME</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">PIMAGE_IMPORT_BY_NAME</span><span class="p">;</span>
</code></pre></div>
<p>So how does all this work?</p>
<p><code>FirstThunk</code> in IDT contains the RVA to the array of <code>IMAGE_THUNK_DATA</code> structures with the same length as the OriginalFirstThunk array.
The <code>OriginalFirstThunk</code> represents ILT, an array of names of imported functions (if they are not imported using ordinal value). The <code>FirstThunk</code> is an array of addresses of imported functions IAT (after they are bound, initially it's the same as <code>OriginalFirstThunk</code>).</p>
<p>The <code>OriginalFirstThunk</code> uses the <code>AddressOfData</code> element of the <code>IMAGE_THUNK_DATA</code> structure to point to <code>IMAGE_IMPORT_BY_NAME</code> structure that contains the <code>Name</code> element, function name. The <code>FirstThunk</code> uses the <code>Function</code> element of the <code>IMAGE_THUNK_DATA</code> structure, which points to the address of the imported function. When the executable is loaded, the loader goes through the <code>OriginalFirstThunk</code> array and finds all imported function names the executable is using. Then it calculates the addresses of the functions and populates the <code>FirstThunk</code> array so that real functions can be accessed.</p>
<p>As a refresher, RVA and VA are defined as:</p>
<p><strong>RVA</strong> (relative virtual address). In an image file, the address of an item after it is loaded into memory, with the base address of the image file subtracted from it. The RVA of an item almost always differs from its position within the file on disk (file pointer).</p>
<p>In an object file, an RVA is less meaningful because memory locations are not assigned. In this case, an RVA would be an address within a section (described later in this table), to which a relocation is later applied during linking. For simplicity, a compiler should just set the first RVA in each section to zero.</p>
<div class="highlight"><pre><span></span><code>base address of module + RVA of PE element = linear address of PE element
</code></pre></div>
<p><strong>VA</strong> (virtual address). Same as RVA, except that the base address of the image file is not subtracted. The address is called a “VA” because Windows creates a distinct VA space for each process, independent of physical memory. For almost all purposes, a VA should be considered just an address. A VA is not as predictable as an RVA because the loader might not load the image at its preferred location.</p>
<p>So, let's look at required steps to do IAT hook:</p>
<ol>
<li>Locate optional header in <code>.exe</code> we want to hook</li>
<li>Locate IDT of dll containing function we want to hook</li>
<li>Locate entry in ILT (pointed to from OriginalFirstThunk) with the name of a function we want to hook</li>
<li>Replace entry with the same index in IAT (pointed to from FirstThunk) with the address we control</li>
</ol>
<p>Example VS 2017 project with IAT hook implementation (without checks if PE format is ok) can be found <a href="https://github.com/F3real/ctf_solutions/tree/master/2019/processHider">here</a>. To use dll we get we also need to inject it in memory of the target process.</p>
</div>
<footer class="text-right">
<p>- F3real</p>
</footer>
<div id="show-comments" class="span7 text-center">
<a href="https://f3real.github.io/iat_hooking.html#disqus_thread"
data-disqus-identifier="iat_hooking"
class="btn btn-primary twitchy-background">Show Comments</a>
</div>
<section id="comments" class="comments hidden">
<hr/>
<h2>Comments</h2>
<div id="disqus_thread"></div>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by
Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</section>
</div>
</div>
</article>
</section>
<footer>
<hr>
<div class="row">
<div class="col-lg-10 text-center">
<p><small>
Built by <a href="http://docs.getpelican.com/en/latest">Pelican</a> / <a href="https://github.com/F3real/pelican-twitchy">pelican-twitchy</a>
· © 2024 F3real
</small></p>
</div>
</div>
</footer> </div>
</div>
<!-- /#page-content-wrapper -->
</div>
<!-- /#wrapper -->
<!-- disqus -->
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'https-f3real-github-io'; // required: replace example with your forum shortname
var disqus_identifier = 'iat_hooking';
var disqus_url = 'https://f3real.github.io/iat_hooking.html';
var disqus_config = function () {
this.language = "en";
};
var commentsDiv = document.getElementById('show-comments');
commentsDiv.onclick = function() {
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
this.style.display = 'none';
};
</script>
<!-- /disqus -->
<script>
const wrapper = document.getElementById('wrapper');
const sidebarBig = document.getElementById('sidebar-wrapper');
const sidebarSmall = document.getElementById('sidebar-wrapper-small');
const triggers = Array.from(document.querySelectorAll('[data-toggle="collapse"]'));
for (var i = 0; i < triggers.length; i++) {
triggers[i].addEventListener('click', (ev) => {
const elm = ev.currentTarget;
ev.preventDefault();
const selector = elm.getAttribute('href').replace('#','');
elm.classList.toggle('collapsed');
document.getElementById(selector).classList.toggle('show');
}, false);
}
function showBigNav() {;
sidebarBig.style.display = 'block';
sidebarSmall.style.display = 'none';
}
function showSmallNav() {
sidebarBig.style.display = 'none';
sidebarSmall.style.display = 'block';
}
const mediaQuery = window.matchMedia('(min-width:768px)');
mediaQuery.onchange = e => {
if (wrapper.classList.contains('toggled')) {
wrapper.classList.remove('toggled');
} else {
if (e.matches) {
showBigNav();
} else {
showSmallNav();
}
}
}
function setNavbar() {
var condition = wrapper.classList.contains('toggled');
if (!mediaQuery.matches) {
condition = !condition;
}
if (condition) {
showSmallNav();
} else {
showBigNav();
}
}
function toggleMenu(){
wrapper.classList.toggle('toggled');
setNavbar();
}
window.onload = setNavbar;
</script>
<script data-goatcounter="https://f3real.goatcounter.com/count"
async src="//gc.zgo.at/count.js"></script>
</body>
</html>