コード例 #1
0
    def create_enums(self, d):
        kfh = CKoretFuzzyHashing()
        kfh.bsize = 1
        kfh.output_size = 8

        fuzzy_hashes = {}
        for key in d.keys():
            hash1, hash2, _ = kfh.hash_bytes(key).split(";")
            new_key = "%s-%s" % (hash1, hash2)
            if new_key in fuzzy_hashes:
                fuzzy_hashes[new_key].append(key)
            else:
                fuzzy_hashes[new_key] = [key]

        enums = {}
        enums[DEFAULT_ENUM] = []
        for key in fuzzy_hashes:
            l = fuzzy_hashes[key]
            if len(l) == 1:
                continue

            enum_name = self.get_enum_name(l)
            enums[enum_name] = []
            tmp = []
            for element in l:
                tmp.append("  %s = %s, " % (element, str(d[element])))

            tmp.sort()
            tmp.insert(0, "enum %s {" % enum_name)
            tmp.append("};")
            enums[enum_name] = "\n".join(tmp)

        return enums
コード例 #2
0
  def create_enums(self, d):
    kfh = CKoretFuzzyHashing()
    kfh.bsize = 1
    kfh.output_size = 8

    fuzzy_hashes = {}
    for key in d.keys():
      hash1, hash2, _ = kfh.hash_bytes(key).split(";")
      new_key = "%s-%s" % (hash1, hash2)
      if new_key in fuzzy_hashes:
        fuzzy_hashes[new_key].append(key)
      else:
        fuzzy_hashes[new_key] = [key]

    enums = {}
    enums[DEFAULT_ENUM] = []
    for key in fuzzy_hashes:
      l = fuzzy_hashes[key]
      if len(l) == 1:
        continue

      enum_name = self.get_enum_name(l)
      enums[enum_name] = []
      tmp = []
      for element in l:
        value = None
        if type(d[element]) is decimal.Decimal:
          eng_str = d[element].to_eng_string()
          if str(eng_str).find(".") == -1:
            value = "0x%08x" % long(eng_str)

        if value is None:
          value = str(d[element])
        tmp.append("  %s = %s, " % (element, value))

      tmp.sort()
      tmp.insert(0, "enum %s {" % enum_name)
      tmp.append("};")
      enums[enum_name] = "\n".join(tmp)

    return enums
コード例 #3
0
def find_original_file(db, id):
  
  vars = {"id":id}
  where = "sample_id = $id"
  res = db.select("samples", what="sample_hash", where=where, vars=vars)
  res = list(res)
  if len(res) == 0:
    raise Exception("Invalid crash identifier")
  sample_hash = res[0].sample_hash

  res = db.select("config", what="value", where="name='SAMPLES_PATH'")
  res = list(res)
  if len(res) == 0:
    raise Exception("Invalid configuration value for 'SAMPLES_PATH'")

  path = os.path.join(res[0].value, "crashes")
  path = os.path.join(path, sample_hash)
  if not os.path.exists(path):
    raise Exception("Crash sample does not exists! %s" % path)

  magic = open(path, "rb").read(3)
  if magic == "PK\x03":
    z = ZipFile(path, "r")
    cmt = z.comment
    z.close()
    if cmt == "NIGHTMARE":
      raise Exception("Cannot find the original sample for ZIP archives created by Nightmare, sorry.")

  res = db.select("config", what="value", where="name = 'TEMPLATES_PATH'")
  res = list(res)
  if len(res) == 0:
    raise Exception("Invalid configuration value for 'TEMPLATES_PATH'")
  templates_path = res[0].value

  sql = """select p.subfolder subfolder
             from projects p,
                  crashes c
            where c.sample_id = $id
              and p.project_id = c.project_id"""
  vars = {"id":id}
  res = db.query(sql, vars=vars)
  res = list(res)
  if len(res) == 0:
    raise Exception("Cannot find the project associated to the crash identifier")

  project_path = os.path.join(templates_path, res[0].subfolder)
  if not os.path.exists(project_path):
    raise Exception("Cannot find path '%s'" % project_path)

  kfh = CKoretFuzzyHashing()
  kfh.bsize = 16
  h1, h2, h3 = kfh.hash_file(path).split(";")

  original_file = None
  for f in os.listdir(project_path):
    filename = os.path.join(project_path, f)
    if not os.path.isfile(filename):
      continue

    tmp1, tmp2, tmp3 = kfh.hash_file(filename).split(";")
    if h1 == tmp1 and h2 == tmp2 and h3 == tmp3:
      original_file = filename
      break
    elif h1 == tmp1 or h2 == tmp2 or h3 == tmp3:
      original_file = filename
      break

  return original_file, path