forked from cisagov/Malcolm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzeek_carve_utils.py
1299 lines (1086 loc) · 49.7 KB
/
zeek_carve_utils.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2022 Battelle Energy Alliance, LLC. All rights reserved.
import clamd
import hashlib
import json
import malass_client
import os
import re
import requests
import sys
import time
import yara
import zmq
from abc import ABC, abstractmethod
from bs4 import BeautifulSoup
from collections import Counter
from collections import deque
from collections import defaultdict
from datetime import datetime
from multiprocessing import RawValue
from subprocess import PIPE, Popen
from threading import get_ident
from threading import Lock
###################################################################################################
VENTILATOR_PORT = 5987
SINK_PORT = 5988
TOPIC_FILE_SCAN = "file"
###################################################################################################
# modes for file preservation settings
PRESERVE_QUARANTINED = "quarantined"
PRESERVE_ALL = "all"
PRESERVE_NONE = "none"
PRESERVE_QUARANTINED_DIR_NAME = "quarantine"
PRESERVE_PRESERVED_DIR_NAME = "preserved"
###################################################################################################
FILE_SCAN_RESULT_SCANNER = "scanner"
FILE_SCAN_RESULT_FILE = "file"
FILE_SCAN_RESULT_FILE_SIZE = "size"
FILE_SCAN_RESULT_FILE_TYPE = "type"
FILE_SCAN_RESULT_ENGINES = "engines"
FILE_SCAN_RESULT_HITS = "hits"
FILE_SCAN_RESULT_MESSAGE = "message"
FILE_SCAN_RESULT_DESCRIPTION = "description"
###################################################################################################
# the notice field for the signature.log we're writing out mimicing Zeek
ZEEK_SIGNATURE_NOTICE = "Signatures::Sensitive_Signature"
###################################################################################################
# VirusTotal public API
VTOT_MAX_REQS = 4 # maximum 4 public API requests (default)
VTOT_MAX_SEC = 60 # in 60 seconds (default)
VTOT_CHECK_INTERVAL = 0.05
VTOT_URL = 'https://www.virustotal.com/vtapi/v2/file/report'
VTOT_RESP_NOT_FOUND = 0
VTOT_RESP_FOUND = 1
VTOT_RESP_QUEUED = -2
###################################################################################################
# Malass web API
MAL_MAX_REQS = 20 # maximum scanning requests concurrently
MAL_END_OF_TRANSACTION = 'End_of_Transaction'
MAL_SUBMIT_TIMEOUT_SEC = 60
MAL_CHECK_INTERVAL = 1
MAL_RESP_NOT_FOUND = 0
MAL_RESP_FOUND = 1
MAL_RESP_QUEUED = -2
###################################################################################################
# ClamAV Interface
CLAM_MAX_REQS = 8 # maximum scanning requests concurrently, should be <= clamd.conf MaxThreads
CLAM_SUBMIT_TIMEOUT_SEC = 10
CLAM_CHECK_INTERVAL = 0.1
CLAM_ENGINE_ID = 'ClamAV'
CLAM_FOUND_KEY = 'FOUND'
###################################################################################################
# Yara Interface
YARA_RULES_DIR = os.path.join(os.getenv('YARA_RULES_DIR', "/yara-rules"), '')
YARA_CUSTOM_RULES_DIR = os.path.join(YARA_RULES_DIR, "custom")
YARA_SUBMIT_TIMEOUT_SEC = 60
YARA_ENGINE_ID = 'Yara'
YARA_MAX_REQS = 8 # maximum scanning threads concurrently
YARA_CHECK_INTERVAL = 0.1
YARA_RUN_TIMEOUT_SEC = 300
###################################################################################################
# Capa
CAPA_MAX_REQS = 4 # maximum scanning threads concurrently
CAPA_SUBMIT_TIMEOUT_SEC = 60
CAPA_ENGINE_ID = 'Capa'
CAPA_CHECK_INTERVAL = 0.1
CAPA_MIMES_TO_SCAN = (
'application/bat',
'application/ecmascript',
'application/javascript',
'application/PowerShell',
'application/vnd.microsoft.portable-executable',
'application/x-bat',
'application/x-dosexec',
'application/x-elf',
'application/x-executable',
'application/x-msdos-program',
'application/x-msdownload',
'application/x-pe-app-32bit-i386',
'application/x-sh',
'text/jscript',
'text/vbscript',
'text/x-python',
'text/x-shellscript',
)
CAPA_VIV_SUFFIX = '.viv'
CAPA_VIV_MIME = 'data'
CAPA_ATTACK_KEY = 'att&ck'
CAPA_RUN_TIMEOUT_SEC = 300
###################################################################################################
# a structure representing the fields of a line of Zeek's signatures.log, and the corresponding string formatting and type definitions
class BroSignatureLine:
__slots__ = (
'ts',
'uid',
'orig_h',
'orig_p',
'resp_h',
'resp_p',
'note',
'signature_id',
'event_message',
'sub_message',
'signature_count',
'host_count',
)
def __init__(
self,
ts='-',
uid='-',
orig_h='-',
orig_p='-',
resp_h='-',
resp_p='-',
note='-',
signature_id='-',
event_message='-',
sub_message='-',
signature_count='-',
host_count='-',
):
self.ts = ts
self.uid = uid
self.orig_h = orig_h
self.orig_p = orig_p
self.resp_h = resp_h
self.resp_p = resp_p
self.note = note
self.signature_id = signature_id
self.event_message = event_message
self.sub_message = sub_message
self.signature_count = signature_count
self.host_count = host_count
def __str__(self):
return "\t".join(
map(
str,
[
self.ts,
self.uid,
self.orig_h,
self.orig_p,
self.resp_h,
self.resp_p,
self.note,
self.signature_id,
self.event_message,
self.sub_message,
self.signature_count,
self.host_count,
],
)
)
@classmethod
def signature_format_line(cls):
return "\t".join(['{' + x + '}' for x in cls.__slots__])
@classmethod
def signature_types_line(cls):
return "\t".join(
['time', 'string', 'addr', 'port', 'addr', 'port', 'enum', 'string', 'string', 'string', 'count', 'count']
)
# AnalyzerScan
# .provider - a FileScanProvider subclass doing the scan/lookup
# .name - the filename to be scanned
# .size - the size (in bytes) of the file
# .fileType - the file's mime type
# .submissionResponse - a unique identifier to be returned by the provider with which to check status
class AnalyzerScan:
__slots__ = ('provider', 'name', 'size', 'fileType', 'submissionResponse')
def __init__(self, provider=None, name=None, size=None, fileType=None, submissionResponse=None):
self.provider = provider
self.name = name
self.size = size
self.fileType = fileType
self.submissionResponse = submissionResponse
# AnalyzerResult
# .finished - the scan/lookup is no longer executing (whether or not it was successful or returned a "match")
# .success - requesting the status was done successfully (whether or not it was finished)
# .result - the "result" of the scan/lookup, in whatever format is native to the provider
class AnalyzerResult:
__slots__ = ('finished', 'success', 'verbose', 'result')
def __init__(self, finished=False, success=False, verbose=False, result=None):
self.finished = finished
self.success = success
self.verbose = verbose
self.result = result
# the filename parts used by our Zeek instance for extracted files:
# source-fuid-uid-time.ext, eg., SSL-FTnzwn4hEPJi7BfzRk-CsRaviydrGyYROuX3-20190402105425.crt
class ExtractedFileNameParts:
__slots__ = ('source', 'fid', 'uid', 'time', 'ext')
def __init__(self, source=None, fid=None, uid=None, time=None, ext=None):
self.source = source
self.fid = fid
self.uid = uid
self.time = time
self.ext = ext
###################################################################################################
# convenient boolean argument parsing
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
###################################################################################################
# print to stderr
def eprint(*args, **kwargs):
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), *args, file=sys.stderr, **kwargs)
###################################################################################################
# calculate a sha256 hash of a file
def sha256sum(filename):
h = hashlib.sha256()
b = bytearray(64 * 1024)
mv = memoryview(b)
with open(filename, 'rb', buffering=0) as f:
for n in iter(lambda: f.readinto(mv), 0):
h.update(mv[:n])
return h.hexdigest()
###################################################################################################
# recursive dictionary key search
def dictsearch(d, target):
val = filter(
None, [[b] if a == target else dictsearch(b, target) if isinstance(b, dict) else None for a, b in d.items()]
)
return [i for b in val for i in b]
###################################################################################################
# filespec to various fields as per the extractor zeek script (/opt/zeek/share/zeek/site/extractor.zeek)
# source-fuid-uid-time.ext
# eg.
# SSL-FTnzwn4hEPJi7BfzRk-CsRaviydrGyYROuX3-20190402105425.crt
#
# there are other extracted files that come from the mitre-attack/bzar scripts, they are formatted like this:
# local fname = fmt("%s_%s%s", c$uid, f$id, subst_string(smb_name, "\\", "_"));
#
# CR7X4q2hmcXKqP0vVj_F3jZ2VjYttqhKaGfh__172.16.1.8_C$_WINDOWS_sny4u_un1zbd94ytwj99hcymmsad7j54gr4wdskwnqs0ki252jdsrf763zsm531b.exe
# └----------------┘ └---------------┘└------------------------------------------------------------------------------------------┘
# UID FID subst_string(smb_name, "\\", "_"))
#
# (see https://github.com/mitre-attack/bzar/blob/master/scripts/bzar_files.zeek#L50)
def extracted_filespec_to_fields(filespec):
baseFileSpec = os.path.basename(filespec)
match = re.search(r'^(?P<source>.*)-(?P<fid>.*)-(?P<uid>.*)-(?P<time>\d+)\.(?P<ext>.*?)$', baseFileSpec)
if match is not None:
result = ExtractedFileNameParts(
match.group('source'),
match.group('fid'),
match.group('uid'),
time.mktime(datetime.strptime(match.group('time'), "%Y%m%d%H%M%S").timetuple()),
match.group('ext'),
)
else:
match = re.search(r'^(?P<uid>[0-9a-zA-Z]+)_(?P<fid>[0-9a-zA-Z]+).+\.(?P<ext>.*?)$', baseFileSpec)
if match is not None:
result = ExtractedFileNameParts(
'MITRE', match.group('fid'), match.group('uid'), time.time(), match.group('ext')
)
else:
result = ExtractedFileNameParts(None, None, None, time.time(), None)
return result
###################################################################################################
# open a file and close it, updating its access time
def touch(filename):
open(filename, 'a').close()
os.utime(filename, None)
###################################################################################################
# run command with arguments and return its exit code, stdout, and stderr
def check_output_input(*popenargs, **kwargs):
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden')
if 'stderr' in kwargs:
raise ValueError('stderr argument not allowed, it will be overridden')
if 'input' in kwargs and kwargs['input']:
if 'stdin' in kwargs:
raise ValueError('stdin and input arguments may not both be used')
inputdata = kwargs['input']
kwargs['stdin'] = PIPE
else:
inputdata = None
kwargs.pop('input', None)
process = Popen(*popenargs, stdout=PIPE, stderr=PIPE, **kwargs)
try:
output, errput = process.communicate(input=inputdata)
except:
process.kill()
process.wait()
raise
retcode = process.poll()
return retcode, output, errput
###################################################################################################
# run command with arguments and return its exit code and output
def run_process(command, stdout=True, stderr=True, stdin=None, cwd=None, env=None, debug=False):
retcode = -1
output = []
try:
# run the command
retcode, cmdout, cmderr = check_output_input(command, input=stdin.encode() if stdin else None, cwd=cwd, env=env)
# split the output on newlines to return a list
if stderr and (len(cmderr) > 0):
output.extend(cmderr.decode(sys.getdefaultencoding()).split('\n'))
if stdout and (len(cmdout) > 0):
output.extend(cmdout.decode(sys.getdefaultencoding()).split('\n'))
except (FileNotFoundError, OSError, IOError) as e:
if stderr:
output.append("Command {} not found or unable to execute".format(command))
if debug:
eprint(
"{}{} returned {}: {}".format(
command, "({})".format(stdin[:80] + bool(stdin[80:]) * '...' if stdin else ""), retcode, output
)
)
return retcode, output
###################################################################################################
class AtomicInt:
def __init__(self, value=0):
self.val = RawValue('i', value)
self.lock = Lock()
def increment(self):
with self.lock:
self.val.value += 1
return self.val.value
def decrement(self):
with self.lock:
self.val.value -= 1
return self.val.value
def value(self):
with self.lock:
return self.val.value
###################################################################################################
class CarvedFileSubscriberThreaded:
# ---------------------------------------------------------------------------------
# constructor
def __init__(
self,
debug=False,
verboseDebug=False,
host="localhost",
port=VENTILATOR_PORT,
context=None,
topic='',
rcvTimeout=5000,
scriptName='',
):
self.debug = debug
self.verboseDebug = verboseDebug
self.scriptName = scriptName
self.lock = Lock()
# initialize ZeroMQ context and socket(s) to receive filenames and send scan results
self.context = context if (context is not None) else zmq.Context()
# Socket to receive messages on
self.newFilesSocket = self.context.socket(zmq.SUB)
self.newFilesSocket.connect(f"tcp://{host}:{port}")
self.newFilesSocket.setsockopt(zmq.SUBSCRIBE, bytes(topic, encoding='ascii'))
self.newFilesSocket.RCVTIMEO = rcvTimeout
if self.debug:
eprint(f"{self.scriptName}:\tbound to ventilator at {port}")
# ---------------------------------------------------------------------------------
def Pull(self, scanWorkerId=0):
fileinfo = defaultdict(str)
with self.lock:
# accept a fileinfo dict from newFilesSocket
try:
fileinfo.update(json.loads(self.newFilesSocket.recv_string()))
except zmq.Again as timeout:
# no file received due to timeout, return empty dict. which means no file available
pass
if self.verboseDebug:
eprint(
f"{self.scriptName}[{scanWorkerId}]:\t{'📨' if (FILE_SCAN_RESULT_FILE in fileinfo) else '🕑'}\t{fileinfo[FILE_SCAN_RESULT_FILE] if (FILE_SCAN_RESULT_FILE in fileinfo) else '(recv)'}"
)
return fileinfo
###################################################################################################
class FileScanProvider(ABC):
@staticmethod
@abstractmethod
def scanner_name(cls):
# returns this scanner name
pass
@abstractmethod
def max_requests(self):
# returns the maximum number of concurrently open requests this type of provider can handle
pass
@staticmethod
@abstractmethod
def check_interval(cls):
# returns the amount of seconds you should sleep between checking for results
pass
@abstractmethod
def submit(self, fileName=None, fileSize=None, fileType=None, block=False, timeout=0):
# returns something that can be passed into check_result for checking the scan status
pass
@abstractmethod
def check_result(self, submissionResponse):
# returns AnalyzerResult based on submissionResponse
pass
@staticmethod
@abstractmethod
def format(cls, fileName, response):
# returns result dict based on response (see FILE_SCAN_RESULT_* above)
pass
###################################################################################################
# class for searching for a hash with a VirusTotal public API, handling rate limiting
class VirusTotalSearch(FileScanProvider):
# ---------------------------------------------------------------------------------
# constructor
def __init__(self, apiKey, reqLimit=None, reqLimitSec=None):
self.apiKey = apiKey
self.lock = Lock()
self.history = deque()
self.reqLimit = reqLimit if reqLimit else VTOT_MAX_REQS
self.reqLimitSec = reqLimitSec if reqLimitSec else VTOT_MAX_SEC
@staticmethod
def scanner_name():
return 'virustotal'
def max_requests(self):
return self.reqLimit
@staticmethod
def check_interval():
return VTOT_CHECK_INTERVAL
# ---------------------------------------------------------------------------------
# do a hash lookup against VirusTotal, respecting rate limiting
# VirusTotalSearch does the request and gets the response immediately;
# the subsequent call to check_result (using submit's response as input)
# will always return "True" since the work has already been done
def submit(self, fileName=None, fileSize=None, fileType=None, block=False, timeout=0):
if timeout is None:
timeout = self.reqLimitSec + 5
allowed = False
response = None
# timeout only applies if block=True
timeoutTime = int(time.time()) + timeout
# while limit only repeats if block=True
while (not allowed) and (response is None):
with self.lock:
# first make sure we haven't exceeded rate limits
nowTime = int(time.time())
if len(self.history) < self.reqLimit:
# we've done fewer than the allowed requests, so assume we're good to go
self.history.append(nowTime + self.reqLimitSec)
allowed = True
elif self.history[0] < nowTime:
# we've done more than the allowed requests, but the oldest one is older than the window
_ = self.history.popleft()
self.history.append(nowTime + self.reqLimitSec)
allowed = True
if allowed:
try:
response = requests.get(VTOT_URL, params={'apikey': self.apiKey, 'resource': sha256sum(fileName)})
except requests.exceptions.RequestException as e:
# things are bad
return None
elif block and (nowTime < timeoutTime):
# rate limited, wait for a bit and come around and try again
time.sleep(1)
else:
break
return response
# ---------------------------------------------------------------------------------
# see comment for VirusTotalSearch.submit, the work has already been done
def check_result(self, submissionResponse):
result = AnalyzerResult(finished=True)
if submissionResponse is not None:
try:
result.success = submissionResponse.ok
except:
pass
try:
result.result = submissionResponse.json()
except (ValueError, TypeError):
result.success = False
return result
# ---------------------------------------------------------------------------------
# static method for formatting the response JSON (from requests.get) as a dict
@staticmethod
def format(fileName, response):
result = {
FILE_SCAN_RESULT_SCANNER: VirusTotalSearch.scanner_name(),
FILE_SCAN_RESULT_FILE: fileName,
FILE_SCAN_RESULT_ENGINES: 0,
FILE_SCAN_RESULT_HITS: 0,
FILE_SCAN_RESULT_MESSAGE: None,
FILE_SCAN_RESULT_DESCRIPTION: None,
}
if isinstance(response, AnalyzerResult):
resp = response.result
else:
resp = response
if isinstance(resp, str):
try:
resp = json.loads(resp)
except (ValueError, TypeError):
pass
# see https://www.virustotal.com/en/documentation/public-api/
if isinstance(resp, dict):
if 'response_code' in resp:
if (resp['response_code'] == VTOT_RESP_FOUND) and ('positives' in resp) and (resp['positives'] > 0):
result[FILE_SCAN_RESULT_HITS] = resp['positives']
if 'scans' in resp:
result[FILE_SCAN_RESULT_ENGINES] = len(resp['scans'])
scans = {
engine: resp['scans'][engine]
for engine in resp['scans']
if ('detected' in resp['scans'][engine]) and (resp['scans'][engine]['detected'] == True)
}
hits = defaultdict(list)
for k, v in scans.items():
hits[v['result'] if 'result' in v else 'unknown'].append(k)
if len(hits) > 0:
# short result is most common signature name
result[FILE_SCAN_RESULT_MESSAGE] = max(hits, key=lambda x: len(set(hits[x])))
# long result is list of the signature names and the engines which generated them
result[FILE_SCAN_RESULT_DESCRIPTION] = ";".join(
[f"{k}<{','.join(v)}>" for k, v in hits.items()]
)
else:
# we were reported positives, but no no details
result[FILE_SCAN_RESULT_MESSAGE] = "VirusTotal reported signature matches"
if 'permalink' in resp:
result[FILE_SCAN_RESULT_DESCRIPTION] = resp['permalink']
else:
# this shouldn't have happened after our checking above, so I guess just return the string
# and let the caller deal with it
result[FILE_SCAN_RESULT_MESSAGE] = "Invalid response"
result[FILE_SCAN_RESULT_DESCRIPTION] = f"{resp}"
return result
###################################################################################################
# class for scanning a file with Malass
class MalassScan(FileScanProvider):
# ---------------------------------------------------------------------------------
# constructor
def __init__(self, host, port, reqLimit=None):
self.host = host
self.port = port
self.reqLimit = reqLimit if reqLimit else MAL_MAX_REQS
self.transactionIdToFilenameDict = defaultdict(str)
self.scanningFilesCount = AtomicInt(value=0)
@staticmethod
def scanner_name():
return 'malass'
def max_requests(self):
return self.reqLimit
@staticmethod
def check_interval():
return MAL_CHECK_INTERVAL
# ---------------------------------------------------------------------------------
# upload a file to scan with Malass, respecting rate limiting. return submitted transaction ID
def submit(self, fileName=None, fileSize=None, fileType=None, block=False, timeout=MAL_SUBMIT_TIMEOUT_SEC):
submittedTransactionId = None
allowed = False
# timeout only applies if block=True
timeoutTime = int(time.time()) + timeout
# while limit only repeats if block=True
while (not allowed) and (submittedTransactionId is None):
# first make sure we haven't exceeded rate limits
if self.scanningFilesCount.increment() <= self.reqLimit:
# we've got fewer than the allowed requests open, so we're good to go!
allowed = True
else:
self.scanningFilesCount.decrement()
if allowed:
# submit the sample for scanning
success, transactionId, httpResponsePage = malass_client.upload_file_to_malass(
fileName, self.host, self.port
)
if success:
submittedTransactionId = transactionId
self.transactionIdToFilenameDict[submittedTransactionId] = os.path.basename(fileName)
elif block and (nowTime < timeoutTime):
# rate limited, wait for a bit and come around and try again
time.sleep(1)
else:
break
return submittedTransactionId
# ---------------------------------------------------------------------------------
# check the status of a previously submitted file
def check_result(self, transactionId):
result = AnalyzerResult()
# make a nice dictionary of this AV report
summaryDict = dict()
finishedAvsDict = dict()
summaryDict['complete'] = False
summaryDict['error'] = ""
filename = self.transactionIdToFilenameDict[transactionId]
try:
success, errMsg, avSummaryStr = malass_client.query_av_summary_rpt(
transactionId, filename, self.host, self.port
)
if success:
# get body text
body = BeautifulSoup(avSummaryStr, "html.parser").find("body")
if body is not None:
result.success = True
lines = body.text.split('\n')
# see if analysis is complete (look for termination string, then truncate the list starting at MAL_END_OF_TRANSACTION, inclusive)
eotIndices = [i for i, s in enumerate(lines) if MAL_END_OF_TRANSACTION in s]
summaryDict['complete'] = len(eotIndices) > 0
if summaryDict['complete']:
del lines[eotIndices[0] :]
# take report name/value pairs (minus AV results) and insert them into summaryDict
try:
summaryDict.update(dict(map(str, x[1:].split('=')) for x in lines if x.startswith('#')))
except (ValueError, TypeError) as e:
summaryDict['error'] = f"Report parse error: {str(e)}"
summaryDict['complete'] = True # avoid future lookups, abandon submission
# take AV results in this report and merge them into summaryDict
summaryDict['av'] = {}
for vmLine in [x for x in lines if x.startswith('av_vm_')]:
avDict = dict(map(str, x.split('=')) for x in vmLine.split(","))
if ('av_vm_name' in avDict) and (len(avDict['av_vm_name']) > 0):
summaryDict['av'][avDict['av_vm_name']] = avDict
# merge any new av results in this response into the final finishedAvsDict
for avName, avEntry in summaryDict['av'].items():
if (
('av_vm_num' in avEntry)
and (avEntry['av_vm_num'].isnumeric())
and (not (int(avEntry['av_vm_num']) in finishedAvsDict))
):
finishedAvsDict[int(avEntry['av_vm_num'])] = avName
# are we done?
if summaryDict['complete']:
# yes, we are done! let's make sure at least one AV scanned, and report an error if not
if (len(finishedAvsDict) == 0) and (len(summaryDict['error']) == 0):
summaryDict['error'] = f"No AVs scanned file sample ({transactionId}/{filename})"
else:
summaryDict['error'] = f"Summary report contained no body ({transactionId}/{filename})"
summaryDict['complete'] = True # avoid future lookups, abandon submission
else:
summaryDict['error'] = f"Summary report was not generated: {errMsg} ({transactionId}/{filename})"
summaryDict['complete'] = True # avoid future lookups, abandon submission
finally:
if (transactionId is not None) and summaryDict['complete']:
# decrement scanning counter and remove trans->filename mapping if this scan is complete
self.scanningFilesCount.decrement()
self.transactionIdToFilenameDict.pop(transactionId, None)
result.finished = summaryDict['complete']
result.result = summaryDict
return result
# ---------------------------------------------------------------------------------
# static method for formatting the response summaryDict (from check_result)
@staticmethod
def format(fileName, response):
result = {
FILE_SCAN_RESULT_SCANNER: MalassScan.scanner_name(),
FILE_SCAN_RESULT_FILE: fileName,
FILE_SCAN_RESULT_ENGINES: 0,
FILE_SCAN_RESULT_HITS: 0,
FILE_SCAN_RESULT_MESSAGE: None,
FILE_SCAN_RESULT_DESCRIPTION: None,
}
if isinstance(response, AnalyzerResult):
resp = response.result
else:
resp = response
if isinstance(resp, dict) and ('av' in resp) and (len(resp['av']) > 0):
hitAvs = {
k: v
for k, v in resp['av'].items()
if ('contains_a_virus' in resp['av'][k]) and (resp['av'][k]['contains_a_virus'].lower() == "yes")
}
result[FILE_SCAN_RESULT_HITS] = len(hitAvs)
result[FILE_SCAN_RESULT_ENGINES] = len(resp['av'])
hits = defaultdict(list)
for k, v in hitAvs.items():
hits[v['virus_name'] if 'virus_name' in v else 'unknown'].append(k)
if len(hits) > 0:
# short result is most common signature name
result[FILE_SCAN_RESULT_MESSAGE] = max(hits, key=lambda x: len(set(hits[x])))
# long result is list of the signature names and the engines which generated them
result[FILE_SCAN_RESULT_DESCRIPTION] = ";".join([f"{k}<{','.join(v)}>" for k, v in hits.items()])
else:
result[FILE_SCAN_RESULT_MESSAGE] = "Error or invalid response"
if isinstance(resp, dict) and ('error' in resp):
result[FILE_SCAN_RESULT_DESCRIPTION] = f"{resp['error']}"
else:
result[FILE_SCAN_RESULT_DESCRIPTION] = f"{resp}"
return result
###################################################################################################
# class for scanning a file with ClamAV
class ClamAVScan(FileScanProvider):
# ---------------------------------------------------------------------------------
# constructor
def __init__(self, debug=False, verboseDebug=False, socketFileName=None, reqLimit=None):
self.scanningFilesCount = AtomicInt(value=0)
self.debug = debug
self.verboseDebug = verboseDebug
self.socketFileName = socketFileName
self.reqLimit = reqLimit if reqLimit else CLAM_MAX_REQS
@staticmethod
def scanner_name():
return 'clamav'
def max_requests(self):
return self.reqLimit
@staticmethod
def check_interval():
return CLAM_CHECK_INTERVAL
# ---------------------------------------------------------------------------------
# submit a file to scan with ClamAV, respecting rate limiting. return scan result
def submit(self, fileName=None, fileSize=None, fileType=None, block=False, timeout=CLAM_SUBMIT_TIMEOUT_SEC):
clamavResult = AnalyzerResult()
allowed = False
connected = False
# timeout only applies if block=True
timeoutTime = int(time.time()) + timeout
# while limit only repeats if block=True
while (not allowed) and (not clamavResult.finished):
if not connected:
if self.verboseDebug:
eprint(f"{get_ident()}: ClamAV attempting connection")
clamAv = (
clamd.ClamdUnixSocket(path=self.socketFileName)
if self.socketFileName is not None
else clamd.ClamdUnixSocket()
)
try:
clamAv.ping()
connected = True
if self.verboseDebug:
eprint(f"{get_ident()}: ClamAV connected!")
except Exception as e:
connected = False
if self.debug:
eprint(f"{get_ident()}: ClamAV connection failed: {str(e)}")
if connected:
# first make sure we haven't exceeded rate limits
if self.scanningFilesCount.increment() <= self.reqLimit:
# we've got fewer than the allowed requests open, so we're good to go!
allowed = True
else:
self.scanningFilesCount.decrement()
if connected and allowed:
try:
if self.verboseDebug:
eprint(f'{get_ident()} ClamAV scanning: {fileName}')
clamavResult.result = clamAv.scan(fileName)
if self.verboseDebug:
eprint(f'{get_ident()} ClamAV scan result: {clamavResult.result}')
clamavResult.success = clamavResult.result is not None
clamavResult.finished = True
except Exception as e:
if clamavResult.result is None:
clamavResult.result = str(e)
if self.debug:
eprint(f'{get_ident()} ClamAV scan error: {clamavResult.result}')
finally:
self.scanningFilesCount.decrement()
elif block and (nowTime < timeoutTime):
# rate limited, wait for a bit and come around and try again
time.sleep(1)
else:
break
return clamavResult
# ---------------------------------------------------------------------------------
# return the result of the previously scanned file
def check_result(self, clamavResult):
return (
clamavResult
if isinstance(clamavResult, AnalyzerResult)
else AnalyzerResult(finished=True, success=False, result=None)
)
# ---------------------------------------------------------------------------------
# static method for formatting the response summaryDict (from check_result)
@staticmethod
def format(fileName, response):
result = {
FILE_SCAN_RESULT_SCANNER: ClamAVScan.scanner_name(),
FILE_SCAN_RESULT_FILE: fileName,
FILE_SCAN_RESULT_ENGINES: 1,
FILE_SCAN_RESULT_HITS: 0,
FILE_SCAN_RESULT_MESSAGE: None,
FILE_SCAN_RESULT_DESCRIPTION: None,
}
if isinstance(response, AnalyzerResult):
resp = response.result
else:
resp = response
if isinstance(resp, dict):
hits = []
for filename, resultTuple in resp.items():
if (len(resultTuple) == 2) and (resultTuple[0] == CLAM_FOUND_KEY):
hits.append(resultTuple[1])
result[FILE_SCAN_RESULT_HITS] = len(hits)
if len(hits) > 0:
cnt = Counter(hits)
# short message is most common signature name
result[FILE_SCAN_RESULT_MESSAGE] = cnt.most_common(1)[0][0]
# long description is list of the signature names and the engines which generated them
result[FILE_SCAN_RESULT_DESCRIPTION] = ";".join([f"{x}<{CLAM_ENGINE_ID}>" for x in hits])
else:
result[FILE_SCAN_RESULT_MESSAGE] = "Error or invalid response"
result[FILE_SCAN_RESULT_DESCRIPTION] = f"{resp}"
return result
###################################################################################################
# class for scanning a file with Yara
class YaraScan(FileScanProvider):
# ---------------------------------------------------------------------------------
# constructor
def __init__(self, debug=False, verboseDebug=False, rulesDirs=[], reqLimit=None):
self.scanningFilesCount = AtomicInt(value=0)
self.debug = debug
self.verboseDebug = verboseDebug
self.reqLimit = reqLimit if reqLimit else YARA_MAX_REQS
self.ruleFilespecs = {}
for yaraDir in rulesDirs:
for root, dirs, files in os.walk(yaraDir):
for file in files:
# skip hidden, backup or system related files
if file.startswith(".") or file.startswith("~") or file.startswith("_"):
continue
filename = os.path.join(root, file)
extension = os.path.splitext(file)[1].lower()
try: