about summary refs log tree commit diff stats
path: root/doc/jitter/jitter.ipynb
blob: 0201143747737db634847ba5ecdf9dbef2076e91 (plain) (blame)
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Miasm Emulation Engine\n",
    "\n",
    "This short document provides an introduction to the APIs of the Miasm emulation engine. This emulation engine is commonly referred to as **jitter** throughout the project's code (as it is based on [JiT](https://en.wikipedia.org/wiki/Just-in-time_compilation) methods)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first step is to obtain an instance of the jitter. To do this, we will go through an intermediate object, `Machine`. It will allow us to instantiate a set of elements linked to a given architecture supported by Miasm (x86, arm, mips, ...)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['arml',\n",
       " 'armb',\n",
       " 'armtl',\n",
       " 'armtb',\n",
       " 'sh4',\n",
       " 'x86_16',\n",
       " 'x86_32',\n",
       " 'x86_64',\n",
       " 'msp430',\n",
       " 'mips32b',\n",
       " 'mips32l',\n",
       " 'aarch64l',\n",
       " 'aarch64b',\n",
       " 'ppc32b',\n",
       " 'mepl',\n",
       " 'mepb']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from miasm.analysis.machine import Machine\n",
    "Machine.available_machine()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x86_32\n"
     ]
    }
   ],
   "source": [
    "machine = Machine(\"x86_32\")\n",
    "print(machine.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `Machine` class in Miasm provides various interfaces:\n",
    "\n",
    "* `jitter`: an emulation engine\n",
    "* `dis_engine`: a disassembly engine\n",
    "* `lifter`: a lifting engine, to lift assembly code to the corresponding Miasm internal representation (IR)\n",
    "* `lifter_model_call`: as `lifter`, but function calls are abstracted\n",
    "* `mn`: low level object to interact with an architecture (assembly, disassembly of only a few bytes, etc.)\n",
    "* `gdbserver`: a GDB remote debugging server (to link with a `jitter` instance)\n",
    "\n",
    "The IR related objects are already discussed in [this notebook](../ir/lift.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To obtain an emulator instance, we first need to:\n",
    "\n",
    "1. instanciate a *symbol table* (`LocationDB` in Miasm, already explained in [this notebook](../locationdb/locationdb.ipynb)) ;\n",
    "1. (optionnaly) choose a backed for the emulation (more specifically, to JiT the code):\n",
    "\n",
    "    * \"python\": a backend entirely in Python, slow\n",
    "    * \"gcc\": a backend based on the compiler GCC (Assembly $\\rightarrow$ IR Miasm $\\rightarrow$ C $\\rightarrow$ GCC), fast\n",
    "    * \"llvm\": a backend based on the LLVM JiT engine (Assembly $\\rightarrow$ IR Miasm $\\rightarrow$ IR LLVM $\\rightarrow$ LLVM JiT), fast\n",
    "\n",
    "Note: using a JiT backend as `llvm` or `gcc` greatly improve the performance over the `python` one. As a result, without any argument, instanciating an emulator will automatically try to look for and choose the best suited backend. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<miasm.arch.x86.jit.jitter_x86_32 object at 0x7f195fe07d60>\n"
     ]
    }
   ],
   "source": [
    "from miasm.core.locationdb import LocationDB\n",
    "\n",
    "loc_db = LocationDB()\n",
    "jitter = machine.jitter(loc_db)\n",
    "print(jitter)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For now, our emulator is an empty box. It has:\n",
    "\n",
    "* registers, reachable from the `.cpu` attribute. These are initiallized to 0.\n",
    "* a virtual memory, reachable from the `.vm` attribute. It starts empty."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### CPU state\n",
    "\n",
    "Let's manipulate a few registers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# Read a register\n",
    "print(jitter.cpu.EAX)\n",
    "\n",
    "# Write a register\n",
    "jitter.cpu.EAX = 1\n",
    "print(jitter.cpu.EAX)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A few *helpers* are also available. For instance, one can get every registers using:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'RAX': 1, 'RBX': 0, 'RCX': 0, 'RDX': 0, 'RSI': 0, 'RDI': 0, 'RSP': 0, 'RBP': 0, 'R8': 0, 'R9': 0, 'R10': 0, 'R11': 0, 'R12': 0, 'R13': 0, 'R14': 0, 'R15': 0, 'RIP': 0, 'zf': 0, 'nf': 0, 'pf': 0, 'of': 0, 'cf': 0, 'af': 0, 'df': 0, 'ES': 0, 'CS': 0, 'SS': 0, 'DS': 0, 'FS': 0, 'GS': 0, 'MM0': 0, 'MM1': 0, 'MM2': 0, 'MM3': 0, 'MM4': 0, 'MM5': 0, 'MM6': 0, 'MM7': 0, 'XMM0': 0, 'XMM1': 0, 'XMM2': 0, 'XMM3': 0, 'XMM4': 0, 'XMM5': 0, 'XMM6': 0, 'XMM7': 0, 'XMM8': 0, 'XMM9': 0, 'XMM10': 0, 'XMM11': 0, 'XMM12': 0, 'XMM13': 0, 'XMM14': 0, 'XMM15': 0, 'tsc': 1234605616436508552}\n"
     ]
    }
   ],
   "source": [
    "# GPReg : General Purpose registers\n",
    "regs = jitter.cpu.get_gpreg()\n",
    "print(regs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note: in Miasm, `x86_32` implementation is merged with the `x86_64` one. As a result, the register names are the x64 ones. Still, the computation is made accordingly to the 32bits implementation, and registers can be accessed either using the x64 name or the 32bits name (ie `.EAX` and `.RAX`)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Virtual memory\n",
    "\n",
    "Initially, the virtual memory is empty. The *repr* output offers a summary of the availables pages: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Addr               Size               Access Comment"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jitter.vm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Memory pages can then be added.\n",
    "The **memory address is arbitrary** (no alignment requirement). **The size is also arbitrary** (byte precision).\n",
    "\n",
    "Optionnaly, a comment associated to the page can be provided."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Addr               Size               Access Comment\n",
       "0x1000             0x1000             RWX    test page\n",
       "0x112233           0x666              RWX    no alignment, byte precision"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from miasm.jitter.csts import PAGE_READ, PAGE_WRITE, PAGE_EXEC\n",
    "jitter.vm.add_memory_page(0x1000, PAGE_READ | PAGE_WRITE | PAGE_EXEC, b\"\\x00\" * 0x1000, \"test page\")\n",
    "jitter.vm.add_memory_page(0x112233, PAGE_READ | PAGE_WRITE | PAGE_EXEC, b\"\\x00\" * 0x666, \"no alignment, byte precision\")\n",
    "jitter.vm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The memory can now be accessed, read and write:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jitter.vm.get_mem(0x1000, 0x10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'toto\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jitter.vm.set_mem(0x1000, b\"toto\")\n",
    "jitter.vm.get_mem(0x1000, 0x10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note: having a byte-precision memory has some advantages. For instance:\n",
    "* when mapping a partially known structure, instead of defaulting unknown fields to 0, we can instead only map in memory the known field. As a result, we obtain a sparse memory layout and if the programm try to read an unknown field, the execution will stop right on the responsible instruction, helping the reversing work\n",
    "* allocation can be made byte-wise. Thus, it could be easier to detect overflows and underflows"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Emulation\n",
    "\n",
    "We will now run the emulator.\n",
    "First, actual instructions are needed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "jitter.vm.set_mem(0x1000, bytes.fromhex(\"B844332211C3\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will also enable debug logging.\n",
    "By default, the logger will logs executed instruction and register values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "jitter.set_trace_log()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "The emulation is initialized with `jitter.init_run(address)` and resumed with `jitter.continue_run()`.\n",
    "For convenience, `jitter.run(address)` is usually used, wrapping these two API."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[WARNING ]: [Errno cannot get mem ad] 0x1337beef\n",
      "WARNING: address 0x1337BEEF is not mapped in virtual memory:\n",
      "[WARNING ]: cannot disasm at 1337BEEF\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "00001000 MOV        EAX, 0x11223344\n",
      "EAX 11223344 EBX 00000000 ECX 00000000 EDX 00000000 ESI 00000000 EDI 00000000 ESP 88880FFC EBP 00000000 EIP 00001005 zf 0 nf 0 of 0 cf 0\n",
      "00001005 RET        \n",
      "EAX 11223344 EBX 00000000 ECX 00000000 EDX 00000000 ESI 00000000 EDI 00000000 ESP 88881000 EBP 00000000 EIP 1337BEEF zf 0 nf 0 of 0 cf 0\n"
     ]
    }
   ],
   "source": [
    "jitter.vm.add_memory_page(0x88880000, PAGE_READ | PAGE_WRITE | PAGE_EXEC, b\"\\x00\" * 0x1000, \"stack\")\n",
    "jitter.vm.set_mem(0x88880000 + 0x1000 - 4, b\"\\xef\\xbe\\x37\\x13\")\n",
    "jitter.cpu.ESP = 0x88880000 + 0x1000 - 4\n",
    "\n",
    "jitter.run(0x1000)\n",
    "\n",
    "# The execution ends with an error, which is expected.\n",
    "# Indeed, we RET on 0x1337beef, which is not mapped in memory,\n",
    "# hence the \"WARNING: address 0x1337BEEF is not mapped in virtual memory\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Breakpoints\n",
    "\n",
    "One can register breakpoints to be raised just before a given address will be executed.\n",
    "\n",
    "As we control the CPU, the breakpoint implementation is invisible from the emulated environnement and we can use an arbitrary number of them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Breakpoints are actually callbacks which receive the emulation instance in argument\n",
    "def hello_world(jitter):\n",
    "    print(\"Hello, world!\")\n",
    "    print(\"EAX value is %d\" % jitter.cpu.EAX)\n",
    "    # Stop execution right here\n",
    "    return False\n",
    "\n",
    "jitter.add_breakpoint(0x1005, hello_world)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If a breakpoint callback returns `True`, the emulation continues.\n",
    "Otherwise, the emulation will stop and the value returned by the `jitter.continue_run()` call is the callback return value.\n",
    "\n",
    "Note: if one need to add more arguments to the callback, there are many Pythonic way to do it.\n",
    "For instance, one can use `lambda` to capture an argument or object instance (ie. `jitter.add_breakpoint(address, obj.callback)` with a `def callback(self, jitter)` inside the `obj` definition).\n",
    "\n",
    "The breakpoint mechanism is used to implement several features. For instance:\n",
    "* it can be used to hook a function, by breakpointing on the function address, emulating a few side effects then returning to the return address. Indeed, modifying the programm counter (usually using `jitter.pc = XXX`) in a breakpoint will resume the execution on this new address\n",
    "* in Miasm examples, a breakpoint on a fake address is often used to properly stop the emulation. The callback is usually named `code_sentinelle` and allow to get back control after the emulation of a code snippet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Exercices\n",
    "\n",
    "For the interested reader, this section introduce a few exercice to practice the API.\n",
    "\n",
    "#### Exercice 1\n",
    "\n",
    "Starting from a new instance:\n",
    "1. Add a memory page which will act as a stack page\n",
    "1. Update the stack pointer accordingly\n",
    "1. Manually push a fake return address\n",
    "1. Map a `RET` instruction (0xC3) in another page\n",
    "1. Run the emulation and ensure it finish on your fake address"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Exercice 1 - Spoiler\n",
    "\n",
    "A few helpers are actually availables to reduce the burden of these manual actions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0x1000\n"
     ]
    }
   ],
   "source": [
    "# Init a stack and set stack pointer accordingly (architecture agnostic way of writing)\n",
    "jitter.init_stack()\n",
    "\n",
    "# Push 0x1000 on the stack\n",
    "jitter.push_uint32_t(0x1000)\n",
    "\n",
    "# Pop 0x1000 from the stack\n",
    "print(hex(jitter.pop_uint32_t()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Exercice 2\n",
    "\n",
    "Same a Exercice 1, but instead of ending on an error, we will this time add a breakpoint on the fake return address to properly stop the execution."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "#### Exercice 2 - Spoiler\n",
    "\n",
    "The example `jitter/x86_32.py` is exactly doing this."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}