Link to home
Start Free TrialLog in
Avatar of SunnyX
SunnyX

asked on

Brute Force password attack java could you explain how to work one method.

Please could you explain for me how does a method
public void doItMulti()

Open in new window

works.
May be you could draw for me  some action UML or sequence or any other behavior UML. May be you could provide for this methods some useful and detail comments for each line of code. Thx for your help in advance !

package bfpasswrd;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class PasswordCracker
{

String passwordToCrack;
public boolean passwordFound;
int min;
int max;
StringBuilder crackedPassword;

public void prepare(String text)
{
    passwordToCrack = text;

    passwordFound = false;
    min = 32;
    max = 126;
    crackedPassword = new StringBuilder();
    crackedPassword.append((char) (min - 1));
}

public void result()
{
    System.out.println("Cracked Password is: " + crackedPassword.toString());
}

public void incrementString(StringBuilder text, int min, int max)
{
    text.setCharAt(0, (char) ((int) text.charAt(0) + 1));
    for (int i = 0; i < text.length(); i++)
    {
        if (text.charAt(i) > (char) max)
        {
            text.setCharAt(i, (char) min);
            if (text.length() == i + 1)
            {
                text.append((char) min);
            }
            else
            {
                text.setCharAt(i + 1, (char) ((int) text.charAt(i + 1) + 1));
            }
        }
    }
}

public void runMulti(String text)
{
    prepare(text);
    double time = System.nanoTime();
    doItMulti();
    time = System.nanoTime() - time;
    System.out.println(time / (1000000000));
    result();

}

public void runSingle(String text)
{
    prepare(text);
    double time = System.nanoTime();
    doItSingle();
    time = System.nanoTime() - time;
    System.out.println(time / (1000000000));
    result();
}

public void doItSingle()
{
    while (passwordFound == false)
    {
        incrementString(crackedPassword, min, max);
        passwordFound = crackedPassword.toString().equals(passwordToCrack);
    }
}

public void doItMulti()
{
    int cores = Runtime.getRuntime().availableProcessors();
    ArrayList<Future<?>> tasks = new ArrayList<Future<?>>(cores);
    ExecutorService executor = Executors.newFixedThreadPool(cores);
    final long step = 2000;
    for (long i = 0; i < Long.MAX_VALUE; i += step)
    {
        while(tasks.size() > cores)
        {
            for(int w = 0; w < tasks.size();w++)
            {
                if(tasks.get(w).isDone())
                {
                    tasks.remove(w);
                    break;
                }
            }
            try
            {
                Thread.sleep(0);
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        {
            final long j = i;
            if (passwordFound == false)
            {
                tasks.add(executor.submit(new Runnable()
                {

                    public void run()
                    {
                        long border = j + step;
                        StringBuilder toCrack = new StringBuilder(10);
                        toCrack.append(constructString3(j, min, max));
                        for (long k = j; k < border; k++)
                        {
                            incrementString(toCrack, min, max);
                            boolean found = toCrack.toString().equals(passwordToCrack);
                            if (found)
                            {
                                crackedPassword = toCrack;
                                passwordFound = found;
                                break;
                            }
                        }
                    }
                }));
            }
            else
            {
                break;
            }
        }
    }
    executor.shutdownNow();
}

public String constructString3(long number, long min, long max)
{
    StringBuilder text = new StringBuilder();
    if (number > Long.MAX_VALUE - min)
    {
        number = Long.MAX_VALUE - min;
    }
    ArrayList<Long> vector = new ArrayList<Long>(10);
    vector.add(min - 1 + number);
    long range = max - min + 1;
    boolean nextLetter = false;
    for (int i = 0; i < vector.size(); i++)
    {
        long nextLetterCounter = 0;
        while (vector.get(i) > max)
        {
            nextLetter = true;
            long multiplicator = Math.abs(vector.get(i) / range);
            if ((vector.get(i) - (multiplicator * range)) < min)
            {
                multiplicator -= 1;
            }
            vector.set(i, vector.get(i) - (multiplicator * range));
            nextLetterCounter += multiplicator;
        }
        if (nextLetter)
        {
            vector.add((long) (min + nextLetterCounter - 1));
            nextLetter = false;
        }
        text.append((char) vector.get(i).intValue());
    }
    return text.toString();
}
}

Open in new window


package bfpasswrd;


import java.util.Scanner;

public class Test
{
    public static void main(String[] args)
    {

    System.out.print("Type password to be cracked: ");
    @SuppressWarnings("resource")
	String input = new Scanner(System.in).nextLine();
    PasswordCracker cracker = new PasswordCracker();
    System.out.println("Multithreaded");
    cracker.runMulti(input);
    cracker = new PasswordCracker();
    System.out.println("Single threaded Original");
    cracker.runSingle(input);
    System.out.println("Finished...");
    }
}

Open in new window

Avatar of SunnyX
SunnyX

ASKER

Probably I should make a remark.
Please understand me correctly, my goal is to figure out how does java concurrency actually works by using simple examples. I found example that simulate bank ( transaction, log in to account, withdraw and etc ) on producer consumer pattern. Now I would like to figure out how does password cracker works. Then I try to connect this too projects and so on and so forth. Nobody use Brut forth approach to crack the password, may be in 90s it was possible.
This code, I  found here :
stackoverflow.com/questions/9351923/brute-force-performance-java-vs-c-sharp
Avatar of SunnyX

ASKER


Experts,

As you probably know, it is against EE policy for members to assist in creating any sort of hacking/cracking program.

Be sure that your posts do not verge upon this policy.

Regards,
phoffric
ZA


Sorry but brute force attack is not hacking method since 80s. It even include in the George Tech Master Computer Science program please take a look . as a part of Linear Programming chapter in GT - Computability, Complexity, Theory: Algorithms cource

https://www.youtube.com/watch?v=thtUvQ0tCLE
Brute Force Algorithm Solution - GT - Computability, Complexity, Theory: Algorithms
https://www.udacity.com/course/computability-complexity-algorithms--ud061
 Truth be told this is the reason why I post this question.

You remark, it is like telling that if I decided to take in my University nuclear physics 101 course  that mean that no-none should help me because of policy and  because I definitely  create duty bomb after that. This is absurd.

Therefore, you as administrator , rather should tell me ( or do it for me )  how I should rename all my question related to this topic in order that I finally receive the help on my numerous questions about brute-force attack.

Moreover this code from stockoverflow I believe the stockoverflow has moderators too. stackoverflow.com/questions/9351923/brute-force-performance-java-vs-c-sharp

Phoffric, I'm waiting for feedback from you.
Regards,

Sunny Amigo
Avatar of SunnyX

ASKER

The tag "Neglected" is unfair !
Avatar of krakatoa
Neglected means no expert has commented, that's all - nothing to do with your question as such.

As I said, the code you wanted explained is just a trial of each permutation of the given string. Put some printouts in the code between the iterations to see the mechanics as they happen.

You don't need to use a password cracking problem to understand concurrency- reading up about the API would be more useful.
Avatar of SunnyX

ASKER


Neglected means no expert has commented, that's all - nothing to do with your question as such.

As I said, the code you wanted explained is just a trial of each permutation of the given string. Put some printouts in the code between the iterations to see the mechanics as they happen.

Thx for your comment. It is very important for me to do not fill that I'm along in this challenge. Even tiny replies warm my motivation up. I found book where describe how to test concurrency app through the JUnit, I think for me it will be solution.


You don't need to use a password cracking problem to understand concurrency- reading up about the API would be more useful.

As I implied above the brute-force algorithm was not my chose at the first place, besides I found batch of solid plug-and-play projects.

Neglected means no expert has commented, that's all - nothing to do with your question as such.
Thx for your elaboration. If so then I strongly believe that negative connotation in the admin's comment about my question is not fair.
The code is taken from
stackoverflow.com/questions/9351923/brute-force-performance-java-vs-c-sharp

All the comments there should be - code is neutered by removing the actual password cracking, just to explore java logistics around parallelism.
Avatar of SunnyX

ASKER

Thx for attention


All the comments there should be

I know the source of the project. I post the same link before. Unfortunately, There aren't comments for the methods that I want to explore. Could you help me the ends comments for the
public void doItMulti() methods

code is neutered by removing the actual password cracking
Yes it is easy to understand in debug mode for single thread version of the project

Thx for reply!
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.