我正在演示cookie安全性,其中一些重要的数据连同HMAC- the 256摘要一起传递给客户端,以防止数据的修改。我想要证明的是,使用弱键可以很容易地破解这个问题。
要生成哈希和数据,下面是一个简单的python脚本:
import hmac
from hashlib import sha256
message = "This is a long message that might be a little too long"
mkey = "password"
digest = hmac.new(mkey, message, sha256)
print(digest.hexdigest())
print(message.encode('hex'))
它输出这些数据:
e3762ae7f3bc6c9f8906e5f1f2cd19e80d3ebd281bdd31119a66adaef33d3b3c
546869732069732061206c6f6e67206d6573736167652074686174206d696768742062652061206c6974746c6520746f6f206c6f6e67
有什么工具能帮我有效破解钥匙吗?我在哈希猫找不到办法去做。
我可以使用我自己的字典攻击,但如果它存在的话,我宁愿使用现成的东西。
发布于 2017-02-05 22:54:30
事实证明,开膛手约翰可以破解HMAC摘要(至少从出血分支构建),这是很棒的。我只是看得不够努力。
发布于 2017-02-05 00:01:23
如果您想保护消息,那么您就有了一个严重的问题,因为它可以很容易地用以下方法解码:
crypto = "546869732069732061206c6f6e67206d6573736167652074686174206d696768742062652061206c6974746c6520746f6f206c6f6e67"
print crypto
print crypto.decode('hex')
但是,由于您要求通过字典攻击检索密码,我编写了一个脚本,将十六进制解码为明文,然后在HMAC中用作salt,并在字典和SHA256中生成密码,生成哈希(模仿原始HMAC函数),然后将其与目标哈希字符串进行比较:
import hmac
from hashlib import sha256
hashd = 'e3762ae7f3bc6c9f8906e5f1f2cd19e80d3ebd281bdd31119a66adaef33d3b3c'
crypto = '546869732069732061206c6f6e67206d6573736167652074686174206d696768742062652061206c6974746c6520746f6f206c6f6e67'
decrypto = crypto.decode('hex')
keys = open('dictionary.txt').read().split() # Specifiy the path to the dictionary file
def mykey():
for i in keys:
digest=hmac.new(i, decrypto, sha256)
digest.hexdigest()
if digest.hexdigest()==hashd:
print 'password:', i
break
if mykey():
print mykey()
所以如果密码在字典里,它就会打印出来。在python 2.7中进行了测试。还请注意,这些单词必须是字典中的一行词才能正常工作。
编辑:这里有一个更华而不实的版本,以防您在演示时需要它:
import hmac
from hashlib import sha256
from datetime import datetime
startTime = datetime.now()
hashd = raw_input('SHA256: ')
crypto = raw_input('HEX: ')
decrypto = crypto.decode('hex')
keys = open('dictionary').read().split() # Specifiy the path to the dictionary file
def mykey():
for i in keys:
digest=hmac.new(i, decrypto, sha256)
print digest.hexdigest(), '-', i
if digest.hexdigest()==hashd:
print 'Password found:', i
break
else:
print 'Password not in the dictionary.'
if mykey():
print mykey()
print 'Time Elapsed:', datetime.now() - startTime
https://security.stackexchange.com/questions/150388
复制相似问题