Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package Cx

import data.generic.terraform as tf_lib
port_fields := ["destination_port_ranges","destination_port_range"]

CxPolicy[result] {
resource := input.document[i].resource.azurerm_network_security_rule[var0]
upper(resource.access) == "ALLOW"
upper(resource.direction) == "INBOUND"

isRelevantProtocol(resource.protocol)
isRelevantPort(resource.destination_port_range)
isRelevantPort(resource[port_fields[i2]])
isRelevantAddressPrefix(resource.source_address_prefix)

result := {
"documentId": input.document[i].id,
"resourceType": "azurerm_network_security_rule",
"resourceName": tf_lib.get_resource_name(resource, var0),
"searchKey": sprintf("azurerm_network_security_rule[%s].destination_port_range", [var0]),
"searchKey": sprintf("azurerm_network_security_rule[%s].%s", [var0,port_fields[i2]]),
"issueType": "IncorrectValue",
"keyExpectedValue": sprintf("'azurerm_network_security_rule.%s.destination_port_range' cannot be 3389", [var0]),
"keyActualValue": sprintf("'azurerm_network_security_rule.%s.destination_port_range' might be 3389", [var0]),
"keyExpectedValue": sprintf("'azurerm_network_security_rule.%s.%s' cannot be 3389", [var0,port_fields[i2]]),
"keyActualValue": sprintf("'azurerm_network_security_rule.%s.%s' might be 3389", [var0,port_fields[i2]]),
}
}

Expand All @@ -30,62 +31,47 @@ CxPolicy[result] {
upper(rule.direction) == "INBOUND"

isRelevantProtocol(rule.protocol)
isRelevantPort(rule.destination_port_range)
isRelevantPort(rule[port_fields[i2]])
isRelevantAddressPrefix(rule.source_address_prefix)

result := {
"documentId": input.document[i].id,
"resourceType": "azurerm_network_security_group",
"resourceName": tf_lib.get_resource_name(rule, [groupName, "security_rule", idx]),
"searchKey": sprintf("azurerm_network_security_group[%s].security_rule.name={{%s}}.destination_port_range", [groupName, rule.name]),
"searchKey": sprintf("azurerm_network_security_group[%s].security_rule.name={{%s}}.%s", [groupName, rule.name, port_fields[i2]]),
"issueType": "IncorrectValue",
"keyExpectedValue": "'destination_port_range' cannot be 3389",
"keyActualValue": "'destination_port_range' might be 3389",
"keyExpectedValue": sprintf("'%s' cannot be 3389", [port_fields[i2]]),
"keyActualValue": sprintf("'%s' might be 3389", [port_fields[i2]]),
}
}



isRelevantProtocol(protocol) = allow {
upper(protocol) != "UDP"
isRelevantProtocol(protocol) = true {
upper(protocol) != "ICMP"
allow = true
}

isRelevantPort(port) = allow {
isRelevantPort(port) = true {
regex.match("(^|\\s|,)3389(-|,|$|\\s)", port)
allow = true
}

else = allow {
} else = true {
ports = split(port, ",")
sublist = split(ports[var], "-")
sublist = split(ports[i], "-")
to_number(trim(sublist[0], " ")) <= 3389
to_number(trim(sublist[1], " ")) >= 3389
} else = true {
regex.match("(^|\\s|,)3389(-|,|$|\\s)", port[i])
} else = true {
sublist = split(port[i], "-")
to_number(trim(sublist[0], " ")) <= 3389
to_number(trim(sublist[1], " ")) >= 3389
allow = true
}

isRelevantAddressPrefix(prefix) = allow {
isRelevantAddressPrefix(prefix) = true {
prefix == "*"
allow = true
}

else = allow {
} else = true {
prefix == "0.0.0.0"
allow = true
}

else = allow {
} else = true {
endswith(prefix, "/0")
allow = true
}

else = allow {
} else = true {
prefix == "internet"
allow = true
}

else = allow {
} else = true {
prefix == "any"
allow = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ resource "azurerm_network_security_rule" "negative1" {
}

resource "azurerm_network_security_rule" "negative2" {
name = "example"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "UDP"
source_port_range = "*"
destination_port_range = "2000-5000"
source_address_prefix = "*"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.example.name
network_security_group_name = azurerm_network_security_group.example.name
}


resource "azurerm_network_security_rule" "negative3" {
name = "example"
priority = 100
direction = "Inbound"
Expand All @@ -42,7 +27,7 @@ resource "azurerm_network_security_rule" "negative3" {
}


resource "azurerm_network_security_rule" "negative4" {
resource "azurerm_network_security_rule" "negative3" {
name = "example"
priority = 100
direction = "Inbound"
Expand All @@ -57,7 +42,7 @@ resource "azurerm_network_security_rule" "negative4" {
}


resource "azurerm_network_security_rule" "negative5" {
resource "azurerm_network_security_rule" "negative4" {
name = "example"
priority = 100
direction = "Inbound"
Expand All @@ -71,7 +56,7 @@ resource "azurerm_network_security_rule" "negative5" {
network_security_group_name = azurerm_network_security_group.example.name
}

resource "azurerm_network_security_rule" "negative6" {
resource "azurerm_network_security_rule" "negative5" {
name = "example"
priority = 100
direction = "Inbound"
Expand All @@ -85,23 +70,7 @@ resource "azurerm_network_security_rule" "negative6" {
network_security_group_name = azurerm_network_security_group.example.name
}


resource "azurerm_network_security_rule" "negative7" {
name = "example"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "UDP"
source_port_range = "*"
destination_port_range = "3389"
source_address_prefix = "internet"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.example.name
network_security_group_name = azurerm_network_security_group.example.name
}


resource "azurerm_network_security_rule" "negative8" {
resource "azurerm_network_security_rule" "negative6" {
name = "example"
priority = 100
direction = "Inbound"
Expand All @@ -115,22 +84,7 @@ resource "azurerm_network_security_rule" "negative8" {
network_security_group_name = azurerm_network_security_group.example.name
}


resource "azurerm_network_security_rule" "negative9" {
name = "example"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "UDP"
source_port_range = "*"
destination_port_range = "3389"
source_address_prefix = "/0"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.example.name
network_security_group_name = azurerm_network_security_group.example.name
}

resource "azurerm_network_security_rule" "negative10" {
resource "azurerm_network_security_rule" "negative7" {
name = "example"
priority = 100
direction = "Inbound"
Expand All @@ -144,7 +98,7 @@ resource "azurerm_network_security_rule" "negative10" {
network_security_group_name = azurerm_network_security_group.example.name
}

resource "azurerm_network_security_rule" "negative11" {
resource "azurerm_network_security_rule" "negative8" {
name = "example"
priority = 100
direction = "Inbound"
Expand All @@ -158,13 +112,13 @@ resource "azurerm_network_security_rule" "negative11" {
network_security_group_name = azurerm_network_security_group.example.name
}

resource azurerm_network_security_group "negative12-22" {
resource azurerm_network_security_group "negative9-17" {
location = var.location
name = "terragoat-${var.environment}"
resource_group_name = azurerm_resource_group.example.name

security_rule {
name = "negative12"
name = "negative9"
priority = 100
direction = "Inbound"
access = "Deny"
Expand All @@ -178,22 +132,7 @@ resource azurerm_network_security_group "negative12-22" {
}

security_rule {
name = "negative13"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "UDP"
source_port_range = "*"
destination_port_range = "2000-5000"
source_address_prefix = "*"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.example.name
network_security_group_name = azurerm_network_security_group.example.name
}


security_rule {
name = "negative14"
name = "negative10"
priority = 100
direction = "Inbound"
access = "Allow"
Expand All @@ -208,7 +147,7 @@ resource azurerm_network_security_group "negative12-22" {


security_rule {
name = "negative15"
name = "negative11"
priority = 100
direction = "Inbound"
access = "Allow"
Expand All @@ -223,7 +162,7 @@ resource azurerm_network_security_group "negative12-22" {


security_rule {
name = "negative16"
name = "negative12"
priority = 100
direction = "Inbound"
access = "Allow"
Expand All @@ -237,7 +176,7 @@ resource azurerm_network_security_group "negative12-22" {
}

security_rule {
name = "negative17"
name = "negative13"
priority = 100
direction = "Inbound"
access = "Allow"
Expand All @@ -250,24 +189,8 @@ resource azurerm_network_security_group "negative12-22" {
network_security_group_name = azurerm_network_security_group.example.name
}


security_rule {
name = "negative18"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "UDP"
source_port_range = "*"
destination_port_range = "3389"
source_address_prefix = "internet"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.example.name
network_security_group_name = azurerm_network_security_group.example.name
}


security_rule {
name = "negative19"
name = "negative14"
priority = 100
direction = "Inbound"
access = "Allow"
Expand All @@ -280,46 +203,57 @@ resource azurerm_network_security_group "negative12-22" {
network_security_group_name = azurerm_network_security_group.example.name
}


security_rule {
name = "negative20"
name = "negative15"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "UDP"
protocol = "TCP"
source_port_range = "*"
destination_port_range = "3389"
source_address_prefix = "/0"
destination_port_range = "3389 , 3390"
source_address_prefix = "0.0.1.0"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.example.name
network_security_group_name = azurerm_network_security_group.example.name
}

security_rule {
name = "negative20"
name = "negative16"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "TCP"
source_port_range = "*"
destination_port_range = "3389 , 3390"
source_address_prefix = "0.0.1.0"
destination_port_range = "338,389"
source_address_prefix = "0.0.0.0"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.example.name
network_security_group_name = azurerm_network_security_group.example.name
}

security_rule {
name = "negative21"
priority = 100
name = "negative17"
priority = 114
direction = "Inbound"
access = "Allow"
protocol = "TCP"
protocol = "Udp"
source_port_range = "*"
destination_port_range = "338,389"
source_address_prefix = "0.0.0.0"
destination_port_range = "1000-1200"
source_address_prefix = "*"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.example.name
network_security_group_name = azurerm_network_security_group.example.name
}
}

resource "azurerm_network_security_rule" "negative18" {
name = "negative18"
priority = 114
direction = "Inbound"
access = "Allow"
protocol = "Udp"
source_port_range = "*"
destination_port_range = "1000-2000"
source_address_prefix = "*"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.example.name
network_security_group_name = azurerm_network_security_group.example.name
}
Loading
Loading