From 4f9a362e6a52df904d5803ad6ad348787312f4a1 Mon Sep 17 00:00:00 2001 From: manish Date: Wed, 20 Feb 2019 13:36:24 +0100 Subject: [PATCH 1/5] created reporter for json format --- kube-hunter.py | 3 +++ src/modules/report/base.py | 34 +++++++++++++++++++++++++ src/modules/report/json_reporter.py | 12 +++++++++ src/modules/report/yaml.py | 39 +++-------------------------- 4 files changed, 52 insertions(+), 36 deletions(-) create mode 100644 src/modules/report/base.py create mode 100644 src/modules/report/json_reporter.py diff --git a/kube-hunter.py b/kube-hunter.py index 107729a..967ce1b 100755 --- a/kube-hunter.py +++ b/kube-hunter.py @@ -35,9 +35,12 @@ if config.log.lower() != "none": from src.modules.report.plain import PlainReporter from src.modules.report.yaml import YAMLReporter +from src.modules.report.json_reporter import JSONReporter if config.report.lower() == "yaml": config.reporter = YAMLReporter() +elif config.report.lower() == "json": + config.reporter = JSONReporter() else: config.reporter = PlainReporter() diff --git a/src/modules/report/base.py b/src/modules/report/base.py new file mode 100644 index 0000000..318a40f --- /dev/null +++ b/src/modules/report/base.py @@ -0,0 +1,34 @@ +from collector import services, vulnerabilities, services_lock, vulnerabilities_lock + +class BaseReporter(object): + def get_nodes(self): + nodes = list() + node_locations = set() + services_lock.acquire() + for service in services: + node_location = str(service.host) + if node_location not in node_locations: + nodes.append({"type": "Node/Master", "location": str(service.host)}) + node_locations.add(node_location) + services_lock.release() + return nodes + + def get_services(self): + services_lock.acquire() + services_data = [{"service": service.get_name(), + "location": "{}:{}{}".format(service.host, service.port, service.get_path()), + "description": service.explain()} + for service in services] + services_lock.release() + return services_data + + def get_vulenrabilities(self): + vulnerabilities_lock.acquire() + vulnerabilities_data = [{"location": "{}:{}".format(vuln.host, vuln.port) if vuln.host else "", + "category": vuln.category.name, + "vulnerability": vuln.get_name(), + "description": vuln.explain(), + "evidence": str(vuln.evidence)} + for vuln in vulnerabilities] + vulnerabilities_lock.release() + return vulnerabilities_data diff --git a/src/modules/report/json_reporter.py b/src/modules/report/json_reporter.py new file mode 100644 index 0000000..d1d38fb --- /dev/null +++ b/src/modules/report/json_reporter.py @@ -0,0 +1,12 @@ +import StringIO +import json +from base import BaseReporter + +class JSONReporter(BaseReporter): + def get_report(self): + report = { + "nodes": self.get_nodes(), + "services": self.get_services(), + "vulnerabilities": self.get_vulenrabilities() + } + return json.dumps(report) diff --git a/src/modules/report/yaml.py b/src/modules/report/yaml.py index 1ffcf58..76260f9 100644 --- a/src/modules/report/yaml.py +++ b/src/modules/report/yaml.py @@ -1,10 +1,9 @@ import StringIO from ruamel.yaml import YAML +from base import BaseReporter -from collector import services, vulnerabilities, services_lock, vulnerabilities_lock - -class YAMLReporter(object): +class YAMLReporter(BaseReporter): def get_report(self): yaml = YAML() report = { @@ -14,36 +13,4 @@ class YAMLReporter(object): } output = StringIO.StringIO() yaml.dump(report, output) - return output.getvalue() - - def get_nodes(self): - nodes = list() - node_locations = set() - services_lock.acquire() - for service in services: - node_location = str(service.host) - if node_location not in node_locations: - nodes.append({"type": "Node/Master", "location": str(service.host)}) - node_locations.add(node_location) - services_lock.release() - return nodes - - def get_services(self): - services_lock.acquire() - services_data = [{"service": service.get_name(), - "location": "{}:{}{}".format(service.host, service.port, service.get_path()), - "description": service.explain()} - for service in services] - services_lock.release() - return services_data - - def get_vulenrabilities(self): - vulnerabilities_lock.acquire() - vulnerabilities_data = [{"location": "{}:{}".format(vuln.host, vuln.port) if vuln.host else "", - "category": vuln.category.name, - "vulnerability": vuln.get_name(), - "description": vuln.explain(), - "evidence": str(vuln.evidence)} - for vuln in vulnerabilities] - vulnerabilities_lock.release() - return vulnerabilities_data + return output.getvalue() \ No newline at end of file From 78b75742176b696a825dbfb018e81b0f59323c86 Mon Sep 17 00:00:00 2001 From: maniish-jaiin Date: Thu, 21 Feb 2019 11:17:06 +0100 Subject: [PATCH 2/5] renamed the function name and json_reporter to json --- src/modules/report/base.py | 2 +- src/modules/report/json.py | 13 +++++++++++++ src/modules/report/yaml.py | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/modules/report/json.py diff --git a/src/modules/report/base.py b/src/modules/report/base.py index 318a40f..b8986a0 100644 --- a/src/modules/report/base.py +++ b/src/modules/report/base.py @@ -22,7 +22,7 @@ class BaseReporter(object): services_lock.release() return services_data - def get_vulenrabilities(self): + def get_vulnerabilities(self): vulnerabilities_lock.acquire() vulnerabilities_data = [{"location": "{}:{}".format(vuln.host, vuln.port) if vuln.host else "", "category": vuln.category.name, diff --git a/src/modules/report/json.py b/src/modules/report/json.py new file mode 100644 index 0000000..ab2ac74 --- /dev/null +++ b/src/modules/report/json.py @@ -0,0 +1,13 @@ +import StringIO +import json +from base import BaseReporter + +class JSONReporter(BaseReporter): + def get_report(self): + report = { + "nodes": self.get_nodes(), + "services": self.get_services(), + "vulnerabilities": self.get_vulnerabilities(), + "vulnerability_count": self.get_count() + } + return json.dumps(report) diff --git a/src/modules/report/yaml.py b/src/modules/report/yaml.py index 76260f9..2060fed 100644 --- a/src/modules/report/yaml.py +++ b/src/modules/report/yaml.py @@ -9,7 +9,7 @@ class YAMLReporter(BaseReporter): report = { "nodes": self.get_nodes(), "services": self.get_services(), - "vulnerabilities": self.get_vulenrabilities() + "vulnerabilities": self.get_vulnerabilities() } output = StringIO.StringIO() yaml.dump(report, output) From 9d68679df041e4d50e7f9b25d215381bd85bdc41 Mon Sep 17 00:00:00 2001 From: maniish-jaiin Date: Thu, 21 Feb 2019 11:17:06 +0100 Subject: [PATCH 3/5] renamed the function name and json_reporter to json --- kube-hunter.py | 2 +- src/modules/report/base.py | 2 +- src/modules/report/json.py | 13 +++++++++++++ src/modules/report/yaml.py | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 src/modules/report/json.py diff --git a/kube-hunter.py b/kube-hunter.py index 967ce1b..113a779 100755 --- a/kube-hunter.py +++ b/kube-hunter.py @@ -35,7 +35,7 @@ if config.log.lower() != "none": from src.modules.report.plain import PlainReporter from src.modules.report.yaml import YAMLReporter -from src.modules.report.json_reporter import JSONReporter +from src.modules.report.json import JSONReporter if config.report.lower() == "yaml": config.reporter = YAMLReporter() diff --git a/src/modules/report/base.py b/src/modules/report/base.py index 318a40f..b8986a0 100644 --- a/src/modules/report/base.py +++ b/src/modules/report/base.py @@ -22,7 +22,7 @@ class BaseReporter(object): services_lock.release() return services_data - def get_vulenrabilities(self): + def get_vulnerabilities(self): vulnerabilities_lock.acquire() vulnerabilities_data = [{"location": "{}:{}".format(vuln.host, vuln.port) if vuln.host else "", "category": vuln.category.name, diff --git a/src/modules/report/json.py b/src/modules/report/json.py new file mode 100644 index 0000000..ab2ac74 --- /dev/null +++ b/src/modules/report/json.py @@ -0,0 +1,13 @@ +import StringIO +import json +from base import BaseReporter + +class JSONReporter(BaseReporter): + def get_report(self): + report = { + "nodes": self.get_nodes(), + "services": self.get_services(), + "vulnerabilities": self.get_vulnerabilities(), + "vulnerability_count": self.get_count() + } + return json.dumps(report) diff --git a/src/modules/report/yaml.py b/src/modules/report/yaml.py index 76260f9..2060fed 100644 --- a/src/modules/report/yaml.py +++ b/src/modules/report/yaml.py @@ -9,7 +9,7 @@ class YAMLReporter(BaseReporter): report = { "nodes": self.get_nodes(), "services": self.get_services(), - "vulnerabilities": self.get_vulenrabilities() + "vulnerabilities": self.get_vulnerabilities() } output = StringIO.StringIO() yaml.dump(report, output) From f11319766b29ab8ea042c6dd9021f080d8eeb888 Mon Sep 17 00:00:00 2001 From: Liz Rice Date: Thu, 21 Feb 2019 11:19:19 +0000 Subject: [PATCH 4/5] Typo --- src/modules/report/json_reporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/report/json_reporter.py b/src/modules/report/json_reporter.py index d1d38fb..0a1dde9 100644 --- a/src/modules/report/json_reporter.py +++ b/src/modules/report/json_reporter.py @@ -7,6 +7,6 @@ class JSONReporter(BaseReporter): report = { "nodes": self.get_nodes(), "services": self.get_services(), - "vulnerabilities": self.get_vulenrabilities() + "vulnerabilities": self.get_vulnerabilities() } return json.dumps(report) From 5380caca6b304ca68385a0708c329134cd689f9b Mon Sep 17 00:00:00 2001 From: Liz Rice Date: Thu, 21 Feb 2019 11:20:25 +0000 Subject: [PATCH 5/5] Add json to the format options in help --- kube-hunter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kube-hunter.py b/kube-hunter.py index 113a779..d9ab4d1 100755 --- a/kube-hunter.py +++ b/kube-hunter.py @@ -20,7 +20,7 @@ parser.add_argument('--mapping', action="store_true", help="outputs only a mappi parser.add_argument('--remote', nargs='+', metavar="HOST", default=list(), help="one or more remote ip/dns to hunt") parser.add_argument('--active', action="store_true", help="enables active hunting") parser.add_argument('--log', type=str, metavar="LOGLEVEL", default='INFO', help="set log level, options are: debug, info, warn, none") -parser.add_argument('--report', type=str, default='plain', help="set report type, options are: plain, yaml") +parser.add_argument('--report', type=str, default='plain', help="set report type, options are: plain, yaml, json") import plugins