How can I get-enabled-standards from securityhub using boto3
- User
How can I get-enabled-standards from securityhub using boto3
- Assistant
To get the list of enabled standards in AWS Security Hub using Boto3, you can use the
get_enabled_standards()method of theSecurityHubclient. Here's an example code snippet:import boto3 # Create a SecurityHub client client = boto3.client('securityhub') # Get the list of enabled standards response = client.get_enabled_standards() # Iterate over each standard for standard in response['StandardsSubscriptions']: standard_arn = standard['StandardsSubscriptionArn'] standard_status = standard['StandardsStatus'] # Print the ARN and status of each enabled standard print(f"Standard ARN: {standard_arn}") print(f"Standard Status: {standard_status}") print()Make sure you have the necessary AWS credentials configured to interact with the SecurityHub service.
- User
How can I now describe-standards-controls and put each control id in a list?
- Assistant
To describe the controls for each enabled standard in AWS Security Hub and put each control ID in a list, you can use the
describe_standards_controls()method of theSecurityHubclient. Here's an example code snippet:import boto3 # Create a SecurityHub client client = boto3.client('securityhub') # Get the list of enabled standards response = client.get_enabled_standards() # Iterate over each standard for standard in response['StandardsSubscriptions']: standard_arn = standard['StandardsSubscriptionArn'] # Describe the controls for each standard response = client.describe_standards_controls(StandardsSubscriptionArns=[standard_arn]) # Create a list to store control IDs control_ids = [] # Iterate over each control for control in response['Controls']: control_id = control['ControlId'] # Append the control ID to the list control_ids.append(control_id) # Print the list of control IDs print(f"Standard ARN: {standard_arn}") print(f"Control IDs: {control_ids}") print()This code will print the standard ARN and the list of control IDs associated with each enabled standard in Security Hub.
- User
Add a filter for only enabled controls
- Assistant
To filter the controls for only the enabled ones in AWS Security Hub, you can add a condition to the code snippet provided. Here's an updated version:
import boto3 # Create a SecurityHub client client = boto3.client('securityhub') # Get the list of enabled standards response = client.get_enabled_standards() # Iterate over each standard for standard in response['StandardsSubscriptions']: standard_arn = standard['StandardsSubscriptionArn'] # Describe the controls for each standard response = client.describe_standards_controls(StandardsSubscriptionArns=[standard_arn]) # Create a list to store control IDs control_ids = [] # Iterate over each control for control in response['Controls']: # Filter only the enabled controls if control['Status']['Status'] == 'ENABLED': control_id = control['ControlId'] # Append the control ID to the list control_ids.append(control_id) # Print the list of control IDs print(f"Standard ARN: {standard_arn}") print(f"Control IDs: {control_ids}") print()