Exemple #1
0
    def load_task_dict(self, task_dict):
        """Load all dict key values as attributes to the object.
        Try to change target dictionaries to target objects"""
        # Cast to Cuckoo dictionary, so keys can be accessed as attributes
        targets = []
        for target_dict in task_dict.get("targets", []):
            target = Target()
            target.target_dict = target_dict
            targets.append(target)

        newtask = DbTask().to_dict()
        task_dict["targets"] = targets
        newtask.update(task_dict)
        self.task_dict = newtask
        self.path = cwd(analysis=task_dict["id"])
Exemple #2
0
    def add_massurl(self,
                    urls=[],
                    package="ie",
                    options="",
                    priority=1,
                    custom="",
                    owner="",
                    machine="",
                    platform="",
                    tags=None,
                    memory=False,
                    clock=None,
                    start_on=None):
        if not urls:
            log.error("No URLs provided. Cannot create task.")
            return None

        return self.add(targets=Target.create_urls(urls),
                        timeout=len(urls) * 60,
                        package=package,
                        options=options,
                        priority=priority,
                        custom=custom,
                        owner=owner,
                        machine=machine,
                        platform=platform,
                        tags=tags,
                        memory=memory,
                        enforce_timeout=True,
                        clock=clock,
                        task_type="massurl",
                        start_on=start_on)
Exemple #3
0
def process_task(task):
    db = Database()
    if not task.dir_exists():
        log.error("Task #%s directory %s does not exist, cannot process it",
                  task.id, task.path)
        db.set_status(task.id, TASK_FAILED_PROCESSING)
        return

    task_log_start(task.id)

    if task.targets:
        target = task.targets[0]
    else:
        target = Target()

    logger("Starting task reporting",
           action="task.report",
           status="pending",
           target=target.target,
           category=target.category,
           package=task["package"],
           options=emit_options(task["options"]),
           custom=task["custom"])

    success = False
    try:
        success = task.process()
    except Exception as e:
        log.error("Failed to process task #%s. Error: %s", task.id, e)
    finally:
        if success:
            log.info("Task #%d: reports generation completed",
                     task.id,
                     extra={
                         "action": "task.report",
                         "status": "success",
                     })
            db.set_status(task.id, TASK_REPORTED)
        else:
            log.error("Failed to process task #%s",
                      task.id,
                      extra={
                          "action": "task.report",
                          "status": "failed",
                      })
            db.set_status(task.id, TASK_FAILED_PROCESSING)
        task_log_stop(task.id)
Exemple #4
0
    def set_task(self, db_task):
        """Update Task wrapper with new task db object
        @param db_task: Task Db object"""
        self.db_task = db_task
        self.path = cwd(analysis=db_task.id)
        self.task_dict = db_task.to_dict()

        self.task_dict["targets"] = [
            Target(db_target) for db_target in db_task.targets
        ]
        # For backwards compatibility, load these two attributes
        # TODO Remove when processing and reporting changes
        if self.task_dict["targets"]:
            self.task_dict["target"] = self.task_dict["targets"][0].target
            self.task_dict["category"] = self.task_dict["targets"][0].category
        else:
            self.task_dict["target"] = "none"
            self.task_dict["category"] = None
Exemple #5
0
    def add(self,
            targets=[],
            timeout=0,
            package="",
            options="",
            priority=1,
            custom="",
            owner="",
            machine="",
            platform="",
            tags=None,
            memory=False,
            enforce_timeout=False,
            clock=None,
            task_type=None,
            submit_id=None,
            start_on=None,
            longterm_id=None):
        """Create new task
        @param targets: List of ORM Target objects.
        @param timeout: selected timeout.
        @param package: the analysis package to use
        @param options: analysis options.
        @param priority: analysis priority.
        @param custom: custom options.
        @param owner: task owner.
        @param machine: selected machine.
        @param platform: platform.
        @param tags: optional tags that must be set for machine selection
        @param memory: toggle full memory dump.
        @param enforce_timeout: toggle full timeout execution.
        @param clock: virtual machine clock time
        @param task_type: The type of task: regular, longterm, other type
        @param longterm_id: Longterm analysis ID to connect this task to
        @return: task id or None.
        """
        if isinstance(start_on, basestring):
            start_on = str_to_datetime(start_on)
            if not start_on:
                log.error("'start on' format should be: 'YYYY-M-D H:M'")
                return None

        # If no clock time was provided, but a specific starting time/date
        # was, also use this starting time for the system clock instead of
        # the default current time (now).
        if not clock and start_on:
            clock = start_on

        if clock and isinstance(clock, basestring):
            clock = str_to_datetime(clock)
            if not clock:
                log.warning(
                    "Datetime %s not in format M-D-YYY H:M:S. Using current "
                    "timestamp", clock)
                clock = datetime.datetime.now()

        newtask = DbTask()
        newtask.type = task_type
        newtask.timeout = timeout
        newtask.priority = priority
        newtask.custom = custom
        newtask.owner = owner
        newtask.machine = machine
        newtask.package = package
        newtask.options = options
        newtask.platform = platform
        newtask.memory = memory
        newtask.enforce_timeout = enforce_timeout
        newtask.clock = clock
        newtask.submit_id = submit_id
        newtask.start_on = start_on
        newtask.longterm_id = longterm_id

        session = db.Session()
        for tag in self.get_tags_list(tags):
            newtask.tags.append(db.get_or_create(session, name=tag))

        session.add(newtask)
        try:
            session.commit()
        except SQLAlchemyError as e:
            log.exception("Exception when adding task to database: %s", e)
            session.rollback()
            session.close()
            return None

        task_id = newtask.id
        for t in targets:
            t.task_id = task_id

        try:
            if len(targets) > 1:
                # Bulk add targets
                db.engine.execute(DbTarget.__table__.insert(),
                                  [t.to_dict(exclude=["id"]) for t in targets])
            elif targets:
                session.add(targets[0])
                session.commit()

            # Create the directories for this task
            self.create_dirs(id=task_id)

            # If the target type is a file, create a symlink pointing to it
            # inside the task folder.
            if targets and targets[0].category in Target.files:
                Target(targets[0]).symlink_to_task(task_id)

        except SQLAlchemyError as e:
            log.exception("Exception while adding targets to database: %s", e)
            session.rollback()
            return None
        finally:
            session.close()

        return task_id
Exemple #6
0
from cuckoo.common.config import config
from cuckoo.common.exceptions import CuckooOperationalError
from cuckoo.common.files import Folders, Files
from cuckoo.common.utils import (get_directory_size, json_default, json_encode,
                                 str_to_datetime)
from cuckoo.core.database import (Database, TASK_RECOVERED, Task as DbTask,
                                  Target as DbTarget)
from cuckoo.core.plugins import RunProcessing, RunSignatures, RunReporting
from cuckoo.core.target import Target
from cuckoo.misc import cwd

from sqlalchemy.exc import SQLAlchemyError

log = logging.getLogger(__name__)
db = Database()
target = Target()


class Task(object):

    dirs = ["shots", "logs", "files", "extracted", "buffer", "memory"]
    latest_symlink_lock = threading.Lock()

    def __init__(self, db_task=None):
        self.db_task = None
        self.task_dict = {}

        if db_task:
            self.set_task(db_task)

    def set_task(self, db_task):
Exemple #7
0
 def set_target(self, targets):
     if targets:
         self.target = targets[0]
     else:
         self.target = Target()
import mock
import os
import pytest
import shutil
import tempfile
import zipfile

from cuckoo.common.objects import File
from cuckoo.core.database import Database, Task as DbTask
from cuckoo.core.task import Task
from cuckoo.core.target import Target
from cuckoo.main import cuckoo_create
from cuckoo.misc import set_cwd, cwd

submit_task = Task()
create_target = Target()

class TestTask(object):
    def setup(self):
        self.cwd = tempfile.mkdtemp()
        set_cwd(self.cwd)
        cuckoo_create()
        self.db = Database()
        self.db.connect()
        self.tmpfile = None
        self.files = []

    def teardown(self):
        shutil.rmtree(self.cwd)
        for path in self.files:
            try: