Class: Ronin::PostExploitation::Resources::FS

Inherits:
Ronin::PostExploitation::Resource show all
Defined in:
lib/ronin/post_exploitation/resources/fs.rb

Overview

Controls the resources of a File System.

Supported Control Methods

The File System resource uses the following control methods, defined by the controller object:

  • fs_getcwd() # => String
  • fs_chdir(path)
  • fs_readlink(path) # => String
  • fs_readdir(path) # => [String, ...]
  • fs_glob(pattern) { |path| ... }
  • fs_mktemp(basename) # => String
  • fs_mkdir(new_path)
  • fs_copy(src,dest)
  • fs_unlink(path)
  • fs_rmdir(path)
  • fs_move(src,dest)
  • fs_link(src,dest)
  • fs_chgrp(group,path)
  • fs_chown(user,path)
  • fs_chmod(mode,path)
  • fs_compare(file1,file2) # => Boolean

Since:

  • 1.0.0

Instance Attribute Summary

Attributes inherited from Ronin::PostExploitation::Resource

#controller

Instance Method Summary collapse

Methods inherited from Ronin::PostExploitation::Resource

#initialize, #requires_method!, resource_method, resource_methods, #supports, #supports?

Constructor Details

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

Instance Method Details

#chdir(path) ⇒ String

Note:

May call the fs_chdir method, if defined by the controller 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.

Since:

  • 1.0.0


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 99

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

  @cwd = if @controller.respond_to?(:fs_chdir)
           @controller.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 controller 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.

Since:

  • 1.0.0


498
499
500
501
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 498

def chgrp(group,path)
  @controller.fs_chgrp(group,join(path))
  return true
end

#chmod(mode, path) ⇒ true

Note:

Requires the fs_chmod method be defined by the controller object.

Changes permissions on one or more file or directorie.

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.

Since:

  • 1.0.0


523
524
525
526
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 523

def chmod(mode,path)
  @controller.fs_chmod(mode,join(path))
  return true
end

#chown(owner, path) ⇒ true

Note:

Requires the fs_chown method be defined by the controller object.

Changes ownership of a file or directory.

Examples:

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

Parameters:

  • owner (user, (user,group))

    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.

Since:

  • 1.0.0


469
470
471
472
473
474
475
476
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 469

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

  chgrp(group,path) if group

  @controller.fs_chown(user,join(path))
  return true
end

#compare(path, other_path) ⇒ Boolean Also known as: cmp

Note:

Requires the fs_compare method be defined by the controller object.

Compares the contents of two files.

Parameters:

  • path (String)

    The path of the first file.

  • other_path (String)

    The path of the second file.

Returns:

  • (Boolean)

    Specifies whether the two files are identical.

Since:

  • 1.0.0


561
562
563
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 561

def compare(path,other_path)
  @controller.fs_compare(path,other_path)
end

#consoleObject

Starts an interactive File System console.

Since:

  • 1.0.0


682
683
684
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 682

def console
  Shells::FS.start(self)
end

#copy(path, new_path) ⇒ true

Note:

Requires the fs_copy method be defined by the controller 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.

Since:

  • 1.0.0


355
356
357
358
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 355

def copy(path,new_path)
  @controller.fs_copy(join(path),join(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.

Since:

  • 1.0.0


614
615
616
617
618
619
620
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 614

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.

Since:

  • 1.0.0


577
578
579
580
581
582
583
584
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 577

def exists?(path)
  begin
    stat(path)
    return true
  rescue Errno::ENOENT
    return false
  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.

Since:

  • 1.0.0


596
597
598
599
600
601
602
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 596

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 controller object.

Gets the current working directory.

Returns:

  • (String)

    The path of the current working directory.

Since:

  • 1.0.0


74
75
76
77
78
79
80
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 74

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

  return @cwd
end

#glob(pattern) {|path| ... } ⇒ Array<String>

Note:

Requires the fs_glob method be defined by the controller 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.

Yield Parameters:

  • path (String)

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

Returns:

  • (Array<String>)

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

Since:

  • 1.0.0


202
203
204
205
206
207
208
209
210
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 202

def glob(pattern,&block)
  path = join(pattern)

  if block
    @controller.fs_glob(pattern,&block)
  else
    @controller.enum_for(:fs_glob,pattern).to_a
  end
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)

Since:

  • 1.0.0


260
261
262
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 260

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

#join(path) ⇒ String

Joins the path with the current working directory.

Parameters:

  • path (String)

    The path to join.

Returns:

  • (String)

    The absolute path.

Since:

  • 1.0.0


127
128
129
130
131
132
133
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 127

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

Requires the fs_link method be defined by the controller 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.

Since:

  • 1.0.0


441
442
443
444
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 441

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

#mkdir(path) ⇒ true

Note:

Requires the fs_mkdir method be defined by the controller object.

Creates a directory.

Parameters:

  • path (String)

    The path of the directory.

Returns:

  • (true)

    Specifies that the directory was successfully created.

Since:

  • 1.0.0


333
334
335
336
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 333

def mkdir(path)
  @controller.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 controller 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.

Since:

  • 1.0.0


417
418
419
420
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 417

def move(path,new_path)
  @controller.fs_move(join(path),join(new_path))
  return true
end

#open(path) {|file| ... } ⇒ File?

Opens a file for reading.

Parameters:

  • path (String)

    The path to file.

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 (File)

    The temporarily opened file.

Returns:

  • (File, nil)

    The newly opened file.

Since:

  • 1.0.0


230
231
232
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 230

def open(path,&block)
  File.open(@controller,join(path),&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.

Since:

  • 1.0.0


632
633
634
635
636
637
638
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 632

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

#read(path) ⇒ String

Reads the contents of a file.

Parameters:

  • path (String)

    The path of the file.

Returns:

  • (String)

    The contents of the file.

Since:

  • 1.0.0


244
245
246
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 244

def read(path)
  open(path).read
end

#readdir(path) ⇒ Dir

Note:

Requires the fs_readdir method be defined by the controller object.

Opens a directory for reading.

Parameters:

  • path (String)

    The path to the directory.

Returns:

  • (Dir)

    The opened directory.

Since:

  • 1.0.0


166
167
168
169
170
171
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 166

def readdir(path)
  path    = join(path)
  entries = @controller.fs_readdir(path)

  return Dir.new(path,entries)
end
Note:

Requires the fs_readlink method be defined by the controller object.

Reads the destination of a link.

Parameters:

  • path (String)

    The path to the link.

Returns:

  • (String)

    The destination of the link.

Since:

  • 1.0.0


148
149
150
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 148

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

#rmdir(path) ⇒ true

Note:

Requires the fs_rmdir method be defined by the controller object.

Removes a directory.

Parameters:

  • path (String)

    The path of the directory.

Returns:

  • (true)

    Specifies that the directory was successfully removed.

Since:

  • 1.0.0


395
396
397
398
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 395

def rmdir(path)
  @controller.fs_rmdir(join(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.

Since:

  • 1.0.0


650
651
652
653
654
655
656
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 650

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

#stat(path) ⇒ File::Stat

Gathers statistics on a file or directory.

Parameters:

  • path (String)

    The path of the file or directory.

Returns:

  • (File::Stat)

    The statistics on the file or directory.

See Also:

  • File::Stat.new

Since:

  • 1.0.0


540
541
542
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 540

def stat(path)
  File::Stat.new(@controller,join(path))
end

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

Note:

Requires the fs_mktemp method be defined by the controller 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 (File)

    The temporarily opened tempfile.

Returns:

  • (File, nil)

    The newly opened tempfile.

Since:

  • 1.0.0


315
316
317
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 315

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

#touch(path) ⇒ nil

Touches a file.

Parameters:

  • path (String)

    The path of the file.

Returns:

  • (nil)

Since:

  • 1.0.0


289
290
291
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 289

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

Requires the fs_unlink method be defined by the controller object.

Unlinks a file.

Parameters:

  • path (String)

    The path of the file.

Returns:

  • (true)

    Specifies that the file was successfully removed.

Since:

  • 1.0.0


374
375
376
377
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 374

def unlink(path)
  @controller.fs_unlink(join(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)

Since:

  • 1.0.0


276
277
278
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 276

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.

Since:

  • 1.0.0


668
669
670
671
672
673
674
# File 'lib/ronin/post_exploitation/resources/fs.rb', line 668

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