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.



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

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

Instance Attribute Details

#stathashObject

Returns the value of attribute stathash.



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

def stathash
  @stathash
end

Instance Method Details

#atimeObject



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

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

#blksizeObject



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

def blksize
  self.stathash['st_blksize']
end

#blockdev?Boolean

Returns:

  • (Boolean)


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

def blockdev?
  filetype?(060000)
end

#blocksObject



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

def blocks
  self.stathash['st_blocks']
end

#chardev?Boolean

Returns:

  • (Boolean)


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

def chardev?
  filetype?(020000)
end

#ctimeObject



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

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

#devObject



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

def dev
  self.stathash['st_dev']
end

#directory?Boolean

Returns:

  • (Boolean)


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

def directory?
  filetype?(040000)
end

#executable?Boolean

Returns:

  • (Boolean)

Raises:



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

def executable?
  raise NotImplementedError
end

#executable_real?Boolean

Returns:

  • (Boolean)

Raises:



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

def executable_real?
  raise NotImplementedError
end

#file?Boolean

Returns:

  • (Boolean)


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

def file?
  filetype?(0100000)
end

#filetype?(mask) ⇒ Boolean

this is my own, just a helper…

Returns:

  • (Boolean)


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

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

#ftypeObject



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

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

#gidObject



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

def gid
  self.stathash['st_gid']
end

#grpowned?Boolean

Returns:

  • (Boolean)

Raises:



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

def grpowned?
  raise NotImplementedError
end

#inoObject



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

def ino
  self.stathash['st_ino']
end

#modeObject



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

def mode
  self.stathash['st_mode']
end

#mtimeObject



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

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


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

def nlink
  self.stathash['st_nlink']
end

#owned?Boolean

Returns:

  • (Boolean)

Raises:



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

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)


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

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

#pipe?Boolean

Returns:

  • (Boolean)


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

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

#prettyObject

Return pretty information about a file.



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

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.



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

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



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

def rdev
  self.stathash['st_rdev']
end

#readable?Boolean

Returns:

  • (Boolean)

Raises:



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

def readable?
  raise NotImplementedError
end

#readable_real?Boolean

Returns:

  • (Boolean)

Raises:



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

def readable_real?
  raise NotImplementedError
end

#setgid?Boolean

Returns:

  • (Boolean)


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

def setgid?
  perm?(02000)
end

#setuid?Boolean

Returns:

  • (Boolean)


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

def setuid?
  perm?(04000)
end

#sizeObject



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

def size
  self.stathash['st_size']
end

#socket?Boolean

Returns:

  • (Boolean)


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

def socket?
  filetype(0140000)
end

#sticky?Boolean

Returns:

  • (Boolean)


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

def sticky?
  perm?(01000)
end

#symlink?Boolean

Returns:

  • (Boolean)


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

def symlink?
  filetype(0120000)
end

#uidObject



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

def uid
  self.stathash['st_uid']
end

#update(buf) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rex/post/file_stat.rb', line 76

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:



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

def writeable?
  raise NotImplementedError
end

#writeable_real?Boolean

Returns:

  • (Boolean)

Raises:



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

def writeable_real?
  raise NotImplementedError
end