Files
CyberPanel/CyberCP/secMiddleware.py

172 lines
9.7 KiB
Python
Raw Normal View History

# coding=utf-8
2024-01-22 17:03:01 +05:00
import os.path
2024-01-04 22:43:56 +01:00
from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging
from django.shortcuts import HttpResponse, render
2018-09-24 18:56:48 +05:00
import json
import re
from loginSystem.models import Administrator
2018-06-30 15:29:56 +05:00
2024-01-06 15:11:55 +05:00
2018-06-30 15:29:56 +05:00
class secMiddleware:
HIGH = 0
LOW = 1
2021-06-08 23:21:41 +00:00
def get_client_ip(request):
ip = request.META.get('HTTP_CF_CONNECTING_IP')
if ip is None:
ip = request.META.get('REMOTE_ADDR')
return ip
2018-06-30 15:29:56 +05:00
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
try:
uID = request.session['userID']
admin = Administrator.objects.get(pk=uID)
2024-10-31 07:48:51 +04:00
ipAddr = secMiddleware.get_client_ip(request)
2019-07-24 22:37:37 +05:00
if ipAddr.find('.') > -1:
if request.session['ipAddr'] == ipAddr or admin.securityLevel == secMiddleware.LOW:
2019-07-24 22:37:37 +05:00
pass
else:
del request.session['userID']
del request.session['ipAddr']
2024-10-31 07:48:51 +04:00
logging.writeToFile(secMiddleware.get_client_ip(request))
2019-07-24 22:37:37 +05:00
final_dic = {'error_message': "Session reuse detected, IPAddress logged.",
"errorMessage": "Session reuse detected, IPAddress logged."}
final_json = json.dumps(final_dic)
return HttpResponse(final_json)
else:
2024-10-31 07:48:51 +04:00
ipAddr = secMiddleware.get_client_ip(request).split(':')[:3]
if request.session['ipAddr'] == ipAddr or admin.securityLevel == secMiddleware.LOW:
2019-07-24 22:37:37 +05:00
pass
else:
del request.session['userID']
del request.session['ipAddr']
2024-10-31 07:48:51 +04:00
logging.writeToFile(secMiddleware.get_client_ip(request))
2019-07-24 22:37:37 +05:00
final_dic = {'error_message': "Session reuse detected, IPAddress logged.",
"errorMessage": "Session reuse detected, IPAddress logged."}
final_json = json.dumps(final_dic)
return HttpResponse(final_json)
except:
pass
2024-01-22 17:03:01 +05:00
from plogical.processUtilities import ProcessUtilities
2024-01-24 11:05:51 +05:00
# if os.path.exists(ProcessUtilities.debugPath):
# logging.writeToFile(request.build_absolute_uri())
2024-01-22 17:03:01 +05:00
FinalURL = request.build_absolute_uri().split('?')[0]
2024-01-24 11:05:51 +05:00
# if os.path.exists(ProcessUtilities.debugPath):
# logging.writeToFile(f'Final actual URL without QS {FinalURL}')
2024-01-22 17:03:01 +05:00
2024-10-31 07:39:43 +04:00
if request.method == 'POST' or request.method == 'OPTIONS':
try:
2024-01-22 17:03:01 +05:00
2024-01-06 15:11:55 +05:00
# logging.writeToFile(request.body)
data = json.loads(request.body)
2019-12-10 15:09:10 +05:00
for key, value in data.items():
2019-02-21 17:19:04 +05:00
if request.path.find('gitNotify') > -1:
break
2019-12-15 11:34:09 +05:00
if type(value) == str or type(value) == bytes:
2018-10-03 18:46:44 +05:00
pass
2022-08-24 13:05:27 +05:00
elif type(value) == list:
2022-08-17 10:38:02 +05:00
for items in value:
2024-01-04 22:43:56 +01:00
if items.find('- -') > -1 or items.find('\n') > -1 or items.find(';') > -1 or items.find(
'&&') > -1 or items.find('|') > -1 or items.find('...') > -1 \
or items.find("`") > -1 or items.find("$") > -1 or items.find(
"(") > -1 or items.find(")") > -1 \
or items.find("'") > -1 or items.find("[") > -1 or items.find(
"]") > -1 or items.find("{") > -1 or items.find("}") > -1 \
2024-03-15 12:40:21 +05:00
or items.find(":") > -1 or items.find("<") > -1 or items.find(
">") > -1 or items.find("&") > -1:
2024-01-04 22:43:56 +01:00
logging.writeToFile(request.body)
2022-08-17 10:38:02 +05:00
final_dic = {
2024-01-04 22:43:56 +01:00
'error_message': "Data supplied is not accepted, following characters are not allowed in the input ` $ & ( ) [ ] { } ; : < >.",
"errorMessage": "Data supplied is not accepted, following characters are not allowed in the input ` $ & ( ) [ ] { } ; : < >."}
2022-08-17 10:38:02 +05:00
final_json = json.dumps(final_dic)
return HttpResponse(final_json)
else:
2022-08-24 13:05:27 +05:00
continue
if key == 'backupDestinations':
2024-01-06 15:11:55 +05:00
if re.match('^[a-z|0-9]+:[a-z|0-9|\.]+\/?[A-Z|a-z|0-9|\.]*$',
value) == None and value != 'local':
2024-01-04 22:43:56 +01:00
logging.writeToFile(request.body)
2022-08-24 13:05:27 +05:00
final_dic = {'error_message': "Data supplied is not accepted.",
"errorMessage": "Data supplied is not accepted."}
2022-08-17 10:38:02 +05:00
final_json = json.dumps(final_dic)
return HttpResponse(final_json)
2022-08-24 13:05:27 +05:00
2024-01-22 17:03:01 +05:00
if FinalURL.find(
'api/remoteTransfer') > -1 or FinalURL.find(
'api/verifyConn') > -1 or FinalURL.find(
'webhook') > -1 or FinalURL.find(
'saveSpamAssassinConfigurations') > -1 or FinalURL.find(
'docker') > -1 or FinalURL.find(
'cloudAPI') > -1 or FinalURL.find(
'verifyLogin') > -1 or FinalURL.find('submitUserCreation') > -1:
2022-08-24 13:05:27 +05:00
continue
2024-03-15 12:40:21 +05:00
if key == 'ownerPassword' or key == 'scriptUrl' or key == 'CLAMAV_VIRUS' or key == "Rspamdserver" or key == 'smtpd_milters' or key == 'non_smtpd_milters' or key == 'key' or key == 'cert' or key == 'recordContentAAAA' or key == 'backupDestinations' or key == 'ports' \
or key == 'imageByPass' or key == 'passwordByPass' or key == 'PasswordByPass' or key == 'cronCommand' \
2022-08-24 13:05:27 +05:00
or key == 'emailMessage' or key == 'configData' or key == 'rewriteRules' \
or key == 'modSecRules' or key == 'recordContentTXT' or key == 'SecAuditLogRelevantStatus' \
or key == 'fileContent' or key == 'commands' or key == 'gitHost' or key == 'ipv6' or key == 'contentNow':
continue
if value.find('- -') > -1 or value.find('\n') > -1 or value.find(';') > -1 or value.find(
'&&') > -1 or value.find('|') > -1 or value.find('...') > -1 \
or value.find("`") > -1 or value.find("$") > -1 or value.find("(") > -1 or value.find(
")") > -1 \
or value.find("'") > -1 or value.find("[") > -1 or value.find("]") > -1 or value.find(
"{") > -1 or value.find("}") > -1 \
2024-03-15 12:40:21 +05:00
or value.find(":") > -1 or value.find("<") > -1 or value.find(">") > -1 or value.find(
"&") > -1:
2024-01-04 22:43:56 +01:00
logging.writeToFile(request.body)
2022-08-24 13:05:27 +05:00
final_dic = {
2024-01-04 22:43:56 +01:00
'error_message': "Data supplied is not accepted, following characters are not allowed in the input ` $ & ( ) [ ] { } ; : < >.",
"errorMessage": "Data supplied is not accepted, following characters are not allowed in the input ` $ & ( ) [ ] { } ; : < >."}
2022-08-24 13:05:27 +05:00
final_json = json.dumps(final_dic)
return HttpResponse(final_json)
if key.find(';') > -1 or key.find('&&') > -1 or key.find('|') > -1 or key.find('...') > -1 \
or key.find("`") > -1 or key.find("$") > -1 or key.find("(") > -1 or key.find(")") > -1 \
or key.find("'") > -1 or key.find("[") > -1 or key.find("]") > -1 or key.find(
"{") > -1 or key.find("}") > -1 \
2024-01-22 17:03:01 +05:00
or key.find(":") > -1 or key.find("<") > -1 or key.find(">") > -1 or key.find("&") > -1:
2024-01-04 22:43:56 +01:00
logging.writeToFile(request.body)
2022-08-24 13:05:27 +05:00
final_dic = {'error_message': "Data supplied is not accepted.",
2024-01-04 22:43:56 +01:00
"errorMessage": "Data supplied is not accepted following characters are not allowed in the input ` $ & ( ) [ ] { } ; : < >."}
2022-08-24 13:05:27 +05:00
final_json = json.dumps(final_dic)
return HttpResponse(final_json)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
logging.writeToFile(str(msg))
response = self.get_response(request)
return response
2024-01-04 22:43:56 +01:00
# else:
# try:
# if request.path.find('cloudAPI/') > -1 or request.path.find('api/') > -1:
# pass
# else:
# uID = request.session['userID']
# except:
# return render(request, 'loginSystem/login.html', {})
2019-11-05 14:07:37 +05:00
2018-06-30 15:29:56 +05:00
response = self.get_response(request)
2019-11-06 14:02:30 +05:00
2019-11-05 14:07:37 +05:00
response['X-XSS-Protection'] = "1; mode=block"
2019-11-06 14:02:30 +05:00
response['X-Frame-Options'] = "sameorigin"
2019-11-07 09:37:06 +05:00
response['Content-Security-Policy'] = "script-src 'self' https://www.jsdelivr.com"
response['Content-Security-Policy'] = "connect-src *;"
2024-01-06 15:11:55 +05:00
response[
'Content-Security-Policy'] = "font-src 'self' 'unsafe-inline' https://www.jsdelivr.com https://fonts.googleapis.com"
response[
'Content-Security-Policy'] = "style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://www.jsdelivr.com https://cdnjs.cloudflare.com https://maxcdn.bootstrapcdn.com https://cdn.jsdelivr.net"
# response['Content-Security-Policy'] = "default-src 'self' cyberpanel.cloud *.cyberpanel.cloud"
2019-11-07 09:37:06 +05:00
response['X-Content-Type-Options'] = "nosniff"
response['Referrer-Policy'] = "same-origin"
2019-11-05 14:07:37 +05:00
2021-06-08 23:21:41 +00:00
return response