Microservices (200)

Description

Bร i nร y chแป‰ cho ta ARN Role, ฤ‘แปƒ tรฌm hiแปƒu ARN Role lร  gรฌ vร  cรกch dรนng nรณ nhฦฐ thแบฟ nร o, cรกc bแบกn cรณ thแปƒ tham khแบฃo:

ฤแปƒ sแปญ dแปฅng ฤ‘ฦฐแปฃc ARN Role mรฌnh sแบฝ assume role vแป›i mแป™t credentials bแบฅt kแปณ, mรฌnh sแบฝ sแปญ dแปฅng credentials ฤ‘รฃ cรณ tแปซ bร i trฦฐแป›c

Ta sแบฝ cรณ file configure nhฦฐ sau

source_profile trแป ฤ‘แบฟn cloud1 credentials

Tแปซ config nร y mรฌnh sแบฝ assume role cho cloud1 cรณ thแปƒ truy cแบญp ฤ‘ฦฐแปฃc resources mร  role cho phรฉp vแป›i cmd sau:

aws sts assume-role --role-arn arn:aws:iam::543303393859:role/TetCtf2Stack-EcsTaskRole8DFA0181-qubavXABtWiL --role-session-name test --profile cloud2

Khi cรณ ฤ‘ฦฐแปฃc key rแป“i thรฌ configure nhฦฐ bร i 1 lร  ฤ‘ฦฐแปฃc

ฤแบงu tiรชn mรฌnh check policy ฤ‘แปƒ xem mรฌnh cรณ thแปƒ lร m ฤ‘ฦฐแปฃc gรฌ vแป›i role ฤ‘แป cho

โ””โ”€$ aws iam get-role-policy --role-name TetCtf2Stack-EcsTaskRole8DFA0181-qubavXABtWiL --policy-name EcsTaskRoleDefaultPolicy50882C77 --profile test
{
    "RoleName": "TetCtf2Stack-EcsTaskRole8DFA0181-qubavXABtWiL",
    "PolicyName": "EcsTaskRoleDefaultPolicy50882C77",
    "PolicyDocument": {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Action": "ecs:RunTask",
                "Resource": "arn:aws:ecs:eu-west-2:543303393859:task-definition/TetCtf2StackCtfTaskDefB40F186A:3",
                "Effect": "Allow"
            },
            {
                "Action": [
                    "iam:ListRolePolicies",
                    "iam:GetRolePolicy",
                    "ecs:ListClusters",
                    "ec2:DescribeSecurityGroups",
                    "ec2:DescribeSubnets"
                ],
                "Resource": "*",
                "Effect": "Allow"
            },
            {
                "Effect": "Allow",
                "Action": "iam:PassRole",
                "Resource": [
                    "arn:aws:iam::543303393859:role/TetCtf2Stack-EcsExecutionRoleFD93B7A2-O8bY2QagMK25",
                    "arn:aws:iam::543303393859:role/TetCtf2Stack-CtfTaskDefTaskRoleD17F896A-vJxGKfIFhChH"
                ]
            },
            {
                "Sid": "Statement1",
                "Effect": "Allow",
                "Action": [
                    "logs:GetLogEvents",
                    "logs:DescribeLogStreams",
                    "logs:DescribeLogGroups"
                ],
                "Resource": [
                    "arn:aws:logs:eu-west-2:543303393859:*"
                ]
            }
        ]
    }
}

Nhรฌn แปŸ trรชn thรฌ ta biแบฟt ฤ‘ฦฐแปฃc:

  • Ta cรณ thแปƒ thแปฑc thi RunTask vแป›i ECS

  • Ta cรณ thแปƒ enum IAM vร  EC2

  • Ta cรณ thแปƒ thแปฑc hiแป‡n PassRole. Passrole lร  tรญnh nฤƒng cho phรฉp cแบฅp quyแปn cho user truy cแบญp ฤ‘แบฟn role mร  cแปฅ thแปƒ แปŸ ฤ‘รขy lร  2 role role/TetCtf2Stack-EcsExecutionRoleFD93B7A2-O8bY2QagMK25 vร  role/TetCtf2Stack-CtfTaskDefTaskRoleD17F896A-vJxGKfIFhChH

  • Cuแป‘i cรนng ta cรณ thแปƒ check logs (lรบc nร y mรฌnh khรดng rรต log nร y lร  log gรฌ)

Mรฌnh sแบฝ ฤ‘i vร o tแปซng chแปฉc nฤƒng tแปซ trรชn xuแป‘ng.

ฤแบงu tiรชn ta cรณ thแปƒ thแปฑc thi ecs:Runtask, cรกc bแบกn cรณ thแปƒ tรฌm hiแปƒu thรชm vแป ecs service trong aws tแบกi ฤ‘รขy

Nรณi cho dแป… hiแปƒu thรฌ ecs lร  service cho phรฉp quแบฃn lรฝ cรกc docker container trรชn amazon cloud, mแป—i container sแบฝ cรณ nhiแป‡m vแปฅ riรชng, ta gแปi lร  task, nhiแปu container sแบฝ ฤ‘ฦฐแปฃc sแปญ dแปฅng kแบฟt hแปฃp vแป›i nhau ฤ‘แปƒ cแบฅu thร nh service (ฤ‘รณ cลฉng chรญnh lร  lรฝ do vรฌ sao bร i nร y tรชn lร  Microservices)

ร tฦฐแปŸng ฤ‘แบงu tiรชn cแปงa mรฌnh lร  gแปi ฤ‘ฦฐแปฃc task vร  tรฌm hiแปƒu hร nh vi cแปงa nรณ ฤ‘แปƒ lแปฃi dแปฅng. Vแป›i chall nร y ฤ‘แปƒ gแปi ฤ‘ฦฐแปฃc task thรฌ ta cแบงn

  • --task-definition : ฤ‘รฃ biแบฟt

  • --cluster : cรณ thแปƒ lแบฅy bแบฑng ecs:ListClusters (cluster cรณ thแปƒ hiแปƒu ฤ‘ฦกn giแบฃn lร  mแป™t goup cแปงa cรกc containers)

  • --network-configuration : ta cลฉng cรณ ฤ‘แปง thรดng tin cho options nร y

Lแบฅy Cluster name

Format cแปงa --network-configuration cรณ dแบกng nhฦฐ sau:

Ta dแป… dร ng lแบฅy ฤ‘ฦฐแปฃc qua ec2:DescribeSecurityGroups vร  ec2:DescribeSubnets

GroupId

โ””โ”€$ aws ec2 describe-security-groups --profile test
{
    "SecurityGroups": [
        {
            "Description": "Security Group for CTF ECS tasks",
            "GroupName": "TetCtf2Stack-CtfSecurityGroupA7633774-1DAGZMZKB7EY4",
            "IpPermissions": [],
            "OwnerId": "543303393859",
            "GroupId": "sg-0f6583e3532e99a62",
            "IpPermissionsEgress": [
                {
                    "FromPort": 252,
                    "IpProtocol": "icmp",
                    "IpRanges": [
                        {
                            "CidrIp": "255.255.255.255/32",
                            "Description": "Disallow all traffic"
                        }
                    ],
                    "Ipv6Ranges": [],
                    "PrefixListIds": [],
                    "ToPort": 86,
                    "UserIdGroupPairs": []
                }
            ],
            "Tags": [
                {
                    "Key": "aws:cloudformation:stack-name",
                    "Value": "TetCtf2Stack"
                },
                {
                    "Key": "aws:cloudformation:logical-id",
                    "Value": "CtfSecurityGroupA7633774"
                },
                {
                    "Key": "aws:cloudformation:stack-id",
                    "Value": "arn:aws:cloudformation:eu-west-2:543303393859:stack/TetCtf2Stack/54b3d720-bc03-11ee-9235-06cbbf25eaf7"
                }
            ],
            "VpcId": "vpc-07e8cd02a7c992f43"
        },
        {
            "Description": "default VPC security group",
            "GroupName": "default",
            "IpPermissions": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [],
                    "Ipv6Ranges": [],
                    "PrefixListIds": [],
                    "UserIdGroupPairs": [
                        {
                            "GroupId": "sg-2a62a941",
                            "UserId": "543303393859"
                        }
                    ]
                }
            ],
            "OwnerId": "543303393859",
            "GroupId": "sg-2a62a941",
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "Ipv6Ranges": [],
                    "PrefixListIds": [],
                    "UserIdGroupPairs": []
                }
            ],
            "VpcId": "vpc-5744993f"
        },
        {
            "Description": "default VPC security group",
            "GroupName": "default",
            "IpPermissions": [],
            "OwnerId": "543303393859",
            "GroupId": "sg-0e0be2c862c2b3241",
            "IpPermissionsEgress": [],
            "VpcId": "vpc-07e8cd02a7c992f43"
        },
        {
            "Description": "GET FLAG",
            "GroupName": "TetCTF-GETFLAG",
            "IpPermissions": [],
            "OwnerId": "543303393859",
            "GroupId": "sg-0636ad23bae6f21e7",
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "Ipv6Ranges": [],
                    "PrefixListIds": [],
                    "UserIdGroupPairs": []
                }
            ],
            "VpcId": "vpc-07e8cd02a7c992f43"
        }
    ]
}

SubnetId

Thแปฑc thi ecs:RunTask

Tuy nhiรชn vแบซn chฦฐa ฤ‘ฦฐแปฃc vรฌ cรฒn thiแบฟu gรฌ ฤ‘รณ, sau mแป™t hแป“i search gg thรฌ mรฌnh biแบฟt cรณ 3 mode ฤ‘แปƒ tแบกo instance cho container lร  EC2, FARGATE vร  EXTERNAL. ฤแป‘i vแป›i bร i nร y thรฌ chแป‰ cรณ FARGATE lร  cรณ thแปƒ run task

โ””โ”€$ aws ecs run-task --task-definition TetCtf2StackCtfTaskDefB40F186A:3 --cluster CtfEcsCluster --network-configuration "awsvpcConfiguration={subnets=[subnet-05dc4f12caf437c48],securityGroups=[sg-0636ad23bae6f21e7]}" --launch-type FARGATE --profile test
{
    "tasks": [
        {
            "attachments": [
                {
                    "id": "30cb9cbf-cfa7-4ac6-be42-5dcf94e0bb6a",
                    "type": "ElasticNetworkInterface",
                    "status": "PRECREATED",
                    "details": [
                        {
                            "name": "subnetId",
                            "value": "subnet-05dc4f12caf437c48"
                        }
                    ]
                }
            ],
            "attributes": [
                {
                    "name": "ecs.cpu-architecture",
                    "value": "x86_64"
                }
            ],
            "availabilityZone": "eu-west-2a",
            "clusterArn": "arn:aws:ecs:eu-west-2:543303393859:cluster/CtfEcsCluster",
            "containers": [
                {
                    "containerArn": "arn:aws:ecs:eu-west-2:543303393859:container/CtfEcsCluster/cf2c5c1b8c0041b699078d1aeb485503/2e74da1d-75b0-4614-8804-0d7681daca30",
                    "taskArn": "arn:aws:ecs:eu-west-2:543303393859:task/CtfEcsCluster/cf2c5c1b8c0041b699078d1aeb485503",
                    "name": "CtfContainer",
                    "image": "543303393859.dkr.ecr.eu-west-2.amazonaws.com/tet-ctf:flag",
                    "lastStatus": "PENDING",
                    "networkInterfaces": [],
                    "cpu": "0"
                }
            ],
            "cpu": "1024",
            "createdAt": 1706716630.41,
            "desiredStatus": "RUNNING",
            "enableExecuteCommand": false,
            "group": "family:TetCtf2StackCtfTaskDefB40F186A",
            "lastStatus": "PROVISIONING",
            "launchType": "FARGATE",
            "memory": "2048",
            "overrides": {
                "containerOverrides": [
                    {
                        "name": "CtfContainer"
                    }
                ],
                "inferenceAcceleratorOverrides": []
            },
            "platformVersion": "1.4.0",
            "platformFamily": "Linux",
            "tags": [],
            "taskArn": "arn:aws:ecs:eu-west-2:543303393859:task/CtfEcsCluster/cf2c5c1b8c0041b699078d1aeb485503",
            "taskDefinitionArn": "arn:aws:ecs:eu-west-2:543303393859:task-definition/TetCtf2StackCtfTaskDefB40F186A:3",
            "version": 1,
            "ephemeralStorage": {
                "sizeInGiB": 20
            }
        }
    ],
    "failures": []
}

Sau khi gแปi ฤ‘ฦฐแปฃc task thรฌ mรฌnh bรญ hoร n toร n khรดng biแบฟt lร m gรฌ tiแบฟp theo

Nhแป› lแบกi ฤ‘แป cรฒn cho phรฉp mรฌnh PassRole vร  xem logs, nรชn mรฌnh check lแบกi cแบฃ 2, thรฌ kแบฟt quแบฃ tแปซ logs cรณ vแบป khรก khแบฃ quan

Mรฌnh nhแบญn thแบฅy sแบฝ cรณ logs ฤ‘แป‘i vแป›i /ecs/tet-ctf:* , hay chรญnh lร  task mร  mรฌnh thแปฑc thi, thแปญ thแปฑc thi task vร  check logs ta sแบฝ ฤ‘ฦฐแปฃc (nแบฟu ta gแปi task khรดng phแบฃi bแบฑng GroupID cแปงa TetCTF-GETFLAGthรฌ ta sแบฝ khรดng thแบฅy logs)

ฤแบฟn ฤ‘รขy mรฌnh tiแบฟp tแปฅc bรญ vร  khรดng biแบฟt phแบฃi lร m gรฌ vแป›i ฤ‘แป‘ng logs nร y

Quay trแปŸ lแบกi vแป›i hร nh vi run-task, mรฌnh thแบฏc mแบฏc khรดng biแบฟt liแป‡u cรณ mแป™t tรญnh nฤƒng nร o ฤ‘รณ cรณ thแปƒ giรบp thแปฑc thi command hay khรดng. Search cแบฃ ngร y trแปi khรดng ra kแบฟt quแบฃ, nhฦฐng khi hแปi ChatGPT thรฌ mรฌnh cรณ cรขu trแบฃ lแปi ๐Ÿข๐Ÿข๐Ÿข

Options --overide sแบฝ cho phรฉp ta ghi ฤ‘รจ command sแบฝ thแปฑc thi kรจm vแป›i quรก trรฌnh khแปŸi tแบกi cแปงa container. Tuy nhiรชn cmd thแปฑc thi khรดng trแบฃ vแป output

โ””โ”€$ aws ecs run-task --task-definition TetCtf2StackCtfTaskDefB40F186A:3 --cluster CtfEcsCluster --network-configuration "awsvpcConfiguration={subnets=[subnet-05dc4f12caf437c48],securityGroups=[sg-0636ad23bae6f21e7]}" --launch-type FARGATE --profile test --overrides '{"containerOverrides": [{"name": "CtfContainer", "command": ["id"]}]}'
{
    "tasks": [
        {
            "attachments": [
                {
                    "id": "d39de2d8-14a4-42eb-934b-bef7704c3dc5",
                    "type": "ElasticNetworkInterface",
                    "status": "PRECREATED",
                    "details": [
                        {
                            "name": "subnetId",
                            "value": "subnet-05dc4f12caf437c48"
                        }
                    ]
                }
            ],
            "attributes": [
                {
                    "name": "ecs.cpu-architecture",
                    "value": "x86_64"
                }
            ],
            "availabilityZone": "eu-west-2a",
            "clusterArn": "arn:aws:ecs:eu-west-2:543303393859:cluster/CtfEcsCluster",
            "containers": [
                {
                    "containerArn": "arn:aws:ecs:eu-west-2:543303393859:container/CtfEcsCluster/eecf1f8af5c24f34be5bc4507c837909/7bc76a3c-9f5b-4e1a-a8e8-bcc9dd1a3c5e",
                    "taskArn": "arn:aws:ecs:eu-west-2:543303393859:task/CtfEcsCluster/eecf1f8af5c24f34be5bc4507c837909",
                    "name": "CtfContainer",
                    "image": "543303393859.dkr.ecr.eu-west-2.amazonaws.com/tet-ctf:flag",
                    "lastStatus": "PENDING",
                    "networkInterfaces": [],
                    "cpu": "0"
                }
            ],
            "cpu": "1024",
            "createdAt": 1706716669.563,
            "desiredStatus": "RUNNING",
            "enableExecuteCommand": false,
            "group": "family:TetCtf2StackCtfTaskDefB40F186A",
            "lastStatus": "PROVISIONING",
            "launchType": "FARGATE",
            "memory": "2048",
            "overrides": {
                "containerOverrides": [
                    {
                        "name": "CtfContainer",
                        "command": [
                            "id"
                        ]
                    }
                ],
                "inferenceAcceleratorOverrides": []
            },
            "platformVersion": "1.4.0",
            "platformFamily": "Linux",
            "tags": [],
            "taskArn": "arn:aws:ecs:eu-west-2:543303393859:task/CtfEcsCluster/eecf1f8af5c24f34be5bc4507c837909",
            "taskDefinitionArn": "arn:aws:ecs:eu-west-2:543303393859:task-definition/TetCtf2StackCtfTaskDefB40F186A:3",
            "version": 1,
            "ephemeralStorage": {
                "sizeInGiB": 20
            }
        }
    ],
    "failures": []
}

Mรฌnh check thแปญ OutBound thรฌ cลฉng khรดng cรณ kแบฟt quแบฃ (quรก dแป… ฤ‘oรกn)

Lรบc nร y thรฌ logs phรกt huy cรดng dแปฅng, mรฌnh thแปญ check logs vร  cรณ ฤ‘ฦฐแปฃc output

Viแป‡c bรขy giแป chแป‰ lร  tรฌm flag thรดi

Last updated