<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Zohaib Hassan on Medium]]></title>
        <description><![CDATA[Stories by Zohaib Hassan on Medium]]></description>
        <link>https://medium.com/@zohaib.hassan78669?source=rss-91af9e1d1c42------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ZppApz7pNmd8OL-P</url>
            <title>Stories by Zohaib Hassan on Medium</title>
            <link>https://medium.com/@zohaib.hassan78669?source=rss-91af9e1d1c42------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 15:58:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zohaib.hassan78669/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[CloudCustodian Integration With AWS SecurityHub using Periodic Mode]]></title>
            <link>https://medium.com/@zohaib.hassan78669/cloudcustodian-integration-with-aws-securityhub-using-periodic-mode-2d1101ba77b9?source=rss-91af9e1d1c42------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d1101ba77b9</guid>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[aws-security-hub]]></category>
            <category><![CDATA[cloudwatch]]></category>
            <category><![CDATA[cloud-custodian]]></category>
            <dc:creator><![CDATA[Zohaib Hassan]]></dc:creator>
            <pubDate>Mon, 22 Jun 2020 15:45:53 GMT</pubDate>
            <atom:updated>2020-06-22T15:45:53.988Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yPXnrQIi0AClDBYA5RZ8eQ.png" /></figure><p>Here again I am writing a blog related to CloudCustodian. In my previous <a href="https://link.medium.com/fjDn85o1n7">blog </a>I have explained how to write a policy using CloudTrail mode with CloudCustodian. I will explain the way how to integrate Custodian with AWS SecurityHub and how to run a policy with periodic mode on a schedule time.</p><p>One of the issue with CloudCustodian is lake of native dashboard for visualization . Although custodian have CLI based reporting generated while running from CLI but still devops and security guys need some visualization insights.</p><blockquote>In 2018 AWS has announced the integration of CloudCustodian with AWS SecurityHub now you can integrate Custodian with SecurityHub and visualize the insights and findings.</blockquote><p>I will explain how we can integrate CloudCustodian with security hub using a simple policy with periodic mode.</p><h4>Prerequisite:</h4><p>Make sure SecurityHub is enabled in your account . if not first enable it</p><p>Now you will land on SecurityHUb default page and you will see different links like insights, summary, integrations and Security Standard as you can see in image 1 . Each page will explain it self also you can read SecurtiyHub documentation for this ..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*YD1YxGB_8w-KbWcPPjKHXg.png" /><figcaption>Image 1</figcaption></figure><p>Click integrations page you will see there will be AWS and third party services available from which AWS SecurtiyHub accepting findings . As you can see in image 2 there are a couple of AWS services listed .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5EC82C1WkWuDfgka54OIJw.png" /><figcaption>Image 2</figcaption></figure><p>On integrations page search bar search Custodian it will shoe CloudCustodian services which is currently not enable as in image 3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*eBYth0N2q_YhXMr9Ar24JQ.png" /><figcaption>Image 3</figcaption></figure><p>So you can read a description about the services and now Click on Accept Finding button it will open a pop with configuration click on Accept Findings</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/747/1*xpYoV-vpmKHc4EG1nmq98w.png" /><figcaption>Image 4</figcaption></figure><p>Now after clicking Accept Findings it shows the previous page but now findings will be enabled as shown in image 5</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*P7ek5_2A3OP_QUcqIGZLSw.png" /><figcaption>Image 5</figcaption></figure><p>Now Security hub will accept findings from custodian as well and you can see the insights and findings on summary and insights pages related to Custodian findings.</p><h4>Policy</h4><p>Now our next task is to create a policy which we will run and it should send findings too AWS SecurityHub and we can see it in the dashboard .</p><p>Here is a description of policy:</p><ul><li><strong>Line 2</strong>: Provides a unique name for the policy</li><li><strong>Line 3</strong>: Identifies the AWS Resource Types as EBS volume</li><li><strong>Line 4</strong>: ‘periodic’ mode is set here, which will create a scheduled CloudWatch Event Rule that will trigger a Lambda function every 10 minutes</li><li><strong>Line 6</strong>: Role that will be used for give necessary permissions to lambda to execute policy/ send findings to SecurityHUb</li><li><strong>Line 10</strong>: The filters will find EBS volumes with the Encrypted value false</li><li><strong>Line 15</strong>: The actions which is use in the ‘post-finding’ action, will publish results to AWS SecurityHub</li></ul><pre>policies:<br>  - name: ec2-unencrypted-ebs-periodic<br>    resource: ebs<br>    mode:<br>      type: periodic<br>      role: arn:aws:iam::{account-id}:role/CloudCustodian-QucickStart<br>      schedule: &quot;rate(10 minutes)&quot;<br>      packages: [boto3, botocore, urllib3]<br>    filters:<br>      - type: value<br>        key: Encrypted<br>        op: eq<br>        value: false<br>    actions:<br>      - type: post-finding<br>        severity_normalized: 30<br>        compliance_status: WARNING<br>        types: <br>          - &quot;Software and Configuration Checks&quot;</pre><p>The role CloudCustodian-QucickStart used in the code should have this policy attached to publish findings to SecurityHub</p><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Action&quot;: &quot;securityhub:*&quot;,<br>            &quot;Resource&quot;: &quot;*&quot;<br>        },<br>        {<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Action&quot;: &quot;iam:CreateServiceLinkedRole&quot;,<br>            &quot;Resource&quot;: &quot;*&quot;,<br>            &quot;Condition&quot;: {<br>                &quot;StringLike&quot;: {<br>                    &quot;iam:AWSServiceName&quot;: &quot;securityhub.amazonaws.com&quot;<br>                }<br>            }<br>        }<br>    ]<br>}</pre><p>To execute the policy we will run this custodian command</p><pre>custodian run -s output encrypted-ebs.yml</pre><p>So now after successful execution if we look in to our account we will see a cloud watch event rule and a lambda like in images 6 and 8</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*RA-DMVfIQzmf6E4lmolmow.png" /><figcaption>Image 7 CloudWatch Event rule</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/516/1*9l_R1Yl3edU0fXLlFjC30A.png" /><figcaption>Image 8 Lambda Function</figcaption></figure><p>Now to test this I have create an ebs volume with out encryption and in my AWS Account and after 10 minutes found these findings in SecurityHub dashboard, As shown in Image 9.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7dbxg8mhEQ2YFKtJXR3y4Q.png" /><figcaption>Image 9 Custodian Findings</figcaption></figure><p>As it was a small demonstration of but is shows how CloudCustodian can be used to identify security and governance issues for organizations.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d1101ba77b9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CloudCustodian Policy with CloudTrail Mode]]></title>
            <link>https://medium.com/@zohaib.hassan78669/cloudcustodian-policy-with-cloudtrail-mode-308e6efdef1b?source=rss-91af9e1d1c42------2</link>
            <guid isPermaLink="false">https://medium.com/p/308e6efdef1b</guid>
            <category><![CDATA[cloud-custodian]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[cloudwatch-events]]></category>
            <category><![CDATA[cloudtrail]]></category>
            <category><![CDATA[cloudwatch]]></category>
            <dc:creator><![CDATA[Zohaib Hassan]]></dc:creator>
            <pubDate>Wed, 17 Jun 2020 07:47:09 GMT</pubDate>
            <atom:updated>2020-06-22T16:09:22.239Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*2gqaujFln0uVFhG92zJ14Q.jpeg" /></figure><p>Cloud Custodian is very mature tool for writing compliance and governance policy and It has very good documentation . Last week I was working on it to apply security and governance policies for my AWS account and had a lot of research, go through its documentation , couple of GitHub repositories and than started writing policies. In start most of the things work fine like I have created below policy and it worked perfectly..</p><pre>policies:<br> — name: ec2-day-plus<br> resource: ec2<br> filters:<br> — type: instance-age<br> op: ge<br> days: 1<br> actions:<br> — terminate</pre><p>It uses by default pull mode and we have to run it from Custodian CLI every time. That was easy but my aim was to explore it and compare it with some other tools like AWS Config, in that case I have to deep dive into all custodian modes and see how it can be beneficial.</p><p>I have first work with periodic mode it took me 10 to 20 minutes to run this as its deployment process was easy, you just have to create a policy and run it from CLI than rest will be take care by custodian .</p><p>Now periodic work for policy which we have to run on schedule but there are many services and API working in your cloud account and you have to take a quick action on them like tagging an EC2 instance, creating an alert for security group created or modified with 0.0.0.0/0 IP , for these scenarios we will use CloudTrail Mode . When we will create a policy in custodian it will create a lambda and CloudWatch event and that lambda will be triggered by CloudWatch event on each AWS API call.</p><p>I will describe to the point how I have worked with CloudTrail mode</p><h4>Prerequisite</h4><p>Before you can match these events, you must use AWS CloudTrail to set up a trail. If you do not have a trail, complete the following procedure.</p><p><strong>To create a trail</strong></p><ol><li>Open the CloudTrail console at <a href="https://console.aws.amazon.com/cloudtrail/">https://console.aws.amazon.com/cloudtrail/</a>.</li><li>Choose <strong>Trails</strong>, <strong>Create trail</strong>.</li><li>For <strong>Trail name</strong>, type a name for the trail.</li><li>For <strong>Storage location</strong>, in <strong>Create a new S3 bucket</strong> type the name for the new bucket that CloudTrail should deliver logs to.</li><li>Choose <strong>Create</strong>.</li></ol><p>Now create the policy with mode cloud trail like this</p><pre>policies:</pre><pre>- name: ec2-auto-tag-instances<br>    resource: aws.ec2<br>    # use cloudtrail event, not ec2-instance-state, when you need to filter on an instance&#39;s tags (this will let any tagging Lambdas that are triggered by instance creation events take effect first)<br>    mode:<br>      type: cloudtrail<br>      role: arn:aws:iam::{account-id}:role/Custodian-Role<br>      source: ec2<br>      events:<br>        - source: ec2.amazonaws.com<br>          event: StartInstances<br>          ids: &quot;responseElements.instancesSet.items[].instanceId&quot;<br>        - source: ec2.amazonaws.com<br>          event: StopInstances<br>          ids: &quot;responseElements.instancesSet.items[].instanceId&quot;<br>    comment: |<br>      # Tag all instances with a Custodian tag, so they&#39;ll be subject to the auto-termination policy<br>    filters:<br>      - tag:owner: absent<br>    actions:<br>      - type: auto-tag-user<br>        tag: owner<br>        principal_id_tag: CreatorId</pre><p>This is a simple policy which will tag EC2 instances with owner and creator name once an EC2 is stopped or start .</p><p>I will discuss some important point in this policy first IAM role this role will be used by lambda created with custodian to create tags and logs CloudWatch events . Here is the example policy of the IAM Role, create this policy and attach it IAM Role.</p><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Sid&quot;: &quot;VisualEditor0&quot;,<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Action&quot;: [<br>                &quot;ec2:AuthorizeSecurityGroupIngress&quot;,<br>                &quot;ec2:DescribeInstances&quot;,<br>                &quot;ec2:TerminateInstances&quot;,<br>                &quot;ec2:DeleteTags&quot;,<br>                &quot;logs:DescribeLogGroups&quot;,<br>                &quot;ec2:CreateTags&quot;,<br>                &quot;logs:DescribeLogStreams&quot;,<br>                &quot;logs:CreateLogGroup&quot;,<br>                &quot;logs:PutLogEvents&quot;,<br>                &quot;ec2:DescribeSecurityGroups&quot;,<br>                &quot;ec2:RevokeSecurityGroupIngress&quot;,<br>                &quot;logs:CreateLogStream&quot;,<br>                &quot;ec2:DescribeVolumes&quot;,<br>                &quot;logs:GetLogEvents&quot;,<br>                &quot;logs:FilterLogEvents&quot;<br>            ],<br>            &quot;Resource&quot;: &quot;*&quot;<br>        }<br>    ]<br>}</pre><p>Now run this policy from custodian command line like this</p><pre>custodian run -s output ec2-auto-tag.yml</pre><p>It will give this output</p><pre>2020-06-16 17:25:20,027: custodian.policy:INFO Provisioning policy lambda: ec2-tag-instances-auto region: us-east-1            <br>2020-06-16 17:25:21,753: custodian.serverless:INFO Publishing custodian policy lambda function custodian-ec2-tag-instances-auto</pre><p>Now go to AWS Console and start or stop any existing instance after some time policy will add tags like below picture</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wbcf0-bmMwz75ViPz-oJ6g.png" /><figcaption>tags</figcaption></figure><p>Now It is possible that policy did not work , in that case follow below steps</p><ol><li>Check that CloudTrail trail is created and it is adding logs in s3 bucket</li><li>CloudTrail logs the event in CloudTrail event history</li><li>Lambda is triggered and its created logs in CloudWatch study the logs for issue , there must be some permissions issues or issues in the policy or something fix them and update them</li><li>Lambda is not even triggered in that case verify all three above steps mentions.</li></ol><p>That’s it I will explain some other important things related to Custodian in my upcoming blogs related to AWS SecurityHub and AWS Config</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=308e6efdef1b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>