School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,099 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,034 people online right now. Registration is fast and FREE... Join Now!




Perl Wordlist Creator

 

Perl Wordlist Creator, To create a worldist based on bruteforce

Cbeppe

18 Sep, 2009 - 09:55 AM
Post #1

New D.I.C Head
*

Joined: 16 Sep, 2009
Posts: 12


My Contributions
I need a wordlist that can be used for password cracking (No, nothing illegal ;-) ), but I couldn't find any real good ones online so I decided to make a short Perl script that would create one. The problem is, while I have an idea what it should look like, I can't make Perl do it. As you probably can guess by that statement, I'm pretty novice at this...

Anyway, here's the code I have so far.

CODE
#!D:\Perl\bin\perl

use warnings;


BEGIN {
    open my $filename,">>", "words.txt";
}

#This is the array of characters I want my words to make.
@1 = qw(0 1 2 3 4 5 6 7 8 9);


foreach $1(@1) {
    print "$1\n";
    $1++
    
}


The idea is that it will print something like this:

0 1 2 3 4 5 6 7 8 9 00 01 02 03 04 05 06 07 08 09 10 11 12 13... and so on...

I don't want someone to write the entire script for me, but if someone would tell me what kind of code I should use to make this happen, I'd be grateful.

The next step after that would be to make it write only the combos between 7 and 15(f.ex) characters.

Thanks a lot guys ;-)



User is offlineProfile CardPM
+Quote Post


dsherohman

RE: Perl Wordlist Creator

19 Sep, 2009 - 02:52 AM
Post #2

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 204



Thanked: 36 times
My Contributions
The biggest problem in your posted code is that you have meaningless variable names. Your particular choice of meaningless names is also problematic, as $1 is used to store the results of regular expression matches, so if you later add any code which captures portions of a string using a regex, you'll be at high risk of suddenly developing bugs of a sort which are very difficult to track down.

As another bit of general advice, you should always "use strict" along with "use warnings". (There are times when you need to do something "strict" won't allow, but they're few and far between.)

Just applying minor cleanup to your posted code:
CODE

#!D:\Perl\bin\perl

use strict;
use warnings;

open my $outfile, ">>", "words.txt";

#This is the array of characters I want my words to make.
my @word_characters = (0 .. 9);

foreach my $character (@word_characters) {
    print $outfile "$character\n";
}

close $outfile;  # You can get away without this, but it's a good habit


I removed the BEGIN block because you didn't need it. Although it's not entirely accurate, you can think of BEGIN blocks as executing while the program is being compiled, before it actually runs. You don't need to open the file until the program starts running, so it shouldn't be in a BEGIN. (This also means that code in BEGIN blocks will run when you syntax-check your code with "perl -c", which would not normally be a sensible time for you to be opening files.)

I also removed your $1++ (or $character++, with the new variable names) line because the foreach loop already handles the process of walking through the list for you. $character is automatically reassigned to the next value in @word_characters at the end of each iteration, so you don't need to increment anything yourself and, actually, the incremented value would just be thrown away anyhow when the foreach overwrote it.

The "my" before the first use of each variable is there to declare it (and assign its scope, but I won't get into a discussion of variable scoping just now). This is one of the things required by "strict" and helps to protect you against bugs caused by typos in variable names as well as preventing you from accidentally creating global variables.

Changing the definition of @word_characters to "(0 .. 9)" makes it a little more compact while still doing the same thing. More importantly, this will save you from having to worry about whether you missed or duplicated anything when you add letters and can just say "( 0 .. 9, 'a' .. 'z', 'A' .. 'Z' )". Unfortunately, you can't use .. in a qw list (it will just give you a literal ".."), but I think that's a more-than-worthwhile tradeoff here.

Just a plain "print" will send output to STDOUT - generally the console window where the program is running. I changed it to "print $outfile" so that output will instead go to the file opened on the filehandle $outfile. (Bonus points for using a lexical filehandle and three-argument open, by the way!)

Anyhow, with this cleanup, you now have a well-written Perl program which prints out the list of values contained in @word_characters. Now you just need to add some code to give you combinations of multiple characters - I would suggest looking into recursion as a way to do this.
User is offlineProfile CardPM
+Quote Post

Cbeppe

RE: Perl Wordlist Creator

21 Sep, 2009 - 02:41 AM
Post #3

New D.I.C Head
*

Joined: 16 Sep, 2009
Posts: 12


My Contributions
^-> Thanks for cleaning it up. Recursion, as far as I understand it, is to divide the problem into smaller bits, and solve one of these problems at a time. Unfortunately, i do not see how this can be done here. I will look into it further though.

Thank you very much for your help.
User is offlineProfile CardPM
+Quote Post

dsherohman

RE: Perl Wordlist Creator

21 Sep, 2009 - 06:21 AM
Post #4

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 204



Thanked: 36 times
My Contributions
QUOTE(Cbeppe @ 21 Sep, 2009 - 10:41 AM) *
Recursion, as far as I understand it, is to divide the problem into smaller bits, and solve one of these problems at a time.

You do not understand correctly. The standard joke is "To understand recursion, one must first understand recursion."

Recursion is the (broadly general) technique of using a subroutine which calls itself. In this case, you could build all 8-character strings by taking each character and appending each possible 7-character string (which would be built by taking each character and appending each possible 6-character string, etc., until you get down to 1-character strings).

Going back to your (mis)understanding of recursion, although breaking the problem into smaller pieces is not itself recursion, you are making use of recursion when you break the smaller pieces into even smaller ones, then break those down even further, etc.

In any case, while this may be an excellent learning exercise and expose you to some useful concepts (such as recursion), your stated ultimate goal of generating all possible 8- to 12-character alphanumeric combinations isn't terribly feasible, as I went into in a reply to you earlier today over on PerlMonks.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 12:10PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month