【问题标题】:Decrypting Windows EC2 password in Python在 Python 中解密 Windows EC2 密码
【发布时间】:2022-03-21 20:41:03
【问题描述】:

我想启动一个 Windows EC2 实例并使用 Python 以编程方式获取管理员密码。我知道这可以使用像这样的 CLI 来完成,但我更愿意在本地解密以避免通过 Internet 发送我的私钥。

aws ec2 get-password-data --instance-id i-0d4d8273cadcae0a0 --priv-launch-key .ssh/elliott2.pem

阅读 Cryptodome 文档后,我尝试这样:

import boto3
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

ec2 = boto3.resource('ec2', 'us-west-2')
i = ec2.Instance('i-028dee2acb533fc59')

encrypted_str = i.password_data()['PasswordData']
with open('mykey.pem') as fp:
  key = RSA.importKey(fp.read())

cipher = PKCS1_OAEP.new(key)
print(cipher.decrypt(enc_str))

这失败并出现错误:

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    print(cipher.decrypt(encrypted_str))
  File "/Users/elliott/Library/Python/3.8/lib/python/site-packages/Crypto/Cipher/PKCS1_OAEP.py", line 167, in decrypt
    raise ValueError("Ciphertext with incorrect length.")
ValueError: Ciphertext with incorrect length.

我认为cipherkey 必须正好是 256 字节。但是密码数据比这个长,所以我不知道该怎么办。

【问题讨论】:

  • 您确定i.password_data()['PasswordData'] 不是''?我遇到了同样的问题,发现我在没有密钥对的情况下启动了我的 EC2 实例,所以一开始就没有密码。

标签: python amazon-ec2 boto3


【解决方案1】:

迟到但可能会有所帮助。

from Crypto.PublicKey import RSA
from Crypto.Cipher import AES, PKCS1_v1_5

def decrypt(ciphertext, keyfile = PEM_FILE ):
    input = open(keyfile)
    key = RSA.importKey(input.read())
    input.close()
    cipher = PKCS1_v1_5.new(key)
    plaintext = cipher.decrypt(ciphertext, None)
    return plaintext

【讨论】:

    猜你喜欢
    • 2020-05-14
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 2019-06-17
    • 2013-08-28
    • 1970-01-01
    相关资源
    最近更新 更多