def test_keys(): needles = [ b'28f7b6ba10dbe3389ebf299bcbb0b289123a60aab853fa6c50fc1e87f6245e16a88de70f84c8b9d7fb0cd4f6facadc693f2990175329917f8d6066cb14699100', b'87bf74053a820258a3a1f5cc8976192773abd0e391201c5d055d0c4f2d1912d0b516aa1994811fef7ccc831f4694243a63a1710533fd0d504e20cbc838c23ab8', b'd90fabcbfe2f9cf7d4c280c13ec5412206289f20b7cbabe840ab8a03cce1bab07fa813c6a1a7608085c2a71b58d79dce7aae420e1d1f9bfcc8b2c5a2de8ed0ac', ] expected_summary = b'c96fde34472bc4fdda6ef224f6b57d8ec2d1d5e86868b616e2ca1771f726b969b92e133ccdf7c5b877047bf53c0af74fd3c9f0e0321c3fd18a43509464bdc042' summary = b'' with open(str(ROOT_DIR / 'keys.txt'), 'r') as infile: toks = ''.join((c if c in string.hexdigits else ' ') for c in infile.read()).split() for tok in toks: tok = tok.lower().encode() if tok.startswith(b'0x'): # Get rid of leading 0x tok = tok[2:] tok_hash = tohex(_iter_hash(tok)) if tok_hash in needles: needles.remove(tok_hash) summary += tok + tok_hash assert tohex(_iter_hash(summary)) == expected_summary, \ 'The correct encryption key(s) were not found in keys.txt ' + \ '(make sure it is a hex string!).'
def __repr__(self): return "<packet proto:%s, target:%s, site:%s, type:%s>" % ( str(tohex(pack(">H", self.proto)), encoding="utf-8"), str(tohex(self.target), encoding="utf-8"), str(tohex(self.site), encoding="utf-8"), self.payload.name, )
def decode(self, bs): if len(bs) != calcsize(self.pack_str): print 'could not decode %s' % (self.name, ) print tohex(bs) return data = unpack(self.pack_str, bs) self.data = dict( zip(self.pack_struct, data) )
def decode(self, bs): if len(bs) != calcsize(self.pack_str): print( 'could not decode %s' % (self.name, ) ) print( tohex(bs)) return data = unpack(self.pack_str, bs) self.data = dict( zip(self.pack_struct, data) )
def test_hasher(): with open(str(TEST_DIR / 'hr.csv'), 'r') as infile: reader = csv.reader(infile) cases = list(reader) for plaintext, hashed_hash in cases: cmd = ['./hasher', plaintext] assert (tohex(_iter_hash(_run(cmd, timeout=0.1))) == hashed_hash.encode()), \ "Your hash function is incorrect for plaintext '{}'".format( plaintext)
def getTorrentVariations(self, t): t = tohex(t) d = [] for f in os.listdir(self.dir_torrentcache): f = os.path.basename(f) if f[:len(t)] == t: try: garbage, ver = f.split('.') except: ver = '0' d.append(int(ver)) d.sort() return d
def dump(self,addr=0x0,length=None,width=16): addr*=width if length: length=addr+length*width else: length=None dmp=str(tohex(self.mem[addr:length]),'ascii') odmp=["Memory:\n"] odmp_p="" for addr,n in enumerate(dmp,(addr*2)+2*width): odmp_p+=n if len(odmp_p)==width*2: odmp_p="0x{:04x}: [{}]".format(1+addr//2-width*2,odmp_p) print(odmp_p) odmp_p="" return
def writeTorrentData(self, t, data): self.TorrentDataBuffer[t] = data try: f = open(os.path.join(self.dir_datacache, tohex(t)), 'wb') f.write(bencode(data)) success = True except: success = False try: f.close() except: pass if not success: self.deleteTorrentData(t) return success
def getTorrent(self, t, v=-1): t = tohex(t) if v == -1: v = max(self.getTorrentVariations(t)) # potential exception if v: t += '.' + str(v) try: f = open(os.path.join(self.dir_torrentcache, t), 'rb') r = bdecode(f.read()) except: r = None try: f.close() except: pass return r
def getTorrent(self, t, v = -1): t = tohex(t) if v == -1: v = max(self.getTorrentVariations(t)) # potential exception if v: t += '.'+str(v) try: f = open(os.path.join(self.dir_torrentcache, t), 'rb') r = bdecode(f.read()) except: r = None try: f.close() except: pass return r
def getTorrentData(self, t): if self.TorrentDataBuffer.has_key(t): return self.TorrentDataBuffer[t] t = os.path.join(self.dir_datacache, tohex(t)) if not os.path.exists(t): return None try: f = open(t, 'rb') r = bdecode(f.read()) except: r = None try: f.close() except: pass self.TorrentDataBuffer[t] = r return r
def writeTorrent(self, data, t, v = -1): t = tohex(t) if v == -1: try: v = max(self.getTorrentVariations(t))+1 except: v = 0 if v: t += '.'+str(v) try: f = open(os.path.join(self.dir_torrentcache, t), 'wb') f.write(bencode(data)) except: v = None try: f.close() except: pass return v
def writeTorrent(self, data, t, v=-1): t = tohex(t) if v == -1: try: v = max(self.getTorrentVariations(t)) + 1 except: v = 0 if v: t += '.' + str(v) try: f = open(os.path.join(self.dir_torrentcache, t), 'wb') f.write(bencode(data)) except: v = None try: f.close() except: pass return v
def _test_plaintext(filename, num_needed, require_root=True): master = _get_master() with open(str(ROOT_DIR / filename), 'r') as infile: answer = list(csv.DictReader(infile)) assert len(answer) >= num_needed, \ '{} needs at least {} username/password pairs.'.format( filename, num_needed) assert len(set(entry['username'] for entry in answer)) == len(answer), \ 'Usernames in {} are not unique.'.format(filename) if require_root: root_idx = next( (i for i, entry in enumerate(answer) if entry['username'] == 'root'), -1) assert root_idx > -1, \ '{} needs to have the root user/pass.'.format(filename) tmp = answer[0] answer[0] = answer[root_idx] answer[root_idx] = tmp answer = answer[:num_needed] for entry in answer: username, password = entry['username'].strip( ), entry['password'].strip() entry['username'], entry['password'] = username, password assert username in master, \ 'User {} does not exist in the database.'.format(username) hashed = _run(['./hasher', password], timeout=0.1) hashed_hash = tohex(_iter_hash(username.encode() + hashed)) assert hashed_hash == master[username].encode(), \ "'{}' is an incorrect password for user {}".format( password, username) return answer
def __repr__(self): return ('<packet proto:%s, target:%s, site:%s, type:%s>' % (str(tohex(pack('>H', self.proto)), encoding='utf-8'), str(tohex(self.target), encoding='utf-8'), str(tohex(self.site), encoding='utf-8'), self.payload.name))
def __str__(self): return('<packet proto:%s, target:%s, site:%s, type:%s>' % (unicode(tohex(pack('>H', self.proto)), encoding='utf-8'), unicode(tohex(self.target), encoding='utf-8'), unicode(tohex(self.site), encoding='utf-8'), self.payload.name ))
def deleteTorrentData(self, t): try: os.remove(os.path.join(self.dir_datacache, tohex(t))) except: pass
def tohexl(b): """Convert little endian bytecode to hex""" return tohex(b[::-1])
def getPieceDir(self, t): return os.path.join(self.dir_piececache, tohex(t))