def get_hints(self, plugin): """ Return plugin hints from ``plugin``. """ hints = [] for hint_name in getattr(plugin, "hints", []): hint_plugin = self._plugins.get(hint_name) if hint_plugin: hint_result = Result( name=hint_plugin.name, homepage=hint_plugin.homepage, from_url=self.requested_url, type=HINT_TYPE, plugin=plugin.name, ) hints.append(hint_result) logger.debug(f"{plugin.name} & hint {hint_result.name} detected") else: logger.error(f"{plugin.name} hints an invalid plugin: {hint_name}") return hints
def process_from_splash(self): ''' Add softwares found in the DOM ''' for software in self._softwares_from_splash: plugin = self._plugins.get(software['name']) # Determine if it's a version or presence result try: additional_data = {'version': software['version']} except KeyError: additional_data = {'type': INDICATOR_TYPE} self._results.add_result( Result( name=plugin.name, homepage=plugin.homepage, from_url=self.requested_url, **additional_data, )) for hint in self.get_hints(plugin): self._results.add_result(hint)
def process_har(self): """ Detect plugins present in the page. """ hints = [] version_plugins = self._plugins.with_version_matchers() generic_plugins = self._plugins.with_generic_matchers() for entry in self.har: for plugin in version_plugins: pm = self.apply_plugin_matchers(plugin, entry) if not pm: continue # Set name if matchers could detect modular name if pm.name: name = "{}-{}".format(plugin.name, pm.name) else: name = plugin.name if pm.version: self._results.add_result( Result( name=name, version=pm.version, homepage=plugin.homepage, from_url=get_url(entry), plugin=plugin.name, ) ) elif pm.presence: # Try to get version through file hashes version = get_version_via_file_hashes(plugin, entry) if version: self._results.add_result( Result( name=name, version=version, homepage=plugin.homepage, from_url=get_url(entry), plugin=plugin.name, ) ) else: self._results.add_result( Result( name=name, homepage=plugin.homepage, from_url=get_url(entry), type=INDICATOR_TYPE, plugin=plugin.name, ) ) hints += self.get_hints(plugin) for plugin in generic_plugins: pm = self.apply_plugin_matchers(plugin, entry) if not pm: continue plugin_data = plugin.get_information(entry) # Only add to results if it's a valid result if "name" in plugin_data: self._results.add_result( Result( name=plugin_data["name"], homepage=plugin_data["homepage"], from_url=get_url(entry), type=GENERIC_TYPE, plugin=plugin.name, ) ) hints += self.get_hints(plugin) for hint in hints: self._results.add_result(hint)
class TestResultCollection(): @staticmethod def _assert_results(detected, results): c = ResultCollection() for d in detected: c.add_result(d) assert set(c.get_results()) == set(results) @pytest.mark.parametrize( 'detected,results', [ ([ Result('pluginA', '1.1'), Result('pluginB', '3.8.7'), Result('pluginC', '4.0') ], [ Result('pluginA', '1.1'), Result('pluginB', '3.8.7'), Result('pluginC', '4.0') ]), ( [ Result('pluginA', '1.3'), Result('pluginA', '1.2'), Result('pluginA', '1.1') ], [ Result('pluginA', '1.1'), Result('pluginA', '1.2'), Result('pluginA', '1.3') ], ), ([ Result('pluginA', '1.1'), Result('pluginC', type=HINT_TYPE), Result('pluginB', type=INDICATOR_TYPE), Result('pluginD', type=GENERIC_TYPE), ], [ Result('pluginA', '1.1'), Result('pluginB', type=INDICATOR_TYPE), Result('pluginC', type=HINT_TYPE), Result('pluginD', type=GENERIC_TYPE), ]), ] ) def test_get_all_detected_plugins(self, detected, results): self._assert_results(detected, results) @pytest.mark.parametrize( 'detected,results', [ ([ Result('pluginA', '1.1'), Result('pluginA', '1.2'), Result('pluginA', '1.1'), ], [Result('pluginA', '1.1'), Result('pluginA', '1.2')]), ([ Result('pluginA', '1.1'), Result('pluginA', type=INDICATOR_TYPE), Result('pluginA', type=HINT_TYPE), ], [Result('pluginA', '1.1')]), ([ Result('pluginB', type=HINT_TYPE), Result('pluginB', type=HINT_TYPE), ], [Result('pluginB', type=HINT_TYPE)]), ([ Result('pluginB', type=INDICATOR_TYPE), Result('pluginB', type=INDICATOR_TYPE) ], [Result('pluginB', type=INDICATOR_TYPE)]), ([ Result('pluginB', type=INDICATOR_TYPE), Result('pluginB', type=HINT_TYPE), ], [Result('pluginB', type=INDICATOR_TYPE)]), ([ Result('pluginB', type=INDICATOR_TYPE), Result('pluginB', type=GENERIC_TYPE), ], [Result('pluginB', type=INDICATOR_TYPE)]), ] ) def test_remove_duplicated_results(self, detected, results): self._assert_results(detected, results)
class TestResultCollection: @staticmethod def _assert_results(detected, results): c = ResultCollection() for d in detected: c.add_result(d) assert set(c.get_results()) == set(results) @pytest.mark.parametrize( "detected,results", [ ( [ Result("pluginA", "1.1"), Result("pluginB", "3.8.7"), Result("pluginC", "4.0"), ], [ Result("pluginA", "1.1"), Result("pluginB", "3.8.7"), Result("pluginC", "4.0"), ], ), ( [ Result("pluginA", "1.3"), Result("pluginA", "1.2"), Result("pluginA", "1.1"), ], [ Result("pluginA", "1.1"), Result("pluginA", "1.2"), Result("pluginA", "1.3"), ], ), ( [ Result("pluginA", "1.1"), Result("pluginC", type=HINT_TYPE), Result("pluginB", type=INDICATOR_TYPE), Result("pluginD", type=GENERIC_TYPE), ], [ Result("pluginA", "1.1"), Result("pluginB", type=INDICATOR_TYPE), Result("pluginC", type=HINT_TYPE), Result("pluginD", type=GENERIC_TYPE), ], ), ], ) def test_get_all_detected_plugins(self, detected, results): self._assert_results(detected, results) @pytest.mark.parametrize( "detected,results", [ ( [ Result("pluginA", "1.1"), Result("pluginA", "1.2"), Result("pluginA", "1.1"), ], [Result("pluginA", "1.1"), Result("pluginA", "1.2")], ), ( [ Result("pluginA", "1.1"), Result("pluginA", type=INDICATOR_TYPE), Result("pluginA", type=HINT_TYPE), ], [Result("pluginA", "1.1")], ), ( [ Result("pluginB", type=HINT_TYPE), Result("pluginB", type=HINT_TYPE) ], [Result("pluginB", type=HINT_TYPE)], ), ( [ Result("pluginB", type=INDICATOR_TYPE), Result("pluginB", type=INDICATOR_TYPE), ], [Result("pluginB", type=INDICATOR_TYPE)], ), ( [ Result("pluginB", type=INDICATOR_TYPE), Result("pluginB", type=HINT_TYPE), ], [Result("pluginB", type=INDICATOR_TYPE)], ), ( [ Result("pluginB", type=INDICATOR_TYPE), Result("pluginB", type=GENERIC_TYPE), ], [Result("pluginB", type=INDICATOR_TYPE)], ), ], ) def test_remove_duplicated_results(self, detected, results): self._assert_results(detected, results)