Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/post/file_stat.rb
Views: 11780
# -*- coding: binary -*-12#3# This is just a container class basically, that acts like File::Struct4#5# You must supply an initialize method that somehow populates the stathash..6#78module Rex9module Post1011###12#13# This class emulates the ruby FileStat class against a remote entity in a14# generic fashion. Refer to the ruby documentation for expected behavior.15#16###17class FileStat1819#20# Basic file types.21#22@@ftypes = [23'fifo', 'characterSpecial', 'directory',24'blockSpecial', 'file', 'link', 'socket'25]2627attr_accessor :stathash2829def initialize(buf='')30self.stathash = {}31update(buf) if (buf and not buf.empty?)32end3334def dev35self.stathash['st_dev']36end37def ino38self.stathash['st_ino']39end40def mode41self.stathash['st_mode']42end43def nlink44self.stathash['st_nlink']45end46def uid47self.stathash['st_uid']48end49def gid50self.stathash['st_gid']51end52def rdev53self.stathash['st_rdev']54end55def size56self.stathash['st_size']57end58def blksize59self.stathash['st_blksize']60end61def blocks62self.stathash['st_blocks']63end64def atime65::Time.at(self.stathash['st_atime'])66end67def mtime68::Time.at(self.stathash['st_mtime'])69end70def ctime71::Time.at(self.stathash['st_ctime'])72end7374def update(buf)75skeys = %W{st_dev st_mode st_nlink st_uid st_gid st_rdev st_ino st_size st_atime st_mtime st_ctime}76svals = buf.unpack("VVVVVVQQQQQ")77skeys.each_index do |i|78self.stathash[ skeys[i] ] = svals[i]79end80end8182#83# This handles the old 32bit st_size buf from old stageless meterpreters for backwards compatibility84# Maybe we can remove this in the future85#86def update32(buf)87skeys = %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}88svals = buf.unpack("VvvvvvvVVVVV")89skeys.each_index do |i|90self.stathash[ skeys[i] ] = svals[i]91end92end9394#95# S_IFMT 0170000 bitmask for the file type bitfields96# S_IFSOCK 0140000 socket97# S_IFLNK 0120000 symbolic link98# S_IFREG 0100000 regular file99# S_IFBLK 0060000 block device100# S_IFDIR 0040000 directory101# S_IFCHR 0020000 character device102# S_IFIFO 0010000 fifo103#104105# this is my own, just a helper...106def filetype?(mask)107return true if mode & 0170000 == mask108return false109end110111def blockdev?112filetype?(060000)113end114def chardev?115filetype?(020000)116end117def directory?118filetype?(040000)119end120def file?121filetype?(0100000)122end123def pipe?124filetype?(010000) # ??? fifo?125end126def socket?127filetype?(0140000)128end129def symlink?130filetype?(0120000)131end132133def ftype134return @@ftypes[(mode & 0170000) >> 13].dup135end136137#138# S_ISUID 0004000 set UID bit139# S_ISGID 0002000 set GID bit (see below)140# S_ISVTX 0001000 sticky bit (see below)141# S_IRWXU 00700 mask for file owner permissions142# S_IRUSR 00400 owner has read permission143# S_IWUSR 00200 owner has write permission144# S_IXUSR 00100 owner has execute permission145# S_IRWXG 00070 mask for group permissions146# S_IRGRP 00040 group has read permission147# S_IWGRP 00020 group has write permission148# S_IXGRP 00010 group has execute permission149# S_IRWXO 00007 mask for permissions for others (not in group)150# S_IROTH 00004 others have read permission151# S_IWOTH 00002 others have write permission152# S_IXOTH 00001 others have execute permission153#154155def perm?(mask)156return true if mode & mask == mask157return false158end159160def setgid?161perm?(02000)162end163def setuid?164perm?(04000)165end166def sticky?167perm?(01000)168end169170def executable?171raise NotImplementedError172end173def executable_real?174raise NotImplementedError175end176def grpowned?177raise NotImplementedError178end179def owned?180raise NotImplementedError181end182def readable?183raise NotImplementedError184end185def readable_real?186raise NotImplementedError187end188def writeable?189raise NotImplementedError190end191def writeable_real?192raise NotImplementedError193end194195#196# Return pretty information about a file's permissions.197#198def prettymode199m = mode200om = '%06o' % m201perms = ''2022033.times {204perms = ((m & 01) == 01 ? 'x' : '-') + perms205perms = ((m & 02) == 02 ? 'w' : '-') + perms206perms = ((m & 04) == 04 ? 'r' : '-') + perms207m >>= 3208}209210return "#{om}/#{perms}"211end212213#214# Return pretty information about a file.215#216def pretty217" Size: #{size} Blocks: #{blocks} IO Block: #{blksize} Type: #{rdev}\n"\218"Device: #{dev} Inode: #{ino} Links: #{nlink}\n"\219" Mode: #{prettymode}\n"\220" Uid: #{uid} Gid: #{gid}\n"\221"Access: #{atime}\n"\222"Modify: #{mtime}\n"\223"Change: #{ctime}\n"224end225226end227end; end # Post/Rex228229230