forked from aws-samples/cloud-trail-lake-query-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2-security-historical-changes.sql
47 lines (40 loc) · 1.51 KB
/
ec2-security-historical-changes.sql
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
/*
This query returns historical changes of security groups. This is useful when you are auditing / investigating
changes made to security groups.
Notice that there are two queries below that are being combined using the "UNION ALL" clause.
The first query pulls the AuthorizeSecurityGroupIngress events (for newly created security group rules).
The 2nd query pulls the ModifySecurityGroupRulesRequest (for modification on security group rules).
*/
-- This part of the query returns AuthorizeSecurityGroupIngress events
SELECT
element_at(requestParameters, 'groupId') AS securityGroup,
eventName,
eventTime,
element_at(requestParameters, 'ipPermissions') AS securityGroupRule,
userIdentity.arn AS user,
sourceIPAddress,
eventSource
FROM
<event_data_store_id>
WHERE
eventTime >= '2023-07-06 00:00:00'
AND eventTime <= '2023-07-08 00:00:00'
AND eventName = 'AuthorizeSecurityGroupIngress'
UNION ALL
-- This part of the query returns ModifySecurityGroupRulesRequest events
SELECT
json_extract_scalar(element_at(requestParameters, 'ModifySecurityGroupRulesRequest'), '$.GroupId') securityGroup,
eventName,
eventTime,
element_at(requestParameters, 'ModifySecurityGroupRulesRequest') securityGroupRule,
userIdentity.arn AS user,
sourceIPAddress,
eventSource
FROM
<event_data_store_id>
WHERE
eventTime >= '2023-07-06 00:00:00'
AND eventTime <= '2023-07-09 00:00:00'
AND eventName = 'ModifySecurityGroupRules'
ORDER BY securityGroup,
eventTime