Skip to content

Ajinkya elb prevention#125

Open
ajinkya1986 wants to merge 2 commits intodevelopmentfrom
ajinkya-elb-prevention
Open

Ajinkya elb prevention#125
ajinkya1986 wants to merge 2 commits intodevelopmentfrom
ajinkya-elb-prevention

Conversation

@ajinkya1986
Copy link
Copy Markdown
Contributor

No description provided.

@ajinkya1986 ajinkya1986 requested a review from mpurusottamc July 30, 2024 04:35
for attr in attributes['Attributes']:
if attr['Key'] == 'routing.http.desync_mitigation_mode':
if attr['Value'] != 'defensive':
print(f"ALB {lb['LoadBalancerName']} has Desync Mitigation Mode not set")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


This script will print the names of all classic ELBs in use. If there are no classic ELBs, it won't print anything.
</Accordion>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

elb_client.set_security_groups(
LoadBalancerArn=elb_arn,
SecurityGroups=secure_group_ids
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

if 'Certificates' not in listener or not any(cert['IsDefault'] for cert in listener['Certificates']):
print(f"Load balancer {lb['LoadBalancerName']} has a listener without an ACM certificate.")
```

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


This script will print the names of all load balancers that do not have any security groups. Please replace `'YOUR_ACCESS_KEY'` and `'YOUR_SECRET_KEY'` with your actual AWS access key and secret key.
</Accordion>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


# Replace 'your-load-balancer-name' with the name of your CLB
load_balancer_name = 'your-load-balancer-name'
enable_desync_mitigation_mode(load_balancer_name)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

4. Analyze the output:
If the Desync Mitigation Mode of a load balancer is not 'defensive' or 'strictest', it means that the load balancer is misconfigured. You should see an output similar to this for each load balancer:
```
Load Balancer: my-load-balancer, Desync Mitigation Mode: defensive
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

# Initialize a session using Amazon EC2
session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


# Example usage
elb_name = 'your-elb-name'
target = 'HTTP:80/ping' # Example target
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

# Initialize a session using Amazon EC2
session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

4. The above script will print out the names of all load balancers that are not using the latest AWS security policy for SSL negotiations. If no such load balancers are found, the script will not output anything. This way, you can easily detect any misconfigurations in your AWS Elastic Load Balancers.

Remember to replace `'YOUR_ACCESS_KEY'`, `'YOUR_SECRET_KEY'`, and `'YOUR_REGION'` with your actual AWS access key, secret key, and region, respectively.
</Accordion>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

unused_elbs = get_unused_elbs(elb_client)
print("Unused ELBs:", unused_elbs)
```

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

if not target_health_response['TargetHealthDescriptions']:
unused_elbs.append(elb['LoadBalancerName'])
```

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

# Initialize a session using Amazon EC2
session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

'Timeout': 300 # Set the timeout as per your requirement
}
}
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

print(f"Connection Draining is not enabled for {lb['LoadBalancerName']}")
```

This script will print the names of all load balancers that do not have connection draining enabled.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

# Initialize a session using Amazon EC2
session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

'Protocol': 'HTTP',
'LoadBalancerPort': 80,
'InstanceProtocol': 'HTTP',
'InstancePort': 80,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

# Initialize a session using Amazon EC2
session = boto3.Session(
aws_access_key_id='YOUR_AWS_ACCESS_KEY',
aws_secret_access_key='YOUR_AWS_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

for cert in listener['Certificates']:
if 'IsDefault' in cert and cert['IsDefault']:
print(f"Load balancer {lb['LoadBalancerName']} has default ACM certificate: {cert['CertificateArn']}")
else:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

import boto3
session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

)
else:
print(f"Listener {listener['ListenerArn']} is already using TLS.")
```
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

```python
import boto3

client = boto3.client('elbv2')
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

Then, configure your AWS credentials. You can do this by setting the following environment variables:
```
AWS_ACCESS_KEY_ID = 'your_access_key'
AWS_SECRET_ACCESS_KEY = 'your_secret_key'
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


def main():
load_balancers = get_load_balancers()
for lb in load_balancers:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

# Initialize a session using Amazon EC2
session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

}
)
print(f"Deletion protection enabled for load balancer: {lb_name}")
```
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


</AccordionGroup>
</Tab>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

# Initialize a session using Amazon EC2
session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

import boto3
session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

Check the listeners for each load balancer and update them to use secure protocols (HTTPS or SSL) if they are not already configured.
```python
# Iterate through each load balancer
for lb in load_balancers['LoadBalancerDescriptions']:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

# Create a session using your AWS credentials
session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

response = elb_client.configure_health_check(
LoadBalancerName=elb_name,
HealthCheck=health_check_config
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


This script will print the names of all the load balancers that are misconfigured.
</Accordion>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

# Initialize a session using Amazon EC2
session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


</AccordionGroup>
</Tab>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='your_access_key',
aws_secret_access_key='your_secret_key',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

print(f"Load balancer {lb['LoadBalancerName']} is using outdated SSL policy {listener['SslPolicy']}")
```

This script will print out the names of all load balancers that are using an SSL policy other than 'ELBSecurityPolicy-2016-08', which is the latest policy. Please replace 'YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY', and 'YOUR_REGION' with your actual AWS credentials and region.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


# Check if the policy specifies a WebACLId
if 'Policy' in response and 'SecurityServicePolicyData' in response['Policy'] and 'ManagedServiceData' in response['Policy']['SecurityServicePolicyData']:
managed_service_data = json.loads(response['Policy']['SecurityServicePolicyData']['ManagedServiceData'])
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


```python
AWS_ACCESS_KEY_ID = 'your_access_key'
AWS_SECRET_ACCESS_KEY = 'your_secret_key'
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

return elb_instances

elb_instances = get_elb_instances(elb_client)
```
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

print(f"Load Balancer Name: {load_balancer['LoadBalancerName']}")
print(f"Number of Instances: {num_instances}")
if num_instances < 2:
print("Misconfiguration Detected: Less than 2 instances configured for this ELB.")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

# Initialize a session using Amazon EC2
session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details


session = boto3.Session(
aws_access_key_id='your_access_key',
aws_secret_access_key='your_secret_key',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret: Secret Keyword

Click here for more details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants