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

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, #detach, #handle_suspend, #handle_usr1, #interact, #interact_stream, #prompt, #prompt_yesno, #restore_suspend, #restore_usr1

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.


96
97
98
# File 'lib/msf/core/session/interactive.rb', line 96

def rstream
  @rstream
end

Instance Method Details

#_interactObject (protected)

Stub method that is meant to handler interaction.


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

def _interact
  framework.events.on_session_interact(self)
end

#_interact_completeObject (protected)

If the session reaches EOF, deregister it.


161
162
163
164
# File 'lib/msf/core/session/interactive.rb', line 161

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.


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/msf/core/session/interactive.rb', line 110

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 !(self.platform=="windows" && self.type =="shell")
        print_status("Aborting foreground process in the shell session")
        self.rstream.write("\u0003")
      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.


143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/msf/core/session/interactive.rb', line 143

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)


133
134
135
136
137
138
# File 'lib/msf/core/session/interactive.rb', line 133

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

#cleanupObject

Closes rstream.


82
83
84
85
86
87
88
89
90
91
# File 'lib/msf/core/session/interactive.rb', line 82

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

  rstream = nil
  super
end

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

Initializes the session.


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

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)

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

def interactive?
  true
end

#killObject

Terminate the session


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

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

#run_cmd(cmd) ⇒ Object

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


67
68
# File 'lib/msf/core/session/interactive.rb', line 67

def run_cmd(cmd)
end

#tunnel_localObject

Returns the local information.


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

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.


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

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)

169
170
171
# File 'lib/msf/core/session/interactive.rb', line 169

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