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

218 lines
4.1 KiB
Ruby
Raw Normal View History

# -*- coding: binary -*-
2015-02-26 20:05:50 +00:00
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
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
2015-02-25 23:09:44 +00:00
# Query a file
#
# @param path [String] Remote path
# @return [Boolean] True if file exists
def fsquery(path)
if path !~ /^[0-2]:/
raise ArgumentError, "Path must begin with 0:, 1:, or 2:"
end
file = false
@sock.put(%Q{#{FSQUERY} NAME = "#{path}"\n})
if @sock.get(DEFAULT_TIMEOUT) =~ /TYPE=(FILE|DIR)/m
file = true
end
file
end
2014-01-15 19:49:37 +00:00
# List a directory
#
# @param path [String] Remote path
# @param count [Fixnum] Number of entries to list
# @return [String] Directory listing
def fsdirlist(path, count = COUNT_MAX)
if path !~ /^[0-2]:/
raise ArgumentError, "Path 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 = "#{path}" ENTRY=1 COUNT=#{count}\n})
2014-01-15 19:49:37 +00:00
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 path [String] Remote path
# @return [String] File as a string
2015-02-26 20:10:53 +00:00
def fsupload(path)
if path !~ /^[0-2]:/
raise ArgumentError, "Path 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
2015-02-26 20:10:53 +00:00
@sock.put(%Q{#{FSUPLOAD} NAME = "#{path}" OFFSET=0 SIZE=#{SIZE_MAX}\n})
2014-01-15 19:49:37 +00:00
if @sock.get(DEFAULT_TIMEOUT) =~ /SIZE=\d+\r?\n(.*)\f/m
file = $1
end
2014-01-15 19:49:37 +00:00
file
end
2015-02-25 20:46:42 +00:00
# Upload a file
#
# @param lpath [String] Local path
# @param rpath [String] Remote path
# @return [Boolean] True if the file was uploaded
2015-02-25 20:46:42 +00:00
def fsdownload(lpath, rpath)
if rpath !~ /^[0-2]:/
raise ArgumentError, "Path must begin with 0:, 1:, or 2:"
end
file = File.read(lpath)
@sock.put(
%Q{#{FSDOWNLOAD} FORMAT:BINARY SIZE=#{file.length} NAME = "#{rpath}"\n}
)
@sock.put(file)
2015-02-25 23:30:50 +00:00
@sock.put(UEL)
fsquery(rpath)
2015-02-25 20:46:42 +00:00
end
2015-02-25 21:41:40 +00:00
# Delete a file
#
# @param path [String] Remote path
# @return [Boolean] True if the file was deleted
2015-02-25 21:41:40 +00:00
def fsdelete(path)
if path !~ /^[0-2]:/
raise ArgumentError, "Path must begin with 0:, 1:, or 2:"
end
@sock.put(%Q{#{FSDELETE} NAME = "#{path}"\n})
!fsquery(path)
2015-02-25 21:41:40 +00:00
end
2013-12-05 03:46:45 +00:00
end
end