我想启动一个Windows EC2实例并使用Python编程获取管理员密码。我知道可以使用CLI完成此操作,但我更喜欢本地解密以避免通过互联网发送我的私钥。
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 实例时没有使用密钥对,所以一开始就没有密码。 - Mathieu Dhondtpassword_data()
将返回空字符串。有关更多信息,请参见此处。 - Shane Bishopencrypted_str
看看是否获取了密码了吗? - Abhinav Mathur