Class: Rex::Proto::SMB::Client::UnitTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/rex/proto/smb/client.rb.ut.rb

Constant Summary collapse

Klass =
Rex::Proto::SMB::Client
DCERPCPacket =

Alias over the Rex DCERPC protocol modules

Rex::Proto::DCERPC::Packet
DCERPCClient =
Rex::Proto::DCERPC::Client
DCERPCResponse =
Rex::Proto::DCERPC::Response
DCERPCUUID =
Rex::Proto::DCERPC::UUID

Instance Method Summary collapse

Instance Method Details

#test_smb_open_shareObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rex/proto/smb/client.rb.ut.rb', line 24

def test_smb_open_share

	share = 'C$'

	write_data = ('A' * 256)
	filename = 'smb_test.txt'

	begin
	Timeout.timeout($_REX_TEST_TIMEOUT) {
	s = Rex::Socket.create_tcp(
		'PeerHost' => $_REX_TEST_SMB_HOST,
		'PeerPort' => 139
	)

	c = Klass.new(s)

	# Request a SMB session over NetBIOS
	# puts "[*] Requesting a SMB session over NetBIOS..."
	ok = c.session_request()
	assert_kind_of(Rex::Struct2::CStruct, ok)

	# Check for a positive session response
	# A negative response is 0x83
	assert_equal(ok.v['Type'], 0x82)

	# puts "[*] Negotiating SMB dialects..."
	ok = c.negotiate()
	assert_kind_of(Rex::Struct2::CStruct, ok)

	# puts "[*] Authenticating with NTLMv2..."
	ok = c.session_setup_with_ntlmssp($_REX_TEXT_SMB_USER, $_REX_TEXT_SMB_PASS)
	assert_kind_of(Rex::Struct2::CStruct, ok)
	assert_not_equal(c.auth_user_id, 0)

	# puts "[*] Connecting to the share..."
	ok = c.tree_connect(share)
	assert_kind_of(Rex::Struct2::CStruct, ok)
	assert_not_equal(c.last_tree_id, 0)

	# puts "[*] Opening a file for write..."
	ok = c.open(filename)
	assert_kind_of(Rex::Struct2::CStruct, ok)
	assert_not_equal(c.last_file_id, 0)

	# puts "[*] Writing data to the test file..."
	ok = c.write(c.last_file_id, 0, write_data)
	assert_kind_of(Rex::Struct2::CStruct, ok)
	assert_equal(ok['Payload'].v['CountLow'], write_data.length)

	# puts "[*] Closing the test file..."
	ok = c.close(c.last_file_id)
	assert_kind_of(Rex::Struct2::CStruct, ok)

	# puts "[*] Opening a file for read..."
	ok = c.open(filename, 1)
	assert_kind_of(Rex::Struct2::CStruct, ok)
	assert_not_equal(c.last_file_id, 0)

	# puts "[*] Reading data from the test file..."
	ok = c.read(c.last_file_id, 0, write_data.length)
	assert_kind_of(Rex::Struct2::CStruct, ok)
	assert_equal(ok['Payload'].v['DataLenLow'], write_data.length)

	read_data =  ok.to_s.slice(
		ok['Payload'].v['DataOffset'] + 4,
		ok['Payload'].v['DataLenLow']
	)
	assert_equal(read_data, write_data)

	# puts "[*] Closing the test file..."
	ok = c.close(c.last_file_id)
	assert_kind_of(Rex::Struct2::CStruct, ok)

	# puts "[*] Disconnecting from the tree..."
	ok = c.tree_disconnect
	assert_kind_of(Rex::Struct2::CStruct, ok)

	s.close


	# Reconnect and delete the file
	s = Rex::Socket.create_tcp(
		'PeerHost' => $_REX_TEST_SMB_HOST,
		'PeerPort' => 139
	)

	c = Klass.new(s)

	# Request a SMB session over NetBIOS
	# puts "[*] Requesting a SMB session over NetBIOS..."
	ok = c.session_request()
	assert_kind_of(Rex::Struct2::CStruct, ok)

	# Check for a positive session response
	# A negative response is 0x83
	assert_equal(ok.v['Type'], 0x82)

	# puts "[*] Negotiating SMB dialects..."
	ok = c.negotiate()
	assert_kind_of(Rex::Struct2::CStruct, ok)

	# puts "[*] Authenticating with NTLMv2..."
	ok = c.session_setup_with_ntlmssp($_REX_TEXT_SMB_USER, $_REX_TEXT_SMB_PASS)
	assert_kind_of(Rex::Struct2::CStruct, ok)
	assert_not_equal(c.auth_user_id, 0)

	# puts "[*] Connecting to the share..."
	ok = c.tree_connect(share)
	assert_kind_of(Rex::Struct2::CStruct, ok)
	assert_not_equal(c.last_tree_id, 0)

	# puts "[*] Deleting the test file..."
	ok = c.delete(filename)
	assert_kind_of(Rex::Struct2::CStruct, ok)

	# puts "[*] Diconnecting from the tree..."
	ok = c.tree_disconnect
	assert_kind_of(Rex::Struct2::CStruct, ok)

	s.close
	}
	rescue Timeout::Error
		flunk('timeout')
	end

end

#test_smb_session_requestObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/rex/proto/smb/client.rb.ut.rb', line 151

def test_smb_session_request
	begin
	Timeout.timeout($_REX_TEST_TIMEOUT) {
	s = Rex::Socket.create_tcp(
		'PeerHost' => $_REX_TEST_SMB_HOST,
		'PeerPort' => 139
	)

	c = Klass.new(s)

	# Request a SMB session over NetBIOS
	# puts "[*] Requesting a SMB session over NetBIOS..."
	ok = c.session_request()
	assert_kind_of(Rex::Struct2::CStruct, ok)

	# Check for a positive session response
	# A negative response is 0x83
	assert_equal(ok.v['Type'], 0x82)

	# puts "[*] Negotiating SMB dialects..."
	ok = c.negotiate()
	assert_kind_of(Rex::Struct2::CStruct, ok)

	# puts "[*] Authenticating with NTLMv2..."
	ok = c.session_setup_with_ntlmssp
	assert_kind_of(Rex::Struct2::CStruct, ok)

	# puts "[*] Authenticating with NTLMv1..."
	ok = c.session_setup_no_ntlmssp
	assert_kind_of(Rex::Struct2::CStruct, ok)

	# puts "[*] Authenticating with clear text passwords..."
	begin
		ok = c.session_setup_clear
		assert_kind_of(Rex::Struct2::CStruct, ok)
	rescue Rex::Proto::SMB::Exceptions::ErrorCode
		if ($!.error_code != 0x00010002)
			raise $!
		end
	end

	# puts "[*] Connecting to IPC$..."
	ok = c.tree_connect
	assert_kind_of(Rex::Struct2::CStruct, ok)

	# puts "[*] Opening the \BROWSER pipe..."
	ok = c.create_pipe('\BROWSER')
	assert_kind_of(Rex::Struct2::CStruct, ok)

	vers = DCERPCUUID.vers_by_name('SRVSVC')
	uuid = DCERPCUUID.uuid_by_name('SRVSVC')
	bind, ctx = DCERPCPacket.make_bind_fake_multi(uuid, vers)

	# puts "[*] Binding to the Server Service..."
	ok = c.trans_named_pipe(c.last_file_id, bind)
	assert_kind_of(Rex::Struct2::CStruct, ok)

	data = ok.to_s.slice(
		ok['Payload'].v['DataOffset'] + 4,
		ok['Payload'].v['DataCount']
	)
	assert_not_equal(data, nil)

	resp = DCERPCResponse.new(data)
	assert_equal(resp.type, 12)
	}
	rescue Timeout::Error
		flunk('timeout')
	end
end