libnotify.rb 2.8 KB

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