Perl - 我的解压缩系统调用 zip 文件破解时出现问题

Perl - Trouble with my unzip system call for zip file crack

我是一名大三学生,目前正在学习一种脚本语言 class,该语言应该在一个学期内让我们掌握中级水平 bash、perl 和 python。由于这个 class 是加速的,我们可以快速浏览主题,我们的教授赞成在我们有问题时使用论坛来补充我们的学习。

我目前正在处理我们的第一个任务。要求是使用提供的词表 "linux.words" 和基本的暴力攻击创建一个非常简单的字典攻击。暴力破解需要补偿 4 个字符串的任意组合。

我已经使用 print 语句来检查我的逻辑是否合理,而且看起来确实如此。如果您有任何关于如何提高我的逻辑的建议,我在这里学习,我洗耳恭听。

如果相关,这是在 Ubuntu v12.04 上。

我尝试用像 unicorn 这样的直接词替换调用中的标量,它运行良好,显然是错误的密码,并且 returns 正确。我已经在终端和脚本本身中完成了这项工作。我的教授已经花了 15 分钟的时间来查看它,然后将我推荐给论坛,并说它看起来不错。他怀疑我用Notepad++写的代码可能有隐藏字符。我使用 vim 直接在终端中重写了代码,它给出了与上面相同的错误。下面粘贴的代码来自 vim.

我的实际问题是我的系统调用给我带来了问题。它 returns 解压显示用法和其他帮助的帮助功能 material。

这是我的代码。

#!/usr/bin/perl 

use strict;
use warnings;

#Prototypes
sub brute();
sub dict();
sub AddSlashes($);

### ADD SLASHES ###

sub AddSlashes($)
{

    my $text = shift;
    $text =~ s/\/\\/g;
    $text =~ s/'/\'/g;
    $text =~ s/"/\"/g;
    $text =~ s/\0/\\0/g;
    return $text;

}

### BRUTEFORCE ATTACK ###
sub brute()
{

    print "Bruteforce Attack...\n";
    print "Press any key to continue.\n";
    if (<>)
    {

        #INCEPTION START
        my @larr1 = ('a'..'z'); #LEVEL 1 +
        foreach (@larr1)
        {
        my $layer1 = $_; #LEVEL 1 -

            my @larr2 = ('a'..'z'); #LEVEL 2 +
            foreach (@larr2)
            {
            my $layer2 = $_; # LEVEL 2 -

                my@larr3 = ('a'..'z'); #LEVEL 3 +
                foreach (@larr3)
                {
                my $layer3 = $_; #LEVEL 3 -

                    my@larr4 = ('a'..'z'); #LEVEL 4 +
                    foreach (@larr4)
                    {
                    my $layer4 = $_;
                    my $pass = ("$layer1$layer2$layer3$layer4"); 
                    print ($pass); #LEVEL 4 -
                    }

                }

            }

        }

    }

}

### DICTIONARY ATTACK ###
sub dict()
{

    print "Dictionary Attack...\n"; #Prompt User
    print "Provide wordlist: ";
    my $uInput = "";
    chomp($uInput = <>); #User provides wordlist
    (open IN, $uInput) #Bring in wordlist
        or die "Cannot open $uInput, $!"; #If we cannot open file, alert

    my @dict = <IN>; #Throw the wordlist into an array

    foreach (@dict)
    {

        print $_; #Debug, shows what word we are on
        #next; #Debug
        my $pass = AddSlashes($_); #To store the $_ value for later use

        #Check pass call
        my $status = system("unzip -qq -o -P $pass secret_file_dict.zip > /dev/null 2>&1"); #Return unzip system call set to var

        #Catch the correct password
        if ($status == 0)
        {

            print ("Return of unzip is ", $status, " and pass is ", $pass, "\n"); #Print out value of return as well as pass
            last;

        }

    }
}
### MAIN ###
dict();


exit (0);

这是我的错误

See "unzip -hh" or unzip.txt for more help.  Examples:
  unzip data1 -x joe   => extract all files except joe from zipfile data1.zip
  unzip -p foo | more  => send contents of foo.zip via pipe into program more
  unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer
aerify
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.

Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
  Default action is to extract files in list, except those in xlist, to exdir;
  file[.zip] may be a wildcard.  -Z => ZipInfo mode ("unzip -Z" for usage).

  -p  extract files to pipe, no messages     -l  list files (short format)
  -f  freshen existing files, create none    -t  test compressed archive data
  -u  update files, create if necessary      -z  display archive comment only
  -v  list verbosely/show version info       -T  timestamp archive to latest
  -x  exclude files that follow (in xlist)   -d  extract files into exdir
modifiers:
  -n  never overwrite existing files         -q  quiet mode (-qq => quieter)
  -o  overwrite files WITHOUT prompting      -a  auto-convert any text files
  -j  junk paths (do not make directories)   -aa treat ALL files as text
  -U  use escapes for all non-ASCII Unicode  -UU ignore any Unicode fields
  -C  match filenames case-insensitively     -L  make (some) names lowercase
  -X  restore UID/GID info                   -V  retain VMS version numbers
  -K  keep setuid/setgid/tacky permissions   -M  pipe through "more" pager
  -O CHARSET  specify a character encoding for DOS, Windows and OS/2 archives
  -I CHARSET  specify a character encoding for UNIX and other archives

See "unzip -hh" or unzip.txt for more help.  Examples:
  unzip data1 -x joe   => extract all files except joe from zipfile data1.zip
  unzip -p foo | more  => send contents of foo.zip via pipe into program more
  unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer
aerifying

显然不完整。主要我会切换 brute();对于字典();根据需要进行测试。一旦我的系统调用开始工作,我就会把它扔到暴力破解部分。

如果您需要我详细说明我的问题,请告诉我。我在这里专注于学习,所以请在您回复我的任何内容中添加白痴证明评论。

首先:不要使用 PERL 的原型。他们不做你或你的教授可能希望他们做的事。

其次:不要编写AddSlashes等自制转义例程。 Perl 有 quotemeta。使用它。

您的问题不在于特定的编程语言。你的教授花了多少时间在你的问题上,你拿了多少 类 与问题无关。关注实际问题,而不是所有无关紧要的问题"stuff".

例如,sub brute有什么意义?你不是在这个脚本中调用它,它与你的问题无关,所以不要post它。将您的问题缩小到最小的相关部分。

不要在 dict 的正文中提示输入 wordlist 文件。将功能分成一口大小的块,这样在每个上下文中您都可以专注于手头的问题。您的 dict_attack 子例程应该会收到文件句柄或对单词数组的引用。为了保持低内存占用,我们假设它是一个文件句柄(因此您不必将整个单词列表保存在内存中)。

因此,您的 main 看起来像:

sub main {
    # obtain name of wordlist file
    # open wordlist file
    # if success, call dict_attack with filehandle
    # dict_attack returns password on success
}

现在,您可以专注于 dict_attack

#!/usr/bin/perl

use strict;
use warnings;

main();

sub dict_attack {
    my $dict_fh = shift;

    while (my $word = <$dict_fh>) {
        $word =~ s/\A\s+//;
        $word =~ s/\s+\z//;

        print "Trying $word\n";

        my $pass = quotemeta( $word );
        my $cmd = "unzip -qq -o -P $pass test.zip";
        my $status = system $cmd;
        if ($status == 0) {
            return $word;
        }
    }

    return;
}

sub main {
    my $words = join("\n", qw(one two three four five));
    open my $fh, '<', $words or die $!;
    if (my $pass = dict_attack($fh)) {
        print "Password is '$pass'\n";
    }
    else {
        print "Not found\n";
    }
    return;
}

输出:

C:\...> perl y.pl
Trying one
Trying two
Trying three
Trying four
Trying five
Password is 'five'