รหัสผ่าน Bruteforcing

Aug 21 2020

วันที่แล้วฉันเริ่มโครงการ ac # โดยที่ฉันใช้รหัสผ่าน bruteforce รหัสผ่านสามารถเป็นได้ทั้งจำนวนเต็มและสตริง ในรหัสฉันตรวจสอบว่ารหัสผ่านมีอักขระกี่ตัว แม้ว่านี่จะเป็นการโกง แต่จะใช้เวลานานเกินไปในการถอดรหัสเป็นอย่างอื่น ฉันโพสต์ไว้ที่นี่เพื่อดูว่าดีพอหรือไม่

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

คำเตือน: ด้านล่างนี้เป็นเพียงความคิดเห็นของฉันโปรดอย่าถือว่าเป็นแหล่งที่มาของความจริง นอกจากนี้ฉันสมมติว่ารหัสทำงานได้ตามที่คาดไว้ - ฉันจะไม่จมอยู่กับประสิทธิภาพหรือความสามารถในการใช้งาน

  • มีความคิดเห็นมากเกินไปให้พยายาม refactor โค้ดเพื่อให้อธิบายได้มากขึ้น ลุงบ๊อบพูดได้ดีที่สุด: 'ความคิดเห็นคือความล้มเหลวในการแสดงตัวตนในรหัส' (เว้นแต่จะเป็น 'ทำไม' ซึ่งไม่สามารถอธิบายได้แน่นอน)
  • เปลี่ยนรหัสไปยังฟังก์ชัน / คลาสความรับผิดชอบขนาดเล็กและเดี่ยวโปรดดูคำแนะนำที่นี่
  • การดำเนินการที่เกี่ยวข้องกับคอนโซลสามารถมอบหมายให้กับคลาสแยกต่างหาก (ตัวหุ้ม) เพื่อให้สามารถขยายได้อย่างง่ายดายในอนาคตเพื่อจัดการอินพุต / เอาต์พุตจากแหล่งอื่น นอกจากนี้เพื่อหลีกเลี่ยงการทำซ้ำ (เช่นการเขียน\n\nด้านหน้าของสตริงสามารถทำให้เป็นนามธรรมได้)
  • ชื่อตัวแปรควรระบุเนื้อหา (เว้นแต่จะชัดเจนมาก) คุณเขียนความคิดเห็นว่า 'ไม่ใช่ชื่อที่ดีที่สุด แต่ก็โอเค' ใช่มันไม่ใช่ชื่อที่ดีที่สุด แต่ฉันไม่คิดว่ามัน 'โอเค' ครึ่งทางของโค้ดฉันต้องเลื่อนขึ้นเพื่อตรวจสอบว่าตัวแปรนี้หมายถึงอะไรซึ่งไม่ถูกต้อง ที่คล้ายกันไปสำหรับตัวแปรเช่นchoices1, digits, posและอื่น ๆ
  • while (done == false)อาจเป็นwhile(!done)ได้ทั้งภาษาอังกฤษที่สั้นกว่าและอธิบายได้มากกว่า
  • ใช้Console.WriteLineแทนการเพิ่ม(คุณยังสามารถทำ\n\nConsole.WriteLine("")
  • ใช้ while วนซ้ำแทนการเรียกซ้ำสำหรับMain. เหตุผลสองประการ: คุณสามารถเล่นเกินจำนวนสแต็คได้ (ไม่น่ามีสำหรับมนุษย์ แต่บอททุกตัวสามารถทำได้อย่างง่ายดาย) และคนอื่น ๆ คาดว่าจะเห็นลูปของเกมมากกว่าฟังก์ชั่นซ้ำ
  • แสดงค่าอินพุตที่เป็นไปได้สำหรับการรีสตาร์ทผู้ใช้อาจลองYซึ่งจะไม่ทำงาน