|
| 1 | +# -*- coding:binary -*- |
| 2 | +require 'spec_helper' |
| 3 | + |
| 4 | +require 'rex/java/serialization' |
| 5 | +require 'rex/proto/rmi' |
| 6 | +require 'msf/java/rmi/client' |
| 7 | +require 'stringio' |
| 8 | + |
| 9 | +describe Msf::Java::Rmi::Client::Registry do |
| 10 | + |
| 11 | + let(:list_with_names_response) do |
| 12 | + "\x51\xac\xed\x00\x05\x77\x0f\x01\x82\x73\x92\x35\x00\x00\x01\x4c" + |
| 13 | + "\x48\x27\x84\x49\x80\xb9\x75\x72\x00\x13\x5b\x4c\x6a\x61\x76\x61" + |
| 14 | + "\x2e\x6c\x61\x6e\x67\x2e\x53\x74\x72\x69\x6e\x67\x3b\xad\xd2\x56" + |
| 15 | + "\xe7\xe9\x1d\x7b\x47\x02\x00\x00\x70\x78\x70\x00\x00\x00\x01\x74" + |
| 16 | + "\x00\x06\x6a\x6d\x78\x72\x6d\x69" |
| 17 | + end |
| 18 | + |
| 19 | + let(:list_empty_response) do |
| 20 | + "\x51\xac\xed\x00\x05\x77\x0f\x01\xbb\x2e\x19\xae\x00\x00\x01\x4c" + |
| 21 | + "\x32\xa9\x92\x56\x80\x04\x75\x72\x00\x13\x5b\x4c\x6a\x61\x76\x61" + |
| 22 | + "\x2e\x6c\x61\x6e\x67\x2e\x53\x74\x72\x69\x6e\x67\x3b\xad\xd2\x56" + |
| 23 | + "\xe7\xe9\x1d\x7b\x47\x02\x00\x00\x70\x78\x70\x00\x00\x00\x00" |
| 24 | + end |
| 25 | + |
| 26 | + let(:lookup_response) do |
| 27 | + "\x51\xac\xed\x00\x05\x77\x0f\x01\x82\x73\x92\x35\x00\x00\x01\x4c" + |
| 28 | + "\x48\x27\x84\x49\x80\xba\x73\x72\x00\x2e\x6a\x61\x76\x61\x78\x2e" + |
| 29 | + "\x6d\x61\x6e\x61\x67\x65\x6d\x65\x6e\x74\x2e\x72\x65\x6d\x6f\x74" + |
| 30 | + "\x65\x2e\x72\x6d\x69\x2e\x52\x4d\x49\x53\x65\x72\x76\x65\x72\x49" + |
| 31 | + "\x6d\x70\x6c\x5f\x53\x74\x75\x62\x00\x00\x00\x00\x00\x00\x00\x02" + |
| 32 | + "\x02\x00\x00\x70\x78\x72\x00\x1a\x6a\x61\x76\x61\x2e\x72\x6d\x69" + |
| 33 | + "\x2e\x73\x65\x72\x76\x65\x72\x2e\x52\x65\x6d\x6f\x74\x65\x53\x74" + |
| 34 | + "\x75\x62\xe9\xfe\xdc\xc9\x8b\xe1\x65\x1a\x02\x00\x00\x70\x78\x72" + |
| 35 | + "\x00\x1c\x6a\x61\x76\x61\x2e\x72\x6d\x69\x2e\x73\x65\x72\x76\x65" + |
| 36 | + "\x72\x2e\x52\x65\x6d\x6f\x74\x65\x4f\x62\x6a\x65\x63\x74\xd3\x61" + |
| 37 | + "\xb4\x91\x0c\x61\x33\x1e\x03\x00\x00\x70\x78\x70\x77\x37\x00\x0a" + |
| 38 | + "\x55\x6e\x69\x63\x61\x73\x74\x52\x65\x66\x00\x0e\x31\x37\x32\x2e" + |
| 39 | + "\x31\x36\x2e\x31\x35\x38\x2e\x31\x33\x32\x00\x00\x13\x26\xa0\x59" + |
| 40 | + "\x9d\x0d\x09\xd3\x01\xbd\x82\x73\x92\x35\x00\x00\x01\x4c\x48\x27" + |
| 41 | + "\x84\x49\x80\x01\x01\x78" |
| 42 | + end |
| 43 | + |
| 44 | + let(:lookup_exception) do |
| 45 | + "\x51\xac\xed\x00\x05\x77\x0f\x02\x82\x73\x92\x35\x00\x00\x01\x4c" + |
| 46 | + "\x48\x27\x84\x49\x80\xbc\x73\x72\x00\x1a\x6a\x61\x76\x61\x2e\x72" + |
| 47 | + "\x6d\x69\x2e\x4e\x6f\x74\x42\x6f\x75\x6e\x64\x45\x78\x63\x65\x70" + |
| 48 | + "\x74\x69\x6f\x6e\xe6\x37\xf9\xa7\x2d\x7c\x3a\xfb\x02\x00\x00\x70" + |
| 49 | + "\x78\x72\x00\x13\x6a\x61\x76\x61\x2e\x6c\x61\x6e\x67\x2e\x45\x78" + |
| 50 | + "\x63\x65\x70\x74\x69\x6f\x6e\xd0\xfd\x1f\x3e\x1a\x3b\x1c\xc4\x02" + |
| 51 | + "\x00\x00\x70\x78\x72\x00\x13\x6a\x61\x76\x61\x2e\x6c\x61\x6e\x67" + |
| 52 | + "\x2e\x54\x68\x72\x6f\x77\x61\x62\x6c\x65\xd5\xc6\x35\x27\x39\x77" + |
| 53 | + "\xb8\xcb\x03\x00\x04\x4c\x00\x05\x63\x61\x75\x73\x65\x74\x00\x15" + |
| 54 | + "\x4c\x6a\x61\x76\x61\x2f\x6c\x61\x6e\x67\x2f\x54\x68\x72\x6f\x77" + |
| 55 | + "\x61\x62\x6c\x65\x3b\x4c\x00\x0d\x64\x65\x74\x61\x69\x6c\x4d\x65" + |
| 56 | + "\x73\x73\x61\x67\x65\x74\x00\x12\x4c\x6a\x61\x76\x61\x2f\x6c\x61" + |
| 57 | + "\x6e\x67\x2f\x53\x74\x72\x69\x6e\x67\x3b\x5b\x00\x0a\x73\x74\x61" + |
| 58 | + "\x63\x6b\x54\x72\x61\x63\x65\x74\x00\x1e\x5b\x4c\x6a\x61\x76\x61" + |
| 59 | + "\x2f\x6c\x61\x6e\x67\x2f\x53\x74\x61\x63\x6b\x54\x72\x61\x63\x65" + |
| 60 | + "\x45\x6c\x65\x6d\x65\x6e\x74\x3b\x4c\x00\x14\x73\x75\x70\x70\x72" + |
| 61 | + "\x65\x73\x73\x65\x64\x45\x78\x63\x65\x70\x74\x69\x6f\x6e\x73\x74" + |
| 62 | + "\x00\x10\x4c\x6a\x61\x76\x61\x2f\x75\x74\x69\x6c\x2f\x4c\x69\x73" + |
| 63 | + "\x74\x3b\x70\x78\x70\x71\x00\x7e\x00\x07\x74\x00\x2f\x4e\x6f\x74" + |
| 64 | + "\x20\x62\x6f\x75\x6e\x64\x3a\x20\x22\x74\x65\x73\x74\x22\x20\x28" + |
| 65 | + "\x6f\x6e\x6c\x79\x20\x62\x6f\x75\x6e\x64\x20\x6e\x61\x6d\x65\x20" + |
| 66 | + "\x69\x73\x20\x22\x6a\x6d\x78\x72\x6d\x69\x22\x29\x75\x72\x00\x1e" + |
| 67 | + "\x5b\x4c\x6a\x61\x76\x61\x2e\x6c\x61\x6e\x67\x2e\x53\x74\x61\x63" + |
| 68 | + "\x6b\x54\x72\x61\x63\x65\x45\x6c\x65\x6d\x65\x6e\x74\x3b\x02\x46" + |
| 69 | + "\x2a\x3c\x3c\xfd\x22\x39\x02\x00\x00\x70\x78\x70\x00\x00\x00\x0e" + |
| 70 | + "\x73\x72\x00\x1b\x6a\x61\x76\x61\x2e\x6c\x61\x6e\x67\x2e\x53\x74" + |
| 71 | + "\x61\x63\x6b\x54\x72\x61\x63\x65\x45\x6c\x65\x6d\x65\x6e\x74\x61" + |
| 72 | + "\x09\xc5\x9a\x26\x36\xdd\x85\x02\x00\x04\x49\x00\x0a\x6c\x69\x6e" + |
| 73 | + "\x65\x4e\x75\x6d\x62\x65\x72\x4c\x00\x0e\x64\x65\x63\x6c\x61\x72" + |
| 74 | + "\x69\x6e\x67\x43\x6c\x61\x73\x73\x71\x00\x7e\x00\x04\x4c\x00\x08" + |
| 75 | + "\x66\x69\x6c\x65\x4e\x61\x6d\x65\x71\x00\x7e\x00\x04\x4c\x00\x0a" + |
| 76 | + "\x6d\x65\x74\x68\x6f\x64\x4e\x61\x6d\x65\x71\x00\x7e\x00\x04\x70" + |
| 77 | + "\x78\x70\xff\xff\xff\xff\x74\x00\x2c\x73\x75\x6e\x2e\x6d\x61\x6e" + |
| 78 | + "\x61\x67\x65\x6d\x65\x6e\x74\x2e\x6a\x6d\x78\x72\x65\x6d\x6f\x74" + |
| 79 | + "\x65\x2e\x53\x69\x6e\x67\x6c\x65\x45\x6e\x74\x72\x79\x52\x65\x67" + |
| 80 | + "\x69\x73\x74\x72\x79\x70\x74\x00\x06\x6c\x6f\x6f\x6b\x75\x70\x73" + |
| 81 | + "\x71\x00\x7e\x00\x0b\xff\xff\xff\xff\x74\x00\x22\x73\x75\x6e\x2e" + |
| 82 | + "\x72\x6d\x69\x2e\x72\x65\x67\x69\x73\x74\x72\x79\x2e\x52\x65\x67" + |
| 83 | + "\x69\x73\x74\x72\x79\x49\x6d\x70\x6c\x5f\x53\x6b\x65\x6c\x70\x74" + |
| 84 | + "\x00\x08\x64\x69\x73\x70\x61\x74\x63\x68\x73\x71\x00\x7e\x00\x0b" + |
| 85 | + "\xff\xff\xff\xff\x74\x00\x1f\x73\x75\x6e\x2e\x72\x6d\x69\x2e\x73" + |
| 86 | + "\x65\x72\x76\x65\x72\x2e\x55\x6e\x69\x63\x61\x73\x74\x53\x65\x72" + |
| 87 | + "\x76\x65\x72\x52\x65\x66\x70\x74\x00\x0b\x6f\x6c\x64\x44\x69\x73" + |
| 88 | + "\x70\x61\x74\x63\x68\x73\x71\x00\x7e\x00\x0b\xff\xff\xff\xff\x71" + |
| 89 | + "\x00\x7e\x00\x13\x70\x71\x00\x7e\x00\x11\x73\x71\x00\x7e\x00\x0b" + |
| 90 | + "\xff\xff\xff\xff\x74\x00\x1d\x73\x75\x6e\x2e\x72\x6d\x69\x2e\x74" + |
| 91 | + "\x72\x61\x6e\x73\x70\x6f\x72\x74\x2e\x54\x72\x61\x6e\x73\x70\x6f" + |
| 92 | + "\x72\x74\x24\x31\x70\x74\x00\x03\x72\x75\x6e\x73\x71\x00\x7e\x00" + |
| 93 | + "\x0b\xff\xff\xff\xff\x71\x00\x7e\x00\x17\x70\x71\x00\x7e\x00\x18" + |
| 94 | + "\x73\x71\x00\x7e\x00\x0b\xff\xff\xff\xfe\x74\x00\x1e\x6a\x61\x76" + |
| 95 | + "\x61\x2e\x73\x65\x63\x75\x72\x69\x74\x79\x2e\x41\x63\x63\x65\x73" + |
| 96 | + "\x73\x43\x6f\x6e\x74\x72\x6f\x6c\x6c\x65\x72\x70\x74\x00\x0c\x64" + |
| 97 | + "\x6f\x50\x72\x69\x76\x69\x6c\x65\x67\x65\x64\x73\x71\x00\x7e\x00" + |
| 98 | + "\x0b\xff\xff\xff\xff\x74\x00\x1b\x73\x75\x6e\x2e\x72\x6d\x69\x2e" + |
| 99 | + "\x74\x72\x61\x6e\x73\x70\x6f\x72\x74\x2e\x54\x72\x61\x6e\x73\x70" + |
| 100 | + "\x6f\x72\x74\x70\x74\x00\x0b\x73\x65\x72\x76\x69\x63\x65\x43\x61" + |
| 101 | + "\x6c\x6c\x73\x71\x00\x7e\x00\x0b\xff\xff\xff\xff\x74\x00\x22\x73" + |
| 102 | + "\x75\x6e\x2e\x72\x6d\x69\x2e\x74\x72\x61\x6e\x73\x70\x6f\x72\x74" + |
| 103 | + "\x2e\x74\x63\x70\x2e\x54\x43\x50\x54\x72\x61\x6e\x73\x70\x6f\x72" + |
| 104 | + "\x74\x70\x74\x00\x0e\x68\x61\x6e\x64\x6c\x65\x4d\x65\x73\x73\x61" + |
| 105 | + "\x67\x65\x73\x73\x71\x00\x7e\x00\x0b\xff\xff\xff\xff\x74\x00\x34" + |
| 106 | + "\x73\x75\x6e\x2e\x72\x6d\x69\x2e\x74\x72\x61\x6e\x73\x70\x6f\x72" + |
| 107 | + "\x74\x2e\x74\x63\x70\x2e\x54\x43\x50\x54\x72\x61\x6e\x73\x70\x6f" + |
| 108 | + "\x72\x74\x24\x43\x6f\x6e\x6e\x65\x63\x74\x69\x6f\x6e\x48\x61\x6e" + |
| 109 | + "\x64\x6c\x65\x72\x70\x74\x00\x04\x72\x75\x6e\x30\x73\x71\x00\x7e" + |
| 110 | + "\x00\x0b\xff\xff\xff\xff\x71\x00\x7e\x00\x24\x70\x71\x00\x7e\x00" + |
| 111 | + "\x18\x73\x71\x00\x7e\x00\x0b\xff\xff\xff\xff\x74\x00\x27\x6a\x61" + |
| 112 | + "\x76\x61\x2e\x75\x74\x69\x6c\x2e\x63\x6f\x6e\x63\x75\x72\x72\x65" + |
| 113 | + "\x6e\x74\x2e\x54\x68\x72\x65\x61\x64\x50\x6f\x6f\x6c\x45\x78\x65" + |
| 114 | + "\x63\x75\x74\x6f\x72\x70\x74\x00\x09\x72\x75\x6e\x57\x6f\x72\x6b" + |
| 115 | + "\x65\x72\x73\x71\x00\x7e\x00\x0b\xff\xff\xff\xff\x74\x00\x2e\x6a" + |
| 116 | + "\x61\x76\x61\x2e\x75\x74\x69\x6c\x2e\x63\x6f\x6e\x63\x75\x72\x72" + |
| 117 | + "\x65\x6e\x74\x2e\x54\x68\x72\x65\x61\x64\x50\x6f\x6f\x6c\x45\x78" + |
| 118 | + "\x65\x63\x75\x74\x6f\x72\x24\x57\x6f\x72\x6b\x65\x72\x70\x71\x00" + |
| 119 | + "\x7e\x00\x18\x73\x71\x00\x7e\x00\x0b\xff\xff\xff\xff\x74\x00\x10" + |
| 120 | + "\x6a\x61\x76\x61\x2e\x6c\x61\x6e\x67\x2e\x54\x68\x72\x65\x61\x64" + |
| 121 | + "\x70\x71\x00\x7e\x00\x18\x73\x72\x00\x26\x6a\x61\x76\x61\x2e\x75" + |
| 122 | + "\x74\x69\x6c\x2e\x43\x6f\x6c\x6c\x65\x63\x74\x69\x6f\x6e\x73\x24" + |
| 123 | + "\x55\x6e\x6d\x6f\x64\x69\x66\x69\x61\x62\x6c\x65\x4c\x69\x73\x74" + |
| 124 | + "\xfc\x0f\x25\x31\xb5\xec\x8e\x10\x02\x00\x01\x4c\x00\x04\x6c\x69" + |
| 125 | + "\x73\x74\x71\x00\x7e\x00\x06\x70\x78\x72\x00\x2c\x6a\x61\x76\x61" + |
| 126 | + "\x2e\x75\x74\x69\x6c\x2e\x43\x6f\x6c\x6c\x65\x63\x74\x69\x6f\x6e" + |
| 127 | + "\x73\x24\x55\x6e\x6d\x6f\x64\x69\x66\x69\x61\x62\x6c\x65\x43\x6f" + |
| 128 | + "\x6c\x6c\x65\x63\x74\x69\x6f\x6e\x19\x42\x00\x80\xcb\x5e\xf7\x1e" + |
| 129 | + "\x02\x00\x01\x4c\x00\x01\x63\x74\x00\x16\x4c\x6a\x61\x76\x61\x2f" + |
| 130 | + "\x75\x74\x69\x6c\x2f\x43\x6f\x6c\x6c\x65\x63\x74\x69\x6f\x6e\x3b" + |
| 131 | + "\x70\x78\x70\x73\x72\x00\x13\x6a\x61\x76\x61\x2e\x75\x74\x69\x6c" + |
| 132 | + "\x2e\x41\x72\x72\x61\x79\x4c\x69\x73\x74\x78\x81\xd2\x1d\x99\xc7" + |
| 133 | + "\x61\x9d\x03\x00\x01\x49\x00\x04\x73\x69\x7a\x65\x70\x78\x70\x00" + |
| 134 | + "\x00\x00\x00\x77\x04\x00\x00\x00\x00\x78\x71\x00\x7e\x00\x33\x78" |
| 135 | + end |
| 136 | + |
| 137 | + let(:name) do |
| 138 | + 'jmxrmi' |
| 139 | + end |
| 140 | + |
| 141 | + let(:interface_class) do |
| 142 | + 'javax.management.remote.rmi.RMIServerImpl_Stub' |
| 143 | + end |
| 144 | + |
| 145 | + subject(:mod) do |
| 146 | + mod = ::Msf::Exploit.new |
| 147 | + mod.extend ::Msf::Java::Rmi::Client |
| 148 | + mod.send(:initialize) |
| 149 | + mod |
| 150 | + end |
| 151 | + |
| 152 | + let(:io) { StringIO.new('', 'w+b') } |
| 153 | + |
| 154 | + describe "#send_registry_list" do |
| 155 | + context "when there aren't names registered" do |
| 156 | + before(:each) do |
| 157 | + allow_any_instance_of(::StringIO).to receive(:put) do |io, data| |
| 158 | + io.seek(0) |
| 159 | + io.write(list_empty_response) |
| 160 | + io.seek(0) |
| 161 | + end |
| 162 | + |
| 163 | + allow_any_instance_of(::StringIO).to receive(:get_once) do |io, length, timeout| |
| 164 | + io.read |
| 165 | + end |
| 166 | + end |
| 167 | + |
| 168 | + it "returns empty array" do |
| 169 | + expect(mod.send_registry_list(sock: io)).to eq([]) |
| 170 | + end |
| 171 | + end |
| 172 | + |
| 173 | + context "when there are names registered" do |
| 174 | + before(:each) do |
| 175 | + allow_any_instance_of(::StringIO).to receive(:put) do |io, data| |
| 176 | + io.seek(0) |
| 177 | + io.write(list_with_names_response) |
| 178 | + io.seek(0) |
| 179 | + end |
| 180 | + |
| 181 | + allow_any_instance_of(::StringIO).to receive(:get_once) do |io, length, timeout| |
| 182 | + io.read |
| 183 | + end |
| 184 | + end |
| 185 | + |
| 186 | + it "returns the list of registered names" do |
| 187 | + expect(mod.send_registry_list(sock: io)).to eq([name]) |
| 188 | + end |
| 189 | + end |
| 190 | + |
| 191 | + end |
| 192 | + |
| 193 | + describe "#send_registry_lookup" do |
| 194 | + context "when there isn't an interface bound" do |
| 195 | + before(:each) do |
| 196 | + allow_any_instance_of(::StringIO).to receive(:put) do |io, data| |
| 197 | + io.seek(0) |
| 198 | + io.write(lookup_exception) |
| 199 | + io.seek(0) |
| 200 | + end |
| 201 | + |
| 202 | + allow_any_instance_of(::StringIO).to receive(:get_once) do |io, length, timeout| |
| 203 | + io.read |
| 204 | + end |
| 205 | + end |
| 206 | + |
| 207 | + it "raises an Rex::Proto::Rmi::Exception" do |
| 208 | + expect { mod.send_registry_lookup(sock: io, name: 'test') }.to raise_error(Rex::Proto::Rmi::Exception) |
| 209 | + end |
| 210 | + end |
| 211 | + |
| 212 | + context "when there is an interface bound" do |
| 213 | + before(:each) do |
| 214 | + allow_any_instance_of(::StringIO).to receive(:put) do |io, data| |
| 215 | + io.seek(0) |
| 216 | + io.write(lookup_response) |
| 217 | + io.seek(0) |
| 218 | + end |
| 219 | + |
| 220 | + allow_any_instance_of(::StringIO).to receive(:get_once) do |io, length, timeout| |
| 221 | + io.read |
| 222 | + end |
| 223 | + end |
| 224 | + |
| 225 | + it "returns the reference information" do |
| 226 | + expect(mod.send_registry_lookup(sock: io, name: name)[:object]).to eq(interface_class) |
| 227 | + end |
| 228 | + end |
| 229 | + end |
| 230 | +end |
| 231 | + |
0 commit comments