Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file_stat to linux shell sessions #15251

Merged
merged 1 commit into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 40 additions & 12 deletions lib/msf/core/post/file.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: binary -*-
#
require 'rex/post/meterpreter/extensions/stdapi/command_ids'
require 'rex/post/file_stat'

module Msf::Post::File

Expand Down Expand Up @@ -160,18 +161,8 @@ def file?(path)
#
# @param path [String] Remote filename to check
def setuid?(path)
if session.type == 'meterpreter'
stat = session.fs.file.stat(path) rescue nil
return false unless stat
return stat.setuid?
else
if session.platform != 'windows'
f = session.shell_command_token("test -u \"#{path}\" && echo true")
end
return false if f.nil? || f.empty?
return false unless f =~ /true/
true
end
stat = stat(path)
stat.setuid?
smcintyre-r7 marked this conversation as resolved.
Show resolved Hide resolved
end

#
Expand Down Expand Up @@ -692,4 +683,41 @@ def _unix_max_line_length

line_max
end

def stat(filename)
if session.type == 'meterpreter'
return session.fs.file.stat(filename)
else
raise NotImplementedError if session.platform == 'windows'
raise "`stat' command doesn't exist on target system" unless command_exists?('stat')
return FileStat.new(filename, session)
smcintyre-r7 marked this conversation as resolved.
Show resolved Hide resolved
end
end


class FileStat < Rex::Post::FileStat

attr_accessor :stathash

def initialize(filename, session)
data = session.shell_command_token("stat --format='%d,%i,%h,%u,%g,%t,%s,%B,%o,%X,%Y,%Z,%f' '#{filename}'").to_s.chomp
raise 'format argument of stat command not behaving as expected' unless data =~ /(\d+,){12}\w+/
data = data.split(",")
smcintyre-r7 marked this conversation as resolved.
Show resolved Hide resolved
@stathash = Hash.new
@stathash['st_dev'] = data[0].to_i
@stathash['st_ino'] = data[1].to_i
@stathash['st_nlink'] = data[2].to_i
@stathash['st_uid'] = data[3].to_i
@stathash['st_gid'] = data[4].to_i
@stathash['st_rdev'] = data[5].to_i
@stathash['st_size'] = data[6].to_i
@stathash['st_blksize'] = data[7].to_i
@stathash['st_blocks'] = data[8].to_i
@stathash['st_atime'] = data[9].to_i
@stathash['st_mtime'] = data[10].to_i
@stathash['st_ctime'] = data[11].to_i
@stathash['st_mode'] = data[12].to_i(16) #stat command returns hex value of mode"
end
end

end
4 changes: 2 additions & 2 deletions lib/rex/post/file_stat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ def pipe?
filetype?(010000) # ??? fifo?
end
def socket?
filetype(0140000)
filetype?(0140000)
end
def symlink?
filetype(0120000)
filetype?(0120000)
end

def ftype
Expand Down