感觉可能是需要对 connect_session 改动一下

后面有时间验证一下

lib/msf/ui/web/driver.rb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 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
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
157
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
# -*- coding: binary -*-
require 'rex/proto/http'
require 'msf/core'
require 'msf/base'
require 'msf/ui'

module Msf
module Ui
module Web

require 'rex/ui/text/bidirectional_pipe'
require 'msf/ui/web/console'


###
#
# This class implements a user interface driver on a web interface.
#
###
class Driver < Msf::Ui::Driver


  attr_accessor :framework # :nodoc:
  attr_accessor :consoles # :nodoc:
  attr_accessor :sessions # :nodoc:
  attr_accessor :last_console # :nodoc:

  ConfigCore  = "framework/core"
  ConfigGroup = "framework/ui/web"

  #
  # Initializes a web driver instance and prepares it for listening to HTTP
  # requests.  The constructor takes a hash of options that can control how
  # the web server will operate.
  #
  def initialize(opts = {})
    # Call the parent
    super()

    # Set the passed options hash for referencing later on.
    self.opts = opts

    self.consoles = {}
    self.sessions = {}

    if(opts[:framework])
      self.framework = opts[:framework]
    else
      # Initialize configuration
      Msf::Config.init

      # Initialize logging
      initialize_logging

      # Initialize attributes
      self.framework = Msf::Simple::Framework.create
    end

    # Initialize the console count
    self.last_console = 0
  end

  def create_console(opts={})
    # Destroy any unused consoles
    clean_consoles

    console = WebConsole.new(self.framework, self.last_console, opts)
    self.last_console += 1
    self.consoles[console.console_id.to_s] = console
    console.console_id.to_s
  end

  def destroy_console(cid)
    con = self.consoles[cid]
    if(con)
      con.shutdown
      self.consoles.delete(cid)
    end
  end


  def write_console(id, buf)
    self.consoles[id] ? self.consoles[id].write(buf) : nil
  end

  def read_console(id)
    self.consoles[id] ? self.consoles[id].read() : nil
  end

  def clean_consoles(timeout=300)
    self.consoles.each_pair do |id, con|
      if (con.last_access + timeout < Time.now)
        con.shutdown
        self.consoles.delete(id)
      end
    end
  end

  def write_session(id, buf)
    ses = self.framework.sessions[id]
    return if not ses
    return if not ses.user_input
    ses.user_input.put(buf)
  end

  def read_session(id)
    ses = self.framework.sessions[id]
    return if not ses
    return if not ses.user_output
    ses.user_output.read_subscriber('session_reader')
  end

  # Detach the session from an existing input/output pair
  def connect_session(id)

    # Ignore invalid sessions
    ses = self.framework.sessions[id]
    return if not ses

    # Has this session already been detached?
    if (ses.user_output)
      return if ses.user_output.has_subscriber?('session_reader')
    end

    # Create a new pipe
    spipe = WebConsole::WebConsolePipe.new
    spipe.input = spipe.pipe_input

    # Create a read subscriber
    spipe.create_subscriber('session_reader')

    framework.threads.spawn("ConnectSessionInteraction", false) do
      ses.interact(spipe.input, spipe)
    end
  end

  def sessions
    self.framework.sessions
  end

  #
  # Stub
  #
  def run
    true
  end

protected

  attr_accessor :opts      # :nodoc:

  #
  # Initializes logging for the web interface
  #
  def initialize_logging
    level = (opts['LogLevel'] || 0).to_i

    Msf::Logging.enable_log_source(LogSource, level)
  end

end


# Add DriverFactory, makes it possible to get the same Driver instance
class DriverFactory
  include Singleton

  def initialize()
    @drivers = Hash.new
    @mutex = Mutex.new
  end

  def get_or_create(opts={}, name='default')
    if not @drivers.key?(name)
      @mutex.synchronize {
        if not @drivers.key?(name)
          @drivers[name] = Driver.new(opts)
          return @drivers[name]
        end
      }
    end
    return @drivers[name]
  end
end

end
end
end