暴力字典密码破解之crypt

使用crypt加密密码的暴力破解

在本文中,我们假设已经获取到用户密码的密文,从密文格式知道密码是通过crypt算法加密。下面我们尝试通过字典中的单词进行破解。

准备工作

先准备好我们获取到的密文文件。假设文件名为passwords.txt,文件内容如下:

victim: HX9LLTdc/jiDE: 503:100:Iama Victim:/home/victim:/bin/sh
root: DFNFxgW7C05fo: 504:100: Markus Hess:/root:/bin/bash

另外准备我们的字典文件。假设文件名为dictionary.txt,作为演示,假设字典中只有三个单词,如下所示:

get
apple
egg

实际可以在网上下载字典文件,包含字典中所有的单词。

总体思想

crypt算法是一种加密算法,在获取到密文到条件下,我们将字典中的单词作为明文使用crypt算法加密,将加密得到的密文和我们获取到的密文对比,如果一致,则密码得到破解。
python中已有自带到crypt库。要计算一个明文的密文,只需调用crypt.crypt(),并将明文和salt作为参数传递给它。

代码示例

使用python实现密码暴力破解的简单示例代码如下:
以下代码使用python2.*运行。

import crypt
def testPass(cryptPass):
    salt = cryptPass[0:2]
    dictFile = open('dictionary.txt','r')
    for word in dictFile.readlines():
        word = word.strip('\n')
        cryptWord = crypt.crypt(word,salt)
        if(cryptWord == cryptPass):
            print "[+] Found Password: "+word+"\n"
            return
    print "[-] Password Not Found.\n"
    return
def main():
    passFile = open('passwords.txt')
    for line in passFile.readlines():
        if ":" in line:
            user = line.split(':')[0]
            cryptPass = line.split(':')[1].strip(' ')
            print "[*] Cracking Password For: " + user
            testPass(cryptPass)
if __name__ == "__main__":
    main()
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
暴力破解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。注意,这是一种非常低效的方法,因为它需要尝试所有可能的密码。在现实世界中,密码长度和复杂度通常足以使暴力破解成为不切实际的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值