Module: Msf::Session::Interactive

Includes:
Rex::Ui::Interactive
Included in:
Basic, Msf::Sessions::HWBridge, Msf::Sessions::Meterpreter
Defined in:
lib/msf/core/session/interactive.rb

Overview

This class implements the stubs that are needed to provide an interactive session.

Instance Attribute Summary collapse

Attributes included from Rex::Ui::Interactive

#completed, #interacting, #next_session, #on_command_proc, #on_print_proc, #orig_suspend, #orig_usr1, #orig_winch

Attributes included from Rex::Ui::Subscriber::Input

#user_input

Attributes included from Rex::Ui::Subscriber::Output

#user_output

Instance Method Summary collapse

Methods included from Rex::Ui::Interactive

#_local_fd, #_remote_fd, #_stream_read_local_write_remote, #_stream_read_remote_write_local, #_winch, #detach, #handle_suspend, #handle_usr1, #handle_winch, #interact, #interact_stream, #prompt, #prompt_yesno, #restore_suspend, #restore_usr1, #restore_winch

Methods included from Rex::Ui::Subscriber

#copy_ui, #init_ui, #reset_ui

Methods included from Rex::Ui::Subscriber::Input

#gets

Methods included from Rex::Ui::Subscriber::Output

#flush, #print, #print_blank_line, #print_error, #print_good, #print_line, #print_status, #print_warning

Instance Attribute Details

#rstreamObject

The remote stream handle. Must inherit from Rex::IO::Stream.



102
103
104
# File 'lib/msf/core/session/interactive.rb', line 102

def rstream
  @rstream
end

Instance Method Details

#_interactObject (protected)

Stub method that is meant to handler interaction.



109
110
111
# File 'lib/msf/core/session/interactive.rb', line 109

def _interact
  framework.events.on_session_interact(self)
end

#_interact_completeObject (protected)

If the session reaches EOF, deregister it.



175
176
177
178
# File 'lib/msf/core/session/interactive.rb', line 175

def _interact_complete
  framework.events.on_session_interact_completed()
  framework.sessions.deregister(self, "User exit")
end

#_interruptObject (protected)

Check to see if the user wants to abort.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/msf/core/session/interactive.rb', line 116

def _interrupt
  begin
    intent = user_want_abort?
    # Judge the user wants to abort the reverse shell session
    # Or just want to abort the process running on the target machine
    # If the latter, just send ASCII Control Character \u0003 (End of Text) to the socket fd
    # The character will be handled by the line dicipline program of the pseudo-terminal on target machine
    # It will send the SEGINT singal to the foreground process
    if !intent
      # TODO: Check the shell is interactive or not
      # If the current shell is not interactive, the ASCII Control Character will not work
      if abort_foreground_supported
        print_status("Aborting foreground process in the shell session")
        abort_foreground
      end
      return
    end
  rescue Interrupt
    # The user hit ctrl-c while we were handling a ctrl-c. Ignore
  end
  true
end

#_suspendObject (protected)

Check to see if we should suspend.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/msf/core/session/interactive.rb', line 157

def _suspend
  # Ask the user if they would like to background the session
  intent = prompt_yesno("Background session #{name}?")
  if !intent
    # User does not want to background the current session
    # Assuming the target is *nix, we'll forward CTRL-Z to the foreground process on the target
    if !(self.platform=="windows" && self.type =="shell")
      print_status("Backgrounding foreground process in the shell session")
      self.rstream.write("\u001A")
    end
    return
  end
  self.interacting = false
end

#_usr1Object (protected)



147
148
149
150
151
152
# File 'lib/msf/core/session/interactive.rb', line 147

def _usr1
  # A simple signal to exit vim in reverse shell
  # Just for fun
  # Make sure you have already executed `shell` meta-shell command to pop up an interactive shell
  self.rstream.write("\x1B\x1B\x1B:q!\r")
end

#abort_foregroundObject (protected)



143
144
145
# File 'lib/msf/core/session/interactive.rb', line 143

def abort_foreground
  self.rstream.write("\u0003")
end

#abort_foreground_supportedObject (protected)



139
140
141
# File 'lib/msf/core/session/interactive.rb', line 139

def abort_foreground_supported
  true
end

#cleanupObject

Closes rstream.



88
89
90
91
92
93
94
95
96
97
# File 'lib/msf/core/session/interactive.rb', line 88

def cleanup
  begin
    self.interacting = false if self.interactive?
    rstream.close if (rstream)
  rescue ::Exception
  end

  rstream = nil
  super
end

#comm_channelObject



63
64
65
66
67
68
# File 'lib/msf/core/session/interactive.rb', line 63

def comm_channel
  return @comm_info if @comm_info
  if rstream.respond_to?(:channel) && rstream.channel.respond_to?(:client)
    @comm_info = "via session #{rstream.channel.client.sid}" if rstream.channel.client.respond_to?(:sid)
  end
end

#initialize(rstream, opts = {}) ⇒ Object

Initializes the session.



23
24
25
26
27
28
29
# File 'lib/msf/core/session/interactive.rb', line 23

def initialize(rstream, opts={})
  # A nil is passed in the case of non-stream interactive sessions (Meterpreter)
  if rstream
    self.rstream = rstream
  end
  super()
end

#interactive?Boolean

Returns that, yes, indeed, this session supports going interactive with the user.

Returns:

  • (Boolean)


35
36
37
# File 'lib/msf/core/session/interactive.rb', line 35

def interactive?
  true
end

#killObject

Terminate the session



79
80
81
82
83
# File 'lib/msf/core/session/interactive.rb', line 79

def kill
  self.reset_ui
  self.cleanup
  super()
end

#run_cmd(cmd) ⇒ Object

Run an arbitrary command as if it came from user input.



73
74
# File 'lib/msf/core/session/interactive.rb', line 73

def run_cmd(cmd)
end

#tunnel_localObject

Returns the local information.



42
43
44
45
46
47
48
49
# File 'lib/msf/core/session/interactive.rb', line 42

def tunnel_local
  return @local_info if @local_info
  begin
    @local_info = rstream.localinfo
  rescue ::Exception
    @local_info = '127.0.0.1'
  end
end

#tunnel_peerObject

Returns the remote peer information.



54
55
56
57
58
59
60
61
# File 'lib/msf/core/session/interactive.rb', line 54

def tunnel_peer
  return @peer_info if @peer_info
  begin
    @peer_info = rstream.peerinfo
  rescue ::Exception
    @peer_info = '127.0.0.1'
  end
end

#user_want_abort?Boolean (protected)

Checks to see if the user wants to abort.

Returns:

  • (Boolean)


183
184
185
# File 'lib/msf/core/session/interactive.rb', line 183

def user_want_abort?
  prompt_yesno("Abort session #{name}?")
end