Class: Ronin::PostEx::System::FS

Inherits:
Resource
  • Object
show all
Defined in:
lib/ronin/post_ex/system/fs.rb

Overview

Provides access to a system's a File System (FS).

Supported API Methods

The File System resource uses the following post-exploitation methods, defined by the Resource#session object:

  • fs_getcwd() -> String
  • fs_chdir(path : String)
  • fs_readlink(path : String) -> String
  • fs_readdir(path : String) -> Array[String]
  • fs_glob(pattern : String) -> Array[String]
  • fs_mktemp(basename : String) -> String
  • fs_mkdir(new_path : String)
  • fs_copy(src : String, dest : String)
  • fs_unlink(path : String)
  • fs_rmdir(path : String)
  • fs_move(src : String, dest : String)
  • fs_link(src : String, dest : String)
  • fs_chgrp(group : String, path : String)
  • fs_chown(user : String, path : String)
  • fs_chmod(mode : Integer, path : String)
  • fs_stat(path : String) => Hash[Symbol, Object] | nil

Instance Attribute Summary

Attributes inherited from Resource

#session

Instance Method Summary collapse

Methods inherited from Resource

#initialize, #interact, #supports, #supports?

Constructor Details

This class inherits a constructor from Ronin::PostEx::Resource

Instance Method Details

#chdir(path) ⇒ String

Note:

May call the fs_chdir method, if defined by the Resource#session object.

Changes the current working directory.

Parameters:

  • path (String)

    The path to use as the new current working directory.

Returns:

  • (String)

    The new current working directory.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ronin/post_ex/system/fs.rb', line 93

def chdir(path)
  path = expand_path(path)
  old_cwd = @cwd

  @cwd = if @session.respond_to?(:fs_chdir)
           @session.fs_chdir(path)
         else
           path
         end

  if block_given?
    yield @cwd
    chdir(old_cwd)
  end

  return @cwd
end

#chgrp(group, path) ⇒ true

Note:

Requires the fs_chgrp method be defined by the Resource#session object.

Changes group ownership on one or more files or directories.

Examples:

exploit.fs.chgrp('www', 'one.html')

Parameters:

  • group (String)

    The group that will own the file or directory.

  • path (String)

    The path of the file or directory.

Returns:

  • (true)

    Specifies that the group ownership was successfully changed.



490
491
492
493
# File 'lib/ronin/post_ex/system/fs.rb', line 490

def chgrp(group,path)
  @session.fs_chgrp(group,expand_path(path))
  return true
end

#chmod(mode, path) ⇒ true

Note:

Requires the fs_chmod method be defined by the Resource#session object.

Changes permissions on one or more file or directory.

Examples:

exploit.fs.chmod(0665, 'one.html')

Parameters:

  • mode (Integer)

    The new mode for the file or directory.

  • path (String)

    The path of the file or directory.

Returns:

  • (true)

    Specifies that the permissions were successfully changed.



514
515
516
517
# File 'lib/ronin/post_ex/system/fs.rb', line 514

def chmod(mode,path)
  @session.fs_chmod(mode,expand_path(path))
  return true
end

#chown(owner, path) ⇒ true

Note:

Requires the fs_chown method be defined by the Resource#session object.

Changes ownership of a file or directory.

Examples:

exploit.fs.chown('www', 'one.html')
exploit.fs.chown(['alice', 'users'], 'one.html')

Parameters:

  • owner (String, (String,String))

    the user and/or group that will own the file or directory.

  • path (String)

    The path of the file or directory.

Returns:

  • (true)

    Specifies that the ownership was successfully changed.



462
463
464
465
466
467
468
469
# File 'lib/ronin/post_ex/system/fs.rb', line 462

def chown(owner,path)
  user, group = owner

  chgrp(group,path) if group

  @session.fs_chown(user,expand_path(path))
  return true
end

#copy(path, new_path) ⇒ true

Note:

Requires the fs_copy method be defined by the Resource#session object.

Copies a file.

Parameters:

  • path (String)

    The path of the file to copy.

  • new_path (String)

    The destination path to copy to.

Returns:

  • (true)

    Specifies that the file was successfully copied.



353
354
355
356
# File 'lib/ronin/post_ex/system/fs.rb', line 353

def copy(path,new_path)
  @session.fs_copy(expand_path(path),expand_path(new_path))
  return true
end

#directory?(path) ⇒ Boolean

Tests whether a directory exists.

Parameters:

  • path (String)

    The path of the directory in question.

Returns:

  • (Boolean)

    Specifies whether the directory exists.



582
583
584
585
586
587
588
# File 'lib/ronin/post_ex/system/fs.rb', line 582

def directory?(path)
  begin
    stat(path).directory?
  rescue Errno::ENOENT
    return false
  end
end

#exists?(path) ⇒ Boolean

Tests whether a file or directory exists.

Parameters:

  • path (String)

    The path of the file or directory in question.

Returns:

  • (Boolean)

    Specifies whether the file or directory exists.



545
546
547
548
549
550
551
552
# File 'lib/ronin/post_ex/system/fs.rb', line 545

def exists?(path)
  begin
    stat(path)
    return true
  rescue Errno::ENOENT
    return false
  end
end

#expand_path(path) ⇒ String

Joins the path with the current working directory.

Parameters:

  • path (String)

    The path to join.

Returns:

  • (String)

    The absolute path.



121
122
123
124
125
126
127
# File 'lib/ronin/post_ex/system/fs.rb', line 121

def expand_path(path)
  if (@cwd && path[0,1] != '/')
    ::File.expand_path(::File.join(@cwd,path))
  else
    path
  end
end

#file?(path) ⇒ Boolean

Tests whether a file exists.

Parameters:

  • path (String)

    The path of the file in question.

Returns:

  • (Boolean)

    Specifies whether the file exists.



564
565
566
567
568
569
570
# File 'lib/ronin/post_ex/system/fs.rb', line 564

def file?(path)
  begin
    stat(path).file?
  rescue Errno::ENOENT
    return false
  end
end

#getcwdString Also known as: getwd, pwd

Note:

May call the fs_getcwd method, if defined by the Resource#session object.

Gets the current working directory.

Returns:

  • (String)

    The path of the current working directory.



68
69
70
71
72
73
74
# File 'lib/ronin/post_ex/system/fs.rb', line 68

def getcwd
  if @session.respond_to?(:fs_getcwd)
    @cwd = @session.fs_getcwd
  end

  return @cwd
end

#glob(pattern) {|path| ... } ⇒ String+

Note:

Requires the fs_glob method be defined by the Resource#session object.

Searches the file-system for matching paths.

Examples:

exploit.fs.glob('*.txt')
# => [...]
exploit.fs.glob('**/*.xml') do |path|
  # ...
end

Parameters:

  • pattern (String)

    A path-glob pattern.

Yields:

  • (path)

    The given block, will be passed each matching path.

Returns:

  • (String)

    path A path in the file-system that matches the pattern.

  • (Array<String>)

    If no block is given, the matching paths will be returned.



213
214
215
216
217
218
219
# File 'lib/ronin/post_ex/system/fs.rb', line 213

def glob(pattern,&block)
  path  = expand_path(pattern)
  paths = @session.fs_glob(pattern)

  paths.each(&block) if block
  return paths
end

#hexdump(path, output = STDOUT) ⇒ nil

Hexdumps the contents of a file.

Parameters:

  • path (String)

    The path of the file.

  • output (IO) (defaults to: STDOUT)

    The output stream to write the hexdump to.

Returns:

  • (nil)


261
262
263
# File 'lib/ronin/post_ex/system/fs.rb', line 261

def hexdump(path,output=STDOUT)
  open(path) { |file| Hexdump.dump(file,output: output) }
end
Note:

Requires the fs_link method be defined by the Resource#session object.

Creates a symbolic link.

Parameters:

  • path (String)

    The path that the link will point to.

  • new_path (String)

    The path of the link.

Returns:

  • (true)

    Specifies that the symbolic link was successfully created.



435
436
437
438
# File 'lib/ronin/post_ex/system/fs.rb', line 435

def link(path,new_path)
  @session.fs_link(path,new_path)
  return true
end

#mkdir(path) ⇒ true

Note:

Requires the fs_mkdir method be defined by the Resource#session object.

Creates a directory.

Parameters:

  • path (String)

    The path of the directory.

Returns:

  • (true)

    Specifies that the directory was successfully created.



332
333
334
335
# File 'lib/ronin/post_ex/system/fs.rb', line 332

def mkdir(path)
  @session.fs_mkdir(path)
  return true
end

#move(path, new_path) ⇒ true Also known as: rename

Note:

Requires the fs_move method be defined by the Resource#session object.

Moves a file or directory.

Parameters:

  • path (String)

    The path of the file or directory to be moved.

  • new_path (String)

    The destination path for the file or directory to be moved to.

Returns:

  • (true)

    Specifies that the file or directory was successfully moved.



412
413
414
415
# File 'lib/ronin/post_ex/system/fs.rb', line 412

def move(path,new_path)
  @session.fs_move(expand_path(path),expand_path(new_path))
  return true
end

#open(path, mode = 'r') {|file| ... } ⇒ RemoteFile?

Opens a file for reading.

Parameters:

  • path (String)

    The path to file.

  • mode (String) (defaults to: 'r')

    The mode to open the file in.

Yields:

  • (file)

    If a block is given, it will be passed the newly opened file. After the block has returned, the file will be closed and nil will be returned.

Yield Parameters:

  • file (RemoteFile)

    The temporarily opened remote file.

Returns:

  • (RemoteFile, nil)

    If no block was given, then the newly opened remote file will be returned.

See Also:



245
246
247
# File 'lib/ronin/post_ex/system/fs.rb', line 245

def open(path,mode='r',&block)
  RemoteFile.open(@session,expand_path(path),mode,&block)
end

#pipe?(path) ⇒ Boolean

Tests whether a FIFO pipe exists.

Parameters:

  • path (String)

    The path of the FIFO pipe in question.

Returns:

  • (Boolean)

    Specifies whether the FIFO pipe exists.



600
601
602
603
604
605
606
# File 'lib/ronin/post_ex/system/fs.rb', line 600

def pipe?(path)
  begin
    stat(path).pipe?
  rescue Errno::ENOENT
    return false
  end
end

#readdir(path) ⇒ RemoteDir

Note:

Requires the fs_readdir method be defined by the Resource#session object.

Opens a directory for reading.

Parameters:

  • path (String)

    The path to the directory.

Returns:



178
179
180
181
182
183
# File 'lib/ronin/post_ex/system/fs.rb', line 178

def readdir(path)
  path    = expand_path(path)
  entries = @session.fs_readdir(path)

  return RemoteDir.new(path,entries)
end

#readfile(path) ⇒ String

Note:

Requires the fs_readfile method be defined by the Resource#session object.

Reads the full contents of a file.

Parameters:

  • path (String)

    The path to the file.

Returns:

  • (String)

    The contents of the file.



142
143
144
# File 'lib/ronin/post_ex/system/fs.rb', line 142

def readfile(path)
  @session.fs_readfile(path)
end
Note:

Requires the fs_readlink method be defined by the Resource#session object.

Reads the destination of a link.

Parameters:

  • path (String)

    The path to the link.

Returns:

  • (String)

    The destination of the link.



160
161
162
# File 'lib/ronin/post_ex/system/fs.rb', line 160

def readlink(path)
  @session.fs_readlink(path)
end

#rmdir(path) ⇒ true

Note:

Requires the fs_rmdir method be defined by the Resource#session object.

Removes a directory.

Parameters:

  • path (String)

    The path of the directory.

Returns:

  • (true)

    Specifies that the directory was successfully removed.



391
392
393
394
# File 'lib/ronin/post_ex/system/fs.rb', line 391

def rmdir(path)
  @session.fs_rmdir(expand_path(path))
  return true
end

#socket?(path) ⇒ Boolean

Tests whether a UNIX socket exists.

Parameters:

  • path (String)

    The path of the UNIX socket in question.

Returns:

  • (Boolean)

    Specifies whether the UNIX socket exists.



618
619
620
621
622
623
624
# File 'lib/ronin/post_ex/system/fs.rb', line 618

def socket?(path)
  begin
    stat(path).socket?
  rescue Errno::ENOENT
    return false
  end
end

#stat(path) ⇒ RemoteFile::Stat

Gathers statistics on a file or directory.

Parameters:

  • path (String)

    The path of the file or directory.

Returns:

See Also:



531
532
533
# File 'lib/ronin/post_ex/system/fs.rb', line 531

def stat(path)
  RemoteFile::Stat.new(@session,expand_path(path))
end

#tmpfile(basename) {|tempfile| ... } ⇒ RemoteFile?

Note:

Requires the fs_mktemp method be defined by the Resource#session object.

Opens a tempfile.

Parameters:

  • basename (String)

    The base-name to use in the tempfile.

Yields:

  • (tempfile)

    The given block will be passed the newly opened tempfile. After the block has returned, the tempfile will be closed and nil will be returned.

Yield Parameters:

  • tempfile (RemoteFile)

    The temporarily opened tempfile.

Returns:

  • (RemoteFile, nil)

    The newly opened tempfile.



315
316
317
# File 'lib/ronin/post_ex/system/fs.rb', line 315

def tmpfile(basename,&block)
  open(@session.fs_mktemp(basename),&block)
end

#touch(path) ⇒ nil

Touches a file.

Parameters:

  • path (String)

    The path of the file.

Returns:

  • (nil)


290
291
292
# File 'lib/ronin/post_ex/system/fs.rb', line 290

def touch(path)
  open(path) { |file| file << '' }
end
Note:

Requires the fs_unlink method be defined by the Resource#session object.

Unlinks a file.

Parameters:

  • path (String)

    The path of the file.

Returns:

  • (true)

    Specifies that the file was successfully removed.



371
372
373
374
# File 'lib/ronin/post_ex/system/fs.rb', line 371

def unlink(path)
  @session.fs_unlink(expand_path(path))
  return true
end

#write(path, data) ⇒ nil

Writes data to a file.

Parameters:

  • path (String)

    The path to the file.

  • data (String)

    The data to write.

Returns:

  • (nil)


277
278
279
# File 'lib/ronin/post_ex/system/fs.rb', line 277

def write(path,data)
  open(path) { |file| file.write(data) }
end

#zero?(path) ⇒ Boolean Also known as: empty?

Tests whether a file is empty.

Parameters:

  • path (String)

    The path of the file in question.

Returns:

  • (Boolean)

    Specifies whether the file is empty.



636
637
638
639
640
641
642
# File 'lib/ronin/post_ex/system/fs.rb', line 636

def zero?(path)
  begin
    stat(path).zero?
  rescue Errno::ENOENT
    return false
  end
end