Skip to content

Commit c99ce6c

Browse files
committed
site check
1 parent bca5dfc commit c99ce6c

File tree

5 files changed

+148
-12
lines changed

5 files changed

+148
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ _sass/dist
2727
assets/js/dist
2828

2929
.ruby-version
30+
31+
.DS_Store

HOWTO.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# How to manage
2+
3+
## start the site at localhost
4+
5+
- navigate to the greyshell.github.io folder
6+
- type `bundle exec jekyll s` and access the site at http://127.0.0.1:4000/
7+
8+
## how to post
9+
10+
- copy an existing post from _post folder as template.
11+
- save the post related assets inside assets folder. create a folder with the same filename as post.
12+
- for pasting the clipboard data use `pngpaste image_name.png`.
13+
14+
## how to publish
15+
16+
- git commit -am "publish post"
17+
- git push
18+
- check the site at https://greyshell.github.io/

_config.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ github:
2929
username: greyshell # change to your GitHub username
3030

3131
twitter:
32-
username: twitter_username # change to your Twitter username
32+
username: greyshell__ # change to your Twitter username
3333

3434
social:
3535
# Change to your full name.
3636
# It will be displayed as the default author of the posts and the copyright owner in the Footer
3737
name: Abhijit Sinha
38-
email: example@domain.com # change to your email address
38+
email: grey.shell@gmail.com # change to your email address
3939
links:
4040
# The first element serves as the copyright owner's link
4141
- https://twitter.com/greyshell__ # change to your Twitter homepage
@@ -88,7 +88,7 @@ pageviews:
8888
# light — Use the light color scheme
8989
# dark — Use the dark color scheme
9090
#
91-
theme_mode: # [light | dark]
91+
theme_mode: dark # [light | dark]
9292

9393
# The CDN endpoint for media resources.
9494
# Notice that once it is assigned, the CDN url
@@ -98,7 +98,7 @@ theme_mode: # [light | dark]
9898
cdn:
9999

100100
# the avatar on sidebar, support local or CORS resources
101-
avatar:
101+
avatar: assets/hat_man.png
102102

103103
# The URL of the site-wide social preview image used in SEO `og:image` meta tag.
104104
# It can be overridden by a customized `page.image` in front matter.

_posts/2019-11-22-insecure_deserialization_java.md

Lines changed: 121 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,135 @@ categories: [web_security]
55
tags: [java, insecure_deserialization] # TAG names should always be lowercase
66
---
77

8-
I was always curious about how the actual remote code execution occurs during the Insecure Deserialization process. So I thought of giving a try to understand the known harmful `gadgets` from `commons-collections-3.2.2.jar` and develop the entire chain from scratch.
8+
## Concept of Serialization
99

10-
<!-- more -->
11-
12-
## Serialization
13-
14-
The process of converting the `state` of object into stream of bytes is called `serialization`.
10+
The process of converting the `state` of object into stream of bytes is called serialization.
1511

1612
The purpose of serialization is to save the object’s state to the file system or transmit it over the network for future use.
1713

18-
### Serialization in Java
14+
### In the context of Java
1915

2016
> - `Serializable` is a `marker interface`.
2117
> - It has no `data member` and `method`.
2218
> - It is only used to `mark` java classes so that objects of these type of classes may get a certain `capability`.
19+
{: .prompt-info }
2320

2421

2522
Create a `User` class and make it `serializable`.
23+
24+
Create an object from the `User` class and save it into the file system in `.ser` format.
25+
26+
![serialized_object](assets/2019-11-22-insecure_deserialization_java.assets/serialized_object.png)
27+
28+
## Concept of Deserialization
29+
30+
The process of `re-creating` the actual object in memory from byte stream is called de-serialization.
31+
32+
> The `User` class must be available in the Java classpath for deserialization to succeed.
33+
{: .prompt-warning }
34+
35+
36+
![deserialized_object](assets/2019-11-22-insecure_deserialization_java.assets/deserialized_object.png)
37+
38+
### Observations
39+
40+
- [x] For instance, if a serialized object is created using the `User` class but type checking during deserialization is performed with the `SuperUser` class, then application will throw a `ClassCastException`.
41+
42+
![consume_superclass](assets/2019-11-22-insecure_deserialization_java.assets/consume_superclass.png)
43+
44+
- [x] However, if a serialized object is created using the `SuperUser` class but type checking during deserialization is performed with the `User` class, the application will not throw any exception because `SuperUser` class is derived from the base class `User`.
45+
46+
![consume_baseclass](assets/2019-11-22-insecure_deserialization_java.assets/consume_baseclass.png)
47+
48+
- [x] Some objects may be required to implement `Serializable` due to inheritance for example `SuperUser`. It inherites the base class `User` that implements `Serializable`.
49+
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.
51+
52+
![stop_deserialization_using_final](assets/2019-11-22-insecure_deserialization_java.assets/stop_deserialization_using_final.png)
53+
54+
## The Bug
55+
56+
1. The readObject method of `java.io.ObjectInputStream` is vulnerable.
57+
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.
59+
60+
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.
61+
62+
63+
## How to Identify
64+
65+
From a Blackbox perspective
66+
1. Look for magic numbers like `AC ED 00 05` or `rO0A` (base64-encoded) in the request/response to identify if the application is handling a serialized object.
67+
68+
2. The `Content-Type` header in the HTTP response is set to `application/x-java-serialized-object`.
69+
70+
71+
From a Whitebox perspective
72+
1. Search the codebase for Java Serialization APIs such as `ObjectInputStream`, particularly instances of `readObject()` method, and analyze how `ObjectInputStream` is utilized.
73+
74+
2. Before calling `readObject()`, ensure the code checks for all expected classes from the serialized object using a `whitelist`.
75+
76+
77+
## What is the Impact
78+
79+
1. Remote code execution through `property-oriented programming` or gadget chaining.
80+
81+
2. Bypass authorization or escalate privileges via Insecure Direct Object Reference (IDOR) if the object’s signature / authenticity is not verified.
82+
83+
3. Denial of Service (DoS) attacks, such as exhausting heap memory, CPU cycle.
84+
85+
86+
## How to Exploit
87+
88+
### Denial of Service
89+
90+
1. Generate a malicious serialized object.
91+
92+
2. During deserialization, when the application attempts to reconstruct the object in memory, it consumes 100% of the CPU resources.
93+
94+
![dos_deserialization](assets/2019-11-22-insecure_deserialization_java.assets/dos_deserialization.png)
95+
96+
### Remote Code Execution
97+
98+
## How to Mitigate
99+
100+
1. Do not blindly accept serialized objects from untrusted sources. Implement integrity checks or sign the serialized objects to prevent tampering or the creation of malicious objects.
101+
102+
2. Use a whitelist approach to secure `java.io.ObjectInputStream`
103+
- Create a `HashSet` containing all expected classes wrapped in the object.
104+
- Extend `ObjectInputStream` to create a custom `SafeObjectInputStream` class.
105+
- Override the `resolveClass()` method to verify if `cls.getName()` exists in the `HashSet`, otherwise, throw an `InvalidClassException`.
106+
107+
When we provide any object other than `User` type, it throws exception.
108+
109+
![bad_object](assets/2019-11-22-insecure_deserialization_java.assets/bad_object.png)
110+
111+
112+
When we provide `User` type object, it does not throw any exception.
113+
114+
![good_object](assets/2019-11-22-insecure_deserialization_java.assets/good_object.png)
115+
116+
> A Denial of Service (DoS) is inevitable if the `expected` object type is a `HashSet`, `HashMap`, or `ArrayList`.
117+
{: .prompt-danger }
118+
119+
120+
121+
Defense in depth
122+
1. Use the `transient` keyword for sensitive fields that you do not want to be serialized. The `transient` keyword prevents a variable, like a password field, from being serialized. When the JVM encounters a variable marked as transient or `static`, it disregards its original value and instead saves the default value corresponding to that variable’s data type.
123+
124+
2. For detective controls, log any exceptions or failures that occur during the deserialization process.
125+
126+
127+
3. Use Java Security Manager to block specific classes such as `InvokerTransformer`.
128+
129+
```java
130+
// in current Java, by default enableUnsafeSerialization is set to 'false'
131+
System.setProperty(
132+
"org.apache.commons.collections.enableUnsafeSerialization",
133+
"false");
134+
```
135+
136+
137+
## Code Repo
138+
139+
https://github.com/greyshell/java_insecure_deserialization

_tabs/about.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ icon: fas fa-info-circle
44
order: 4
55
---
66

7-
> Add Markdown syntax content to file `_tabs/about.md`{: .filepath } and it will show up on this page.
7+
> The quieter you become the more you able to hear
88
{: .prompt-tip }
9+
10+
A passionate learner of `offensive` security

0 commit comments

Comments
 (0)