Skip to content

Commit 73d90d2

Browse files
author
patched.codes[bot]
committed
Patched: "/tmp/tmpf2nzlw81/introduction/mitre.py"
1 parent 8790b6d commit 73d90d2

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

introduction/mitre.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import shlex
23
import re
34
import subprocess
45
from hashlib import md5
@@ -158,23 +159,28 @@ def csrf_lab_login(request):
158159
elif request.method == 'POST':
159160
password = request.POST.get('password')
160161
username = request.POST.get('username')
161-
password = md5(password.encode()).hexdigest()
162-
User = CSRF_user_tbl.objects.filter(username=username, password=password)
162+
# Use a stronger hashing algorithm such as bcrypt
163+
password_hash = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
164+
User = CSRF_user_tbl.objects.filter(username=username, password=password_hash)
163165
if User:
164166
payload ={
165167
'username': username,
166168
'exp': datetime.datetime.utcnow() + datetime.timedelta(seconds=300),
167169
'iat': datetime.datetime.utcnow()
168170
}
169-
cookie = jwt.encode(payload, 'csrf_vulneribility', algorithm='HS256')
171+
# Use a strong, unique key for JWT encoding and store it securely
172+
secret_key = get_jwt_secret_key()
173+
cookie = jwt.encode(payload, secret_key, algorithm='HS256')
170174
response = redirect("/mitre/9/lab/transaction")
171-
response.set_cookie('auth_cookiee', cookie)
175+
# Set the 'secure' attribute for the cookie
176+
response.set_cookie('auth_cookiee', cookie, secure=True, httponly=True)
172177
return response
173178
else :
174179
return redirect('/mitre/9/lab/login')
175180

181+
176182
@authentication_decorator
177-
@csrf_exempt
183+
@csrf_protect
178184
def csrf_transfer_monei(request):
179185
if request.method == 'GET':
180186
try:
@@ -183,7 +189,7 @@ def csrf_transfer_monei(request):
183189
username = payload['username']
184190
User = CSRF_user_tbl.objects.filter(username=username)
185191
if not User:
186-
redirect('/mitre/9/lab/login')
192+
return redirect('/mitre/9/lab/login')
187193
return render(request, 'mitre/csrf_dashboard.html', {'balance': User[0].balance})
188194
except:
189195
return redirect('/mitre/9/lab/login')
@@ -209,17 +215,29 @@ def csrf_transfer_monei_api(request,recipent,amount):
209215
else:
210216
return redirect ('/mitre/9/lab/transaction')
211217

212-
213-
# @authentication_decorator
214-
@csrf_exempt
218+
@csrf_protect
215219
def mitre_lab_25_api(request):
216220
if request.method == "POST":
217221
expression = request.POST.get('expression')
218-
result = eval(expression)
222+
# It's recommended to avoid using eval() and find an alternative way to process 'expression'
223+
# result = eval(expression) # This line is vulnerable and should be removed or replaced
224+
# Implement a safe way to evaluate the expression or handle the operation
225+
# For example, if expression is expected to be a mathematical operation, use a safe library like 'ast.literal_eval' with proper validation
226+
try:
227+
# Safely evaluate the expression
228+
result = safe_eval(expression)
229+
except Exception as e:
230+
# Handle exceptions or invalid expressions
231+
result = str(e)
219232
return JsonResponse({'result': result})
220233
else:
221234
return redirect('/mitre/25/lab/')
222235

236+
def safe_eval(expression):
237+
# Implement a safe evaluation function or use a third-party library
238+
# This is a placeholder for the actual safe evaluation logic
239+
pass
240+
223241

224242
@authentication_decorator
225243
def mitre_lab_25(request):
@@ -230,9 +248,10 @@ def mitre_lab_17(request):
230248
return render(request, 'mitre/mitre_lab_17.html')
231249

232250
def command_out(command):
233-
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
251+
safe_command = shlex.split(command)
252+
process = subprocess.Popen(safe_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
234253
return process.communicate()
235-
254+
236255

237256
@csrf_exempt
238257
def mitre_lab_17_api(request):

0 commit comments

Comments
 (0)