Skip to content

Commit 25d14a1

Browse files
committed
Updated the java serialization post
1 parent 475ea07 commit 25d14a1

File tree

8 files changed

+66
-10
lines changed

8 files changed

+66
-10
lines changed

_config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ title: Greyshell's Diary # the main title
1919
tagline: Slip through the cracks # it will display as the subtitle
2020

2121
description: >- # used by seo meta and the atom feed
22-
A minimal, responsive and feature-rich Jekyll theme for technical writing.
22+
Technical write up on information security
2323
2424
# Fill in the protocol & hostname for your site.
2525
# E.g. 'https://username.github.io', note that it does not end with a '/'.
@@ -59,7 +59,7 @@ webmaster_verifications:
5959
# Web Analytics Settings
6060
analytics:
6161
google:
62-
id: # fill in your Google Analytics ID
62+
id: G-YL9S076DP2 # fill in your Google Analytics ID
6363
goatcounter:
6464
id: # fill in your GoatCounter ID
6565
umami:

_posts/2019-11-22-insecure_deserialization_java.md

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ The process of `re-creating` the actual object in memory from byte stream is cal
4747

4848
- [x] Some objects may be required to implement `Serializable` due to inheritance for example `SuperUser`. It inherites the base class `User` that implements `Serializable`.
4949

50-
To ensure that such objects (e.g., `SuperUser`) cannot be deserialized, we can override the `readObject()` method and mark it as final to throw an exception during the deserialization process.
50+
To ensure that such objects (e.g., `SuperUser`) cannot be deserialized, we can override the `readObject()` method and mark it as `final` to throw an exception during the deserialization process.
5151

5252
![stop_deserialization_using_final](assets/2019-11-22-insecure_deserialization_java.assets/stop_deserialization_using_final.png)
5353

5454
## The Bug
5555

56-
1. The readObject method of `java.io.ObjectInputStream` is vulnerable.
56+
1. The `readObject()` method of `java.io.ObjectInputStream` is vulnerable.
5757

58-
2. During the Deserialization process, the `readObject()` method is always being called, and it can construct any sort of Serializable object that can be found on the Java classpath before passing it back to the caller for the type_check.
58+
2. During the Deserialization process, the `readObject()` method is always being called, and it can construct any sort of Serializable object that can be found on the Java classpath before passing it back to the caller for the type check.
5959

6060
3. An Exception occurs only when there’s a type mismatch between the returned object and the expected object. If the constructed object performs any harmful actions during its construction, it’s already too late to prevent them by the time type checking.
6161

@@ -85,17 +85,73 @@ From a Whitebox perspective
8585

8686
## How to Exploit
8787

88-
### Denial of Service
88+
### Perform Denial of Service
8989

90-
1. Generate a malicious serialized object.
90+
1. Generate a malicious serialized object using `DoSExploit.java`.
9191

9292
2. During deserialization, when the application attempts to reconstruct the object in memory, it consumes 100% of the CPU resources.
9393

9494
![dos_deserialization](assets/2019-11-22-insecure_deserialization_java.assets/dos_deserialization.png)
9595

96-
### Remote Code Execution
96+
### Execute Remote Code
97+
98+
#### Leveraging `ysoserial`
99+
100+
- Generate the rce payload to open `gnome-calculator` using the latest [ysoserial](https://github.com/frohoff/ysoserial).
101+
102+
```bash
103+
java -jar ysoserial-all.jar CommonsCollections7 gnome-calculator > bad_serialized_object_ysoserial.ser
104+
```
105+
106+
- [x] The modern Java Security Manager by default includes protections against unsafe deserialization by blocking blacklisted gadgets. Therefore, `disabling` that feature in code by adding the following line in `DemoDeserilization.java`
107+
108+
```java
109+
// in current Java, by default enableUnsafeSerialization is set to 'false'
110+
System.setProperty(
111+
"org.apache.commons.collections.enableUnsafeSerialization",
112+
"true");
113+
```
114+
115+
During deserialization, when the application tries to reconstruct the object in memory, it launches the calculator.
116+
117+
![rce_ysoserial](assets/2019-11-22-insecure_deserialization_java.assets/rce_ysoserial.png)
118+
119+
120+
#### Handcraft the payload
121+
122+
> I’ve put together a detailed blog [post](https://greyshell.github.io/posts/demystify_java_gadget_chain/) on how to create the entire RCE gadget chain from scratch.
123+
{: .prompt-tip }
124+
125+
- Set up the exploit dev environment
126+
- JDK version: `openjdk-23`
127+
- Add `commons-collectios-3.2.2.jar`, `commons-lang3-3.7.jar` and `mockito-all-1.9.5.jar` into the Java classpath.
128+
129+
![add_lib](assets/2019-11-22-insecure_deserialization_java.assets/add_lib.png)
130+
131+
132+
- Download the exploit code - [RCE.java](https://github.com/greyshell/java_insecure_deserialization/blob/main/src/RCE.java).
133+
134+
> Java’s **strong encapsulation** introduced in **Java 9+**, which restricts reflective access to certain internal Java classes and fields by default. This is especially relevant when using libraries or tools that attempt to access private or internal fields of classes like `HashMap`.
135+
{: .prompt-danger }
136+
137+
- [x] By adding the `--add-opens` option, we can explicitly open the necessary package (`java.util`) for reflection. In IntelliJ IDEA -> Run -> Edit Configurations -> In **VM options** field, add the following
138+
139+
```text
140+
--add-opens java.base/java.util=ALL-UNNAMED
141+
```
142+
143+
![add_vm_options2](assets/2019-11-22-insecure_deserialization_java.assets/add_vm_options.png)
144+
145+
- Execute `RCE.java` and generate the `rce_serialized_object`.
146+
147+
![crete_rce_object](assets/2019-11-22-insecure_deserialization_java.assets/create_rce_object.png)
148+
149+
During deserialization, when the application attempts to reconstruct the object in memory, it opens the calculator.
150+
151+
![rce_calculator](assets/2019-11-22-insecure_deserialization_java.assets/rce_calculator.png)
152+
153+
97154

98-
TBD
99155

100156
## How to Mitigate
101157

_tabs/archives.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
22
layout: archives
33
icon: fas fa-archive
4-
order: 3
4+
order: 1
55
---
139 KB
Loading
1.29 MB
Loading
947 KB
Loading
1.27 MB
Loading
843 KB
Loading

0 commit comments

Comments
 (0)