-
Notifications
You must be signed in to change notification settings - Fork 402
/
abstracts.py
1773 lines (1514 loc) · 68.5 KB
/
abstracts.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
# encoding: utf-8
# Copyright (C) 2010-2015 Cuckoo Foundation, Optiv, Inc. (brad.spengler@optiv.com).
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.
import datetime
import io
import logging
import os
import socket
import threading
import time
import timeit
import xml.etree.ElementTree as ET
from builtins import NotImplementedError
from pathlib import Path
from typing import Dict, List
try:
import dns.resolver
except ImportError:
print("Missed dependency -> pip3 install dnspython")
import PIL
import requests
from lib.cuckoo.common.config import Config
from lib.cuckoo.common.constants import CUCKOO_ROOT
from lib.cuckoo.common.dictionary import Dictionary
from lib.cuckoo.common.exceptions import (
CuckooCriticalError,
CuckooDependencyError,
CuckooMachineError,
CuckooOperationalError,
CuckooReportError,
)
from lib.cuckoo.common.integrations.mitre import mitre_load
from lib.cuckoo.common.path_utils import path_exists, path_mkdir
from lib.cuckoo.common.url_validate import url as url_validator
from lib.cuckoo.common.utils import create_folder, get_memdump_path, load_categories
from lib.cuckoo.core.database import Database, Machine, _Database
try:
import re2 as re
except ImportError:
import re
try:
import libvirt
HAVE_LIBVIRT = True
except ImportError:
HAVE_LIBVIRT = False
try:
from tldextract import TLDExtract
HAVE_TLDEXTRACT = True
logging.getLogger("filelock").setLevel("WARNING")
except ImportError:
HAVE_TLDEXTRACT = False
repconf = Config("reporting")
_, categories_need_VM = load_categories()
mitre, HAVE_MITRE, _ = mitre_load(repconf.mitre.enabled)
log = logging.getLogger(__name__)
cfg = Config()
machinery_conf = Config(cfg.cuckoo.machinery)
myresolver = dns.resolver.Resolver()
myresolver.timeout = 5.0
myresolver.lifetime = 5.0
myresolver.domain = dns.name.Name("google-public-dns-a.google.com")
myresolver.nameserver = ["8.8.8.8"]
class Auxiliary:
"""Base abstract class for auxiliary modules."""
def __init__(self):
self.task = None
self.machine = None
self.options = None
self.db = Database()
def set_task(self, task):
self.task = task
def set_machine(self, machine):
self.machine = machine
def set_options(self, options):
self.options = options
def start(self):
raise NotImplementedError
def stop(self):
raise NotImplementedError
class Machinery:
"""Base abstract class for machinery modules."""
# Default label used in machinery configuration file to supply virtual
# machine name/label/vmx path. Override it if you dubbed it in another
# way.
LABEL: str = "label"
# This must be defined in sub-classes.
module_name: str
def __init__(self):
self.options = None
# Database pointer.
self.db: _Database = Database()
self.set_options(self.read_config())
def read_config(self) -> None:
return Config(self.module_name)
def set_options(self, options: dict) -> None:
"""Set machine manager options.
@param options: machine manager options dict.
"""
self.options = options
mmanager_opts = self.options.get(self.module_name)
if not isinstance(mmanager_opts["machines"], list):
mmanager_opts["machines"] = str(mmanager_opts["machines"]).strip().split(",")
def initialize(self) -> None:
"""Read, load, and verify machines configuration."""
# Machine table is cleaned to be filled from configuration file
# at each start.
self.db.clean_machines()
# Load.
self._initialize()
# Run initialization checks.
self._initialize_check()
def _initialize(self) -> None:
"""Read configuration."""
mmanager_opts = self.options.get(self.module_name)
for machine_id in mmanager_opts["machines"]:
try:
machine_opts = self.options.get(machine_id.strip())
machine = Dictionary()
machine.id = machine_id.strip()
machine.label = machine_opts[self.LABEL]
machine.platform = machine_opts["platform"]
machine.tags = machine_opts.get("tags")
machine.ip = machine_opts["ip"]
machine.arch = machine_opts["arch"]
machine.reserved = machine_opts.get("reserved", False)
# If configured, use specific network interface for this
# machine, else use the default value.
if machine_opts.get("interface"):
machine.interface = machine_opts["interface"]
else:
machine.interface = mmanager_opts.get("interface")
# If configured, use specific snapshot name, else leave it
# empty and use default behaviour.
machine.snapshot = machine_opts.get("snapshot")
machine.resultserver_ip = machine_opts.get("resultserver_ip", cfg.resultserver.ip)
machine.resultserver_port = machine_opts.get("resultserver_port")
if machine.resultserver_port is None:
# The ResultServer port might have been dynamically changed,
# get it from the ResultServer singleton. Also avoid import
# recursion issues by importing ResultServer here.
from lib.cuckoo.core.resultserver import ResultServer
machine.resultserver_port = ResultServer().port
# Strip parameters.
for key, value in machine.items():
if value and isinstance(value, str):
machine[key] = value.strip()
self.db.add_machine(
name=machine.id,
label=machine.label,
arch=machine.arch,
ip=machine.ip,
platform=machine.platform,
tags=machine.tags,
interface=machine.interface,
snapshot=machine.snapshot,
resultserver_ip=machine.resultserver_ip,
resultserver_port=machine.resultserver_port,
reserved=machine.reserved,
)
except (AttributeError, CuckooOperationalError) as e:
log.warning("Configuration details about machine %s are missing: %s", machine_id.strip(), e)
continue
def _initialize_check(self) -> None:
"""Runs checks against virtualization software when a machine manager is initialized.
@note: in machine manager modules you may override or superclass his method.
@raise CuckooMachineError: if a misconfiguration or a unkown vm state is found.
"""
try:
configured_vms = self._list()
except NotImplementedError:
return
self.shutdown_running_machines(configured_vms)
self.check_screenshot_support()
if not cfg.timeouts.vm_state:
raise CuckooCriticalError("Virtual machine state change timeout setting not found, please add it to the config file")
def check_screenshot_support(self) -> None:
# If machinery_screenshots are enabled, check the machinery supports it.
if not cfg.cuckoo.machinery_screenshots:
return
# inspect function members available on the machinery class
func = getattr(self.__class__, "screenshot")
if func == Machinery.screenshot:
msg = f"machinery {self.module_name} does not support machinery screenshots"
raise CuckooCriticalError(msg)
def shutdown_running_machines(self, configured_vms: List[str]) -> None:
for machine in self.machines():
# If this machine is already in the "correct" state, then we
# go on to the next machine.
if machine.label in configured_vms and self._status(machine.label) in (self.POWEROFF, self.ABORTED):
continue
# This machine is currently not in its correct state, we're going
# to try to shut it down. If that works, then the machine is fine.
try:
self.stop(machine.label)
except CuckooMachineError as e:
msg = f"Please update your configuration. Unable to shut '{machine.label}' down or find the machine in its proper state: {e}"
raise CuckooCriticalError(msg) from e
def machines(self):
"""List virtual machines.
@return: virtual machines list
"""
return self.db.list_machines(include_reserved=True)
def availables(self, label=None, platform=None, tags=None, arch=None, include_reserved=False, os_version=None):
"""How many (relevant) machines are free.
@param label: machine ID.
@param platform: machine platform.
@param tags: machine tags
@param arch: machine arch
@return: free machines count.
"""
return self.db.count_machines_available(
label=label, platform=platform, tags=tags, arch=arch, include_reserved=include_reserved, os_version=os_version
)
def scale_pool(self, machine: Machine) -> None:
"""This can be overridden in sub-classes to scale the pool of machines once one has been acquired."""
return
def release(self, machine: Machine) -> Machine:
"""Release a machine.
@param label: machine name.
"""
return self.db.unlock_machine(machine)
def running(self):
"""Returns running virtual machines.
@return: running virtual machines list.
"""
return self.db.list_machines(locked=True)
def running_count(self):
return self.db.count_machines_running()
def screenshot(self, label, path):
"""Screenshot a running virtual machine.
@param label: machine name
@param path: where to store the screenshot
@raise NotImplementedError
"""
raise NotImplementedError
def shutdown(self):
"""Shutdown the machine manager. Kills all alive machines.
@raise CuckooMachineError: if unable to stop machine.
"""
running = self.running()
if len(running) > 0:
log.info("Still %d guests still alive, shutting down...", len(running))
for machine in running:
try:
self.stop(machine.label)
except CuckooMachineError as e:
log.warning("Unable to shutdown machine %s, please check manually. Error: %s", machine.label, e)
def set_status(self, label, status):
"""Set status for a virtual machine.
@param label: virtual machine label
@param status: new virtual machine status
"""
self.db.set_machine_status(label, status)
def start(self, label=None):
"""Start a machine.
@param label: machine name.
@raise NotImplementedError: this method is abstract.
"""
raise NotImplementedError
def stop(self, label=None):
"""Stop a machine.
@param label: machine name.
@raise NotImplementedError: this method is abstract.
"""
raise NotImplementedError
def _list(self):
"""Lists virtual machines configured.
@raise NotImplementedError: this method is abstract.
"""
raise NotImplementedError
def dump_memory(self, label, path):
"""Takes a memory dump of a machine.
@param path: path to where to store the memory dump.
"""
raise NotImplementedError
def _wait_status(self, label, state):
"""Waits for a vm status.
@param label: virtual machine name.
@param state: virtual machine status, accepts multiple states as list.
@raise CuckooMachineError: if default waiting timeout expire.
"""
# This block was originally suggested by Loic Jaquemet.
waitme = 0
try:
current = self._status(label)
except NameError:
return
if isinstance(state, str):
state = [state]
while current not in state:
log.debug("Waiting %d cuckooseconds for machine %s to switch to status %s", waitme, label, state)
if waitme > int(cfg.timeouts.vm_state):
raise CuckooMachineError(f"Timeout hit while for machine {label} to change status")
time.sleep(1)
waitme += 1
current = self._status(label)
def delete_machine(self, name):
"""Delete a virtual machine.
@param name: virtual machine name
"""
_ = self.db.delete_machine(name)
class LibVirtMachinery(Machinery):
"""Libvirt based machine manager.
If you want to write a custom module for a virtualization software
supported by libvirt you have just to inherit this machine manager and
change the connection string.
"""
# VM states.
RUNNING = "running"
PAUSED = "paused"
POWEROFF = "poweroff"
ERROR = "machete"
ABORTED = "abort"
def __init__(self):
if not HAVE_LIBVIRT:
raise CuckooDependencyError(
"Unable to import libvirt. Ensure that you properly installed it by running: cd /opt/CAPEv2/ ; sudo -u cape poetry run extra/libvirt_installer.sh"
)
super().__init__()
def _initialize_check(self):
"""Runs all checks when a machine manager is initialized.
@raise CuckooMachineError: if libvirt version is not supported.
"""
# Version checks.
if not self._version_check():
raise CuckooMachineError("Libvirt version is not supported, please get an updated version")
# Preload VMs
self.vms = self._fetch_machines()
# Base checks. Also attempts to shutdown any machines which are
# currently still active.
super()._initialize_check()
def start(self, label):
"""Starts a virtual machine.
@param label: virtual machine name.
@raise CuckooMachineError: if unable to start virtual machine.
"""
log.debug("Starting machine %s", label)
vm_info = self.db.view_machine_by_label(label)
if vm_info is None:
msg = f"Unable to find machine with label {label} in database."
raise CuckooMachineError(msg)
if self._status(label) != self.POWEROFF:
msg = f"Trying to start a virtual machine that has not been turned off {label}"
raise CuckooMachineError(msg)
conn = self._connect(label)
snapshot_list = self.vms[label].snapshotListNames(flags=0)
# If a snapshot is configured try to use it.
if vm_info.snapshot and vm_info.snapshot in snapshot_list:
# Revert to desired snapshot, if it exists.
log.debug("Using snapshot %s for virtual machine %s", vm_info.snapshot, label)
try:
vm = self.vms[label]
snapshot = vm.snapshotLookupByName(vm_info.snapshot, flags=0)
self.vms[label].revertToSnapshot(snapshot, flags=0)
except libvirt.libvirtError as e:
msg = f"Unable to restore snapshot {vm_info.snapshot} on virtual machine {label}"
raise CuckooMachineError(msg) from e
finally:
self._disconnect(conn)
elif self._get_snapshot(label):
snapshot = self._get_snapshot(label)
log.debug("Using snapshot %s for virtual machine %s", snapshot.getName(), label)
try:
self.vms[label].revertToSnapshot(snapshot, flags=0)
except libvirt.libvirtError as e:
raise CuckooMachineError(f"Unable to restore snapshot on virtual machine {label}") from e
finally:
self._disconnect(conn)
else:
self._disconnect(conn)
raise CuckooMachineError(f"No snapshot found for virtual machine {label}")
# Check state.
self._wait_status(label, self.RUNNING)
def stop(self, label):
"""Stops a virtual machine. Kill them all.
@param label: virtual machine name.
@raise CuckooMachineError: if unable to stop virtual machine.
"""
log.debug("Stopping machine %s", label)
if self._status(label) == self.POWEROFF:
raise CuckooMachineError(f"Trying to stop an already stopped machine {label}")
# Force virtual machine shutdown.
conn = self._connect(label)
try:
if not self.vms[label].isActive():
log.debug("Trying to stop an already stopped machine %s, skipping", label)
else:
self.vms[label].destroy() # Machete's way!
except libvirt.libvirtError as e:
raise CuckooMachineError(f"Error stopping virtual machine {label}: {e}") from e
finally:
self._disconnect(conn)
# Check state.
self._wait_status(label, self.POWEROFF)
def shutdown(self):
"""Override shutdown to free libvirt handlers - they print errors."""
for machine in self.machines():
# If the machine is already shutdown, move on
if self._status(machine.label) in (self.POWEROFF, self.ABORTED):
continue
try:
log.info("Shutting down machine '%s'", machine.label)
self.stop(machine.label)
except CuckooMachineError as e:
log.warning("Unable to shutdown machine %s, please check manually. Error: %s", machine.label, e)
# Free handlers.
self.vms = None
def screenshot(self, label, path):
"""Screenshot a running virtual machine.
@param label: machine name
@param path: where to store the screenshot
"""
conn = self._connect()
try:
vm = conn.lookupByName(label)
except libvirt.libvirtError as e:
raise CuckooMachineError(f"Error screenshotting virtual machine {label}: {e}") from e
stream0, screen = conn.newStream(), 0
# ignore the mime type returned by the call to screenshot()
_ = vm.screenshot(stream0, screen)
buffer = io.BytesIO()
def stream_handler(_, data, buffer):
buffer.write(data)
folder_name, _ = path.rsplit("/", 1)
if not path_exists(folder_name):
path_mkdir(folder_name, parent=True, exist_ok=True)
stream0.recvAll(stream_handler, buffer)
stream0.finish()
streamed_img = PIL.Image.open(buffer)
streamed_img.convert(mode="RGB").save(path)
def dump_memory(self, label, path):
"""Takes a memory dump.
@param path: path to where to store the memory dump.
"""
log.debug("Dumping memory for machine %s", label)
conn = self._connect(label)
try:
# create the memory dump file ourselves first so it doesn't end up root/root 0600
# it'll still be owned by root, so we can't delete it, but at least we can read it
fd = open(path, "w")
fd.close()
self.vms[label].coreDump(path, flags=libvirt.VIR_DUMP_MEMORY_ONLY)
except libvirt.libvirtError as e:
raise CuckooMachineError(f"Error dumping memory virtual machine {label}: {e}") from e
finally:
self._disconnect(conn)
def _status(self, label):
"""Gets current status of a vm.
@param label: virtual machine name.
@return: status string.
"""
log.debug("Getting status for %s", label)
# Stetes mapping of python-libvirt.
# virDomainState
# VIR_DOMAIN_NOSTATE = 0
# VIR_DOMAIN_RUNNING = 1
# VIR_DOMAIN_BLOCKED = 2
# VIR_DOMAIN_PAUSED = 3
# VIR_DOMAIN_SHUTDOWN = 4
# VIR_DOMAIN_SHUTOFF = 5
# VIR_DOMAIN_CRASHED = 6
# VIR_DOMAIN_PMSUSPENDED = 7
conn = self._connect(label)
try:
state = self.vms[label].state(flags=0)
except libvirt.libvirtError as e:
raise CuckooMachineError(f"Error getting status for virtual machine {label}: {e}") from e
finally:
self._disconnect(conn)
if state:
if state[0] == 1:
status = self.RUNNING
elif state[0] == 3:
status = self.PAUSED
elif state[0] in {4, 5}:
status = self.POWEROFF
else:
status = self.ERROR
# Report back status.
if status:
self.set_status(label, status)
return status
else:
raise CuckooMachineError(f"Unable to get status for {label}")
def _connect(self, label=None):
"""Connects to libvirt subsystem.
@raise CuckooMachineError: when unable to connect to libvirt.
"""
# Check if a connection string is available.
if not self.dsn:
raise CuckooMachineError("You must provide a proper connection string")
try:
return libvirt.open(self.dsn)
except libvirt.libvirtError as e:
raise CuckooMachineError("Cannot connect to libvirt") from e
def _disconnect(self, conn):
"""Disconnects to libvirt subsystem.
@raise CuckooMachineError: if cannot disconnect from libvirt.
"""
try:
conn.close()
except libvirt.libvirtError as e:
raise CuckooMachineError("Cannot disconnect from libvirt") from e
def _fetch_machines(self):
"""Fetch machines handlers.
@return: dict with machine label as key and handle as value.
"""
return {vm.label: self._lookup(vm.label) for vm in self.machines()}
def _lookup(self, label):
"""Search for a virtual machine.
@param conn: libvirt connection handle.
@param label: virtual machine name.
@raise CuckooMachineError: if virtual machine is not found.
"""
conn = self._connect(label)
try:
vm = conn.lookupByName(label)
except libvirt.libvirtError as e:
raise CuckooMachineError(f"Cannot find machine {label}") from e
finally:
self._disconnect(conn)
return vm
def _list(self):
"""List available virtual machines.
@raise CuckooMachineError: if unable to list virtual machines.
"""
conn = self._connect()
try:
names = conn.listDefinedDomains()
except libvirt.libvirtError as e:
raise CuckooMachineError("Cannot list domains") from e
finally:
self._disconnect(conn)
return names
def _version_check(self):
"""Check if libvirt release supports snapshots.
@return: True or false.
"""
return libvirt.getVersion() >= 8000
def _get_snapshot(self, label):
"""Get current snapshot for virtual machine
@param label: virtual machine name
@return None or current snapshot
@raise CuckooMachineError: if cannot find current snapshot or
when there are too many snapshots available
"""
def _extract_creation_time(node):
"""Extracts creation time from a KVM vm config file.
@param node: config file node
@return: extracted creation time
"""
xml = ET.fromstring(node.getXMLDesc(flags=0))
return xml.findtext("./creationTime")
snapshot = None
conn = self._connect(label)
try:
vm = self.vms[label]
# Try to get the currrent snapshot, otherwise fallback on the latest
# from config file.
if vm.hasCurrentSnapshot(flags=0):
snapshot = vm.snapshotCurrent(flags=0)
else:
log.debug("No current snapshot, using latest snapshot")
# No current snapshot, try to get the last one from config file.
all_snapshots = vm.listAllSnapshots(flags=0)
if all_snapshots:
snapshot = sorted(all_snapshots, key=_extract_creation_time, reverse=True)[0]
except libvirt.libvirtError:
raise CuckooMachineError(f"Unable to get snapshot for virtual machine {label}")
finally:
self._disconnect(conn)
return snapshot
class Processing:
"""Base abstract class for processing module."""
order = 1
enabled = True
def __init__(self, results=None):
self.analysis_path = ""
self.logs_path = ""
self.task = None
self.options = None
self.results = results
def set_options(self, options):
"""Set report options.
@param options: report options dict.
"""
self.options = options
def set_task(self, task):
"""Add task information.
@param task: task dictionary.
"""
self.task = task
def set_path(self, analysis_path):
"""Set paths.
@param analysis_path: analysis folder path.
"""
self.analysis_path = analysis_path
self.aux_path = os.path.join(self.analysis_path, "aux")
self.log_path = os.path.join(self.analysis_path, "analysis.log")
self.package_files = os.path.join(self.analysis_path, "package_files")
self.file_path = os.path.realpath(os.path.join(self.analysis_path, "binary"))
self.dropped_path = os.path.join(self.analysis_path, "files")
self.files_metadata = os.path.join(self.analysis_path, "files.json")
self.procdump_path = os.path.join(self.analysis_path, "procdump")
self.CAPE_path = os.path.join(self.analysis_path, "CAPE")
self.logs_path = os.path.join(self.analysis_path, "logs")
self.shots_path = os.path.join(self.analysis_path, "shots")
self.pcap_path = os.path.join(self.analysis_path, "dump.pcap")
self.pmemory_path = os.path.join(self.analysis_path, "memory")
self.memory_path = get_memdump_path(analysis_path.rsplit("/", 1)[-1])
# self.memory_path = os.path.join(self.analysis_path, "memory.dmp")
self.network_path = os.path.join(self.analysis_path, "network")
self.tlsmaster_path = os.path.join(self.analysis_path, "tlsmaster.txt")
self.self_extracted = os.path.join(self.analysis_path, "selfextracted")
def add_statistic_tmp(self, name, field, pretime):
timediff = timeit.default_timer() - pretime
value = round(timediff, 3)
if name not in self.results["temp_processing_stats"]:
self.results["temp_processing_stats"][name] = {}
# To be able to add yara/capa and others time summary over all processing modules
if field in self.results["temp_processing_stats"][name]:
self.results["temp_processing_stats"][name][field] += value
else:
self.results["temp_processing_stats"][name][field] = value
def run(self):
"""Start processing.
@raise NotImplementedError: this method is abstract.
"""
raise NotImplementedError
class Signature:
"""Base class for Cuckoo signatures."""
name = ""
description = ""
severity = 1
confidence = 100
weight = 1
categories = []
families = []
authors = []
references = []
alert = False
enabled = True
minimum = None
maximum = None
ttps = []
mbcs = []
# Higher order will be processed later (only for non-evented signatures)
# this can be used for having meta-signatures that check on other lower-
# order signatures being matched
order = 0
evented = False
filter_processnames = set()
filter_apinames = set()
filter_categories = set()
filter_analysistypes = set()
banned_suricata_sids = ()
def __init__(self, results=None):
self.data = []
self.new_data = []
self.results = results
self._current_call_cache = None
self._current_call_dict = None
self._current_call_raw_cache = None
self._current_call_raw_dict = None
self.hostname2ips = {}
self.machinery_conf = machinery_conf
self.matched = False
# These are set during the iteration of evented signatures
self.pid = None
self.cid = None
self.call = None
def statistics_custom(self, pretime, extracted: bool = False):
"""
Aux function for custom stadistics on signatures
@param pretime: start time as datetime object
@param extracted: conf extraction from inside signature to count success extraction vs sig run
"""
timediff = timeit.default_timer() - pretime
self.results["custom_statistics"] = {
"name": self.name,
"time": round(timediff, 3),
"extracted": int(extracted),
}
def set_path(self, analysis_path):
"""Set analysis folder path.
@param analysis_path: analysis folder path.
"""
self.analysis_path = analysis_path
self.conf_path = os.path.join(self.analysis_path, "analysis.conf")
self.file_path = os.path.realpath(os.path.join(self.analysis_path, "binary"))
self.dropped_path = os.path.join(self.analysis_path, "files")
self.procdump_path = os.path.join(self.analysis_path, "procdump")
self.CAPE_path = os.path.join(self.analysis_path, "CAPE")
self.reports_path = os.path.join(self.analysis_path, "reports")
self.shots_path = os.path.join(self.analysis_path, "shots")
self.pcap_path = os.path.join(self.analysis_path, "dump.pcap")
self.pmemory_path = os.path.join(self.analysis_path, "memory")
# self.memory_path = os.path.join(self.analysis_path, "memory.dmp")
self.memory_path = get_memdump_path(analysis_path.rsplit("/", 1)[-1])
self.self_extracted = os.path.join(self.analysis_path, "selfextracted")
self.files_metadata = os.path.join(self.analysis_path, "files.json")
try:
create_folder(folder=self.reports_path)
except CuckooOperationalError as e:
CuckooReportError(e)
def yara_detected(self, name):
target = self.results.get("target", {})
if target.get("category") in ("file", "static") and target.get("file"):
for keyword in ("cape_yara", "yara"):
for yara_block in self.results["target"]["file"].get(keyword, []):
if re.findall(name, yara_block["name"], re.I):
yield "sample", self.results["target"]["file"]["path"], yara_block, self.results["target"]["file"]
for block in target["file"].get("extracted_files", []):
for keyword in ("cape_yara", "yara"):
for yara_block in block[keyword]:
if re.findall(name, yara_block["name"], re.I):
# we can't use here values from set_path
yield "sample", block["path"], yara_block, block
for block in self.results.get("CAPE", {}).get("payloads", []) or []:
for sub_keyword in ("cape_yara", "yara"):
for yara_block in block.get(sub_keyword, []):
if re.findall(name, yara_block["name"], re.I):
yield sub_keyword, block["path"], yara_block, block
for subblock in block.get("extracted_files", []):
for keyword in ("cape_yara", "yara"):
for yara_block in subblock[keyword]:
if re.findall(name, yara_block["name"], re.I):
yield "sample", subblock["path"], yara_block, block
for keyword in ("procdump", "procmemory", "extracted", "dropped"):
if self.results.get(keyword) is not None:
for block in self.results.get(keyword, []):
if not isinstance(block, dict):
continue
for sub_keyword in ("cape_yara", "yara"):
for yara_block in block.get(sub_keyword, []):
if re.findall(name, yara_block["name"], re.I):
path = block["path"] if block.get("path", False) else ""
yield keyword, path, yara_block, block
if keyword == "procmemory":
for pe in block.get("extracted_pe", []) or []:
for sub_keyword in ("cape_yara", "yara"):
for yara_block in pe.get(sub_keyword, []) or []:
if re.findall(name, yara_block["name"], re.I):
yield "extracted_pe", pe["path"], yara_block, block
for subblock in block.get("extracted_files", []):
for keyword in ("cape_yara", "yara"):
for yara_block in subblock[keyword]:
if re.findall(name, yara_block["name"], re.I):
yield "sample", subblock["path"], yara_block, block
macro_path = os.path.join(CUCKOO_ROOT, "storage", "analyses", str(self.results["info"]["id"]), "macros")
for macroname in self.results.get("static", {}).get("office", {}).get("Macro", {}).get("info", []) or []:
for yara_block in self.results["static"]["office"]["Macro"]["info"].get("macroname", []) or []:
for sub_block in self.results["static"]["office"]["Macro"]["info"]["macroname"].get(yara_block, []) or []:
if re.findall(name, sub_block["name"], re.I):
yield "macro", os.path.join(macro_path, macroname), sub_block, self.results["static"]["office"]["Macro"][
"info"
]
if self.results.get("static", {}).get("office", {}).get("XLMMacroDeobfuscator", False):
for yara_block in self.results["static"]["office"]["XLMMacroDeobfuscator"].get("info", []).get("yara_macro", []) or []:
if re.findall(name, yara_block["name"], re.I):
yield "macro", os.path.join(macro_path, "xlm_macro"), yara_block, self.results["static"]["office"][
"XLMMacroDeobfuscator"
]["info"]
def signature_matched(self, signame: str) -> bool:
# Check if signature has matched (useful for ordered signatures)
matched_signatures = [sig["name"] for sig in self.results.get("signatures", [])]
return signame in matched_signatures
def get_signature_data(self, signame: str) -> List[Dict[str, str]]:
# Retrieve data from matched signature (useful for ordered signatures)
if self.signature_matched(signame):
signature = next((match for match in self.results.get("signatures", []) if match.get("name") == signame), None)
if signature:
return signature.get("data", []) + signature.get("new_data", [])
return []
def add_statistic(self, name, field, value):
if name not in self.results["statistics"]["signatures"]:
self.results["statistics"]["signatures"][name] = {}
self.results["statistics"]["signatures"][name][field] = value
def get_pids(self):
pids = []
logs = os.path.join(CUCKOO_ROOT, "storage", "analyses", str(self.results["info"]["id"]), "logs")
processes = self.results.get("behavior", {}).get("processtree", [])
if processes:
for pid in processes:
pids.append(int(pid.get("pid", "")))
pids += [int(cpid["pid"]) for cpid in pid.get("children", []) if "pid" in cpid]
# in case if bsons too big
if path_exists(logs):
pids += [int(pidb.replace(".bson", "")) for pidb in os.listdir(logs) if ".bson" in pidb]
# in case if injection not follows
if self.results.get("procmemory") is not None:
pids += [int(block["pid"]) for block in self.results["procmemory"]]
if self.results.get("procdump") is not None:
pids += [int(block["pid"]) for block in self.results["procdump"]]
log.debug(list(set(pids)))
return list(set(pids))
def advanced_url_parse(self, url):
if HAVE_TLDEXTRACT:
EXTRA_SUFFIXES = ("bit",)
parsed = False
try:
parsed = TLDExtract(extra_suffixes=EXTRA_SUFFIXES, suffix_list_urls=None)(url)
except Exception as e:
log.error(e)
return parsed
else:
log.info("missed tldextract dependency")
def _get_ip_by_host(self, hostname):
return next(
(
[data.get("ip", "")]
for data in self.results.get("network", {}).get("hosts", [])
if data.get("hostname", "") == hostname
),
[],
)
def _get_ip_by_host_dns(self, hostname):
ips = []
try:
answers = myresolver.query(hostname, "A")
for rdata in answers:
n = dns.reversename.from_address(rdata.address)
try:
answers_inv = myresolver.query(n, "PTR")
ips.extend(rdata.address for _ in answers_inv)
except dns.resolver.NoAnswer:
ips.append(rdata.address)
except dns.resolver.NXDOMAIN:
ips.append(rdata.address)
except dns.name.NeedAbsoluteNameOrOrigin:
print(
"An attempt was made to convert a non-absolute name to wire when there was also a non-absolute (or missing) origin"
)
except dns.resolver.NoAnswer:
print("IPs: Impossible to get response")
except Exception as e:
log.info(str(e))
return ips
def _is_ip(self, ip):
# is this string an ip?
try:
socket.inet_aton(ip)
return True
except Exception:
return False
def _check_valid_url(self, url, all_checks=False):