Example #1
0
        private static void RunExperiment()
        {
            // Create evolution algorithm using the seed model to initialize the population
            Console.WriteLine("Creating population...");
            Console.Write("Building Markov model...");

            // Load the XML configuration file
            XmlDocument xmlConfig = new XmlDocument();

            xmlConfig.Load(cp.ConfigFile);

            // Set Training File
            string trainingSetFile = cp.TrainingDb;

            // Create seedFile
            string seedFile = cp.SeedFile;

            // Create results file.
            string resultsFile = cp.ResultsFile;

            Console.WriteLine();
            Console.WriteLine("Training File: {0}", trainingSetFile);
            Console.WriteLine("Seed File: {0}", seedFile);
            Console.WriteLine("Results File: {0}", resultsFile);

            // Load the training set passwords from file
            var passwords = PasswordUtil.LoadPasswords(trainingSetFile, cp.PasswordLength);


            // Create a Markov model from the passwords. This model will be used
            // as our seed for the evolution.
            int outputs = MarkovFilterCreator.GenerateFirstOrderMarkovFilter(seedFile, passwords);

            // Free up the memory used by the passwords
            passwords = null;

            Console.WriteLine("Done! Outputs: {0}", outputs);

            _experiment             = new PasswordEvolutionExperiment();
            _experiment.OutputCount = outputs;

            // Initialize the experiment with the specifications in the config file.
            _experiment.Initialize("PasswordEvolution", xmlConfig.DocumentElement); //cmd arguments

            if (cp.EvolutionDb != null)
            {
                Console.WriteLine("Using command-line password file for evolution: {0}", cp.EvolutionDb);
                Console.WriteLine("Password length: {0}", cp.PasswordLength);
                PasswordCrackingEvaluator.Passwords = PasswordUtil.LoadPasswords(cp.EvolutionDb, cp.PasswordLength);
                Console.WriteLine("PasswordCrackingEvaluator.Passwords = {0}", PasswordCrackingEvaluator.Passwords == null ? "NULL" : "NOT NULL");
            }
            else
            {
                // Set the passwords to be used by the fitness evaluator.
                // These are the passwords our models will try to guess.
                PasswordCrackingEvaluator.Passwords = _experiment.Passwords;
                Console.WriteLine("Using config file passwords for evolution.");
            }
            Accounts = PasswordCrackingEvaluator.Passwords;

            Console.WriteLine("Loading seed...");

            // Load the seed model that we created at the start of this function
            var seed = _experiment.LoadPopulation(XmlReader.Create(seedFile))[0];

            // Create evolution algorithm using the seed model to initialize the population
            Console.WriteLine("Creating population...");

            ce  = new CondorEvaluator(cp.ExperimentDir, cp.ConfigFile, cp.ResultsFile, CondorGroup.Grad, outputs, PasswordCrackingEvaluator.Passwords, cp.PasswordLength);
            _ea = _experiment.CreateEvolutionAlgorithm(seed, ce);


            // Attach an update event handler. This will be called at the end of every generation
            // to log the progress of the evolution (see function logEvolutionProgress below).
            _ea.UpdateScheme = new UpdateScheme(1);
            _ea.UpdateEvent += new EventHandler(logEvolutionProgress);

            // Setup results file
            using (TextWriter writer = new StreamWriter(resultsFile))
                writer.WriteLine("Generation,Champion Accounts,Champion Uniques,Average Accounts,Average Uniques,Total Accounts,Total Uniques");
            //_generationalResultsFile = resultsFile;

            // Start algorithm (it will run on a background thread).
            Console.WriteLine("Starting evolution. Pop size: {0} Guesses: {1}", _experiment.DefaultPopulationSize, _experiment.GuessesPerIndividual);
            _ea.StartContinue();

            // Wait until the evolution is finished.
            while (_ea.RunState == RunState.Running)
            {
                Thread.Sleep(1000);
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            if (args.Length < 7)
            {
                Console.WriteLine("Usage: ModelEvaluator.exe <results_file> <model_id> <model_file> <finished_flag> <passwords_found_file> <config_file> <outputs> [passwords] [pw_length]");
                return;
            }
            Console.WriteLine("Starting");
            PasswordEvolutionExperiment experiment = new PasswordEvolutionExperiment();

            int    curArg             = 0;
            string resultsFile        = args[curArg++];
            int    modelId            = int.Parse(args[curArg++]);
            string modelFile          = args[curArg++];
            string finishedFlag       = args[curArg++];
            string passwordsFoundFile = args[curArg++];
            string configFile         = args[curArg++];
            int    outputs            = int.Parse(args[curArg++]);

            experiment.OutputCount = outputs;

            // Load the XML configuration file
            XmlDocument xmlConfig = new XmlDocument();

            xmlConfig.Load(configFile);

            experiment.Initialize("evaluation", xmlConfig.DocumentElement);

            // Optionally load the passwords from somewhere besides the file specified
            // in the experiment config file.
            if (args.Length > curArg)
            {
                Console.WriteLine("Passwords file: {0}", args[curArg]);
                string passwordFile = args[curArg++];

                int?pwLength = null;
                if (args.Length > curArg)
                {
                    Console.WriteLine("Password Length: {0}", args[curArg]);
                    pwLength = int.Parse(args[curArg++]);
                }
                // Load the passwords to evaluate with
                experiment.Passwords = PasswordUtil.LoadPasswords(passwordFile, pwLength);
                Console.WriteLine("Passwords loaded");
            }
            PasswordCrackingEvaluator.Passwords = experiment.Passwords;

            PasswordCrackingEvaluator eval = new PasswordCrackingEvaluator(experiment.GuessesPerIndividual, experiment.Hashed);

            var modelGenome = experiment.LoadPopulation(XmlReader.Create(modelFile))[0];
            var model       = experiment.CreateGenomeDecoder().Decode(modelGenome);

            using (TextWriter tw = new StreamWriter(resultsFile))
            {
                // Evaluate
                if (model == null)
                {   // Non-viable genome.
                    tw.WriteLine("0.0 0.0");
                }
                else
                {
                    FitnessInfo fitnessInfo = eval.Evaluate(model);
                    tw.WriteLine(fitnessInfo._fitness + " " + fitnessInfo._alternativeFitness);
                }
            }

            using (TextWriter writer = new StreamWriter(passwordsFoundFile))
                foreach (var pw in eval.FoundPasswords)
                {
                    writer.WriteLine(pw);
                }

            File.Create(finishedFlag);
        }