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)

104
105
106
# File 'lib/rex/post/file_stat.rb', line 104

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)

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

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)

110
111
112
# File 'lib/rex/post/file_stat.rb', line 110

def directory?
  filetype?(040000)
end

#executable?Boolean

Returns:

  • (Boolean)

Raises:


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

def executable?
  raise NotImplementedError
end

#executable_real?Boolean

Returns:

  • (Boolean)

Raises:


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

def executable_real?
  raise NotImplementedError
end

#file?Boolean

Returns:

  • (Boolean)

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

def file?
  filetype?(0100000)
end

#filetype?(mask) ⇒ Boolean

this is my own, just a helper…

Returns:

  • (Boolean)

99
100
101
102
# File 'lib/rex/post/file_stat.rb', line 99

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

#ftypeObject


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

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:


169
170
171
# File 'lib/rex/post/file_stat.rb', line 169

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:


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

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 permisson S_IXOTH 00001 others have execute permission

Returns:

  • (Boolean)

148
149
150
151
# File 'lib/rex/post/file_stat.rb', line 148

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

#pipe?Boolean

Returns:

  • (Boolean)

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

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

#prettyObject

Return pretty information about a file.


209
210
211
212
213
214
215
216
217
# File 'lib/rex/post/file_stat.rb', line 209

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.


191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/rex/post/file_stat.rb', line 191

def prettymode
  m  = mode
  om = '%04o' % 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:


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

def readable?
  raise NotImplementedError
end

#readable_real?Boolean

Returns:

  • (Boolean)

Raises:


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

def readable_real?
  raise NotImplementedError
end

#setgid?Boolean

Returns:

  • (Boolean)

153
154
155
# File 'lib/rex/post/file_stat.rb', line 153

def setgid?
  perm?(02000)
end

#setuid?Boolean

Returns:

  • (Boolean)

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

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)

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

def socket?
  filetype(0140000)
end

#sticky?Boolean

Returns:

  • (Boolean)

159
160
161
# File 'lib/rex/post/file_stat.rb', line 159

def sticky?
  perm?(01000)
end

#symlink?Boolean

Returns:

  • (Boolean)

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

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
82
83
84
85
# File 'lib/rex/post/file_stat.rb', line 75

def update(buf)

  # XXX: This needs to understand more than just 'stat' structures
  # Windows can also return _stat32, _stat32i64, _stat64i32, and _stat64 structures

  skeys = %W{st_dev st_ino st_mode st_wtf 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:


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

def writeable?
  raise NotImplementedError
end

#writeable_real?Boolean

Returns:

  • (Boolean)

Raises:


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

def writeable_real?
  raise NotImplementedError
end