2016-04-11 15 views
1

Ich bin ein Student, und ich kämpfe, um eine Längenausdehnung Angriff auf einem schlecht Implementierung Python HMAC-Code mit der Durchführung unterhmac Länge Angriff Verlängerung Python

ich der folgenden gefälschten aufgezeichneten Nachricht

581b464951404558504f071e7359100d00|Delete all files on c: and d: and e: 

gegeben habe und möchte etwas wie "und install Linux" oder etwas an das Ende des Codes anhängen.

megaBlockSize=128 
def strToNum(inp): 
    """Takes a sequence of bytes and makes a number""" 
    out=0 
    for i in inp: 
     out=out<<8 
     out^=ord(i) 
    return out 

def numToStr(inp): 
    """Take a number and make a sequence of bytes in a string""" 
    out="" 
    while inp!=0: 
     out=chr(inp & 255)+out 
     inp=inp>>8 
    return out 

def cueh_hash_2(inp, blockSize): 
    """ CUEH Hash Function v 2.0 
    Returns given-length hash of any string input or stringable input 
    Uses given number of bits, must be multiple of 8 
    """ 
    if blockSize%8!=0 or blockSize<=0: 
     raise Exception("Block size must be a multiple of 8") 
    inp=str(inp) #Make sure we have a string 
    while (len(inp)%(blockSize/8)!=0): 
     inp=" "+inp #Pad it if we need to 
    val=0<<blockSize #Our accumulator 
    for pos in range(0,len(inp),blockSize/8): #Now in blocks of the rigth size... 
     for pos2 in range(blockSize/8): 
      tval=ord(inp[pos2+pos]) 
      tval=tval<<(blockSize-pos2*8) 
      val=val^tval 
    return val 

def cueh_hmac_2(key, message, blockSize): 
    """Outputs a hash-based digest of the message and secret key combo""" 
    key=str(key) 
    message=str(message) 
    if len(key)>blockSize/8: 
     key=numToStr(cueh_hash_2(key,blockSize)) #Keys are shortened to blocksize 
    while len(key)<blockSize/8: 
     key+="#" #Keys are padded with spaces if they're too short 
    return cueh_hash_2(key+message,blockSize) 


def main(): 
    print "Testing hash: %06x"%cueh_hash_2("ABC\0\0\0",megaBlockSize) 

    #Examples of flipping between numbers and strings of bytes 
    #Just makes it easier to have "password" style keys 
    print "%x"%strToNum("ABC") 
    print numToStr((65<<16) + (66<<8) + 67) 

    #Now to see it in practice 
    secretKey="sswrodfishez" #This is known by both parties 
    authedMessage="ssdsdsdsdsd" 


    out=cueh_hmac_2(secretKey,authedMessage, megaBlockSize) 
    #Now we have the special verification code that can be used to 
    #prove we were the aithor of the message. Anyone else who knows 
    #the secret can do the same and compare the values 
    dispString="0x%%0%dx|%%s"%(megaBlockSize/16) 
    print dispString%(out, authedMessage) 

if __name__ == '__main__': 
    main()'' 

Ich habe viele Stunden damit verschwendet.

Jede Hilfe ist

geschätzt

Referenz md5 implementation of length extension attack

+0

* * - aber es scheint, „Ich habe viele Stunden auf diesen verschwendet.“ Du bist nirgendwohin gekommen. Wir sind nicht hier, um deine Hausaufgaben zu machen. Die 'cueh_hash_2' Funktion ist eine ziemlich schlechte Hash Funktion und die' cueh_hmac_2' ist keine echte HMAC. Es sollte nicht zu schwierig sein, einen Angriff zu machen, da es sich um eine einfache zeichenorientierte XOR handelt. –

Antwort

0

Es tut mir leid zu sagen, dass Ihre Frage bei weitem nicht klar. Sie scheinen ein Kryptografieproblem zu haben, aber Sie fragen nach dem Anhängen einer Zeichenfolge an das Ende einer anderen Zeichenfolge.

mit dem Risiko von Ihrer Notwendigkeit zu stark zu vereinfachen und wenn das der Fall ist, warum dann der folgende Code ist nicht genug für Sie:

str1 = "581b464951404558504f071e7359100d00|Delete all files on c: and d: and e: " 
str2 = "and install linux" 
str3 = str1 + str 2 
+0

Ich versuche, eine Längenerweiterung Angriff, wo ich str2 anhängen und erzeuge den gleichen Hash wie in str1 erwähnt – zLKidda

+0

Oh, dann ist es ein algorithmisches Problem und nicht Python verwandt. Deine Frage scheint eine Anfrage zum Debuggen zu sein. Nicht viele von ihnen werden, wenn überhaupt, beantwortet. Viel Glück. – lps