【问题标题】:AWS SDK for PHP - Decrypting a Password适用于 PHP 的 AWS 开发工具包 - 解密密码
【发布时间】:2015-05-29 11:38:26
【问题描述】:

对于我正在处理的项目,我正在使用适用于 PHP 的 Amazon AWS 开发工具包,并且我需要以纯文本格式检索服务器环境的密码。然而,ec2 方法的documentation 证实了我们的发现:该方法只会返回一个加密字符串。从表面上看,这很好,因为适用于 PHP 的 AWS 开发工具包使用未加密的 HTTP POST 请求通过 cURL 发送和接收数据,对用户是不可见的。因此,我们不会让我们的密码数据在网络上四处传播。

问题是没有解释如何解密字符串。我有我的私钥作为 PEM 文件,但没有方法或文档说明如何处理该字符串以使其可用。几次尝试都没有结果,我开始认为我需要重新考虑我正在进行的项目的策略,但后来我找到了最新版本的 AWS SDK for PHP 的代码,它揭示了如何去做解密字符串以生成纯文本形式的密码。

【问题讨论】:

  • 只要您保持问答格式,to share your finds and solutions 就可以了。我已经进行了相应的编辑。
  • 谢谢!注意以备将来参考。
  • 听起来您仍然需要重新考虑您的方法...为什么要将服务器的管理员密码传递给用户,然后返回?更何况,您为什么要使用没有 ssl 的敏感数据表单帖子?
  • 假设我们有正当理由获取服务器的管理员密码,@Michael - sqlbot。 AWS SDK for PHP 完全有可能使用 SSL。这一切都是通过服务器端 cURL 请求处理的,全部根据AWS SDK for PHP。这不是我写的,但我相信亚马逊会确保他们的服务是安全的。另外,根据我在下面的回答,我能够解密密码,因此无需重新考虑。
  • 我会买那个。 :) 我的评论是基于对问题的部分误解。道歉。

标签: php encryption amazon-web-services amazon-ec2 aws-sdk


【解决方案1】:

我找到的答案是 getPasswordData 方法返回一个既经过 base64 编码又经过加密的字符串。您需要先使用 base64_decode() 对其进行解码,然后才能使用 PHP 的 OpenSSL 库成功解密它。以下函数兼顾两者:

/**
 * @param obj $ec2_client The EC2 PHP client, from the AWS SDK for PHP
 * @param string $client_id The ID of the client whose password we're trying to get.
 * @return mixed The unencrypted password for the client, or false on failure.
 */
function aws_get_ec2_password($ec2_client, $client_id){
    //  First, run getPasswordData to get the Password Data Object.
    $pw_obj = $ec2_client->getPasswordData($client_id);

    //  Next, use the local get() method to isolate the password
    $pw_b64 = $pw_obj->get("PasswordData");

    //  Decode the password string.
    $pw_encrypted = base64_decode($pw_b64);

    //  Now, get your PEM key.
    //
    //  You can also use a raw string of the PEM key instead of get_file_contents(),
    //  or adjust the function so that you can pass it as an argument.
    //
    //  Technically, this step might not be necessary, as the documentation for
    //  openssl_private_decrypt() suggests that $key can just be the path, and it will
    //  create the key object internally.
    $key = openssl_get_privatekey(file_get_contents("path/to/key.pem"));

    //  Create an empty string to hold the password.
    $pw = "";

    //  Finally, decrypt the string and return (will return false if decryption fails).
    if(openssl_private_decrypt($pw_encrypted, $pw, $key)){
        return $pw;
    }else{
        return false;
    }
}

我希望这可以帮助其他人避免它给我带来的麻烦!

【讨论】:

  • 这里需要注意的一点是,这仅适用于使用随机密码生成的 AWS EC2 实例,该服务目前仅适用于以某种形式运行 Windows 的实例。 Linux 机器目前不具备此功能,因此可能值得在这里检查以确保需要密码的实例实际运行的是 Windows。
猜你喜欢
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
相关资源
最近更新 更多