Skip to content

Commit fd62676

Browse files
Merge pull request #47 from jinyoungmoonDEV/master
add: add vulnerable_ports logic
2 parents 62311f8 + 4abebbe commit fd62676

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

src/plugin/conf/cloud_service_conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
DEFAULT_REGION = "us-east-1"
1010
FILTER_FORMAT = []
1111
BOTO3_HTTPS_VERIFIED = None
12+
DEFAULT_VULNERABLE_PORTS = "22,3306"
1213

1314
ASSET_URL = "https://spaceone-custom-assets.s3.ap-northeast-2.amazonaws.com/console-assets/icons/cloud-services/aws"
1415

src/plugin/main.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import logging
22
from spaceone.inventory.plugin.collector.lib.server import CollectorPluginServer
3+
4+
from .conf.cloud_service_conf import DEFAULT_VULNERABLE_PORTS
35
from .manager.base import ResourceManager
46

57
_LOGGER = logging.getLogger("cloudforet")
@@ -311,6 +313,17 @@ def _create_init_metadata():
311313
"inventory.Region",
312314
"inventory.ErrorResource",
313315
],
314-
"options_schema": {},
316+
"options_schema": {
317+
"required": ["vulnerable_ports"],
318+
"type": "object",
319+
"properties": {
320+
"vulnerable_ports": {
321+
"title": "Vulnerable Ports Option",
322+
"type": "string",
323+
"default": DEFAULT_VULNERABLE_PORTS,
324+
"description": "Ex) 22,8080,3306 (Default = 22,3306)",
325+
}
326+
},
327+
},
315328
}
316329
}

src/plugin/manager/ec2/security_group_manager.py

+35-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import copy
22
from spaceone.inventory.plugin.collector.lib import *
33
from ..base import ResourceManager
4-
from ...conf.cloud_service_conf import ASSET_URL, INSTANCE_FILTERS
4+
from ...conf.cloud_service_conf import ASSET_URL, INSTANCE_FILTERS, DEFAULT_VULNERABLE_PORTS
5+
from ...error.custom import ERROR_VULNERABLE_PORTS
56

67

78
class SecurityGroupManager(ResourceManager):
@@ -35,6 +36,9 @@ def create_cloud_service_type(self):
3536
def create_cloud_service(self, region, options, secret_data, schema):
3637
cloudtrail_resource_type = "AWS::EC2::SecurityGroup"
3738

39+
# If Port Filter Option Exist
40+
vulnerable_ports = options.get("vulnerable_ports", DEFAULT_VULNERABLE_PORTS)
41+
3842
# Get default VPC
3943
default_vpcs = self._get_default_vpc()
4044

@@ -62,7 +66,7 @@ def create_cloud_service(self, region, options, secret_data, schema):
6266
in_rule_copy = copy.deepcopy(in_rule)
6367
inbound_rules.append(
6468
self.custom_security_group_rule_info(
65-
in_rule_copy, _ip_range, "ip_ranges"
69+
in_rule_copy, _ip_range, "ip_ranges",vulnerable_ports
6670
)
6771
)
6872

@@ -73,14 +77,15 @@ def create_cloud_service(self, region, options, secret_data, schema):
7377
in_rule_copy,
7478
_user_group_pairs,
7579
"user_id_group_pairs",
80+
vulnerable_ports,
7681
)
7782
)
7883

7984
for _ip_v6_range in in_rule.get("Ipv6Ranges", []):
8085
in_rule_copy = copy.deepcopy(in_rule)
8186
inbound_rules.append(
8287
self.custom_security_group_rule_info(
83-
in_rule_copy, _ip_v6_range, "ipv6_ranges"
88+
in_rule_copy, _ip_v6_range, "ipv6_ranges",vulnerable_ports
8489
)
8590
)
8691

@@ -91,7 +96,7 @@ def create_cloud_service(self, region, options, secret_data, schema):
9196
out_rule_copy = copy.deepcopy(out_rule)
9297
outbound_rules.append(
9398
self.custom_security_group_rule_info(
94-
out_rule_copy, _ip_range, "ip_ranges"
99+
out_rule_copy, _ip_range, "ip_ranges",vulnerable_ports
95100
)
96101
)
97102

@@ -101,15 +106,15 @@ def create_cloud_service(self, region, options, secret_data, schema):
101106
self.custom_security_group_rule_info(
102107
out_rule_copy,
103108
_user_group_pairs,
104-
"user_id_group_pairs",
109+
"user_id_group_pairs",vulnerable_ports,
105110
)
106111
)
107112

108113
for _ip_v6_range in out_rule.get("Ipv6Ranges", []):
109114
out_rule_copy = copy.deepcopy(out_rule)
110115
outbound_rules.append(
111116
self.custom_security_group_rule_info(
112-
out_rule_copy, _ip_v6_range, "ipv6_ranges"
117+
out_rule_copy, _ip_v6_range, "ipv6_ranges",vulnerable_ports
113118
)
114119
)
115120

@@ -160,16 +165,16 @@ def create_cloud_service(self, region, options, secret_data, schema):
160165
region_name=region,
161166
)
162167

163-
def custom_security_group_rule_info(self, raw_rule, remote, remote_type):
168+
def custom_security_group_rule_info(self, raw_rule, remote, remote_type, vulnerable_ports):
169+
protocol_display = self._get_protocol_display(raw_rule.get("IpProtocol"))
164170
raw_rule.update(
165171
{
166-
"protocol_display": self._get_protocol_display(
167-
raw_rule.get("IpProtocol")
168-
),
172+
"protocol_display": protocol_display,
169173
"port_display": self._get_port_display(raw_rule),
170174
"source_display": self._get_source_display(remote),
171175
"description_display": self._get_description_display(remote),
172176
remote_type: remote,
177+
"vulnerable_ports": self._get_vulnerable_ports(protocol_display, raw_rule, vulnerable_ports)
173178
}
174179
)
175180

@@ -287,3 +292,23 @@ def get_instance_name_from_tags(instance):
287292
return _tag.get("Value")
288293

289294
return ""
295+
296+
@staticmethod
297+
def _get_vulnerable_ports(protocol_display: str, raw_rule: dict, vulnerable_ports: str):
298+
try:
299+
if protocol_display == "ALL":
300+
return [int(port.strip()) for port in vulnerable_ports.split(',')]
301+
302+
to_port = raw_rule.get("ToPort")
303+
from_port = raw_rule.get("FromPort")
304+
305+
if to_port is None or from_port is None:
306+
return []
307+
308+
return [
309+
int(port.strip())
310+
for port in vulnerable_ports.split(',')
311+
if from_port <= int(port.strip()) <= to_port
312+
]
313+
except ValueError:
314+
raise ERROR_VULNERABLE_PORTS(vulnerable_ports)

0 commit comments

Comments
 (0)