rex 2.0.5 → 2.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rex/exploitation/egghunter.rb +4 -6
  3. data/lib/rex/exploitation/powershell/psh_methods.rb +9 -0
  4. data/lib/rex/java/serialization.rb +2 -1
  5. data/lib/rex/java/serialization/builder.rb +94 -0
  6. data/lib/rex/java/serialization/model.rb +29 -18
  7. data/lib/rex/java/serialization/model/annotation.rb +2 -2
  8. data/lib/rex/java/serialization/model/field.rb +2 -2
  9. data/lib/rex/java/serialization/model/new_array.rb +8 -3
  10. data/lib/rex/java/serialization/model/new_class_desc.rb +3 -3
  11. data/lib/rex/java/serialization/model/new_enum.rb +4 -4
  12. data/lib/rex/java/serialization/model/new_object.rb +17 -10
  13. data/lib/rex/ole/direntry.rb +1 -1
  14. data/lib/rex/ole/samples/create_ole.rb +0 -0
  15. data/lib/rex/ole/samples/dir.rb +0 -0
  16. data/lib/rex/ole/samples/dump_stream.rb +0 -0
  17. data/lib/rex/ole/samples/ole_info.rb +0 -0
  18. data/lib/rex/parser/foundstone_nokogiri.rb +1 -1
  19. data/lib/rex/parser/fs/ntfs.rb +252 -0
  20. data/lib/rex/parser/openvas_nokogiri.rb +2 -0
  21. data/lib/rex/payloads/win32/kernel.rb +3 -3
  22. data/lib/rex/post/meterpreter/client_core.rb +172 -64
  23. data/lib/rex/post/meterpreter/extensions/priv/priv.rb +3 -2
  24. data/lib/rex/post/meterpreter/extensions/stdapi/fs/file.rb +12 -10
  25. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/api_constants.rb +64 -37
  26. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/dll.rb +8 -2
  27. data/lib/rex/post/meterpreter/extensions/stdapi/ui.rb +15 -3
  28. data/lib/rex/post/meterpreter/packet.rb +41 -38
  29. data/lib/rex/post/meterpreter/packet_dispatcher.rb +7 -1
  30. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/core.rb +17 -4
  31. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb +11 -4
  32. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/ui.rb +1 -1
  33. data/lib/rex/proto.rb +2 -0
  34. data/lib/rex/proto/acpp.rb +17 -0
  35. data/lib/rex/proto/acpp/client.rb +29 -0
  36. data/lib/rex/proto/acpp/message.rb +183 -0
  37. data/lib/rex/proto/http/client.rb +1 -2
  38. data/lib/rex/proto/iax2/call.rb +22 -3
  39. data/lib/rex/proto/iax2/client.rb +1 -0
  40. data/lib/rex/proto/kerberos.rb +13 -0
  41. data/lib/rex/proto/kerberos/client.rb +213 -0
  42. data/lib/rex/proto/kerberos/credential_cache.rb +19 -0
  43. data/lib/rex/proto/kerberos/credential_cache/cache.rb +81 -0
  44. data/lib/rex/proto/kerberos/credential_cache/credential.rb +151 -0
  45. data/lib/rex/proto/kerberos/credential_cache/element.rb +49 -0
  46. data/lib/rex/proto/kerberos/credential_cache/key_block.rb +62 -0
  47. data/lib/rex/proto/kerberos/credential_cache/principal.rb +70 -0
  48. data/lib/rex/proto/kerberos/credential_cache/time.rb +69 -0
  49. data/lib/rex/proto/kerberos/crypto.rb +21 -0
  50. data/lib/rex/proto/kerberos/crypto/rc4_hmac.rb +65 -0
  51. data/lib/rex/proto/kerberos/crypto/rsa_md5.rb +15 -0
  52. data/lib/rex/proto/kerberos/model.rb +133 -0
  53. data/lib/rex/proto/kerberos/model/ap_req.rb +98 -0
  54. data/lib/rex/proto/kerberos/model/authenticator.rb +143 -0
  55. data/lib/rex/proto/kerberos/model/authorization_data.rb +85 -0
  56. data/lib/rex/proto/kerberos/model/checksum.rb +59 -0
  57. data/lib/rex/proto/kerberos/model/element.rb +67 -0
  58. data/lib/rex/proto/kerberos/model/enc_kdc_response.rb +215 -0
  59. data/lib/rex/proto/kerberos/model/encrypted_data.rb +171 -0
  60. data/lib/rex/proto/kerberos/model/encryption_key.rb +106 -0
  61. data/lib/rex/proto/kerberos/model/kdc_request.rb +166 -0
  62. data/lib/rex/proto/kerberos/model/kdc_request_body.rb +315 -0
  63. data/lib/rex/proto/kerberos/model/kdc_response.rb +141 -0
  64. data/lib/rex/proto/kerberos/model/krb_error.rb +219 -0
  65. data/lib/rex/proto/kerberos/model/last_request.rb +82 -0
  66. data/lib/rex/proto/kerberos/model/pre_auth_data.rb +104 -0
  67. data/lib/rex/proto/kerberos/model/pre_auth_enc_time_stamp.rb +126 -0
  68. data/lib/rex/proto/kerberos/model/pre_auth_pac_request.rb +81 -0
  69. data/lib/rex/proto/kerberos/model/principal_name.rb +116 -0
  70. data/lib/rex/proto/kerberos/model/ticket.rb +151 -0
  71. data/lib/rex/proto/kerberos/pac.rb +36 -0
  72. data/lib/rex/proto/kerberos/pac/client_info.rb +53 -0
  73. data/lib/rex/proto/kerberos/pac/element.rb +52 -0
  74. data/lib/rex/proto/kerberos/pac/logon_info.rb +566 -0
  75. data/lib/rex/proto/kerberos/pac/priv_svr_checksum.rb +29 -0
  76. data/lib/rex/proto/kerberos/pac/server_checksum.rb +30 -0
  77. data/lib/rex/proto/kerberos/pac/type.rb +121 -0
  78. data/lib/rex/proto/rmi.rb +7 -0
  79. data/lib/rex/proto/rmi/model.rb +31 -0
  80. data/lib/rex/proto/rmi/model/call.rb +60 -0
  81. data/lib/rex/proto/rmi/model/continuation.rb +76 -0
  82. data/lib/rex/proto/rmi/model/dgc_ack.rb +62 -0
  83. data/lib/rex/proto/rmi/model/element.rb +143 -0
  84. data/lib/rex/proto/rmi/model/output_header.rb +86 -0
  85. data/lib/rex/proto/rmi/model/ping.rb +41 -0
  86. data/lib/rex/proto/rmi/model/ping_ack.rb +41 -0
  87. data/lib/rex/proto/rmi/model/protocol_ack.rb +100 -0
  88. data/lib/rex/proto/rmi/model/return_data.rb +60 -0
  89. data/lib/rex/socket.rb +9 -1
  90. data/lib/rex/socket/tcp_server.rb +3 -0
  91. data/lib/rex/ui/text/dispatcher_shell.rb +4 -4
  92. data/lib/rex/ui/text/output/tee.rb +2 -0
  93. data/lib/rex/zip/samples/comment.rb +0 -0
  94. data/lib/rex/zip/samples/mkwar.rb +0 -0
  95. data/lib/rex/zip/samples/mkzip.rb +0 -0
  96. data/lib/rex/zip/samples/recursive.rb +0 -0
  97. data/rex.gemspec +1 -1
  98. metadata +56 -2
@@ -0,0 +1,141 @@
1
+ # -*- coding: binary -*-
2
+
3
+ module Rex
4
+ module Proto
5
+ module Kerberos
6
+ module Model
7
+ # This class provides a representation of a Kerberos KDC-REQ (response) data
8
+ # definition
9
+ class KdcResponse < Element
10
+ # @!attribute pvno
11
+ # @return [Fixnum] The protocol version number
12
+ attr_accessor :pvno
13
+ # @!attribute msg_type
14
+ # @return [Fixnum] The type of a protocol message
15
+ attr_accessor :msg_type
16
+ # @!attribute crealm
17
+ # @return [String] The realm part of the client's principal identifier
18
+ attr_accessor :crealm
19
+ # @!attribute cname
20
+ # @return [Rex::Proto::Kerberos::Model::PrincipalName] The name part of the client's principal identifier
21
+ attr_accessor :cname
22
+ # @!attribute ticket
23
+ # @return [Rex::Proto::Kerberos::Model::Ticket] The issued ticket
24
+ attr_accessor :ticket
25
+ # @!attribute enc_part
26
+ # @return [Rex::Proto::Kerberos::Model::EncryptedData] The encrypted part of the response
27
+ attr_accessor :enc_part
28
+
29
+ # Decodes the Rex::Proto::Kerberos::Model::KdcResponse from an input
30
+ #
31
+ # @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from
32
+ # @return [self] if decoding succeeds
33
+ # @raise [RuntimeError] if decoding doesn't succeed
34
+ def decode(input)
35
+ case input
36
+ when String
37
+ decode_string(input)
38
+ when OpenSSL::ASN1::ASN1Data
39
+ decode_asn1(input)
40
+ else
41
+ raise ::RuntimeError, 'Failed to decode KdcResponse, invalid input'
42
+ end
43
+
44
+ self
45
+ end
46
+
47
+ # Rex::Proto::Kerberos::Model::KdcResponse encoding isn't supported
48
+ #
49
+ # @raise [NotImplementedError]
50
+ def encode
51
+ raise ::NotImplementedError, 'KdcResponse encoding not supported'
52
+ end
53
+
54
+ private
55
+
56
+ # Decodes a Rex::Proto::Kerberos::Model::KdcResponse from an String
57
+ #
58
+ # @param input [String] the input to decode from
59
+ def decode_string(input)
60
+ asn1 = OpenSSL::ASN1.decode(input)
61
+
62
+ decode_asn1(asn1)
63
+ end
64
+
65
+ # Decodes a Rex::Proto::Kerberos::Model::KdcResponse
66
+ #
67
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
68
+ # @raise [RuntimeError] if decoding doesn't succeed
69
+ def decode_asn1(input)
70
+ input.value[0].value.each do |val|
71
+ case val.tag
72
+ when 0
73
+ self.pvno = decode_pvno(val)
74
+ when 1
75
+ self.msg_type = decode_msg_type(val)
76
+ when 3
77
+ self.crealm = decode_crealm(val)
78
+ when 4
79
+ self.cname = decode_cname(val)
80
+ when 5
81
+ self.ticket = decode_ticket(val)
82
+ when 6
83
+ self.enc_part = decode_enc_part(val)
84
+ else
85
+ raise ::RuntimeError, 'Failed to decode KDC-RESPONSE SEQUENCE'
86
+ end
87
+ end
88
+ end
89
+
90
+ # Decodes the pvno from an OpenSSL::ASN1::ASN1Data
91
+ #
92
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
93
+ # @return [Fixnum]
94
+ def decode_pvno(input)
95
+ input.value[0].value.to_i
96
+ end
97
+
98
+ # Decodes the msg_type from an OpenSSL::ASN1::ASN1Data
99
+ #
100
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
101
+ # @return [Fixnum]
102
+ def decode_msg_type(input)
103
+ input.value[0].value.to_i
104
+ end
105
+
106
+ # Decodes the crealm field
107
+ #
108
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
109
+ # @return [String]
110
+ def decode_crealm(input)
111
+ input.value[0].value
112
+ end
113
+
114
+ # Decodes the cname field
115
+ #
116
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
117
+ # @return [Rex::Proto::Kerberos::Type::PrincipalName]
118
+ def decode_cname(input)
119
+ Rex::Proto::Kerberos::Model::PrincipalName.decode(input.value[0])
120
+ end
121
+
122
+ # Decodes the ticket field
123
+ #
124
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
125
+ # @return [Rex::Proto::Kerberos::Type::Ticket]
126
+ def decode_ticket(input)
127
+ Rex::Proto::Kerberos::Model::Ticket.decode(input.value[0])
128
+ end
129
+
130
+ # Decodes the enc_part
131
+ #
132
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
133
+ # @return [Rex::Proto::Kerberos::Model::EncryptedData]
134
+ def decode_enc_part(input)
135
+ Rex::Proto::Kerberos::Model::EncryptedData.decode(input.value[0])
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,219 @@
1
+ # -*- coding: binary -*-
2
+
3
+ module Rex
4
+ module Proto
5
+ module Kerberos
6
+ module Model
7
+ # This class provides a representation of a Kerberos KRB-ERROR (response error)
8
+ # message definition.
9
+ class KrbError < Element
10
+ # @!attribute pvno
11
+ # @return [Fixnum] The protocol version number
12
+ attr_accessor :pvno
13
+ # @!attribute msg_type
14
+ # @return [Fixnum] The type of a protocol message
15
+ attr_accessor :msg_type
16
+ # @!attribute ctime
17
+ # @return [Time] The current time of the client's host
18
+ attr_accessor :ctime
19
+ # @!attribute cusec
20
+ # @return [Fixnum] The microseconds part of the client timestamp
21
+ attr_accessor :cusec
22
+ # @!attribute stime
23
+ # @return [Time] The current time of the server
24
+ attr_accessor :stime
25
+ # @!attribute susec
26
+ # @return [Fixnum] The microseconds part of the server timestamp
27
+ attr_accessor :susec
28
+ # @!attribute error_code
29
+ # @return [Fixnum] The error request returned by kerberos or the server when a request fails
30
+ attr_accessor :error_code
31
+ # @!attribute crealm
32
+ # @return [String] The realm part of the client's principal identifier
33
+ attr_accessor :crealm
34
+ # @!attribute cname
35
+ # @return [Rex::Proto::Kerberos::Model::PrincipalName] The name part of the client's principal identifier
36
+ attr_accessor :cname
37
+ # @!attribute realm
38
+ # @return [String] The realm part of the server's principal identifier
39
+ attr_accessor :realm
40
+ # @!attribute sname
41
+ # @return [Rex::Proto::Kerberos::Model::PrincipalName] The name part of the server's identity
42
+ attr_accessor :sname
43
+ # @!attribute e_data
44
+ # @return [String] additional data about the error (ASN.1 encoded data)
45
+ attr_accessor :e_data
46
+
47
+ # Decodes the Rex::Proto::Kerberos::Model::KrbError from an input
48
+ #
49
+ # @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from
50
+ # @return [self] if decoding succeeds
51
+ # @raise [RuntimeError] if decoding doesn't succeed
52
+ def decode(input)
53
+ case input
54
+ when String
55
+ decode_string(input)
56
+ when OpenSSL::ASN1::ASN1Data
57
+ decode_asn1(input)
58
+ else
59
+ raise ::RuntimeError, 'Failed to decode KrbError, invalid input'
60
+ end
61
+
62
+ self
63
+ end
64
+
65
+ # Rex::Proto::Kerberos::Model::KrbError encoding isn't supported
66
+ #
67
+ # @raise [NotImplementedError]
68
+ def encode
69
+ raise ::NotImplementedError, 'KrbError encoding not supported'
70
+ end
71
+
72
+ private
73
+
74
+ # Decodes a Rex::Proto::Kerberos::Model::KrbError from an String
75
+ #
76
+ # @param input [String] the input to decode from
77
+ def decode_string(input)
78
+ asn1 = OpenSSL::ASN1.decode(input)
79
+
80
+ decode_asn1(asn1)
81
+ end
82
+
83
+ # Decodes a Rex::Proto::Kerberos::Model::KrbError
84
+ #
85
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
86
+ # @raise [RuntimeError] if decoding doesn't succeed
87
+ def decode_asn1(input)
88
+ input.value[0].value.each do |val|
89
+ case val.tag
90
+ when 0
91
+ self.pvno = decode_pvno(val)
92
+ when 1
93
+ self.msg_type = decode_msg_type(val)
94
+ when 2
95
+ self.ctime = decode_ctime(val)
96
+ when 3
97
+ self.cusec = decode_cusec(val)
98
+ when 4
99
+ self.stime = decode_stime(val)
100
+ when 5
101
+ self.susec = decode_susec(val)
102
+ when 6
103
+ self.error_code = decode_error_code(val)
104
+ when 7
105
+ self.crealm = decode_crealm(val)
106
+ when 8
107
+ self.cname = decode_cname(val)
108
+ when 9
109
+ self.realm = decode_realm(val)
110
+ when 10
111
+ self.sname = decode_sname(val)
112
+ when 12
113
+ self.e_data = decode_e_data(val)
114
+ else
115
+ raise ::RuntimeError, 'Failed to decode KRB-ERROR SEQUENCE'
116
+ end
117
+ end
118
+ end
119
+
120
+ # Decodes the pvno from an OpenSSL::ASN1::ASN1Data
121
+ #
122
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
123
+ # @return [Fixnum]
124
+ def decode_pvno(input)
125
+ input.value[0].value.to_i
126
+ end
127
+
128
+ # Decodes the msg_type from an OpenSSL::ASN1::ASN1Data
129
+ #
130
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
131
+ # @return [Fixnum]
132
+ def decode_msg_type(input)
133
+ input.value[0].value.to_i
134
+ end
135
+
136
+ # Decodes the ctime field
137
+ #
138
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
139
+ # @return [Time]
140
+ def decode_ctime(input)
141
+ input.value[0].value
142
+ end
143
+
144
+ # Decodes the cusec field
145
+ #
146
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
147
+ # @return [Fixnum]
148
+ def decode_cusec(input)
149
+ input.value[0].value
150
+ end
151
+
152
+ # Decodes the stime field
153
+ #
154
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
155
+ # @return [Time]
156
+ def decode_stime(input)
157
+ input.value[0].value
158
+ end
159
+
160
+ # Decodes the susec field
161
+ #
162
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
163
+ # @return [Fixnum]
164
+ def decode_susec(input)
165
+ input.value[0].value.to_i
166
+ end
167
+
168
+ # Decodes the error_code field
169
+ #
170
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
171
+ # @return [Fixnum]
172
+ def decode_error_code(input)
173
+ input.value[0].value.to_i
174
+ end
175
+
176
+ # Decodes the crealm field
177
+ #
178
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
179
+ # @return [String]
180
+ def decode_crealm(input)
181
+ input.value[0].value
182
+ end
183
+
184
+ # Decodes the cname field
185
+ #
186
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
187
+ # @return [Rex::Proto::Kerberos::Model::PrincipalName]
188
+ def decode_cname(input)
189
+ Rex::Proto::Kerberos::Model::PrincipalName.decode(input.value[0])
190
+ end
191
+
192
+ # Decodes the realm field
193
+ #
194
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
195
+ # @return [String]
196
+ def decode_realm(input)
197
+ input.value[0].value
198
+ end
199
+
200
+ # Decodes the sname field
201
+ #
202
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
203
+ # @return [Rex::Proto::Kerberos::Model::PrincipalName]
204
+ def decode_sname(input)
205
+ Rex::Proto::Kerberos::Model::PrincipalName.decode(input.value[0])
206
+ end
207
+
208
+ # Decodes the e_data from an OpenSSL::ASN1::ASN1Data
209
+ #
210
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
211
+ # @return [String]
212
+ def decode_e_data(input)
213
+ input.value[0].value
214
+ end
215
+ end
216
+ end
217
+ end
218
+ end
219
+ end
@@ -0,0 +1,82 @@
1
+ # -*- coding: binary -*-
2
+
3
+ module Rex
4
+ module Proto
5
+ module Kerberos
6
+ module Model
7
+ # This class provides a representation of request time
8
+ class LastRequest < Element
9
+
10
+ # @!attribute type
11
+ # @return [Fixnum] The type of value
12
+ attr_accessor :type
13
+ # @!attribute value
14
+ # @return [Time] the time of the last request
15
+ attr_accessor :value
16
+
17
+ # Decodes a Rex::Proto::Kerberos::Model::LastRequest
18
+ #
19
+ # @param input [String, OpenSSL::ASN1::Sequence] the input to decode from
20
+ # @return [self] if decoding succeeds
21
+ # @raise [RuntimeError] if decoding doesn't succeed
22
+ def decode(input)
23
+ case input
24
+ when String
25
+ decode_string(input)
26
+ when OpenSSL::ASN1::Sequence
27
+ decode_asn1(input)
28
+ else
29
+ raise ::RuntimeError, 'Failed to decode LastRequest, invalid input'
30
+ end
31
+
32
+ self
33
+ end
34
+
35
+ # Rex::Proto::Kerberos::Model::LastRequest encoding isn't supported
36
+ #
37
+ # @raise [NotImplementedError]
38
+ def encode
39
+ raise ::NotImplementedError, 'LastRequest encoding not supported'
40
+ end
41
+
42
+ private
43
+
44
+ # Decodes a Rex::Proto::Kerberos::Model::LastReque from an String
45
+ #
46
+ # @param input [String] the input to decode from
47
+ def decode_string(input)
48
+ asn1 = OpenSSL::ASN1.decode(input)
49
+
50
+ decode_asn1(asn1)
51
+ end
52
+
53
+ # Decodes a Rex::Proto::Kerberos::Model::EncryptionKey from an
54
+ # OpenSSL::ASN1::Sequence
55
+ #
56
+ # @param input [OpenSSL::ASN1::Sequence] the input to decode from
57
+ def decode_asn1(input)
58
+ seq_values = input.value
59
+ self.type = decode_type(seq_values[0])
60
+ self.value = decode_value(seq_values[1])
61
+ end
62
+
63
+ # Decodes the key_type from an OpenSSL::ASN1::ASN1Data
64
+ #
65
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
66
+ # @return [Fixnum]
67
+ def decode_type(input)
68
+ input.value[0].value.to_i
69
+ end
70
+
71
+ # Decodes the value from an OpenSSL::ASN1::ASN1Data
72
+ #
73
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
74
+ # @return [Time]
75
+ def decode_value(input)
76
+ input.value[0].value
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,104 @@
1
+ # -*- coding: binary -*-
2
+
3
+ module Rex
4
+ module Proto
5
+ module Kerberos
6
+ module Model
7
+ # This class provides a representation for Kerberos pre authenticated
8
+ # data
9
+ class PreAuthData < Element
10
+
11
+ # @!attribute type
12
+ # @return [Fixnum] The padata type
13
+ attr_accessor :type
14
+ # @!attribute value
15
+ # @return [String] The padata value
16
+ attr_accessor :value
17
+
18
+ # Decodes a Rex::Proto::Kerberos::Model::PreAuthData
19
+ #
20
+ # @param input [String, OpenSSL::ASN1::Sequence] the input to decode from
21
+ # @return [self] if decoding succeeds
22
+ # @raise [RuntimeError] if decoding doesn't succeed
23
+ def decode(input)
24
+ case input
25
+ when String
26
+ decode_string(input)
27
+ when OpenSSL::ASN1::Sequence
28
+ decode_asn1(input)
29
+ else
30
+ raise ::RuntimeError, 'Failed to decode PreAuthData, invalid input'
31
+ end
32
+
33
+ self
34
+ end
35
+
36
+ # Encodes a Rex::Proto::Kerberos::Model::PreAuthData into an ASN.1 String
37
+ #
38
+ # @return [String]
39
+ def encode
40
+ type_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_type], 1, :CONTEXT_SPECIFIC)
41
+ value_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_value], 2, :CONTEXT_SPECIFIC)
42
+ seq = OpenSSL::ASN1::Sequence.new([type_asn1, value_asn1])
43
+
44
+ seq.to_der
45
+ end
46
+
47
+ private
48
+
49
+ # Encodes the type
50
+ #
51
+ # @return [OpenSSL::ASN1::Integer]
52
+ def encode_type
53
+ int_bn = OpenSSL::BN.new(type.to_s)
54
+ int = OpenSSL::ASN1::Integer.new(int_bn)
55
+
56
+ int
57
+ end
58
+
59
+ # Encodes the value
60
+ #
61
+ # @return [OpenSSL::ASN1::OctetString]
62
+ def encode_value
63
+ OpenSSL::ASN1::OctetString.new(value)
64
+ end
65
+
66
+ # Decodes a Rex::Proto::Kerberos::Model::PreAuthData
67
+ #
68
+ # @param input [String] the input to decode from
69
+ def decode_string(input)
70
+ asn1 = OpenSSL::ASN1.decode(input)
71
+
72
+ decode_asn1(asn1)
73
+ end
74
+
75
+ # Decodes a Rex::Proto::Kerberos::Model::PreAuthData from an
76
+ # OpenSSL::ASN1::Sequence
77
+ #
78
+ # @param input [OpenSSL::ASN1::Sequence] the input to decode from
79
+ def decode_asn1(input)
80
+ seq_values = input.value
81
+ self.type = decode_asn1_type(seq_values[0])
82
+ self.value = decode_asn1_value(seq_values[1])
83
+ end
84
+
85
+ # Decodes the type from an OpenSSL::ASN1::ASN1Data
86
+ #
87
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
88
+ # @return [Fixnum]
89
+ def decode_asn1_type(input)
90
+ input.value[0].value.to_i
91
+ end
92
+
93
+ # Decodes the value from an OpenSSL::ASN1::ASN1Data
94
+ #
95
+ # @param input [OpenSSL::ASN1::ASN1Data] the input to decode from
96
+ # @return [Fixnum]
97
+ def decode_asn1_value(input)
98
+ input.value[0].value
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end