hlextend icon indicating copy to clipboard operation
hlextend copied to clipboard

Am I misunderstanding how to use your library?

Open S3j5b0 opened this issue 2 years ago • 1 comments

Hi, I tried to use your small example in order to create a proof of concept for myself of how an attack would work.

So I made this small script:

import hlextend
import hashlib


def hash(message, secret):
    payload = secret + message
    hash_object = hashlib.sha1(bytes(payload,"utf-8"))
    return hash_object.hexdigest()


message = "hello"
secret = "secret1234"

### First, we create our own hash
print(hash(message, secret)) 

# 52e98441017043eee154a6d1af98c5e0efab055c


## We then try to make a forged hash, using length extension, where we try to append the word "file"
sha = hlextend.new('sha1')
forged = sha.extend("file", message, 10,'52e98441017043eee154a6d1af98c5e0efab055c')
print(forged)
print(sha.hexdigest())


## and now we check if our forged signature makes is correct:

print(hash(forged,secret))

But this does not work, currently, the last line is 876d8406a52c71321930e99ac44740ccaea3d080, while it should be c60fa7de0860d4048a3bfb36b70299a95e6587c9, as described in your readme Have I misunderstoof something about how your libary works?

S3j5b0 avatar Jan 22 '23 16:01 S3j5b0

This module was originally written for Python 2 many years ago and has some issues running correctly in Python 3. There is a PR here that fixes these issues, and Im just waiting for one minor modification to it to be made before I merge.

Either wait for that PR to be merged or clone from the fork and then modify your test script as follows to use byte values as input and it should work as expected.

import hlextend
import hashlib

def hash(message, secret):
    payload = secret + message
    hash_object = hashlib.sha1(payload)
    return hash_object.hexdigest()


message = b"hello"
secret = b"secret1234"

### First, we create our own hash
print(hash(message, secret))

# 52e98441017043eee154a6d1af98c5e0efab055c


## We then try to make a forged hash, using length extension, where we try to append the word "file"
sha = hlextend.new('sha1')
forged = sha.extend(b"file", message, 10,'52e98441017043eee154a6d1af98c5e0efab055c')
print(forged)
print(sha.hexdigest())


## and now we check if our forged signature makes is correct:

print(hash(forged,secret))

Output:

$ python test.py
52e98441017043eee154a6d1af98c5e0efab055c
b'hello\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00xfile'
c60fa7de0860d4048a3bfb36b70299a95e6587c9
c60fa7de0860d4048a3bfb36b70299a95e6587c9

stephenbradshaw avatar Jan 28 '23 05:01 stephenbradshaw