From 843449196d839eacc2a1826eb0170414ac135f52 Mon Sep 17 00:00:00 2001 From: atxr Date: Mon, 26 Feb 2024 14:38:09 +0100 Subject: [PATCH] Add simple webapp to upload files --- webapp/app.py | 57 +++++++++++++++++++++++++++++++++++++ webapp/templates/index.html | 15 ++++++++++ 2 files changed, 72 insertions(+) create mode 100644 webapp/app.py create mode 100644 webapp/templates/index.html diff --git a/webapp/app.py b/webapp/app.py new file mode 100644 index 0000000..27cfed8 --- /dev/null +++ b/webapp/app.py @@ -0,0 +1,57 @@ +from flask import Flask, render_template, request +import socket +from struct import pack, unpack +import hashlib +import sys + +PORT = 8989 + +files = [] + +app = Flask(__name__) + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/upload', methods=['POST']) +def upload(): + if 'file' not in request.files: + return 'No file part' + file = request.files['file'] + if file.filename == '': + return 'No selected file' + + buf = file.read() + hash = hashlib.sha256(buf).digest() + + # Scan file + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(10) + s.connect(("localhost", PORT)) + + s.send(pack("I", len(buf))) + s.send(buf) + + try: + recv_hash = s.recv(32) + status = unpack("B", s.recv(1)) + + if (hash != recv_hash): + message = 'Error: Mismatching sha256.' + + elif (status == 1): + message = "Error: Potential virus found, cannot upload." + + else: + message = 'File successfully uploaded' + files.append({'filename': file.filename, 'content':buf}) + + except: + message = 'Error: Failed to scan file' + + s.close() + return message + +if __name__ == '__main__': + app.run(debug=True) diff --git a/webapp/templates/index.html b/webapp/templates/index.html new file mode 100644 index 0000000..bea13a6 --- /dev/null +++ b/webapp/templates/index.html @@ -0,0 +1,15 @@ + + + + + + File Upload + + +

File Upload

+
+ + +
+ +