NefMoto

Technical => Reverse Engineering => Topic started by: prj on August 26, 2022, 02:29:31 AM



Title: BCB XOR key breaker (Python)
Post by: prj on August 26, 2022, 02:29:31 AM
Code:
def stripBCBHead(imgxor):
return imgxor[imgxor.index(b"\x1A\x01") + 2:]

def freqtable(data, klen):
freqtable = {}
for x in range (0, klen):
curfreq = {}
for y in range (0, 256):
curfreq[y] = 0
freqtable[x] = curfreq

for nr in range(len(data)):
freqtable[nr % klen][data[nr]] += 1

return freqtable

def findXORkeyfreq(bcbdata, byte, confidence, maxlen):
data = stripBCBHead(bcbdata)
keyFound = False
key = bytearray()

for curlen in reversed(range(4, maxlen+1)):
fqtable = freqtable(data, curlen)
key = bytearray(curlen)

avgconf1 = 0
for fqpos in sorted(fqtable):
sortedlist = sorted(fqtable[fqpos].items(), key=lambda x: x[1], reverse=True)
avgconf1 += 100 - sortedlist[1][1]/sortedlist[0][1]*100
key[fqpos] = sortedlist[0][0] ^ byte

avgconf1 = avgconf1/curlen

if (avgconf1 >= confidence):
key = deleterepeat(key)
return key
return b""