|
| 1 | +"""Processes an entry under GroupDetailList""" |
| 2 | +from cloudsplaining.scan.policy_document import PolicyDocument |
| 3 | +from cloudsplaining.shared.utils import get_non_provider_id |
| 4 | + |
| 5 | + |
| 6 | +class GroupDetailList: |
| 7 | + """Processes all entries under the GroupDetailList""" |
| 8 | + def __init__(self, group_details, policy_details): |
| 9 | + self.groups = [] |
| 10 | + for group_detail in group_details: |
| 11 | + self.groups.append(GroupDetail(group_detail, policy_details)) |
| 12 | + |
| 13 | + def get_group_detail(self, name): |
| 14 | + """Get a GroupDetail object by providing the Name of the group. This is useful to UserDetail objects""" |
| 15 | + result = None |
| 16 | + for group_detail in self.groups: |
| 17 | + if group_detail.group_name == name: |
| 18 | + result = group_detail |
| 19 | + break |
| 20 | + return result |
| 21 | + |
| 22 | + def get_all_allowed_actions_for_group(self, name): |
| 23 | + """Returns a list of all allowed actions by the group across all its policies""" |
| 24 | + result = None |
| 25 | + for group_detail in self.groups: |
| 26 | + if group_detail.group_name == name: |
| 27 | + result = group_detail.all_allowed_actions |
| 28 | + break |
| 29 | + return result |
| 30 | + |
| 31 | + def get_all_iam_statements_for_group(self, name): |
| 32 | + """Returns a list of all StatementDetail objects across all the policies assigned to the group""" |
| 33 | + result = None |
| 34 | + for group_detail in self.groups: |
| 35 | + if group_detail.group_name == name: |
| 36 | + result = group_detail.all_iam_statements |
| 37 | + break |
| 38 | + return result |
| 39 | + |
| 40 | + @property |
| 41 | + def group_names(self): |
| 42 | + """Get a list of all group names in the account""" |
| 43 | + results = [] |
| 44 | + for group_detail in self.groups: |
| 45 | + results.append(group_detail.group_name) |
| 46 | + results.sort() |
| 47 | + return results |
| 48 | + |
| 49 | + @property |
| 50 | + def json(self): |
| 51 | + """Get all JSON results""" |
| 52 | + result = {} |
| 53 | + for group in self.groups: |
| 54 | + result[group.group_id] = group.json |
| 55 | + return result |
| 56 | + |
| 57 | + |
| 58 | +class GroupDetail: |
| 59 | + """Processes an entry under GroupDetailList""" |
| 60 | + def __init__(self, group_detail, policy_details): |
| 61 | + """ |
| 62 | + Initialize the GroupDetail object. |
| 63 | +
|
| 64 | + :param group_detail: Details about a particular group |
| 65 | + :param policy_details: The PolicyDetails object - i.e., details about all managed policies in the account so the group can inherit those attributes |
| 66 | + """ |
| 67 | + self.create_date = group_detail.get("CreateDate") |
| 68 | + self.arn = group_detail.get("Arn") |
| 69 | + self.path = group_detail.get("Path") |
| 70 | + self.group_id = group_detail.get("GroupId") |
| 71 | + self.group_name = group_detail.get("GroupName") |
| 72 | + |
| 73 | + # Inline Policies |
| 74 | + self.inline_policies = {} |
| 75 | + if group_detail.get("GroupPolicyList"): |
| 76 | + for inline_policy in group_detail.get("GroupPolicyList"): |
| 77 | + non_provider_id = get_non_provider_id(inline_policy.get("PolicyName")) |
| 78 | + self.inline_policies[non_provider_id] = dict( |
| 79 | + PolicyName=inline_policy.get("PolicyName"), |
| 80 | + PolicyDocument=PolicyDocument(inline_policy.get("PolicyDocument")), |
| 81 | + ) |
| 82 | + |
| 83 | + # Managed Policies (either AWS-managed or Customer managed) |
| 84 | + self.attached_managed_policies = [] |
| 85 | + if group_detail.get("AttachedManagedPolicies"): |
| 86 | + self._attached_managed_policies_details( |
| 87 | + group_detail.get("AttachedManagedPolicies"), |
| 88 | + policy_details |
| 89 | + ) |
| 90 | + else: |
| 91 | + self.attached_managed_policies = [] |
| 92 | + |
| 93 | + def _attached_managed_policies_details(self, attached_managed_policies_list, policy_details): |
| 94 | + for policy in attached_managed_policies_list: |
| 95 | + arn = policy.get("PolicyArn") |
| 96 | + attached_managed_policy_details = policy_details.get_policy_detail(arn) |
| 97 | + self.attached_managed_policies.append(attached_managed_policy_details) |
| 98 | + |
| 99 | + @property |
| 100 | + def attached_managed_policies_json(self): |
| 101 | + """Return JSON representation of attached managed policies""" |
| 102 | + policies = {} |
| 103 | + for policy in self.attached_managed_policies: |
| 104 | + policies[policy.policy_id] = policy.json |
| 105 | + return policies |
| 106 | + |
| 107 | + @property |
| 108 | + def all_allowed_actions(self): |
| 109 | + """Return a list of which actions are allowed by the principal""" |
| 110 | + privileges = [] |
| 111 | + for managed_policy in self.attached_managed_policies: |
| 112 | + privileges.extend(managed_policy.policy_document.all_allowed_actions) |
| 113 | + for inline_policy in self.inline_policies: |
| 114 | + privileges.extend(self.inline_policies[inline_policy]["PolicyDocument"].all_allowed_actions) |
| 115 | + return privileges |
| 116 | + |
| 117 | + @property |
| 118 | + def all_iam_statements(self): |
| 119 | + """Return a list of which actions are allowed by the principal""" |
| 120 | + statements = [] |
| 121 | + for managed_policy in self.attached_managed_policies: |
| 122 | + statements.extend(managed_policy.policy_document.statements) |
| 123 | + for inline_policy in self.inline_policies: |
| 124 | + statements.extend(self.inline_policies[inline_policy]["PolicyDocument"].statements) |
| 125 | + return statements |
| 126 | + |
| 127 | + @property |
| 128 | + def consolidated_risks(self): |
| 129 | + """Return a dict containing the consolidated risks from all inline and managed policies""" |
| 130 | + privilege_escalation_results = {} |
| 131 | + resource_exposure_results = [] |
| 132 | + data_exfiltration_results = [] |
| 133 | + |
| 134 | + # Get it from each inline policy |
| 135 | + if self.inline_policies: |
| 136 | + for inline_policy_key in self.inline_policies: |
| 137 | + # Privilege Escalation |
| 138 | + if self.inline_policies[inline_policy_key]["PolicyDocument"].allows_privilege_escalation: |
| 139 | + for entry in self.inline_policies[inline_policy_key]["PolicyDocument"].allows_privilege_escalation: |
| 140 | + if entry["type"] not in privilege_escalation_results.keys(): |
| 141 | + privilege_escalation_results[entry["type"]] = entry["actions"] |
| 142 | + # Resource Exposure |
| 143 | + if self.inline_policies[inline_policy_key]["PolicyDocument"].permissions_management_without_constraints: |
| 144 | + for action in self.inline_policies[inline_policy_key]["PolicyDocument"].permissions_management_without_constraints: |
| 145 | + if action not in resource_exposure_results: |
| 146 | + resource_exposure_results.append(action) |
| 147 | + # Data Exfiltration |
| 148 | + if self.inline_policies[inline_policy_key]["PolicyDocument"].allows_data_leak_actions: |
| 149 | + for action in self.inline_policies[inline_policy_key]["PolicyDocument"].allows_data_leak_actions: |
| 150 | + if action not in data_exfiltration_results: |
| 151 | + data_exfiltration_results.append(action) |
| 152 | + |
| 153 | + if self.attached_managed_policies: |
| 154 | + for managed_policy in self.attached_managed_policies: |
| 155 | + # Privilege Escalation |
| 156 | + if managed_policy.policy_document.allows_privilege_escalation: |
| 157 | + for entry in managed_policy.policy_document.allows_privilege_escalation: |
| 158 | + if entry["type"] not in privilege_escalation_results.keys(): |
| 159 | + privilege_escalation_results[entry["type"]] = entry["actions"] |
| 160 | + # Resource Exposure |
| 161 | + if managed_policy.policy_document.permissions_management_without_constraints: |
| 162 | + for action in managed_policy.policy_document.permissions_management_without_constraints: |
| 163 | + if action not in resource_exposure_results: |
| 164 | + resource_exposure_results.append(action) |
| 165 | + # Data Exfiltration |
| 166 | + if managed_policy.policy_document.allows_data_leak_actions: |
| 167 | + for action in managed_policy.policy_document.allows_data_leak_actions: |
| 168 | + if action not in data_exfiltration_results: |
| 169 | + data_exfiltration_results.append(action) |
| 170 | + |
| 171 | + # turn it into a list because we want to be able to count the number of results |
| 172 | + these_privilege_escalation_results = [] |
| 173 | + |
| 174 | + for key in privilege_escalation_results: |
| 175 | + result = { |
| 176 | + "type": key, |
| 177 | + "actions": privilege_escalation_results[key] |
| 178 | + } |
| 179 | + these_privilege_escalation_results.append(result) |
| 180 | + |
| 181 | + resource_exposure_results.sort() |
| 182 | + data_exfiltration_results.sort() |
| 183 | + |
| 184 | + results = { |
| 185 | + "PrivilegeEscalation": these_privilege_escalation_results, |
| 186 | + "ResourceExposure": resource_exposure_results, |
| 187 | + "DataExfiltration": data_exfiltration_results, |
| 188 | + } |
| 189 | + |
| 190 | + return results |
| 191 | + |
| 192 | + @property |
| 193 | + def inline_policies_json(self): |
| 194 | + """Return JSON representation of attached inline policies""" |
| 195 | + inline_policies = {} |
| 196 | + if self.inline_policies: |
| 197 | + for inline_policy_key in self.inline_policies: |
| 198 | + inline_policies[inline_policy_key] = dict( |
| 199 | + PolicyDocument=self.inline_policies[inline_policy_key]["PolicyDocument"].json, |
| 200 | + Name=self.inline_policies[inline_policy_key]["PolicyName"] |
| 201 | + ) |
| 202 | + return inline_policies |
| 203 | + |
| 204 | + @property |
| 205 | + def json(self): |
| 206 | + """Return the JSON representation of the Group Detail""" |
| 207 | + |
| 208 | + this_group_detail = dict( |
| 209 | + arn=self.arn, |
| 210 | + create_date=self.create_date, |
| 211 | + id=self.group_id, |
| 212 | + inline_policies=self.inline_policies_json, |
| 213 | + inline_policies_count=len(self.inline_policies_json), |
| 214 | + path=self.path, |
| 215 | + managed_policies_count=len(self.attached_managed_policies), |
| 216 | + managed_policies=self.attached_managed_policies_json, |
| 217 | + risks=self.consolidated_risks |
| 218 | + ) |
| 219 | + return this_group_detail |
0 commit comments