[英]EC2 Windows - Get Administrator Password
目前,我知道從新創建的EC2窗口實例檢索管理員密碼的唯一方法是通過AWS管理控制台。 這很好,但我需要知道如何通過Java API實現這一點 - 我似乎無法找到關於這個主題的任何內容。 此外,一旦獲得,我如何使用相同的API修改密碼?
EC2 API有一個調用“GetPasswordData”,您可以使用它來檢索包含管理員密碼的加密數據塊。 要解密它,你需要兩件事:
首先,私鑰。 這是您用於實例化實例的密鑰對的私有一半。 復雜的是,通常亞馬遜使用PEM格式的密鑰(“----- BEGIN”...),但Java Crypto API需要DER格式的密鑰。 您可以自己進行轉換 - 剝離----- BEGIN和----- END行,在中間取出文本塊並對其進行base64解碼。
二,加密參數。 數據使用RSA加密,帶有PKCS1填充 - 因此給予JCE的神奇調用是: Cipher.getInstance("RSA/NONE/PKCS1Padding")
這是一個完整的例子(依賴於BouncyCastle,但可以修改為使用不同的加密引擎)
package uk.co.frontiertown;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.GetPasswordDataRequest;
import com.amazonaws.services.ec2.model.GetPasswordDataResult;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
import javax.crypto.Cipher;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Security;
import java.security.spec.PKCS8EncodedKeySpec;
public class GetEc2WindowsAdministratorPassword {
private static final String ACCESS_KEY = "xxxxxxxxxxxxxxxxxxxx";
private static final String SECRET_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private static final String PRIVATE_KEY_MATERIAL = "-----BEGIN RSA PRIVATE KEY-----\n" +
"MIIEowIBAAKCAQEAjdD54kJ88GxkeRc96EQPL4h8c/7V2Q2QY5VUiJ+EblEdcVnADRa12qkohT4I\n" +
// several more lines of key data
"srz+xXTvbjIJ6RL/FDqF8lvWEvb8uSC7GeCMHTznkicwUs0WiFax2AcK3xjgtgQXMgoP\n" +
"-----END RSA PRIVATE KEY-----\n";
public static void main(String[] args) throws GeneralSecurityException, InterruptedException {
Security.addProvider(new BouncyCastleProvider());
String password = getPassword(ACCESS_KEY, SECRET_KEY, "i-XXXXXXXX", PRIVATE_KEY_MATERIAL);
System.out.println(password);
}
private static String getPassword(String accessKey, String secretKey, String instanceId, String privateKeyMaterial) throws GeneralSecurityException, InterruptedException {
// Convert the private key in PEM format to DER format, which JCE can understand
privateKeyMaterial = privateKeyMaterial.replace("-----BEGIN RSA PRIVATE KEY-----\n", "");
privateKeyMaterial = privateKeyMaterial.replace("-----END RSA PRIVATE KEY-----", "");
byte[] der = Base64.decode(privateKeyMaterial);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(der);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
// Get the encrypted password data from EC2
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonEC2Client client = new AmazonEC2Client(awsCredentials);
GetPasswordDataRequest getPasswordDataRequest = new GetPasswordDataRequest().withInstanceId(instanceId);
GetPasswordDataResult getPasswordDataResult = client.getPasswordData(getPasswordDataRequest);
String passwordData = getPasswordDataResult.getPasswordData();
while (passwordData == null || passwordData.isEmpty()) {
System.out.println("No password data - probably not generated yet - waiting and retrying");
Thread.sleep(10000);
getPasswordDataResult = client.getPasswordData(getPasswordDataRequest);
passwordData = getPasswordDataResult.getPasswordData();
}
// Decrypt the password
Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cipherText = Base64.decode(passwordData);
byte[] plainText = cipher.doFinal(cipherText);
String password = new String(plainText, Charset.forName("ASCII"));
return password;
}
}
ObDisclosure:我最初在http://www.frontiertown.co.uk/2012/03/java-administrator-password-windows-ec2-instance/上的博客上回答了這個問題。
您可以創建實例,設置密碼,然后將其重新轉換為圖像。 有效地為您創建的每個實例設置默認密碼。 這不是更簡單嗎?
看起來您正在尋找API的以下部分: GetPasswordDataRequest和GetPasswordDataResult
您還可以在該Image上創建一個具有默認用戶名和密碼設置的圖像。然后啟動具有該圖像id的所有實例。所以您不需要創建和檢索密碼evry time ..只需啟動啟動實例的實例rdp在圖像中使用definde credntials。 我正在做同樣的事情。它完全適合我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.