Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I'm relatively new to PERL, I know the basic syntax, but there's still a lot to learn for me. I'm attempting to write a script that will print out a wordlist based on the "Brute-force principle". By that I mean that it will create a list of "words" in the format of "0 1 2 3 4 5 6 7 8 9 and then continue to 00 01 02 03 04 05 06 07 08 09 10 11 12 13", and so on. So far I have got the following script that creates the first line. The problem now is that I have absolutely no clue how to continue the script.
#!D:\Perl\bin\perl use warnings; use strict; open my $outfile,">>", "words.txt"; my @word_characters = (0..9); foreach my $character(@word_characters) { print $outfile "$character\n"; } close $outfile;
In the future, I plan to add a filter to only print those between 6 and 20 (f.ex. characters), but for now, I just want to get this working. Please note that I do not want anyone to write the whole script for me, but if you would like to point me in he right directions, and maybe tell me what arguments to use, I would be very grateful ;-) Thanks a lot.

Replies are listed 'Best First'.
Re: Attempting to create a brute-force wordlist
by tmharish (Friar) on Sep 20, 2009 at 14:56 UTC
    You should check out this thread.

    Its very close to what you are looking for!
Re: Attempting to create a brute-force wordlist
by CountZero (Bishop) on Sep 20, 2009 at 17:00 UTC
    What do you think of this?
    use strict; { local $,="\t"; print @{add_next_level(add_next_level([qw/1 2 3/], [qw/a b c/]),[q +w/* _ +/])}; } sub add_next_level { my ($basic_array_ref, $add_array_ref) = @_; my @result; for my $basic (@$basic_array_ref) { for my $additional (@$add_array_ref) { push @result, "$basic$additional"; } } return \@result; }
    The subroutine takes two arguments, both arrayrefs: the first one refers to the array to add to, and the second one points to the array of things to add to each of the elements of the first array. The subroutine returns an arrayref with the result. As you see, you can "chain" this subroutine and build higher levels easily.

    And BTW, the name of the Language is "Perl" and the interpreter is "perl". Never, ever use "PERL".

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Thank you very much. I see where you are pointing me, but unfortunately my Perl skills just don't follow. I do not understand what I should add to/modify to make it work. I will look up arrayfrefs and subroutines, though before requesting anymore of a walkthrough ;-) Thank you very much for your help.
Re: Attempting to create a brute-force wordlist
by graff (Chancellor) on Sep 20, 2009 at 16:05 UTC
    How you "continue the script" will depend on how far you want your "word list" to continue. The nature of your "word list" seems so basic that I wonder if you really need to store it as a file at all. What will it be used for? (And wouldn't that use be better served by writing a loop to iterate over a series of numbers, rather than reading a list of numbers from a file?)

    Anyway, if your question is really "how do I create numbers with leading zeros?", the answer is sprintf (and its cousin "printf"):

    my $begin = sprintf( "%03d", 0 ); my $end = sprintf( "%03d", 79 ); print "make a list that runs from $begin to $end\n";
    Have fun with that.
      I'm sorry, but I think you misunderstood my question a bit. Although it might have a simple solution, it isn't as simple as printing a list of numbers with zeros in front of them. (That I could do myself). No, the purpose of the file is to act as a dictionary for a password cracking program (don't worry, I'm not going to do anything illegal ;-) ).

      Therefore this should work with letters as well as numbers. The reason I started with numbers is because there are less combinations with 10 different numbers, than with 26 different letters. This was supposed to be a first try to see if I could get it to work. The goal is to print all possible combinations of the numbers 0-9 (for now) until they reach the point of 10 characters (again, for now.) I hope this explains it a bit clearer, I'm sorry if it was poorly explained before.

        There are 36^10=3656158440062976 different 10 character words if you will use [0-9a-z] characters, and that would require quite a lot of disk space.