# -*- coding: binary -*-1module Rex2module Post34##5# Base IO class that is modeled after the ruby IO class.6#7# This is an abstract base class that defines the interface for8# post-exploitation I/O operations. Subclasses must implement9# the actual I/O functionality.10##11class IO12protected13attr_accessor :filed, :mode14public1516##17# Conditionals18##1920# Checks if the end of file has been reached.21#22# @return [Boolean] true if at end of file23def eof?24return eof25end2627# Checks if the I/O stream is closed.28#29# @raise [NotImplementedError] Must be implemented by subclass30#31# @return [Boolean] true if stream is closed32def closed?33raise NotImplementedError34end3536# Checks if the I/O stream is a terminal device.37#38# @return [Boolean] true if stream is a TTY39def tty?40return isatty41end4243##44# I/O operations45##4647# Sets the stream to binary mode.48#49# @raise [NotImplementedError] Must be implemented by subclass50#51# @return [IO] self52def binmode53raise NotImplementedError54end5556# Closes the I/O stream.57#58# @raise [NotImplementedError] Must be implemented by subclass59#60# @return [nil]61def close62raise NotImplementedError63end6465# Closes the read end of a duplex I/O stream.66#67# @raise [NotImplementedError] Must be implemented by subclass68#69# @return [nil]70def close_read71raise NotImplementedError72end7374# Closes the write end of a duplex I/O stream.75#76# @raise [NotImplementedError] Must be implemented by subclass77#78# @return [nil]79def close_write80raise NotImplementedError81end8283# Iterates over each line in the stream.84#85# @param sep [String] line separator (default: $/)86# @yield [String] each line from the stream87#88# @raise [NotImplementedError] Must be implemented by subclass89#90# @return [void]91def each(sep = $/, &block)92raise NotImplementedError93end9495# Alias for {#each}.96#97# @param sep [String] line separator (default: $/)98# @yield [String] each line from the stream99#100# @raise [NotImplementedError] Must be implemented by subclass101#102# @return [void]103def each_line(sep = $/, &block)104raise NotImplementedError105end106107# Iterates over each byte in the stream.108#109# @yield [Integer] each byte from the stream110#111# @raise [NotImplementedError] Must be implemented by subclass112#113# @return [void]114def each_byte(&block)115raise NotImplementedError116end117118# Checks if end of file has been reached.119#120# @raise [NotImplementedError] Must be implemented by subclass121#122# @return [Boolean] true if at end of file123def eof124raise NotImplementedError125end126127# Performs low-level file control operation.128#129# @param cmd [Integer] control command130# @param arg [Integer, String] command argument131#132# @raise [NotImplementedError] Must be implemented by subclass133#134# @return [Integer] result of operation135def fcntl(cmd, arg)136raise NotImplementedError137end138139# Flushes buffered data to the underlying I/O stream.140#141# @raise [NotImplementedError] Must be implemented by subclass142#143# @return [IO] self144def flush145raise NotImplementedError146end147148# Synchronizes all buffered data with the storage device.149#150# @raise [NotImplementedError] Must be implemented by subclass151#152# @return [0] zero on success153def fsync154raise NotImplementedError155end156157# Reads a single character from the stream.158#159# @raise [NotImplementedError] Must be implemented by subclass160#161# @return [Integer, nil] character code or nil at EOF162def getc163raise NotImplementedError164end165166# Reads the next line from the stream.167#168# @param sep [String] line separator (default: $/)169#170# @raise [NotImplementedError] Must be implemented by subclass171#172# @return [String, nil] next line or nil at EOF173def gets(sep = $/)174raise NotImplementedError175end176177# Performs low-level I/O control operation.178#179# @param cmd [Integer] control command180# @param arg [Integer, String] command argument181#182# @raise [NotImplementedError] Must be implemented by subclass183#184# @return [Integer] result of operation185def ioctl(cmd, arg)186raise NotImplementedError187end188189# Checks if the stream is associated with a terminal device.190#191# @raise [NotImplementedError] Must be implemented by subclass192#193# @return [Boolean] true if stream is a TTY194def isatty195raise NotImplementedError196end197198# Gets the current line number.199#200# @raise [NotImplementedError] Must be implemented by subclass201#202# @return [Integer] current line number203def lineno204raise NotImplementedError205end206207# Gets the current file position.208#209# @raise [NotImplementedError] Must be implemented by subclass210#211# @return [Integer] current byte offset212def pos213raise NotImplementedError214end215216# Writes a string to the stream.217#218# @raise [NotImplementedError] Must be implemented by subclass219#220# @return [Integer] number of bytes written221def print222raise NotImplementedError223end224225# Writes a formatted string to the stream.226#227# @param fmt [String] format string228# @param args [Array] values to format229#230# @raise [NotImplementedError] Must be implemented by subclass231#232# @return [nil]233def printf(fmt, *args)234raise NotImplementedError235end236237# Writes a character to the stream.238#239# @param obj [Integer] character code to write240#241# @raise [NotImplementedError] Must be implemented by subclass242#243# @return [Integer] the character code written244def putc(obj)245raise NotImplementedError246end247248# Writes a string followed by newline to the stream.249#250# @param obj [String] data to write251#252# @raise [NotImplementedError] Must be implemented by subclass253#254# @return [nil]255def puts(obj)256raise NotImplementedError257end258259# Reads data from the stream.260#261# @param length [Integer, nil] number of bytes to read (nil = read all)262# @param buffer [String, nil] buffer to read into263#264# @raise [NotImplementedError] Must be implemented by subclass265#266# @return [String, nil] data read or nil at EOF267def read(length = nil, buffer = nil)268raise NotImplementedError269end270271# Reads a single character as a string.272#273# @raise [NotImplementedError] Must be implemented by subclass274#275# @return [String, nil] character or nil at EOF276def readchar277raise NotImplementedError278end279280# Reads the next line from the stream.281#282# @param sep [String] line separator (default: $/)283#284# @raise [NotImplementedError] Must be implemented by subclass285#286# @return [String] next line287# @raise [EOFError] if at end of file288def readline(sep = $/)289raise NotImplementedError290end291292# Reads all lines from the stream into an array.293#294# @param sep [String] line separator (default: $/)295#296# @raise [NotImplementedError] Must be implemented by subclass297#298# @return [Array<String>] array of lines299def readlines(sep = $/)300raise NotImplementedError301end302303# Repositions the stream to the beginning.304#305# @raise [NotImplementedError] Must be implemented by subclass306#307# @return [0] zero on success308def rewind309raise NotImplementedError310end311312# Repositions the file pointer.313#314# @param offset [Integer] byte offset315# @param whence [Integer] position reference (SEEK_SET, SEEK_CUR, SEEK_END)316#317# @raise [NotImplementedError] Must be implemented by subclass318#319# @return [0] zero on success320def seek(offset, whence = SEEK_SET)321raise NotImplementedError322end323324# Gets file status information.325#326# @raise [NotImplementedError] Must be implemented by subclass327#328# @return [File::Stat] file status object329def stat330raise NotImplementedError331end332333# Synchronizes the stream with the underlying storage.334#335# @raise [NotImplementedError] Must be implemented by subclass336#337# @return [IO] self338def sync339raise NotImplementedError340end341342# Reads data at the system level.343#344# @param length [Integer] number of bytes to read345#346# @raise [NotImplementedError] Must be implemented by subclass347#348# @return [String] data read349def sysread(length)350raise NotImplementedError351end352353# Repositions the file pointer at the system level.354#355# @param offset [Integer] byte offset356# @param whence [Integer] position reference (SEEK_SET, SEEK_CUR, SEEK_END)357#358# @raise [NotImplementedError] Must be implemented by subclass359#360# @return [Integer] new file offset361def sysseek(offset, whence = SEEK_SET)362raise NotImplementedError363end364365# Writes data to the stream at the OS level.366#367# @param buf [String] data to write368#369# @raise [NotImplementedError] Must be implemented by subclass370#371# @return [Integer] number of bytes written372def syswrite(buf)373raise NotImplementedError374end375376# Gets the current file position (alias for {#pos}).377#378# @return [Integer] current byte offset379def tell380return pos381end382383# Pushes a character back onto the stream.384#385# @param val [Integer] character code to push back386#387# @raise [NotImplementedError] Must be implemented by subclass388#389# @return [nil]390def ungetc(val)391raise NotImplementedError392end393394# Writes data to the stream.395#396# @param buf [String] data to write397#398# @raise [NotImplementedError] Must be implemented by subclass399#400# @return [Integer] number of bytes written401def write(buf)402raise NotImplementedError403end404405end406407end; end # Post/Rex408409410