Пример #1
0
    def test_source(self):
        all_source = Source.get_all()
        source = Source.get_by_id(1)
        self.assertEqual(len(all_source), 2)
        self.assertEqual(source.method.name, "URL HTTP")
        self.assertEqual(source.data_type.name, "one file not compressed")
        self.assertEqual(
            source.uri, "https://sslbl.abuse.ch/blacklist/sslblacklist.rules")
        self.assertEqual(
            str(source), "https://sslbl.abuse.ch/blacklist/sslblacklist.rules")
        source = Source.get_by_uri(
            "https://sslbl.abuse.ch/blacklist/sslblacklist.rules")
        self.assertEqual(source.data_type.name, "one file not compressed")

        with self.assertLogs('rules.models', level='DEBUG'):
            Source.get_by_uri("https://sslbl.abuse.ch/lacklist.rules")
        source = Source.get_by_id(99)
        self.assertEqual(source, None)
        with self.assertRaises(AttributeError):
            source.uri
        with self.assertRaises(IntegrityError):
            Source.objects.create(
                method=MethodUpload.get_by_id(1),
                uri="https://sslbl.abuse.ch/blacklist/sslblacklist.rules",
                data_type=DataTypeUpload.get_by_id(1),
            )
Пример #2
0
 def test_source_delete(self):
     self.assertEqual(len(SourceSuricata.get_all()), 2)
     for source in SourceSuricata.get_all():
         response = self.client.post('/admin/suricata/sourcesuricata/', {
             'action': 'delete_selected',
             '_selected_action': source.id
         },
                                     follow=True)
         self.assertEqual(response.status_code, 200)
         self.assertIn('Are you sure you want to delete the selected ',
                       str(response.content))
         response = self.client.post('/admin/suricata/sourcesuricata/', {
             'action': 'delete_selected',
             '_selected_action': source.id,
             'post': 'yes'
         },
                                     follow=True)
         self.assertEqual(response.status_code, 200)
         self.assertIn('Successfully deleted 1 ', str(response.content))
     self.assertEqual(len(SourceSuricata.get_all()), 0)
     response = self.client.post('/admin/suricata/sourcesuricata/add/', {
         'method':
         MethodUpload.get_by_name("URL HTTP").id,
         'uri':
         'https://sslbl.abuse.ch/blacklist/sslblacklist.rules',
         'scheduled_rules_deployment_enabled':
         'True',
         'scheduled_rules_deployment_crontab':
         CrontabSchedule.objects.get(id=1).id,
         'scheduled_deploy':
         'True',
         'rulesets':
         '1',
         'data_type':
         DataTypeUpload.get_by_name("one file not compressed").id
     },
                                 follow=True)
     self.assertEqual(response.status_code, 200)
     self.assertIn('Upload source in progress.', str(response.content))
     self.assertEqual(len(SourceSuricata.get_all()), 1)
     response = self.client.post(
         '/admin/suricata/sourcesuricata/', {
             'action': 'delete_selected',
             '_selected_action': SourceSuricata.get_all()[0].id
         },
         follow=True)
     self.assertEqual(response.status_code, 200)
     self.assertIn('Are you sure you want to delete the selected ',
                   str(response.content))
     response = self.client.post(
         '/admin/suricata/sourcesuricata/', {
             'action': 'delete_selected',
             '_selected_action': SourceSuricata.get_all()[0].id,
             'post': 'yes'
         },
         follow=True)
     self.assertEqual(response.status_code, 200)
     self.assertIn('Successfully deleted 1 ', str(response.content))
     self.assertEqual(len(SourceSuricata.get_all()), 0)
Пример #3
0
 def test_raise_not_found_param(self):
     self.assertEqual(len(SignatureSuricata.get_all()), 2)
     with open(settings.BASE_DIR + '/suricata/tests/data/error-sid.rules',
               encoding='utf_8') as fp:
         response = self.client.post(
             '/admin/suricata/sourcesuricata/add/', {
                 'method':
                 MethodUpload.get_by_name("Upload file").id,
                 'file':
                 fp,
                 'scheduled_rules_deployment_enabled':
                 'False',
                 'scheduled_deploy':
                 'False',
                 'data_type':
                 DataTypeUpload.get_by_name("one file not compressed").id,
                 'rulesets':
                 '1',
             },
             follow=True)
     self.assertEqual(response.status_code, 200)
     self.assertIn('File uploaded successfully :', str(response.content))
     self.assertEqual(len(SignatureSuricata.get_all()), 2)
     with open(settings.BASE_DIR +
               '/suricata/tests/data/error-classtype.rules',
               encoding='utf_8') as fp:
         response = self.client.post(
             '/admin/suricata/sourcesuricata/add/', {
                 'method':
                 MethodUpload.get_by_name("Upload file").id,
                 'file':
                 fp,
                 'scheduled_rules_deployment_enabled':
                 'False',
                 'scheduled_deploy':
                 'False',
                 'data_type':
                 DataTypeUpload.get_by_name("one file not compressed").id,
                 'rulesets':
                 '1',
             },
             follow=True)
     self.assertEqual(response.status_code, 200)
     self.assertIn('SignatureSuricata has no classtype.',
                   str(response.content))
     self.assertEqual(len(SignatureSuricata.get_all()), 2)
Пример #4
0
 def test_source_signature_file_misp(self):
     response = self.client.post('/admin/suricata/sourcesuricata/add/', {
         'method':
         MethodUpload.get_by_name("MISP").id,
         'scheduled_rules_deployment_enabled':
         'False',
         'scheduled_deploy':
         'False',
         'data_type':
         DataTypeUpload.get_by_name("one file not compressed").id
     },
                                 follow=True)
     self.assertEqual(response.status_code, 200)
     self.assertIn('Upload source in progress.', str(response.content))
Пример #5
0
    def test_method_upload(self):
        all_method_upload = MethodUpload.get_all()
        method_upload = MethodUpload.get_by_id(1)
        self.assertEqual(len(all_method_upload), 2)
        self.assertEqual(method_upload.name, "Upload file")
        self.assertEqual(str(method_upload), "Upload file")

        method_upload = MethodUpload.get_by_name("Upload file")
        self.assertEqual(method_upload.name, "Upload file")
        with self.assertLogs('rules.models', level='DEBUG'):
            MethodUpload.get_by_name("https")

        method_upload = MethodUpload.get_by_id(99)
        self.assertEqual(method_upload, None)
        with self.assertRaises(AttributeError):
            method_upload.name
        with self.assertLogs('rules.models', level='DEBUG'):
            MethodUpload.get_by_id(99)
        with self.assertRaises(IntegrityError):
            MethodUpload.objects.create(name="Upload file")
Пример #6
0
 def test_source_signature_http_multiple_files(self):
     response = self.client.post('/admin/suricata/sourcesuricata/add/', {
         'method':
         MethodUpload.get_by_name("URL HTTP").id,
         'uri':
         'https://rules.emergingthreats.net/open/suricata-2.0.1/'
         'emerging.rules.tar.gz',
         'scheduled_rules_deployment_enabled':
         'True',
         'scheduled_rules_deployment_crontab':
         CrontabSchedule.objects.get(id=1).id,
         'scheduled_deploy':
         'False',
         'data_type':
         DataTypeUpload.get_by_name("multiple files in compressed file").id
     },
                                 follow=True)
     self.assertEqual(response.status_code, 200)
     self.assertIn('Upload source in progress.', str(response.content))
Пример #7
0
 def test_source_signature_file_one_file(self):
     with open(settings.BASE_DIR +
               '/suricata/tests/data/sslblacklist.rules',
               encoding='utf_8') as fp:
         response = self.client.post(
             '/admin/suricata/sourcesuricata/add/', {
                 'method':
                 MethodUpload.get_by_name("Upload file").id,
                 'file':
                 fp,
                 'scheduled_rules_deployment_enabled':
                 'False',
                 'scheduled_deploy':
                 'False',
                 'data_type':
                 DataTypeUpload.get_by_name("one file not compressed").id
             },
             follow=True)
     self.assertEqual(response.status_code, 200)
     self.assertIn('File uploaded successfully :', str(response.content))
Пример #8
0
 def test_source_signature_file_multiple_files(self):
     with open(
             settings.BASE_DIR +
             '/suricata/tests/data/emerging.rules.tar.gz', 'rb') as fp:
         response = self.client.post(
             '/admin/suricata/sourcesuricata/add/', {
                 'method':
                 MethodUpload.get_by_name("Upload file").id,
                 'file':
                 fp,
                 'scheduled_rules_deployment_enabled':
                 'False',
                 'scheduled_deploy':
                 'False',
                 'data_type':
                 DataTypeUpload.get_by_name(
                     "multiple files in compressed file").id
             },
             follow=True)
     self.assertEqual(response.status_code, 200)
     self.assertIn('File uploaded successfully :', str(response.content))
Пример #9
0
 def test_source_signature_http_one_file(self):
     for source in SourceSuricata.objects.all():
         source.delete()
     for p in PeriodicTask.objects.all():
         p.delete()
     response = self.client.post('/admin/suricata/sourcesuricata/add/', {
         'method':
         MethodUpload.get_by_name("URL HTTP").id,
         'uri':
         'https://sslbl.abuse.ch/blacklist/sslblacklist.rules',
         'scheduled_rules_deployment_enabled':
         'True',
         'scheduled_rules_deployment_crontab':
         CrontabSchedule.objects.get(id=1).id,
         'scheduled_deploy':
         'False',
         'data_type':
         DataTypeUpload.get_by_name("one file not compressed").id
     },
                                 follow=True)
     self.assertEqual(response.status_code, 200)
     self.assertIn('Upload source in progress.', str(response.content))
Пример #10
0
    def test_source_suricata(self):
        all_source_suricata = SourceSuricata.get_all()
        source_suricata = SourceSuricata.get_by_id(1)
        self.assertEqual(len(all_source_suricata), 2)
        self.assertEqual(source_suricata.method.name, "URL HTTP")
        self.assertEqual(
            str(source_suricata),
            "https://sslbl.abuse.ch/blacklist/sslblacklist.rules")
        source_suricata = SourceSuricata.get_by_id(99)
        self.assertEqual(source_suricata, None)
        source_misp = SourceSuricata.objects.create(
            method=MethodUpload.get_by_name("MISP"),
            scheduled_rules_deployment_enabled=False,
            scheduled_deploy=False,
            data_type=DataTypeUpload.get_by_name("one file not compressed"))
        self.assertEqual((1, 0, 0, 0), source_misp.download_from_misp())
        conf = CoreConfiguration.objects.get(key="MISP_HOST")
        conf.value = ""
        conf.save()
        with self.assertRaisesMessage(Exception, 'Missing MISP Configuration'):
            source_misp.download_from_misp()

        SourceSuricata.get_by_uri(
            'https://sslbl.abuse.ch/blacklist/sslblacklist.rules').delete()
        source = SourceSuricata.objects.create(
            method=MethodUpload.get_by_name("URL HTTP"),
            uri='https://sslbl.abuse.ch/blacklist/sslblacklist.rules',
            scheduled_rules_deployment_enabled=False,
            scheduled_deploy=False,
            data_type=DataTypeUpload.get_by_name("one file not compressed"))
        self.assertGreater(source.download_from_http()[0], 2000)
        self.assertGreater(source.download_from_http()[1], 2000)

        SourceSuricata.get_by_uri(
            'https://rules.emergingthreats.net/open/'
            'suricata-3.3.1/emerging.rules.tar.gz').delete()
        source = SourceSuricata.objects.create(
            method=MethodUpload.get_by_name("URL HTTP"),
            uri='https://rules.emergingthreats.net/open/'
            'suricata-3.3.1/emerging.rules.tar.gz',
            scheduled_rules_deployment_enabled=False,
            scheduled_deploy=False,
            data_type=DataTypeUpload.get_by_name(
                "multiple files in compressed file"))
        self.assertGreater(source.download_from_http()[0], 2000)
        self.assertGreater(source.download_from_http()[1], 2000)

        with open(settings.BASE_DIR + '/suricata/tests/data/test.rules',
                  encoding='utf_8') as fp:
            source = SourceSuricata.objects.create(
                method=MethodUpload.get_by_name("Upload file"),
                uri="test_signature",
                file=fp.name,
                scheduled_rules_deployment_enabled=False,
                scheduled_deploy=False,
                data_type=DataTypeUpload.get_by_name(
                    "one file not compressed"))
            self.assertEqual((2, 0, 0, 0), source.download_from_file(fp.name))
        with open(settings.BASE_DIR + '/suricata/tests/data/error.rules',
                  encoding='utf_8') as fp:
            source = SourceSuricata.objects.create(
                method=MethodUpload.get_by_name("Upload file"),
                uri="test_signature_error",
                file=fp.name,
                scheduled_rules_deployment_enabled=False,
                scheduled_deploy=False,
                data_type=DataTypeUpload.get_by_name(
                    "one file not compressed"))
            self.assertEqual((0, 8, 0, 0), source.download_from_file(fp.name))
        with open(settings.BASE_DIR + '/suricata/tests/data/test-script.lua',
                  encoding='utf_8') as fp:
            source = SourceSuricata.objects.create(
                method=MethodUpload.get_by_name("Upload file"),
                uri="test_script",
                file=fp.name,
                scheduled_rules_deployment_enabled=False,
                scheduled_deploy=False,
                data_type=DataTypeUpload.get_by_name(
                    "one file not compressed"))
            self.assertEqual((0, 0, 1, 0), source.download_from_file(fp.name))

        with self.assertRaises(IntegrityError):
            SourceSuricata.objects.create(
                uri="https://sslbl.abuse.ch/blacklist/sslblacklist.rules")