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

Conversation

pingport80
Copy link
Contributor

Summary

This PR adds FileStat class for linux systems which can be used to create a stat object containing information about a specific file similar to stat in meterpreter file system API.

Verification Steps

>> passwd = stat('/etc/passwd')
=> 
#<FileStat:0x00007fa2120db4f0
...
>> passwd.size
=> 2809
>> passwd.setuid?
=> false
>> passwd.atime
=> 2021-05-24 19:52:01.071081298 +0530
>> passwd.dev
=> 66309
>> passwd.ctime
=> 2021-04-03 14:55:18.615202365 +0530
>> passwd.mtime
=> 2021-04-03 14:55:18.611202365 +0530
>> mount = stat('/bin/mount')
=> 
#<FileStat:0x0000555af0a18fd8
...
>> mount.setuid?
=> true
>> mount.file?
=> true
>> mount.directory?
=> false
>> mount.symlink?
=> false
>> mount.socket?
=> false
>> mount.pipe?
=> false
>> mount.chardev?
=> false
>> mount.blockdev?
=> false

@adfoster-r7 adfoster-r7 mentioned this pull request May 25, 2021
6 tasks
@smcintyre-r7 smcintyre-r7 self-assigned this May 26, 2021
lib/msf/core/post/file.rb Show resolved Hide resolved
lib/msf/core/post/file_stat.rb Outdated Show resolved Hide resolved
lib/msf/core/post/file_stat.rb Outdated Show resolved Hide resolved
lib/msf/core/post/file.rb Show resolved Hide resolved
lib/msf/core/post/file.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@smcintyre-r7 smcintyre-r7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good to me! I tested a Windows Meterpreter, Windows shell, Python Meterpreter on Linux and a Linux shell session.

Each one behaved as I would expect. The exception in the output is due to Windows shell sessions not being supported yet which is fine since it raises a NotImplementedError.

Testing Output
msf6 post(windows/gather/credentials/gpp) > irb
[*] You are in post/windows/gather/credentials/gpp
irb: warn: can't alias ls from irb_ls.
>> stat('C:\\Users')
=> #<#<Class:0x00007f07a47ee0c0>:0x00000000087d4cd8 @stathash={"st_dev"=>0, "st_mode"=>16749, "st_nlink"=>0, "st_uid"=>0, "st_gid"=>0, "st_rdev"=>0, "st_ino"=>0, "st_size"=>4096, "st_atime"=>1622225745, "st_mtime"=>1585758963, "st_ctime"=>1536991766}>
>> exit
msf6 post(windows/gather/credentials/gpp) > set SESSION 2
SESSION => 2
[*] Starting IRB shell...
[*] You are in post/windows/gather/credentials/gpp

>> stat('C:\\Users')
Traceback (most recent call last):
	25: from ./msfconsole:23:in `<main>'
	24: from /home/smcintyre/Repositories/metasploit-framework.pr/lib/metasploit/framework/command/base.rb:82:in `start'
	23: from /home/smcintyre/Repositories/metasploit-framework.pr/lib/metasploit/framework/command/console.rb:48:in `start'
	22: from /home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/shell.rb:157:in `run'
	21: from /home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/dispatcher_shell.rb:475:in `run_single'
	20: from /home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/dispatcher_shell.rb:475:in `each'
	19: from /home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/dispatcher_shell.rb:481:in `block in run_single'
	18: from /home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/dispatcher_shell.rb:532:in `run_command'
	17: from /home/smcintyre/Repositories/metasploit-framework.pr/lib/msf/ui/console/command_dispatcher/developer.rb:116:in `cmd_irb'
	16: from /home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/irb_shell.rb:52:in `run'
	15: from /home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/irb_shell.rb:52:in `catch'
	14: from /home/smcintyre/Repositories/metasploit-framework.pr/lib/rex/ui/text/irb_shell.rb:53:in `block in run'
	 1: from (irb):1:in `<main>'
/home/smcintyre/Repositories/metasploit-framework.pr/lib/msf/core/post/file.rb:691:in `stat': NotImplementedError (NotImplementedError)
>> exit
msf6 post(windows/gather/credentials/gpp) > set SESSION 3
SESSION => 3
msf6 post(windows/gather/credentials/gpp) > irb
[*] Starting IRB shell...
[*] You are in post/windows/gather/credentials/gpp

>> stat('/etc')
=> #<#<Class:0x0000000009044558>:0x0000000005916ab8 @stathash={"st_dev"=>64768, "st_mode"=>16877, "st_nlink"=>190, "st_uid"=>0, "st_gid"=>0, "st_rdev"=>0, "st_ino"=>1441793, "st_size"=>12288, "st_atime"=>1622205354, "st_mtime"=>1622205328, "st_ctime"=>1622205328}>
>> exit
msf6 post(windows/gather/credentials/gpp) > set SESSION 4
SESSION => 4
msf6 post(windows/gather/credentials/gpp) > irb
[*] Starting IRB shell...
[*] You are in post/windows/gather/credentials/gpp

>> stat('/etc/')
=> #<Msf::Post::File::FileStat:0x00007f07a5380960 @stathash={"st_dev"=>64768, "st_ino"=>1441793, "st_nlink"=>190, "st_uid"=>0, "st_gid"=>0, "st_rdev"=>0, "st_size"=>12288, "st_blksize"=>512, "st_blocks"=>4096, "st_atime"=>1622205354, "st_mtime"=>1622205328, "st_ctime"=>1622205328, "st_mode"=>16877}>
>> 

update file.rb and file_stat.rb

update file_stat.rb

fix Zeitwerk:NameError

upadte `setuid?` to use stat

Update lib/msf/core/post/file_stat.rb

Co-authored-by: Spencer McIntyre <58950994+smcintyre-r7@users.noreply.github.com>

add inheritance and change time conversions to integers

Update lib/msf/core/post/file.rb

Co-authored-by: Spencer McIntyre <58950994+smcintyre-r7@users.noreply.github.com>

add some checks

fix one error in `Rex::Post::FileStat`

add NotImplemented raise
@smcintyre-r7 smcintyre-r7 merged commit 882c272 into rapid7:master May 28, 2021
@smcintyre-r7
Copy link
Contributor

smcintyre-r7 commented May 28, 2021

Release Notes

Added support for obtaining a stat object from the Post API via shell sessions when the stat command is available.

@pbarry-r7 pbarry-r7 added the rn-enhancement release notes enhancement label Jun 3, 2021
@smcintyre-r7 smcintyre-r7 added the GSoC Google Summer of Code project PRs label Jun 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement GSoC Google Summer of Code project PRs library rn-enhancement release notes enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants