AWS Security Blog
Continuous compliance monitoring with Chef InSpec and AWS Security Hub
In this post, I will show you how to run a Chef InSpec scan with AWS Systems Manager and Systems Manager Run Command across your managed instances. InSpec is an open-source runtime framework that lets you create human-readable profiles to define security, compliance, and policy requirements and then test your Amazon Elastic Compute Cloud (Amazon EC2) instances against those profiles. InSpec profiles can also be used to make sure certain network ports aren’t reachable, to verify that certain packages are not installed, and/or to confirm that certain processes are running on your instances.
InSpec is integrated within AWS Systems Manager, an AWS service that you can use to view and control your infrastructure on AWS. InSpec compliance scans are run by using an AWS Systems Manager document (SSM document), which installs InSpec on your servers and removes InSpec after scans are completed.
In this post, you will create the supporting infrastructure to collect non-compliant findings from AWS Systems Manager Configuration Compliance and send them to AWS Security Hub via a Lambda function. You will also explore methods to correlate finding information in Security Hub for non-compliant resources.
Note: AWS Systems Manager (Systems Manager) was formerly known as “Amazon Simple Systems Manager (SSM)” and “Amazon EC2 Systems Manager (SSM).” The original abbreviated name of the service, “SSM,” is still reflected in various AWS resources. For more information, see Systems Manager Service Name History.
Solution overview
The following diagram shows the flow of events in the solution I describe in this post.
- Invoke an AWS-RunInSpecChecks document on-demand by using Run Command against your target instances (State Manager is another option for scheduling InSpec scans, but is not covered in this post).
- Systems Manager downloads the InSpec Ruby files from Amazon Simple Storage Service (Amazon S3), installs InSpec on your server, runs the scan, and removes InSpec when complete.
- AWS Systems Manager pushes scan results to the Compliance API and presents the information in the Systems Manager Compliance console, to include severity and compliance state.
- A CloudWatch Event is emitted for Compliance state changes.
- A CloudWatch Event Rule listens for these state changes and when detected, invokes a Lambda function.
- Lambda calls the Compliance APIs for additional data about which InSpec check failed.
- Lambda calls the EC2 APIs to further enrich the data about the non-compliant instance.
- Lambda maps these details to the AWS Security Finding Format and sends them to Security Hub.
To support the steps above, you will deploy a CloudFormation template that creates a CloudWatch Event Rule and a placeholder Lambda function. You will then create an InSpec profile, upload it to Amazon S3, and use Run Command to invoke an InSpec compliance scan.
When the scan completes, you can then search for the findings in Security Hub. You can create saved searches with insights in AWS Security Hub, and use different filtering to correlate InSpec compliance failures with other information from Amazon Inspector and Amazon GuardDuty.
Prerequisites
- Security Hub enabled in the region you deploy this solution
- One Linux-based Systems Manager managed instance. See Getting Started with AWS Systems Manager to learn how to setup a managed instance.
- S3 Bucket to upload code samples to
Getting started
Before creating your CloudFormation stack, you’ll upload your InSpec profile and Lambda function package to an S3 bucket.
To upload the Lambda package:
- Download the Lambda function ZIP file from GitHub.
- Save the file to your workstation. In your S3 bucket, choose Upload to upload the InSpecToSecurityHub.zip file.
The next step is to create and upload an InSpec profile to Amazon S3.
To create and upload an InSpec profile:
- From your workstation, create a file named Inspec_SSH.rb and paste in the code that follows:
- Save your file, and in your S3 bucket, choose Upload to upload the Inspec_SSH.rb file.
Note: The InSpec profile in the example code above ensures that SSH (Port 22) is listening on your instance and that SSH access is not publicly available, as noted by {should_not include ‘0.0.0.0’}. For other InSpec profiles, see the DevSec chef-os-hardening project on GitHub for profiles to help you secure your instances and servers, or you can learn more about Compliance Automation with InSpec on the Chef website.
You will now deploy the CloudFormation template to finish setting up the solution.
To deploy the CloudFormation template:
- Download the CloudFormation template from GitHub and create a CloudFormation stack.
Note: For more information about how to create a CloudFormation stack, see Getting Started with AWS CloudFormation in the AWS CloudFormation User Guide.
- Under Parameters, enter the name of the bucket you uploaded the package to, as shown in Figure 2, and finish creating your stack
After your stack is successfully created, you will run an InSpec scan.
Run an InSpec scan
Now that you have your Lambda function and InSpec profile ready, you will run your InSpec scan against one or more managed instances with Run Command.
- Navigate to the AWS Systems Manager Console and in the navigation pane, choose Run Command.
- In the search box, enter AWS-RunInspecChecks. In the search results, select AWS-RunInspecChecks.
- For Source Type, select S3. The Source Info format is:
Change BUCKET_NAME, PATH, and the InSpec profile name as appropriate, and enter it in the Source Info box, as shown in Figure 3.
- Under Targets, select Choose instances manually and select one or more instances, as shown in Figure 3.
- Scroll down to Output options, and select S3, CloudWatch Logs, or both, then select Run.
- In the Run Command console for the specific Command invocation you just sent, you will see your instances and their status, as shown in Figure 4. Refresh the page after a few minutes to see if the invocation was successful.
- If you see anything other than Success, you can find information about different statuses in Understanding Command Statuses. You can also see Troubleshooting Systems Manager Run Command.
- In the AWS Systems Manager Console, navigate to the Compliance menu to see your full Compliance resources summary, as shown in Figure 5. You may also see additional compliance types and statuses. Only failed Custom:InSpec compliance types will make their way to Security Hub.
Analyze with Security Hub
After Lambda has sent your non-compliant InSpec results to Security Hub, you can create saved searches by using Security Hub insights to do basic correlation.
- Navigate to the Security Hub console and in the navigation pane, choose Findings.
- To find InSpec-related findings, select the search bar, and choose Product fields.
- For the Key field, enter Provider Name and for the Value field enter AWS Systems Manager Compliance, then choose Apply, as shown in Figure 6.
This will give you your InSpec-related findings, as shown in Figure 7, but there is not enough data to correlate across to other findings from GuardDuty or Amazon Inspector.
- Navigate the Insights menu on the navigation pane and choose Create insight on the top-right.
- Select the search bar, enter Resource type, enter AwsEc2Instance and select Apply, as shown in Figure 8. This will group all findings related to EC2 instances together.
- Select the search bar again, select Group by, scroll down to select Severity label and select Apply. We now can see our total findings by severity across all EC2 instances, as shown in Figure 9.
- On the top right select Create insight, enter a name such as EC2 Instances by Severity and choose Create insight to save your insight.
- Navigate back to the main Insights menu and select your new Insight for more detailed graphics to appear on the right-hand side, as shown in Figure 10.
You can hover over the values to get the exact count and select them to filter down further on details such as the ARN of the resource, the name of the product, or what account the finding originated from. This view can be used by security analysts who are looking to do fast correlation of findings for a resource or to take action on high severity findings first.
Conclusion
In this post, I showed you how to run InSpec scans to monitor the compliance of your instances against your policy requirements, as defined by InSpec profiles. InSpec can help identify when certain points are improperly configured or publicly accessible. By using Systems Manager, you can continuously monitor the compliance against these profiles with State Manager, and run these checks on demand by using Run Command. Systems Manager allows you to rapidly scale across your managed instances, and enriches instance data through the SSM Agent.
You also learned how to use Security Hub to perform correlation within the findings menu, so you can quickly search based on resource type, or the VPC and subnet that the EC2 instances are in. When you have an instance that fails compliance against an InSpec profile, you can look for any threat-related findings from GuardDuty, or vulnerability information from Amazon Inspector or partner integrations.
To avoid incurring additional charges from the resources created in this blog, delete the CloudFormation stack you deployed. For more information on Security Hub pricing, see the AWS Security Hub Pricing page.
If you have feedback about this blog post, submit comments in the Comments section below. If you have questions about this blog post, start a new thread on the Security Hub forum or contact AWS Support.
Want more AWS Security how-to content, news, and feature announcements? Follow us on Twitter.