-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aws): add fixers for threat detection checks (#7085)
- Loading branch information
1 parent
a48e5cb
commit 96cae5e
Showing
11 changed files
with
232 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
.../cloudtrail_threat_detection_enumeration/cloudtrail_threat_detection_enumeration_fixer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import json | ||
|
||
from prowler.lib.logger import logger | ||
from prowler.providers.aws.services.iam.iam_client import iam_client | ||
|
||
|
||
def fixer(resource_arn: str) -> bool: | ||
""" | ||
Restricts access to a compromised AWS entity by attaching a deny-all inline policy to the user or role. | ||
Requires the following permissions: | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"iam:PutUserPolicy", | ||
"iam:PutRolePolicy", | ||
], | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
Args: | ||
resource_arn (str): The ARN of the compromised AWS entity (IAM User or Role). | ||
Returns: | ||
bool: True if the fix was applied successfully, False otherwise. | ||
""" | ||
try: | ||
if ":user/" in resource_arn: | ||
entity_type = "user" | ||
entity_name = resource_arn.split("/")[-1] | ||
elif ":role/" in resource_arn: | ||
entity_type = "role" | ||
entity_name = resource_arn.split("/")[-1] | ||
else: | ||
return False | ||
|
||
deny_policy = { | ||
"Version": "2012-10-17", | ||
"Statement": [{"Effect": "Deny", "Action": "*", "Resource": "*"}], | ||
} | ||
|
||
policy_name = "DenyAllAccess" | ||
|
||
if entity_type == "user": | ||
iam_client.client.put_user_policy( | ||
UserName=entity_name, | ||
PolicyName=policy_name, | ||
PolicyDocument=json.dumps(deny_policy), | ||
) | ||
logger.info(f"Applied Deny policy to user {entity_name}") | ||
|
||
elif entity_type == "role": | ||
iam_client.client.put_role_policy( | ||
RoleName=entity_name, | ||
PolicyName=policy_name, | ||
PolicyDocument=json.dumps(deny_policy), | ||
) | ||
logger.info(f"Applied Deny policy to role {entity_name}") | ||
|
||
return True | ||
|
||
except Exception as error: | ||
logger.error( | ||
f"{error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}" | ||
) | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
.../cloudtrail_threat_detection_llm_jacking/cloudtrail_threat_detection_llm_jacking_fixer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import json | ||
|
||
from prowler.lib.logger import logger | ||
from prowler.providers.aws.services.iam.iam_client import iam_client | ||
|
||
|
||
def fixer(resource_arn: str) -> bool: | ||
""" | ||
Restricts access to a compromised AWS entity by attaching a deny-all inline policy to the user or role. | ||
Requires the following permissions: | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"iam:PutUserPolicy", | ||
"iam:PutRolePolicy", | ||
], | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
Args: | ||
resource_arn (str): The ARN of the compromised AWS entity (IAM User or Role). | ||
Returns: | ||
bool: True if the fix was applied successfully, False otherwise. | ||
""" | ||
try: | ||
if ":user/" in resource_arn: | ||
entity_type = "user" | ||
entity_name = resource_arn.split("/")[-1] | ||
elif ":role/" in resource_arn: | ||
entity_type = "role" | ||
entity_name = resource_arn.split("/")[-1] | ||
else: | ||
return False | ||
|
||
deny_policy = { | ||
"Version": "2012-10-17", | ||
"Statement": [{"Effect": "Deny", "Action": "*", "Resource": "*"}], | ||
} | ||
|
||
policy_name = "DenyAllAccess" | ||
|
||
if entity_type == "user": | ||
iam_client.client.put_user_policy( | ||
UserName=entity_name, | ||
PolicyName=policy_name, | ||
PolicyDocument=json.dumps(deny_policy), | ||
) | ||
logger.info(f"Applied Deny policy to user {entity_name}") | ||
|
||
elif entity_type == "role": | ||
iam_client.client.put_role_policy( | ||
RoleName=entity_name, | ||
PolicyName=policy_name, | ||
PolicyDocument=json.dumps(deny_policy), | ||
) | ||
logger.info(f"Applied Deny policy to role {entity_name}") | ||
|
||
return True | ||
|
||
except Exception as error: | ||
logger.error( | ||
f"{error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}" | ||
) | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
..._detection_privilege_escalation/cloudtrail_threat_detection_privilege_escalation_fixer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import json | ||
|
||
from prowler.lib.logger import logger | ||
from prowler.providers.aws.services.iam.iam_client import iam_client | ||
|
||
|
||
def fixer(resource_arn: str) -> bool: | ||
""" | ||
Restricts access to a compromised AWS entity by attaching a deny-all inline policy to the user or role. | ||
Requires the following permissions: | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"iam:PutUserPolicy", | ||
"iam:PutRolePolicy", | ||
], | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
Args: | ||
resource_arn (str): The ARN of the compromised AWS entity (IAM User or Role). | ||
Returns: | ||
bool: True if the fix was applied successfully, False otherwise. | ||
""" | ||
try: | ||
if ":user/" in resource_arn: | ||
entity_type = "user" | ||
entity_name = resource_arn.split("/")[-1] | ||
elif ":role/" in resource_arn: | ||
entity_type = "role" | ||
entity_name = resource_arn.split("/")[-1] | ||
else: | ||
return False | ||
|
||
deny_policy = { | ||
"Version": "2012-10-17", | ||
"Statement": [{"Effect": "Deny", "Action": "*", "Resource": "*"}], | ||
} | ||
|
||
policy_name = "DenyAllAccess" | ||
|
||
if entity_type == "user": | ||
iam_client.client.put_user_policy( | ||
UserName=entity_name, | ||
PolicyName=policy_name, | ||
PolicyDocument=json.dumps(deny_policy), | ||
) | ||
logger.info(f"Applied Deny policy to user {entity_name}") | ||
|
||
elif entity_type == "role": | ||
iam_client.client.put_role_policy( | ||
RoleName=entity_name, | ||
PolicyName=policy_name, | ||
PolicyDocument=json.dumps(deny_policy), | ||
) | ||
logger.info(f"Applied Deny policy to role {entity_name}") | ||
|
||
return True | ||
|
||
except Exception as error: | ||
logger.error( | ||
f"{error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}" | ||
) | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters