Class: Msf::Plugin::SessionNotifier::SessionNotifierCommandDispatcher

Inherits:
Object
  • Object
show all
Includes:
Ui::Console::CommandDispatcher
Defined in:
plugins/session_notifier.rb

Instance Attribute Summary collapse

Attributes included from Ui::Console::CommandDispatcher

#driver

Attributes included from Rex::Ui::Text::DispatcherShell::CommandDispatcher

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Ui::Console::CommandDispatcher

#active_module, #active_module=, #active_session, #active_session=, #build_range_array, #docs_dir, #framework, #load_config, #log_error, #remove_lines

Methods included from Rex::Ui::Text::DispatcherShell::CommandDispatcher

#cmd_help, #cmd_help_help, #cmd_help_tabs, #deprecated_cmd, #deprecated_commands, #deprecated_help, #docs_dir, #help_to_s, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #tab_complete_directory, #tab_complete_filenames, #tab_complete_generic, #tab_complete_source_address, #unknown_command, #update_prompt

Constructor Details

#initialize(driver) ⇒ SessionNotifierCommandDispatcher

Returns a new instance of SessionNotifierCommandDispatcher.


54
55
56
57
# File 'plugins/session_notifier.rb', line 54

def initialize(driver)
  super(driver)
  load_settings_from_config
end

Instance Attribute Details

#dingtalk_webhookObject (readonly)

Returns the value of attribute dingtalk_webhook


25
26
27
# File 'plugins/session_notifier.rb', line 25

def dingtalk_webhook
  @dingtalk_webhook
end

#gotify_addressObject (readonly)

Returns the value of attribute gotify_address


26
27
28
# File 'plugins/session_notifier.rb', line 26

def gotify_address
  @gotify_address
end

#gotify_sslcert_pathObject (readonly)

Returns the value of attribute gotify_sslcert_path


27
28
29
# File 'plugins/session_notifier.rb', line 27

def gotify_sslcert_path
  @gotify_sslcert_path
end

#maximum_ipObject (readonly)

Returns the value of attribute maximum_ip


24
25
26
# File 'plugins/session_notifier.rb', line 24

def maximum_ip
  @maximum_ip
end

#minimum_ipObject (readonly)

Returns the value of attribute minimum_ip


23
24
25
# File 'plugins/session_notifier.rb', line 23

def minimum_ip
  @minimum_ip
end

#sms_carrierObject (readonly)

Returns the value of attribute sms_carrier


16
17
18
# File 'plugins/session_notifier.rb', line 16

def sms_carrier
  @sms_carrier
end

#sms_clientObject (readonly)

Returns the value of attribute sms_client


15
16
17
# File 'plugins/session_notifier.rb', line 15

def sms_client
  @sms_client
end

#sms_numberObject (readonly)

Returns the value of attribute sms_number


17
18
19
# File 'plugins/session_notifier.rb', line 17

def sms_number
  @sms_number
end

#smtp_addressObject (readonly)

Returns the value of attribute smtp_address


18
19
20
# File 'plugins/session_notifier.rb', line 18

def smtp_address
  @smtp_address
end

#smtp_fromObject (readonly)

Returns the value of attribute smtp_from


22
23
24
# File 'plugins/session_notifier.rb', line 22

def smtp_from
  @smtp_from
end

#smtp_passwordObject (readonly)

Returns the value of attribute smtp_password


21
22
23
# File 'plugins/session_notifier.rb', line 21

def smtp_password
  @smtp_password
end

#smtp_portObject (readonly)

Returns the value of attribute smtp_port


19
20
21
# File 'plugins/session_notifier.rb', line 19

def smtp_port
  @smtp_port
end

#smtp_usernameObject (readonly)

Returns the value of attribute smtp_username


20
21
22
# File 'plugins/session_notifier.rb', line 20

def smtp_username
  @smtp_username
end

Instance Method Details

#cmd_restart_session_notifier(*args) ⇒ Object


194
195
196
197
# File 'plugins/session_notifier.rb', line 194

def cmd_restart_session_notifier(*args)
  cmd_stop_session_notifier(args)
  cmd_start_session_notifier(args)
end

#cmd_save_session_notifier_settings(*_args) ⇒ Object


153
154
155
156
# File 'plugins/session_notifier.rb', line 153

def cmd_save_session_notifier_settings(*_args)
  save_settings_to_config
  print_status('Session Notifier settings saved in config file.')
end

#cmd_set_session_dingtalk_webhook(*args) ⇒ Object


142
143
144
145
146
147
148
149
150
151
# File 'plugins/session_notifier.rb', line 142

def cmd_set_session_dingtalk_webhook(*args)
  webhook_url = args[0]
  if webhook_url.blank?
    @dingtalk_webhook = nil
  elsif !(webhook_url =~ URI::DEFAULT_PARSER.make_regexp).nil?
    @dingtalk_webhook = webhook_url
  else
    print_error('Invalid webhook_url')
  end
end

#cmd_set_session_gotify_address(*args) ⇒ Object


119
120
121
122
123
124
125
126
127
128
129
# File 'plugins/session_notifier.rb', line 119

def cmd_set_session_gotify_address(*args)
  webhook_url = args[0]
  if webhook_url.blank?
    @gotify_address = nil
  elsif !(webhook_url =~ URI::DEFAULT_PARSER.make_regexp).nil?
    @gotify_address = webhook_url
  else
    @gotify_address = nil
    print_error('Invalid gotify_address')
  end
end

#cmd_set_session_gotify_sslcert_path(*args) ⇒ Object


131
132
133
134
135
136
137
138
139
140
# File 'plugins/session_notifier.rb', line 131

def cmd_set_session_gotify_sslcert_path(*args)
  cert_path = args[0]
  if !cert_path.blank? && ::File.file?(cert_path) && ::File.readable?(cert_path)
    @gotify_sslcert_path = cert_path
    print_status("Set Gotify ssl_mode ON! Your cert path is #{gotify_sslcert_path}")
  else
    @gotify_sslcert_path = nil
    print_status('Set Gotify ssl_mode OFF!')
  end
end

#cmd_set_session_maximum_ip(*args) ⇒ Object


108
109
110
111
112
113
114
115
116
117
# File 'plugins/session_notifier.rb', line 108

def cmd_set_session_maximum_ip(*args)
  ip = args[0]
  if ip.blank?
    @maximum_ip = nil
  elsif Rex::Socket.self.dotted_ip?(ip)
    @maximum_ip = IPAddr.new(ip)
  else
    print_error('Invalid IP format')
  end
end

#cmd_set_session_minimum_ip(*args) ⇒ Object


97
98
99
100
101
102
103
104
105
106
# File 'plugins/session_notifier.rb', line 97

def cmd_set_session_minimum_ip(*args)
  ip = args[0]
  if ip.blank?
    @minimum_ip = nil
  elsif Rex::Socket.dotted_ip?(ip)
    @minimum_ip = IPAddr.new(ip)
  else
    print_error('Invalid IP format')
  end
end

#cmd_set_session_mobile_carrier(*args) ⇒ Object


93
94
95
# File 'plugins/session_notifier.rb', line 93

def cmd_set_session_mobile_carrier(*args)
  @sms_carrier = args[0].to_sym
end

#cmd_set_session_mobile_number(*args) ⇒ Object


84
85
86
87
88
89
90
91
# File 'plugins/session_notifier.rb', line 84

def cmd_set_session_mobile_number(*args)
  num = args[0]
  if num =~ /^\d{10}$/
    @sms_number = args[0]
  else
    print_error('Invalid phone format. It should be a 10-digit number that looks like: XXXXXXXXXX')
  end
end

#cmd_set_session_smtp_address(*args) ⇒ Object


59
60
61
# File 'plugins/session_notifier.rb', line 59

def cmd_set_session_smtp_address(*args)
  @smtp_address = args[0]
end

#cmd_set_session_smtp_from(*args) ⇒ Object


80
81
82
# File 'plugins/session_notifier.rb', line 80

def cmd_set_session_smtp_from(*args)
  @smtp_from = args[0]
end

#cmd_set_session_smtp_password(*args) ⇒ Object


76
77
78
# File 'plugins/session_notifier.rb', line 76

def cmd_set_session_smtp_password(*args)
  @smtp_password = args[0]
end

#cmd_set_session_smtp_port(*args) ⇒ Object


63
64
65
66
67
68
69
70
# File 'plugins/session_notifier.rb', line 63

def cmd_set_session_smtp_port(*args)
  port = args[0]
  if port =~ /^\d+$/
    @smtp_port = args[0]
  else
    print_error('Invalid port setting. Must be a number.')
  end
end

#cmd_set_session_smtp_username(*args) ⇒ Object


72
73
74
# File 'plugins/session_notifier.rb', line 72

def cmd_set_session_smtp_username(*args)
  @smtp_username = args[0]
end

#cmd_start_session_notifier(*_args) ⇒ Object


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
# File 'plugins/session_notifier.rb', line 158

def cmd_start_session_notifier(*_args)
  if session_notifier_subscribed?
    print_status('You already have an active session notifier.')
    return
  end

  begin
    framework.events.add_session_subscriber(self)
    if validate_sms_settings?
      smtp = Rex::Proto::Sms::Model::Smtp.new(
        address: smtp_address,
        port: smtp_port,
        username: smtp_username,
        password: smtp_password,
        login_type: :login,
        from: smtp_from
      )
      @sms_client = Rex::Proto::Sms::Client.new(carrier: sms_carrier, smtp_server: smtp)
      print_status('Session notification started.')
    end
    if !dingtalk_webhook.nil?
      print_status('DingTalk notification started.')
    end
    if !gotify_address.nil?
      print_status('Gotify notification started.')
    end
  rescue Msf::Plugin::SessionNotifier::Exception, Rex::Proto::Sms::Exception => e
    print_error(e.message)
  end
end

#cmd_stop_session_notifier(*_args) ⇒ Object


189
190
191
192
# File 'plugins/session_notifier.rb', line 189

def cmd_stop_session_notifier(*_args)
  framework.events.remove_session_subscriber(self)
  print_status('Session notification stopped.')
end

#commandsObject


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'plugins/session_notifier.rb', line 33

def commands
  {
    'set_session_smtp_address'       => 'Set the SMTP address for the session notifier',
    'set_session_smtp_port'          => 'Set the SMTP port for the session notifier',
    'set_session_smtp_username'      => 'Set the SMTP username',
    'set_session_smtp_password'      => 'Set the SMTP password',
    'set_session_smtp_from'          => 'Set the from field of SMTP',
    'set_session_mobile_number'      => 'Set the 10-digit mobile number you want to notify',
    'set_session_mobile_carrier'     => 'Set the mobile carrier of the phone',
    'set_session_minimum_ip'         => 'Set the minimum session IP range you want to be notified for',
    'set_session_maximum_ip'         => 'Set the maximum session IP range you want to be notified for',
    'set_session_dingtalk_webhook'   => 'Set the DingTalk webhook for the session notifier (keyword: session).',
    'set_session_gotify_address'     => 'Set the Gotify address for the session notifier',
    'set_session_gotify_sslcert_path' => 'Set the path to load your Gotify SSL cert (if you want to use HTTPS)',
    'save_session_notifier_settings' => 'Save all the session notifier settings to framework',
    'start_session_notifier'         => 'Start notifying sessions',
    'stop_session_notifier'          => 'Stop notifying sessions',
    'restart_session_notifier'       => 'Restart notifying sessions'
  }
end

#nameObject


29
30
31
# File 'plugins/session_notifier.rb', line 29

def name
  'SessionNotifier'
end

#on_session_open(session) ⇒ Object


199
200
201
202
203
# File 'plugins/session_notifier.rb', line 199

def on_session_open(session)
  subject = "You have a new #{session.type} session!"
  msg = "#{session.tunnel_peer} (#{session.session_host}) #{session.info ? "\"#{session.info}\"" : nil}"
  notify_session(session, subject, msg)
end