C++Brute Force攻击函数不会返回结果

C++ Brute Force attack function does not return results

本文关键字:返回 结果 函数 Force 攻击 C++Brute      更新时间:2023-10-16

所以我目前正在C++中开发一个暴力攻击者项目。我已经设法让它工作了,但我面临的一个问题是,如果程序真的得到了正确的猜测,函数仍然会继续。我认为问题是程序无法返回猜测。看看我的代码:(很抱歉搞砸了,顺便说一句,我在C++方面没有经验——我以前用Python/JS编码。(

#include <iostream>
#include <cstdlib>
#include <string>
std::string chars = "abcdefghijklmnopqrstuvwxyz";
std::string iterateStr(std::string s, std::string guess, int pos);
std::string crack(std::string s);
std::string iterateChar(std::string s, std::string guess, int pos);
int main() {
crack("bb");
return EXIT_SUCCESS;
}
// this function iterates through the letters of the alphabet
std::string iterateChar(std::string s, std::string guess, int pos) {
for(int i = 0; i < chars.length(); i++) {
// sets the char to a certain letter from the chars variable
guess[pos] = chars[i];
// if the position reaches the end of the string
if(pos == s.length()) {
if(guess.compare(s) == 0) {
break;
} 
} else {
// else, recursively call the function
std::cout << guess << " : " << s << std::endl;
iterateChar(s, guess, pos+1);
}
}
return guess;
}
// this function iterates through the characters in the string
std::string iterateStr(std::string s, std::string guess, int pos) {
for(int i = 0; i < s.length(); i++) {
guess = iterateChar(s, guess, i);
if(s.compare(guess) == 0) {
return guess;
}
}
return guess;
}
std::string crack(std::string s) {
int len = s.length();
std::string newS(len, 'a');
std::string newGuess;
newGuess = iterateStr(s, newS, 0);
return newGuess;
}

编辑:已更新代码。

发布的代码中的主要缺陷是递归函数返回一个字符串(猜测的密码(,而没有向调用方明确指示找到了密码。

按值传递所有字符串也是一个潜在的效率问题,但OP应该担心这样的片段:

guess[pos] = chars[i];  // 'chars' contains the alphabet
if(pos == s.length()) {
if(guess.compare(s) == 0) {
break;
}
} 

其中,guesss是具有相同长度的字符串。如果长度为2(OP的最后一个例子(,则guess[2]在边界之外,但对guess.compare(s)的连续调用将只比较"内部"的两个字符。

iterateStr内部的循环也没有任何用处,并且pos参数未使用。

与其修复这种尝试,不如从头开始重写

#include <iostream>
#include <string>
#include <utility>
// Sets up the variable and start the brute force search
template <class Predicate>
auto crack(std::string const &src, size_t length, Predicate is_correct)
-> std::pair<bool, std::string>;
// Implements the brute force search in a single recursive function. It uses a
// lambda to check the password, instead of passing it directly
template <class Predicate>
bool recursive_search(std::string const &src, std::string &guess, size_t pos,
Predicate is_correct);
// Helper function, for testing purpouse
void test_cracker(std::string const &alphabet, std::string const &password);
int main()
{
test_cracker("abcdefghijklmnopqrstuvwxyz", "dance");
test_cracker("abcdefghijklmnopqrstuvwxyz ", "go on");
test_cracker("0123456789", "42");
test_cracker("0123456789", "one");     // <- 'Password not found.'
}
void test_cracker(std::string const &alphabet, std::string const &password)
{
auto [found, pwd] = crack(alphabet, password.length(),
[&password] (std::string const &guess) { return guess == password; });
std::cout << (found ? pwd : "Password not found.") << 'n';
}
// Brute force recursive search 
template <class Predicate>
bool recursive_search(std::string const &src, std::string &guess, size_t pos,
Predicate is_correct)
{
if ( pos + 1 == guess.size() )
{
for (auto const ch : src)
{
guess[pos] = ch;
if ( is_correct(guess) )
return true;
}     
}
else
{
for (auto const ch : src)
{
guess[pos] = ch;
if ( recursive_search(src, guess, pos + 1, is_correct) )
return true;
}              
}
return false;
}
template <class Predicate>
auto crack(std::string const &src, size_t length, Predicate is_correct)
-> std::pair<bool, std::string>
{
if ( src.empty() )
return { length == 0 && is_correct(src), src };
std::string guess(length, src[0]);
return { recursive_search(src, guess, 0, is_correct), guess };
}

我已经尝试过您的代码,即使是iterateStr()函数的修改版本。我用了abduct这个词,因为它搜索起来更快。在调试程序时,我注意到当找到匹配项时,iterateChar()函数没有返回。我还注意到,传入的string s的长度是6,但在每次迭代中更新的猜测字符串的长度为7。您可能需要逐步了解您的代码并检查它。

例如,在特定迭代中,s字符串包含:abduct,但guess字符串包含aaaabjz,然后在下一次迭代中guess字符串包含aaaabkz。这可能是您关心的问题,即为什么即使您认为找到了匹配项,循环或函数仍会继续。

这里的长度差异可能是罪魁祸首。

此外,当您逐步通过修改后的代码:

for ( size_t i = 0; i < s.length(); i++ ) {
guess = iterCh( s, guess, i );
std::cout << "in the iterStr loopn";
if ( guess.compare( s ) == 0 ) {
return guess;
}
}
return guess;

iterateStr()函数中,递归总是调用guess = iterCh( s, guess, i );,而代码从不打印in the iterStr loopn";。您的iterateChar函数是通过整个字符串或字符序列完成的,从未找到并返回匹配项。我甚至尝试了abs这个词,因为它更容易、更快地通过调试器,而且我得到了同样的结果。