# Path: cn_log.py
# def log(msg):
#   print("[%s %d:%d] %s" % (time.asctime(), os.getpid(), _thread.get_ident(), msg))
#   sys.stdout.flush()
# 
# Path: cn_db.py
# def init_web_db(cfg_file = "config.cfg"):
#   parser = configparser.SafeConfigParser()
#   parser.optionxform = str
#   cn_db_file = os.getenv("CN_DB")
#   if cn_db_file is not None:
#     #print "*** USING DATABASE", cn_db_file
#     cfg_file = cn_db_file
# 
#   if os.path.exists(cfg_file):
#     parser.read(cfg_file)
#   else:
#     tmp = os.path.dirname(os.path.realpath(__file__))
#     parser.read(os.path.join(tmp, cfg_file))
# 
#   section = 'database'
#   if 'database' not in parser.sections():
#     raise Exception("No database section in " + cfg_file)
# 
#   dbn = parser.get(section, 'dbn')
# 
#   if dbn == "mysql":
#     db = parser.get(section, 'db')
#     user = parser.get(section, 'user')
#     pw = parser.get(section, 'pw')
#     host = parser.get(section, 'host')
#     db = web.database(dbn='mysql', db=db, user=user, pw=pw, host=host)
#     
#     db.query('SET NAMES utf8;')
#     db.query('SET CHARACTER SET utf8;')
#     db.query('SET character_set_connection=utf8;')
#   elif dbn == "sqlite":
#     dbname = parser.get(section, 'db')
#     db = web.database(dbn='sqlite', db=dbname)
#     
#     # We need to mimic some MySQL functions in order to be able to use
#     # SQLite or use different SQL commands for each database server. I
#     # prefer the 1st option, naturally...
#     db._db_cursor().connection.create_function("concat", 2, sqlite_concat)
#     db._db_cursor().connection.create_function("conv", 3, sqlite_conv)
#     db._db_cursor().connection.create_function("instr", 2, sqlite_instr)
#     db._db_cursor().connection.create_function("rand", 0, sqlite_rand)
# 
#   return db
# 
# Path: cosa_nostra.py
# def open_db():
#   global g_db
#   
#   if g_db is not None:
#     return g_db
# 
#   db = init_web_db()
#   if not 'schema' in session or session.schema is None:
#     dbn = get_dbn()
#     if dbn == "mysql":
#       create_schema_mysql(db)
#     else: # Assumed to be SQLite...
#       create_schema_sqlite(db)
#     session.schema = True
# 
#   g_db = db
#   return db


import os
import sys
import time
import sqlite3
  import pyclamd
from hashlib import sha1
from pyew_core import CPyew
from cn_log import log
from cn_db import init_web_db
from cosa_nostra import open_db
    # Calculate prime numbers
    self.primes_table = primes(16384*4)
    self.db = open_db()
    self.db.printing = False

    self.clamd = None
    if pyclamd is not None:
      self.clamd = pyclamd.ClamdAgnostic()
      self.clamd.ping()

  def file_exists(self, sha1_hash):
    what = "1"
    args = {"sha1":sha1_hash}
    where = "hash = $sha1"
    ret = self.db.select("samples", args, what=what, where=where)
    rows = list(ret)
    return len(rows) > 0

  def get_description(self, buf):
    if self.clamd is None:
      return None

    ret = self.clamd.scan_stream(buf)
    if ret is None:
      return None

    # Answer format is in the following form:
    # >>> cd.scan_stream(buf)
    # >>> {u'stream': ('FOUND', 'Win.Trojan.Miniduke-3')}
    ret = ret["stream"][1]

# Path: cn_log.py
# def log(msg):
#   print("[%s %d:%d] %s" % (time.asctime(), os.getpid(), _thread.get_ident(), msg))
#   sys.stdout.flush()
# 
# Path: cn_db.py
# def init_web_db(cfg_file = "config.cfg"):
#   parser = configparser.SafeConfigParser()
#   parser.optionxform = str
#   cn_db_file = os.getenv("CN_DB")
#   if cn_db_file is not None:
#     #print "*** USING DATABASE", cn_db_file
#     cfg_file = cn_db_file
# 
#   if os.path.exists(cfg_file):
#     parser.read(cfg_file)
#   else:
#     tmp = os.path.dirname(os.path.realpath(__file__))
#     parser.read(os.path.join(tmp, cfg_file))
# 
#   section = 'database'
#   if 'database' not in parser.sections():
#     raise Exception("No database section in " + cfg_file)
# 
#   dbn = parser.get(section, 'dbn')
# 
#   if dbn == "mysql":
#     db = parser.get(section, 'db')
#     user = parser.get(section, 'user')
#     pw = parser.get(section, 'pw')
#     host = parser.get(section, 'host')
#     db = web.database(dbn='mysql', db=db, user=user, pw=pw, host=host)
#     
#     db.query('SET NAMES utf8;')
#     db.query('SET CHARACTER SET utf8;')
#     db.query('SET character_set_connection=utf8;')
#   elif dbn == "sqlite":
#     dbname = parser.get(section, 'db')
#     db = web.database(dbn='sqlite', db=dbname)
#     
#     # We need to mimic some MySQL functions in order to be able to use
#     # SQLite or use different SQL commands for each database server. I
#     # prefer the 1st option, naturally...
#     db._db_cursor().connection.create_function("concat", 2, sqlite_concat)
#     db._db_cursor().connection.create_function("conv", 3, sqlite_conv)
#     db._db_cursor().connection.create_function("instr", 2, sqlite_instr)
#     db._db_cursor().connection.create_function("rand", 0, sqlite_rand)
# 
#   return db
# 
# Path: cosa_nostra.py
# def open_db():
#   global g_db
#   
#   if g_db is not None:
#     return g_db
# 
#   db = init_web_db()
#   if not 'schema' in session or session.schema is None:
#     dbn = get_dbn()
#     if dbn == "mysql":
#       create_schema_mysql(db)
#     else: # Assumed to be SQLite...
#       create_schema_sqlite(db)
#     session.schema = True
# 
#   g_db = db
#   return db


# Path: pyew_batch.py
import os
import sys
import time
import sqlite3
  import pyclamd
from hashlib import sha1
from pyew_core import CPyew
from cn_log import log
from cn_db import init_web_db
from cosa_nostra import open_db

    # Calculate prime numbers
    self.primes_table = primes(16384*4)
    self.db = open_db()
    self.db.printing = False

    self.clamd = None
    if pyclamd is not None:
      self.clamd = pyclamd.ClamdAgnostic()
      self.clamd.ping()

  def file_exists(self, sha1_hash):
    what = "1"
    args = {"sha1":sha1_hash}
    where = "hash = $sha1"
    ret = self.db.select("samples", args, what=what, where=where)
    rows = list(ret)
    return len(rows) > 0

  def get_description(self, buf):
    if self.clamd is None:
      return None

    ret = self.clamd.scan_stream(buf)
    if ret is None:
      return None

    # Answer format is in the following form:
    # >>> cd.scan_stream(buf)
    # >>> {u'stream': ('FOUND', 'Win.Trojan.Miniduke-3')}
    ret = ret["stream"][1]

    log("Found malware name %s" % repr(ret))