M1-SR-SOME/Witter/apps/Witter/views.py

55 lines
1.3 KiB
Python

import json
from datetime import datetime
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse, HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from configuration.settings import BASE_DIR
# create the logging directory for the attacks
LOG_DIRECTORY = BASE_DIR / ".logs"
LOG_DIRECTORY.mkdir(exist_ok=True)
LOG_SOME_DIRECTORY = LOG_DIRECTORY / "some"
LOG_SOME_DIRECTORY.mkdir(exist_ok=True)
def view_csrf(request: WSGIRequest) -> HttpResponse:
"""
CSRF attack page
"""
return render(
request,
"Witter/csrf.html",
)
def view_some(request: WSGIRequest) -> HttpResponse:
"""
SOME attack page
"""
return render(
request,
"Witter/some.html",
)
@csrf_exempt
def view_logger(request: WSGIRequest) -> HttpResponse:
"""
Log all the information about a POST request
"""
# get the path of the file were to log the information
log_path = LOG_SOME_DIRECTORY / datetime.now().strftime("%Y-%m-%d %H-%M-%S.req")
# log the request
with log_path.open("w", encoding="utf-8") as log_file:
# dump all the data in the file
print(f"Body: {request.body}", file=log_file)
return HttpResponse()