cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

encrypt_token.py (1488B)


      1import json
      2import base64
      3import os
      4from Crypto.Cipher import AES
      5from Crypto.Util.Padding import pad, unpad
      6
      7CIPHER = 'AES-256-CBC'
      8SECRET_KEY = b'MySuperSecretKeyForParamsToken12'  # 32 bytes key for AES-256
      9
     10token_object = {
     11    "connection": {
     12        "type": "rdp",
     13        "settings": {
     14            "hostname": "10.0.0.12",
     15            "username": "Administrator",
     16            "password": "pAsSwOrD",
     17            "enable-drive": True,
     18            "create-drive-path": True,
     19            "security": "any",
     20            "ignore-cert": True,
     21            "enable-wallpaper": False
     22        }
     23    }
     24}
     25
     26
     27def encrypt_token(value):
     28    iv = os.urandom(16)  # 16 bytes for AES
     29    cipher = AES.new(SECRET_KEY, AES.MODE_CBC, iv)
     30
     31    # Convert value to JSON and pad it
     32    padded_data = pad(json.dumps(value).encode(), AES.block_size)
     33
     34    # Encrypt data
     35    encrypted_data = cipher.encrypt(padded_data)
     36
     37    # Encode the IV and encrypted data
     38    data = {
     39        'iv': base64.b64encode(iv).decode('utf-8'),
     40        'value': base64.b64encode(encrypted_data).decode('utf-8')
     41    }
     42
     43    # Convert the data dictionary to JSON and then encode it
     44    json_data = json.dumps(data)
     45    return base64.b64encode(json_data.encode()).decode('utf-8')
     46
     47
     48token = encrypt_token(token_object)
     49
     50print("Parameters:")
     51print(json.dumps(token_object, indent=4))
     52
     53print("\n\n")
     54
     55print("Encrypted token:")
     56print(token)
     57
     58print("\n\n")
     59
     60print("Use this token in the URL:")
     61print("ws://localhost:8080/?token=" + token)