Windows的密码生成算法——NTLM算法破解

NTLM:CDABE1D16CE42A13B8A9982888F3E3BE

hint:密码长度不超过5,数字和符号组成

Windows下NTLM Hash生成原理:‍‍
  IBM设计的LM Hash算法存在几个弱点,微软在保持向后兼容性的同时提出了自己的挑战响应机制,NTLM Hash便应运而生。
  假设明文口令是”123456″:
  1、首先转换成Unicode字符串,与LM Hash算法不同,这次不需要添加0补足14字节,从ASCII串转换成Unicode串时,使用little-endian序,0x80之前的标准ASCII码转换成Unicode码,就是简单地从0x??变成 0×00??。此类标准ASCII串按little-endian序转换成Unicode串,就是简单地在原有每个字节之后添加0x00。
  2、对所获取的 Unicode串进行标准MD4单向哈希,无论数据源有多少字节,MD4固定产生128-bit的哈希值。
  与LM Hash算法相比,明文口令大小写敏感,无法根据NTLM Hash判断原始明文口令是否小于8字节,摆脱了魔术字符串”KGS!@#$%”。NTLM安全性的关键在于MD4是真正的单向哈希函数,穷举作为数据源出现的明文,难度较大。
  例如:
“123456”先转换成Unicode码“310032003300340035003600”,
然后“310032003300340035003600”进行标准MD4单向哈希就得到了最后的NTLM Hash:32ED87BDB5FDC5E9CBA88547376818D4。

加密的流程就是:
(1)把字符串转成Unicode编码串;
(2)把Unicode编码串用MD4单项哈希算法生成固定128比特的哈希值。

方法一、Python代码暴力破解

接下来,我们就根据加密流程来暴力破解,其中密码长度不超过5,数字和符号组成:

# py -3
# -*- coding: utf-8 -*-
# coding:utf-8

from Crypto.Hash import MD4
import string
import hashlib

# 数据字典:密码由数字和符号组成
dataDictionary = string.digits + string.punctuation
# 密码长度不超过5
for a in range(len(dataDictionary)):
    for b in range(len(dataDictionary)):
        for c in range(len(dataDictionary)):
            for d in range(len(dataDictionary)):
                for e in range(len(dataDictionary)):
                    password1 = dataDictionary[a]+"\0"+dataDictionary[b]+"\0"+dataDictionary[c]+"\0"+dataDictionary[d]+"\0"+dataDictionary[e]+"\0"
                    # print(password)
	       # 以指定的utf-8编码格式编码字符串,返回bytes 对象
                    password2 = password1.encode('utf-8')
                    # print(password)

                    # Crypto.Hash模块的MD4算法
                    # result = MD4.new()
                    # result.update(password2)
                    # if "CDABE1D16CE42A13B8A9982888F3E3BE" == str.upper(result.hexdigest()):
                    
                    # hashlib的md4算法,但是生成的明文都是小写,但是题目给的是大写,所以用str的upper函数把明文转化成大写
                    if 'CDABE1D16CE42A13B8A9982888F3E3BE' == str.upper(hashlib.new('md4',password2).hexdigest()):
                        print("CDABE1D16CE42A13B8A9982888F3E3BE对应的密码是: " + password1)
                        exit()

运行结果:
在这里插入图片描述
得到CDABE1D16CE42A13B8A9982888F3E3BE解密后的结果为:1+2=3。

方法二、hashcat工具破解

因为老师提示了密码长度不超过5,数字和符号组成,直接生成攻击字典:
在这里插入图片描述
在这里插入图片描述

在cmd下运行hashcat:hashcat32.exe -m 1000 -a 0 CDABE1D16CE42A13B8A9982888F3E3BE mutou.txt
在这里插入图片描述

28秒找到了结果:
在这里插入图片描述

打开hashcat.potfile查看结果,CDABE1D16CE42A13B8A9982888F3E3BE解密后的结果为:1+2=3。
在这里插入图片描述

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
暴力破解NTLM加密算法需要对于每个可能的密码进行哈希计算,并将其与目标哈希进行比较。C++可以使用Windows API中的Cryptographic Service Provider (CSP)来实现NTLM哈希计算。以下是一个使用CSP进行NTLM哈希计算的示例代码: ```c++ #include <windows.h> #include <wincrypt.h> #include <iostream> #include <string> bool CrackNTLMHash(const std::string& hash, const std::string& charset, int length, std::string& password) { // Open a handle to the Microsoft Base Cryptographic Provider HCRYPTPROV hProv; if (!CryptAcquireContext(&hProv, nullptr, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { std::cerr << "Failed to acquire cryptographic context\n"; return false; } // Convert the hex-encoded hash to binary std::string hashBytes; for (size_t i = 0; i < hash.length(); i += 2) { hashBytes += static_cast<char>(std::stoi(hash.substr(i, 2), nullptr, 16)); } // Allocate a buffer for the hash object HCRYPTHASH hHash; if (!CryptCreateHash(hProv, CALG_MD4, 0, 0, &hHash)) { std::cerr << "Failed to create hash object\n"; CryptReleaseContext(hProv, 0); return false; } // Iterate over all possible passwords std::string passwordHash; for (int i = 0; i < length; ++i) { std::string password(length, charset[0]); do { // Hash the password if (!CryptHashData(hHash, reinterpret_cast<const BYTE*>(password.data()), length, 0)) { std::cerr << "Failed to hash data\n"; CryptDestroyHash(hHash); CryptReleaseContext(hProv, 0); return false; } // Get the hash value DWORD hashLength = 16; BYTE hashValue[16]; if (!CryptGetHashParam(hHash, HP_HASHVAL, hashValue, &hashLength, 0)) { std::cerr << "Failed to get hash value\n"; CryptDestroyHash(hHash); CryptReleaseContext(hProv, 0); return false; } // Compare the hash value to the target hash passwordHash.assign(reinterpret_cast<const char*>(hashValue), hashLength); if (passwordHash == hashBytes) { password = password.substr(0, i) + password[i] + password.substr(i + 1); break; } // Reset the hash object if (!CryptDestroyHash(hHash) || !CryptCreateHash(hProv, CALG_MD4, 0, 0, &hHash)) { std::cerr << "Failed to reset hash object\n"; CryptReleaseContext(hProv, 0); return false; } } while (std::next_permutation(password.begin(), password.end(), [&charset](char a, char b) { return charset.find(a) < charset.find(b); })); } // Release resources CryptDestroyHash(hHash); CryptReleaseContext(hProv, 0); // Check if the password was found if (passwordHash == hashBytes) { password = password.substr(0, length); return true; } else { return false; } } ``` 该代码将目标哈希作为十六进制字符串传递,因此您需要将其从NTLM哈希格式转换为十六进制字符串。此外,您需要为字符集和密码长度指定值。代码将在字符集中迭代所有可能的密码,并使用Cryptographic Service Provider计算NTLM哈希。如果找到密码,则将其保存在传递的字符串参数中并返回true,否则返回false。注意,这是一种非常低效的方法,因为它需要尝试所有可能的密码。在现实世界中,密码长度和复杂度通常足以使暴力破解成为不切实际的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值