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.


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

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

Class Attribute Details

.clientObject

Returns the value of attribute client


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

def client
  @client
end

Instance Attribute Details

#pathObject

The path of the directory that was opened.


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

def path
  @path
end

Class Method Details

.chdir(path) ⇒ Object

Changes the working directory of the remote process.


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

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.


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

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.


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

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.


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

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.


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

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.


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

def Dir.getwd
  pwd
end

.mkdir(path) ⇒ Object

Creates a directory.


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

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.


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

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.


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

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

Synonyms for delete.


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

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.


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

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.


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

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