Class: Ronin::PostExploitation::File

Inherits:
Resource
  • Object
show all
Includes:
IO
Defined in:
lib/ronin/post_exploitation/file/file.rb,
lib/ronin/post_exploitation/file/stat.rb

Overview

The File class represents files on a remote system. File requires the controller object to define either fs_read and/or fs_write. Additionally, File can optionally use the fs_open, fs_close, fs_tell, fs_seek and fs_stat methods.

Since:

  • 1.0.0

Defined Under Namespace

Classes: Stat

Instance Attribute Summary

Attributes included from IO

#eof, #fd, #pos

Attributes inherited from Resource

#controller

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IO

#append_buffer, #autoclose=, #autoclose?, #binmode, #binmode?, #clear_buffer!, #close, #close_on_exec=, #close_on_exec?, #close_read, #close_write, #closed?, #each_block, #each_byte, #each_char, #each_codepoint, #each_line, #empty_buffer?, #eof?, #fileno, #flush, #fsync, #getbyte, #getc, #gets, #io_initialize, #isatty, #lineno, #lineno=, #open, #pid, #prepend_buffer, #print, #printf, #putc, #puts, #read, #read_buffer, #readbyte, #readchar, #readline, #readlines, #readpartial, #rewind, #sync, #sync=, #sysseek, #to_io, #tty?, #ungetbyte, #ungetc, #write

Methods inherited from Resource

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

Constructor Details

#initialize(controller, path, mode = 'r') ⇒ File

Creates a new remote controlled File object.

Since:

  • 1.0.0


52
53
54
55
56
57
58
# File 'lib/ronin/post_exploitation/file/file.rb', line 52

def initialize(controller,path,mode='r')
  @controller = controller
  @path       = path.to_s
  @mode       = mode.to_s

  io_initialize
end

Class Method Details

.open(controller, path) {|file| ... } ⇒ Object

Opens a file.

Yields:

  • (file)

    The given block will be passed the newly created file object. When the block has returned, the File object will be closed.

Yield Parameters:

  • The (File)

    newly created file object.

Since:

  • 1.0.0


76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ronin/post_exploitation/file/file.rb', line 76

def self.open(controller,path)
  io = new(controller,path)

  if block_given?
    value = yield(io)

    io.close
    return value
  else
    return io
  end
end

Instance Method Details

#fcntl(command, argument) ⇒ Object

Executes a low-level command to control or query the file stream.

Raises:

  • (RuntimeError)

    The controller object does not define fs_fcntl.

Since:

  • 1.0.0


157
158
159
160
161
162
163
# File 'lib/ronin/post_exploitation/file/file.rb', line 157

def fcntl(command,argument)
  unless @controller.respond_to?(:fs_fcntl)
    raise(RuntimeError,"#{@controller.inspect} does not define fs_fcntl")
  end

  return @controller.fs_fcntl(command,argument)
end

#inspectString

Inspects the open file.

Since:

  • 1.0.0


200
201
202
# File 'lib/ronin/post_exploitation/file/file.rb', line 200

def inspect
  "#<#{self.class}:#{@path}>"
end

#io_closeObject (protected)

Attempts calling fs_close from the controller object to close the file.

Since:

  • 1.0.0


270
271
272
273
274
# File 'lib/ronin/post_exploitation/file/file.rb', line 270

def io_close
  if @controller.respond_to?(:fs_close)
    @controller.fs_close(@fd)
  end
end

#io_openObject (protected)

Attempts calling fs_open from the controller object to open the remote file.

Since:

  • 1.0.0


213
214
215
216
217
218
219
# File 'lib/ronin/post_exploitation/file/file.rb', line 213

def io_open
  if @controller.respond_to?(:fs_open)
    @controller.fs_open(@path,@mode)
  else
    @path
  end
end

#io_readString? (protected)

Reads a block from the remote file by calling fs_read or fs_readfile from the controller object.

Raises:

  • (IOError)

    The controller object does not define fs_read or fs_readfile.

Since:

  • 1.0.0


232
233
234
235
236
237
238
239
240
241
# File 'lib/ronin/post_exploitation/file/file.rb', line 232

def io_read
  if @controller.respond_to?(:fs_readfile)
    @eof = true
    @controller.fs_readfile(@path)
  elsif @controller.respond_to?(:fs_read)
    @controller.fs_read(@fd,@pos)
  else
    raise(IOError,"#{@controller.inspect} does not support reading")
  end
end

#io_write(data) ⇒ Integer (protected)

Writes data to the remote file by calling fs_write from the controller object.

Raises:

  • (IOError)

    The controller object does not define fs_write.

Since:

  • 1.0.0


257
258
259
260
261
262
263
# File 'lib/ronin/post_exploitation/file/file.rb', line 257

def io_write(data)
  if @controller.respond_to?(:fs_write)
    @pos += @controller.fs_write(@fd,@pos,data)
  else
    raise(IOError,"#{@controller.inspect} does not support writing to files")
  end
end

#ioctl(command, argument) ⇒ Object

Executes a low-level command to control or query the IO stream.

Raises:

  • (RuntimeError)

    The controller object does not define fs_ioctl.

Since:

  • 1.0.0


136
137
138
139
140
141
142
# File 'lib/ronin/post_exploitation/file/file.rb', line 136

def ioctl(command,argument)
  unless @controller.respond_to?(:fs_ioctl)
    raise(RuntimeError,"#{@controller.inspect} does not define fs_ioctl")
  end

  return @controller.fs_ioctl(command,argument)
end

#reopen(path) ⇒ File

Re-opens the file.

Since:

  • 1.0.0


175
176
177
178
179
180
# File 'lib/ronin/post_exploitation/file/file.rb', line 175

def reopen(path)
  close

  @path = path.to_s
  return open
end

#seek(new_pos, whence = SEEK_SET) ⇒ Integer

Sets the position in the file to read.

Since:

  • 1.0.0


98
99
100
101
102
103
104
105
106
# File 'lib/ronin/post_exploitation/file/file.rb', line 98

def seek(new_pos,whence=SEEK_SET)
  clear_buffer!

  if @controller.respond_to?(:fs_seek)
    @controller.fs_seek(@fd,new_pos,whence)
  end

  @pos = new_pos
end

#statStat

The status information for the file.

Since:

  • 1.0.0


189
190
191
# File 'lib/ronin/post_exploitation/file/file.rb', line 189

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

#tellInteger

The current offset in the file.

Since:

  • 1.0.0


115
116
117
118
119
120
121
# File 'lib/ronin/post_exploitation/file/file.rb', line 115

def tell
  if @controller.respond_to?(:fs_tell)
    @pos = @controller.fs_tell(@fd)
  else
    @pos
  end
end