cmp.rego
  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
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# CMP policy evaluations =======================================================
# Controls : CMP-001, CMP-002, CMP-003, CMP-004, CMP-005, CMP-006, CMP-007,
#            CMP-008, CMP-009, CMP-011
#
# Co-emitted from ekm.rego:
#   CMP-010  → EKM-006  EBS Default Encryption (enabled flag check)

package controls.aws.cmp

import data.common.definitions as defs
import data.common.exemptions as exemptions
import data.common.network as network
import data.common.outputs as outputs

# CMP-001 — Require IMDSv2 on EC2 Instances -----------------------------------

deny contains msg if {
	some policy_id in {"ICP-TF-AWS-CMP-001", "ICP-TF-AWS-GAC-007"}

	some _, rc in input.resource_changes
	rc.type in {"aws_instance", "aws_launch_template"}
	rc.change.actions[_] in ["create", "update"]

	not _imdsv2_required(rc.change.after)

	meta := data.policies[policy_id]
	meta.status == "active"

	not exemptions.policy_exemption(policy_id)
	not exemptions.severity_exemption(meta.severity)

	msg := outputs.violation(
		policy_id, meta,
		sprintf("'%s.%s' does not require IMDSv2 (http_tokens != required)", [rc.type, rc.name]),
	)
}

_imdsv2_required(after) if {
	some opt in after.metadata_options
	opt.http_tokens == "required"
}

# CMP-002 — Restrict EC2 Metadata Hop Limit -----------------------------------

deny contains msg if {
	policy_id := "ICP-TF-AWS-CMP-002"

	some _, rc in input.resource_changes
	rc.type in {"aws_instance", "aws_launch_template"}
	rc.change.actions[_] in ["create", "update"]

	not _hop_limit_restricted(rc.change.after)

	meta := data.policies[policy_id]
	meta.status == "active"

	not exemptions.policy_exemption(policy_id)
	not exemptions.severity_exemption(meta.severity)

	msg := outputs.violation(
		policy_id, meta,
		sprintf("'%s.%s' metadata hop limit is not set to 1", [rc.type, rc.name]),
	)
}

_hop_limit_restricted(after) if {
	some opt in after.metadata_options
	opt.http_put_response_hop_limit == 1
}

# CMP-003 — Prohibit Public IPv4 Addresses on EC2 Instances -------------------

deny contains msg if {
	policy_id := "ICP-TF-AWS-CMP-003"

	some _, rc in input.resource_changes
	rc.type == "aws_instance"
	rc.change.actions[_] in ["create", "update"]

	rc.change.after.associate_public_ip_address == true

	meta := data.policies[policy_id]
	meta.status == "active"

	not exemptions.policy_exemption(policy_id)
	not exemptions.severity_exemption(meta.severity)

	msg := outputs.violation(
		policy_id, meta,
		sprintf("EC2 instance '%s' has a public IPv4 address assigned", [rc.name]),
	)
}

# CMP-004 — Prevent Auto-Assignment of Public IPs on Subnets ------------------

deny contains msg if {
	policy_id := "ICP-TF-AWS-CMP-004"

	some _, rc in input.resource_changes
	rc.type == "aws_subnet"
	rc.change.actions[_] in ["create", "update"]

	rc.change.after.map_public_ip_on_launch == true

	meta := data.policies[policy_id]
	meta.status == "active"

	not exemptions.policy_exemption(policy_id)
	not exemptions.severity_exemption(meta.severity)

	msg := outputs.violation(
		policy_id, meta,
		sprintf("Subnet '%s' auto-assigns public IPs on launch", [rc.name]),
	)
}

# CMP-005 — Prohibit Unrestricted SSH Ingress ---------------------------------

deny contains msg if {
	some policy_id in {"ICP-TF-AWS-CMP-005", "ICP-TF-AWS-NCD-001", "ICP-TF-AWS-GAC-017"}

	some _, rc in input.resource_changes
	rc.type in {"aws_security_group", "aws_vpc_security_group_ingress_rule"}
	rc.change.actions[_] in ["create", "update"]

	some rule in network.ingress_rules(rc)
	network.unrestricted_cidr(rule)
	network.port_in_range(22, rule)

	meta := data.policies[policy_id]
	meta.status == "active"

	not exemptions.policy_exemption(policy_id)
	not exemptions.severity_exemption(meta.severity)

	msg := outputs.violation(
		policy_id, meta,
		sprintf("'%s.%s' allows unrestricted SSH ingress (port 22) from the internet", [rc.type, rc.name]),
	)
}

# CMP-006 — Prohibit Unrestricted RDP Ingress ---------------------------------

deny contains msg if {
	some policy_id in {"ICP-TF-AWS-CMP-006", "ICP-TF-AWS-NCD-002", "ICP-TF-AWS-GAC-018"}

	some _, rc in input.resource_changes
	rc.type in {"aws_security_group", "aws_vpc_security_group_ingress_rule"}
	rc.change.actions[_] in ["create", "update"]

	some rule in network.ingress_rules(rc)
	network.unrestricted_cidr(rule)
	network.port_in_range(3389, rule)

	meta := data.policies[policy_id]
	meta.status == "active"

	not exemptions.policy_exemption(policy_id)
	not exemptions.severity_exemption(meta.severity)

	msg := outputs.violation(
		policy_id, meta,
		sprintf("'%s.%s' allows unrestricted RDP ingress (port 3389) from the internet", [rc.type, rc.name]),
	)
}

# CMP-007 — Restrict Unrestricted Ingress to High-Risk Ports ------------------

deny contains msg if {
	policy_id := "ICP-TF-AWS-CMP-007"

	some _, rc in input.resource_changes
	rc.type in {"aws_security_group", "aws_vpc_security_group_ingress_rule"}
	rc.change.actions[_] in ["create", "update"]

	some rule in network.ingress_rules(rc)
	network.unrestricted_cidr(rule)

	some port in defs.common_use_ports
	network.port_in_range(port, rule)

	meta := data.policies[policy_id]
	meta.status == "active"

	not exemptions.policy_exemption(policy_id)
	not exemptions.severity_exemption(meta.severity)

	msg := outputs.violation(
		policy_id, meta,
		sprintf("'%s.%s' allows unrestricted ingress to high-risk port %d from the internet", [rc.type, rc.name, port]),
	)
}

# CMP-008 — Require Empty Default Security Group Rules ------------------------

deny contains msg if {
	some policy_id in {"ICP-TF-AWS-CMP-008", "ICP-TF-AWS-NCD-004", "ICP-TF-AWS-GAC-019"}

	some _, rc in input.resource_changes
	rc.type == "aws_default_security_group"
	rc.change.actions[_] in ["create", "update"]

	_has_rules(rc.change.after)

	meta := data.policies[policy_id]
	meta.status == "active"

	not exemptions.policy_exemption(policy_id)
	not exemptions.severity_exemption(meta.severity)

	msg := outputs.violation(
		policy_id, meta,
		sprintf("Default security group '%s' has ingress or egress rules defined", [rc.name]),
	)
}

_has_rules(after) if count(after.ingress) > 0
_has_rules(after) if count(after.egress) > 0

# CMP-009 — Require Encryption on Attached EBS Volumes ------------------------

deny contains msg if {
	some policy_id in {"ICP-TF-AWS-CMP-009", "ICP-TF-AWS-GAC-005"}

	some _, rc in input.resource_changes
	rc.type == "aws_ebs_volume"
	rc.change.actions[_] in ["create", "update"]

	rc.change.after.encrypted != true

	meta := data.policies[policy_id]
	meta.status == "active"

	not exemptions.policy_exemption(policy_id)
	not exemptions.severity_exemption(meta.severity)

	msg := outputs.violation(
		policy_id, meta,
		sprintf("EBS volume '%s' is not encrypted", [rc.name]),
	)
}

deny contains msg if {
	some policy_id in {"ICP-TF-AWS-CMP-009", "ICP-TF-AWS-GAC-005"}

	some _, rc in input.resource_changes
	rc.type == "aws_instance"
	rc.change.actions[_] in ["create", "update"]

	some dev in rc.change.after.ebs_block_device
	dev.encrypted != true

	meta := data.policies[policy_id]
	meta.status == "active"

	not exemptions.policy_exemption(policy_id)
	not exemptions.severity_exemption(meta.severity)

	msg := outputs.violation(
		policy_id, meta,
		sprintf("EBS block device on instance '%s' is not encrypted", [rc.name]),
	)
}

# CMP-011 — Prohibit Privileged ECS Containers --------------------------------

deny contains msg if {
	policy_id := "ICP-TF-AWS-CMP-011"

	some _, rc in input.resource_changes
	rc.type == "aws_ecs_task_definition"
	rc.change.actions[_] in ["create", "update"]

	container_defs := json.unmarshal(rc.change.after.container_definitions)
	some container in container_defs
	container.privileged == true

	meta := data.policies[policy_id]
	meta.status == "active"

	not exemptions.policy_exemption(policy_id)
	not exemptions.severity_exemption(meta.severity)

	msg := outputs.violation(
		policy_id, meta,
		sprintf("ECS container '%s' in task '%s' runs in privileged mode", [container.name, rc.name]),
	)
}