Module: Rex::Ui::Text::Resource

Included in:
Msf::Sessions::CommandShell, Msf::Ui::Console::Driver, DispatcherShell
Defined in:
lib/rex/ui/text/resource.rb

Instance Method Summary collapse

Instance Method Details

#load_resource(path) ⇒ void

This method returns an undefined value.

Processes a resource script file for the console.

Parameters:

  • path (String)

    Path to a resource file to run


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rex/ui/text/resource.rb', line 15

def load_resource(path)
  if path == '-'
    resource_file = $stdin.read
    path = 'stdin'
  elsif ::File.exist?(path)
    resource_file = ::File.read(path)
  else
    print_error("Cannot find resource script: #{path}")
    return
  end

  # Process ERB directives first
  print_status "Processing #{path} for ERB directives."
  erb = ERB.new(resource_file)
  processed_resource = erb.result(binding)

  lines = processed_resource.each_line.to_a
  bindings = {}
  while lines.length > 0

    line = lines.shift
    break if not line
    line.strip!
    next if line.length == 0
    next if line =~ /^#/

    # Pretty soon, this is going to need an XML parser :)
    # TODO: case matters for the tag and for binding names
    if line =~ /<ruby/
      if line =~ /\s+binding=(?:'(\w+)'|"(\w+)")(>|\s+)/
        bin = ($~[1] || $~[2])
        bindings[bin] = binding unless bindings.has_key? bin
        bin = bindings[bin]
      else
        bin = binding
      end
      buff = ''
      while lines.length > 0
        line = lines.shift
        break if not line
        break if line =~ /<\/ruby>/
        buff << line
      end
      if ! buff.empty?
        print_status("resource (#{path})> Ruby Code (#{buff.length} bytes)")
        begin
          eval(buff, bin)
        rescue ::Interrupt
          raise $!
        rescue ::Exception => e
          print_error("resource (#{path})> Ruby Error: #{e.class} #{e} #{e.backtrace}")
        end
      end
    else
      print_line("resource (#{path})> #{line}")
      run_single(line)
    end
  end
end