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

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

Overview

This class wrappers gathering information about a given file and implements the Rex::Post::FileStat interface in terms of data acquisition.

Constant Summary collapse

@@struct_stat =
[
  'st_dev',     4,  # 0
  'st_mode',    4,  # 4
  'st_nlink',   4,  # 8
  'st_uid',     4,  # 12
  'st_gid',     4,  # 16
  'st_rdev',    4,  # 20
  'st_ino',     8,  # 24
  'st_size',    8,  # 32
  'st_atime',   8,  # 40
  'st_mtime',   8,  # 48
  'st_ctime',   8,  # 56
]
@@struct_stat32 =
[
  'st_dev',     4,  # 0
  'st_ino',     2,  # 4
  'st_mode',    2,  # 6
  'st_nlink',   2,  # 8
  'st_uid',     2,  # 10
  'st_gid',     2,  # 12
  'pad1',       2,  # 14
  'st_rdev',    4,  # 16
  'st_size',    4,  # 20
  'st_atime',   8,  # 24
  'st_mtime',   8,  # 32
  'st_ctime',   8,  # 40
]

Class Attribute Summary collapse

Attributes inherited from FileStat

#stathash

Instance Method Summary collapse

Methods inherited from FileStat

#atime, #blksize, #blockdev?, #blocks, #chardev?, #ctime, #dev, #directory?, #executable?, #executable_real?, #file?, #filetype?, #ftype, #gid, #grpowned?, #ino, #mode, #mtime, #nlink, #owned?, #perm?, #pipe?, #pretty, #prettymode, #rdev, #readable?, #readable_real?, #setgid?, #setuid?, #size, #socket?, #sticky?, #symlink?, #uid, #writeable?, #writeable_real?

Constructor Details

#initialize(file) ⇒ FileStat

Returns an instance of a FileStat object.


63
64
65
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/file_stat.rb', line 63

def initialize(file)
  self.stathash = stat(file) if (file)
end

Class Attribute Details

.clientObject

Returns the value of attribute client


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

def client
  @client
end

Instance Method Details

#stat(file) ⇒ Object (protected)

Gets information about the supplied file and returns a populated hash to the requestor.


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/file_stat.rb', line 132

def stat(file)
  request = Packet.create_request(COMMAND_ID_STDAPI_FS_STAT)

  request.add_tlv(TLV_TYPE_FILE_PATH, self.class.client.unicode_filter_decode( file ))

  response = self.class.client.send_request(request)
  stat_buf = response.get_tlv(TLV_TYPE_STAT_BUF)

  unless stat_buf
    stat_buf = response.get_tlv(TLV_TYPE_STAT_BUF32)
    return update32(stat_buf.value)
  end

  # Next, we go through the returned stat_buf and fix up the values
  # and insert them into a hash
  return update(stat_buf.value)
end

#update(stat_buf) ⇒ Object

Swaps in a new stat hash.


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

def update(stat_buf)
  elem   = @@struct_stat
  hash   = {}
  offset = 0
  index  = 0

  while (index < elem.length)
    size = elem[index + 1]
    format = 'V'
    case size
    when 2
      format = 'v'
    when 8
      format = 'Q'
    end

    value = stat_buf[offset, size].unpack(format)[0]
    offset += size

    hash[elem[index]] = value

    index += 2
  end

  return (self.stathash = hash)
end

#update32(stat_buf) ⇒ Object

Swaps in a new old style stat hash.


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rex/post/meterpreter/extensions/stdapi/fs/file_stat.rb', line 100

def update32(stat_buf)
  elem   = @@struct_stat32
  hash   = {}
  offset = 0
  index  = 0

  while (index < elem.length)
    size = elem[index + 1]

    value   = stat_buf[offset, size].unpack(size == 2 ? 'v' : 'V')[0]
    offset += size

    hash[elem[index]] = value

    index += 2
  end

  return (self.stathash = hash)
end