-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudtrail_detector.py
More file actions
56 lines (45 loc) · 2.23 KB
/
Copy pathcloudtrail_detector.py
File metadata and controls
56 lines (45 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from __future__ import annotations
import argparse
from dataclasses import asdict
from src.aws_iam_detections import detect_aws_iam_risks
from src.cloudtrail_parser import load_cloudtrail
from src.rule_loader import evaluate_yaml_rules, load_yaml_rules
def main() -> None:
parser = argparse.ArgumentParser(description="Detect risky AWS IAM CloudTrail control-plane events.")
parser.add_argument("--file", required=True, help="Path to CloudTrail JSON file. Supports Records[], list, or single-record JSON.")
parser.add_argument("--engine", choices=["python", "yaml"], default="python", help="Detection engine to use. The Python engine is richer; the YAML engine demonstrates detection-as-code rules.")
parser.add_argument("--rules", default="rules/cloudtrail_iam_rules.yaml", help="YAML rule file used when --engine yaml is selected.")
parser.add_argument("--json", action="store_true", help="Print machine-readable JSON findings.")
args = parser.parse_args()
events = load_cloudtrail(args.file)
if args.engine == "yaml":
findings = evaluate_yaml_rules(events, load_yaml_rules(args.rules))
else:
findings = detect_aws_iam_risks(events)
if args.json:
import json
print(json.dumps([asdict(finding) for finding in findings], indent=2))
return
print(f"IdentityRiskGraph CloudTrail IAM Detector")
print(f"File: {args.file}")
print(f"Engine: {args.engine}")
print(f"Events parsed: {len(events)}")
print(f"Alerts: {len(findings)}")
print("=" * 72)
if not findings:
print("No risky IAM control-plane alerts detected.")
return
for finding in findings:
print(f"[{finding.severity.upper()}] {finding.detection_name}")
print(f"Event: {finding.event_name}")
print(f"User: {finding.target_identity}")
print(f"Actor: {finding.actor}")
print(f"Source IP: {finding.source_ip}")
if finding.evidence.get("policyArn"):
print(f"Policy: {finding.evidence['policyArn'].split('/')[-1]}")
print(f"Reason: {finding.reason}")
print(f"MITRE: {finding.mitre_technique}")
print(f"Recommended action: {finding.recommended_action}")
print("-" * 72)
if __name__ == "__main__":
main()