class Bruteforce

Simple multithreaded BruteForce class. It reads username and password list inside text files (initialize arguments). Thread number can be changed with setNumThreads method (default 3). To start execusion, you must pass reference to a class that has an exec method with credentials as input parameters.

TODO ==> 
- Progress bar (pacman one)

Public Class Methods

new(username_list, password_list) click to toggle source
# File lib/bruteforce.rb, line 23
def initialize(username_list, password_list)
        @num_threads = 3
        @usernames = Concurrent::Array.new
        @passwords = Concurrent::Array.new
        @user_idx = Concurrent::AtomicReference.new(0)
        @pwd_idx = Concurrent::AtomicReference.new(0)

        @result = Concurrent::Hash.new

        File.open(username_list).each do |line|
                @usernames[@usernames.size] = line
        end

        File.open(password_list).each do |line|
                @passwords[@passwords.size] = line
        end
        @has_next = Concurrent::MutexAtomicBoolean.new(false)
end

Public Instance Methods

addPasswords(passwords_list) click to toggle source
# File lib/bruteforce.rb, line 48
def addPasswords(passwords_list)
        File.open(passwords_list).each do |line|
                @passwords[@passwords.size] = line
        end
end
addUsernames(username_list) click to toggle source
# File lib/bruteforce.rb, line 42
def addUsernames(username_list)
        File.open(username_list).each do |line|
                @usernames[@usernames.size] = line
        end
end
getNext() click to toggle source
# File lib/bruteforce.rb, line 68
def getNext
        res = Hash.new
        res["username"] = @usernames[@user_idx.value]
        res["password"] = @passwords[@pwd_idx.value]

        @pwd_idx.update {|v| v + 1}
        if @pwd_idx.value == @passwords.size 
                @pwd_idx.update {|v| 0}
                @user_idx.update {|v| v + 1}

                if @user_idx.value == @usernames.size
                        @has_next.value = false
                end
        end
        @tried.update {|v| v + 1}
        return res
end
hasNext() click to toggle source
# File lib/bruteforce.rb, line 60
def hasNext
        return @has_next.value
end
progression() click to toggle source
# File lib/bruteforce.rb, line 64
def progression
        res = " ====> Tried : " + @tried.value.to_s + "/" + @possibilities.value.to_s
end
setNumThreads(num) click to toggle source
# File lib/bruteforce.rb, line 86
def setNumThreads(num)
        @num_threads = num
end
setResult(uname,pwd) click to toggle source
# File lib/bruteforce.rb, line 54
def setResult(uname,pwd)
        @result["username"] = username
        @result["password"] = password
end
start(class_ref) click to toggle source
# File lib/bruteforce.rb, line 90
def start(class_ref)

        @has_next.value = if @usernames.size >0 and @passwords.size > 0
                                  true
                          else
                                  false
                          end

        puts "Number of usernames #{@usernames.size}"
        puts "Number of passwords #{@passwords.size}"
        @possibilities = Concurrent::AtomicReference.new(@passwords.size*@usernames.size)
        @tried = Concurrent::AtomicReference.new(0)
        puts "Number of combinations : #{@possibilities.value}"

        @user_idx.update {|v| 0}
        @pwd_idx.update{|v| 0}

        @threads = []

        @num_threads.times do |i|
                @@count.update {|v| v + 1}
                @threads[i] = Thread.new {
                        Thread.current["instance"] = i
                        puts "New thread #{Thread.current["instance"]}"
                        client = class_ref.new
                        while self.hasNext
                                puts self.progression
                                cred = self.getNext
                                if client.exec(cred) == true
                                        setResult(cred["username"], cred["password"])
                                end
                        end
                        @@count.update {|v| v - 1}
                }
        end
        @threads.each(&:join)
end
wait(path = "bruteforce_result.txt") click to toggle source
# File lib/bruteforce.rb, line 128
def wait(path = "bruteforce_result.txt")
        while @@count > 0 do
        end
        of = File.open(path,"w")
        of.write(@result["username"])
        of.write("\n")
        of.write(@result["password"])
        of.close
end