Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(azure): properly check ports in AVD-AZU-0058 and AVD-AZU-0050 #268

Merged
merged 1 commit into from
Oct 10, 2024
Merged
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
5 changes: 4 additions & 1 deletion checks/cloud/azure/network/disable_rdp_from_internet.rego
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ deny contains res if {
)
}

port_range_includes(from, to, port) if from <= port <= to
port_range_includes(from, to, port) if {
from.value <= port
port <= to.value
}
Comment on lines -52 to +55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious is this valid:

if from.value <= port <= to.value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of expression doesn't work in Rego: https://play.openpolicyagent.org/p/2RS5HYLT5d

28 changes: 22 additions & 6 deletions checks/cloud/azure/network/disable_rdp_from_internet_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ test_deny_inbound_rule_allows_rdp_access_from_internet if {
"protocol": {"value": "Tcp"},
"sourceaddresses": [{"value": "*"}],
"destinationports": [{
"start": 3310,
"end": 3390,
"start": {"value": 3310},
"end": {"value": 3390},
}],
}]}]}}}

Expand All @@ -28,8 +28,8 @@ test_allow_inbound_rule_allow_rdp_access_from_specific_address if {
"protocol": {"value": "Tcp"},
"sourceaddresses": [{"value": "237.84.2.178"}],
"destinationports": [{
"start": 3310,
"end": 3390,
"start": {"value": 3310},
"end": {"value": 3390},
}],
}]}]}}}

Expand All @@ -44,8 +44,24 @@ test_allow_inbound_rule_allow_access_for_icmp if {
"protocol": {"value": "Icmp"},
"sourceaddresses": [{"value": "*"}],
"destinationports": [{
"start": 3310,
"end": 3390,
"start": {"value": 3310},
"end": {"value": 3390},
}],
}]}]}}}

res := check.deny with input as inp
res == set()
}

test_allow_inbound_rule_allow_access_for_non_rdp_port if {
inp := {"azure": {"network": {"securitygroups": [{"rules": [{
"outbound": {"value": false},
"allow": {"value": true},
"protocol": {"value": "Icmp"},
"sourceaddresses": [{"value": "*"}],
"destinationports": [{
"start": {"value": 8080},
"end": {"value": 8080},
}],
}]}]}}}

Expand Down
5 changes: 4 additions & 1 deletion checks/cloud/azure/network/ssh_blocked_from_internet.rego
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ deny contains res if {
)
}

port_range_includes(from, to, port) if from <= port <= to
port_range_includes(from, to, port) if {
from.value <= port
port <= to.value
}
28 changes: 22 additions & 6 deletions checks/cloud/azure/network/ssh_blocked_from_internet_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ test_deny_inbound_rule_allows_rdp_access_from_internet if {
"protocol": {"value": "Tcp"},
"sourceaddresses": [{"value": "*"}],
"destinationports": [{
"start": 22,
"end": 22,
"start": {"value": 22},
"end": {"value": 22},
}],
}]}]}}}

Expand All @@ -28,8 +28,8 @@ test_allow_inbound_rule_allow_rdp_access_from_specific_address if {
"protocol": {"value": "Tcp"},
"sourceaddresses": [{"value": "237.84.2.178"}],
"destinationports": [{
"start": 22,
"end": 22,
"start": {"value": 22},
"end": {"value": 22},
}],
}]}]}}}

Expand All @@ -44,8 +44,24 @@ test_allow_inbound_rule_allow_access_for_icmp if {
"protocol": {"value": "Icmp"},
"sourceaddresses": [{"value": "*"}],
"destinationports": [{
"start": 22,
"end": 22,
"start": {"value": 22},
"end": {"value": 22},
}],
}]}]}}}

res := check.deny with input as inp
res == set()
}

test_allow_inbound_rule_allow_access_for_non_ssh_port if {
inp := {"azure": {"network": {"securitygroups": [{"rules": [{
"outbound": {"value": false},
"allow": {"value": true},
"protocol": {"value": "Tcp"},
"sourceaddresses": [{"value": "*"}],
"destinationports": [{
"start": {"value": 8080},
"end": {"value": 8080},
}],
}]}]}}}

Expand Down
60 changes: 60 additions & 0 deletions test/rego/azure_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,37 @@ var azureNetworkTestCases = testCases{
}}},
expected: false,
},
{
name: "Security group inbound rule allowing non RDP access from public internet",
input: state.State{Azure: azure.Azure{Network: network.Network{
SecurityGroups: []network.SecurityGroup{
{
Metadata: trivyTypes.NewTestMetadata(),
Rules: []network.SecurityGroupRule{
{
Metadata: trivyTypes.NewTestMetadata(),
Outbound: trivyTypes.Bool(false, trivyTypes.NewTestMetadata()),
Allow: trivyTypes.Bool(true, trivyTypes.NewTestMetadata()),
SourceAddresses: []trivyTypes.StringValue{
trivyTypes.String("*", trivyTypes.NewTestMetadata()),
},
SourcePorts: nil,
DestinationAddresses: nil,
DestinationPorts: []network.PortRange{
{
Metadata: trivyTypes.NewTestMetadata(),
Start: trivyTypes.IntTest(8080),
End: trivyTypes.IntTest(8080),
},
},
Protocol: trivyTypes.String("Tcp", trivyTypes.NewTestMetadata()),
},
},
},
},
}}},
expected: false,
},
},
"AVD-AZU-0051": {
{
Expand Down Expand Up @@ -302,6 +333,35 @@ var azureNetworkTestCases = testCases{
}}},
expected: false,
},
{
name: "Security group rule allowing non SSH access from the public internet",
input: state.State{Azure: azure.Azure{Network: network.Network{
SecurityGroups: []network.SecurityGroup{
{
Metadata: trivyTypes.NewTestMetadata(),
Rules: []network.SecurityGroupRule{
{
Metadata: trivyTypes.NewTestMetadata(),
Allow: trivyTypes.Bool(true, trivyTypes.NewTestMetadata()),
Outbound: trivyTypes.Bool(false, trivyTypes.NewTestMetadata()),
DestinationPorts: []network.PortRange{
{
Metadata: trivyTypes.NewTestMetadata(),
Start: trivyTypes.IntTest(8080),
End: trivyTypes.IntTest(8080),
},
},
SourceAddresses: []trivyTypes.StringValue{
trivyTypes.String("82.102.23.23", trivyTypes.NewTestMetadata()),
},
Protocol: trivyTypes.String("Tcp", trivyTypes.NewTestMetadata()),
},
},
},
},
}}},
expected: false,
},
{
name: "Security group rule allowing SSH access from a specific address",
input: state.State{Azure: azure.Azure{Network: network.Network{
Expand Down