aws 存储区别_AWS凭证,存储更安全

aws 存储区别

默认情况下,aws cli在一个众所周知的位置以明文形式存储密钥ID和密钥。 怎么可能出问题了?!?

如果我告诉过你可以改变这一点并使事情变得至少有点困难怎么办?

AWS配置中有一个设置,允许AWS从外部获取凭证。 如果您不想将它们存储为纯文本内容,这可能会非常方便。

这称为“凭证过程”。 我们可以将其与本机openssl结合使用,为您提供穷人的加密AWS密钥。

首先,我们将获取aws凭证并将其转储到名为key.json的临时文件中

#key.json
{"Version" : 1 , 
"AccessKeyId" : "NOTAREALKEYID, 
" SecretAccessKey ": " NOTAREALSECRET
}

接下来,我们将创建一个名为crypto.sh的脚本,该脚本将接受传递给它的文件,然后生成一个加密文件。

#encrypt.sh
openssl aes-256-cbc -a -salt - in ${1} -out ${1} .enc -k "password"
# yes it is just "password" its a demo script! \
# Store this in an environment variable 
# or something safer than the script.

现在我们需要编写一个快速脚本,该脚本将解密加密的Blob,然后将其回显。 在openssl上内置了一个功能。 同样,文件顶部的适当shebang对于aws能否正确运行脚本也至关重要。

#decrypt.sh
#!/bin/bash -eu
#this will take the encrypted file passed to it decrypt it, and echo it
openssl aes-256-cbc -d -a - in $1 -k "password"
# yes it is just "password" its a demo script! \
# Store this in an environment variable 
# or something safer than the script.

让我们加密文件并进行准备。

$ ./encrypt.sh key.json# creates key.json.enc

接下来,我们将设置我们的aws配置文件,以从外部过程中读取证书。 这意味着我们将需要更新〜/ .aws / config并传递脚本的完整路径和加密的Blob的完整路径。

#~/.aws/config
[profile mine-encrypted]
credential_process = /Users/MYUSERNAME/decrypt.sh /Users/MYUSERNAME/key.json.enc

最后,我们将告诉当前会话使用我的新个人资料。

export AWS_PROFILE=mine-encrypted

最后,我们将验证整个过程是否有效。

aws sts get-caller-identity

我们也可以朝另一个方向发展。 假设有一个流程可以为您颁发凭据,然后将其存储在您的AWS配置文件中。 我至少希望使这些凭证的存储更好一些。 因此,这是一个可以处理此问题的漂亮脚本。 您将为Shell脚本提供一个命名的帐户配置文件,供该帐户参考。 该脚本还依赖于我们前面讨论的crypto.sh脚本。

#wrap up and harden local aws keys
function _encrypt () {
    openssl aes-256-cbc -a -salt - in ${1} -out ${1} .enc -k "password"
    filestats=( $( ls -Lon " ${1} " ) ) # to get size
    dd if =/dev/urandom of= ${1} bs= ${filestats[3]} count=1 &>/dev/null
    rm ${1}
}
function _shape (){
    id= $1
    key= $2
    session= $3
json=$(cat << DATA
{
  "Version" : 1,
  "AccessKeyId" : " $id " ,
  "SecretAccessKey" : " $key " ,
  "SessionToken" : " $session " 
}  
DATA
)
    echo " $json "
}
function _cleanup (){
    aws configure set profile. $1 .aws_access_key_id "xxxx"
    aws configure set profile. $1 .aws_secret_access_key "xxxx"
    aws configure set profile. $1 .aws_session_token "xxxx"
}
function _set_encrypted (){
    aws configure set credential_process "/Users/ $(pwd) /aws-crypt/decrypt.sh /Users/ $(pwd) /aws-crypt/ $1 .json.enc"  --profile " $1 -encrypted"
}
profile= $1
raw_aws=$(grep "\[ $profile \]" ~/.aws/credentials -A 5 | awk 'NR == 1 || /^aws/'  | awk '{print $3}' | tr '\r\n' ' ' |sed 's/ //' )
full_json=$(_shape $raw_aws )
if [ -f " $profile .json" ]; then
 rm " $profile .json"
fi
echo " $full_json " >> " $profile .json"
_encrypt " $profile .json"
_cleanup $profile
_set_encrypted $profile

当我们运行此脚本时,它将查看aws / credentials文件,并提取id / key / token,将这些值放入新文件中,对其进行加密,覆盖,然后删除临时文件i,然后“ xxxx” creds文件中的原始值。 如果有问题的配置文件不使用会话令牌,则只需删除脚本的该部分。

我希望这可以帮助某人至少保护并锁定存储在知名位置的纯文本凭据。 我们可以采取的每一个小步骤来更好地保护我们的系统,这都是迈向更好的安全态势的一小步。

HACK THE PLANET!

翻译自: https://hackernoon.com/aws-credentials-stored-safer-m5673wd3

aws 存储区别

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值