2006-07-27 00:59:00 +00:00
|
|
|
require 'dl'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class provides os-specific functionality
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module Compat
|
|
|
|
|
|
|
|
STD_INPUT_HANDLE = -10
|
|
|
|
ENABLE_LINE_INPUT = 2
|
|
|
|
ENABLE_ECHO_INPUT = 4
|
|
|
|
ENABLE_PROCESSED_INPUT = 1
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Platform detection
|
|
|
|
#
|
|
|
|
def self.is_windows
|
|
|
|
(RUBY_PLATFORM =~ /mswin32/) ? true : false
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_macosx
|
|
|
|
(RUBY_PLATFORM =~ /darwin/) ? true : false
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_linux
|
|
|
|
(RUBY_PLATFORM =~ /linux/) ? true : false
|
|
|
|
end
|
|
|
|
|
2008-01-06 21:51:07 +00:00
|
|
|
|
|
|
|
def self.is_freebsd
|
|
|
|
(RUBY_PLATFORM =~ /freebsd/) ? true : false
|
|
|
|
end
|
|
|
|
|
2007-12-31 01:19:49 +00:00
|
|
|
def self.open_browser(url='http://metasploit.com/')
|
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /mswin32/
|
|
|
|
system("start #{url}")
|
|
|
|
when /darwin/
|
|
|
|
system("open #{url}")
|
|
|
|
else
|
|
|
|
system("firefox #{url} &")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-31 04:05:51 +00:00
|
|
|
def self.open_email(addr)
|
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /mswin32/
|
|
|
|
system("start mailto:#{addr}")
|
|
|
|
when /darwin/
|
|
|
|
system("open mailto:#{addr}")
|
|
|
|
else
|
|
|
|
# ?
|
|
|
|
end
|
|
|
|
end
|
2006-07-27 22:28:19 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Change the Windows console to non-blocking mode
|
|
|
|
#
|
2006-07-27 00:59:00 +00:00
|
|
|
def self.win32_stdin_unblock
|
|
|
|
begin
|
|
|
|
@@k32 ||= DL.dlopen("kernel32.dll")
|
|
|
|
gsh = @@k32['GetStdHandle', 'LL']
|
|
|
|
gcm = @@k32['GetConsoleMode', 'LLP']
|
|
|
|
scm = @@k32['SetConsoleMode', 'LLL']
|
|
|
|
|
|
|
|
inp = gsh.call(STD_INPUT_HANDLE)[0]
|
|
|
|
inf = DL.malloc(DL.sizeof('L'))
|
|
|
|
gcm.call(inp, inf)
|
|
|
|
old_mode = inf.to_a('L', 1)[0]
|
|
|
|
new_mode = old_mode & ~(ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT|ENABLE_PROCESSED_INPUT)
|
|
|
|
scm.call(inp, new_mode)
|
|
|
|
|
|
|
|
rescue ::Exception
|
|
|
|
raise $!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
#
|
|
|
|
# Change the Windows console to blocking mode
|
|
|
|
#
|
2006-07-27 00:59:00 +00:00
|
|
|
def self.win32_stdin_block
|
|
|
|
begin
|
|
|
|
@@k32 ||= DL.dlopen("kernel32.dll")
|
|
|
|
gsh = @@k32['GetStdHandle', 'LL']
|
|
|
|
gcm = @@k32['GetConsoleMode', 'LLP']
|
|
|
|
scm = @@k32['SetConsoleMode', 'LLL']
|
|
|
|
|
|
|
|
inp = gsh.call(STD_INPUT_HANDLE)[0]
|
|
|
|
inf = DL.malloc(DL.sizeof('L'))
|
|
|
|
gcm.call(inp, inf)
|
|
|
|
old_mode = inf.to_a('L', 1)[0]
|
|
|
|
new_mode = old_mode | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT
|
|
|
|
scm.call(inp, new_mode)
|
|
|
|
|
|
|
|
rescue ::Exception
|
|
|
|
raise $!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
#
|
|
|
|
# Obtain the path to our interpreter
|
|
|
|
#
|
2006-07-27 00:59:00 +00:00
|
|
|
def self.win32_ruby_path
|
|
|
|
begin
|
|
|
|
@@k32 ||= DL.dlopen("kernel32.dll")
|
|
|
|
gmh = @@k32['GetModuleHandle', 'LP']
|
|
|
|
gmf = @@k32['GetModuleFileName', 'LLPL']
|
|
|
|
|
|
|
|
mod = gmh.call(nil)[0]
|
|
|
|
inf = DL.malloc(1024)
|
|
|
|
|
|
|
|
gmf.call(mod, inf, 1024)
|
|
|
|
return inf.to_s
|
|
|
|
|
|
|
|
rescue ::Exception
|
|
|
|
raise $!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
#
|
|
|
|
# Call WinExec (equiv to system("cmd &")
|
|
|
|
#
|
2006-07-27 00:59:00 +00:00
|
|
|
def self.win32_winexec(cmd)
|
|
|
|
begin
|
|
|
|
@@k32 ||= DL.dlopen("kernel32.dll")
|
|
|
|
win = @@k32['WinExec', 'LPL']
|
|
|
|
win.call(cmd.to_ptr, 0)
|
|
|
|
rescue ::Exception
|
|
|
|
raise $!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
#
|
|
|
|
# Read directly from the win32 console
|
|
|
|
#
|
|
|
|
def self.win32_stdin_read(size=512)
|
2006-07-27 05:03:56 +00:00
|
|
|
begin
|
|
|
|
@@k32 ||= DL.dlopen("kernel32.dll")
|
|
|
|
gsh = @@k32['GetStdHandle', 'LL']
|
|
|
|
rco = @@k32['ReadConsole', 'LLPLPL']
|
|
|
|
|
|
|
|
inp = gsh.call(STD_INPUT_HANDLE)[0]
|
2006-07-27 22:28:19 +00:00
|
|
|
buf = DL.malloc(size)
|
2006-07-27 05:03:56 +00:00
|
|
|
num = DL.malloc(DL.sizeof('L'))
|
2006-07-27 22:28:19 +00:00
|
|
|
rco.call(inp, buf, size, num, 0)
|
2006-07-27 05:03:56 +00:00
|
|
|
buf.to_s
|
|
|
|
|
|
|
|
rescue ::Exception
|
|
|
|
raise $!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
#
|
|
|
|
# Platform independent socket pair
|
|
|
|
#
|
|
|
|
def self.pipe
|
2006-07-27 05:35:29 +00:00
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
if (! is_windows())
|
|
|
|
# Standard pipes should be fine
|
|
|
|
return ::IO.pipe
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a socket connection for Windows
|
2006-07-27 00:59:00 +00:00
|
|
|
serv = nil
|
|
|
|
port = 1024
|
|
|
|
|
|
|
|
while (! serv and port < 65535)
|
|
|
|
begin
|
|
|
|
serv = TCPServer.new('127.0.0.1', (port += 1))
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
pipe1 = TCPSocket.new('127.0.0.1', port)
|
2006-07-27 04:16:39 +00:00
|
|
|
|
2006-07-27 00:59:00 +00:00
|
|
|
# Accept the forked child
|
2006-07-27 22:28:19 +00:00
|
|
|
pipe2 = serv.accept
|
2006-07-27 00:59:00 +00:00
|
|
|
|
|
|
|
# Shutdown the server
|
|
|
|
serv.close
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
return [pipe1, pipe2]
|
2006-07-27 00:59:00 +00:00
|
|
|
end
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
|
2006-07-27 00:59:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|