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.



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

def path
  @path
end

Class Method Details

.chdir(path) ⇒ Object

Changes the working directory of the remote process.



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

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.



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

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, glob = nil, &stat) ⇒ Object

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



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

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

  self.entries(src, glob).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
        result = client.fs.file.download_file(dst_item, src_item)
        stat.call(result, 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, glob, &stat)
      stat.call('mirrored', src_item, dst_item) if (stat)
    end
  }
end

.entries(name = getwd, glob = nil) ⇒ 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
69
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb', line 55

def Dir.entries(name = getwd, glob = nil)
  request = Packet.create_request('stdapi_fs_ls')
  files   = []
  name = name + ::File::SEPARATOR + glob if glob

  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
108
109
# 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)
  fsname = response.get_tlvs(TLV_TYPE_FILE_SHORT_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),
        'FileShortName' => fsname[idx] ? fsname[idx].value : nil,
        'StatBuf'  => st,
      }
  }

  return files
end

.getwdObject

Synonym for pwd.



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

def Dir.getwd
  pwd
end

.mkdir(path) ⇒ Object

Creates a directory.



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

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.



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

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.



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

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

Synonyms for delete.



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

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.



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

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