|
1 | | -def check_wildcard_actions(policy): |
| 1 | +import json |
| 2 | + |
| 3 | +def _find_statement_line(stmt, raw_lines): |
| 4 | + if not raw_lines: |
| 5 | + return None |
| 6 | + |
| 7 | + effect = stmt.get("Effect") |
| 8 | + actions = stmt.get("Action", []) |
| 9 | + resources = stmt.get("Resource", []) |
| 10 | + |
| 11 | + if isinstance(actions, str): |
| 12 | + actions = [actions] |
| 13 | + if isinstance(resources, str): |
| 14 | + resources = [resources] |
| 15 | + |
| 16 | + # Scan for Action lines directly |
| 17 | + for i in range(len(raw_lines)): |
| 18 | + if any(a in raw_lines[i] for a in actions): |
| 19 | + # Look ahead for Resource in next few lines |
| 20 | + block = "\n".join(raw_lines[i:i+5]) |
| 21 | + if any(r in block for r in resources): |
| 22 | + return i + 1 |
| 23 | + |
| 24 | + # Fallback: look for Effect + Action in block |
| 25 | + for i in range(len(raw_lines)): |
| 26 | + if f'"Effect": "{effect}"' in raw_lines[i] or f"'Effect': '{effect}'" in raw_lines[i]: |
| 27 | + block = "\n".join(raw_lines[i:i+10]) |
| 28 | + if any(a in block for a in actions) and any(r in block for r in resources): |
| 29 | + return i + 1 |
| 30 | + |
| 31 | + return None |
| 32 | + |
| 33 | +def check_wildcard_actions(policy, raw_lines=None): |
| 34 | + findings = [] |
2 | 35 | statements = policy.get("Statement", []) |
3 | 36 | if not isinstance(statements, list): |
4 | 37 | statements = [statements] |
| 38 | + |
5 | 39 | for stmt in statements: |
6 | 40 | if stmt.get("Effect", "Allow") != "Allow": |
7 | | - continue # Skip Deny statements |
| 41 | + continue |
| 42 | + |
8 | 43 | actions = stmt.get("Action", []) |
| 44 | + resources = stmt.get("Resource", []) |
| 45 | + |
9 | 46 | if isinstance(actions, str): |
10 | 47 | actions = [actions] |
11 | | - if any(a == "*" or a.endswith(":*") for a in actions): |
12 | | - return "Policy uses wildcard in Action, which is overly permissive." |
13 | | - return None |
| 48 | + if isinstance(resources, str): |
| 49 | + resources = [resources] |
14 | 50 |
|
15 | | -def check_passrole_wildcard(policy): |
| 51 | + for a in actions: |
| 52 | + if a == "*" or a.endswith(":*"): |
| 53 | + line_num = _find_statement_line(stmt, raw_lines) |
| 54 | + findings.append({ |
| 55 | + "id": "IAM001", |
| 56 | + "level": "high", |
| 57 | + "message": ( |
| 58 | + f"Policy uses overly permissive action '{a}' " |
| 59 | + + (f"with resource {resources}" if resources else "without resource scope") |
| 60 | + + (f". Statement starts at line {line_num}." if line_num else "") |
| 61 | + ) |
| 62 | + }) |
| 63 | + return findings |
| 64 | + |
| 65 | + |
| 66 | +def check_passrole_wildcard(policy, raw_lines=None): |
| 67 | + findings = [] |
16 | 68 | statements = policy.get("Statement", []) |
17 | 69 | if not isinstance(statements, list): |
18 | 70 | statements = [statements] |
| 71 | + |
19 | 72 | for stmt in statements: |
20 | 73 | if stmt.get("Effect", "Allow") != "Allow": |
21 | | - continue # Skip Deny statements |
| 74 | + continue |
| 75 | + |
22 | 76 | actions = stmt.get("Action", []) |
23 | 77 | resources = stmt.get("Resource", []) |
| 78 | + |
24 | 79 | if isinstance(actions, str): |
25 | 80 | actions = [actions] |
26 | 81 | if isinstance(resources, str): |
27 | 82 | resources = [resources] |
28 | | - if "iam:PassRole" in actions and "*" in resources: |
29 | | - return "iam:PassRole with wildcard resource can lead to privilege escalation." |
30 | | - return None |
| 83 | + |
| 84 | + if any(a.lower() == "iam:passrole" for a in actions) and "*" in resources: |
| 85 | + line_num = _find_statement_line(stmt, raw_lines) |
| 86 | + findings.append({ |
| 87 | + "id": "IAM002", |
| 88 | + "level": "high", |
| 89 | + "message": ( |
| 90 | + f"iam:PassRole with wildcard Resource ('*') can lead to privilege escalation." |
| 91 | + + (f" Statement starts at line {line_num}." if line_num else "") |
| 92 | + ) |
| 93 | + }) |
| 94 | + return findings |
31 | 95 |
|
32 | 96 | RULES = [ |
33 | 97 | { |
34 | 98 | "id": "IAM001", |
35 | 99 | "level": "high", |
36 | | - "description": "Wildcard in Action", |
37 | | - "check": check_wildcard_actions |
| 100 | + "description": "Wildcard in Action (e.g. * or service:*) is overly permissive", |
| 101 | + "check": check_wildcard_actions, |
38 | 102 | }, |
39 | 103 | { |
40 | 104 | "id": "IAM002", |
41 | 105 | "level": "high", |
42 | 106 | "description": "PassRole with wildcard Resource", |
43 | | - "check": check_passrole_wildcard |
44 | | - } |
| 107 | + "check": check_passrole_wildcard, |
| 108 | + }, |
45 | 109 | ] |
0 commit comments