$30 off During Our Annual Pro Sale. View Details »

Step by step AWS Cloud Hacking

Step by step AWS Cloud Hacking

andresriancho

October 07, 2019
Tweet

More Decks by andresriancho

Other Decks in Programming

Transcript

  1. Step by Step AWS Cloud Hacking
    SecTor 2019
    @AndresRiancho

    View Slide

  2. The most common attack
    Using publicly available tools, widely
    known techniques and no zero days.
    This is how one of your AWS accounts
    will eventually get compromised.

    View Slide

  3. Storing AWS credentials
    Most applications running in EC2 instances need
    credentials to consume the AWS API.
    Developers can provide these in multiple ways:
    ● Hard-coded into the application
    ● Environment variables
    ● Provided via an instance profile

    View Slide

  4. EC2 instance metadata
    AWS and other cloud service providers attach a
    virtual HTTP server to each compute instance
    This server stores information about the instance
    such as host name, IP address, and instance profile
    credentials

    View Slide

  5. Permissions and least privilege
    Instance profile credentials have a set of IAM
    permissions that grant access AWS services such
    as S3, DynamoDB or EC2.
    Least privilege principle should be used when
    configuring these permissions, but...

    View Slide

  6. Metadata URLs
    The instance metadata server lives at:
    The following paths are used to gain access to the instance profile
    credentials:
    The first one returns the role name, which is required to perform
    the second HTTP request. The second HTTP response yields a
    JSON document containing the credentials.
    http://169.254.169.254/
    /latest/meta-data/iam/security-credentials/
    /latest/meta-data/iam/security-credentials/{role-name}

    View Slide

  7. Server-Side Request Forgery

    View Slide

  8. Web application vulnerable to SSRF
    from urllib.request import urlopen
    from flask import request
    @app.route('/ssrf')
    def handler():
    url = request.args.get('url')
    return urlopen(url).read()

    View Slide

  9. SSRF
    Instance metadata
    Compromised S3 buckets

    View Slide

  10. The attacker's view

    View Slide

  11. View Slide

  12. Enumerate IAM permissions
    In the AWS cloud there are two ways to enumerate permissions
    for a given credential set:
    ● Use the IAM service to get the role permissions. In most
    cases this will fail because the role itself has no permission
    for the IAM API.
    ● Call each AWS API and analyze the response: Brute-force

    View Slide

  13. Read only calls
    Enumerate Get* / List* / Describe*. Try anything else and you
    might change (break) the target AWS account and generate an
    undesired denial of service attack.
    There are thousands of API calls

    View Slide

  14. Pacu permission enumeration
    The iam__bruteforce_permissions module implements
    enumeration for only two services:
    SUPPORTED_SERVICES = [
    'ec2',
    's3'
    ]

    View Slide

  15. Permission enumeration
    enumerate-iam.py

    View Slide

  16. The attacker's view

    View Slide

  17. WebApplicationRole Policy
    {
    "Statement":[
    {
    "Effect":"Allow",
    "Action":[
    "s3:*",
    "lambda:*",
    "..."
    ],
    "Resource":"*"
    }
    ]
    }
    After permission enumeration the attacker
    knows that he's able to run all read-only API
    actions for S3 and Lambda.
    The most common scenario is that all API
    calls for S3 and Lambda are allowed.
    Even after permission enumeration there are
    many things the attacker doesn't know.

    View Slide

  18. Escalating IAM privileges
    Most attackers will try to elevate privileges
    to a principal with full access to the AWS
    account
    There are 28 well known privilege escalation
    techniques. Most of them are implemented in
    Pacu.
    {
    "Statement":[
    {
    "Effect":"Allow",
    "Action":[
    "*",
    ],
    "Resource": "*"
    }
    ]
    }

    View Slide

  19. Lambda function and privileged role
    A commonly exploited privilege escalation method:
    ● Identify an existing IAM role with high privileges
    ● Create a new Lambda and associate the role
    ● Run the lambda function
    The Lambda function will have access to the IAM role, just
    like EC2 instances have access to EC2 instance profile
    credentials.

    View Slide

  20. Privilege escalation
    Getting * on *

    View Slide

  21. View Slide

  22. Hiding the attack
    There is a small chance of being detected when a
    new lambda function is created and removed.
    Another alternative is to change the source code
    for an existing lambda function to include our
    exploit.

    View Slide

  23. Persistence via trust policies
    Trust policies limit which principals are allowed to
    iam:AssumeRole.
    In the compromised AWS account there is an existing trust
    policy in the AdminRole which allows all principals in the
    SSO AWS account to assume role.

    View Slide

  24. Persistence via trust policies

    View Slide

  25. {
    "Effect": "Allow",
    "Principal": {
    "AWS": ["arn:aws:iam::925877178748:root",
    "arn:aws:iam::320222540496:root"]
    },
    "Action": "sts:AssumeRole"
    }
    Trust policy modification
    {
    "Effect": "Allow",
    "Principal": {
    "AWS": "arn:aws:iam::925877178748:root"
    },
    "Action": "sts:AssumeRole"
    }

    View Slide

  26. Backdooring IAM Role
    Trust policies

    View Slide

  27. Backdoor notifications
    Each time the backdoor lambda function runs, the attacker
    receives the ARN for the backdoored role:

    View Slide

  28. View Slide

  29. The Private VPC
    At this point the attacker was able to gain access to
    most resources in the AWS account.
    The accounts payable EC2 instance remains out of
    reach. The VPC is completely isolated from the
    Internet.

    View Slide

  30. View Slide

  31. Pivoting into VPC networks
    vpc-vpn-pivot automates the process of creating a VPN between the attacker's
    workstation and a VPC.

    View Slide

  32. Pivoting into VPC networks
    The permissions required to create a VPN connection
    using vpc-vpn-pivot depend on the technique being
    exploited.
    When no permissions are limiting the attack the default
    technique with AWS Client VPN is used

    View Slide

  33. Connecting to private VPCs
    vpc-vpn-pivot

    View Slide

  34. View Slide

  35. Attack summary
    From SSRF to complete pwn

    View Slide

  36. View Slide

  37. Top #3 Countermeasures
    Blue team tips

    View Slide

  38. AWS credential compromise
    AWS credentials for your company accounts will be
    eventually compromised by an evil third-party.
    SSRF is the most common attack vector but others such as
    credentials hard-coded in applications, misconfigured AWS
    Cognito instances, phishing and developer workstation
    compromise also exist.
    The following countermeasures focus on reducing blast
    radius and threat detection.

    View Slide

  39. 1# Least privilege principle
    Implement least privilege principle for all IAM principals.
    Restricted permissions on the web server role
    WebApplicationRole will prevent S3 bucket access and
    privilege escalation
    Restricted permissions on MonitoringRole would have
    reduced the impact of the privilege escalation.

    View Slide

  40. 2# Use multiple AWS accounts
    Reduce the blast radius of any breach by using different
    AWS accounts to host your company workloads.
    The accounts payable application should have been
    deployed in a different AWS account within the same AWS
    Organization.

    View Slide

  41. 3# Monitoring
    Every action performed during this attack created a log
    entry in CloudTrail, but nobody watches the logs.
    Use StreamAlert and GuardDuty to identify threats in your
    AWS accounts.

    View Slide

  42. Closing thoughts

    View Slide

  43. Key takeaways
    These are the three most important things to remember:
    ● It is possible to enumerate AWS credential permissions in a fast,
    safe and in-depth manner using enumerate-iam
    ● Cloud exploitation can be automated using pacu
    ● Private VPC networks can be breached using vpc-vpn-pivot
    Follow @AndresRiancho on twitter for more interesting cloud security
    content

    View Slide

  44. Thanks!

    View Slide

  45. For hire
    Does your company or startup need these services?
    ● Cloud Security Assessment
    ● Intro to AWS Hacking training
    ● Application Penetration Test
    ● Source Code Review
    Let me know, I can help you deliver secure web applications.

    View Slide