code
stringlengths
26
124k
docstring
stringlengths
23
125k
func_name
stringlengths
1
98
language
stringclasses
1 value
repo
stringlengths
5
53
path
stringlengths
7
151
url
stringlengths
50
211
license
stringclasses
7 values
def file @file ||= Operations::FileFactory.new(self) end
Returns an Net::SFTP::Operations::FileFactory instance, which can be used to mimic synchronous, IO-like file operations on a remote file via SFTP. sftp.file.open("/path/to/file") do |file| while line = file.gets puts line end end See Net::SFTP::Operations::FileFactory and Net::SFTP::Operations::File for more details.
file
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def dir @dir ||= Operations::Dir.new(self) end
Returns a Net::SFTP::Operations::Dir instance, which can be used to conveniently iterate over and search directories on the remote server. sftp.dir.glob("/base/path", "*/**/*.rb") do |entry| p entry.name end See Net::SFTP::Operations::Dir for a more detailed discussion of how to use this.
dir
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def open(path, flags="r", options={}, &callback) request :open, path, flags, options, &callback end
:call-seq: open(path, flags="r", options={}) -> request open(path, flags="r", options={}) { |response| ... } -> request Opens a file on the remote server. The +flags+ parameter determines how the flag is open, and accepts the same format as IO#open (e.g., either a string like "r" or "w", or a combination of the IO constants). The +options+ parameter is a hash of attributes to be associated with the file, and varies greatly depending on the SFTP protocol version in use, but some (like :permissions) are always available. Returns immediately with a Request object. If a block is given, it will be invoked when the server responds, with a Response object as the only parameter. The :handle property of the response is the handle of the opened file, and may be passed to other methods (like #close, #read, #write, and so forth). sftp.open("/path/to/file") do |response| raise "fail!" unless response.ok? sftp.close(response[:handle]) end sftp.loop
open
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def open!(path, flags="r", options={}, &callback) wait_for(open(path, flags, options, &callback), :handle) end
Identical to #open, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the handle of the newly opened file. handle = sftp.open!("/path/to/file")
open!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def close(handle, &callback) request :close, handle, &callback end
:call-seq: close(handle) -> request close(handle) { |response| ... } -> request Closes an open handle, whether obtained via #open, or #opendir. Returns immediately with a Request object. If a block is given, it will be invoked when the server responds. sftp.open("/path/to/file") do |response| raise "fail!" unless response.ok? sftp.close(response[:handle]) end sftp.loop
close
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def close!(handle, &callback) wait_for(close(handle, &callback)) end
Identical to #close, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it returns the Response object for this request. sftp.close!(handle)
close!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def read(handle, offset, length, &callback) request :read, handle, offset, length, &callback end
:call-seq: read(handle, offset, length) -> request read(handle, offset, length) { |response| ... } -> request Requests that +length+ bytes, starting at +offset+ bytes from the beginning of the file, be read from the file identified by +handle+. (The +handle+ should be a value obtained via the #open method.) Returns immediately with a Request object. If a block is given, it will be invoked when the server responds. The :data property of the response will contain the requested data, assuming the call was successful. request = sftp.read(handle, 0, 1024) do |response| if response.eof? puts "end of file reached before reading any data" elsif !response.ok? puts "error (#{response})" else print(response[:data]) end end request.wait To read an entire file will usually require multiple calls to #read, unless you know in advance how large the file is.
read
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def read!(handle, offset, length, &callback) wait_for(read(handle, offset, length, &callback), :data) end
Identical to #read, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. If the end of the file was reached, +nil+ will be returned. Otherwise, it returns the data that was read, as a String. data = sftp.read!(handle, 0, 1024)
read!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def write(handle, offset, data, &callback) request :write, handle, offset, data, &callback end
:call-seq: write(handle, offset, data) -> request write(handle, offset, data) { |response| ... } -> request Requests that +data+ be written to the file identified by +handle+, starting at +offset+ bytes from the start of the file. The file must have been opened for writing via #open. Returns immediately with a Request object. If a block is given, it will be invoked when the server responds. request = sftp.write(handle, 0, "hello, world!\n") request.wait
write
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def write!(handle, offset, data, &callback) wait_for(write(handle, offset, data, &callback)) end
Identical to #write, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful, or the end of the file was reached. Otherwise, it returns the Response object for this request. sftp.write!(handle, 0, "hello, world!\n")
write!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def lstat(path, flags=nil, &callback) request :lstat, path, flags, &callback end
:call-seq: lstat(path, flags=nil) -> request lstat(path, flags=nil) { |response| ... } -> request This method is identical to the #stat method, with the exception that it will not follow symbolic links (thus allowing you to stat the link itself, rather than what it refers to). The +flags+ parameter is not used in SFTP protocol versions prior to 4, and will be ignored in those versions of the protocol that do not use it. For those that do, however, you may provide hints as to which file proprties you wish to query (e.g., if all you want is permissions, you could pass the Net::SFTP::Protocol::V04::Attributes::F_PERMISSIONS flag as the value for the +flags+ parameter). The method returns immediately with a Request object. If a block is given, it will be invoked when the server responds. The :attrs property of the response will contain an Attributes instance appropriate for the the protocol version (see Protocol::V01::Attributes, Protocol::V04::Attributes, and Protocol::V06::Attributes). request = sftp.lstat("/path/to/file") do |response| raise "fail!" unless response.ok? puts "permissions: %04o" % response[:attrs].permissions end request.wait
lstat
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def lstat!(path, flags=nil, &callback) wait_for(lstat(path, flags, &callback), :attrs) end
Identical to the #lstat method, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the attribute object describing the path. puts sftp.lstat!("/path/to/file").permissions
lstat!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def fstat(handle, flags=nil, &callback) request :fstat, handle, flags, &callback end
The fstat method is identical to the #stat and #lstat methods, with the exception that it takes a +handle+ as the first parameter, such as would be obtained via the #open or #opendir methods. (See the #lstat method for full documentation).
fstat
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def fstat!(handle, flags=nil, &callback) wait_for(fstat(handle, flags, &callback), :attrs) end
Identical to the #fstat method, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the attribute object describing the path. puts sftp.fstat!(handle).permissions
fstat!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def setstat(path, attrs, &callback) request :setstat, path, attrs, &callback end
:call-seq: setstat(path, attrs) -> request setstat(path, attrs) { |response| ... } -> request This method may be used to set file metadata (such as permissions, or user/group information) on a remote file. The exact metadata that may be tweaked is dependent on the SFTP protocol version in use, but in general you may set at least the permissions, user, and group. (See Protocol::V01::Attributes, Protocol::V04::Attributes, and Protocol::V06::Attributes for the full lists of attributes that may be set for the different protocols.) The +attrs+ parameter is a hash, where the keys are symbols identifying the attributes to set. The method returns immediately with a Request object. If a block is given, it will be invoked when the server responds. request = sftp.setstat("/path/to/file", :permissions => 0644) request.wait puts "success: #{request.response.ok?}"
setstat
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def setstat!(path, attrs, &callback) wait_for(setstat(path, attrs, &callback)) end
Identical to the #setstat method, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the Response object for the request. sftp.setstat!("/path/to/file", :permissions => 0644)
setstat!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def fsetstat(handle, attrs, &callback) request :fsetstat, handle, attrs, &callback end
The fsetstat method is identical to the #setstat method, with the exception that it takes a +handle+ as the first parameter, such as would be obtained via the #open or #opendir methods. (See the #setstat method for full documentation.)
fsetstat
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def fsetstat!(handle, attrs, &callback) wait_for(fsetstat(handle, attrs, &callback)) end
Identical to the #fsetstat method, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the Response object for the request. sftp.fsetstat!(handle, :permissions => 0644)
fsetstat!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def opendir(path, &callback) request :opendir, path, &callback end
:call-seq: opendir(path) -> request opendir(path) { |response| ... } -> request Attempts to open a directory on the remote host for reading. Once the handle is obtained, directory entries may be retrieved using the #readdir method. The method returns immediately with a Request object. If a block is given, it will be invoked when the server responds. sftp.opendir("/path/to/directory") do |response| raise "fail!" unless response.ok? sftp.close(response[:handle]) end sftp.loop
opendir
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def opendir!(path, &callback) wait_for(opendir(path, &callback), :handle) end
Identical to #opendir, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return a handle to the given path. handle = sftp.opendir!("/path/to/directory")
opendir!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def readdir(handle, &callback) request :readdir, handle, &callback end
:call-seq: readdir(handle) -> request raeddir(handle) { |response| ... } -> request Reads a set of entries from the given directory handle (which must have been obtained via #opendir). If the response is EOF, then there are no more entries in the directory. Otherwise, the entries will be in the :names property of the response: loop do request = sftp.readdir(handle).wait break if request.response.eof? raise "fail!" unless request.response.ok? request.response[:names].each do |entry| puts entry.name end end See also Protocol::V01::Name and Protocol::V04::Name for the specific properties of each individual entry (which vary based on the SFTP protocol version in use).
readdir
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def readdir!(handle, &callback) wait_for(readdir(handle, &callback), :names) end
Identical to #readdir, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return nil if there were no more names to read, or an array of name entries. while (entries = sftp.readdir!(handle)) do entries.each { |entry| puts(entry.name) } end
readdir!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def remove(filename, &callback) request :remove, filename, &callback end
:call-seq: remove(filename) -> request remove(filename) { |response| ... } -> request Attempts to remove the given file from the remote file system. Returns immediately with a Request object. If a block is given, the block will be invoked when the server responds, and will be passed a Response object. sftp.remove("/path/to/file").wait
remove
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def remove!(filename, &callback) wait_for(remove(filename, &callback)) end
Identical to #remove, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the Response object for the request. sftp.remove!("/path/to/file")
remove!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def mkdir(path, attrs={}, &callback) request :mkdir, path, attrs, &callback end
:call-seq: mkdir(path, attrs={}) -> request mkdir(path, attrs={}) { |response| ... } -> request Creates the named directory on the remote server. If an attribute hash is given, it must map to the set of attributes supported by the version of the SFTP protocol in use. (See Protocol::V01::Attributes, Protocol::V04::Attributes, and Protocol::V06::Attributes.) sftp.mkdir("/path/to/directory", :permissions => 0550).wait
mkdir
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def mkdir!(path, attrs={}, &callback) wait_for(mkdir(path, attrs, &callback)) end
Identical to #mkdir, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the Response object for the request. sftp.mkdir!("/path/to/directory", :permissions => 0550)
mkdir!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def rmdir(path, &callback) request :rmdir, path, &callback end
:call-seq: rmdir(path) -> request rmdir(path) { |response| ... } -> request Removes the named directory on the remote server. The directory must be empty before it can be removed. sftp.rmdir("/path/to/directory").wait
rmdir
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def rmdir!(path, &callback) wait_for(rmdir(path, &callback)) end
Identical to #rmdir, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the Response object for the request. sftp.rmdir!("/path/to/directory")
rmdir!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def realpath(path, &callback) request :realpath, path, &callback end
:call-seq: realpath(path) -> request realpath(path) { |response| ... } -> request Tries to canonicalize the given path, turning any given path into an absolute path. This is primarily useful for converting a path with ".." or "." segments into an identical path without those segments. The answer will be in the response's :names attribute, as a one-element array. request = sftp.realpath("/path/../to/../directory").wait puts request[:names].first.name
realpath
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def realpath!(path, &callback) wait_for(realpath(path, &callback), :names).first end
Identical to #realpath, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return a name object identifying the path. puts(sftp.realpath!("/path/../to/../directory"))
realpath!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def stat(path, flags=nil, &callback) request :stat, path, flags, &callback end
Identical to the #lstat method, except that it follows symlinks (e.g., if you give it the path to a symlink, it will stat the target of the symlink rather than the symlink itself). See the #lstat method for full documentation.
stat
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def stat!(path, flags=nil, &callback) wait_for(stat(path, flags, &callback), :attrs) end
Identical to #stat, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return an attribute object for the named path. attrs = sftp.stat!("/path/to/file")
stat!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def rename(name, new_name, flags=nil, &callback) request :rename, name, new_name, flags, &callback end
:call-seq: rename(name, new_name, flags=nil) -> request rename(name, new_name, flags=nil) { |response| ... } -> request Renames the given file. This operation is only available in SFTP protocol versions two and higher. The +flags+ parameter is ignored in versions prior to 5. In versions 5 and higher, the +flags+ parameter can be used to specify how the rename should be performed (atomically, etc.). The following flags are defined in protocol version 5: * 0x0001 - overwrite an existing file if the new name specifies a file that already exists. * 0x0002 - perform the rewrite atomically. * 0x0004 - allow the server to perform the rename as it prefers.
rename
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def rename!(name, new_name, flags=nil, &callback) wait_for(rename(name, new_name, flags, &callback)) end
Identical to #rename, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the Response object for the request. sftp.rename!("/path/to/old", "/path/to/new")
rename!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def readlink(path, &callback) request :readlink, path, &callback end
:call-seq: readlink(path) -> request readlink(path) { |response| ... } -> request Queries the server for the target of the specified symbolic link. This operation is only available in protocol versions 3 and higher. The response to this request will include a names property, a one-element array naming the target of the symlink. request = sftp.readlink("/path/to/symlink").wait puts request.response[:names].first.name
readlink
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def readlink!(path, &callback) wait_for(readlink(path, &callback), :names).first end
Identical to #readlink, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the Name object for the path that the symlink targets. item = sftp.readlink!("/path/to/symlink")
readlink!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def symlink(path, target, &callback) request :symlink, path, target, &callback end
:call-seq: symlink(path, target) -> request symlink(path, target) { |response| ... } -> request Attempts to create a symlink to +path+ at +target+. This operation is only available in protocol versions 3, 4, and 5, but the Net::SFTP library mimics the symlink behavior in protocol version 6 using the #link method, so it is safe to use this method in protocol version 6. sftp.symlink("/path/to/file", "/path/to/symlink").wait
symlink
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def symlink!(path, target, &callback) wait_for(symlink(path, target, &callback)) end
Identical to #symlink, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the Response object for the request. sftp.symlink!("/path/to/file", "/path/to/symlink")
symlink!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def link(new_link_path, existing_path, symlink=true, &callback) request :link, new_link_path, existing_path, symlink, &callback end
:call-seq: link(new_link_path, existing_path, symlink=true) -> request link(new_link_path, existing_path, symlink=true) { |response| ... } -> request Attempts to create a link, either hard or symbolic. This operation is only available in SFTP protocol versions 6 and higher. If the +symlink+ paramter is true, a symbolic link will be created, otherwise a hard link will be created. The link will be named +new_link_path+, and will point to the path +existing_path+. sftp.link("/path/to/symlink", "/path/to/file", true).wait Note that #link is only available for SFTP protocol 6 and higher. You can use #symlink for protocols 3 and higher.
link
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def link!(new_link_path, existing_path, symlink=true, &callback) wait_for(link(new_link_path, existing_path, symlink, &callback)) end
Identical to #link, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the Response object for the request. sftp.link!("/path/to/symlink", "/path/to/file", true)
link!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def block(handle, offset, length, mask, &callback) request :block, handle, offset, length, mask, &callback end
:call-seq: block(handle, offset, length, mask) -> request block(handle, offset, length, mask) { |response| ... } -> request Creates a byte-range lock on the file specified by the given +handle+. This operation is only available in SFTP protocol versions 6 and higher. The lock may be either mandatory or advisory. The +handle+ parameter is a file handle, as obtained by the #open method. The +offset+ and +length+ parameters describe the location and size of the byte range. The +mask+ describes how the lock should be defined, and consists of some combination of the following bit masks: * 0x0040 - Read lock. The byte range may not be accessed for reading by via any other handle, though it may be written to. * 0x0080 - Write lock. The byte range may not be written to via any other handle, though it may be read from. * 0x0100 - Delete lock. No other handle may delete this file. * 0x0200 - Advisory lock. The server need not honor the lock instruction. Once created, the lock may be removed via the #unblock method.
block
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def block!(handle, offset, length, mask, &callback) wait_for(block(handle, offset, length, mask, &callback)) end
Identical to #block, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the Response object for the request.
block!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def unblock(handle, offset, length, &callback) request :unblock, handle, offset, length, &callback end
:call-seq: unblock(handle, offset, length) -> request unblock(handle, offset, length) { |response| ... } -> request Removes a previously created byte-range lock. This operation is only available in protocol versions 6 and higher. The +offset+ and +length+ parameters must exactly match those that were given to #block when the lock was acquired.
unblock
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def unblock!(handle, offset, length, &callback) wait_for(unblock(handle, offset, length, &callback)) end
Identical to #unblock, but blocks until the server responds. It will raise a StatusException if the request was unsuccessful. Otherwise, it will return the Response object for the request.
unblock!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def close_channel return unless open? channel.close loop { !closed? } end
Closes the SFTP connection, but not the SSH connection. Blocks until the session has terminated. Once the session has terminated, further operations on this object will result in errors. You can reopen the SFTP session via the #connect method.
close_channel
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def open? state == :open end
Returns true if the connection has been initialized.
open?
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def closed? state == :closed end
Returns true if the connection has been closed.
closed?
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def opening? !(open? || closed?) end
Returns true if the connection is in the process of being initialized (e.g., it is not closed, but is not yet fully open).
opening?
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def connect(&block) case state when :open block.call(self) if block when :closed @state = :opening @channel = session.open_channel(&method(:when_channel_confirmed)) @packet_length = nil @protocol = nil @on_ready = Array(block) else # opening @on_ready << block if block end self end
Attempts to establish an SFTP connection over the SSH session given when this object was instantiated. If the object is already open, this will simply execute the given block (if any), passing the SFTP session itself as argument. If the session is currently being opened, this will add the given block to the list of callbacks, to be executed when the session is fully open. This method does not block, and will return immediately. If you pass a block to it, that block will be invoked when the connection has been fully established. Thus, you can do something like this: sftp.connect do puts "open!" end If you just want to block until the connection is ready, see the #connect! method.
connect
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def connect!(&block) connect(&block) loop { opening? } self end
Same as the #connect method, but blocks until the SFTP connection has been fully initialized.
connect!
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def loop(&block) block ||= Proc.new { pending_requests.any? } session.loop{Rex::ThreadSafe.sleep(0.5); block.call } end
Runs the SSH event loop while the given block returns true. This lets you set up a state machine and then "fire it off". If you do not specify a block, the event loop will run for as long as there are any pending SFTP requests. This makes it easy to do thing like this: sftp.remove("/path/to/file") sftp.loop
loop
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def send_packet(type, *args) data = Net::SSH::Buffer.from(*args) msg = Net::SSH::Buffer.from(:long, data.length+1, :byte, type, :raw, data) channel.send_data(msg.to_s) end
Formats, constructs, and sends an SFTP packet of the given type and with the given data. This does not block, but merely enqueues the packet for sending and returns. You should probably use the operation methods, rather than building and sending the packet directly. (See #open, #close, etc.)
send_packet
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def request(type, *args, &callback) request = Request.new(self, type, protocol.send(type, *args), &callback) info { "sending #{type} packet (#{request.id})" } pending_requests[request.id] = request end
Create and enqueue a new SFTP request of the given type, with the given arguments. Returns a new Request instance that encapsulates the request.
request
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def wait_for(request, property=nil) request.wait if request.response.eof? nil elsif !request.response.ok? raise StatusException.new(request.response) elsif property request.response[property.to_sym] else request.response end end
Waits for the given request to complete. If the response is EOF, nil is returned. If the response was not successful (e.g., !response.ok?), a StatusException will be raised. If +property+ is given, the corresponding property from the response will be returned; otherwise, the response object itself will be returned.
wait_for
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def when_channel_confirmed(channel) debug { "requesting sftp subsystem" } @state = :subsystem channel.subsystem("sftp", &method(:when_subsystem_started)) end
Called when the SSH channel is confirmed as "open" by the server. This is one of the states of the SFTP state machine, and is followed by the #when_subsystem_started state.
when_channel_confirmed
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def when_subsystem_started(channel, success) raise Net::SFTP::Exception, "could not start SFTP subsystem" unless success debug { "sftp subsystem successfully started" } @state = :init channel.on_data { |c,data| input.append(data) } channel.on_extended_data { |c,t,data| debug { data } } channel.on_close(&method(:when_channel_closed)) channel.on_process(&method(:when_channel_polled)) send_packet(FXP_INIT, :long, HIGHEST_PROTOCOL_VERSION_SUPPORTED) end
Called when the SSH server confirms that the SFTP subsystem was successfully started. This sets up the appropriate callbacks on the SSH channel and then starts the SFTP protocol version negotiation process.
when_subsystem_started
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def when_channel_closed(channel) debug { "sftp channel closed" } @channel = nil @state = :closed end
Called when the SSH server closes the underlying channel.
when_channel_closed
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def when_channel_polled(channel) while input.length > 0 if @packet_length.nil? # make sure we've read enough data to tell how long the packet is return unless input.length >= 4 @packet_length = input.read_long end return unless input.length >= @packet_length packet = Net::SFTP::Packet.new(input.read(@packet_length)) input.consume! @packet_length = nil debug { "received sftp packet #{packet.type} len #{packet.length}" } if packet.type == FXP_VERSION do_version(packet) else dispatch_request(packet) end end end
Called whenever Net::SSH polls the SFTP channel for pending activity. This basically checks the input buffer to see if enough input has been accumulated to handle. If there has, the packet is parsed and dispatched, according to its type (see #do_version and #dispatch_requestl_sftp.state.to_s).
when_channel_polled
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def do_version(packet) debug { "negotiating sftp protocol version, mine is #{HIGHEST_PROTOCOL_VERSION_SUPPORTED}" } server_version = packet.read_long debug { "server reports sftp version #{server_version}" } negotiated_version = [server_version, HIGHEST_PROTOCOL_VERSION_SUPPORTED].min info { "negotiated version is #{negotiated_version}" } extensions = {} until packet.eof? name = packet.read_string data = packet.read_string extensions[name] = data end @protocol = Protocol.load(self, negotiated_version) @pending_requests = {} @state = :open @on_ready.each { |callback| callback.call(self) } @on_ready = nil end
Called to handle FXP_VERSION packets. This performs the SFTP protocol version negotiation, instantiating the appropriate Protocol instance and invoking the callback given to #connect, if any.
do_version
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def dispatch_request(packet) id = packet.read_long request = pending_requests.delete(id) or raise Net::SFTP::Exception, "no such request `#{id}'" request.respond_to(packet) end
Parses the packet, finds the associated Request instance, and tells the Request instance to respond to the packet (see Request#respond_to).
dispatch_request
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/session.rb
MIT
def initialize(sftp) @sftp = sftp end
Create a new instance on top of the given SFTP session instance.
initialize
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/dir.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/dir.rb
MIT
def foreach(path) handle = sftp.opendir!(path) while entries = sftp.readdir!(handle) entries.each { |entry| yield entry } end return nil ensure sftp.close!(handle) if handle end
Calls the block once for each entry in the named directory on the remote server. Yields a Name object to the block, rather than merely the name of the entry.
foreach
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/dir.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/dir.rb
MIT
def entries(path) results = [] foreach(path) { |entry| results << entry } return results end
Returns an array of Name objects representing the items in the given remote directory, +path+.
entries
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/dir.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/dir.rb
MIT
def glob(path, pattern, flags=0) flags |= ::File::FNM_PATHNAME path = path.chop if path[-1,1] == "/" results = [] unless block_given? queue = entries(path).reject { |e| e.name == "." || e.name == ".." } while queue.any? entry = queue.shift if entry.directory? && !%w(. ..).include?(::File.basename(entry.name)) queue += entries("#{path}/#{entry.name}").map do |e| e.name.replace("#{entry.name}/#{e.name}") e end end if ::File.fnmatch(pattern, entry.name, flags) if block_given? yield entry else results << entry end end end return results unless block_given? end
Works as ::Dir.glob, matching (possibly recursively) all directory entries under +path+ against +pattern+. If a block is given, matches will be yielded to the block as they are found; otherwise, they will be returned in an array when the method finishes. Because working over an SFTP connection is always going to be slower than working purely locally, don't expect this method to perform with the same level of alacrity that ::Dir.glob does; it will work best for shallow directory hierarchies with relatively few directories, though it should be able to handle modest numbers of files in each directory.
glob
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/dir.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/dir.rb
MIT
def initialize(sftp, local, remote, options={}, &progress) @sftp = sftp @local = local @remote = remote @progress = progress || options[:progress] @options = options @active = 0 @properties = options[:properties] || {} self.logger = sftp.logger if recursive? && local.respond_to?(:write) raise ArgumentError, "cannot download a directory tree in-memory" end @stack = [Entry.new(remote, local, recursive?)] process_next_entry end
Instantiates a new downloader process on top of the given SFTP session. +local+ is either an IO object that should receive the data, or a string identifying the target file or directory on the local host. +remote+ is a string identifying the location on the remote host that the download should source. This will return immediately, and requires that the SSH event loop be run in order to effect the download. (See #wait.)
initialize
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def active? @active > 0 || stack.any? end
Returns true if there are any active requests or pending files or directories.
active?
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def wait sftp.loop { active? } self end
Runs the SSH event loop for as long as the downloader is active (see #active?). This can be used to block until the download completes.
wait
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def read_size options[:read_size] || DEFAULT_READ_SIZE end
The number of bytes to read at a time from remote files.
read_size
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def requests options[:requests] || (recursive? ? 16 : 2) end
The number of simultaneou SFTP requests to use to effect the download. Defaults to 16 for recursive downloads.
requests
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def process_next_entry while stack.any? && requests > @active entry = stack.shift @active += 1 if entry.directory update_progress(:mkdir, entry.local) ::Dir.mkdir(entry.local) unless ::File.directory?(entry.local) request = sftp.opendir(entry.remote, &method(:on_opendir)) request[:entry] = entry else open_file(entry) end end update_progress(:finish) if !active? end
Enqueues as many files and directories from the stack as possible (see #requests).
process_next_entry
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def on_opendir(response) entry = response.request[:entry] raise "opendir #{entry.remote}: #{response}" unless response.ok? entry.handle = response[:handle] request = sftp.readdir(response[:handle], &method(:on_readdir)) request[:parent] = entry end
Called when a remote directory is "opened" for reading, e.g. to enumerate its contents. Starts an readdir operation if the opendir operation was successful.
on_opendir
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def on_readdir(response) entry = response.request[:parent] if response.eof? request = sftp.close(entry.handle, &method(:on_closedir)) request[:parent] = entry elsif !response.ok? raise "readdir #{entry.remote}: #{response}" else response[:names].each do |item| next if item.name == "." || item.name == ".." stack << Entry.new(::File.join(entry.remote, item.name), ::File.join(entry.local, item.name), item.directory?, item.attributes.size) end # take this opportunity to enqueue more requests process_next_entry request = sftp.readdir(entry.handle, &method(:on_readdir)) request[:parent] = entry end end
Called when the next batch of items is read from a directory on the remote server. If any items were read, they are added to the queue and #process_next_entry is called.
on_readdir
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def open_file(entry) update_progress(:open, entry) request = sftp.open(entry.remote, &method(:on_open)) request[:entry] = entry end
Called when a file is to be opened for reading from the remote server.
open_file
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def on_closedir(response) @active -= 1 entry = response.request[:parent] raise "close #{entry.remote}: #{response}" unless response.ok? process_next_entry end
Called when a directory handle is closed.
on_closedir
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def on_open(response) entry = response.request[:entry] raise "open #{entry.remote}: #{response}" unless response.ok? entry.handle = response[:handle] entry.sink = entry.local.respond_to?(:write) ? entry.local : ::File.open(entry.local, "wb") entry.offset = 0 download_next_chunk(entry) end
Called when a file has been opened. This will call #download_next_chunk to initiate the data transfer.
on_open
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def download_next_chunk(entry) request = sftp.read(entry.handle, entry.offset, read_size, &method(:on_read)) request[:entry] = entry request[:offset] = entry.offset entry.offset += read_size end
Initiates a read of the next #read_size bytes from the file.
download_next_chunk
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def on_read(response) entry = response.request[:entry] if response.eof? update_progress(:close, entry) entry.sink.close request = sftp.close(entry.handle, &method(:on_close)) request[:entry] = entry elsif !response.ok? raise "read #{entry.remote}: #{response}" else update_progress(:get, entry, response.request[:offset], response[:data]) entry.sink.write(response[:data]) download_next_chunk(entry) end end
Called when a read from a file finishes. If the read was successful and returned data, this will call #download_next_chunk to read the next bit from the file. Otherwise the file will be closed.
on_read
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def on_close(response) @active -= 1 entry = response.request[:entry] raise "close #{entry.remote}: #{response}" unless response.ok? process_next_entry end
Called when a file handle is closed.
on_close
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def update_progress(hook, *args) on = "on_#{hook}" if progress.respond_to?(on) progress.send(on, self, *args) elsif progress.respond_to?(:call) progress.call(hook, self, *args) end end
If a progress callback or object has been set, this will report the progress to that callback or object.
update_progress
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/download.rb
MIT
def initialize(sftp, handle) @sftp = sftp @handle = handle @pos = 0 @real_pos = 0 @real_eof = false @buffer = "" end
Creates a new wrapper that encapsulates the given +handle+ (such as would be returned by Net::SFTP::Session#open!). The +sftp+ parameter must be the same Net::SFTP::Session instance that opened the file.
initialize
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
MIT
def close sftp.close!(handle) @handle = nil end
Closes the underlying file and sets the handle to +nil+. Subsequent operations on this object will fail.
close
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
MIT
def eof? @real_eof && @buffer.empty? end
Returns true if the end of the file has been encountered by a previous read. Setting the current file position via #pos= will reset this flag (useful if the file's contents have changed since the EOF was encountered).
eof?
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
MIT
def read(n=nil) loop do break if n && @buffer.length >= n break unless fill end if n result, @buffer = @buffer[0,n], (@buffer[n..-1] || "") else result, @buffer = @buffer, "" end @pos += result.length return result end
Reads up to +n+ bytes of data from the stream. Fewer bytes will be returned if EOF is encountered before the requested number of bytes could be read. Without an argument (or with a nil argument) all data to the end of the file will be read and returned. This will advance the file pointer (#pos).
read
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
MIT
def readline(sep_string=$/) line = gets(sep_string) raise EOFError if line.nil? return line end
Same as #gets, but raises EOFError if EOF is encountered before any data could be read.
readline
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
MIT
def write(data) data = data.to_s sftp.write!(handle, @real_pos, data) @real_pos += data.length @pos = @real_pos data.length end
Writes the given data to the stream, incrementing the file position and returning the number of bytes written.
write
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
MIT
def print(*items) items.each { |item| write(item) } write($\) if $\ nil end
Writes each argument to the stream. If +$\+ is set, it will be written after all arguments have been written.
print
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
MIT
def puts(*items) items.each do |item| if Array === item puts(*item) else write(item) write("\n") unless item[-1] == ?\n end end nil end
Writes each argument to the stream, appending a newline to any item that does not already end in a newline. Array arguments are flattened.
puts
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
MIT
def fill data = sftp.read!(handle, @real_pos, 8192) if data.nil? @real_eof = true return false else @real_pos += data.length @buffer << data end !@real_eof end
Fills the buffer. Returns +true+ if it succeeded, and +false+ if EOF was encountered before any data was read.
fill
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file.rb
MIT
def initialize(sftp) @sftp = sftp end
Create a new instance on top of the given SFTP session instance.
initialize
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file_factory.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file_factory.rb
MIT
def open(name, flags="r", mode=nil, &block) handle = sftp.open!(name, flags, :permissions => mode) file = Operations::File.new(sftp, handle) if block_given? begin yield file ensure file.close end else return file end end
:call-seq: open(name, flags="r", mode=nil) -> file open(name, flags="r", mode=nil) { |file| ... } Attempt to open a file on the remote server. The +flags+ parameter accepts the same values as the standard Ruby ::File#open method. The +mode+ parameter must be an integer describing the permissions to use if a new file is being created. If a block is given, the new Operations::File instance will be yielded to it, and closed automatically when the block terminates. Otherwise the object will be returned, and it is the caller's responsibility to close the file. sftp.file.open("/tmp/names.txt", "w") do |f| # ... end
open
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file_factory.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/file_factory.rb
MIT
def initialize(sftp, local, remote, options={}, &progress) #:nodoc: @sftp = sftp @local = local @remote = remote @progress = progress || options[:progress] @options = options @properties = options[:properties] || {} @active = 0 self.logger = sftp.logger @uploads = [] @recursive = local.respond_to?(:read) ? false : ::File.directory?(local) if recursive? @stack = [entries_for(local)] @local_cwd = local @remote_cwd = remote @active += 1 sftp.mkdir(remote) do |response| @active -= 1 raise StatusException.new(response, "mkdir `#{remote}'") unless response.ok? (options[:requests] || RECURSIVE_READERS).to_i.times do break unless process_next_entry end end else raise ArgumentError, "expected a file to upload" unless local.respond_to?(:read) || ::File.exists?(local) @stack = [[local]] process_next_entry end end
Instantiates a new uploader process on top of the given SFTP session. +local+ is either an IO object containing data to upload, or a string identifying a file or directory on the local host. +remote+ is a string identifying the location on the remote host that the upload should target. This will return immediately, and requires that the SSH event loop be run in order to effect the upload. (See #wait.)
initialize
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
MIT
def active? @active > 0 || @stack.any? end
Returns true if the uploader is currently running. When this is false, the uploader has finished processing.
active?
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
MIT
def wait sftp.loop { active? } self end
Blocks until the upload has completed.
wait
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
MIT
def process_next_entry if @stack.empty? if @uploads.any? write_next_chunk(@uploads.first) elsif !active? update_progress(:finish) end return false elsif @stack.last.empty? @stack.pop @local_cwd = ::File.dirname(@local_cwd) @remote_cwd = ::File.dirname(@remote_cwd) process_next_entry elsif recursive? entry = @stack.last.shift lpath = ::File.join(@local_cwd, entry) rpath = ::File.join(@remote_cwd, entry) if ::File.directory?(lpath) @stack.push(entries_for(lpath)) @local_cwd = lpath @remote_cwd = rpath @active += 1 update_progress(:mkdir, rpath) request = sftp.mkdir(rpath, &method(:on_mkdir)) request[:dir] = rpath else open_file(lpath, rpath) end else open_file(@stack.pop.first, remote) end return true end
Examines the stack and determines what action to take. This is the starting point of the state machine.
process_next_entry
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
MIT
def open_file(local, remote) @active += 1 if local.respond_to?(:read) file = local name = options[:name] || "<memory>" else file = ::File.open(local, "rb") name = local end if file.respond_to?(:stat) size = file.stat.size else size = file.size end metafile = LiveFile.new(name, remote, file, size) update_progress(:open, metafile) request = sftp.open(remote, "w", &method(:on_open)) request[:file] = metafile end
Prepares to send +local+ to +remote+.
open_file
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
MIT
def on_mkdir(response) @active -= 1 dir = response.request[:dir] raise StatusException.new(response, "mkdir #{dir}") unless response.ok? process_next_entry end
Called when a +mkdir+ request finishes, successfully or otherwise. If the request failed, this will raise a StatusException, otherwise it will call #process_next_entry to continue the state machine.
on_mkdir
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
MIT
def on_open(response) @active -= 1 file = response.request[:file] raise StatusException.new(response, "open #{file.remote}") unless response.ok? file.handle = response[:handle] @uploads << file write_next_chunk(file) if !recursive? (options[:requests] || SINGLE_FILE_READERS).to_i.times { write_next_chunk(file) } end end
Called when an +open+ request finishes. Raises StatusException if the open failed, otherwise it calls #write_next_chunk to begin sending data to the remote server.
on_open
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
MIT
def on_write(response) @active -= 1 file = response.request[:file] raise StatusException.new(response, "write #{file.remote}") unless response.ok? write_next_chunk(file) end
Called when a +write+ request finishes. Raises StatusException if the write failed, otherwise it calls #write_next_chunk to continue the write.
on_write
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
MIT
def on_close(response) @active -= 1 file = response.request[:file] raise StatusException.new(response, "close #{file.remote}") unless response.ok? process_next_entry end
Called when a +close+ request finishes. Raises a StatusException if the close failed, otherwise it calls #process_next_entry to continue the state machine.
on_close
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
MIT
def write_next_chunk(file) if file.io.nil? process_next_entry else @active += 1 offset = file.io.pos data = file.io.read(options[:read_size] || DEFAULT_READ_SIZE) if data.nil? update_progress(:close, file) request = sftp.close(file.handle, &method(:on_close)) request[:file] = file file.io.close file.io = nil @uploads.delete(file) else update_progress(:put, file, offset, data) request = sftp.write(file.handle, offset, data, &method(:on_write)) request[:file] = file end end end
Attempts to send the next chunk from the given file (where +file+ is a LiveFile instance).
write_next_chunk
ruby
hahwul/mad-metasploit
mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
https://github.com/hahwul/mad-metasploit/blob/master/mad-metasploit-plugins/meta_ssh/lib/net/sftp/operations/upload.rb
MIT