''' Author: Alexander Hanel Date: 20160729 Purpose: Searches for ngrams in a file and then uses those as an XOR key. '''importsysfromcollectionsimportCounterfromitertoolsimportcyclefromitertoolsimportproductdefxor_mb(message,key):'''Multi-byte XOR of a string message and string key'''return''.join(chr(ord(m_byte)^ord(k_byte))form_byte,k_byteinzip(message,cycle(key)))defrun():data=open(sys.argv[1],'rb').read()c=0# source http://stackoverflow.com/a/25071991forninrange(1,len(data)):substr_counter=Counter(data[i:i+n]foriinrange(len(data)-n))key,count=substr_counter.most_common(1)[0]ifcount==1:breaktemp=xor_mb(data,key)if"program"intemp:print"Length: %s, Count: %s Key: %s"%(len(key),count,key)f=open(str(c)+".bin","wb")fk=open("key-"+str(c)+".bin","wb")f.write(temp)fk.write(key)f.close()fk.close()c+=1run()
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.