Exemplo n.º 1
0
 def decode_vault_size(self, lhs, cf):
     assert not lhs.startswith('L')
     assert lhs in self.G, "lhs={} must be in G. I cannot find it in\nG.keys()={}" \
         .format(lhs, list(self.G.keys()))
     cf %= self.G[lhs]['__total__']
     if cf == 0:
         print_err("Grammar of size 0!!!!\nI don't think the decryption will "
                   "be right after this. I am sorry. Argument: (lhs: {}, cf: {})".format(lhs, cf))
     i = getIndex(cf, list(self.G[lhs].values()))
     return i + 1
Exemplo n.º 2
0
 def decode_vault_size(self, lhs, cf):
     assert not lhs.startswith('L')
     assert lhs in self.G, "lhs={} must be in G. I cannot find it in\nG.keys()={}"\
         .format(lhs, self.G.keys())
     cf %= self.G[lhs]['__total__']
     if cf == 0:
         print_err("Grammar of size 0!!!!\nI don't think the decryption will "
                   "be right after this. I am sorry. Argument: (lhs: {}, cf: {})".format(lhs, cf))
     i = getIndex(cf, self.G[lhs].values())
     return i+1
Exemplo n.º 3
0
    def load(self, filename):
        self.G = json.load(open_(filename), object_pairs_hook=OrderedDict)
        for k, v in list(self.G.items()):
            if self.cal_cdf:
                print_err("Calculating CDF!")
                lf = 0
                for l, f in list(v.items()):
                    v[l] += lf
                    lf += f
                v['__total__'] = lf
            else:
                v['__total__'] = sum(v.values())

        # Create dawg/trie of the Wlist items for fast retrieval
        Wlist = [
            x for k, v in list(self.G.items()) for x in v if k.startswith('W')
        ]
        self.date = Date()
        self.Wdawg = IntDAWG(Wlist)
Exemplo n.º 4
0
    def load(self, filename):
        self.G = json.load(open_(filename),
                           object_pairs_hook=OrderedDict)
        for k, v in list(self.G.items()):
            if self.cal_cdf:
                print_err("Calculating CDF!")
                lf = 0
                for l, f in list(v.items()):
                    v[l] += lf
                    lf += f
                v['__total__'] = lf
            else:
                v['__total__'] = sum(v.values())

        # Create dawg/trie of the Wlist items for fast retrieval
        Wlist = [x
                 for k, v in list(self.G.items())
                 for x in v
                 if k.startswith('W')]
        self.date = Date()
        self.Wdawg = IntDAWG(Wlist)
Exemplo n.º 5
0
    def add_password(self, domain_pw_map):
        #print self.dte.G
        nG = copy.deepcopy(self.dte.G)
        print_production("Updating the grammar with new passwords..")
        nG.update_grammar(*(domain_pw_map.values()))
        ndte = DTE(nG)

        # TODO: fix this, currently its a hack to way around my shitty
        # parsing. A password can be generated in a different way than it is parsed in most probably
        # way. The code is supposed to pick one parse tree at random. Currently picking the most 
        # probable one. Need to fix for security reason. Will add a ticket. 
        new_encoding_of_old_pw = [] 

        current_passwords = ['' for _ in xrange(hny_config.HONEY_VAULT_STORAGE_SIZE)]

        if self.dte and (ndte != self.dte):
            # if new dte is different then copy the existing human chosen passwords. 
            # Machine generated passwords are not necessary to reencode. As their grammar
            # does not change. NEED TO CHECK SECURITY.
            print_production("Some new rules found, so adding them to the new grammar. "\
                             "Should not take too long...\n")
            data = [(self.dte, ndte, i, p)
                        for i,p in enumerate(self.S)
                        if self.machine_pass_set[i] == '0']
            result = ProcessParallel(copy_from_old_parallel, data, func_load=100)

            for i,pw, pw_encodings in result:
                if isinstance(pw_encodings, basestring):
                    new_encoding_of_old_pw.append((i, pw_encodings))
                current_passwords[i] = pw
                self.S[i] = pw_encodings

            # print_err(self.H[:10])
            # G_ = self.pcfg.decode_grammar(self.H)
            # print_err("-"*50)
            # print_err("Original: ", nG, '\n', '='*50)
            # print_err("After Decoding:", G_)
            # assert G_ == nG

        print_production("\nAdding new passowrds..\n")
        for domain, pw in domain_pw_map.items():
            i = self.get_domain_index(domain)
            print ">>", i, pw, domain
            current_passwords[i] = pw
            self.S[i] = ndte.encode_pw(pw)
            self.machine_pass_set[i] = '0'

        # Cleaning the mess because of missed passwords
        # Because some password might have failed in copy_from_old_function, They need to be
        # re-encoded 
        if new_encoding_of_old_pw:
            print_err("\n<<<<<<\nFixing Mess!!\n{}>>>>>>>".format(new_encoding_of_old_pw))
            nG.update_grammar(*[p for i,p in new_encoding_of_old_pw])
            for i,p in new_encoding_of_old_pw:
                self.S[i] = ndte.encode_pw(p)
                self.machine_pass_set[i] = '0'
        
        if hny_config.DEBUG:
            for i,pw_encodings in enumerate(self.S):
                if self.machine_pass_set[i] == '0':
                    tpw = ndte.decode_pw(pw_encodings)
                    tpwold = current_passwords[i]
                    assert len(tpwold)<=0 or tpw == tpwold,\
                        "The re-encoding is faulty. Expecting: '{}' at {}. Got '{}'.".format(tpwold, i, tpw)
                
        self.H = self.pcfg.encode_grammar(nG)
        self.dte = ndte