How To: Generate Word-Lists with Python for Dictionary Attacks

Generate Word-Lists with Python for Dictionary Attacks

As per Alex's request, I am posting about generating word-lists in Python.

However, this is my FIRST attempt with Python, so please provide me with critiques and any and all comments. I really want to know what you think as there was a little bump here and there seeing as I am transitioning from C#.

Why the Program?

Well, let's just run through a simple scenario: you're about to hack a vulnerable login page, but you think that brute-force is going to take ages (in fact, there's a decent chance it will), so why not try out a dictionary attack first? Because it's faster.

[Please check my math here. I have not slept in the last 30 hours. I am not responsible for nonsense hereafter!]

The English alphabet is 26 characters in length, and a 5 character password utilising brute force is 26^5, assuming it is not uppercase and has no special characters. 26^5 = 11881376 combinations! And that's the easy tier. Try a full dictionary—916132832 combinations (includes just upper, lower case and numbers).

In these instances, you might want to try a dictionary attack. Now assuming a user has a password such as "thistle", a normal dictionary will suffice, but what if a password is "xZya6"?

Well this is the program for you!

Requirements

  • Python

Step 1 Beginning of Your Code

#! C:\python27

import string, random

The above two lines are the beginning of our code. 

Since I am working on windowzer, my first line points to where I installed my Python. For linux users, change it to #!/usr/bin/python

The import declaration just tells the program to import the string handling library and a library to handle random chars.

Step 2 The Meat & Bones

Now, if we think about it, we want to be able to do the following:

  1. Tell the program how short each word should be.
  2. Tell the program how long each word should be.
  3. Tell the program how many words to generate.

So enter these lines:

minimum=input('Please enter the minimum length of any give word to be generated: ')

maximum=input('Please enter the maximum length of any give word to be generated: ')

wmaximum=input('Please enter the max number of words to be generate in the dictionary: ')

Now decide on what kind of alphabet you will use—I chose the below:

alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYX0123456789'

Replace above with - alphabet = string.letters[0:52] + string.digits + string.punctuation

..for runtime-generated alphabet in full ascii (no special symbols such as ¶)

Next, declare the placeholder for our words.

string=''

Now, we tell Python to open a empty text file in write mode ("w"). (Linux users, point it your respective directory, or just write the file name if the file is next to your PY script)

FILE = open("wl.txt","w")

Now we write a loop which will range from 0 to the maximum number of words you defined, and generate words that hold random characters from the alphabet we defined earlier, in random order at variable length (assuming your min/max values were not identical on imput).

for count in xrange(0,wmaximum):

  for x in random.sample(alphabet,random.randint(minimum,maximum)):

      string+=x

Now we tell Python to write the strings (words) to the file we pointed the program to, by using '\n' to tell Python to separate each word in a new line.

  FILE.write(string+'\n')

And the last functions are just: (1) Clear the string, (2) Close the file after editing—very important as changes might not register if it is not closed—and (3) prints the word "Done!" after finishing.

  string=''

FILE.close()

print 'DONE!'

And that's it! Give it a go!

Source Code

Just updated your iPhone to iOS 18? You'll find a ton of hot new features for some of your most-used Apple apps. Dive in and see for yourself:

15 Comments

nice :D is there a way to do the same thing, only with a dictionary? for example, create random english words?

A friend of mine coded one that got English words :). It's quite clever. She made it spider Wikipedia for words and just made it not spider dupes or shorter than X characters. It's quite cool :3.

I have an English dictionary word list and a few basic wordlist generators - the dictionary for english language was actually found for the game Scrabble!!!

Anyway heres my repository with a few wordlist generators...and wordlists
https://github.com/raseribanez/Word-Lists

you will have to look through the words.txt files - these asre Dictionary words from a to z.....the generators however make all random combinations (non-words)

the words.txt contents are organised in a list - however...sometimes they show as a huge unseparated line of text (one string) but when cloned/downloaded they appear as a list. My generators are very basic....but work!

Yeah you can just adjust the for loop to run through a dictionary and join words together randomly from a given english dictionary. But there will not be any logic to it. You will end up with words like - "daisychaincatspear" or something.

Is that what you meant?

yup, pretty much :) i wonder, it would be pretty cool to have a visual-plot system that plots and connects random points on a given area.

Awesome little program, I think the next step would be to utilize Mechanize: http://wwwsearch.sourceforge.net/mechanize/ to fill the site's form automatically.

I found mechanize to be very useful

First, thanks for the code. Really useful. I however get the following error message:

<--
Traceback (most recent call last):
File "/home/arrayjumper/Desktop/Word-Lists for Dictionary attacks.py", line 10, in <module>
alphabet = string.letters0:52 + string.digits + string.punctuation
AttributeError: 'module' object has no attribute 'letters'
-->

Any idea what the problem might be?

how to do this generating non-repeted words?
Like:
alphabet is '123'
minimum=2
maximum=2
wmaximum=9
so result should be 11,12,13,23,21,32,22,33,31
but this setup gives me repeated results like 11,12,31,11,22,31,12,31,11

HOW TO BEGIN WITH DICTINARY ATTACK TO HACK WIFI SERVER ACCOUNT?

so mine runs, but I dont get the output using this coding example

I know it's supposed to make an empty text file for the results, but I get no text file, no results

Searched through my drive and nothing - it says 'Done' as-well when I run it. Isn't it supposed to create a txt document in the same dir as the python script?

I found it worked for me by printing the output to the Shell Window....then copy/paste into a txt doc - more work I know but I couldn't figure out why it wouldnt make the txt file - no errors either?? wierd lol

IT CAN ACCESS THE FILE BECAUSE IT HAS NOT BEEN CREATED

FIRST CREATE THE FILE
IT NO CREATE 4 U

It should be writing to a file yes. Did you download the code from the link at the bottom of the article?

really need help with a part of the code..

at this part, i want to generate fixed length passwords (e.g. 10 chars length), declared it with length=raw_input('length')

for count in xrange(0,wmaximum):

cant use random.randint(min,max) since i want a fixed value (no min,max just 'length', which module i need to use?

for x in random.sample(alphabet,random.randint(minimum,maximum)):

string+=x

struggeling here..

Share Your Thoughts

  • Hot
  • Latest