Module: Rex::Compat

Defined in:
lib/rex/compat.rb

Overview

This class provides os-specific functionality

Constant Summary collapse

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
ENABLE_LINE_INPUT =
2
ENABLE_ECHO_INPUT =
4
ENABLE_PROCESSED_INPUT =
1
@@is_windows =

Platform detection

@@is_cygwin = @@is_macosx = @@is_linux = @@is_bsdi = @@is_freebsd = @@is_netbsd = @@is_openbsd = @@is_java = @@is_android = false
@@loaded_win32api =
false
@@loaded_tempfile =
false
@@loaded_fileutils =
false

Class Method Summary collapse

Class Method Details

.cygwin_to_win32(path) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/rex/compat.rb', line 101

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

.getenv(var) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rex/compat.rb', line 244

def self.getenv(var)
  if (is_windows and @@loaded_win32api)
    f = Win32API.new("kernel32", "GetEnvironmentVariable", ["P", "P", "I"], "I")
    buff = "\x00" * 16384
    sz = f.call(var, buff, buff.length)
    return nil if sz == 0
    buff[0,sz]
  else
    ENV[var]
  end
end

.is_androidObject



84
85
86
87
# File 'lib/rex/compat.rb', line 84

def self.is_android
  return @@is_android if @@is_android
  @@is_android = (RUBY_PLATFORM =~ /android/) ? true : false
end

.is_bsdiObject



59
60
61
62
# File 'lib/rex/compat.rb', line 59

def self.is_bsdi
  return @@is_bsdi if @@is_bsdi
  @@is_bsdi = (RUBY_PLATFORM =~ /bsdi/i) ? true : false
end

.is_cygwinObject



44
45
46
47
# File 'lib/rex/compat.rb', line 44

def self.is_cygwin
  return @@is_cygwin if @@is_cygwin
  @@is_cygwin = (RUBY_PLATFORM =~ /cygwin/) ? true : false
end

.is_freebsdObject



69
70
71
72
# File 'lib/rex/compat.rb', line 69

def self.is_freebsd
  return @@is_freebsd if @@is_freebsd
  @@is_freebsd = (RUBY_PLATFORM =~ /freebsd/) ? true : false
end

.is_javaObject



79
80
81
82
# File 'lib/rex/compat.rb', line 79

def self.is_java
  return @@is_java if @@is_java
  @@is_java = (RUBY_PLATFORM =~ /java/) ? true : false
end

.is_linuxObject



54
55
56
57
# File 'lib/rex/compat.rb', line 54

def self.is_linux
  return @@is_linux if @@is_linux
  @@is_linux = (RUBY_PLATFORM =~ /linux/) ? true : false
end

.is_macosxObject



49
50
51
52
# File 'lib/rex/compat.rb', line 49

def self.is_macosx
  return @@is_macosx if @@is_macosx
  @@is_macosx = (RUBY_PLATFORM =~ /darwin/) ? true : false
end

.is_netbsdObject



64
65
66
67
# File 'lib/rex/compat.rb', line 64

def self.is_netbsd
  return @@is_netbsd if @@is_netbsd
  @@is_netbsd = (RUBY_PLATFORM =~ /netbsd/) ? true : false
end

.is_openbsdObject



74
75
76
77
# File 'lib/rex/compat.rb', line 74

def self.is_openbsd
  return @@is_openbsd if @@is_openbsd
  @@is_openbsd = (RUBY_PLATFORM =~ /openbsd/) ? true : false
end

.is_windowsObject



39
40
41
42
# File 'lib/rex/compat.rb', line 39

def self.is_windows
  return @@is_windows if @@is_windows
  @@is_windows = (RUBY_PLATFORM =~ /mswin(32|64)|mingw(32|64)/) ? true : false
end

.is_wow64Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rex/compat.rb', line 89

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

.open_browser(url = 'http://google.com/') ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rex/compat.rb', line 122

def self.open_browser(url='http://google.com/')
  case RUBY_PLATFORM
  when /cygwin/
    if(url[0,1] == "/")
      self.open_file(url)
    end
    return if not @@loaded_win32api
    Win32API.new("shell32.dll", "ShellExecute", ["PPPPPL"], "L").call(nil, "open", url, nil, nil, 0)
  when /mswin32|mingw/
    return if not @@loaded_win32api
    Win32API.new("shell32.dll", "ShellExecute", ["PPPPPL"], "L").call(nil, "open", url, nil, nil, 0)
  when /darwin/
    system("open #{url}")
  when /android/
    system("am start --user 0 -a android.intent.action.VIEW -d #{url}")
  else
    # 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']
      # "xdg-open" is more general than "sensible-browser" and can be useful for lots of
      # file types -- text files, pcaps, or URLs. It's nearly always
      # going to use the application the user is expecting. If we're not
      # on something Debian-based, fall back to likely browsers.
      ['xdg-open', 'sensible-browser', 'firefox', 'firefox-bin', 'opera', 'konqueror', 'chromium-browser'].each do |browser|
        ENV['PATH'].split(':').each do |path|
          # Does the browser exists?
          if File.exist?("#{path}/#{browser}")
            system("#{browser} #{url} &")
            return
          end
        end
      end
    end
  end
end

.open_email(addr) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/rex/compat.rb', line 212

def self.open_email(addr)
  case RUBY_PLATFORM
  when /mswin32|cygwin/
    return if not @@loaded_win32api
    Win32API.new("shell32.dll", "ShellExecute", ["PPPPPL"], "L").call(nil, "open", "mailto:"+addr, nil, nil, 0)
  when /darwin/
    system("open mailto:#{addr}")
  when /android/
    system("am start --user 0 -a android.intent.action.VIEW -d mailto:#{addr}")
  else
    # ?
  end
end

.open_file(url = '') ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/rex/compat.rb', line 112

def self.open_file(url='')
  case RUBY_PLATFORM
  when /cygwin/
    path = self.cygwin_to_win32(url)
    system(["cmd", "cmd"], "/c", "explorer", path)
  else
    self.open_browser(url)
  end
end

.open_webrtc_browser(url = 'http://google.com/') ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rex/compat.rb', line 158

def self.open_webrtc_browser(url='http://google.com/')
  case RUBY_PLATFORM
  when /mswin2|mingw|cygwin/
      paths = [
        "Google\\Chrome\\Application\\chrome.exe",
        "Mozilla Firefox\\firefox.exe",
        "Opera\\launcher.exe"
      ]

      prog_files = ENV['ProgramFiles']
      paths = paths.map { |p| "#{prog_files}\\#{p}" }

      # Old chrome path
      app_data = ENV['APPDATA']
      paths << "#{app_data}\\Google\\Chrome\\Application\\chrome.exe"

      paths.each do |path|
        if File.exist?(path)
          args = (path =~ /chrome\.exe/) ? "--allow-file-access-from-files" : ""
          system("\"#{path}\" #{args} \"#{url}\"")
          return true
        end
      end

  when /darwin/
    ['Google Chrome.app', 'Firefox.app'].each do |browser|
      browser_path = "/Applications/#{browser}"
      if File.directory?(browser_path)
        args = (browser_path =~ /Chrome/) ? "--args --allow-file-access-from-files" : ""

        system("open #{url} -a \"#{browser_path}\" #{args} &")
        return true
      end
    end
  when /android/
    system("am start --user 0 -a android.intent.action.VIEW -d #{url}")
  else
    if defined? ENV['PATH']
      ['google-chrome', 'chrome', 'chromium', 'firefox' , 'firefox', 'opera'].each do |browser|
        ENV['PATH'].split(':').each do |path|
          browser_path = "#{path}/#{browser}"
          if File.exist?(browser_path)
            args = (browser_path =~ /Chrome/) ? "--allow-file-access-from-files" : ""
            system("#{browser_path} #{args} #{url} &")
            return true
          end
        end
      end
    end
  end

  false
end

.pipeObject

Platform independent socket pair



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/rex/compat.rb', line 313

def self.pipe

  if (! is_windows())
    # Standard pipes should be fine
    return ::IO.pipe
  end

  # Create a socket connection for Windows
  serv = nil
  port = 1024

  while (! serv and port < 65535)
    begin
      serv = TCPServer.new('127.0.0.1', (port += 1))
    rescue ::Exception
    end
  end

  pipe1 = TCPSocket.new('127.0.0.1', port)

  # Accept the forked child
  pipe2 = serv.accept

  # Shutdown the server
  serv.close

  return [pipe1, pipe2]
end

.play_sound(path) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/rex/compat.rb', line 226

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/
    system("afplay #{path} >/dev/null 2>&1")
  when /android/
    system("termux-open #{path}")
  else
    system("aplay #{path} >/dev/null 2>&1")
  end
end

.setenv(var, val) ⇒ Object



256
257
258
259
260
261
262
263
# File 'lib/rex/compat.rb', line 256

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

.temp_copy(path) ⇒ Object

Copy a file to a temporary path

Raises:



346
347
348
349
350
351
352
353
354
355
# File 'lib/rex/compat.rb', line 346

def self.temp_copy(path)
  raise RuntimeError,"missing Tempfile" if not @@loaded_tempfile
  fd = File.open(path, "rb")
  tp = Tempfile.new("msftemp")
  tp.binmode
  tp.write(fd.read(File.size(path)))
  tp.close
  fd.close
  tp
end

.temp_delete(tp) ⇒ Object

Delete an opened temporary file

Raises:



361
362
363
364
365
366
367
# File 'lib/rex/compat.rb', line 361

def self.temp_delete(tp)
  raise RuntimeError,"missing FileUtils" if not @@loaded_fileutils
  begin
    FileUtils.rm(tp.path)
  rescue
  end
end

.win32_console2_verifyObject

Verify the Console2 environment



291
292
293
294
295
296
297
# File 'lib/rex/compat.rb', line 291

def self.win32_console2_verify
  return nil if ! (is_windows and @@loaded_win32api)
  buf = "\x00" * 512
  out = Win32API.new("kernel32", "GetStdHandle", ["L"], "L").call(STD_OUTPUT_HANDLE)
  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

.win32_expand_path(path) ⇒ Object

Expand a 8.3 path to a full path



302
303
304
305
306
307
308
# File 'lib/rex/compat.rb', line 302

def self.win32_expand_path(path)
  return nil if ! (is_windows and @@loaded_win32api)
  glp = Win32API.new('kernel32', 'GetLongPathName', 'PPL', 'L')
  buf = "\x00" * 260
  len = glp.call(path, buf, buf.length)
  buf[0, len]
end

.win32_ruby_pathObject

Obtain the path to our interpreter



269
270
271
272
273
274
275
276
277
# File 'lib/rex/compat.rb', line 269

def self.win32_ruby_path
  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]
end

.win32_winexec(cmd) ⇒ Object

Call WinExec (equiv to system(“cmd &”))



282
283
284
285
286
# File 'lib/rex/compat.rb', line 282

def self.win32_winexec(cmd)
  return nil if ! (is_windows and @@loaded_win32api)
  exe = Win32API.new("kernel32", "WinExec", ["PL"], "L")
  exe.call(cmd, 0)
end