Mirror of metasploit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

snmp.rb 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: binary -*-
  2. require 'msf/core'
  3. require 'snmp'
  4. module Msf
  5. ###
  6. #
  7. # This module exposes methods for querying a SNMP service
  8. #
  9. ###
  10. module Exploit::Remote::SNMPClient
  11. include Exploit::Remote::Udp
  12. #
  13. # Creates an instance of a SNMP exploit module.
  14. #
  15. def initialize(info = {})
  16. super
  17. # Register the options that all SNMP exploits may make use of.
  18. register_options(
  19. [
  20. Opt::RHOST,
  21. Opt::RPORT(161),
  22. OptString.new('COMMUNITY', [ true, 'SNMP Community String', 'public' ]),
  23. OptString.new('VERSION', [ true, 'SNMP Version <1/2c>', '1' ]),
  24. OptInt.new('TIMEOUT', [ true, 'SNMP Timeout', 1 ]),
  25. OptInt.new('RETRIES', [ true, 'SNMP Retries', 1 ])
  26. ], Msf::Exploit::Remote::SNMPClient)
  27. end
  28. #
  29. # This method wraps the snmp library and passes in
  30. # the Rex UDP socket
  31. #
  32. def connect_snmp(global=true, opts={})
  33. s = connect_udp(false, opts)
  34. version = :SNMPv1 if datastore['VERSION'] == '1'
  35. version = :SNMPv2c if datastore['VERSION'] == '2c'
  36. snmp = ::SNMP::Manager.new(
  37. :Host => opts['PeerHost'] || rhost,
  38. :Port => opts['PeerPort'] || rport,
  39. :Community => datastore['COMMUNITY'],
  40. :Version => version,
  41. :Timeout => datastore['TIMEOUT'],
  42. :Retries => datastore['RETRIES'],
  43. :Transport => SNMP::RexUDPTransport,
  44. :Socket => s
  45. )
  46. @snmp = snmp if global
  47. snmp
  48. end
  49. def disconnect_snmp
  50. @snmp.close if @snmp
  51. @snmp = nil
  52. end
  53. def snmp
  54. @snmp
  55. end
  56. def community
  57. datastore['COMMUNITY'] || 'public'
  58. end
  59. def timeout
  60. datastore['TIMEOUT'] || 1
  61. end
  62. def retries
  63. datastore['RETRIES'] || 1
  64. end
  65. protected
  66. attr_accessor :snmp
  67. end
  68. end