From aa1317ce118b84368e224e910cb19182017bec28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20Mu=C3=B1oz?= Date: Mon, 24 Jun 2024 08:38:51 -0500 Subject: [PATCH] first commit --- .gitignore | 9 ++ app.py | 76 +++++++++++ requirements.txt | 21 +++ templates/index.html | 313 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 419 insertions(+) create mode 100644 .gitignore create mode 100644 app.py create mode 100644 requirements.txt create mode 100644 templates/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e02557 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/.venv +/.static +/__pycache__ +/templates/index.html~ +/templates/result.html +/templates/scan.html +/templates/upload.html +app.py~ +app2.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..b966cc0 --- /dev/null +++ b/app.py @@ -0,0 +1,76 @@ +from flask import Flask, render_template, request, jsonify +from flask_socketio import SocketIO, emit +import subprocess +import uuid +import os +import requests + +app = Flask(__name__) +socketio = SocketIO(app) + +@app.route('/', methods=['GET', 'POST']) +def index(): + return render_template('index.html') + +@app.route('/upload', methods=['POST']) +def upload_file(): + file = request.files.get('file') + if file: + file_path = os.path.join('/tmp', f"{uuid.uuid4()}_{file.filename}") + file.save(file_path) + + # Ejecuta el escaneo en un hilo separado para no bloquear la aplicación + socketio.start_background_task(target=scan_file, file_path=file_path) + + return 'Archivo subido exitosamente: ' + file.filename + return 'No se recibió ningún archivo', 400 + +@app.route('/scan_url', methods=['POST']) +def scan_url(): + url = request.json.get('url') + if not url: + return jsonify({'error': 'No URL provided'}), 400 + + # Añadir esquema si falta + if not url.startswith(('http://', 'https://')): + url = 'http://' + url + + try: + # Descargar el contenido de la URL a un archivo temporal + response = requests.get(url) + if response.status_code != 200: + return jsonify({'error': f'Error al descargar la URL: {response.status_code}'}), 400 + + file_path = os.path.join('/tmp', f"{uuid.uuid4()}_downloaded_content") + with open(file_path, 'wb') as temp_file: + temp_file.write(response.content) + + # Ejecutar el escaneo en un hilo separado para no bloquear la aplicación + socketio.start_background_task(target=scan_file, file_path=file_path) + + return jsonify({'message': f'Archivo descargado y guardado como {file_path}'}), 200 + except Exception as e: + return jsonify({'error': str(e)}), 500 + +def scan_file(file_path): + try: + scan_command = ["clamscan", "-r", file_path] + process = subprocess.Popen(scan_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) + + for line in iter(process.stdout.readline, ''): + if line.strip(): + socketio.emit('scan_output', {'data': line.strip()}) + + socketio.emit('scan_output', {'data': '--- Escaneo completado ---'}) + + process.stdout.close() + process.stderr.close() + process.wait() + except Exception as e: + socketio.emit('scan_output', {'data': f'Error: {str(e)}'}) + finally: + if os.path.exists(file_path): + os.remove(file_path) + +if __name__ == '__main__': + socketio.run(app, debug=True) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fbcf11d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,21 @@ +bidict +blinker +certifi +charset-normalizer +clamd +click +Flask +Flask-SocketIO +h11 +idna +itsdangerous +Jinja2 +MarkupSafe +pyClamd +python-engineio +python-socketio +requests +simple-websocket +urllib3 +Werkzeug +wsproto diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..9e07283 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,313 @@ + + + + + Condor Business Solutions SecureScan + + + + + + +
+
+

Condor Business Solutions CyberScan|Terminal Web de Escaneo

+ + + + + +
+ +
+ + + +