Class: Rex::Post::FileStat

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/post/file_stat.rb

Overview

This class emulates the ruby FileStat class against a remote entity in a generic fashion. Refer to the ruby documentation for expected behavior.

Constant Summary collapse

@@ftypes =

Basic file types.

[
  'fifo', 'characterSpecial', 'directory',
  'blockSpecial', 'file', 'link', 'socket'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buf = '') ⇒ FileStat

Returns a new instance of FileStat.



30
31
32
33
# File 'lib/rex/post/file_stat.rb', line 30

def initialize(buf='')
  self.stathash = {}
  update(buf) if (buf and not buf.empty?)
end

Instance Attribute Details

#stathashObject

Returns the value of attribute stathash.



28
29
30
# File 'lib/rex/post/file_stat.rb', line 28

def stathash
  @stathash
end

Instance Method Details

#atimeObject



65
66
67
# File 'lib/rex/post/file_stat.rb', line 65

def atime
  ::Time.at(self.stathash['st_atime'])
end

#blksizeObject



59
60
61
# File 'lib/rex/post/file_stat.rb', line 59

def blksize
  self.stathash['st_blksize']
end

#blockdev?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/rex/post/file_stat.rb', line 112

def blockdev?
  filetype?(060000)
end

#blocksObject



62
63
64
# File 'lib/rex/post/file_stat.rb', line 62

def blocks
  self.stathash['st_blocks']
end

#chardev?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/rex/post/file_stat.rb', line 115

def chardev?
  filetype?(020000)
end

#ctimeObject



71
72
73
# File 'lib/rex/post/file_stat.rb', line 71

def ctime
  ::Time.at(self.stathash['st_ctime'])
end

#devObject



35
36
37
# File 'lib/rex/post/file_stat.rb', line 35

def dev
  self.stathash['st_dev']
end

#directory?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/rex/post/file_stat.rb', line 118

def directory?
  filetype?(040000)
end

#executable?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


171
172
173
# File 'lib/rex/post/file_stat.rb', line 171

def executable?
  raise NotImplementedError
end

#executable_real?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


174
175
176
# File 'lib/rex/post/file_stat.rb', line 174

def executable_real?
  raise NotImplementedError
end

#file?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/rex/post/file_stat.rb', line 121

def file?
  filetype?(0100000)
end

#filetype?(mask) ⇒ Boolean

this is my own, just a helper…

Returns:

  • (Boolean)


107
108
109
110
# File 'lib/rex/post/file_stat.rb', line 107

def filetype?(mask)
  return true if mode & 0170000 == mask
  return false
end

#ftypeObject



134
135
136
# File 'lib/rex/post/file_stat.rb', line 134

def ftype
  return @@ftypes[(mode & 0170000) >> 13].dup
end

#gidObject



50
51
52
# File 'lib/rex/post/file_stat.rb', line 50

def gid
  self.stathash['st_gid']
end

#grpowned?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


177
178
179
# File 'lib/rex/post/file_stat.rb', line 177

def grpowned?
  raise NotImplementedError
end

#inoObject



38
39
40
# File 'lib/rex/post/file_stat.rb', line 38

def ino
  self.stathash['st_ino']
end

#modeObject



41
42
43
# File 'lib/rex/post/file_stat.rb', line 41

def mode
  self.stathash['st_mode']
end

#mtimeObject



68
69
70
# File 'lib/rex/post/file_stat.rb', line 68

def mtime
  ::Time.at(self.stathash['st_mtime'])
end


44
45
46
# File 'lib/rex/post/file_stat.rb', line 44

def nlink
  self.stathash['st_nlink']
end

#owned?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


180
181
182
# File 'lib/rex/post/file_stat.rb', line 180

def owned?
  raise NotImplementedError
end

#perm?(mask) ⇒ Boolean

S_ISUID 0004000 set UID bit S_ISGID 0002000 set GID bit (see below) S_ISVTX 0001000 sticky bit (see below) S_IRWXU 00700 mask for file owner permissions S_IRUSR 00400 owner has read permission S_IWUSR 00200 owner has write permission S_IXUSR 00100 owner has execute permission S_IRWXG 00070 mask for group permissions S_IRGRP 00040 group has read permission S_IWGRP 00020 group has write permission S_IXGRP 00010 group has execute permission S_IRWXO 00007 mask for permissions for others (not in group) S_IROTH 00004 others have read permission S_IWOTH 00002 others have write permission S_IXOTH 00001 others have execute permission

Returns:

  • (Boolean)


156
157
158
159
# File 'lib/rex/post/file_stat.rb', line 156

def perm?(mask)
  return true if mode & mask == mask
  return false
end

#pipe?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/rex/post/file_stat.rb', line 124

def pipe?
  filetype?(010000) # ??? fifo?
end

#prettyObject

Return pretty information about a file.



217
218
219
220
221
222
223
224
225
# File 'lib/rex/post/file_stat.rb', line 217

def pretty
  "  Size: #{size}   Blocks: #{blocks}   IO Block: #{blksize}   Type: #{rdev}\n"\
  "Device: #{dev}  Inode: #{ino}  Links: #{nlink}\n"\
  "  Mode: #{prettymode}\n"\
  "   Uid: #{uid}  Gid: #{gid}\n"\
  "Access: #{atime}\n"\
  "Modify: #{mtime}\n"\
  "Change: #{ctime}\n"
end

#prettymodeObject

Return pretty information about a file’s permissions.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rex/post/file_stat.rb', line 199

def prettymode
  m  = mode
  om = '%06o' % m
  perms = ''

  3.times {
    perms = ((m & 01) == 01 ? 'x' : '-') + perms
    perms = ((m & 02) == 02 ? 'w' : '-') + perms
    perms = ((m & 04) == 04 ? 'r' : '-') + perms
    m >>= 3
  }

  return "#{om}/#{perms}"
end

#rdevObject



53
54
55
# File 'lib/rex/post/file_stat.rb', line 53

def rdev
  self.stathash['st_rdev']
end

#readable?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


183
184
185
# File 'lib/rex/post/file_stat.rb', line 183

def readable?
  raise NotImplementedError
end

#readable_real?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


186
187
188
# File 'lib/rex/post/file_stat.rb', line 186

def readable_real?
  raise NotImplementedError
end

#setgid?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/rex/post/file_stat.rb', line 161

def setgid?
  perm?(02000)
end

#setuid?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/rex/post/file_stat.rb', line 164

def setuid?
  perm?(04000)
end

#sizeObject



56
57
58
# File 'lib/rex/post/file_stat.rb', line 56

def size
  self.stathash['st_size']
end

#socket?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/rex/post/file_stat.rb', line 127

def socket?
  filetype?(0140000)
end

#sticky?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/rex/post/file_stat.rb', line 167

def sticky?
  perm?(01000)
end

#symlink?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/rex/post/file_stat.rb', line 130

def symlink?
  filetype?(0120000)
end

#uidObject



47
48
49
# File 'lib/rex/post/file_stat.rb', line 47

def uid
  self.stathash['st_uid']
end

#update(buf) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/rex/post/file_stat.rb', line 75

def update(buf)
  skeys = %W{st_dev st_mode st_nlink st_uid st_gid st_rdev st_ino st_size st_atime st_mtime st_ctime}
  svals = buf.unpack("VVVVVVQQQQQ")
  skeys.each_index do |i|
    self.stathash[ skeys[i] ] = svals[i]
  end
end

#update32(buf) ⇒ Object

This handles the old 32bit st_size buf from old stageless meterpreters for backwards compatibility Maybe we can remove this in the future



87
88
89
90
91
92
93
# File 'lib/rex/post/file_stat.rb', line 87

def update32(buf)
  skeys = %W{st_dev st_ino st_mode st_pad st_nlink st_uid st_gid st_rdev st_size st_ctime st_atime st_mtime}
  svals = buf.unpack("VvvvvvvVVVVV")
  skeys.each_index do |i|
    self.stathash[ skeys[i] ] = svals[i]
  end
end

#writeable?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


189
190
191
# File 'lib/rex/post/file_stat.rb', line 189

def writeable?
  raise NotImplementedError
end

#writeable_real?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


192
193
194
# File 'lib/rex/post/file_stat.rb', line 192

def writeable_real?
  raise NotImplementedError
end