ctf md5爆破
1.知道组成的字符为数字,然后知道加密后的MD5,求组成的字符
import hashlibimport stringdef crackMd5(dst):dst = dst.lower()for a in range(0,10):for b in range(0,10):for c in range(0,10):for d in range(0,10):word = str(a) + str(b) + str(c) + str(d) + "_heetian"tmp = hashlib.md5(word).hexdigest()if dst == tmp:return wordreturn Noneif __name__ == "__main__":raw_input(crackMd5("158e64cfc9991940700acc5dc0f310e3"))
2.知道字符长度为0-6,然后给出字符集,用暴力的办法解出该MD5字符
# -*- coding:utf-8 -*-
__author__ = 'Administrator'
#from ultrapower.fd
import itertools as its
import md5#暴力破解
def uncipher(maxlenth,salt,ciphertext_s,str_letter):ciphertext_s=ciphertext_ssalt = saltmaxlenth=int(maxlenth)str_letter=str_letterciphertext=''for i in range(1, maxlenth+1):# 迭代生成明文(例如abc,repeat=2 结果为(a,a)(a,b)(a,c)(b,b)(b,a)(b,c)(c,c)(c,a)(c,b)r = its.product(str_letter, repeat=i)for j in r:plaintext = "".join(j) #连接成字符串plaintext = "%s%s" % (plaintext, salt) #把盐加到明文的后面 每次生成的最终明文#print plaintext #打印明文# 开始解密,方法是,每个明文进来,加密成密文,然后密文与密文做对比md501 = md5.new()md501.update(plaintext)ciphertext = md501.hexdigest()# 对比密文确认明文if ciphertext == ciphertext_s: #如果密文一致 退出2层循环breakif ciphertext == ciphertext_s: #如果密文一致,退出1层循环,打印结果print "task finished(plain,cipher)"print "%s:%s" % (plaintext, ciphertext) #打印结果break#开始执行主函数
#str_letter="abcdefghijklmnopqrstuvwxyz0123456789"
#str_letter="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
#str_letter="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
str_letter="abcdefghijklmnopqrstuvwxyz" #明文的字符范围
maxlenth=6 #明文的最大长度,一般不会超过6位,否则短时间很难暴力破解
salt='5948' #加盐 #如果不加盐,为空就是正常的md5解密
ciphertext_s='81bdf501ef206ae7d3b92070196f7e98' #密文
uncipher(maxlenth,salt,ciphertext_s,str_letter) #开始解密
3.
给md5编码,明文缺省 ,求明文
已知一段md5密文
fa2cdddd07f211664697797a54de08ea
已知线索 明文为: flag{Th?s A Pen},求明文
import hashlibfor i in range(32, 127): # 使用for循环逐个尝试所有的字母,chr(i)返回值是当前整数对应的 ASCII 字符。m = hashlib.md5() # 获取一个md5加密算法对象m.update(str('flag{Th' + chr(i) + 's A Pen}').encode('utf-8')) # 指定需要加密的字符串des = m.hexdigest() # 进行md5加密if des == 'fa2cdddd07f211664697797a54de08ea': # 如果得到的密文和我们预期的密文相同,输出print('flag{Th' + chr(i) + 's A Pen}')
运行结果为
2. 根据部分密文和明文得到md5加密的完整明文
这里有一段丢失的md5密文
7257f353cab82f3087cb2113533???
要求你还原出他的明文
已知线索 明文为: flag{HELLO WOR??}
import hashlibfor i in range(32, 127):for j in range(32, 127):m = hashlib.md5() # 获取一个md5加密算法对象m.update(str('flag{HELLO WOR' + chr(i) + chr(j) + '}').encode("utf-8")) # 指定需要加密的字符串des = m.hexdigest()if '7257f353cab82f3087cb2113533' in des: # 如果得到的密文和我们预期的密文相同,输出print(des)
运行结果为
3.
这里有一段丢失的sha1密文
89aa???0bfd7da1409???c8c76e0461aaf9???
要求你还原出他的原值和hash值
已知线索 明文为: fl??{HELLO ?ORLD}
# @Time : 2022/3/11 11:07
# @Author : 南黎
# @FileName: 3.py
import hashlibfor i in range(32, 127):for j in range(32, 127):for k in range(32, 127):m = hashlib.sha1() # 获取一个md5加密算法对象m.update(('fl' + chr(i) + chr(j) + '{HELLO ' + chr(k) + 'ORLD}').encode("utf8")) # 指定需要加密的字符串des = m.hexdigest()if "89aa" in des and "0bfd7da1409" in des and "c8c76e0461aaf9" in des: # 如果得到的密文和我们预期的密文有相同的字符串子串,输出print(des)print('fl' + chr(i) + chr(j) + '{HELLO ' + chr(k) + 'ORLD}')m = hashlib.sha1()
运行结果为
还可以改良一下代码
if "89aa" in des and "0bfd7da1409" in des and "c8c76e0461aaf9" in des: # 如果得到的密文和我们预期的密文有相同的字符串子串,输出print(des)
相关文章链接:https://blog.csdn.net/u011027547/article/details/123419072