Permalink
Browse files

Refactoring

  • Loading branch information...
botherder committed Apr 28, 2012
1 parent c317a9f commit b4e87ea55f4074bc2ad4a3e896deb58e664d53f6
View
@@ -5,7 +5,7 @@ db/cuckoo.db
log/*.log
# Ignore Cuckoo analyses
-analysis/*
+storage/*
# Ignore Python byte code
*.pyc
@@ -108,7 +108,7 @@ def terminate(self):
else:
return False
- def inject(self, dll = "dll\\cmonitor.dll"):
+ def inject(self, dll="dll\\cmonitor.dll"):
if self.pid == 0:
return False
View
@@ -1,10 +1,6 @@
[Cuckoo]
debug = off
-
analysis_timeout = 120
-
critical_timeout = 600
-
delete_original = off
-
-machiner = virtualbox
+machine_manager = virtualbox
View
@@ -1,18 +1,18 @@
#!/usr/bin/env python
from lib.cuckoo.common.logo import logo
-from lib.cuckoo.core.startup import check_dependencies, create_folders
+from lib.cuckoo.core.startup import check_dependencies, create_structure
from lib.cuckoo.core.scheduler import Scheduler
def main():
logo()
check_dependencies()
- create_folders()
+ create_structure()
try:
- s = Scheduler()
- s.start()
+ sched = Scheduler()
+ sched.start()
except KeyboardInterrupt:
- s.stop()
+ sched.stop()
if __name__ == "__main__":
try:
File renamed without changes.
@@ -0,0 +1,7 @@
+class Analysis(object):
+ def __init__(self, analysis_path=None):
+ if not analysis_path:
+ return
+
+ def run(self):
+ raise NotImplementedError
@@ -0,0 +1,6 @@
+class Dictionary(dict):
+ def __getattr__(self, key):
+ return self.get(key, None)
+
+ __setattr__ = dict.__setitem__
+ __delattr__ = dict.__delitem__
@@ -0,0 +1,11 @@
+class GuestManager(object):
+ def __init__(self, address, user, password):
+ self.address = address
+ self.user = user
+ self.password = password
+
+ def start_analysis(self):
+ raise NotImplementedError
+
+ def get_results(self):
+ raise NotImplementedError
@@ -1,11 +1,8 @@
-class BaseMachiner:
+class MachineManager(object):
def __init__(self):
pass
- def prepare(self):
- raise NotImplementedError
-
- def get_machine(self, label=None):
+ def acquire(self, label=None):
raise NotImplementedError
def start(self, label=None):
@@ -0,0 +1,10 @@
+class Report(object):
+ def __init__(self, analysis_path):
+ self.analysis_path = analysis_path
+ self.options = None
+
+ def set_options(self, options):
+ self.options = options
+
+ def run(self)
+ raise NotImplementedError
@@ -0,0 +1,8 @@
+class Signature(object):
+ def __init__(self):
+ self.alert = False
+ self.enabled = True
+ self.data = []
+
+ def run(self, results=None):
+ raise NotImplementedError
@@ -1,18 +1,18 @@
-import os
-import sys
import ConfigParser
class Config:
- def __init__(self, root="."):
- config_path = os.path.join(root, "conf/cuckoo.conf")
- if not os.path.exists(config_path):
- sys.exit("Configuration file does not exist")
-
+ def __init__(self, cfg="conf/cuckoo.conf"):
config = ConfigParser.ConfigParser()
- config.read(config_path)
+ config.read(cfg)
+
+ for section in config.sections():
+ for name, raw_value in config.items(section):
+ try:
+ value = config.getboolean(section, name)
+ except ValueError:
+ try:
+ value = config.getint(section, name)
+ except ValueError:
+ value = config.get(section, name)
- self.debug = config.getboolean("Cuckoo", "debug")
- self.analysis_timeout = config.getint("Cuckoo", "analysis_timeout")
- self.critical_timeout = config.getint("Cuckoo", "critical_timeout")
- self.delete_original = config.getboolean("Cuckoo", "delete_original")
- self.machiner = config.get("Cuckoo", "machiner")
+ setattr(self, name, value)
@@ -2,6 +2,8 @@
import sys
import sqlite3
+from lib.cuckoo.abstract.dictionary import Dictionary
+
class Database:
def __init__(self, root="."):
self.db_file = os.path.join(root, "db/cuckoo.db")
@@ -28,11 +30,11 @@ def generate(self):
cursor.execute("CREATE TABLE tasks (\n" \
" id INTEGER PRIMARY KEY,\n" \
" md5 TEXT DEFAULT NULL,\n" \
- " target TEXT NOT NULL,\n" \
+ " file_path TEXT NOT NULL,\n" \
" timeout INTEGER DEFAULT NULL,\n" \
" priority INTEGER DEFAULT 0,\n" \
" custom TEXT DEFAULT NULL,\n" \
- " vm_id TEXT DEFAULT NULL,\n" \
+ " machine TEXT DEFAULT NULL,\n" \
" package TEXT DEFAULT NULL,\n" \
" platform TEXT DEFAULT NULL,\n" \
" added_on DATE DEFAULT CURRENT_TIMESTAMP,\n" \
@@ -49,60 +51,63 @@ def generate(self):
return True
- def get_dict(self, row):
+ def dictify(self, row):
try:
- task = {"id" : row[0],
- "md5" : row[1],
- "target" : row[2],
- "timeout" : row[3],
- "priority" : row[4],
- "custom" : row[5],
- "vm_id" : row[6],
- "package" : row[7],
- "platform" : row[8],
- "added_on" : row[9],
- "completed_on" : row[10],
- "lock" : row[11],
- "status" : row[12]}
+ task = Dictionary()
+ task.id = row[0]
+ task.md5 = row[1]
+ task.file_path = row[2]
+ task.timeout = row[3]
+ task.priority = row[4]
+ task.custom = row[5]
+ task.machine = row[6]
+ task.package = row[7]
+ task.platform = row[8]
+ task.added_on = row[9]
+ task.completed_on = row[10]
+ task.lock = row[11]
+ task.status = row[12]
except IndexError as e:
+ print e
return None
return task
- def add_task(self,
- target,
- md5=None,
- timeout=None,
- package=None,
- priority=None,
- custom=None,
- vm_id=None):
- if not target or not os.path.exists(target):
+ def add(self,
+ file_path,
+ md5=None,
+ timeout=None,
+ package=None,
+ priority=None,
+ custom=None,
+ machine=None):
+ if not file_path or not os.path.exists(file_path):
return None
try:
self.cursor.execute("INSERT INTO tasks " \
- "(target, md5, timeout, package, priority, custom, vm_id) " \
+ "(file_path, md5, timeout, package, priority, custom, machine) " \
"VALUES (?, ?, ?, ?, ?, ?, ?);",
- (target, md5, timeout, package, priority, custom, vm_id))
+ (file_path, md5, timeout, package, priority, custom, machine))
self.conn.commit()
return self.cursor.lastrowid
except sqlite3.OperationalError as e:
return None
- def get_task(self):
+ def fetch(self):
try:
self.cursor.execute("SELECT * FROM tasks " \
"WHERE lock = 0 " \
"AND status = 0 " \
"ORDER BY priority, added_on LIMIT 1;")
except sqlite3.OperationalError as e:
+ print e
return None
row = self.cursor.fetchone()
if row:
- return self.get_dict(row)
+ return self.dictify(row)
else:
return None
View
@@ -0,0 +1,40 @@
+import os
+
+try:
+ import magic
+except ImportError:
+ pass
+
+def create_folders(root=".", folders=[]):
+ for folder in folders:
+ if os.path.exists(folder):
+ continue
+
+ try:
+ folder_path = os.path.join(root, folder)
+ os.makedirs(folder_path)
+ except OSError as e:
+ continue
+
+def get_file_type(file_path):
+ if not os.path.exists(file_path):
+ return None
+
+ data = open(file_path, "rb").read()
+
+ try:
+ ms = magic.open(magic.MAGIC_NONE)
+ ms.load()
+ file_type = ms.buffer(data)
+ except:
+ try:
+ file_type = magic.from_buffer(data)
+ except:
+ try:
+ import subprocess
+ file_process = subprocess.Popen(['file', '-b', file_path], stdout = subprocess.PIPE)
+ file_type = file_process.stdout.read().strip()
+ except:
+ return None
+
+ return file_type
@@ -1,42 +1,64 @@
import sys
import time
+from threading import Thread
from lib.cuckoo.common.config import Config
from lib.cuckoo.common.database import Database
+from lib.cuckoo.abstract.machinemanager import MachineManager
+from lib.cuckoo.abstract.guestmanager import GuestManager
+
+MACHINES = []
+
+class AnalysisManager(Thread):
+ def __init__(self, task):
+ Thread.__init__(self)
+ self.cfg = Config()
+ self.db = Database()
+ self.task = task
+
+ def run(self):
+ print MACHINES
+ print self.task
class Scheduler:
def __init__(self):
self.running = True
+ self.config = Config()
+ self.db = Database()
- def stop(self):
- self.running = False
-
- def start(self):
- config = Config()
- db = Database()
+ def initialize(self):
+ global MACHINES
- machiner_name = "plugins.machiners.%s" % config.machiner
+ name = "plugins.machinemanagers.%s" % self.config.machine_manager
try:
- machiner = __import__(machiner_name,
- globals(),
- locals(),
- ["Machiner"],
- -1)
+ __import__(name, globals(), locals(), ["dummy"], -1)
except ImportError as e:
- sys.exit("Unable to import machiner plugin: %s" % e)
+ sys.exit("Unable to import machime manager plugin: %s" % e)
- m = machiner.Machiner()
- m.prepare()
+ MachineManager()
+ module = MachineManager.__subclasses__()[0]
+ self.manager = module()
+ self.manager.initialize()
- if len(m.machines) == 0:
+ if len(self.manager.machines) == 0:
sys.exit("No machines")
else:
- print "Loaded %s machine/s" % len(m.machines)
+ MACHINES = self.manager.machines
+ print "Loader %s machine/s" % len(self.manager.machines)
+
+ def stop(self):
+ self.running = False
+
+ def start(self):
+ self.initialize()
while self.running:
- task = db.get_task()
+ time.sleep(1)
+ task = self.db.fetch()
if not task:
print "No pending tasks"
- time.sleep(1)
continue
+
+ analysis = AnalysisManager(task)
+ analysis.start()
Oops, something went wrong.

0 comments on commit b4e87ea

Please sign in to comment.