Password di forza bruta

Aug 21 2020

Un giorno fa ho avviato il progetto ac # in cui eseguo la forza bruta delle password. Le password possono essere sia numeri interi che stringhe. Nel codice controllo quanti caratteri ha la password. Anche se questo è una specie di imbroglio, altrimenti ci vorrebbe troppo tempo per decifrarlo. Lo pubblico qui per vedere se è abbastanza buono.

using System;

namespace Hacking_Project
{
    class Program
    {
        static void Main()
        {
            //Console Color
            Console.ForegroundColor = ConsoleColor.White;
            Console.Clear();
            //If password is found
            bool done = false;
            //If the password is a string, not the best name but ok
            bool yes = false;
            //If the Guessed password is the same number of characters as the original password
            int pass = 0;
            //Guessed password
            string pass_check = "";
            //Possible characters for the password
            char[] pos = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                          'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                          'u', 'v', 'w', 'x', 'y', 'z'};
            Console.WriteLine("What is the password?");
            //Asking for the password to crack
            string password = Console.ReadLine();
            //How much characters does the password has. I know that this is kinda cheating, but then the bruteforce would take to much
            int digits = password.Length;
            //Initialising the random number generator
            Random rand = new Random();
            //The choices that the calculator will take for the guessed password(int)
            int[] choices = new int[digits];
            //The choices that the calculator will take for the guessed password(string)
            char[] choices1 = new char[digits];
            
            //If the password is a string
            for (int i = 0; i < pos.Length; i++)
            {
                if (password.Contains(pos[i]))
                {
                    password = password.ToLower();
                    yes = true;
                }
            }
            //The Cracking Part
            while (done == false)
            {
                if (!yes)
                {
                    for (int i = 0; i < digits; i++)
                    {
                        choices[i] = rand.Next(0, 9);
                        pass_check += choices[i];
                        pass++;
                        //Console Color
                        Console.ForegroundColor = ConsoleColor.DarkYellow;

                        if (pass != digits)
                        {
                            Console.Write(choices[i]);
                        }
                        else
                        {
                            Console.Write(choices[i] + ", ");
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < digits; i++)
                    {
                        choices1[i] = pos[rand.Next(pos.Length)];
                        pass_check += choices1[i];
                        pass++;
                        Console.ForegroundColor = ConsoleColor.DarkYellow;

                        if (pass != digits)
                        {
                            Console.Write(choices1[i]);
                        }
                        else
                        {
                            Console.Write(choices1[i] + ", ");
                        }
                    }
                }
                pass = 0;
                if (pass_check == password)
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("\n\nThe password is: ");
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write(pass_check);

                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("\n\nThe original password is: ");
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write(password);

                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.Write("\n\nDo you want to restart?");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write(" => ");
                    string restart = Console.ReadLine();
                    restart = restart.ToLower();

                    if (restart == "yes")
                    {
                        Main();
                    }
                    else if (restart == "no")
                    {
                        done = true;
                    }
                }
                //If the password is not found, quessed password is set to empty
                else
                {
                    pass_check = "";
                }
            }
        }
    }
}

Risposte

2 MaLiN2223 Aug 23 2020 at 05:20

Dichiarazione di non responsabilità: di seguito è solo la mia opinione, per favore non trattarla come una fonte di verità. Inoltre, presumo che il codice funzioni esattamente come previsto: non mi soffermerò sulle prestazioni o sulla validità.

  • Troppi commenti, prova invece a refactoring il codice per essere più esplicativo. Lo zio Bob lo ha detto meglio: "Un commento è un fallimento nell'esprimersi in codice" (a meno che non sia un "perché" che non può essere spiegato ovviamente).
  • Dividi il codice in funzioni/classi di responsabilità piccole e singole, vedi qui per indicazioni.
  • Le operazioni relative alla console possono essere delegate a una classe separata (un wrapper) in modo che possa essere facilmente estesa in futuro per gestire l'input/output da altre fonti. Inoltre, per evitare duplicazioni (ad es. la scrittura \n\ndavanti alle stringhe può essere astratta).
  • I nomi delle variabili dovrebbero indicare il contenuto (a meno che non sia molto ovvio). Hai scritto un commento "non è il nome migliore ma ok", sì, non è il nome migliore ma non penso che sia "ok". A metà del codice ho dovuto scorrere verso l'alto per verificare cosa significa questa variabile, non è giusto. Simile vale per variabili come choices1, digitse posaltre.
  • while (done == false)potrebbe essere while(!done)sia più breve che più esplicativo "in inglese".
  • Usa Console.WriteLineinvece di aggiungere \n\n(puoi anche fare Console.WriteLine("").
  • Usa il ciclo while invece della ricorsione per Main. Due motivi per questo: puoi superare gli stack (improbabile per gli umani, ma qualsiasi bot può farlo facilmente) E ci si aspetterebbe più da altri di vedere un ciclo di gioco piuttosto che una funzione ricorsiva.
  • Visualizza i possibili valori di input per il riavvio, l'utente potrebbe provare Y, che non funzionerà.