Exemplo n.º 1
0
def store_and_submit_fileobj(fobj, filename, package="", options="", timeout=0, priority=1, machine="", platform=""):
    # Do everything in tmppath/TMPSUBDIR
    tmppath = tempfile.gettempdir()
    targetpath = os.path.join(tmppath, TMPSUBDIR)
    if not os.path.exists(targetpath): os.mkdir(targetpath)

    # Upload will be stored in a tmpdir with the original name
    tmpdir = tempfile.mkdtemp(prefix="upload_", dir=targetpath)
    tmpf = open(os.path.join(tmpdir, filename), "wb")
    t = fobj.read(BUFSIZE)

    # While reading from client also compute md5hash
    md5h = hashlib.md5()
    while t:
        md5h.update(t)
        tmpf.write(t)
        t = fobj.read(BUFSIZE)

    tmpf.close()

    # Submit task to cuckoo db
    db = Database()
    task_id = db.add(file_path=tmpf.name,
                     md5=md5h.hexdigest(),
                     package=package,
                     timeout=timeout,
                     options=options,
                     priority=priority,
                     machine=machine,
                     platform=platform)

    return task_id
Exemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("path", type=str, help="Path to the file to analyze")
    parser.add_argument("--package", type=str, action="store", default="", help="Specify an analysis package", required=False)
    parser.add_argument("--custom", type=str, action="store", default="", help="Specify any custom value", required=False)
    parser.add_argument("--timeout", type=int, action="store", default=0, help="Specify an analysis timeout", required=False)
    parser.add_argument("--options", type=str, action="store", default="", help="Specify options for the analysis package (e.g. \"name=value,name2=value2\")", required=False)
    parser.add_argument("--priority", type=int, action="store", default=1, help="Specify a priority for the analysis represented by an integer", required=False)
    parser.add_argument("--machine", type=str, action="store", default="", help="Specify the identifier of a machine you want to use", required=False)
    parser.add_argument("--platform", type=str, action="store", default="", help="Specify the operating system platform you want to use (windows/darwin/linux)", required=False)

    try:
        args = parser.parse_args()
    except IOError as e:
        parser.error(e)
        return False

    if not os.path.exists(args.path):
        print("ERROR: the specified file does not exist at path \"%s\"" % args.path)
        return False

    db = Database()

    task_id = db.add(file_path=args.path,
                     md5=File(args.path).get_md5(),
                     package=args.package,
                     timeout=args.timeout,
                     options=args.options,
                     priority=args.priority,
                     machine=args.machine,
                     platform=args.platform,
                     custom=args.custom)

    print("SUCCESS: Task added with id %d" % task_id)
Exemplo n.º 3
0
def store_and_submit_fileobj(fobj,
                             filename,
                             desc,
                             package="",
                             options="",
                             timeout=0,
                             priority=1,
                             machines="",
                             platform=""):
    # Do everything in tmppath/TMPSUBDIR
    tmppath = tempfile.gettempdir()
    targetpath = os.path.join(tmppath, TMPSUBDIR)
    if not os.path.exists(targetpath): os.mkdir(targetpath)

    # Upload will be stored in a tmpdir with the original name
    tmpdir = tempfile.mkdtemp(prefix="upload_", dir=targetpath)
    tmpf = open(os.path.join(tmpdir, filename), "wb")
    t = fobj.read(BUFSIZE)

    # While reading from client also compute md5hash
    md5h = hashlib.md5()
    while t:
        md5h.update(t)
        tmpf.write(t)
        t = fobj.read(BUFSIZE)

    tmpf.close()

    # Submit task to cuckoo db
    db = Database()
    # Create executable record if needed
    exe_id = db.add_exe(file_path=tmpf.name, md5=md5h.hexdigest())
    #print("EXE ID: %s, 0: %s" % (str(exe_id), exe_id[0]))
    # Create analysis record
    a_id = db.add_analysis(desc, exe_id)
    #print("ANALYSIS ID: %s" % anal_id)

    for machine in machines.split(","):
        task_id = db.add(file_path=tmpf.name,
                         a_id=a_id,
                         md5=md5h.hexdigest(),
                         package=package,
                         timeout=timeout,
                         options=options,
                         priority=priority,
                         machine=machine,
                         platform=platform)
        sleep(3)
        print("TASK ID: %s" % task_id)

    return a_id
Exemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("path", type=str, help="Path to the file to analyze")
    parser.add_argument("--package", type=str, action="store", default="", help="Specify an analysis package", required=False)
    parser.add_argument("--custom", type=str, action="store", default="", help="Specify any custom value", required=False)
    parser.add_argument("--timeout", type=int, action="store", default=0, help="Specify an analysis timeout", required=False)
    parser.add_argument("--options", type=str, action="store", default="", help="Specify options for the analysis package (e.g. \"name=value,name2=value2\")", required=False)
    parser.add_argument("--priority", type=int, action="store", default=1, help="Specify a priority for the analysis represented by an integer", required=False)
    parser.add_argument("--machine", type=str, action="store", default="", help="Specify the identifier of a machine you want to use", required=False)
    parser.add_argument("--platform", type=str, action="store", default="", help="Specify the operating system platform you want to use (windows/darwin/linux)", required=False)

    try:
        args = parser.parse_args()
    except IOError as e:
        parser.error(e.message)
        return False

    if not os.path.exists(args.path):
        print("ERROR: the specified file does not exist at path \"%s\"" % args.path)
        return False

    db = Database()
    # Add executable to db
    exe_id = db.add_exe(file_path=args.path,
                        md5=File(args.path).get_md5())
    print("SUCCESS: Created executable id: %d" % exe_id)
    # Create analysis
    anal_id = db.add_analysis("New analysis", exe_id)
    print("SUCCESS: Created new analysis with id: %d" % anal_id)
    # Add tasks for every machine
    for machine in args.machine.split(","):
        task_id = db.add(file_path=args.path,
                     anal_id=anal_id,
                     md5=File(args.path).get_md5(),
                     package=args.package,
                     timeout=args.timeout,
                     options=args.options,
                     priority=args.priority,
                     machine=machine,
                     platform=args.platform,
                     custom=args.custom)
        print("SUCCESS: Task added with id %d" % task_id)
        # Sleep needed for multiple VM startup with VMWare
        sleep(5)
        
    print("SUCCESS: All Tasks added to Analysis")
Exemplo n.º 5
0
def store_and_submit_fileobj(fobj, filename, desc, package="", 
                            options="", timeout=0, priority=1, machines="", platform=""):
    # Do everything in tmppath/TMPSUBDIR
    tmppath = tempfile.gettempdir()
    targetpath = os.path.join(tmppath, TMPSUBDIR)
    if not os.path.exists(targetpath): os.mkdir(targetpath)

    # Upload will be stored in a tmpdir with the original name
    tmpdir = tempfile.mkdtemp(prefix="upload_", dir=targetpath)
    tmpf = open(os.path.join(tmpdir, filename), "wb")
    t = fobj.read(BUFSIZE)

    # While reading from client also compute md5hash
    md5h = hashlib.md5()
    while t:
        md5h.update(t)
        tmpf.write(t)
        t = fobj.read(BUFSIZE)

    tmpf.close()

    # Submit task to cuckoo db
    db = Database()
    # Create executable record if needed
    exe_id = db.add_exe(file_path=tmpf.name,
                        md5=md5h.hexdigest())
    #print("EXE ID: %s, 0: %s" % (str(exe_id), exe_id[0]))
    # Create analysis record
    a_id = db.add_analysis(desc, exe_id)
    #print("ANALYSIS ID: %s" % anal_id)
    
    for machine in machines.split(","):
        task_id = db.add(file_path=tmpf.name,
                         a_id=a_id,
                         md5=md5h.hexdigest(),
                         package=package,
                         timeout=timeout,
                         options=options,
                         priority=priority,
                         machine=machine,
                         platform=platform)
        sleep(3)
        print("TASK ID: %s" % task_id)
        
    return a_id
Exemplo n.º 6
0
def store_and_submit_fileobj(fobj,
                             filename,
                             package="",
                             options="",
                             timeout=0,
                             priority=1,
                             machine="",
                             platform=""):
    # Do everything in tmppath/TMPSUBDIR
    tmppath = tempfile.gettempdir()
    targetpath = os.path.join(tmppath, TMPSUBDIR)
    if not os.path.exists(targetpath): os.mkdir(targetpath)

    # Upload will be stored in a tmpdir with the original name
    tmpdir = tempfile.mkdtemp(prefix="upload_", dir=targetpath)
    tmpf = open(os.path.join(tmpdir, filename), "wb")
    t = fobj.read(BUFSIZE)

    # While reading from client also compute md5hash
    md5h = hashlib.md5()
    while t:
        md5h.update(t)
        tmpf.write(t)
        t = fobj.read(BUFSIZE)

    tmpf.close()

    # Submit task to cuckoo db
    db = Database()
    task_id = db.add(file_path=tmpf.name,
                     md5=md5h.hexdigest(),
                     package=package,
                     timeout=timeout,
                     options=options,
                     priority=priority,
                     machine=machine,
                     platform=platform)

    return task_id
Exemplo n.º 7
0
class TestDatabase:
    def setUp(self):
        self.tmp = os.path.join(tempfile.mkdtemp(), "dbtestcuckoo")
        self.d = Database(db_file=self.tmp)

    def test_db_path_default(self):
        """@note: Regression unit test."""
        d = Database()
        assert_equals(d.db_file, os.path.join(CUCKOO_ROOT, "db", "cuckoo.db"))
        assert os.path.exists(self.d.db_file)

    def test_db_path_custom(self):
        """@note: Regression unit test."""
        tmp = tempfile.mkstemp()[1]
        d = Database(db_file=tmp)
        assert_equals(d.db_file, tmp)
        assert os.path.exists(self.d.db_file)
        os.remove(tmp)

    def test_generate(self):
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute(
            "SELECT count(name) FROM sqlite_master WHERE name='tasks';")
        assert_equals(1, cursor.fetchone()[0])

    def test_add(self):
        tmp = tempfile.mkstemp()[1]
        assert_equals(1, self.d.add(file_path=tmp))
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute("SELECT count(*) FROM tasks;")
        assert_equals(1, cursor.fetchone()[0])
        os.remove(tmp)

    def test_add_file_not_found(self):
        assert_equals(None, self.d.add(file_path="foo"))

    def test_fetch(self):
        tmp = tempfile.mkstemp()[1]
        assert_equals(1, self.d.add(file_path=tmp))
        assert_equals(tmp, self.d.fetch()['file_path'])
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute("SELECT count(*) FROM tasks;")
        assert_equals(1, cursor.fetchone()[0])
        os.remove(tmp)

    def test_lock(self):
        tmp = tempfile.mkstemp()[1]
        assert_equals(1, self.d.add(file_path=tmp))
        assert self.d.lock(1)
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute("SELECT count(*) FROM tasks WHERE lock=0;")
        assert_equals(0, cursor.fetchone()[0])
        os.remove(tmp)

    def test_unlock(self):
        tmp = tempfile.mkstemp()[1]
        assert_equals(1, self.d.add(file_path=tmp))
        assert self.d.lock(1)
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute("SELECT count(*) FROM tasks WHERE lock=0;")
        assert_equals(0, cursor.fetchone()[0])
        assert self.d.unlock(1)
        cursor.execute("SELECT count(*) FROM tasks WHERE lock=0;")
        assert_equals(1, cursor.fetchone()[0])
        os.remove(tmp)

    def test_complete_success(self):
        tmp = tempfile.mkstemp()[1]
        assert_equals(1, self.d.add(file_path=tmp))
        assert self.d.complete(1, True)
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute("SELECT count(*) FROM tasks WHERE status=2;")
        assert_equals(1, cursor.fetchone()[0])
        os.remove(tmp)

    def test_complete_fail(self):
        tmp = tempfile.mkstemp()[1]
        assert_equals(1, self.d.add(file_path=tmp))
        assert self.d.complete(1, False)
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute("SELECT count(*) FROM tasks WHERE status=1;")
        assert_equals(1, cursor.fetchone()[0])
        os.remove(tmp)

    def tearDown(self):
        os.remove(self.tmp)
Exemplo n.º 8
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("path", type=str, help="Path to the file to analyze")
    parser.add_argument("--package",
                        type=str,
                        action="store",
                        default="",
                        help="Specify an analysis package",
                        required=False)
    parser.add_argument("--custom",
                        type=str,
                        action="store",
                        default="",
                        help="Specify any custom value",
                        required=False)
    parser.add_argument("--timeout",
                        type=int,
                        action="store",
                        default=0,
                        help="Specify an analysis timeout",
                        required=False)
    parser.add_argument(
        "--options",
        type=str,
        action="store",
        default="",
        help=
        "Specify options for the analysis package (e.g. \"name=value,name2=value2\")",
        required=False)
    parser.add_argument(
        "--priority",
        type=int,
        action="store",
        default=1,
        help="Specify a priority for the analysis represented by an integer",
        required=False)
    parser.add_argument(
        "--machine",
        type=str,
        action="store",
        default="",
        help="Specify the identifier of a machine you want to use",
        required=False)
    parser.add_argument(
        "--platform",
        type=str,
        action="store",
        default="",
        help=
        "Specify the operating system platform you want to use (windows/darwin/linux)",
        required=False)

    try:
        args = parser.parse_args()
    except IOError as e:
        parser.error(e)
        return False

    if not os.path.exists(args.path):
        print("ERROR: the specified file does not exist at path \"%s\"" %
              args.path)
        return False

    db = Database()

    task_id = db.add(file_path=args.path,
                     md5=File(args.path).get_md5(),
                     package=args.package,
                     timeout=args.timeout,
                     options=args.options,
                     priority=args.priority,
                     machine=args.machine,
                     platform=args.platform,
                     custom=args.custom)

    print("SUCCESS: Task added with id %d" % task_id)
Exemplo n.º 9
0
class TestDatabase:
    def setUp(self):
        self.tmp = os.path.join(tempfile.mkdtemp(), "dbtestcuckoo")
        self.d = Database(db_file=self.tmp)

    def test_db_path_default(self):
        """@note: Regression unit test."""
        d = Database()
        assert_equals(d.db_file, os.path.join(CUCKOO_ROOT, "db", "cuckoo.db"))
        assert os.path.exists(self.d.db_file)

    def test_db_path_custom(self):
        """@note: Regression unit test."""
        tmp = tempfile.mkstemp()[1]
        d = Database(db_file=tmp)
        assert_equals(d.db_file, tmp)
        assert os.path.exists(self.d.db_file)
        os.remove(tmp)

    def test_generate(self):
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute("SELECT count(name) FROM sqlite_master WHERE name='tasks';")
        assert_equals(1, cursor.fetchone()[0])

    def test_add(self):
        tmp = tempfile.mkstemp()[1]
        assert_equals(1, self.d.add(file_path=tmp))
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute("SELECT count(*) FROM tasks;")
        assert_equals(1, cursor.fetchone()[0])
        os.remove(tmp)

    def test_add_file_not_found(self):
        assert_equals(None, self.d.add(file_path="foo"))

    def test_fetch(self):
        tmp = tempfile.mkstemp()[1]
        assert_equals(1, self.d.add(file_path=tmp))
        assert_equals(tmp, self.d.fetch()["file_path"])
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute("SELECT count(*) FROM tasks;")
        assert_equals(1, cursor.fetchone()[0])
        os.remove(tmp)

    def test_lock(self):
        tmp = tempfile.mkstemp()[1]
        assert_equals(1, self.d.add(file_path=tmp))
        assert self.d.lock(1)
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute("SELECT count(*) FROM tasks WHERE lock=0;")
        assert_equals(0, cursor.fetchone()[0])
        os.remove(tmp)

    def test_unlock(self):
        tmp = tempfile.mkstemp()[1]
        assert_equals(1, self.d.add(file_path=tmp))
        assert self.d.lock(1)
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute("SELECT count(*) FROM tasks WHERE lock=0;")
        assert_equals(0, cursor.fetchone()[0])
        assert self.d.unlock(1)
        cursor.execute("SELECT count(*) FROM tasks WHERE lock=0;")
        assert_equals(1, cursor.fetchone()[0])
        os.remove(tmp)

    def test_complete_success(self):
        tmp = tempfile.mkstemp()[1]
        assert_equals(1, self.d.add(file_path=tmp))
        assert self.d.complete(1, True)
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute("SELECT count(*) FROM tasks WHERE status=2;")
        assert_equals(1, cursor.fetchone()[0])
        os.remove(tmp)

    def test_complete_fail(self):
        tmp = tempfile.mkstemp()[1]
        assert_equals(1, self.d.add(file_path=tmp))
        assert self.d.complete(1, False)
        conn = sqlite3.connect(self.tmp)
        cursor = conn.cursor()
        cursor.execute("SELECT count(*) FROM tasks WHERE status=1;")
        assert_equals(1, cursor.fetchone()[0])
        os.remove(tmp)

    def tearDown(self):
        os.remove(self.tmp)
Exemplo n.º 10
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("path", type=str, help="Path to the file to analyze")
    parser.add_argument("--package",
                        type=str,
                        action="store",
                        default="",
                        help="Specify an analysis package",
                        required=False)
    parser.add_argument("--custom",
                        type=str,
                        action="store",
                        default="",
                        help="Specify any custom value",
                        required=False)
    parser.add_argument("--timeout",
                        type=int,
                        action="store",
                        default=0,
                        help="Specify an analysis timeout",
                        required=False)
    parser.add_argument(
        "--options",
        type=str,
        action="store",
        default="",
        help=
        "Specify options for the analysis package (e.g. \"name=value,name2=value2\")",
        required=False)
    parser.add_argument(
        "--priority",
        type=int,
        action="store",
        default=1,
        help="Specify a priority for the analysis represented by an integer",
        required=False)
    parser.add_argument(
        "--machine",
        type=str,
        action="store",
        default="",
        help="Specify the identifier of a machine you want to use",
        required=False)
    parser.add_argument(
        "--platform",
        type=str,
        action="store",
        default="",
        help=
        "Specify the operating system platform you want to use (windows/darwin/linux)",
        required=False)

    try:
        args = parser.parse_args()
    except IOError as e:
        parser.error(e.message)
        return False

    if not os.path.exists(args.path):
        print("ERROR: the specified file does not exist at path \"%s\"" %
              args.path)
        return False

    db = Database()
    # Add executable to db
    exe_id = db.add_exe(file_path=args.path, md5=File(args.path).get_md5())
    print("SUCCESS: Created executable id: %d" % exe_id)
    # Create analysis
    anal_id = db.add_analysis("New analysis", exe_id)
    print("SUCCESS: Created new analysis with id: %d" % anal_id)
    # Add tasks for every machine
    for machine in args.machine.split(","):
        task_id = db.add(file_path=args.path,
                         anal_id=anal_id,
                         md5=File(args.path).get_md5(),
                         package=args.package,
                         timeout=args.timeout,
                         options=args.options,
                         priority=args.priority,
                         machine=machine,
                         platform=args.platform,
                         custom=args.custom)
        print("SUCCESS: Task added with id %d" % task_id)
        # Sleep needed for multiple VM startup with VMWare
        sleep(5)

    print("SUCCESS: All Tasks added to Analysis")
Exemplo n.º 11
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("path", type=str, help="Path to the file or folder to analyze")
    parser.add_argument(
        "--package", type=str, action="store", default="", help="Specify an analysis package", required=False
    )
    parser.add_argument(
        "--custom", type=str, action="store", default="", help="Specify any custom value", required=False
    )
    parser.add_argument(
        "--timeout", type=int, action="store", default=0, help="Specify an analysis timeout", required=False
    )
    parser.add_argument(
        "--options",
        type=str,
        action="store",
        default="",
        help='Specify options for the analysis package (e.g. "name=value,name2=value2")',
        required=False,
    )
    parser.add_argument(
        "--priority",
        type=int,
        action="store",
        default=1,
        help="Specify a priority for the analysis represented by an integer",
        required=False,
    )
    parser.add_argument(
        "--machine",
        type=str,
        action="store",
        default="",
        help="Specify the identifier of a machine you want to use",
        required=False,
    )
    parser.add_argument(
        "--platform",
        type=str,
        action="store",
        default="",
        help="Specify the operating system platform you want to use (windows/darwin/linux)",
        required=False,
    )

    try:
        args = parser.parse_args()
    except IOError as e:
        parser.error(e)
        return False

    # Get absolute path to deal with relative.
    path = os.path.abspath(args.path)

    if not os.path.exists(path):
        print('ERROR: the specified file/folder does not exist at path "%s"' % path)
        return False

    files = []
    if os.path.isdir(path):
        for dirname, dirnames, filenames in os.walk(path):
            for file_name in filenames:
                file_path = os.path.join(dirname, file_name)

                if os.path.isfile(file_path):
                    files.append(file_path)
    else:
        files.append(path)

    db = Database()
    for file_path in files:
        task_id = db.add(
            file_path=file_path,
            md5=File(file_path).get_md5(),
            package=args.package,
            timeout=args.timeout,
            options=args.options,
            priority=args.priority,
            machine=args.machine,
            platform=args.platform,
            custom=args.custom,
        )

        print('SUCCESS: File "%s" added as task with id %d' % (file_path, task_id))