Skip to content
Merged
Show file tree
Hide file tree
Changes from 94 commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
2359dd0
fix(query): fn for remote desktop port open to internet--terraform/aws
cx-andre-pereira Aug 13, 2025
ae196dc
fix expected results
cx-andre-pereira Aug 13, 2025
7fc034b
typo cleanup
cx-andre-pereira Aug 13, 2025
f7dda09
Merge branch 'master' of https://github.com/Checkmarx/kics into AST-1…
cx-andre-pereira Aug 14, 2025
ecb0826
initial changes
cx-andre-pereira Aug 14, 2025
6d262f4
support for generic security group
cx-andre-pereira Aug 14, 2025
06158e9
fix negative tests
cx-andre-pereira Aug 14, 2025
3b75a22
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Aug 14, 2025
71f7571
some changes
cx-andre-pereira Aug 14, 2025
c34f4a7
Merge branch 'master' of https://github.com/Checkmarx/kics into AST-1…
cx-andre-pereira Aug 14, 2025
260e0c3
'all' protocol support
cx-andre-pereira Aug 14, 2025
78d8048
Merge branch 'AST-109541--FN-Remote_Desktop_Port_Open_To_Internet--te…
cx-andre-pereira Aug 14, 2025
a42b55a
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Aug 14, 2025
f402647
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-artur-ribeiro Aug 18, 2025
1e4ca51
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-artur-ribeiro Aug 18, 2025
be15c4f
Merge branch 'master' of https://github.com/Checkmarx/kics into AST-1…
cx-andre-pereira Aug 18, 2025
dd52de9
Merge branch 'master' of https://github.com/Checkmarx/kics into AST-1…
cx-andre-pereira Aug 18, 2025
879aeb1
Merge branch 'AST-109542--FN-Sensitive_Port_Is_Exposed_To_Entire_Netw…
cx-andre-pereira Aug 18, 2025
8a9a479
Merge branch 'AST-109541--FN-Remote_Desktop_Port_Open_To_Internet--te…
cx-andre-pereira Aug 18, 2025
2248c56
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-artur-ribeiro Aug 18, 2025
9be511f
senstive port exposed positive tests and expected resuts
cx-andre-pereira Aug 18, 2025
58867a2
sensitive port negative tests
cx-andre-pereira Aug 18, 2025
6c429d5
Merge branch 'AST-109541--FN-Remote_Desktop_Port_Open_To_Internet--te…
cx-andre-pereira Aug 18, 2025
63fc782
typo fix
cx-andre-pereira Aug 18, 2025
ce7124e
vpc ingress rules for unrestricted sec group index
cx-andre-pereira Aug 18, 2025
ebaa7b7
implemented vpc security ingress rule and generic rule on security gr…
cx-andre-pereira Aug 18, 2025
3f51fee
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Aug 18, 2025
e25d983
added security group rules and ingres rules to senistive port is expo…
cx-andre-pereira Aug 18, 2025
ee41905
fixed accidentale negative test change
cx-andre-pereira Aug 19, 2025
c8a9383
small test fix
cx-andre-pereira Aug 19, 2025
c60e93d
Merge branch 'master' of https://github.com/Checkmarx/kics into AST-1…
cx-andre-pereira Aug 19, 2025
5aad693
tests for Ingress array on 'sensitive port exposed' queries and sensi…
cx-andre-pereira Aug 19, 2025
b871783
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Aug 19, 2025
75630c0
improved search line and key
cx-andre-pereira Aug 19, 2025
952177d
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Aug 19, 2025
f1e5b30
missing searchValue on sensitive port exposed to wide private network…
cx-andre-pereira Aug 19, 2025
23becaf
Merge branch 'master' of https://github.com/Checkmarx/kics into AST-1…
cx-andre-pereira Aug 19, 2025
fc6f282
added support for aws vpc securituy ingress and egress rules for secu…
cx-andre-pereira Aug 19, 2025
e95c89d
added sec group rules, ingress rules and inline ingress array support…
cx-andre-pereira Aug 19, 2025
f8fc5b1
Merge branch 'master' of https://github.com/Checkmarx/kics into AST-1…
cx-andre-pereira Aug 20, 2025
e36a08b
better ingress array/ single ingress support and support for security…
cx-andre-pereira Aug 20, 2025
d835303
reducing CxPolicy ammount 1
cx-andre-pereira Aug 20, 2025
32ef6a8
reducing CxPolicy amount 2
cx-andre-pereira Aug 21, 2025
432b57a
Merge branch 'master' of https://github.com/Checkmarx/kics into AST-1…
cx-andre-pereira Aug 21, 2025
1246d2a
fix array was treated as single element
cx-andre-pereira Aug 21, 2025
36c7039
reducing CxPolicy amount 3
cx-andre-pereira Aug 21, 2025
8b1d05d
reducing CxPolicy amount 4
cx-andre-pereira Aug 21, 2025
d858b4b
mend
cx-andre-pereira Aug 21, 2025
d2c9a8a
added serachValue back (removed accidently)
cx-andre-pereira Aug 21, 2025
f796880
better key values for unrestricted sec group ing and fix 2 for sensit…
cx-andre-pereira Aug 21, 2025
c5b1e97
refactor for unrestricted security group ingress for ingress array vs…
cx-andre-pereira Aug 21, 2025
ea217fd
newlines removed
cx-andre-pereira Aug 21, 2025
b884858
added support for aws_security_group modules to sensitive port expose…
cx-andre-pereira Aug 21, 2025
0196de7
fix query name/severity in results
cx-andre-pereira Aug 21, 2025
3ebf0f8
fixed all invalid 'modules' in security group with unrestricted acc t…
cx-andre-pereira Aug 21, 2025
26afa30
added module support for http port open
cx-andre-pereira Aug 21, 2025
1522b47
added module support for remote desktop port open to internet
cx-andre-pereira Aug 21, 2025
7aef934
added 'security group' module support for sec group without descripti…
cx-andre-pereira Aug 21, 2025
d2dbfff
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Aug 21, 2025
513c0d1
module support for sql analysis services port 2383 is publicly access…
cx-andre-pereira Aug 21, 2025
e276ab4
module support for unknown port exposed to internet
cx-andre-pereira Aug 21, 2025
2cebe85
improved module support for unrestricted security group ingress, now …
cx-andre-pereira Aug 22, 2025
44d4ff9
Merge branch 'master' of https://github.com/Checkmarx/kics into AST-1…
cx-andre-pereira Aug 22, 2025
102238f
standardized unrestricted security group ingress tests, fixed module …
cx-andre-pereira Aug 22, 2025
e20cbeb
new test on positive3 and newline cleanup
cx-andre-pereira Aug 22, 2025
ba12538
tests changes
cx-andre-pereira Aug 22, 2025
b07d3c4
Merge branch 'master' of https://github.com/Checkmarx/kics into AST-1…
cx-andre-pereira Aug 22, 2025
5bb7027
ipv6 support for modules for http port open and new tests
cx-andre-pereira Aug 22, 2025
b790686
standardized testing for 'port x' open queries and added ipv6 module …
cx-andre-pereira Aug 22, 2025
64990a9
fix tests
cx-andre-pereira Aug 22, 2025
11e095f
added support for ipv6 cidr blocks in modules to security group rules…
cx-andre-pereira Aug 22, 2025
69be694
ipv6 support for modules for unknown port exposed to internet and tests
cx-andre-pereira Aug 25, 2025
25ce160
Merge branch 'master' of https://github.com/Checkmarx/kics into AST-1…
cx-andre-pereira Aug 25, 2025
9c64c60
added module ipv6 support for sensitive_port_is_exposed_to_x queries
cx-andre-pereira Aug 25, 2025
2731180
standardizing tests/ queries 1
cx-andre-pereira Aug 25, 2025
07f3ee6
support for wide private ipv6 networks and small public ipv6 networks…
cx-andre-pereira Aug 25, 2025
a69c280
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Aug 25, 2025
4062117
x port publicly accessible/open to internet type queries tests standa…
cx-andre-pereira Aug 25, 2025
7a44cdd
final adjustments for 'without description' queries
cx-andre-pereira Aug 25, 2025
79e32ea
expected results fix and security_group_rules_without_description / u…
cx-andre-pereira Aug 25, 2025
5227a45
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-artur-ribeiro Aug 26, 2025
9b6bb96
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Aug 26, 2025
bc3b8d4
Merge branch 'master' of https://github.com/Checkmarx/kics into AST-1…
cx-andre-pereira Aug 26, 2025
14bf639
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Aug 27, 2025
f209843
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Sep 3, 2025
f0dff82
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Sep 4, 2025
0392384
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Sep 11, 2025
3971505
Merge branch 'AST-109541--FN-Remote_Desktop_Port_Open_To_Internet--te…
cx-andre-pereira Sep 11, 2025
3ce95d2
requested changes
cx-andre-pereira Sep 11, 2025
a700be2
fix cidr_is_unmasked
cx-andre-pereira Sep 11, 2025
0e251c4
removed unecessary variable declaration
cx-andre-pereira Sep 16, 2025
ca77f82
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-artur-ribeiro Sep 16, 2025
5439e06
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Sep 16, 2025
c66d914
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Sep 16, 2025
4b0b34a
fix attempt 1
cx-andre-pereira Sep 16, 2025
24678a5
Merge branch 'AST-109541--FN-Remote_Desktop_Port_Open_To_Internet--te…
cx-andre-pereira Sep 16, 2025
a4e8e01
fix attempt 2
cx-andre-pereira Sep 16, 2025
24f8cac
Merge branch 'master' into AST-109541--FN-Remote_Desktop_Port_Open_To…
cx-andre-pereira Sep 16, 2025
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
6 changes: 3 additions & 3 deletions assets/libraries/common.rego
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ emptyOrNull(null) = true

# Checks if an IP is private
isPrivateIP(ipVal) {
private_ips := ["10.0.0.0/8", "192.168.0.0/16", "172.16.0.0/12"]
private_ips := ["10.0.0.0/8", "192.168.0.0/16", "172.16.0.0/12", "fc00::/8", "fd00::/8"]
some i
net.cidr_contains(private_ips[i], ipVal)
}
Expand Down Expand Up @@ -462,7 +462,7 @@ is_recommended_tls(field) {
}

is_unrestricted(sourceRange) {
cidrs := {"0.0.0.0/0", "::/0"}
cidrs := {"0.0.0.0/0", "::/0", "0000:0000:0000:0000:0000:0000:0000:0000/0", "0:0:0:0:0:0:0:0/0"}
sourceRange == cidrs[_]
}

Expand Down Expand Up @@ -749,4 +749,4 @@ valid_non_empty_key(field, key) = output {
keyObj := field[key]
keyObj == ""
output := concat(".", ["", key])
}
}
64 changes: 55 additions & 9 deletions assets/libraries/terraform.rego
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,85 @@ package generic.terraform

import data.generic.common as common_lib

unrestricted_ipv6 := ["::/0","0000:0000:0000:0000:0000:0000:0000:0000/0","0:0:0:0:0:0:0:0/0"]

check_cidr(rule) {
rule.cidr_blocks[_] == "0.0.0.0/0"
} else {
rule.cidr_block == "0.0.0.0/0"
} else {
rule.ipv6_cidr_blocks[_] == "::/0"
rule.ipv6_cidr_blocks[_] == unrestricted_ipv6[_]
} else {
rule.ipv6_cidr_blocks == "::/0"
rule.ipv6_cidr_blocks == unrestricted_ipv6[_]
} else {
rule.ipv6_cidr_blocks[_] == "0000:0000:0000:0000:0000:0000:0000:0000/0"
rule.cidr_ipv4 == "0.0.0.0/0"
} else {
rule.ipv6_cidr_blocks == "0000:0000:0000:0000:0000:0000:0000:0000/0"
rule.cidr_ipv6 == unrestricted_ipv6[_]
}

is_security_group_ingress(type,resource) {
type == "aws_security_group_rule"
resource.type == "ingress"
} else {
rule.ipv6_cidr_blocks[_] == "0:0:0:0:0:0:0:0/0"
type == "aws_vpc_security_group_ingress_rule"
}

cidr_sources := [
"cidr_blocks",
"ipv6_cidr_blocks",
"cidr_ipv4",
"cidr_ipv6"
]

cidr_is_unmasked(resource) {
#security_group_rule or in security_group ingress field
endswith(resource[cidr_sources[_]][_], "/0")
} else {
rule.ipv6_cidr_blocks == "0:0:0:0:0:0:0:0/0"
#security_group_ingress_rule
endswith(resource[cidr_sources[_]], "/0")
}

prot_types := ["protocol","ip_protocol"]
open_port := ["all","-1"]

# Checks if a TCP port is open in a rule
# Checks if a TCP port is open
portOpenToInternet(rule, port) {
check_cidr(rule)
rule.protocol == "tcp"
rule[prot_types[_]] == "tcp"
containsPort(rule, port)
}

portOpenToInternet(rules, port) {
rule := rules[_]
check_cidr(rule)
rule.protocol == "tcp"
rule[prot_types[_]] == "tcp"
containsPort(rule, port)
}

portOpenToInternet(rule, port) {
check_cidr(rule)
rule[prot_types[_]] == open_port[_]
}

portOpenToInternet(rules, port) {
rule := rules[_]
check_cidr(rule)
rule[prot_types[_]] == open_port[_]
}

get_ingress_list(ingress) = result {
is_array(ingress)
result := {
"value" : ingress,
"is_unique_element" : false
}
} else = result {
result := {
"value" : [ingress],
"is_unique_element" : true
}
}

# Checks if a port is included in a rule
containsPort(rule, port) {
rule.from_port <= port
Expand Down
78 changes: 74 additions & 4 deletions assets/queries/terraform/aws/http_port_open/query.rego
Original file line number Diff line number Diff line change
@@ -1,19 +1,89 @@
package Cx

import data.generic.terraform as tf_lib
import data.generic.common as common_lib

CxPolicy[result] {
#Case of "aws_vpc_security_group_ingress_rule" or "aws_security_group_rule"
types := ["aws_vpc_security_group_ingress_rule","aws_security_group_rule"]
resource := input.document[i].resource[types[i2]][name]

tf_lib.is_security_group_ingress(types[i2],resource)
tf_lib.portOpenToInternet(resource, 80)

result := {
"documentId": input.document[i].id,
"resourceType": types[i2],
"resourceName": tf_lib.get_resource_name(resource, name),
"searchKey": sprintf("%s[%s]", [types[i2],name]),
"issueType": "IncorrectValue",
"keyExpectedValue": sprintf("%s[%s] should not open the HTTP port (80)", [types[i2],name]),
"keyActualValue": sprintf("%s[%s] opens the HTTP port (80)", [types[i2],name]),
"searchLine": common_lib.build_search_line(["resource", types[i2], name], []),
}
}

CxPolicy[result] {
#Case of "aws_security_group"
resource := input.document[i].resource.aws_security_group[name]

tf_lib.portOpenToInternet(resource.ingress, 80)
ingress_list := tf_lib.get_ingress_list(resource.ingress)
results := http_is_open(ingress_list.value[i2],ingress_list.is_unique_element,name,i2)
results != ""

result := {
"documentId": input.document[i].id,
"resourceType": "aws_security_group",
"resourceName": tf_lib.get_resource_name(resource, name),
"searchKey": sprintf("aws_security_group[%s]", [name]),
"searchKey": results.searchKey,
"issueType": "IncorrectValue",
"keyExpectedValue": results.keyExpectedValue,
"keyActualValue": results.keyActualValue,
"searchLine": results.searchLine,
}
}

CxPolicy[result] {
#Case of "security-group" Module
module := input.document[i].module[name]
types := ["ingress_with_cidr_blocks","ingress_with_ipv6_cidr_blocks"]
ingressKey := common_lib.get_module_equivalent_key("aws", module.source, "aws_security_group", types[t])
common_lib.valid_key(module, ingressKey)

ingress := module[ingressKey][i2]

tf_lib.portOpenToInternet(ingress, 80)

result := {
"documentId": input.document[i].id,
"resourceType": "n/a",
"resourceName": "n/a",
"searchKey": sprintf("module[%s].%s.%d", [name, ingressKey,i2]),
"issueType": "IncorrectValue",
"keyExpectedValue": "aws_security_group.ingress shouldn't open the HTTP port (80)",
"keyActualValue": "aws_security_group.ingress opens the HTTP port (80)",
"keyExpectedValue": sprintf("module[%s].%s.%d should not open the HTTP port (80)",[name, ingressKey,i2]),
"keyActualValue": sprintf("module[%s].%s.%d opens the HTTP port (80)",[name, ingressKey,i2]),
"searchLine": common_lib.build_search_line(["module", name, ingressKey, i2], []),
}
}

http_is_open(ingress,is_unique_element,name,i2) = results {
is_unique_element
tf_lib.portOpenToInternet(ingress, 80)

results := {
"searchKey" : sprintf("aws_security_group[%s].ingress", [name]),
"keyExpectedValue" : sprintf("aws_security_group[%s].ingress should not open the HTTP port (80)",[name]),
"keyActualValue" : sprintf("aws_security_group[%s].ingress opens the HTTP port (80)",[name]),
"searchLine" : common_lib.build_search_line(["resource", "aws_security_group", name, "ingress"], []),
}
} else = results {
not is_unique_element
tf_lib.portOpenToInternet(ingress, 80)

results := {
"searchKey" : sprintf("aws_security_group[%s].ingress[%d]", [name,i2]),
"keyExpectedValue" : sprintf("aws_security_group[%s].ingress[%d] should not open the HTTP port (80)", [name,i2]),
"keyActualValue" : sprintf("aws_security_group[%s].ingress[%d] opens the HTTP port (80)", [name,i2]),
"searchLine" : common_lib.build_search_line(["resource", "aws_security_group", name, "ingress", i2], []),
}
} else = ""
23 changes: 0 additions & 23 deletions assets/queries/terraform/aws/http_port_open/test/negative.tf

This file was deleted.

66 changes: 66 additions & 0 deletions assets/queries/terraform/aws/http_port_open/test/negative1.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
resource "aws_security_group" "negative1-1" {

ingress {
description = "Remote desktop open private"
from_port = 70
to_port = 81
protocol = "tcp"
}
}

resource "aws_security_group" "negative1-2" {

ingress {
description = "Remote desktop open private"
from_port = 79
to_port = 100
protocol = "tcp"
cidr_blocks = ["0.1.0.0/0"]
}
}

resource "aws_security_group" "negative1-3" {

ingress {
description = "Remote desktop open private"
from_port = 3380
to_port = 3450
protocol = "tcp"
ipv6_cidr_blocks = ["2001:db8:abcd:0012::/64"]
}
}

resource "aws_security_group" "negative1-4" {
name = "allow_tls"
description = "sample"

ingress {
description = "sample"
from_port = 100
to_port = 200
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
description = "sample"
from_port = 100
to_port = 200
protocol = "tcp"
ipv6_cidr_blocks = ["fd00::/8", "::/0"]
}
}

resource "aws_security_group" "negative1-5" {
name = "allow_tls"
description = "sample"

ingress {
description = "sample"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["192.120.0.0/16"]
ipv6_cidr_blocks = ["fd00::/8"]
}
}
31 changes: 31 additions & 0 deletions assets/queries/terraform/aws/http_port_open/test/negative2.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
resource "aws_security_group" "ec2" {
description = "ec2 sg"
name = "secgroup-ec2"
vpc_id = var.vpc_id
}

resource "aws_vpc_security_group_ingress_rule" "negative2-1" {
security_group_id = aws_security_group.negative.id
from_port = 80
to_port = 80
ip_protocol = "tcp"
description = "TLS from VPC"
}

resource "aws_vpc_security_group_ingress_rule" "negative2-2" {
security_group_id = aws_security_group.ec2.id
cidr_ipv4 = "0.0.1.0/0"
from_port = 80
to_port = 80
ip_protocol = "tcp"
description = "allows RDP from Internet"
}

resource "aws_vpc_security_group_ingress_rule" "negative2-3" {
security_group_id = aws_security_group.ec2.id
cidr_ipv6 = "2001:db8:abcd:0012::/64"
from_port = 80
to_port = 80
ip_protocol = "-1"
description = "allows RDP from Internet"
}
34 changes: 34 additions & 0 deletions assets/queries/terraform/aws/http_port_open/test/negative3.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
resource "aws_security_group" "ec2" {
description = "ec2 sg"
name = "secgroup-ec2"
vpc_id = var.vpc_id
}

resource "aws_security_group_rule" "negative3-1" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
security_group_id = aws_security_group.negative.id
description = "TLS from VPC"
}

resource "aws_security_group_rule" "negative3-2" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.1.0/0"]
security_group_id = aws_security_group.ec2.id
description = "allows RDP from Internet (IPv4)"
}

resource "aws_security_group_rule" "negative3-3" {
type = "ingress"
from_port = 79
to_port = 100
protocol = "-1"
ipv6_cidr_blocks = ["2001:db8:abcd:0012::/64"]
security_group_id = aws_security_group.ec2.id
description = "allows RDP from Internet (IPv6)"
}
Loading
Loading