2006-07-27 00:59:00 +00:00
|
|
|
module Rex
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class provides os-specific functionality
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module Compat
|
|
|
|
|
2008-01-22 04:57:06 +00:00
|
|
|
STD_INPUT_HANDLE = -10
|
|
|
|
STD_OUTPUT_HANDLE = -11
|
|
|
|
STD_ERROR_HANDLE = -12
|
|
|
|
|
|
|
|
GENERIC_READ = 0x80000000
|
|
|
|
GENERIC_WRITE = 0x40000000
|
|
|
|
GENERIC_EXECUTE = 0x20000000
|
|
|
|
|
|
|
|
FILE_SHARE_READ = 0x00000001
|
|
|
|
FILE_SHARE_WRITE = 0x00000002
|
|
|
|
OPEN_EXISTING = 0x00000003
|
|
|
|
|
2006-07-27 00:59:00 +00:00
|
|
|
ENABLE_LINE_INPUT = 2
|
|
|
|
ENABLE_ECHO_INPUT = 4
|
|
|
|
ENABLE_PROCESSED_INPUT = 1
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
|
2008-11-12 22:47:21 +00:00
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
#
|
|
|
|
# Platform detection
|
|
|
|
#
|
2008-07-26 18:15:35 +00:00
|
|
|
|
2008-11-12 22:47:21 +00:00
|
|
|
@@is_windows = @@is_macosx = @@is_linux = @@is_bsdi = @@is_freebsd = @@is_netbsd = @@is_openbsd = @@is_java = false
|
2008-11-17 05:02:13 +00:00
|
|
|
@@loaded_win32api = false
|
|
|
|
@@loaded_dl = false
|
|
|
|
@@loaded_tempfile = false
|
|
|
|
@@loaded_fileutils = false
|
|
|
|
|
2008-07-26 18:15:35 +00:00
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
def self.is_windows
|
2008-07-26 18:15:35 +00:00
|
|
|
return @@is_windows if @@is_windows
|
|
|
|
@@is_windows = (RUBY_PLATFORM =~ /mswin32/) ? true : false
|
2006-07-27 22:28:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_macosx
|
2008-07-26 18:15:35 +00:00
|
|
|
return @@is_macosx if @@is_macosx
|
|
|
|
@@is_macosx = (RUBY_PLATFORM =~ /darwin/) ? true : false
|
2006-07-27 22:28:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_linux
|
2008-07-26 18:15:35 +00:00
|
|
|
return @@is_linux if @@is_linux
|
|
|
|
@@is_linux = (RUBY_PLATFORM =~ /linux/) ? true : false
|
2006-07-27 22:28:19 +00:00
|
|
|
end
|
|
|
|
|
2008-07-26 18:15:35 +00:00
|
|
|
def self.is_bsdi
|
|
|
|
return @@is_bsdi if @@is_bsdi
|
|
|
|
@@is_bsdi = (RUBY_PLATFORM =~ /bsdi/i) ? true : false
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_netbsd
|
|
|
|
return @@is_netbsd if @@is_netbsd
|
|
|
|
@@is_netbsd = (RUBY_PLATFORM =~ /netbsd/) ? true : false
|
|
|
|
end
|
2008-01-06 21:51:07 +00:00
|
|
|
|
|
|
|
def self.is_freebsd
|
2008-07-26 18:15:35 +00:00
|
|
|
return @@is_freebsd if @@is_freebsd
|
|
|
|
@@is_freebsd = (RUBY_PLATFORM =~ /freebsd/) ? true : false
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_openbsd
|
|
|
|
return @@is_openbsd if @@is_openbsd
|
|
|
|
@@is_openbsd = (RUBY_PLATFORM =~ /openbsd/) ? true : false
|
2008-01-06 21:51:07 +00:00
|
|
|
end
|
|
|
|
|
2008-11-12 22:47:21 +00:00
|
|
|
def self.is_java
|
|
|
|
return @@is_java if @@is_java
|
|
|
|
@@is_java = (RUBY_PLATFORM =~ /java/) ? true : false
|
|
|
|
end
|
|
|
|
|
2007-12-31 01:19:49 +00:00
|
|
|
def self.open_browser(url='http://metasploit.com/')
|
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /mswin32/
|
2008-01-24 04:03:10 +00:00
|
|
|
@s32 ||= DL.dlopen("shell32.dll")
|
2008-01-24 04:04:01 +00:00
|
|
|
se = @s32['ShellExecute', 'LPPPPPL']
|
2008-01-24 04:03:10 +00:00
|
|
|
se.call(nil, "open".to_s, url, nil, nil, 0)
|
2007-12-31 01:19:49 +00:00
|
|
|
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/
|
2008-01-24 04:03:10 +00:00
|
|
|
@s32 ||= DL.dlopen("shell32.dll")
|
2008-01-24 04:04:01 +00:00
|
|
|
se = @s32['ShellExecute', 'LPPPPPL']
|
2008-01-24 04:03:10 +00:00
|
|
|
se.call(nil, "open".to_s, url, nil, nil, 0)
|
2007-12-31 04:05:51 +00:00
|
|
|
when /darwin/
|
|
|
|
system("open mailto:#{addr}")
|
|
|
|
else
|
|
|
|
# ?
|
|
|
|
end
|
|
|
|
end
|
2006-07-27 22:28:19 +00:00
|
|
|
|
2008-11-10 21:44:58 +00:00
|
|
|
def self.getenv(var)
|
|
|
|
if (is_windows and @@loaded_win32api)
|
|
|
|
f = Win32API.new("kernel32", "GetEnvironmentVariable", ["P", "P", "I"], "I")
|
|
|
|
buff = "\x00" * 65536
|
|
|
|
sz = f.call(var, buff, buff.length)
|
|
|
|
return nil if sz == 0
|
|
|
|
buff[0,sz]
|
|
|
|
else
|
|
|
|
ENV[var]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.setenv(var,val)
|
|
|
|
if (is_windows and @@loaded_win32api)
|
|
|
|
f = Win32API.new("kernel32", "SetEnvironmentVariable", ["P", "P"], "I")
|
|
|
|
f.call(var, val + "\x00")
|
|
|
|
else
|
|
|
|
ENV[var]= val
|
|
|
|
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
|
|
|
|
|
2008-01-22 04:57:06 +00:00
|
|
|
#
|
|
|
|
# Get a handle to Win32 /dev/null
|
|
|
|
#
|
|
|
|
def self.win32_dev_null
|
|
|
|
begin
|
|
|
|
@@k32 ||= DL.dlopen("kernel32.dll")
|
2008-01-22 05:48:16 +00:00
|
|
|
crt = @@k32['CreateFile', 'LPLLLLLL']
|
2008-01-22 04:57:06 +00:00
|
|
|
|
2008-01-22 05:27:23 +00:00
|
|
|
hnd, rs = crt.call(
|
2008-01-22 05:30:45 +00:00
|
|
|
("NUL\x00").to_ptr,
|
2008-01-22 05:48:16 +00:00
|
|
|
-GENERIC_READ | -GENERIC_WRITE,
|
2008-01-22 05:27:23 +00:00
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
2008-01-22 04:57:06 +00:00
|
|
|
0,
|
2008-01-22 05:27:23 +00:00
|
|
|
OPEN_EXISTING,
|
2008-01-22 04:57:06 +00:00
|
|
|
0,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
|
2008-01-22 05:27:23 +00:00
|
|
|
hnd
|
2008-01-22 04:57:06 +00:00
|
|
|
rescue ::Exception
|
|
|
|
raise $!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Set a standard handle to a new value
|
|
|
|
#
|
|
|
|
def self.win32_set_std_handle(std, hnd)
|
|
|
|
begin
|
|
|
|
|
|
|
|
sid = STD_OUTPUT_HANDLE
|
|
|
|
case std.downcase
|
|
|
|
when 'stdin'
|
|
|
|
sid = STD_INPUT_HANDLE
|
|
|
|
when 'stderr'
|
|
|
|
sid = STD_ERROR_HANDLE
|
|
|
|
when 'stdout'
|
2008-01-22 05:22:14 +00:00
|
|
|
sid = STD_OUTPUT_HANDLE
|
2008-01-22 04:57:06 +00:00
|
|
|
else
|
|
|
|
raise ArgumentError, "Standard handle must be one of stdin/stdout/stderr"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
@@k32 ||= DL.dlopen("kernel32.dll")
|
|
|
|
ssh = @@k32['SetStdHandle', 'LLL']
|
|
|
|
ssh.call(sid, hnd)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2008-11-17 05:02:13 +00:00
|
|
|
#
|
|
|
|
# Copy a file to a temporary path
|
|
|
|
#
|
|
|
|
|
|
|
|
def self.temp_copy(path)
|
|
|
|
raise RuntimeError,"missing Tempfile" if not @@loaded_tempfile
|
|
|
|
fd = File.open(path, "rb")
|
|
|
|
tp = Tempfile.new("msftemp")
|
|
|
|
tp.write(fd.read(File.size(path)))
|
|
|
|
tp.close
|
|
|
|
fd.close
|
|
|
|
tp
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Delete an opened temporary file
|
|
|
|
#
|
|
|
|
|
|
|
|
def self.temp_delete(tp)
|
|
|
|
raise RuntimeError,"missing FileUtils" if not @@loaded_fileutils
|
|
|
|
begin
|
|
|
|
FileUtils.rm(tp.path)
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
|
2008-11-10 21:44:58 +00:00
|
|
|
#
|
|
|
|
# Initialization
|
|
|
|
#
|
|
|
|
|
|
|
|
if(is_windows)
|
|
|
|
begin
|
|
|
|
require "Win32API"
|
|
|
|
@@loaded_win32api = true
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2008-11-12 22:47:21 +00:00
|
|
|
begin
|
|
|
|
require "dl"
|
|
|
|
@@loaded_dl = true
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
|
|
|
|
2008-11-17 05:02:13 +00:00
|
|
|
begin
|
|
|
|
require "tempfile"
|
|
|
|
@@loaded_tempfile = true
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
2008-11-12 22:47:21 +00:00
|
|
|
|
2008-11-17 05:02:13 +00:00
|
|
|
begin
|
|
|
|
require "fileutils"
|
|
|
|
@@loaded_fileutils = true
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
2008-11-12 22:47:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-11-10 21:44:58 +00:00
|
|
|
end
|
2006-07-27 00:59:00 +00:00
|
|
|
end
|