Bruteforcingパスワード

Aug 21 2020

1日前、パスワードをブルートフォースするac#プロジェクトを開始しました。パスワードは整数と文字列の両方にすることができます。コードでは、パスワードの文字数を確認します。これはちょっとごまかしですが、そうでなければクラックするには時間がかかりすぎます。ここに投稿して、十分かどうかを確認します。

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 = "";
                }
            }
        }
    }
}

回答

2 MaLiN2223 Aug 23 2020 at 05:20

免責事項:以下は私の意見です。真実の情報源として扱わないでください。また、コードは期待どおりに機能すると想定しています。パフォーマンスや有効性については詳しく説明しません。

  • コメントが多すぎる場合は、代わりにコードをリファクタリングして説明を強化してください。ボブおじさんはそれを最もよく言いました:「コメントはコードで自分自身を表現することの失敗です」(もちろん説明できない「理由」でない限り)。
  • コードを小規模で単一責任の関数/クラスに分割します。ガイダンスについては、ここを参照してください。
  • コンソール関連の操作は別のクラス(ラッパー)に委任できるため、将来、他のソースからの入出力を処理するために簡単に拡張できます。また、重複を避けるために(たとえば\n\n、文字列の前に書くことを抽象化することができます)。
  • 変数名は内容を示す必要があります(非常に明白でない限り)。あなたはコメントを書いた '最高の名前ではないが大丈夫'、はい-それは最高の名前ではありませんが、私はそれが '大丈夫だとは思いません。コードの途中で、この変数の意味を確認するために上にスクロールする必要がありましたが、これは正しくありません。同様のような変数のために行くchoices1digitsposなど。
  • while (done == false)while(!done)「英語で」より短く、より説明的である可能性があります。
  • Console.WriteLine追加する代わりに使用し\n\nます(実行することもできます)Console.WriteLine("")
  • の再帰の代わりにwhileループを使用しますMain。その理由は2つあります。スタックを超える可能性があり(人間にはありそうにありませんが、どのボットでも簡単に実行できます)、再帰関数ではなくゲームループを見ることが他の人に期待されます。
  • 再起動の可能な入力値を表示します。ユーザーが試行する可能性がありますが、機能しませYん。