def setUp(self, *args, **kwargs): super(LinuxSysVInitParserTest, self).setUp(*args, **kwargs) if self.results is None: # Create a fake filesystem. dirs = [ "/etc", "/etc/rc1.d", "/etc/rc2.d", "/etc/rc6.d", "/etc/rcS.d" ] d_stat, d_files = parsers_test_lib.GenTestData(dirs, [""] * len(dirs), st_mode=16877) files = ["/etc/rc.local", "/etc/ignoreme", "/etc/rc2.d/S20ssh"] f_stat, f_files = parsers_test_lib.GenTestData( files, [""] * len(files)) links = [ "/etc/rc1.d/S90single", "/etc/rc1.d/K20ssh", "/etc/rc1.d/ignore", "/etc/rc2.d/S20ntp", "/etc/rc2.d/S30ufw", "/etc/rc6.d/K20ssh", "/etc/rcS.d/S20firewall" ] l_stat, l_files = parsers_test_lib.GenTestData(links, [""] * len(links), st_mode=41471) stats = d_stat + f_stat + l_stat files = d_files + f_files + l_files parser = linux_service_parser.LinuxSysVInitParser() self.results = list(parser.ParseMultiple(stats, files, None))
def RunXinetdCheck(self, chk_id, svc, disabled, sym, found, xinetd=False, should_detect=True): host_data = self.SetKnowledgeBase() cfgs = parsers_test_lib.GenXinetd(svc, disabled) stats, files = parsers_test_lib.GenTestData(cfgs, cfgs.values()) data = list(self.parser(stats, files, None)) # create entries on whether xinetd itself is setup to start or not if xinetd: cfgs = parsers_test_lib.GenInit("xinetd", "the extended Internet services daemon") stats, files = parsers_test_lib.GenTestData(cfgs, cfgs.values()) lsb_parser = linux_service_parser.LinuxLSBInitParser() data.extend(list(lsb_parser.ParseMultiple(stats, files, None))) host_data["LinuxServices"] = self.SetArtifactData(parsed=data) results = self.RunChecks(host_data) if should_detect: self.assertCheckDetectedAnom(chk_id, results, sym, found) else: self.assertCheckUndetected(chk_id, results)
def RunSysVChecks(self): host_data = self.SetKnowledgeBase() links = [ "/etc/rc2.d/S50xinetd", "/etc/rc2.d/S60wu-ftpd", "/etc/rc2.d/S10ufw" ] stats, files = parsers_test_lib.GenTestData( links, [""] * len(links), st_mode=41471) parsed = list(self.parser(stats, files, None)) host_data["LinuxServices"] = self.SetArtifactData(parsed=parsed) self.results = self.RunChecks(host_data)
def testSkipBadLSBInit(self): """Bad Init entries fail gracefully.""" empty = "" snippet = r"""# Provides: sshd""" unfinished = """ ### BEGIN INIT INFO what are you thinking? """ paths = ["/tmp/empty", "/tmp/snippet", "/tmp/unfinished"] vals = [empty, snippet, unfinished] stats, files = parsers_test_lib.GenTestData(paths, vals) parser = linux_service_parser.LinuxLSBInitParser() results = list(parser.ParseMultiple(stats, files, None)) self.assertFalse(results)
def testParseLSBInit(self): """Init entries return accurate LinuxServiceInformation values.""" configs = parsers_test_lib.GenInit("sshd", "OpenBSD Secure Shell server") stats, files = parsers_test_lib.GenTestData(configs, configs.values()) parser = linux_service_parser.LinuxLSBInitParser() results = list(parser.ParseMultiple(stats, files, None)) self.assertIsInstance(results[0], rdf_client.LinuxServiceInformation) result = results[0] self.assertEqual("sshd", result.name) self.assertEqual("OpenBSD Secure Shell server", result.description) self.assertEqual("INIT", result.start_mode) self.assertItemsEqual([2, 3, 4, 5], result.start_on) self.assertItemsEqual([1], result.stop_on) self.assertItemsEqual([ "umountfs", "umountnfs", "sendsigs", "rsyslog", "sysklogd", "syslog-ng", "dsyslog", "inetutils-syslogd" ], result.start_after) self.assertItemsEqual([ "rsyslog", "sysklogd", "syslog-ng", "dsyslog", "inetutils-syslogd" ], result.stop_after)
def testParseXinetd(self): """Xinetd entries return accurate LinuxServiceInformation values.""" configs = parsers_test_lib.GenXinetd("telnet", "yes") configs.update(parsers_test_lib.GenXinetd("forwarder", "no")) stats, files = parsers_test_lib.GenTestData(configs, configs.values()) parser = linux_service_parser.LinuxXinetdParser() results = list(parser.ParseMultiple(stats, files, None)) self.assertEqual(2, len(results)) self.assertItemsEqual(["forwarder", "telnet"], [r.name for r in results]) for rslt in results: self.assertFalse(rslt.start_on) self.assertFalse(rslt.stop_on) self.assertFalse(rslt.stop_after) if rslt.name == "telnet": self.assertFalse(rslt.start_mode) self.assertFalse(rslt.start_after) self.assertFalse(rslt.starts) else: self.assertEqual("XINETD", str(rslt.start_mode)) self.assertItemsEqual(["xinetd"], list(rslt.start_after)) self.assertTrue(rslt.starts)