db_tracker.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. module Msf
  2. ###
  3. #
  4. # This class hooks all socket calls and updates the database with
  5. # data gathered from the connection parameters
  6. #
  7. ###
  8. class Plugin::DB_Tracer < Msf::Plugin
  9. ###
  10. #
  11. # This class implements a socket communication tracker
  12. #
  13. ###
  14. class DBTracerEventHandler
  15. include Rex::Socket::Comm::Events
  16. def on_before_socket_create(comm, param); end
  17. def on_socket_created(_comm, sock, param)
  18. # Ignore local listening sockets
  19. return if !sock.peerhost
  20. if ((sock.peerhost != '0.0.0.0') && sock.peerport)
  21. # Ignore sockets that didn't set up their context
  22. # to hold the framework in 'Msf'
  23. return if !param.context['Msf']
  24. host = param.context['Msf'].db.find_or_create_host(host: sock.peerhost, state: Msf::HostState::Alive)
  25. return if !host
  26. param.context['Msf'].db.report_service(host: host, proto: param.proto, port: sock.peerport)
  27. end
  28. end
  29. end
  30. def initialize(framework, opts)
  31. super
  32. if !framework.db.active
  33. raise PluginLoadError, 'The database backend has not been initialized'
  34. end
  35. framework.plugins.each do |plugin|
  36. if plugin.instance_of?(Msf::Plugin::DB_Tracer)
  37. raise PluginLoadError, 'This plugin should not be loaded more than once'
  38. end
  39. end
  40. @eh = DBTracerEventHandler.new
  41. Rex::Socket::Comm::Local.register_event_handler(@eh)
  42. end
  43. def cleanup
  44. Rex::Socket::Comm::Local.deregister_event_handler(@eh)
  45. end
  46. def name
  47. 'db_tracker'
  48. end
  49. def desc
  50. 'Monitors socket calls and updates the database backend'
  51. end
  52. end
  53. end