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, included, #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.



43
44
45
46
# File 'plugins/session_notifier.rb', line 43

def initialize(driver)
  super(driver)
  load_settings_from_config
end

Instance Attribute Details

#dingtalk_webhookObject (readonly)

Returns the value of attribute dingtalk_webhook.



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

def dingtalk_webhook
  @dingtalk_webhook
end

#gotify_addressObject (readonly)

Returns the value of attribute gotify_address.



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

def gotify_address
  @gotify_address
end

#gotify_sslcert_pathObject (readonly)

Returns the value of attribute gotify_sslcert_path.



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

def gotify_sslcert_path
  @gotify_sslcert_path
end

#maximum_ipObject (readonly)

Returns the value of attribute maximum_ip.



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

def maximum_ip
  @maximum_ip
end

#minimum_ipObject (readonly)

Returns the value of attribute minimum_ip.



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

def minimum_ip
  @minimum_ip
end

#serverjang_webhookObject (readonly)

Returns the value of attribute serverjang_webhook.



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

def serverjang_webhook
  @serverjang_webhook
end

#sms_carrierObject (readonly)

Returns the value of attribute sms_carrier.



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

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.



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

def sms_number
  @sms_number
end

#smtp_addressObject (readonly)

Returns the value of attribute smtp_address.



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

def smtp_address
  @smtp_address
end

#smtp_fromObject (readonly)

Returns the value of attribute smtp_from.



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

def smtp_from
  @smtp_from
end

#smtp_passwordObject (readonly)

Returns the value of attribute smtp_password.



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

def smtp_password
  @smtp_password
end

#smtp_portObject (readonly)

Returns the value of attribute smtp_port.



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

def smtp_port
  @smtp_port
end

#smtp_usernameObject (readonly)

Returns the value of attribute smtp_username.



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

def smtp_username
  @smtp_username
end

Instance Method Details

#cmd_restart_session_notifier(*args) ⇒ Object



197
198
199
200
# File 'plugins/session_notifier.rb', line 197

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



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

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



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

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



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

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



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

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



86
87
88
89
90
91
92
93
94
95
# File 'plugins/session_notifier.rb', line 86

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



82
83
84
# File 'plugins/session_notifier.rb', line 82

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

#cmd_set_session_mobile_number(*args) ⇒ Object



73
74
75
76
77
78
79
80
# File 'plugins/session_notifier.rb', line 73

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_serverjang_webhook(*args) ⇒ Object



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

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

#cmd_set_session_smtp_address(*args) ⇒ Object



48
49
50
# File 'plugins/session_notifier.rb', line 48

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

#cmd_set_session_smtp_from(*args) ⇒ Object



69
70
71
# File 'plugins/session_notifier.rb', line 69

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

#cmd_set_session_smtp_password(*args) ⇒ Object



65
66
67
# File 'plugins/session_notifier.rb', line 65

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

#cmd_set_session_smtp_port(*args) ⇒ Object



52
53
54
55
56
57
58
59
# File 'plugins/session_notifier.rb', line 52

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



61
62
63
# File 'plugins/session_notifier.rb', line 61

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
188
189
190
# 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
    if !serverjang_webhook.nil?
      print_status('ServerJang 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



192
193
194
195
# File 'plugins/session_notifier.rb', line 192

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

#commandsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'plugins/session_notifier.rb', line 21

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)',
    'set_session_serverjang_webhook' => 'Set the ServerJiang webhook for the session notifier (keyword: session).',
    '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



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

def name
  'SessionNotifier'
end

#on_session_open(session) ⇒ Object



202
203
204
205
206
# File 'plugins/session_notifier.rb', line 202

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