This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
1171 lines (1068 loc) · 51.7 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import csv
import io
import logging
import os
import re
import glob
import ssl
import subprocess
import tarfile
import urllib.request
from string import Template
import select2.fields
from django.conf import settings
from django.db import models
from django.db.models import Q
from django.utils import timezone
from django_celery_beat.models import PeriodicTask
from pymisp import PyMISP
from core.models import Configuration as CoreConfiguration
from core.models import Probe, ProbeConfiguration
from core.modelsmixins import CommonMixin
from core.notifications import send_notification
from core.ssh import execute, execute_copy
from core.utils import process_cmd, create_deploy_rules_task, create_check_task
from rules.models import RuleSet, Rule, Source
from .exceptions import RuleNotFoundParam
from .utils import create_conf, convert_conf
logger = logging.getLogger('suricata')
class ClassType(CommonMixin, models.Model):
"""
Set of Classification for a signature.
The classtype keyword gives information about the classification of rules and alerts.
"""
name = models.CharField(max_length=100, unique=True, db_index=True)
description = models.CharField(max_length=1000)
severity_level = models.IntegerField(default=0)
def __str__(self):
return self.name
@classmethod
def get_by_name(cls, name):
try:
obj = cls.objects.get(name=name)
except cls.DoesNotExist as e:
logger.debug('Tries to access an object that does not exist : ' + str(e))
return None
return obj
class ValidationType(CommonMixin, models.Model):
"""
Set of validation value (yes, no).
"""
name = models.CharField(max_length=100, unique=True, null=False, blank=False)
def __str__(self):
return self.name
class AppLayerType(CommonMixin, models.Model):
"""
Used for the choices for the detection of application protocol. (yes, no, detection-only)
"""
name = models.CharField(max_length=100, unique=True, null=False, blank=False)
def __str__(self):
return self.name
class Configuration(ProbeConfiguration):
"""
Configuration for Suricata IDS, Allows you to reuse the configuration.
"""
probeconfiguration = models.OneToOneField(ProbeConfiguration, parent_link=True,
related_name='suricata_configuration',
on_delete=models.CASCADE, editable=False)
with open(settings.BASE_DIR + "/suricata/default-Suricata-conf.yaml", encoding='utf_8') as f:
CONF_FULL_DEFAULT = f.read()
conf_rules_directory = models.CharField(max_length=400, default="/etc/suricata/rules")
conf_iprep_directory = models.CharField(max_length=400, default='/etc/suricata/iprep')
conf_lua_directory = models.CharField(max_length=400, default='/etc/suricata/lua-output')
conf_file = models.CharField(max_length=400, default="/etc/suricata/suricata.yaml")
conf_advanced = models.BooleanField(default=False)
conf_advanced_text = models.TextField(default=CONF_FULL_DEFAULT)
conf_HOME_NET = models.CharField(max_length=100, default="[192.168.0.0/24]")
conf_EXTERNAL_NET = models.CharField(max_length=100, default="!$HOME_NET")
conf_HTTP_SERVERS = models.CharField(max_length=100, default="$HOME_NET")
conf_SMTP_SERVERS = models.CharField(max_length=100, default="$HOME_NET")
conf_SQL_SERVERS = models.CharField(max_length=100, default="$HOME_NET")
conf_DNS_SERVERS = models.CharField(max_length=100, default="$HOME_NET")
conf_TELNET_SERVERS = models.CharField(max_length=100, default="$HOME_NET")
conf_AIM_SERVERS = models.CharField(max_length=100, default="$EXTERNAL_NET")
conf_DNP3_SERVER = models.CharField(max_length=100, default="$HOME_NET")
conf_DNP3_CLIENT = models.CharField(max_length=100, default="$HOME_NET")
conf_MODBUS_CLIENT = models.CharField(max_length=100, default="$HOME_NET")
conf_MODBUS_SERVER = models.CharField(max_length=100, default="$HOME_NET")
conf_ENIP_CLIENT = models.CharField(max_length=100, default="$HOME_NET")
conf_ENIP_SERVER = models.CharField(max_length=100, default="$HOME_NET")
conf_HTTP_PORTS = models.CharField(max_length=100, default="80")
conf_SHELLCODE_PORTS = models.CharField(max_length=100, default="!80")
conf_ORACLE_PORTS = models.CharField(max_length=100, default="1521")
conf_SSH_PORTS = models.CharField(max_length=100, default="22")
conf_DNP3_PORTS = models.CharField(max_length=100, default="20000")
conf_MODBUS_PORTS = models.CharField(max_length=100, default="502")
conf_stats = models.ForeignKey(ValidationType, related_name="conf_stats", default=1, on_delete=models.CASCADE)
conf_afpacket_interface = models.CharField(max_length=100, default='eth0')
conf_outputs_fast = models.ForeignKey(ValidationType, related_name="conf_outputs_fast", default=1,
on_delete=models.CASCADE)
conf_outputs_evelog = models.ForeignKey(ValidationType, related_name="conf_outputs_evelog", default=0,
on_delete=models.CASCADE)
conf_outputs_evelog_alert_http = models.ForeignKey(ValidationType, related_name="conf_outputs_evelog_alert_http",
default=0, on_delete=models.CASCADE)
conf_outputs_evelog_alert_tls = models.ForeignKey(ValidationType, related_name="conf_outputs_evelog_alert_tls",
default=0, on_delete=models.CASCADE)
conf_outputs_evelog_alert_ssh = models.ForeignKey(ValidationType, related_name="conf_outputs_evelog_alert_ssh",
default=0, on_delete=models.CASCADE)
conf_outputs_evelog_alert_smtp = models.ForeignKey(ValidationType, related_name="conf_outputs_evelog_alert_smtp",
default=0, on_delete=models.CASCADE)
conf_outputs_evelog_alert_dnp3 = models.ForeignKey(ValidationType, related_name="conf_outputs_evelog_alert_dnp3",
default=0, on_delete=models.CASCADE)
conf_outputs_evelog_alert_taggedpackets = models.ForeignKey(ValidationType,
related_name="conf_outputs_evelog_alert_taggedpackets",
default=0, on_delete=models.CASCADE)
conf_outputs_evelog_xff = models.ForeignKey(ValidationType, related_name="conf_outputs_evelog_xff", default=1,
on_delete=models.CASCADE)
conf_outputs_evelog_dns_query = models.ForeignKey(ValidationType, related_name="conf_outputs_evelog_dns_query",
default=0, on_delete=models.CASCADE)
conf_outputs_evelog_dns_answer = models.ForeignKey(ValidationType, related_name="conf_outputs_evelog_dns_answer",
default=0, on_delete=models.CASCADE)
conf_outputs_evelog_http_extended = models.ForeignKey(ValidationType,
related_name="conf_outputs_evelog_http_extended", default=0,
on_delete=models.CASCADE)
conf_outputs_evelog_tls_extended = models.ForeignKey(ValidationType,
related_name="conf_outputs_evelog_tls_extended", default=0,
on_delete=models.CASCADE)
conf_outputs_evelog_files_forcemagic = models.ForeignKey(ValidationType,
related_name="conf_outputs_evelog_files_forcemagic",
default=1, on_delete=models.CASCADE)
conf_outputs_unified2alert = models.ForeignKey(ValidationType, related_name="conf_outputs_unified2alert", default=1,
on_delete=models.CASCADE)
conf_lua = models.ForeignKey(ValidationType, related_name="conf_lua", default=1, on_delete=models.CASCADE)
conf_applayer_tls = models.ForeignKey(AppLayerType, related_name="conf_applayer_tls", default=0,
on_delete=models.CASCADE)
conf_applayer_dcerpc = models.ForeignKey(AppLayerType, related_name="conf_applayer_dcerpc", default=0,
on_delete=models.CASCADE)
conf_applayer_ftp = models.ForeignKey(AppLayerType, related_name="conf_applayer_ftp", default=0,
on_delete=models.CASCADE)
conf_applayer_ssh = models.ForeignKey(AppLayerType, related_name="conf_applayer_ssh", default=0,
on_delete=models.CASCADE)
conf_applayer_smtp = models.ForeignKey(AppLayerType, related_name="conf_applayer_smtp", default=0,
on_delete=models.CASCADE)
conf_applayer_imap = models.ForeignKey(AppLayerType, related_name="conf_applayer_imap", default=2,
on_delete=models.CASCADE)
conf_applayer_msn = models.ForeignKey(AppLayerType, related_name="conf_applayer_msn", default=2,
on_delete=models.CASCADE)
conf_applayer_smb = models.ForeignKey(AppLayerType, related_name="conf_applayer_smb", default=0,
on_delete=models.CASCADE)
conf_applayer_dns = models.ForeignKey(AppLayerType, related_name="conf_applayer_dns", default=0,
on_delete=models.CASCADE)
conf_applayer_http = models.ForeignKey(AppLayerType, related_name="conf_applayer_http", default=0,
on_delete=models.CASCADE)
def __str__(self):
return self.name
def save(self, **kwargs):
if not self.conf_advanced:
create_conf(self)
else:
convert_conf(self)
super().save(**kwargs)
def test(self):
with self.get_tmp_dir(self.pk) as tmp_dir:
rule_file = settings.BASE_DIR + "/suricata/tests/data/test.rules"
conf_file = tmp_dir + self.name + ".yaml"
config = self.conf_advanced_text
config += """
logging:
default-log-level: error
outputs:
- console:
enabled: yes
- file:
enabled: no
filename: /var/log/suricata/suricata.log
level: info
"""
with open(conf_file, 'w', encoding='utf_8') as f:
f.write(str(config))
cmd = [settings.SURICATA_BINARY, '-T',
'-l', tmp_dir,
'-S', rule_file,
'-c', conf_file,
'--set', 'classification-file=' + settings.BASE_DIR + '/suricata/tests/data/classification.config',
'--set', 'reference-config-file=' + settings.BASE_DIR + '/suricata/tests/data/reference.config',
]
return process_cmd(cmd, tmp_dir)
class SignatureSuricata(Rule):
"""
Stores a signature Suricata compatible. (pattern matching),
see http://suricata.readthedocs.io/en/latest/rules/index.html
"""
sid = models.IntegerField(unique=True, db_index=True,
help_text="<a target='_blank' " +
"href='http://doc.emergingthreats.net/bin/view/Main/SidAllocation'>help</a>")
classtype = models.ForeignKey(ClassType, on_delete=models.CASCADE)
msg = models.CharField(max_length=1000)
file_test_success = models.FileField(name='file_test_success', upload_to='file_test_success', blank=True)
class Meta:
verbose_name = 'Signature'
verbose_name_plural = 'Signatures'
def __str__(self):
return str(self.sid) + " : " + str(self.msg)
@classmethod
def get_by_sid(cls, sid):
try:
obj = cls.objects.get(sid=sid)
except cls.DoesNotExist as e:
logger.debug('Tries to access an object that does not exist : ' + str(e))
return None
return obj
@classmethod
def find(cls, pattern):
"""Search the pattern in all the signatures"""
return cls.objects.filter(rule_full__contains=pattern)
@classmethod
def extract_attributs(cls, line, rulesets=None): # TODO -> too complex
rule_created = False
rule_updated = False
getsid = re.compile("sid *: *(\d+)")
getrev = re.compile("rev *: *(\d+)")
getmsg = re.compile("msg *: *\"(.*?)\"")
getref = re.compile("reference *: *url,(.*?);")
getct = re.compile("classtype *:(.*?);")
if "->" in line and "sid" in line and ")" in line:
try:
match = getsid.search(line)
if not match:
raise RuleNotFoundParam("SID not found in : " + line)
# test if exist already -> update
if SignatureSuricata.get_by_sid(match.groups()[0]):
signature = cls.get_by_sid(match.groups()[0])
signature.updated_date = timezone.now()
rule_updated = True
else:
signature = SignatureSuricata()
signature.created_date = timezone.now()
signature.sid = match.groups()[0]
rule_created = True
if rule_created:
signature.enabled = True
if line.startswith('#'):
line = line.lstrip("# ")
if rule_created:
signature.enabled = False
signature.rule_full = line
match = getrev.search(line)
if match:
signature.rev = int(match.groups()[0])
# print("rev : " + str(signature.rev))
else:
signature.rev = 0
match = getmsg.search(line)
if not match:
signature.msg = ""
else:
signature.msg = match.groups()[0]
# print("msg : " + signature.msg)
match = getref.search(line)
if not match:
signature.reference = ""
else:
signature.reference = match.groups()[0]
# print("ref : " + signature.reference)
match = getct.search(line)
if not match:
# print("ClassType not found in : " + line)
signature.classtype = ClassType.get_by_id(1)
else:
# print("classType #" + match.groups()[0].strip() + "#")
signature.classtype = ClassType.get_by_name(match.groups()[0].strip())
if not signature.classtype:
raise RuleNotFoundParam("ClassType not found in : " + line)
signature.save()
if rulesets:
for ruleset in rulesets:
ruleset.signatures.add(signature)
ruleset.save()
# print("rule saved : " + str(signature.sid))
return rule_created, rule_updated
except RuleNotFoundParam:
return rule_created, rule_updated
return rule_created, rule_updated
def test(self):
with self.get_tmp_dir("test_sig") as tmp_dir:
ScriptSuricata.copy_to_rules_directory_for_test()
rule_file = tmp_dir + str(self.sid) + ".rules"
with open(rule_file, 'w', encoding='utf_8') as f:
f.write(self.rule_full)
cmd = [settings.SURICATA_BINARY, '-T',
'-l', tmp_dir,
'-S', rule_file,
'-c', settings.SURICATA_CONFIG
]
return process_cmd(cmd, tmp_dir)
def test_pcap(self):
with self.get_tmp_dir("test_pcap") as tmp_dir:
ScriptSuricata.copy_to_rules_directory_for_test()
rule_file = tmp_dir + "rule.rules"
conf_file = tmp_dir + "suricata.yaml"
with open(rule_file, 'w', encoding='utf_8') as f:
f.write(self.rule_full)
with open(settings.BASE_DIR + "/suricata/default-Suricata-conf.yaml", encoding='utf_8') as f:
conf_full_default = f.read()
config = conf_full_default
config += """
logging:
default-log-level: error
outputs:
- console:
enabled: yes
- file:
enabled: no
filename: /var/log/suricata/suricata.log
level: info
"""
with open(conf_file, 'w', encoding='utf_8') as f:
f.write(config)
# test pcap success
cmd = [settings.SURICATA_BINARY,
'-l', tmp_dir,
'-S', rule_file,
'-c', conf_file,
'-r', settings.BASE_DIR + "/" + self.file_test_success.name,
'--set', 'outputs.0.fast.enabled=yes',
'--set', 'classification-file=' + settings.BASE_DIR + '/suricata/tests/data/classification.config',
'--set', 'reference-config-file=' + settings.BASE_DIR + '/suricata/tests/data/reference.config',
]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
outdata, errdata = process.communicate()
logger.debug("outdata : " + str(outdata), "errdata : " + str(errdata))
# test if alert is generated :
test = False
if os.path.isfile(tmp_dir + "fast.log"):
with open(tmp_dir + "fast.log", "r", encoding='utf_8') as f:
if self.msg in f.read():
test = True
# if success ok
if process.returncode == 0 and test:
return {'status': True}
# if not -> return error
errdata += b"Alert not generated"
return {'status': False, 'errors': errdata}
def test_all(self):
test = True
errors = list()
response = self.test()
if not response['status']:
test = False
errors.append(str(self) + " : " + str(response['errors']))
if self.file_test_success:
response_pcap = self.test_pcap()
if not response_pcap['status']:
test = False
errors.append(str(self) + " : " + str(response_pcap['errors']))
if test:
return {'status': True}
else:
return {'status': False, 'errors': errors}
class ScriptSuricata(Rule):
"""
Stores a script Suricata compatible.
see : http://suricata.readthedocs.io/en/latest/rules/rule-lua-scripting.html
"""
filename = models.CharField(max_length=1000, unique=True, db_index=True)
class Meta:
verbose_name = 'Script'
verbose_name_plural = 'Scripts'
def __str__(self):
return str(self.filename)
@classmethod
def get_by_filename(cls, filename):
try:
obj = cls.objects.get(filename=filename)
except cls.DoesNotExist as e:
logger.debug('Tries to access an object that does not exist : ' + str(e))
return None
return obj
@classmethod
def find(cls, pattern):
"""Search the pattern in all the scripts"""
return cls.objects.filter(rule_full__contains=pattern)
@classmethod
def extract_attributs(cls, file, rulesets=None):
""" A script by file """
rule_created = False
rule_updated = False
if not cls.get_by_filename(os.path.basename(file.name)):
rule_created = True
script = ScriptSuricata()
script.filename = os.path.basename(file.name)
script.created_date = timezone.now()
script.rev = 0
script.rule_full = file.read()
else:
rule_updated = True
script = cls.get_by_filename(file.name)
script.rule_full = file.read()
script.rev = script.rev + 1
script.updated_date = timezone.now()
script.save()
if rulesets:
for ruleset in rulesets:
ruleset.scripts.add(script)
ruleset.save()
return rule_created, rule_updated
def test_lua_output(self):
for f in glob.glob(settings.SURICATA_LUA + '/*'):
os.remove(f)
if 'function setup' in self.rule_full and 'function deinit' in self.rule_full:
with open(settings.SURICATA_LUA + '/' + self.filename, 'w') as f:
f.write(self.rule_full.replace('\r', ''))
with self.get_tmp_dir("test_script") as tmp_dir:
rule_file = settings.BASE_DIR + "/suricata/tests/data/test.rules"
cmd = [settings.SURICATA_BINARY, '-T',
'-l', tmp_dir,
'-S', rule_file,
'-c', settings.SURICATA_CONFIG
]
return process_cmd(cmd, tmp_dir)
else:
return {'status': False, 'errors': "Not a Lua script used to generate output from Suricata."}
@classmethod
def copy_to_rules_directory_for_test(cls):
for script in cls.get_all():
if 'function setup' not in script.rule_full and 'function deinit' not in script.rule_full:
with open(settings.SURICATA_RULES + '/' + script.filename, 'w') as f:
f.write(script.rule_full.replace('\r', ''))
class RuleSetSuricata(RuleSet):
"""
Set of signatures and scripts Suricata compatible
"""
signatures = select2.fields.ManyToManyField(SignatureSuricata,
blank=True,
ajax=True,
search_field=lambda q: Q(sid__icontains=q) | Q(msg__icontains=q),
sort_field='sid',
js_options={'quiet_millis': 200}
)
scripts = select2.fields.ManyToManyField(ScriptSuricata,
blank=True,
ajax=True,
search_field=lambda q: Q(sid__icontains=q) | Q(filename__icontains=q),
sort_field='sid',
js_options={'quiet_millis': 200}
)
class Meta:
verbose_name = 'Ruleset'
verbose_name_plural = 'Rulesets'
def __str__(self):
return str(self.name)
def test_rules(self):
test = True
errors = list()
for signature in self.signatures.all():
response = signature.test()
if not response['status']:
test = False
errors.append(str(signature) + " : " + str(response['errors']))
if not test:
return {'status': False, 'errors': str(errors)}
return {'status': True}
class SourceSuricata(Source):
"""
Set of Suricata Source. For scheduled upload of signatures.
"""
rulesets = models.ManyToManyField(RuleSetSuricata, blank=True)
class Meta:
verbose_name = 'Source'
verbose_name_plural = 'Sources'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.type = self.__class__.__name__
def delete(self, **kwargs):
try:
periodic_task = PeriodicTask.objects.get(name=self.uri + '_download_from_http_task')
periodic_task.delete()
logger.debug(str(periodic_task) + " deleted")
except PeriodicTask.DoesNotExist:
pass
try:
for ruleset in self.rulesets.all():
for probe in ruleset.suricata_set.all():
periodic_task = PeriodicTask.objects.get(
name__contains=probe.name + "_" + self.uri + "_deploy_rules_")
periodic_task.delete()
logger.debug(str(periodic_task) + " deleted")
except PeriodicTask.DoesNotExist:
pass
return super().delete(**kwargs)
@staticmethod
def find_rules(file, file_name, rulesets):
count_signature_created = 0
count_signature_updated = 0
count_script_created = 0
count_script_updated = 0
if os.path.splitext(file_name)[1] == '.rules':
try:
for line in file.readlines():
rule_created, rule_updated = SignatureSuricata.extract_attributs(line, rulesets)
if rule_created:
count_signature_created += 1
if rule_updated:
count_signature_updated += 1
except UnicodeDecodeError as e:
logger.warning("UnicodeDecodeError: " + str(e))
elif os.path.splitext(file_name)[1] == '.lua':
rule_created, rule_updated = ScriptSuricata.extract_attributs(file, rulesets)
if rule_created:
count_script_created += 1
if rule_updated:
count_script_updated += 1
return count_signature_created, count_signature_updated, count_script_created, count_script_updated
def extract_files(self, file_downloaded, rulesets=None):
count = (0, 0, 0, 0)
with self.get_tmp_dir(self.pk) as tmp_dir:
with open(tmp_dir + "temp.tar.gz", 'wb') as f:
f.write(file_downloaded)
with tarfile.open(tmp_dir + "temp.tar.gz", encoding='utf_8') as tar:
for member in tar.getmembers():
if member.isfile():
file = io.TextIOWrapper(tar.extractfile(member), encoding='utf_8')
count = tuple(map(sum, zip(count, self.find_rules(file, member.name, rulesets))))
return count
def download_from_misp(self, rulesets=None):
if CoreConfiguration.get_value("MISP_HOST") and CoreConfiguration.get_value("MISP_API_KEY"):
misp = PyMISP(CoreConfiguration.get_value("MISP_HOST"), CoreConfiguration.get_value("MISP_API_KEY"), True)
with self.get_tmp_dir(self.pk) as tmp_dir:
with open(tmp_dir + 'misp.rules', 'w', encoding='utf_8') as f:
f.write(misp.download_all_suricata().text)
with open(tmp_dir + 'misp.rules', 'r', encoding='utf_8') as f:
return self.find_rules(f, 'misp.rules', rulesets)
else:
logger.error('Missing MISP Configuration')
raise Exception('Missing MISP Configuration')
def download_from_file(self, file_name, rulesets=None):
# Upload file - multiple files in compressed file
if self.data_type.name == "multiple files in compressed file":
logger.debug('multiple files in compressed file')
file_downloaded = self.file.read()
return self.extract_files(file_downloaded, rulesets)
# Upload file - one file not compressed
elif self.data_type.name == "one file not compressed":
logger.debug('one file not compressed')
with self.get_tmp_dir(self.pk) as tmp_dir:
with open(tmp_dir + "temp.rules", 'wb') as f:
f.write(self.file.read())
with open(tmp_dir + "temp.rules", 'r', encoding='utf_8') as f:
return self.find_rules(f, file_name, rulesets)
else:
logger.error('Data type upload unknown: ' + self.data_type.name)
raise Exception('Data type upload unknown : ' + self.data_type.name)
def download_from_http(self, rulesets=None):
context = ssl._create_unverified_context()
response = urllib.request.urlopen(self.uri, context=context)
file_dowloaded = response.read()
# URL HTTP - multiple files in compressed file
if self.data_type.name == "multiple files in compressed file":
logger.debug("multiple files in compressed file")
if response.info()['Content-type'] == 'application/x-gzip' or \
response.info()['Content-type'] == 'application/x-tar':
return self.extract_files(file_dowloaded, rulesets)
else:
logger.error('Compression format unknown : ' + str(response.info()['Content-type']))
raise Exception('Compression format unknown : ' + str(response.info()['Content-type']))
# URL HTTP - one file not compressed
elif self.data_type.name == "one file not compressed":
logger.debug("one file not compressed")
if response.info()['Content-type'] == 'text/plain':
with self.get_tmp_dir(self.pk) as tmp_dir:
with open(tmp_dir + "temp.rules", 'wb') as f:
f.write(file_dowloaded)
with open(tmp_dir + "temp.rules", 'r', encoding='utf_8') as f:
return self.find_rules(f, 'temp.rules', rulesets)
else:
logger.error('Compression format unknown : ' + str(response.info()['Content-type']))
raise Exception('Compression format unknown : ' + str(response.info()['Content-type']))
else:
logger.error('Data type upload unknown.')
raise Exception('Data type upload unknown.')
class Suricata(Probe):
"""
Stores an instance of Suricata IDS software.
"""
rulesets = models.ManyToManyField(RuleSetSuricata, blank=True)
configuration = models.ForeignKey(Configuration, on_delete=models.CASCADE)
class Meta:
verbose_name = 'Suricata instance'
verbose_name_plural = 'Suricata instances'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.type = self.__class__.__name__
def __str__(self):
return self.name + " " + self.description
def save(self, **kwargs):
update = False
if self.id:
update = True
super().save(**kwargs)
if not update:
create_deploy_rules_task(self)
create_check_task(self)
def delete(self, **kwargs):
try:
periodic_task = PeriodicTask.objects.get(
name=self.name + "_deploy_rules_" + str(self.scheduled_rules_deployment_crontab))
periodic_task.delete()
logger.debug(str(periodic_task) + " deleted")
except PeriodicTask.DoesNotExist: # pragma: no cover
pass
try:
periodic_task = PeriodicTask.objects.get(name=self.name + "_check_task")
periodic_task.delete()
logger.debug(str(periodic_task) + " deleted")
except PeriodicTask.DoesNotExist: # pragma: no cover
pass
return super().delete(**kwargs)
def install(self, version=settings.SURICATA_VERSION):
if self.server.os.name == 'debian':
install_script = """
if ! type suricata ; then
echo 'deb http://http.debian.net/debian stretch-backports main' | \
sudo tee -a /etc/apt/sources.list.d/stretch-backports.list
apt update
apt -y -t stretch-backports install suricata
mkdir /etc/suricata/lua-output
mkdir /etc/suricata/iprep
touch /etc/suricata/iprep/categories.txt && touch /etc/suricata/iprep/reputation.list
chown -R $(whoami) /etc/suricata
exit 0
else
echo "Already installed"
exit 0
fi
"""
elif self.server.os.name == 'ubuntu':
install_script = """
if ! type suricata ; then
add-apt-repository -y ppa:oisf/suricata-stable
apt update
apt -y install suricata
mkdir /etc/suricata/lua-output
mkdir /etc/suricata/iprep
touch /etc/suricata/iprep/reputation.list && touch /etc/suricata/iprep/categories.txt
chown -R $(whoami) /etc/suricata
exit 0
else
echo "Already installed"
exit 0
fi
"""
else:
raise NotImplementedError
t = Template(install_script)
command = "sh -c '" + t.substitute(version=version) + "'"
tasks = {"install": command}
try:
response = execute(self.server, tasks, become=True)
self.installed = True
self.save()
except Exception as e:
logger.exception('install failed')
return {'status': False, 'errors': str(e)}
logger.debug("output : " + str(response))
return {'status': True}
def update(self, version=settings.SURICATA_VERSION):
return self.install(version=version)
def reload(self):
if self.server.os.name == 'debian' or self.server.os.name == 'ubuntu':
command = "kill -USR2 $( pidof suricata )"
else:
raise NotImplementedError
tasks = {"reload": command}
try:
response = execute(self.server, tasks, become=True)
except Exception as e:
logger.exception('reload failed')
return {'status': False, 'errors': str(e)}
logger.debug("output : " + str(response))
return {'status': True}
def test_rules(self):
# Set blacklists file
value = ""
for md5 in Md5.get_all():
value += md5.value + '\n'
if not os.path.exists(settings.SURICATA_RULES):
os.mkdir(settings.SURICATA_RULES)
with open(settings.SURICATA_RULES + '/md5-blacklist', 'w', encoding='utf_8') as f:
f.write(value)
test = True
errors = list()
for ruleset in self.rulesets.all():
for signature in ruleset.signatures.all():
response = signature.test()
if not response['status']:
test = False
errors.append(str(response['errors']))
if test:
return {'status': True}
else:
return {'status': False, 'errors': errors}
def test_pcaps(self):
test = True
errors = list()
for ruleset in self.rulesets.all():
for signature in ruleset.signatures.all():
if signature.file_test_success:
response_pcap_test = signature.test_pcap()
if not response_pcap_test['status']:
test = False
errors.append(str(signature) + " : " + str(response_pcap_test['errors']))
else:
return {'status': True}
if test:
return {'status': True}
else:
return {'status': False, 'errors': errors}
def deploy_rules(self):
# Tests
try:
response_rules = self.test_rules()
response_pcaps = self.test_pcaps()
if self.secure_deployment:
if not response_rules['status']:
if self.secure_deployment:
logger.error("Error during the rules test for probe " + str(self.name) + ' : ' +
str(response_rules['errors']))
return {"status": False,
"message": "Error during the rules test for probe " + str(self.name) + ' : ' +
str(response_rules['errors'])}
else:
logger.error("Error during the rules test for probe " + str(self.name) + ' : ' +
str(response_rules['errors']))
send_notification('Error',
'Error during the rules test for probe ' + str(self.name) + ' : ' +
str(response_rules['errors']))
elif not response_pcaps['status']:
if self.secure_deployment:
logger.error("Error during the rules test for probe " + str(self.name) + ' : ' +
str(response_pcaps['errors']))
return {"status": False,
"message": "Error during the pcap test for probe " + str(self.name) + ' : ' +
str(response_pcaps['errors'])}
else:
logger.error("Error during the rules test for probe " + str(self.name) + ' : ' +
str(response_pcaps['errors']))
send_notification('Error',
'Error during the pcap test for probe ' + str(self.name) + ' : ' +
str(response_pcaps['errors']))
except Exception as e:
logger.exception("Error for probe " + str(self.name) + " during the tests")
return {"status": False, "message": "Error for probe " + str(self.name) + " during the tests",
"exception": str(e)}
deploy = True
response = dict()
errors = list()
# Signatures
value = ""
for ruleset in self.rulesets.all():
for signature in ruleset.signatures.all():
if signature.enabled:
value += signature.rule_full.replace('\r', '') + '\n'
with self.get_tmp_dir(self.pk) as tmp_dir:
with open(tmp_dir + "temp.rules", 'w', encoding='utf_8') as f:
f.write(value)
try:
response = execute_copy(self.server, src=tmp_dir + 'temp.rules',
dest=self.configuration.conf_rules_directory.rstrip('/') + '/deployed.rules',
become=True)
except Exception as e:
logger.exception('excecute_copy failed')
deploy = False
errors.append(str(e))
# Blacklists MD5
value = ""
for md5 in Md5.get_all():
value += md5.value + '\n'
with open(tmp_dir + "md5-blacklist", 'w', encoding='utf_8') as f:
f.write(value)
try:
response = execute_copy(self.server, src=tmp_dir + 'md5-blacklist',
dest=self.configuration.conf_rules_directory.rstrip('/') + '/md5-blacklist',
become=True)
except Exception as e:
logger.exception('excecute_copy failed')
deploy = False
errors.append(str(e))
# Scripts
for ruleset in self.rulesets.all():
for script in ruleset.scripts.all():
if script.enabled:
with open(tmp_dir + script.filename, 'w', encoding='utf_8') as f:
f.write(script.rule_full.replace('\r', ''))
try:
if 'function setup' in script.rule_full and 'function deinit' in script.rule_full:
response = execute_copy(self.server, src=tmp_dir + script.filename,
dest=self.configuration.conf_lua_directory.rstrip(
'/') + '/' + script.filename, become=True)
else:
response = execute_copy(self.server, src=tmp_dir + script.filename,
dest=self.configuration.conf_rules_directory.rstrip(
'/') + '/' + script.filename, become=True)
except Exception as e:
logger.exception('excecute_copy failed')
deploy = False
errors.append(str(e))
logger.debug("output : " + str(response))
if deploy:
self.rules_updated_date = timezone.now()
self.save()
return {"status": deploy}
else:
return {'status': deploy, 'errors': errors}
def deploy_conf(self):
with self.get_tmp_dir(self.pk) as tmp_dir:
value = self.configuration.conf_advanced_text
with open(tmp_dir + "temp.conf", 'w', encoding='utf_8') as f:
f.write(value)
deploy = True
errors = list()
response = dict()
try:
response = execute_copy(self.server, src=os.path.abspath(tmp_dir + 'temp.conf'),
dest=self.configuration.conf_file, become=True)
except Exception as e:
logger.exception('deploy conf failed')
deploy = False
errors.append(str(e))
logger.debug("output : " + str(response))
if deploy:
return {'status': deploy}
else:
return {'status': deploy, 'errors': errors}
class Md5(models.Model):
value = models.CharField(max_length=600, unique=True, null=False, blank=False)
signature = models.ForeignKey(SignatureSuricata, editable=False, on_delete=models.CASCADE)
@classmethod
def get_all(cls):
return cls.objects.all()
@classmethod
def get_by_value(cls, value):
try:
obj = cls.objects.get(value=value)
except cls.DoesNotExist as e:
logger.debug('Tries to access an object that does not exist : ' + str(e))
return None
return obj
def increment_sid():
DEFAULT_LAST_ID = 41000000
last_sid = BlackList.objects.all().order_by('id').last()
if not last_sid:
return DEFAULT_LAST_ID
else:
return last_sid.sid + 1
class BlackList(CommonMixin, models.Model):
"""
Stores an instance of a pattern in blacklist. TODO SHA1 SHA256
"""
TYPE_CHOICES = (
('IP', 'IP'),
('MD5', 'MD5'),
('HOST', 'HOST'),
)
type = models.CharField(max_length=255, choices=TYPE_CHOICES)
value = models.CharField(max_length=600, unique=True, null=False, blank=False)
comment = models.CharField(max_length=600, null=True, blank=True)
sid = models.IntegerField(unique=True, editable=False, null=False, default=increment_sid)
rulesets = models.ManyToManyField(RuleSetSuricata, blank=True)
def __str__(self):
return str(self.type) + " " + str(self.value)
def save(self, **kwargs):
super().save(**kwargs)
self.create_blacklist()
def delete(self, **kwargs):
if self.type == "MD5":
if Md5.get_by_value(self.value):
md5_suricata = Md5.get_by_value(self.value)
md5_suricata.delete()
else:
if SignatureSuricata.get_by_sid(self.sid):
signature = SignatureSuricata.get_by_sid(self.sid)
signature.delete()
return super().delete(**kwargs)
@classmethod
def get_by_value(cls, value):
try:
obj = cls.objects.get(value=value)
except cls.DoesNotExist as e:
logger.debug('Tries to access an object that does not exist : ' + str(e))
return None
return obj
def create_signature(self, t):
if not self.comment:
self.comment = str(self.type) + " " + str(self.value) + " in BlackList"
rule_created = t.safe_substitute(value=self.value,
type=self.type,
comment=self.comment,
sid=self.sid
)
if SignatureSuricata.get_by_sid(self.sid):
signature = SignatureSuricata.get_by_sid(self.sid)
signature.delete()
if self.type == "MD5":
signature = SignatureSuricata(sid=self.sid,
classtype=ClassType.get_by_name("misc-attack"),
msg="MD5 in blacklist",
rev=1,
rule_full=rule_created,
enabled=True,
created_date=timezone.now(),
)
else:
signature = SignatureSuricata(sid=self.sid,