CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/plugins/libnotify.rb
Views: 1903
1
###
2
#
3
# This plugin hooks all session creation and db events
4
# and send desktop notifications using notify-send command.
5
#
6
###
7
8
module Msf
9
class Plugin::EventLibnotify < Msf::Plugin
10
include Msf::SessionEvent
11
include Msf::DatabaseEvent
12
13
def initialize(framework, opts)
14
super
15
16
@bin = opts[:bin] || opts['bin'] || `which notify-send`.chomp
17
@bin_opts = opts[:opts] || opts['opts'] || '--app-name=Metasploit'
18
19
raise 'libnotify not found' if @bin.empty?
20
21
self.framework.events.add_session_subscriber(self)
22
self.framework.events.add_db_subscriber(self)
23
end
24
25
def notify_send(urgency, title, message)
26
system(@bin, @bin_opts, '-u', urgency, title, message)
27
rescue StandarError => e
28
puts "Error running #{@bin}: #{e.message}"
29
end
30
31
def on_session_open(session)
32
notify_send('normal', 'Got Shell!',
33
"New Session: #{session.sid}\nIP: #{session.session_host}\nPeer: #{session.tunnel_peer}\n"\
34
"Platform: #{session.platform}\nType: #{session.type}")
35
end
36
37
def on_session_close(session, reason = '')
38
notify_send('normal', 'Connection closed',
39
"Session:#{session.sid} Type:#{session.type} closed.\n#{reason}")
40
end
41
42
def on_session_fail(reason = '')
43
notify_send('critical', 'Session Failure!', reason)
44
end
45
46
def on_db_host(host)
47
notify_send('normal', 'New host',
48
"Address: #{host.address}\nOS: #{host.os_name}")
49
end
50
51
def on_db_host_state(host, _ostate)
52
notify_send('normal', "Host #{host.address} changed",
53
"OS: #{host.os_name}\nNb Services: #{host.service_count}\nNb vulns: #{host.vuln_count}\n")
54
end
55
56
def on_db_service(service)
57
notify_send('normal', 'New service',
58
"New service: #{service.host.address}:#{service.port}")
59
end
60
61
def on_db_service_state(service, _port, _ostate)
62
notify_send('normal', "Service #{service.host.address}:#{service.port} changed",
63
"Name: #{service.name}\nState: #{service.state}\nProto: #{service.proto}\nInfo: #{service.info}")
64
end
65
66
def on_db_vuln(vuln)
67
notify_send('critical', "New vulnerability on #{vuln.host.address}:#{vuln.service ? vuln.service.port : '0'}",
68
"Vuln: #{vuln.name}\nInfos: #{vuln.info}")
69
end
70
71
def on_db_ref(ref)
72
notify_send('normal', 'New ref', "Reference #{ref.name} added in database.")
73
end
74
75
def on_db_client(client)
76
notify_send('critical', 'New client', "New client connected: #{client.ua_string}")
77
end
78
79
def cleanup
80
framework.events.remove_session_subscriber(self)
81
framework.events.remove_db_subscriber(self)
82
end
83
84
def name
85
'libnotify'
86
end
87
88
def desc
89
'Send desktop notification with libnotify on sessions and db events'
90
end
91
end
92
end
93
94