Untitled
import os import shutil import sqlite3 import base64 import hashlib from Crypto.Protocol.KDF import PBKDF2 from Crypto.Cipher import AES # 📌 Clé de chiffrement Master Key (forcée à "ploplop") MASTER_KEY = b"ploplop" # 📌 Fonction pour chiffrer un mot de passe def encryptpwd(plain_password): # Générer un sel unique (comme pgAdmin) SALT = os.urandom(16) # Dériver une clé de chiffrement avec PBKDF2-HMAC-SHA512 KEY = PBKDF2(MASTER_KEY, SALT, dkLen=32, count=25000, hmac_hash_module=hashlib.sha512) # Chiffrement AES-GCM cipher = AES.new(KEY, AES.MODE_GCM) ciphertext, tag = cipher.encrypt_and_digest(plain_password.encode()) # Encodage en base64 (format utilisé par pgAdmin) encrypted_password = base64.b64encode(SALT + cipher.nonce + tag + ciphertext).decode() return encrypted_password # 📌 Chemins vers pgAdmin et son fichier SQLite template_path = "C:/AHK/pgadmin4.db.template" appdata_path = os.path.expandvars(r"%APPDATA%\pgAdmin") pgadmin_db_path = os.path.join(appdata_path, "pgadmin4.db") # 📌 Création du dossier AppData si nécessaire if not os.path.exists(appdata_path): os.makedirs(appdata_path) print("📁 Dossier AppData de pgAdmin créé.") # 📌 Copier le template vers le dossier AppData shutil.copy(template_path, pgadmin_db_path) print("📄 Fichier pgadmin4.db copié depuis le template.") # 📌 Connexion à la copie de `pgadmin4.db` pour insérer le serveur PostgreSQL conn = sqlite3.connect(pgadmin_db_path) cursor = conn.cursor() # 📌 Ajout du serveur PostgreSQL avec le mot de passe chiffré cursor.execute(""" INSERT INTO server (id, user_id, servergroup_id, name, host, port, maintenance_db, username, password, save_password, kerberos_conn, comment, connection_params) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, (1, 1, 1, 'Aiven via Delinea', 'pg-c7fb92c-pam-demo.l.aivencloud.com', 23127, 'defaultdb', 'avnadmin', encryptpwd('toto'), 1, 0, 'Generated by Delinea Secret Server', '{"sslmode": "prefer", "connect_timeout": 10}')) # 📌 Commit et fermeture de la connexion SQLite conn.commit() conn.close() print("✅ Serveur PostgreSQL configuré avec mot de passe chiffré.")
Leave a Comment