metasploit-framework/lib/rex/proto/pjl/client.rb

163 lines
3.0 KiB
Ruby
Raw Normal View History

2013-12-05 06:28:53 +00:00
# https://en.wikipedia.org/wiki/Printer_Job_Language
# See external links for PJL spec
2013-12-05 03:46:45 +00:00
2013-12-05 16:25:04 +00:00
module Rex::Proto::PJL
2013-12-05 03:46:45 +00:00
class Client
2014-01-15 19:49:37 +00:00
attr_reader :sock
2013-12-05 03:46:45 +00:00
def initialize(sock)
@sock = sock
end
2014-01-15 19:49:37 +00:00
# Begin a PJL job
2013-12-05 06:28:53 +00:00
#
2013-12-05 16:25:04 +00:00
# @return [void]
def begin_job
2014-01-15 19:49:37 +00:00
@sock.put("#{UEL}#{PREFIX}\n")
2013-12-05 03:46:45 +00:00
end
2014-01-15 19:49:37 +00:00
# End a PJL job
2013-12-05 06:28:53 +00:00
#
2013-12-05 16:25:04 +00:00
# @return [void]
def end_job
2014-01-15 19:49:37 +00:00
@sock.put(UEL)
2013-12-05 03:46:45 +00:00
end
2014-01-15 19:49:37 +00:00
# Send an INFO request and read the response
2013-12-05 06:28:53 +00:00
#
# @param category [String] INFO category
# @return [String] INFO response
def info(category)
categories = {
2013-12-16 17:35:02 +00:00
:id => Info::ID,
:status => Info::STATUS,
2014-01-16 19:21:33 +00:00
:variables => Info::VARIABLES,
2013-12-16 17:35:02 +00:00
:filesys => Info::FILESYS
}
2014-01-15 19:49:37 +00:00
unless categories.has_key?(category)
raise ArgumentError, "Unknown INFO category"
2013-12-05 03:46:45 +00:00
end
2014-01-15 19:49:37 +00:00
@sock.put("#{categories[category]}\n")
2014-01-16 17:58:37 +00:00
@sock.get(DEFAULT_TIMEOUT)
2013-12-05 03:46:45 +00:00
end
2013-12-05 06:28:53 +00:00
# Get version information
#
# @return [String] Version information
def info_id
2013-12-05 03:46:45 +00:00
id = nil
2014-01-15 19:49:37 +00:00
if info(:id) =~ /"(.*?)"/m
2013-12-05 03:46:45 +00:00
id = $1
end
2014-01-15 19:49:37 +00:00
id
2013-12-05 03:46:45 +00:00
end
2014-01-16 19:21:33 +00:00
# Get environment variables
#
# @return [String] Environment variables
def info_variables
env_vars = nil
if info(:variables) =~ /#{Info::VARIABLES}\r?\n(.*?)\f/m
env_vars = $1
end
env_vars
end
# List volumes
#
# @return [String] Volume listing
def info_filesys
filesys = nil
2014-01-15 19:49:37 +00:00
if info(:filesys) =~ /\[\d+ TABLE\]\r?\n(.*?)\f/m
filesys = $1
end
2014-01-15 19:49:37 +00:00
filesys
end
2014-01-15 19:49:37 +00:00
# Get the ready message
2013-12-05 06:28:53 +00:00
#
# @return [String] Ready message
2013-12-05 16:25:04 +00:00
def get_rdymsg
2013-12-05 03:46:45 +00:00
rdymsg = nil
2014-01-15 19:49:37 +00:00
if info(:status) =~ /DISPLAY="(.*?)"/m
2013-12-05 03:46:45 +00:00
rdymsg = $1
end
2014-01-15 19:49:37 +00:00
rdymsg
2013-12-05 03:46:45 +00:00
end
2014-01-15 19:49:37 +00:00
# Set the ready message
2013-12-05 06:28:53 +00:00
#
# @param message [String] Ready message
2013-12-05 16:25:04 +00:00
# @return [void]
def set_rdymsg(message)
2014-01-15 19:49:37 +00:00
@sock.put(%Q{#{RDYMSG} DISPLAY = "#{message}"\n})
2013-12-05 03:46:45 +00:00
end
2014-01-15 19:49:37 +00:00
# Initialize a volume
#
# @param volume [String] Volume
# @return [void]
def fsinit(volume)
if volume !~ /^[0-2]:$/
raise ArgumentError, "Volume must be 0:, 1:, or 2:"
end
2014-01-15 19:49:37 +00:00
@sock.put(%Q{#{FSINIT} VOLUME = "#{volume}"\n})
end
2014-01-15 19:49:37 +00:00
# List a directory
#
# @param pathname [String] Pathname
# @param count [Fixnum] Number of entries to list
# @return [String] Directory listing
2013-12-09 21:33:44 +00:00
def fsdirlist(pathname, count = COUNT_MAX)
if pathname !~ /^[0-2]:/
raise ArgumentError, "Pathname must begin with 0:, 1:, or 2:"
end
2014-01-15 19:49:37 +00:00
listing = nil
2014-01-15 19:49:37 +00:00
@sock.put(%Q{#{FSDIRLIST} NAME = "#{pathname}" ENTRY=1 COUNT=#{count}\n})
2014-01-16 17:58:37 +00:00
if @sock.get(DEFAULT_TIMEOUT) =~ /ENTRY=1\r?\n(.*?)\f/m
listing = $1
end
2014-01-15 19:49:37 +00:00
listing
end
2014-01-15 19:49:37 +00:00
# Download a file
#
# @param pathname [String] Pathname
# @param size [Fixnum] Size of file
# @return [String] File as a string
2013-12-09 21:33:44 +00:00
def fsupload(pathname, size = SIZE_MAX)
if pathname !~ /^[0-2]:/
raise ArgumentError, "Pathname must begin with 0:, 1:, or 2:"
end
2014-01-15 19:49:37 +00:00
file = nil
2014-01-15 19:49:37 +00:00
@sock.put(%Q{#{FSUPLOAD} NAME = "#{pathname}" OFFSET=0 SIZE=#{size}\n})
if @sock.get(DEFAULT_TIMEOUT) =~ /SIZE=\d+\r?\n(.*)\f/m
file = $1
end
2014-01-15 19:49:37 +00:00
file
end
2013-12-05 03:46:45 +00:00
end
end