Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(IAM): Add inline policies checks and improve custom policy checks #4255

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4cba0ac
chore(policy): Add generic function about policies
puchy22 Jun 13, 2024
1d7f4f1
feat(policy): Add new privilege escalation policies utils
puchy22 Jun 13, 2024
61184ee
chore(policy): Use new general functions for priv escalation
puchy22 Jun 13, 2024
aae658a
feat(iam): New check related to priv escalation with inline policies
puchy22 Jun 13, 2024
6465eb2
tests(iam): Testing new check related with priv esc for inline policies
puchy22 Jun 13, 2024
eff8454
chore(iam): Change priv escalatioon function
puchy22 Jun 17, 2024
dfed6b7
chore(iam): Add new generic function to check full access policy
puchy22 Jun 17, 2024
85b8ef4
chore(iam): Delete moved comments
puchy22 Jun 17, 2024
44f3921
feat(iam): New full access to cloudtrail with inline policies
puchy22 Jun 17, 2024
26b29dc
chore(iam): Change repeated cons
puchy22 Jun 17, 2024
9796db6
chore(iam): Remove unnecesary for loop and change status extended
puchy22 Jun 17, 2024
de8adc9
tests(iam): Testing new check related with full access to cloudtrail …
puchy22 Jun 17, 2024
a660d2e
chore(iam): Update check to reuse generic function
puchy22 Jun 17, 2024
d1e55a3
chore(iam): Solve nested fstrings problem
puchy22 Jun 17, 2024
387a7eb
chore(iam): Delete unnecesary comments
puchy22 Jun 17, 2024
7005a42
chore(iam): Change double quotes by singles
puchy22 Jun 18, 2024
7c7ad7b
chore(iam): Delete backslashes
puchy22 Jun 18, 2024
3a7e213
feat(iam): Add new inline policy check for full access to KMS
puchy22 Jun 18, 2024
372d26c
tests(iam): Testing new check related with full access to kms through…
puchy22 Jun 18, 2024
f90a947
doc(unused_services): Add 3 checks of inline policies to scan unusued…
puchy22 Jun 18, 2024
9e0436e
fix(iam): Fix status extended blank spaces
puchy22 Jun 18, 2024
6716e41
tests(iam): New test cases for NoAction policies
puchy22 Jun 18, 2024
5bb6956
fix(iam): Change generic function to only ensure about policies
puchy22 Jun 18, 2024
ca8e4d8
fix(iam): Change generic function to only ensure about policies and f…
puchy22 Jun 18, 2024
7300463
tests(iam): Add testing for priv esc function
puchy22 Jun 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/tutorials/scan-unused-services.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,13 @@ VPCs should have separate private and public subnets to prevent the exposure of
VPCs should have subnets in different availability zones to prevent a single point of failure. Prowler will only check this configuration for those VPCs that are in use, in other words, only the VPCs where you have ENIs (network interfaces).

- `vpc_subnet_different_az`

#### IAM
Prowler checks that inline policies does not overly guaranteeing access to certain services such as Cloudtrail and KMS, only attached inline policies will be checked.

- `iam_policy_no_full_access_to_cloudtrail`
- `iam_policy_no_full_access_to_kms`

Policies with determined combination could lead into a privilege escalation, only attached inline policies will be checked.

- `iam_policy_allows_privilege_escalation`
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"Provider": "aws",
"CheckID": "iam_inline_policy_allows_privilege_escalation",
"CheckTitle": "Ensure no Inline Managed IAM policies allow actions that may lead into Privilege Escalation",
"CheckType": [
"Software and Configuration Checks",
"Industry and Regulatory Standards"
],
"ServiceName": "iam",
"SubServiceName": "inline_policy",
"ResourceIdTemplate": "arn:partition:service:region:account-id:resource-id",
"Severity": "high",
"ResourceType": "AwsIamPolicy",
"Description": "Ensure no Inline Managed IAM policies allow actions that may lead into Privilege Escalation",
"Risk": "Users with some IAM permissions are allowed to elevate their privileges up to administrator rights.",
"RelatedUrl": "",
"Remediation": {
"Code": {
"CLI": "",
"NativeIaC": "",
"Other": "",
"Terraform": ""
},
"Recommendation": {
"Text": "Grant usage permission on a per-resource basis and applying least privilege principle.",
"Url": "https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege"
}
},
"Categories": [],
"DependsOn": [],
"RelatedTo": [],
"Notes": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from prowler.lib.check.models import Check, Check_Report_AWS
from prowler.providers.aws.services.iam.iam_client import iam_client
from prowler.providers.aws.services.iam.lib.privilege_escalation import (
check_privilege_escalation,
)


class iam_inline_policy_allows_privilege_escalation(Check):
def execute(self) -> Check_Report_AWS:
findings = []

for policy in iam_client.policies:
if (
policy.attached or iam_client.provider.scan_unused_services
) and policy.type == "Inline":
report = Check_Report_AWS(self.metadata())
report.resource_id = policy.name
report.resource_arn = policy.arn
report.region = iam_client.region
report.resource_tags = policy.tags
report.status = "PASS"

if "role" in report.resource_arn:
resource_type_str = "role"
elif "group" in report.resource_arn:
resource_type_str = "group"
elif "user" in report.resource_arn:
resource_type_str = "user"
else:
resource_type_str = "resource"

Check warning on line 30 in prowler/providers/aws/services/iam/iam_inline_policy_allows_privilege_escalation/iam_inline_policy_allows_privilege_escalation.py

View check run for this annotation

Codecov / codecov/patch

prowler/providers/aws/services/iam/iam_inline_policy_allows_privilege_escalation/iam_inline_policy_allows_privilege_escalation.py#L30

Added line #L30 was not covered by tests

report.status_extended = f"Inline Policy '{report.resource_id}'{' attached to ' + resource_type_str + ' ' + report.resource_arn if policy.attached else ''} does not allow privilege escalation."

policies_affected = check_privilege_escalation(
getattr(policy, "document", {})
)

if policies_affected:
report.status = "FAIL"

report.status_extended = (
f"Inline Policy '{report.resource_id}'{' attached to ' + resource_type_str + ' ' + report.resource_arn if policy.attached else ''} allows privilege escalation using the following actions: {policies_affected}".rstrip()
+ "."
)

findings.append(report)

return findings
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"Provider": "aws",
"CheckID": "iam_inline_policy_no_full_access_to_cloudtrail",
"CheckTitle": "Ensure IAM inline policies that allow full \"cloudtrail:*\" privileges are not created",
"CheckType": [
"Software and Configuration Checks",
"Industry and Regulatory Standards",
"CIS AWS Foundations Benchmark"
],
"ServiceName": "iam",
"SubServiceName": "inline_policies",
"ResourceIdTemplate": "arn:partition:service:region:account-id:resource-id",
"Severity": "medium",
"ResourceType": "AwsIamPolicy",
"Description": "Ensure IAM inline policies that allow full \"cloudtrail:*\" privileges are not created",
"Risk": "CloudTrail is a critical service and IAM policies should follow least privilege model for this service in particular",
"RelatedUrl": "",
"Remediation": {
"Code": {
"CLI": "",
"NativeIaC": "",
"Other": "",
"Terraform": ""
},
"Recommendation": {
"Text": "It is more secure to start with a minimum set of permissions and grant additional permissions as necessary, rather than starting with permissions that are too lenient and then trying to tighten them later. List policies an analyze if permissions are the least possible to conduct business activities.",
"Url": "http://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html"
}
},
"Categories": [],
"DependsOn": [],
"RelatedTo": [],
"Notes": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from prowler.lib.check.models import Check, Check_Report_AWS
from prowler.providers.aws.services.iam.iam_client import iam_client
from prowler.providers.aws.services.iam.lib.policy import check_full_service_access

critical_service = "cloudtrail"


class iam_inline_policy_no_full_access_to_cloudtrail(Check):
def execute(self) -> Check_Report_AWS:
findings = []

for policy in iam_client.policies:
# Check only inline policies
if (
policy.attached or iam_client.provider.scan_unused_services
) and policy.type == "Inline":
report = Check_Report_AWS(self.metadata())
report.region = iam_client.region
report.resource_arn = policy.arn
report.resource_id = policy.name
report.resource_tags = policy.tags
report.status = "PASS"
report.status_extended = f"Inline Policy {policy.name} does not allow '{critical_service}:*' privileges."

if policy.document and check_full_service_access(
critical_service, policy.document
):
report.status = "FAIL"
report.status_extended = f"Inline Policy {policy.name} allows '{critical_service}:*' privileges to all resources."

findings.append(report)

return findings
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"Provider": "aws",
"CheckID": "iam_inline_policy_no_full_access_to_kms",
"CheckTitle": "Ensure IAM inline policies that allow full \"kms:*\" privileges are not created",
"CheckType": [
"Software and Configuration Checks"
],
"ServiceName": "iam",
"SubServiceName": "inline_policy",
"ResourceIdTemplate": "arn:partition:service:region:account-id:resource-id",
"Severity": "medium",
"ResourceType": "AwsIamPolicy",
"Description": "Ensure IAM inline policies that allow full \"kms:*\" privileges are not created",
"Risk": "KMS is a critical service and IAM policies should follow least privilege model for this service in particular",
"RelatedUrl": "",
"Remediation": {
"Code": {
"CLI": "",
"NativeIaC": "",
"Other": "",
"Terraform": ""
},
"Recommendation": {
"Text": "It is more secure to start with a minimum set of permissions and grant additional permissions as necessary, rather than starting with permissions that are too lenient and then trying to tighten them later. List policies an analyze if permissions are the least possible to conduct business activities.",
"Url": "http://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html"
}
},
"Categories": [],
"DependsOn": [],
"RelatedTo": [],
"Notes": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from prowler.lib.check.models import Check, Check_Report_AWS
from prowler.providers.aws.services.iam.iam_client import iam_client
from prowler.providers.aws.services.iam.lib.policy import check_full_service_access

critical_service = "kms"


class iam_inline_policy_no_full_access_to_kms(Check):
def execute(self):
findings = []

for policy in iam_client.policies:
if (
policy.attached or iam_client.provider.scan_unused_services
) and policy.type == "Inline":
report = Check_Report_AWS(self.metadata())
report.region = iam_client.region
report.resource_arn = policy.arn
report.resource_id = policy.name
report.resource_tags = policy.tags
report.status = "PASS"
report.status_extended = f"Inline Policy {policy.name} does not allow '{critical_service}:*' privileges."

if policy.document and check_full_service_access(
critical_service, policy.document
):
report.status = "FAIL"
report.status_extended = f"Inline Policy {policy.name} allows '{critical_service}:*' privileges."

findings.append(report)

return findings
Loading
Loading