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
|
2009-11-06 16:43:14 +00:00
|
|
|
OPEN_EXISTING = 0x00000003
|
2008-01-22 04:57:06 +00:00
|
|
|
|
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
|
|
|
|
2009-06-19 21:46:40 +00:00
|
|
|
@@is_windows = @@is_cygwin = @@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_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
|
2010-11-12 19:46:33 +00:00
|
|
|
@@is_windows = (RUBY_PLATFORM =~ /mswin32|mingw32/) ? true : false
|
2006-07-27 22:28:19 +00:00
|
|
|
end
|
|
|
|
|
2009-06-19 21:46:40 +00:00
|
|
|
def self.is_cygwin
|
|
|
|
return @@is_cygwin if @@is_cygwin
|
2009-09-04 15:11:28 +00:00
|
|
|
@@is_cygwin = (RUBY_PLATFORM =~ /cygwin/) ? true : false
|
2009-06-19 21:46:40 +00:00
|
|
|
end
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
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
|
|
|
|
|
2010-12-08 21:05:07 +00:00
|
|
|
def self.is_wow64
|
|
|
|
return false if not is_windows
|
|
|
|
is64 = false
|
|
|
|
begin
|
|
|
|
buff = "\x00" * 4
|
|
|
|
Win32API.new("kernel32","IsWow64Process",['L','P'],'L').call(-1, buff)
|
|
|
|
is64 = (buff.unpack("V")[0]) == 1 ? true : false
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
|
|
|
is64
|
|
|
|
end
|
|
|
|
|
2009-11-06 16:43:14 +00:00
|
|
|
def self.cygwin_to_win32(path)
|
|
|
|
if(path !~ /^\/cygdrive/)
|
|
|
|
return ::IO.popen("cygpath -w #{path}", "rb").read.strip
|
|
|
|
end
|
|
|
|
dir = path.split("/")
|
|
|
|
dir.shift
|
|
|
|
dir.shift
|
|
|
|
dir[0] = dir[0] + ":"
|
|
|
|
dir.join("\\")
|
|
|
|
end
|
|
|
|
|
2009-11-06 20:43:00 +00:00
|
|
|
def self.open_file(url='')
|
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /cygwin/
|
2010-01-24 17:28:03 +00:00
|
|
|
path = self.cygwin_to_win32(url)
|
2009-11-06 20:43:00 +00:00
|
|
|
system(["cmd", "cmd"], "/c", "explorer", path)
|
|
|
|
else
|
|
|
|
self.open_browser(url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-31 01:19:49 +00:00
|
|
|
def self.open_browser(url='http://metasploit.com/')
|
|
|
|
case RUBY_PLATFORM
|
2009-11-06 20:22:28 +00:00
|
|
|
when /cygwin/
|
|
|
|
if(url[0,1] == "/")
|
2009-11-06 20:43:00 +00:00
|
|
|
self.open_file(url)
|
2009-11-06 20:22:28 +00:00
|
|
|
end
|
2010-01-24 17:28:03 +00:00
|
|
|
return if not @@loaded_win32api
|
|
|
|
Win32API.new("shell32.dll", "ShellExecute", ["PPPPPL"], "L").call(nil, "open", url, nil, nil, 0)
|
2011-01-26 19:20:45 +00:00
|
|
|
when /mswin32|mingw/
|
2010-01-24 17:28:03 +00:00
|
|
|
return if not @@loaded_win32api
|
|
|
|
Win32API.new("shell32.dll", "ShellExecute", ["PPPPPL"], "L").call(nil, "open", url, nil, nil, 0)
|
2007-12-31 01:19:49 +00:00
|
|
|
when /darwin/
|
|
|
|
system("open #{url}")
|
|
|
|
else
|
2010-11-23 04:42:59 +00:00
|
|
|
# Search through the PATH variable (if it exists) and chose a browser
|
|
|
|
# We are making an assumption about the nature of "PATH" so tread lightly
|
|
|
|
if defined? ENV['PATH']
|
|
|
|
# "sensible-browser" opens the "default" browser in Ubuntu and others, so try that first
|
|
|
|
# but also provide fallbacks
|
|
|
|
['sensible-browser', 'firefox', 'opera', 'chromium-browser', 'konqueror'].each do |browser|
|
|
|
|
ENV['PATH'].split(':').each do |path|
|
|
|
|
# Does the browser exists?
|
|
|
|
if File.exists?("#{path}/#{browser}")
|
|
|
|
system("#{browser} #{url} &")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# If nothing else worked, default to firefox
|
2007-12-31 01:19:49 +00:00
|
|
|
system("firefox #{url} &")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-31 04:05:51 +00:00
|
|
|
def self.open_email(addr)
|
|
|
|
case RUBY_PLATFORM
|
2010-01-24 17:28:03 +00:00
|
|
|
when /mswin32|cygwin/
|
|
|
|
return if not @@loaded_win32api
|
|
|
|
Win32API.new("shell32.dll", "ShellExecute", ["PPPPPL"], "L").call(nil, "open", "mailto:"+addr, 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
|
|
|
|
2010-01-24 17:28:03 +00:00
|
|
|
def self.play_sound(path)
|
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /cygwin/
|
|
|
|
path = self.cygwin_to_win32(path)
|
|
|
|
return if not @@loaded_win32api
|
|
|
|
Win32API.new("winmm.dll", "sndPlaySoundA", ["SI"], "I").call(path, 0x20000)
|
|
|
|
when /mswin32/
|
|
|
|
return if not @@loaded_win32api
|
|
|
|
Win32API.new("winmm.dll", "sndPlaySoundA", ["SI"], "I").call(path, 0x20000)
|
|
|
|
when /darwin/
|
2010-01-24 18:33:32 +00:00
|
|
|
system("afplay #{path} >/dev/null 2>&1")
|
2010-01-24 17:28:03 +00:00
|
|
|
else
|
2010-01-24 18:33:32 +00:00
|
|
|
system("aplay #{path} >/dev/null 2>&1")
|
2010-01-24 17:28:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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")
|
2010-02-26 21:18:58 +00:00
|
|
|
buff = "\x00" * 16384
|
2008-11-10 21:44:58 +00:00
|
|
|
sz = f.call(var, buff, buff.length)
|
|
|
|
return nil if sz == 0
|
2009-11-06 16:43:14 +00:00
|
|
|
buff[0,sz]
|
2008-11-10 21:44:58 +00:00
|
|
|
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 00:59:00 +00:00
|
|
|
|
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
|
2010-01-24 17:28:03 +00:00
|
|
|
return nil if ! (is_windows and @@loaded_win32api)
|
|
|
|
gmh = Win32API.new("kernel32", "GetModuleHandle", ["P"], "L")
|
|
|
|
gmf = Win32API.new("kernel32", "GetModuleFileName", ["LPL"], "L")
|
|
|
|
mod = gmh.call(nil)
|
|
|
|
inf = "\x00" * 1024
|
|
|
|
gmf.call(mod, inf, 1024)
|
|
|
|
inf.unpack("Z*")[0]
|
2006-07-27 00:59:00 +00:00
|
|
|
end
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
#
|
2009-02-21 19:22:06 +00:00
|
|
|
# Call WinExec (equiv to system("cmd &"))
|
2006-07-27 22:28:19 +00:00
|
|
|
#
|
2006-07-27 00:59:00 +00:00
|
|
|
def self.win32_winexec(cmd)
|
2010-01-24 17:28:03 +00:00
|
|
|
return nil if ! (is_windows and @@loaded_win32api)
|
|
|
|
exe = Win32API.new("kernel32", "WinExec", ["PL"], "L")
|
|
|
|
exe.call(cmd, 0)
|
2008-01-22 04:57:06 +00:00
|
|
|
end
|
|
|
|
|
2011-07-13 05:01:37 +00:00
|
|
|
#
|
|
|
|
# Verify the Console2 environment
|
|
|
|
#
|
|
|
|
def self.win32_console2_verify
|
|
|
|
buf = "\x00" * 512
|
|
|
|
out = Win32API.new("kernel32", "GetStdHandle", ["L"], "L").call(STD_OUTPUT_HANDLE)
|
2011-07-19 14:49:34 +00:00
|
|
|
res = Win32API.new("kernel32","GetConsoleTitle", ["PL"], "L").call(buf, buf.length-1) rescue 0
|
|
|
|
( res > 0 and buf.index("Console2 command").nil? ) ? false : true
|
|
|
|
end
|
2011-07-13 05:01:37 +00:00
|
|
|
|
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)
|
2009-11-06 16:43:14 +00:00
|
|
|
begin
|
2006-07-27 00:59:00 +00:00
|
|
|
serv = TCPServer.new('127.0.0.1', (port += 1))
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
|
|
|
end
|
2009-11-06 16:43:14 +00:00
|
|
|
|
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
|
2009-11-06 16:43:14 +00:00
|
|
|
|
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)
|
2009-11-06 16:43:14 +00:00
|
|
|
raise RuntimeError,"missing Tempfile" if not @@loaded_tempfile
|
2008-11-17 05:02:13 +00:00
|
|
|
fd = File.open(path, "rb")
|
2009-11-06 16:43:14 +00:00
|
|
|
tp = Tempfile.new("msftemp")
|
2010-10-12 01:48:42 +00:00
|
|
|
tp.binmode
|
2008-11-17 05:02:13 +00:00
|
|
|
tp.write(fd.read(File.size(path)))
|
|
|
|
tp.close
|
2009-11-06 16:43:14 +00:00
|
|
|
fd.close
|
2008-11-17 05:02:13 +00:00
|
|
|
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
|
|
|
|
#
|
|
|
|
|
2010-01-24 17:31:50 +00:00
|
|
|
if(is_windows or is_cygwin)
|
2008-11-10 21:44:58 +00:00
|
|
|
begin
|
|
|
|
require "Win32API"
|
|
|
|
@@loaded_win32api = true
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
|
|
|
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
|
2009-11-06 16:43:14 +00:00
|
|
|
|