-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrackmes_one1.html
440 lines (406 loc) · 38.2 KB
/
crackmes_one1.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
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
<!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="reversing,crackme,windows,radare2" />
<meta name="description" content="Solutions to Mexican crackme" />
<title>Crackmes one Mexican - 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 class="active">
<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 >
<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/crackmes_one1.html"
rel="bookmark"
title="Permalink to Crackmes one Mexican">
Crackmes one Mexican
</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-11-09T10:02:00+01:00"> Sat 09 November 2019</time>
</span>
<span class="category">
<i class="fa fa-folder-open padding-small"></i><a href="https://f3real.github.io/category/reversing.html">reversing</a>
</span>
<span class="tags">
<i class="fa fa-tags padding-small"></i>
<a href="https://f3real.github.io/tag/reversing.html">reversing</a> / <a href="https://f3real.github.io/tag/crackme.html">crackme</a> / <a href="https://f3real.github.io/tag/windows.html">windows</a> / <a href="https://f3real.github.io/tag/radare2.html">radare2</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>Recently, I've found interesting <a href="https://medium.com/syscall59/solved-solving-mexican-crackme-82d71a28e189">post</a> about solving <a href="https://crackmes.one/crackme/5d63011533c5d46f00e2c305">Mexican</a> crackme.</p>
<p>Let's look in-depth how the solution works and how is it automatically solving this crackme with radare2 and python.</p>
<p>To analyze binary we can use:</p>
<div class="highlight"><pre><span></span><code>radare2 Untitled1.exe
</code></pre></div>
<p>We can analyze all symbols and entry points with <code>aa</code>. After this, we can list functions using <code>afl</code> (all functions list).</p>
<p>With <code>s main</code> (seek) we can position ourselves at the start of main and we can disassemble function with <code>pdf</code> (print disassembly of function).
We can also use <code>pdf @main</code>.</p>
<p>Disassembly:</p>
<div class="highlight"><pre><span></span><code><span class="err">│</span><span class="w"> </span><span class="nf">int</span><span class="w"> </span><span class="no">main</span><span class="w"> </span><span class="p">(</span><span class="no">int</span><span class="w"> </span><span class="no">argc</span><span class="p">,</span><span class="w"> </span><span class="no">char</span><span class="w"> </span><span class="p">**</span><span class="no">argv</span><span class="p">,</span><span class="w"> </span><span class="no">char</span><span class="w"> </span><span class="p">**</span><span class="no">envp</span><span class="p">)</span><span class="c1">;</span>
<span class="err">│</span><span class="w"> </span><span class="c1">; var int32_t var_1ch @ esp+0x1c</span>
<span class="err">│</span><span class="w"> </span><span class="c1">; CALL XREF from entry0 @ 0x4013dd</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x0040162c</span><span class="w"> </span><span class="mi">55</span><span class="w"> </span><span class="no">push</span><span class="w"> </span><span class="no">ebp</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x0040162d</span><span class="w"> </span><span class="mi">89</span><span class="no">e5</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">ebp</span><span class="p">,</span><span class="w"> </span><span class="no">esp</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x0040162f</span><span class="w"> </span><span class="mi">83</span><span class="no">e4f0</span><span class="w"> </span><span class="no">and</span><span class="w"> </span><span class="no">esp</span><span class="p">,</span><span class="w"> </span><span class="mi">0xfffffff0</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401632</span><span class="w"> </span><span class="mi">83</span><span class="no">ec20</span><span class="w"> </span><span class="no">sub</span><span class="w"> </span><span class="no">esp</span><span class="p">,</span><span class="w"> </span><span class="mi">0x20</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401635</span><span class="w"> </span><span class="no">e886090000</span><span class="w"> </span><span class="no">call</span><span class="w"> </span><span class="no">sym.___main</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x0040163a</span><span class="w"> </span><span class="no">c744241cc100.</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">var_1ch</span><span class="p">],</span><span class="w"> </span><span class="mi">0xc1</span><span class="w"> </span><span class="c1">; [0xc1:4]=-1 ; 193</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401642</span><span class="w"> </span><span class="mi">817</span><span class="no">c241cc100.</span><span class="w"> </span><span class="no">cmp</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">var_1ch</span><span class="p">],</span><span class="w"> </span><span class="mi">0xc1</span>
<span class="err">│</span><span class="w"> </span><span class="err">┌─<</span><span class="w"> </span><span class="err">0</span><span class="nf">x0040164a</span><span class="w"> </span><span class="mi">7</span><span class="no">e07</span><span class="w"> </span><span class="no">jle</span><span class="w"> </span><span class="mi">0x401653</span>
<span class="err">│</span><span class="w"> </span><span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x0040164c</span><span class="w"> </span><span class="no">e8affeffff</span><span class="w"> </span><span class="no">call</span><span class="w"> </span><span class="no">sym</span><span class="w"> </span><span class="no">flag</span><span class="p">()</span><span class="w"> </span><span class="c1">; sym.flag</span>
<span class="err">│</span><span class="w"> </span><span class="err">┌──<</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401651</span><span class="w"> </span><span class="no">eb0c</span><span class="w"> </span><span class="no">jmp</span><span class="w"> </span><span class="mi">0x40165f</span>
<span class="err">│</span><span class="w"> </span><span class="err">│└─></span><span class="w"> </span><span class="err">0</span><span class="nf">x00401653</span><span class="w"> </span><span class="no">c70424034040.</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">esp</span><span class="p">],</span><span class="w"> </span><span class="no">str.try_harder</span><span class="w"> </span><span class="c1">; [0x404003:4]=0x20797274 ; "try harder"</span>
<span class="err">│</span><span class="w"> </span><span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x0040165a</span><span class="w"> </span><span class="no">e8d9100000</span><span class="w"> </span><span class="no">call</span><span class="w"> </span><span class="no">sym._printf</span><span class="w"> </span><span class="c1">; int printf(const char *format)</span>
<span class="err">│</span><span class="w"> </span><span class="err">│</span><span class="w"> </span><span class="c1">; CODE XREF from main @ 0x401651</span>
<span class="err">│</span><span class="w"> </span><span class="err">└──></span><span class="w"> </span><span class="err">0</span><span class="nf">x0040165f</span><span class="w"> </span><span class="no">b800000000</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401664</span><span class="w"> </span><span class="no">c9</span><span class="w"> </span><span class="no">leave</span>
<span class="err">└</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401665</span><span class="w"> </span><span class="no">c3</span><span class="w"> </span><span class="no">ret</span>
</code></pre></div>
<p>We see that it will compare 0xc1 with 0xc1 which will always go to fail condition.
If we look at function flag with <code>pdf @sym.flag</code>, we see:</p>
<div class="highlight"><pre><span></span><code><span class="err">┌</span><span class="w"> </span><span class="err">(</span><span class="nf">fcn</span><span class="p">)</span><span class="w"> </span><span class="no">sym.flag</span><span class="w"> </span><span class="mi">300</span>
<span class="err">│</span><span class="w"> </span><span class="nf">sym.flag</span><span class="w"> </span><span class="p">()</span><span class="c1">;</span>
<span class="err">│</span><span class="w"> </span><span class="c1">; var int32_t var_ch @ ebp-0xc</span>
<span class="err">│</span><span class="w"> </span><span class="c1">; var int32_t var_4h @ esp+0x4</span>
<span class="err">│</span><span class="w"> </span><span class="c1">; CALL XREF from main @ 0x40164c</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401500</span><span class="w"> </span><span class="mi">55</span><span class="w"> </span><span class="no">push</span><span class="w"> </span><span class="no">ebp</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401501</span><span class="w"> </span><span class="mi">89</span><span class="no">e5</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">ebp</span><span class="p">,</span><span class="w"> </span><span class="no">esp</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401503</span><span class="w"> </span><span class="mi">83</span><span class="no">ec28</span><span class="w"> </span><span class="no">sub</span><span class="w"> </span><span class="no">esp</span><span class="p">,</span><span class="w"> </span><span class="mi">0x28</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401506</span><span class="w"> </span><span class="no">c704241d0000.</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">esp</span><span class="p">],</span><span class="w"> </span><span class="mi">0x1d</span><span class="w"> </span><span class="c1">; [0x1d:4]=-1 ; 29</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x0040150d</span><span class="w"> </span><span class="no">e8ee110000</span><span class="w"> </span><span class="no">call</span><span class="w"> </span><span class="no">sym._malloc</span><span class="w"> </span><span class="c1">; void *malloc(size_t size)</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401512</span><span class="w"> </span><span class="mi">8945</span><span class="no">f4</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">var_ch</span><span class="p">],</span><span class="w"> </span><span class="no">eax</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401515</span><span class="w"> </span><span class="mi">8</span><span class="no">b45f4</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">var_ch</span><span class="p">]</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401518</span><span class="w"> </span><span class="no">c60066</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">byte</span><span class="w"> </span><span class="p">[</span><span class="no">eax</span><span class="p">],</span><span class="w"> </span><span class="mi">0x66</span><span class="w"> </span><span class="c1">; 'f'</span>
<span class="err">│</span><span class="w"> </span><span class="c1">; [0x66:1]=255 ; 102</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x0040151b</span><span class="w"> </span><span class="mi">8</span><span class="no">b45f4</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">var_ch</span><span class="p">]</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x0040151e</span><span class="w"> </span><span class="mi">83</span><span class="no">c001</span><span class="w"> </span><span class="no">add</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401521</span><span class="w"> </span><span class="no">c6006c</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">byte</span><span class="w"> </span><span class="p">[</span><span class="no">eax</span><span class="p">],</span><span class="w"> </span><span class="mi">0x6c</span><span class="w"> </span><span class="c1">; 'l'</span>
<span class="err">│</span><span class="w"> </span><span class="c1">; [0x6c:1]=255 ; 108</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401524</span><span class="w"> </span><span class="mi">8</span><span class="no">b45f4</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">var_ch</span><span class="p">]</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401527</span><span class="w"> </span><span class="mi">83</span><span class="no">c002</span><span class="w"> </span><span class="no">add</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x0040152a</span><span class="w"> </span><span class="no">c60061</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">byte</span><span class="w"> </span><span class="p">[</span><span class="no">eax</span><span class="p">],</span><span class="w"> </span><span class="mi">0x61</span><span class="w"> </span><span class="c1">; 'a'</span>
<span class="err">│</span><span class="w"> </span><span class="c1">; [0x61:1]=255 ; 97</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x0040152d</span><span class="w"> </span><span class="mi">8</span><span class="no">b45f4</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">var_ch</span><span class="p">]</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401530</span><span class="w"> </span><span class="mi">83</span><span class="no">c003</span><span class="w"> </span><span class="no">add</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span>
<span class="err">│</span><span class="w"> </span><span class="err">0</span><span class="nf">x00401533</span><span class="w"> </span><span class="no">c60067</span><span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">byte</span><span class="w"> </span><span class="p">[</span><span class="no">eax</span><span class="p">],</span><span class="w"> </span><span class="mi">0x67</span><span class="w"> </span><span class="c1">; 'g'</span>
<span class="err">│</span><span class="w"> </span><span class="c1">; [0x67:1]=255 ; 103</span>
<span class="na">...</span>
<span class="na">...</span>
</code></pre></div>
<p>It will add all chars of a flag to the array and print them. So let's take a look at solution 1. We need to have r2pipe installed (<code>pip install r2pipe</code>).</p>
<div class="highlight"><pre><span></span><code><span class="ch">#!/usr/bin/env python3</span>
<span class="kn">import</span> <span class="nn">r2pipe</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">r2pipe</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"Untitled1.exe"</span><span class="p">)</span>
<span class="c1"># 'analyze all" (all symbols and entry-points)</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s2">"aa"</span><span class="p">)</span>
<span class="c1"># seek to symbol 'flag'</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s2">"s sym.flag"</span><span class="p">)</span>
<span class="c1"># print dissembly of function</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s2">"pdf"</span><span class="p">)</span>
<span class="c1"># configure radare2 search engine to search in given range</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s2">"e search.from = 0x00401500"</span><span class="p">)</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s2">"e search.to = 0x0040162b"</span><span class="p">)</span>
<span class="c1"># search hexadecimal value</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s2">"/x c600"</span><span class="p">)</span>
<span class="n">flag</span> <span class="o">=</span> <span class="nb">bytearray</span><span class="p">()</span>
<span class="c1"># Print hex px, </span>
<span class="c1"># j suffix can be used in most of the radare2 commands to get a JSON output</span>
<span class="c1"># pxj 3 - print 3 hex bytes in JSON format</span>
<span class="c1"># @@ used for looping</span>
<span class="k">for</span> <span class="n">byte_triplet</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s2">"pxj 3 @@hit0"</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">'</span><span class="se">\n</span><span class="s1">'</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">byte_triplet</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="n">byte_triplet</span><span class="p">)</span>
<span class="n">flag</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">byte_triplet</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
<span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="k">pass</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"FLAG: </span><span class="si">{</span><span class="n">flag</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
</code></pre></div>
<p>We will set search range to function we want to analyse and then search for all move eax instructions (<code>c600xx move eax, xx</code>) and extract byte being moved.</p>
<p>Solution 2, we can patch <code>cmp</code> instruction so that the program will print the flag for us.</p>
<div class="highlight"><pre><span></span><code><span class="ch">#!/usr/bin/env python3</span>
<span class="kn">import</span> <span class="nn">r2pipe</span>
<span class="kn">import</span> <span class="nn">subprocess</span>
<span class="n">subprocess</span><span class="o">.</span><span class="n">call</span><span class="p">([</span><span class="s2">"cp"</span><span class="p">,</span> <span class="s2">"./Untitled1.exe"</span><span class="p">,</span> <span class="s2">"Cracked.exe"</span><span class="p">])</span>
<span class="c1"># Open file in write mode</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">r2pipe</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"Cracked.exe"</span><span class="p">,</span> <span class="p">[</span><span class="s1">'-w'</span><span class="p">])</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s1">'aa'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">print_addr_content</span><span class="p">(</span><span class="n">addr</span><span class="p">):</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="sa">f</span><span class="s2">"s </span><span class="si">{</span><span class="n">addr</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Current disas of line at </span><span class="si">{</span><span class="n">addr</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s1">'pd 1 ~[3-6]'</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Current bytecode of line at </span><span class="si">{</span><span class="n">addr</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s1">'pd 1 ~[2]'</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="n">out</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">hex</span><span class="p">,</span> <span class="nb">eval</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s2">"pxj 6"</span><span class="p">))))</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Values at </span><span class="si">{</span><span class="n">addr</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">out</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="sa">f</span><span class="s2">"s -"</span><span class="p">)</span>
<span class="n">print_addr_content</span><span class="p">(</span><span class="mh">0x00401642</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"[!] PATCHING BINARY"</span><span class="p">)</span>
<span class="c1"># seek to address</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s2">"s 0x00401642+4"</span><span class="p">)</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s2">"px 1"</span><span class="p">)</span>
<span class="c1"># w1 write byte</span>
<span class="c1"># - decrement</span>
<span class="c1"># 1 (how many bytes to decrement)</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s2">"w1- 1"</span><span class="p">)</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s2">"px 1"</span><span class="p">)</span>
<span class="n">r</span><span class="o">.</span><span class="n">cmd</span><span class="p">(</span><span class="s2">"s 0x00401642"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"[!] BINARY PATCHED"</span><span class="p">)</span>
<span class="n">print_addr_content</span><span class="p">(</span><span class="mh">0x00401642</span><span class="p">)</span>
</code></pre></div>
<p>After this, we can simply run the patched binary to get the flag.
Both solutions are really nice way to get more familiar with radare2 scripting.</p>
</div>
<footer class="text-right">
<p>- F3real</p>
</footer>
<div id="show-comments" class="span7 text-center">
<a href="https://f3real.github.io/crackmes_one1.html#disqus_thread"
data-disqus-identifier="crackmes_one1"
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 = 'crackmes_one1';
var disqus_url = 'https://f3real.github.io/crackmes_one1.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>