{"id":36349,"date":"2016-06-23T15:06:26","date_gmt":"2016-06-23T09:36:26","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=36349"},"modified":"2016-12-19T15:30:13","modified_gmt":"2016-12-19T10:00:13","slug":"aws-security-re-check","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/aws-security-re-check\/","title":{"rendered":"AWS Security Re-Check"},"content":{"rendered":"<p style=\"text-align: left\"><span style=\"font-weight: 400\"><strong>Security<\/strong> is of prime importance for any cloud vendor including AWS. AWS follows a <\/span><a href=\"https:\/\/aws.amazon.com\/compliance\/shared-responsibility-model\/\"><span style=\"font-weight: 400\">Shared Responsibility Model<\/span><\/a><span style=\"font-weight: 400\"> for security. As the name Shared Responsibility Model suggests, security on AWS is not the sole responsibility of either AWS or the customer. It is a combined effort from both parties. The responsibility of AWS includes providing a global secure infrastructure and services, and the responsibility of the customer includes protecting the confidentiality, integrity, and availability of their data in the cloud.<\/span><\/p>\n<p style=\"text-align: left\"><strong><span style=\"font-weight: 400\">The figure given below depicts the clear line of separation between the responsibilities:<\/span><\/strong><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-36350\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/Shared-responsibility-model.png\" alt=\"Shared-responsibility-model\" width=\"800\" height=\"400\" \/><\/p>\n<h6 style=\"text-align: left\">Image courtesy: aws.amazon.com<\/h6>\n<p style=\"text-align: left\"><span style=\"font-weight: 400\">While <a title=\"AWS DevOps\" href=\"http:\/\/www.tothenew.com\/devops-aws\">using any of the AWS services<\/a>, customers must follow these security checks regularly to meet the critical security requirements of their data.\u00a0<\/span><\/p>\n<p style=\"text-align: left\"><strong>This blog describes what are the basic security checks that should be performed on every AWS account, why they should be performed, and a code snippet to automate all the described basic security checks.<\/strong><\/p>\n<h1 style=\"text-align: left\"><strong>Server Hosting\u00a0<\/strong><\/h1>\n<p style=\"text-align: left\"><span style=\"font-weight: 400\">First of all, you need to identify where to host your servers. Your account may support EC2-VPC and EC2-Classic. If the account was created after 4th Dec 2013, it will only support EC2-VPC. VPC is always recommended due to its better and flexible security. In case you have servers running on Classic, you should plan to move them to VPC as early as possible.\u00a0<\/span>So, a check for the\u00a0existence of any server on Classic layer becomes necessary if you are using the older account.<\/p>\n<p style=\"text-align: left\"><span style=\"font-weight: 400\">Here is the python snippet that checks for all instances in all the regions\u00a0and lists details of instances if any existing in EC2-Classic:<\/span><\/p>\n<p>[sourcecode language=&#8221;python&#8221;]<br \/>\nreservations = connection.get_all_instances()<br \/>\n\tfor reservation in reservations:<br \/>\n\t\tfor instance in reservation.instances:<br \/>\n\t\t\tdetail=instance.__dict__<br \/>\n\t\t\tcount=0<br \/>\n\t\t\tif detail[&#8216;vpc_id&#8217;] == None:<br \/>\n\t\t\t\tcount=count+1<br \/>\n\t\t\tif count!=0:<br \/>\n\t\t\t\tdata = [&#8221;,instance.tags[&#8216;Name&#8217;] , instance.id,&#8221;]<br \/>\n\t\t\t\tcsvwriter.writerow(data)[\/sourcecode]<\/p>\n<h1 style=\"text-align: left\"><strong>Firewall\u00a0<\/strong><\/h1>\n<p><span style=\"font-weight: 400\">Next is the Firewall layer. Once you have placed your servers in VPC, you must configure perimeter security for each of your instances. Firewall is called a Security Group in AWS. Here you define what all machines (or group of machines) can access a particular instance and on what port. It&#8217;s always recommended to keep it restricted to a specific IP.\u00a0So, there should be another check to see if someone has not accidentally opened up any port for public (0.0.0.0\/0).<\/span><\/p>\n<p>[sourcecode language=&#8221;python&#8221;]<\/p>\n<p>sg=connection.get_all_security_groups()<br \/>\nfor group in sg:<br \/>\n\tfor rule in group.rules:<br \/>\n\t\tif &#8216;0.0.0.0\/0&#8242; in str(rule.grants):<br \/>\n\t\t\tif rule.to_port == None:<br \/>\n\t\t\t\trule.to_port=&#8217;ALL&#8217;<br \/>\n\t\t\tdata = [&#8221;, group.name, rule.grants, rule.to_port ]<br \/>\n\t\t\tcsvwriter.writerow(data)<br \/>\n[\/sourcecode]<\/p>\n<h1><b>IAM Key Rotation<\/b><\/h1>\n<p style=\"text-align: left\">IAM keys can be used to access your account. Anyone with the open secret and access keys of your account has as many permissions to make changes to your account as you do. So, to avoid the risk of leak and misuse of access keys, as a security best practice, IAM Keys should be rotated in every 90 days.<\/p>\n<p>[sourcecode language=&#8221;python&#8221;]<\/p>\n<p>for value in range (0,len(val)):<br \/>\n\tif diff[value] &gt; 90:<br \/>\n\t\tdata=[ val[value],keys[value],&#8217;Key Not rotated from 90 days&#8217;,activekeys[value] ]<br \/>\n        csvwriter.writerow(data)<br \/>\n\telse:<br \/>\n\t\tdata=[ val[value],keys[value],&#8217;Key is rotated&#8217;,activekeys[value] ]<br \/>\n\t\tcsvwriter.writerow(data)<br \/>\n[\/sourcecode]<\/p>\n<h1 style=\"text-align: left\"><strong>Multi-Factor Authentication<\/strong><\/h1>\n<p style=\"text-align: left\"><strong><span style=\"font-weight: 400\">MFA (Multi-Factor Authentication) is an added layer of security, used when there is more than one method of authentication required. It adds security as users have to configure a device\/number and enter a unique authentication code from their approved device or text message whenever they want to login to their account and access AWS services. <\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">This restricts someone with your leaked root credentials to make any changes on your account.<\/span><\/strong><\/p>\n<p>[sourcecode language=&#8221;python&#8221;]<br \/>\nfor user in range(0,no_of_users):<br \/>\n\tuser_name=users[&#8216;list_users_response&#8217;][&#8216;list_users_result&#8217;][&#8216;users&#8217;][user][&#8216;user_name&#8217;]<br \/>\n\tmfa=connection.get_all_mfa_devices(user_name)<br \/>\n\tstatus=mfa[&#8216;list_mfa_devices_response&#8217;][&#8216;list_mfa_devices_result&#8217;][&#8216;mfa_devices&#8217;]<br \/>\n\tif len(status)==0:<br \/>\n\t\tdata=[user_name,&#8217;Not Enabled&#8217;]<br \/>\n\t\tcsvwriter.writerow(data)<br \/>\n[\/sourcecode]<\/p>\n<h1 style=\"text-align: left\"><strong>User Activity Tracking<\/strong><\/h1>\n<p style=\"text-align: left\">As the number of users increases in an organization, it becomes harder to track what changes\u00a0each individual is making. As an owner, you might want to see what action was taken on what AWS resource and which user is accountable for it. AWS suggests having CloudTrail enabled for all regions in an account. Cloudtrail is used for API calls logging. It records all information about each API call made on your account in the S3 bucket. You can restrict the access of log information by managing IAM roles. By API calls logging, you can track changes made to your AWS resources.<\/p>\n<p>[sourcecode language=&#8221;python&#8221;]<br \/>\nconnection = boto.cloudtrail.connect_to_region(r_name)<br \/>\nc_trail=connection.describe_trails()<br \/>\nif not c_trail[&#8216;trailList&#8217;]:<br \/>\n\tdata=[r_name, &quot;Not Enabled&quot;]<br \/>\n\tcsvwriter.writerow(data)<br \/>\nelse:<br \/>\n\tdata=[r_name, &quot;Enabled&quot;]<br \/>\n\tcsvwriter.writerow(data)<br \/>\n[\/sourcecode]<\/p>\n<h1 style=\"text-align: left\"><strong>Data Security<\/strong><\/h1>\n<p style=\"text-align: left\"><span style=\"font-weight: 400\">Data security is a big challenge these days. \u00a0Customers keep their critical or non-critical data on AWS Storage service (S3) inside the buckets available in all the regions. It becomes necessary to restrict these buckets with respect to the level of access for the users. Any bucket exposes various permissions like Read, Write, Read_ACP (view Access Control Permissions) and Full Control which are given to different users as per their requirement.<\/span><\/p>\n<p style=\"text-align: left\">There should be a check to identify that only required permission is given to users. For example, if my application is supposed to read and write from the bucket, it does not need permissions for everyone but for a dedicated user. Also, if any bucket needs to have read only permission for everyone to access, it should be limited to \u2018Read Only\u2019 permission and never for Write or ACP related permissions. For more security, data can be encrypted before pushing it in the bucket.<\/p>\n<p>[sourcecode language=&#8221;python&#8221;]<br \/>\nfor bucket in buckets:<br \/>\n\tbucket_policy=bucket.get_acl()<br \/>\n    user_policy=bucket_policy.acl<br \/>\n    user_grants=user_policy.grants<br \/>\n    no_of_user=len(user_grants)<br \/>\n    data=[bucket.name.title(),bucket_policy.owner.display_name,&#8217; &#8216;]<br \/>\n    csvwriter.writerow(data)<br \/>\n\tfor user in user_grants:<br \/>\n\t\tuname=user.display_name<br \/>\n        user_permission=user.permission<br \/>\n        if (uname==None):<br \/>\n        \tu_uri=user.uri<br \/>\n            uri_split=u_uri.split(&#8216;\/&#8217;)<br \/>\n            uname=str(uri_split[-1])<br \/>\n        data=[&#8221;,&#8221;,uname,user_permission ]<br \/>\n        csvwriter.writerow(data)<br \/>\n[\/sourcecode]<\/p>\n<p>All above listed security checks should be performed on every AWS account on a monthly or quarterly basis to make you aware of any potential risks with your account.\u00a0Also, to perform all these checks, you require a user with READ-ONLY permissions.<\/p>\n<p style=\"text-align: left\"><span style=\"font-weight: 400\">I have compiled all these snippets in a single script\u00a0which does all above checks automatically and will give you an easy to understand output for each check. <strong><a href=\"https:\/\/gist.github.com\/sakshi1995\/6154698d14f67bcde534b163f6df3557\">Download<\/a><\/strong> the script from here.<\/span><\/p>\n<p style=\"text-align: left\">Once executed, the script will generate an Excel sheet combining all outputs and will appear something like:<img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-36494\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/output12.png\" alt=\"output1\" width=\"657\" height=\"70\" \/><\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-36379\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/output4.png\" alt=\"output\" width=\"656\" height=\"79\" \/><\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-36493\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/MFAAA.png\" alt=\"MFAAA\" width=\"348\" height=\"78\" \/><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-36491\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/OUTCLOUDTRAIL.png\" alt=\"OUTCLOUDTRAIL\" width=\"347\" height=\"70\" \/><\/p>\n<p>I hope this blog would have given you the reason for doing all these checks and the way it can be done without much effort.<\/p>\n<p style=\"text-align: left\">For running the script:<\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Python should be in the system. <\/span><\/li>\n<li style=\"font-weight: 400\"><strong><a href=\"https:\/\/pypi.python.org\/pypi\/boto\">boto<\/a><\/strong><span style=\"font-weight: 400\"> should be installed and\u00a0<strong><a href=\"http:\/\/docs.pythonboto.org\/en\/latest\/boto_config_tut.html\">configured<\/a><\/strong><\/span><span style=\"font-weight: 400\">.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">To be able to see the output in the CSV format, an additional package <\/span><a href=\"https:\/\/pypi.python.org\/pypi\/xlwt\"><b>xlwt<\/b><\/a><span style=\"font-weight: 400\"> needs to be installed<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">Simply run the script using<\/span><b> python SecurityCheck.py<\/b><span style=\"font-weight: 400\">.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Security is of prime importance for any cloud vendor including AWS. AWS follows a Shared Responsibility Model for security. As the name Shared Responsibility Model suggests, security on AWS is not the sole responsibility of either AWS or the customer. It is a combined effort from both parties. The responsibility of AWS includes providing a [&hellip;]<\/p>\n","protected":false},"author":928,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[1174,2348],"tags":[3582,1611,3590,3588,1899,3585,3587,2755,1358,3586,1329,3584,3583,1692,3589],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/36349"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/928"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=36349"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/36349\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=36349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=36349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=36349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}