Perl暴力攻击

时间:2015-03-09 00:11:55

标签: perl while-loop passwords brute-force

我在尝试创建暴力脚本时遇到了很多麻烦。我需要破解的密码是1到4个字符长,都是小写字母。我想我已经找到了生成所有可能组合的代码,但我不确定如何在文件上测试它。任何指导或提示都会很棒。

$password = "aaaa";
while ( length $password < 5 ) {
print "$password\n";
$password++;

2 个答案:

答案 0 :(得分:0)

我有类似的问题。你是我的班级或全国各地的脚本班同时做这个问题。我的教授鼓励论坛使用,但我们不能与我们大学的直接同学分享答案。

如果您通过我的用户名在课堂上认识我,那么我要求您不要使用我的代码。否则享受。我对代码进行了评论,因为从工作代码中学习是最好的学习方法。

只要您只使用字母,就可以增加标量而不是嵌套循环。如果你确实需要使用其他字符,我打赌你可以使用一组可能的字符,并通过该数组为每个位置增加,但是让我们忽略它,因为你似乎只需要那些字母=)

sub brute2()
{
print "Bruteforce Attack...\n";
print "Enter password length: "; #Prompt user for maximum length for pass
chomp(my $plen = (<>)); #Receive input and remove newline character
print "Password Length is $plen\n";
$plen++;
print "Press any key to continue.\n"; #Execute once they hit any key
if (<>)
{

    my $pass = "a"; #This code assumes only letters a..z, so we just set here
    while ( length $pass < $plen ) #Run check loop until we exaust all possibilities within the maximum length
    {

        my $status = system("unzip -pp -o -P $pass secret_file_brute.zip > /dev/null 2>&1"); #System call to compare our password against a zip file, this will set status to the return value
        print ("Attempting: $pass Return: $status\n");
        if ($status == 0) #Return value of 0 means success
        {

        print ("Password is: $pass Return is: $status\n"); #Print correct password. I did return value also for debug
        last; #Break loop since we got correct password

        }
        $pass++; #Increment $pass var to next iteration IE "a" to "b", "aa" to "ab", "zzz" to "aaaa" etc...

    }

}
}

答案 1 :(得分:-2)

根据我发现的man页面,unzip在无法解密时返回退出代码82。

sub try {
   my ($password) = @_;
   system("unzip -qq -o -P $password secret_file_brute.zip >/dev/null 2>&1");
   die("Can't launch unzip: $!\n") if $? == -1;
   die("unzip killed by signal ".($? & 0x7F)."\n") if $? & 0x7F;
   my $exit_code = $? >> 8;
   die("unzip exited with error $exit_code\n") if $exit_code && $exit_code != 82;
   return !$exit_code;
}

您的代码不会生成所有可能的密码(例如,它不会生成aaa)。以下是:

sub brute_force {
   for (my $password = 'a'; length($password)<5; ++$password) {
      return $password if try($password);
   }

   return undef;
}

最后一位是显示结果。

{
   my $password = brute_force();
   defined($password)
      or die("Password not found\n");

   print("$password\n");
}
相关问题