Member Avatar for sravan953

Hey guys...

I created a uber-basic brute force program(not exactly 'brute force'), where in the program opens a 'log.txt' file and reads the contents. I created a list of alphabets A-Z and numbers 0-9....what I wanted the program to do was, try out different combinations with the given set of characters until it matches the given set of characters in the log.txt file, and then display an appropriate message...here's the code I used:

killzone2(in the log.txt file)

import random

combos=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','v','w','x','y','z']
combos.append(range(10))
choice=random.choice(combos)

log=open('log.txt','r')
read_log=log.read()
while choice!=read_log:
    choice=random.choice(combos)
print choice

Thanks for the help in advance all you guys! ;P

When comparing two strings you want to make sure they are the same length. They will never be equal if they are different lengths, so add a statement which prints if they are not equal,

log_fp=open('log.txt','r')
read_log=log_fp.read()
while choice!=read_log:
    if len(choice) != len(read_log):
        print "choice length", choice, len(choice), \
                "and read_log length", read_log, len(read_log)
    choice=random.choice(combos)
print choice
Member Avatar for sravan953

When comparing two strings you want to make sure they are the same length. They will never be equal if they are different lengths, so add a statement which prints if they are not equal,

log_fp=open('log.txt','r')
read_log=log_fp.read()
while choice!=read_log:
    if len(choice) != len(read_log):
        print "choice length", choice, len(choice), \
                "and read_log length", read_log, len(read_log)
    choice=random.choice(combos)
print choice

Hey, I got to know one more thing(I think I'm correct) that the 'choice' command only picks a *single* entity from the list....so since my actual text is 'killzone2', the program will go on for ever and never end, because every time, only a single character will be matched with the given text! :( Any idea on how to actually pick items randomly from a list? Thnks

Even if you use the modified code below, it will take almost forever ...

import random
import string

combos = list(string.ascii_lowercase + string.digits)

#log=open('log.txt','r')
#search = read_log=log.read()
# for test only
search = 'killzone2'
combo = ''
while combo != search:
    # build up string combo the same length as search
    for n in search:
        combo += random.choice(combos)
    # test
    print combo,
    # reset
    combo = ''

print combo
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.