Class: Rex::Post::Meterpreter::Extensions::Stdapi::Fs::Dir

Inherits:
Dir
  • Object
show all
Defined in:
lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb

Overview

This class implements directory operations against the remote endpoint. It implements the Rex::Post::Dir interface.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Dir

foreach

Constructor Details

#initialize(path) ⇒ Dir

Initializes the directory instance.



35
36
37
38
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 35

def initialize(path)
	self.path   = path
	self.client = self.class.client
end

Class Attribute Details

.clientObject

Returns the value of attribute client.



23
24
25
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 23

def client
  @client
end

Instance Attribute Details

#pathObject

The path of the directory that was opened.



277
278
279
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 277

def path
  @path
end

Class Method Details

.chdir(path) ⇒ Object

Changes the working directory of the remote process.



118
119
120
121
122
123
124
125
126
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 118

def Dir.chdir(path)
	request = Packet.create_request('stdapi_fs_chdir')

	request.add_tlv(TLV_TYPE_DIRECTORY_PATH, client.unicode_filter_decode( path ))

	response = client.send_request(request)

	return 0
end

.delete(path) ⇒ Object

Removes the supplied directory if it’s empty.



162
163
164
165
166
167
168
169
170
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 162

def Dir.delete(path)
	request = Packet.create_request('stdapi_fs_delete_dir')

	request.add_tlv(TLV_TYPE_DIRECTORY_PATH, client.unicode_filter_decode( path ))

	response = client.send_request(request)

	return 0
end

.download(dst, src, recursive = false, force = true, &stat) ⇒ Object

Downloads the contents of a remote directory a local directory, optionally in a recursive fashion.



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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 196

def Dir.download(dst, src, recursive = false, force = true, &stat)

	self.entries(src).each { |src_sub|
		dst_item = dst + ::File::SEPARATOR + client.unicode_filter_encode( src_sub )
		src_item = src + client.fs.file.separator + client.unicode_filter_encode( src_sub )

		if (src_sub == '.' or src_sub == '..')
			next
		end

		src_stat = client.fs.filestat.new(src_item)

		if (src_stat.file?)
			stat.call('downloading', src_item, dst_item) if (stat)
			begin
				client.fs.file.download(dst_item, src_item)
				stat.call('downloaded', src_item, dst_item) if (stat)
			rescue ::Rex::Post::Meterpreter::RequestError => e
				if force
					stat.call('failed', src_item, dst_item) if (stat)
				else
					raise e
				end
			end

		elsif (src_stat.directory?)
			if (recursive == false)
				next
			end

			begin
				::Dir.mkdir(dst_item)
			rescue
			end

			stat.call('mirroring', src_item, dst_item) if (stat)
			download(dst_item, src_item, recursive, force, &stat)
			stat.call('mirrored', src_item, dst_item) if (stat)
		end
	}
end

.entries(name = getwd) ⇒ Object

Enumerates all of the files/folders in a given directory.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 56

def Dir.entries(name = getwd)
	request = Packet.create_request('stdapi_fs_ls')
	files   = []

	request.add_tlv(TLV_TYPE_DIRECTORY_PATH, client.unicode_filter_decode(name))

	response = client.send_request(request)

	response.each(TLV_TYPE_FILE_NAME) { |file_name|
		files << client.unicode_filter_encode( file_name.value )
	}

	return files
end

.entries_with_info(name = getwd) ⇒ Object

Enumerates files with a bit more information than the default entries.



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
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 74

def Dir.entries_with_info(name = getwd)
	request = Packet.create_request('stdapi_fs_ls')
	files   = []

	request.add_tlv(TLV_TYPE_DIRECTORY_PATH, client.unicode_filter_decode(name))

	response = client.send_request(request)

	fname = response.get_tlvs(TLV_TYPE_FILE_NAME)
	fpath = response.get_tlvs(TLV_TYPE_FILE_PATH)
	sbuf  = response.get_tlvs(TLV_TYPE_STAT_BUF)

	if (!fname or !sbuf)
		return []
	end

	fname.each_with_index { |file_name, idx|
		st = nil

		if (sbuf[idx])
			st = ::Rex::Post::FileStat.new
			st.update(sbuf[idx].value)
		end

		files <<
			{
				'FileName' => client.unicode_filter_encode( file_name.value ),
				'FilePath' => client.unicode_filter_encode( fpath[idx].value ),
				'StatBuf'  => st,
			}
	}

	return files
end

.getwdObject

Synonym for pwd.



155
156
157
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 155

def Dir.getwd
	pwd
end

.mkdir(path) ⇒ Object

Creates a directory.



131
132
133
134
135
136
137
138
139
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 131

def Dir.mkdir(path)
	request = Packet.create_request('stdapi_fs_mkdir')

	request.add_tlv(TLV_TYPE_DIRECTORY_PATH, client.unicode_filter_decode( path ))

	response = client.send_request(request)

	return 0
end

.pwdObject

Returns the current working directory of the remote process.



144
145
146
147
148
149
150
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 144

def Dir.pwd
	request = Packet.create_request('stdapi_fs_getwd')

	response = client.send_request(request)

	return client.unicode_filter_encode( response.get_tlv(TLV_TYPE_DIRECTORY_PATH).value )
end

.rmdir(path) ⇒ Object

Synonyms for delete.



175
176
177
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 175

def Dir.rmdir(path)
	delete(path)
end

Synonyms for delete.



182
183
184
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 182

def Dir.unlink(path)
	delete(path)
end

.upload(dst, src, recursive = false, &stat) ⇒ Object

Uploads the contents of a local directory to a remote directory, optionally in a recursive fashion.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 242

def Dir.upload(dst, src, recursive = false, &stat)
	::Dir.entries(src).each { |src_sub|
		dst_item = dst + client.fs.file.separator + client.unicode_filter_encode( src_sub )
		src_item = src + ::File::SEPARATOR + client.unicode_filter_encode( src_sub )

		if (src_sub == '.' or src_sub == '..')
			next
		end

		src_stat = ::File.stat(src_item)

		if (src_stat.file?)
			stat.call('uploading', src_item, dst_item) if (stat)
			client.fs.file.upload(dst_item, src_item)
			stat.call('uploaded', src_item, dst_item) if (stat)
		elsif (src_stat.directory?)
			if (recursive == false)
				next
			end

			begin
				self.mkdir(dst_item)
			rescue
			end

			stat.call('mirroring', src_item, dst_item) if (stat)
			upload(dst_item, src_item, recursive, &stat)
			stat.call('mirrored', src_item, dst_item) if (stat)
		end
	}
end

Instance Method Details

#each(&block) ⇒ Object

Enumerates all of the contents of the directory.



49
50
51
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 49

def each(&block)
	client.fs.dir.foreach(self.path, &block)
end