app.py 3,41 ko
Newer Older
import hashlib
import itertools
from flask import Flask, request, jsonify

app = Flask(__name__)

# Configuration de Redis
redis_host = os.getenv("REDIS_HOST", "redis")  # "redis" correspond au nom du service Redis dans docker-compose
redis_port = int(os.getenv("REDIS_PORT", 6379))
redis_client = redis.StrictRedis(host=redis_host, port=redis_port, db=0, decode_responses=True)
@app.route("/bruteforce", methods=["POST"])
def bruteforce():
    """
    Endpoint pour bruteforcer un hash MD5.
    Reçoit un hash MD5 via un payload JSON et retourne la chaîne brute correspondante.
    """
    # Vérifier si le payload contient un hash
    data = request.json
    if not data or "hash" not in data:
        return jsonify({
            "status": "fail",
            "data": None,
            "errors": {
                "message": "Invalid payload. Please provide a valid hash."
            }
        }), 400

    target_hash = data["hash"]

    # Vérifier si le hash existe déjà dans Redis
    if redis_client.exists(target_hash):
        original = redis_client.get(target_hash)
        return jsonify({
            "status": "success",
            "data": {
                "hash": target_hash,
                "original": original
    # Logique de bruteforce
    charset = "abcdefghijklmnopqrstuvwxyz0123456789"
    for length in range(1, 8):  # Limite la longueur des combinaisons pour les performances
        for guess in itertools.product(charset, repeat=length):
            guess_str = ''.join(guess)
            if hashlib.md5(guess_str.encode()).hexdigest() == target_hash:
                # Ajouter le résultat dans Redis
                redis_client.set(target_hash, guess_str)
                return jsonify({
                    "status": "success",
                    "data": {
                        "hash": target_hash,
                        "original": guess_str
                    },
                    "errors": None
                }), 200

    # Si aucune correspondance n'est trouvée
    return jsonify({
        "status": "fail",
        "data": None,
        "errors": {
            "message": "No match found for the provided hash."
        }
    }), 404

@app.route("/resolved", methods=["GET"])
def get_resolved_hashes():
    """
    Endpoint pour récupérer tous les hash résolus.
    """
    # Récupérer toutes les clés et valeurs de Redis
    keys = redis_client.keys()
    resolved_hashes = {key: redis_client.get(key) for key in keys}

    return jsonify({
        "status": "success",
        "data": resolved_hashes,  # Retourne un dictionnaire {hash: original}
        "errors": None
    }), 200


@app.route("/resolved/<string:hash_value>", methods=["GET"])
def get_resolved_hash(hash_value):
    """
    Endpoint pour récupérer un hash spécifique.
    """
    if redis_client.exists(hash_value):
        original = redis_client.get(hash_value)
        return jsonify({
            "status": "success",
            "data": {
                "hash": hash_value,
                "original": original
            },
            "errors": None
        }), 200
    else:
        return jsonify({
            "status": "fail",
            "data": None,
            "errors": {
                "message": f"No resolved entry found for hash: {hash_value}"
            }
        }), 404


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)