Module: Msf::Handler::ReverseSctp

Includes:
Msf::Handler, Reverse, Msf::Handler::Reverse::Comm
Defined in:
lib/msf/core/handler/reverse_sctp.rb

Constant Summary

Constants included from Msf::Handler

Claimed, Unused

Instance Attribute Summary collapse

Attributes included from Msf::Handler

#exploit_config, #parent_payload, #pending_connections, #session_waiter_event, #sessions

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Msf::Handler::Reverse::Comm

#select_comm, #via_string

Methods included from Reverse

#bind_addresses, #bind_port, #is_loopback_address?

Methods included from Msf::Handler

#add_handler, #create_session, #handle_connection, #handler, #handler_name, #interrupt_wait_for_session, #register_session, #wait_for_session, #wfs_delay

Instance Attribute Details

#conn_threadsObject (protected)

:nodoc:



234
235
236
# File 'lib/msf/core/handler/reverse_sctp.rb', line 234

def conn_threads
  @conn_threads
end

#handler_threadObject (protected)

:nodoc:



233
234
235
# File 'lib/msf/core/handler/reverse_sctp.rb', line 233

def handler_thread
  @handler_thread
end

#listener_sockObject (protected)

:nodoc:



231
232
233
# File 'lib/msf/core/handler/reverse_sctp.rb', line 231

def listener_sock
  @listener_sock
end

#listener_threadObject (protected)

:nodoc:



232
233
234
# File 'lib/msf/core/handler/reverse_sctp.rb', line 232

def listener_thread
  @listener_thread
end

Class Method Details

.general_handler_typeObject

Returns the connection-described general handler type, in this case ‘reverse’.



34
35
36
# File 'lib/msf/core/handler/reverse_sctp.rb', line 34

def self.general_handler_type
  "reverse"
end

.handler_typeObject

Returns the string representation of the handler type, in this case ‘reverse_sctp’.



26
27
28
# File 'lib/msf/core/handler/reverse_sctp.rb', line 26

def self.handler_type
  "reverse_sctp"
end

Instance Method Details

#cleanup_handlerObject

Closes the listener socket if one was created.



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/msf/core/handler/reverse_sctp.rb', line 101

def cleanup_handler
  stop_handler

  # Kill any remaining handle_connection threads that might
  # be hanging around
  conn_threads.each do |thr|
    begin
      thr.kill
    rescue
      nil
    end
  end
end

#comm_stringObject



129
130
131
132
133
134
135
# File 'lib/msf/core/handler/reverse_sctp.rb', line 129

def comm_string
  if listener_sock.nil?
    "(setting up)"
  else
    via_string(listener_sock.client) if listener_sock.respond_to?(:client)
  end
end

#human_nameString

A string suitable for displaying to the user

Returns:

  • (String)


118
119
120
# File 'lib/msf/core/handler/reverse_sctp.rb', line 118

def human_name
  "reverse SCTP"
end

#initialize(info = {}) ⇒ Object

Initializes the reverse SCTP handler and ads the options that are required for all reverse SCTP payloads, like local host and local port.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/msf/core/handler/reverse_sctp.rb', line 42

def initialize(info = {})
  super

  # XXX: Not supported by all modules
  register_advanced_options(
    [
      OptAddress.new(
        'ReverseListenerBindAddress',
        [ false, 'The specific IP address to bind to on the local system' ]
      ),
      OptBool.new(
        'ReverseListenerThreaded',
        [ true, 'Handle every connection in a new thread (experimental)', false ]
      )
    ] +
    Msf::Opt::stager_retry_options,
    Msf::Handler::ReverseSctp
  )

  self.conn_threads = []
end

#listener_uri(addr = ) ⇒ String

A URI describing where we are listening

Parameters:

  • addr (String) (defaults to: )

    the address that

Returns:

  • (String)

    A URI of the form scheme://host:port/



141
142
143
144
145
# File 'lib/msf/core/handler/reverse_sctp.rb', line 141

def listener_uri(addr = datastore['ReverseListenerBindAddress'])
  addr = datastore['LHOST'] if addr.nil? || addr.empty?
  uri_host = Rex::Socket.is_ipv6?(addr) ? "[#{addr}]" : addr
  "sctp://#{uri_host}:#{bind_port}"
end

#payload_uriObject

A URI describing what the payload is configured to use for transport



123
124
125
126
127
# File 'lib/msf/core/handler/reverse_sctp.rb', line 123

def payload_uri
  addr = datastore['LHOST']
  uri_host = Rex::Socket.is_ipv6?(addr) ? "[#{addr}]" : addr
  "sctp://#{uri_host}:#{datastore['LPORT']}"
end

#setup_handlerObject



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
# File 'lib/msf/core/handler/reverse_sctp.rb', line 64

def setup_handler
  if !datastore['Proxies'].blank? && !datastore['ReverseAllowProxy']
    raise RuntimeError, "SCTP connect-back payloads cannot be used with Proxies. Use 'set ReverseAllowProxy true' to override this behaviour."
  end

  ex = false

  comm = select_comm
  local_port = bind_port

  bind_addresses.each do |ip|
    begin
      self.listener_sock = Rex::Socket::SctpServer.create(
        'LocalHost' => ip,
        'LocalPort' => local_port,
        'Comm'      => comm,
        'Context'   =>
        {
          'Msf'        => framework,
          'MsfPayload' => self,
          'MsfExploit' => assoc_exploit
        })
    rescue
      ex = $!
      print_error("Handler failed to bind to #{ip}:#{local_port}:- #{comm} -")
    else
      ex = false
      via = via_string(self.listener_sock.client) if self.listener_sock.respond_to?(:client)
      print_status("Started #{human_name} handler on #{ip}:#{local_port} #{via}")
      break
    end
  end
  raise ex if (ex)
end

#start_handlerObject

Starts monitoring for an inbound connection.



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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/msf/core/handler/reverse_sctp.rb', line 150

def start_handler
  queue = ::Queue.new

  local_port = bind_port

  handler_name = "ReverseSctpHandlerListener-#{local_port}"
  self.listener_thread = framework.threads.spawn(handler_name, false, queue) { |lqueue|
    loop do
      # Accept a client connection
      begin
        client = listener_sock.accept
        if client
          self.pending_connections += 1
          lqueue.push(client)
        end
      rescue Errno::ENOTCONN
        nil
      rescue StandardError => e
        wlog [
          "#{handler_name}: Exception raised during listener accept: #{e.class}",
          $ERROR_INFO.to_s,
          $ERROR_POSITION.join("\n")
        ].join("\n")
      end
    end
  }

  worker_name = "ReverseSctpHandlerWorker-#{local_port}"
  self.handler_thread = framework.threads.spawn(worker_name, false, queue) { |cqueue|
    loop do
      begin
        client = cqueue.pop

        unless client
          elog("#{worker_name}: Queue returned an empty result, exiting...")
        end

        # Timeout and datastore options need to be passed through to the client
        opts = {
          datastore:     datastore,
          expiration:    datastore['SessionExpirationTimeout'].to_i,
          comm_timeout:  datastore['SessionCommunicationTimeout'].to_i,
          retry_total:   datastore['SessionRetryTotal'].to_i,
          retry_wait:    datastore['SessionRetryWait'].to_i
        }

        if datastore['ReverseListenerThreaded']
          thread_name = "#{worker_name}-#{client.peerhost}"
          conn_threads << framework.threads.spawn(thread_name, false, client) do |client_copy|
            handle_connection(client_copy, opts)
          end
        else
          handle_connection(client, opts)
        end
      rescue StandardError => e
        elog('Exception raised from handle_connection', error: e)
      end
    end
  }
end

#stop_handlerObject

Stops monitoring for an inbound connection.



214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/msf/core/handler/reverse_sctp.rb', line 214

def stop_handler
  # Terminate the listener thread
  listener_thread.kill if listener_thread && listener_thread.alive? == true

  # Terminate the handler thread
  handler_thread.kill if handler_thread && handler_thread.alive? == true

  begin
    listener_sock.close if listener_sock
  rescue IOError
    # Ignore if it's listening on a dead session
    dlog("IOError closing listener sock; listening on dead session?", LEV_1)
  end
end