Exemplo n.º 1
0
        //static IGenomeDecoder<NeatGenome, MarkovChain> _genomeDecoder;
        //static PasswordCrackingEvaluator _passwordCrackingEvaluator;



        public static void Evaluate(IGenomeDecoder<NeatGenome, MarkovChain> genomeDecoder, PasswordCrackingEvaluator passwordCrackingEvaluator, PasswordEvolutionExperiment experiment)
        {

            string[] genomeFiles = Directory.GetFiles(@"..\..\..\experiments\genomes\", "*.xml");

            XmlDocument doc = new XmlDocument();
            int genomeNumber;


            foreach (string genomeFile in genomeFiles)
            {
                // Read in genome
                doc.Load(genomeFile);

                //NeatGenome genome = NeatGenomeXmlIO.LoadGenome(doc, false);
                //NeatGenomeFactory genomeFactory = (NeatGenomeFactory)CreateGenomeFactory();

                NeatGenome genome = experiment.LoadPopulation(XmlReader.Create(genomeFile))[0];
                MarkovChain phenome = experiment.CreateGenomeDecoder().Decode(genome);//genomeDecoder.Decode(genome);

                string[] filePath = genomeFile.Split('\\');
                string[] fileName = (filePath[filePath.Length - 1]).Split('-');


                String fileNumber = (fileName[1]).Split('.')[0];

                genomeNumber = Convert.ToInt32(fileNumber);


                //FileStream fs = File.Open(@"..\..\..\experiments\genomes\genome-results\genome-"+genomeNumber+"-results.txt", FileMode.CreateNew, FileAccess.Write);
                TextWriter tw = new StreamWriter(@"..\..\..\experiments\genomes\genome-results\genome-" + genomeNumber + "-results.txt");

                // Evaluate
                if (null == phenome)
                {   // Non-viable genome.
                    tw.WriteLine("0.0 0.0");
                }
                else
                {
                    FitnessInfo fitnessInfo = passwordCrackingEvaluator.Evaluate(phenome);
                    double val = fitnessInfo._fitness;
                    double val2 = fitnessInfo._alternativeFitness;
                    tw.WriteLine(fitnessInfo._fitness + " " + fitnessInfo._alternativeFitness);
                }
                tw.Close();
                File.Create(@"..\..\..\experiments\genomes\genome-finished\genome-" + genomeNumber + "-finished.txt");
            }

            // Write results?? -> genome_#_results
            // Write finished flag -> genome_#_finished

        }
Exemplo n.º 2
0
        //static IGenomeDecoder<NeatGenome, MarkovChain> _genomeDecoder;
        //static PasswordCrackingEvaluator _passwordCrackingEvaluator;



        public static void Evaluate(IGenomeDecoder <NeatGenome, MarkovChain> genomeDecoder, PasswordCrackingEvaluator passwordCrackingEvaluator, PasswordEvolutionExperiment experiment)
        {
            string[] genomeFiles = Directory.GetFiles(@"..\..\..\experiments\genomes\", "*.xml");

            XmlDocument doc = new XmlDocument();
            int         genomeNumber;


            foreach (string genomeFile in genomeFiles)
            {
                // Read in genome
                doc.Load(genomeFile);

                //NeatGenome genome = NeatGenomeXmlIO.LoadGenome(doc, false);
                //NeatGenomeFactory genomeFactory = (NeatGenomeFactory)CreateGenomeFactory();

                NeatGenome  genome  = experiment.LoadPopulation(XmlReader.Create(genomeFile))[0];
                MarkovChain phenome = experiment.CreateGenomeDecoder().Decode(genome);//genomeDecoder.Decode(genome);

                string[] filePath = genomeFile.Split('\\');
                string[] fileName = (filePath[filePath.Length - 1]).Split('-');


                String fileNumber = (fileName[1]).Split('.')[0];

                genomeNumber = Convert.ToInt32(fileNumber);


                //FileStream fs = File.Open(@"..\..\..\experiments\genomes\genome-results\genome-"+genomeNumber+"-results.txt", FileMode.CreateNew, FileAccess.Write);
                TextWriter tw = new StreamWriter(@"..\..\..\experiments\genomes\genome-results\genome-" + genomeNumber + "-results.txt");

                // Evaluate
                if (null == phenome)
                {   // Non-viable genome.
                    tw.WriteLine("0.0 0.0");
                }
                else
                {
                    FitnessInfo fitnessInfo = passwordCrackingEvaluator.Evaluate(phenome);
                    double      val         = fitnessInfo._fitness;
                    double      val2        = fitnessInfo._alternativeFitness;
                    tw.WriteLine(fitnessInfo._fitness + " " + fitnessInfo._alternativeFitness);
                }
                tw.Close();
                File.Create(@"..\..\..\experiments\genomes\genome-finished\genome-" + genomeNumber + "-finished.txt");
            }

            // Write results?? -> genome_#_results
            // Write finished flag -> genome_#_finished
        }
Exemplo n.º 3
0
        /// <summary>
        /// Trains a Markov model on a the training set of passwords, then evolves it against the target password database
        /// specified in the config file. At the end of the evolution, the champion model is evaluated for a larger number
        /// of guesses.
        /// </summary>
        /// <param name="trainingSetFile">The file containing the passwords from which to build the initial Markov model.</param>
        /// <param name="seedFile">The file to which the initial Markov model will be saved.</param>
        /// <param name="configFile">The file containing all the configuration parameters of the evolution.</param>
        /// <param name="resultsFile">The file to which the results will be saved at each generation.</param>
        /// <param name="validateSeed">If true, the seed model will first be validated against a large number of guesses.</param>
        //private static void RunExperiment(string trainingSetFile, string seedFile, string configFile, string resultsFile, bool validateSeed = false)
        private static void RunExperiment(string configFile, bool validateSeed = false)
        {
            Console.Write("Building Markov model...");
            
            // Load the XML configuration file
            XmlDocument xmlConfig = new XmlDocument();
            xmlConfig.Load(configFile);
            XmlElement xmlConfigElement = xmlConfig.DocumentElement;

            // Set Training File
            string trainingSetFile = XmlUtils.GetValueAsString(xmlConfigElement, "TrainingFile");

            // Create seedFile
            string seedFile = XmlUtils.GetValueAsString(xmlConfigElement, "SeedFile");

            // Create results file.
            string resultsFile = XmlUtils.GetValueAsString(xmlConfigElement, "ResultsFile");

            Console.WriteLine("\nTraining File: {0}\nSeed File: {1}\nResults File: {2}", trainingSetFile, seedFile, resultsFile);
            

            // Load the training set passwords from file
            var passwords = PasswordUtil.LoadPasswords(trainingSetFile, 8); 
            
            // 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);

            // Set the passwords to be used by the fitness evaluator.
            // These are the passwords our models will try to guess.
            // PasswordsWithAccounts is the file used for validation. Its account values won't be changed.
            PasswordCrackingEvaluator.Passwords = _experiment.Passwords;
            PasswordCrackingEvaluator.PasswordsWithAccounts = new Dictionary<string,double>(_experiment.Passwords); // Makes a deep copy

            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];

            // Validates the seed model by running it for a large number of guesses 
            if (validateSeed)
            {
                Console.WriteLine("Validating seed model...");
                var seedModel = _experiment.CreateGenomeDecoder().Decode(seed);
                ValidateModel(seedModel, _experiment.Passwords, VALIDATION_GUESSES, _experiment.Hashed);
            }

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

            // 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.UpdateEvent += new EventHandler(logEvolutionProgress);
            //_ea.UpdateScheme = new UpdateScheme(1);//.UpdateMode.
            
            // 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); }

            // Validate the resulting model.
            var decoder = _experiment.CreateGenomeDecoder();
            var champ = decoder.Decode(_ea.CurrentChampGenome);
            ValidateModel(champ, _experiment.Passwords, VALIDATION_GUESSES, _experiment.Hashed);
        }
Exemplo n.º 4
0
        // Runs a comparison of the two model types.
        static void RunAllMarkovModelPairs(object special)
        {
            const string EXPERIMENT_OFFSET = @"..\..\..\experiments\intermediate\";

            string[] models = new string[]
            {
                "first-order",
                "8-layer"
            };

            // For every dataset, create a model
            for (int i = 0; i < _datasetFilenames.Length; i++)
            {
                if (i != (int)special)
                {
                    continue;
                }
                for (int m = 0; m < 2; m++)
                {
                    int    outputs;
                    string seedFile = EXPERIMENT_OFFSET + "seed-" + models[m] + "-" + _datasetFilenames[i].Name + ".xml";
                    Console.Write("Building {0} Markov model...", models[m]);
                    if (m == 0)
                    {
                        outputs = MarkovFilterCreator.GenerateFirstOrderMarkovFilter(seedFile, _passwords[i]);
                    }
                    else
                    {
                        outputs = MarkovFilterCreator.GenerateLayeredMarkovFilter(seedFile, _passwords[i], 8);
                    }

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

                    Console.WriteLine("Loading seed...");
                    var seed = _experiment.LoadPopulation(XmlReader.Create(seedFile))[0];

                    Console.WriteLine("Creating model...");
                    var model = _experiment.CreateGenomeDecoder().Decode(seed);

                    // For every dataset, test the model
                    for (int j = 0; j < _datasetFilenames.Length; j++)
                    {
                        Console.Write("Validating {0} {1} model on {2} with {3} guesses... ", models[m], _datasetFilenames[i].Name, _datasetFilenames[j].Name, VALIDATION_GUESSES);
                        PasswordCrackingEvaluator eval = new PasswordCrackingEvaluator(VALIDATION_GUESSES, false);
                        var results = eval.Validate(model, _passwords[j], EXPERIMENT_OFFSET + models[m] + "-" + _datasetFilenames[i].Name + "-" + _datasetFilenames[j].Name + ".csv", 10000);
                        // Console.WriteLine("Accounts: {0} Uniques: {1}", results._fitness, results._alternativeFitness);
                        Console.WriteLine("Total Score: {0} Uniques: {1}", results._fitness, results._alternativeFitness);

                        lock (_writerLock)
                            using (TextWriter writer = new StreamWriter(@"..\..\..\experiments\summary_results.csv", true))
                                writer.WriteLine("{0},{1},{2},{3},{4}%,{5}%",
                                                 _datasetFilenames[i].Name,
                                                 _datasetFilenames[j].Name,
                                                 results._fitness,
                                                 results._alternativeFitness,
                                                 results._fitness / (double)_passwords[j].Sum(kv => kv.Value) * 100,
                                                 results._alternativeFitness / (double)_passwords[j].Count * 100);
                    }
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Trains a Markov model on a the training set of passwords, then evolves it against the target password database
        /// specified in the config file. At the end of the evolution, the champion model is evaluated for a larger number
        /// of guesses.
        /// </summary>
        /// <param name="trainingSetFile">The file containing the passwords from which to build the initial Markov model.</param>
        /// <param name="seedFile">The file to which the initial Markov model will be saved.</param>
        /// <param name="configFile">The file containing all the configuration parameters of the evolution.</param>
        /// <param name="resultsFile">The file to which the results will be saved at each generation.</param>
        /// <param name="validateSeed">If true, the seed model will first be validated against a large number of guesses.</param>
        //private static void RunExperiment(string trainingSetFile, string seedFile, string configFile, string resultsFile, bool validateSeed = false)
        private static void RunExperiment(string configFile, bool validateSeed = false)
        {
            Console.Write("Building Markov model...");

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

            xmlConfig.Load(configFile);
            XmlElement xmlConfigElement = xmlConfig.DocumentElement;

            // Set Training File
            string trainingSetFile = XmlUtils.GetValueAsString(xmlConfigElement, "TrainingFile");

            // Create seedFile
            string seedFile = XmlUtils.GetValueAsString(xmlConfigElement, "SeedFile");

            // Create results file.
            string resultsFile = XmlUtils.GetValueAsString(xmlConfigElement, "ResultsFile");

            Console.WriteLine("\nTraining File: {0}\nSeed File: {1}\nResults File: {2}", trainingSetFile, seedFile, resultsFile);


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

            // 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);

            // Set the passwords to be used by the fitness evaluator.
            // These are the passwords our models will try to guess.
            // PasswordsWithAccounts is the file used for validation. Its account values won't be changed.
            PasswordCrackingEvaluator.Passwords             = _experiment.Passwords;
            PasswordCrackingEvaluator.PasswordsWithAccounts = new Dictionary <string, double>(_experiment.Passwords); // Makes a deep copy

            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];

            // Validates the seed model by running it for a large number of guesses
            if (validateSeed)
            {
                Console.WriteLine("Validating seed model...");
                var seedModel = _experiment.CreateGenomeDecoder().Decode(seed);
                ValidateModel(seedModel, _experiment.Passwords, VALIDATION_GUESSES, _experiment.Hashed);
            }

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

            // 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.UpdateEvent += new EventHandler(logEvolutionProgress);
            //_ea.UpdateScheme = new UpdateScheme(1);//.UpdateMode.

            // 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);
            }

            // Validate the resulting model.
            var decoder = _experiment.CreateGenomeDecoder();
            var champ   = decoder.Decode(_ea.CurrentChampGenome);

            ValidateModel(champ, _experiment.Passwords, VALIDATION_GUESSES, _experiment.Hashed);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Trains a Markov model on a the training set of passwords, then evolves it against the target password database
        /// specified in the config file. At the end of the evolution, the champion model is evaluated for a larger number
        /// of guesses.
        /// </summary>
        /// <param name="trainingSetFile">The file containing the passwords from which to build the initial Markov model.</param>
        /// <param name="seedFile">The file to which the initial Markov model will be saved.</param>
        /// <param name="configFile">The file containing all the configuration parameters of the evolution.</param>
        /// <param name="resultsFile">The file to which the results will be saved at each generation.</param>
        /// <param name="validateSeed">If true, the seed model will first be validated against a large number of guesses.</param>
        //private static void RunExperiment(string trainingSetFile, string seedFile, string configFile, string resultsFile, bool validateSeed = false)
        private static void RunExperiment(string configFile, bool validateSeed = false)
        {
            Console.WriteLine("Removing previous champions...");
            string[] oldChampionFiles = Directory.GetFiles(@"../../../experiments/champions/", "*.xml");
            foreach (string oldChampion in oldChampionFiles)
                File.Delete(oldChampion);

            Console.Write("Building Markov model...");
            
            // Load the XML configuration file
            XmlDocument xmlConfig = new XmlDocument();
            xmlConfig.Load(configFile);
            XmlElement xmlConfigElement = xmlConfig.DocumentElement;

            // Set Training File
            string trainingSetFile = XmlUtils.GetValueAsString(xmlConfigElement, "TrainingFile");

            // Create seedFile
            string seedFile = XmlUtils.GetValueAsString(xmlConfigElement, "SeedFile");

            // Create results file.
            string resultsFile = XmlUtils.GetValueAsString(xmlConfigElement, "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, 8); 
            
            // 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);

            // Set the passwords to be used by the fitness evaluator.
            // These are the passwords our models will try to guess.
            // PasswordsWithAccounts is the file used for validation. Its account values won't be changed.
            PasswordCrackingEvaluator.Passwords = _experiment.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];

            // Validates the seed model by running it for a large number of guesses 
            if (validateSeed)
            {
                Console.WriteLine("Validating seed model...");
                var seedModel = _experiment.CreateGenomeDecoder().Decode(seed);
                ValidateModel(seedModel, _experiment.Passwords, VALIDATION_GUESSES, _experiment.Hashed);
            }

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

            // 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.UpdateEvent += new EventHandler(logEvolutionProgress);
            //_ea.UpdateScheme = new UpdateScheme(1);//.UpdateMode.
            
            // 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); }

            if (VALIDATE_ALL_STAR)
            {
                // Validate the champions of each generation.
                List<MarkovChain> championModels = new List<MarkovChain>();
                string[] championFiles = Directory.GetFiles(@"../../../experiments/champions/", "*.xml");
                foreach (string championFile in championFiles)
                {
                    var currentChamp = _experiment.LoadPopulation(XmlReader.Create(championFile))[0];
                    var champModel = _experiment.CreateGenomeDecoder().Decode(currentChamp);
                    championModels.Add(champModel);
                }
                ValidateForest(championModels, _experiment.Passwords, VALIDATION_GUESSES/championFiles.Length, _experiment.Hashed);

                // Validate a population made up of copies of the final champion.
            /*    List<MarkovChain> championCopyPop = new List<MarkovChain>();

                Console.WriteLine();
                Console.WriteLine("Validating the final champion population");
                for (int i = 0; i < MAX_GENERATIONS; i++)
                {
                    var decoder = _experiment.CreateGenomeDecoder();
                    var champ = decoder.Decode(_ea.CurrentChampGenome);
                    championCopyPop.Add(champ);
                }
                ValidateAllstarTeam(championCopyPop, _experiment.Passwords, VALIDATION_GUESSES, _experiment.Hashed);
            */
            }
            else
            {
                // Validate the resulting model.
                var decoder = _experiment.CreateGenomeDecoder();
                var champ = decoder.Decode(_ea.CurrentChampGenome);
                ValidateModel(champ, _experiment.Passwords, VALIDATION_GUESSES, _experiment.Hashed);
            }

        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Trains a Markov model on a the training set of passwords, then evolves it against the target password database
        /// specified in the config file. At the end of the evolution, the champion model is evaluated for a larger number
        /// of guesses.
        /// </summary>
        /// <param name="trainingSetFile">The file containing the passwords from which to build the initial Markov model.</param>
        /// <param name="seedFile">The file to which the initial Markov model will be saved.</param>
        /// <param name="configFile">The file containing all the configuration parameters of the evolution.</param>
        /// <param name="resultsFile">The file to which the results will be saved at each generation.</param>
        /// <param name="validateSeed">If true, the seed model will first be validated against a large number of guesses.</param>
        //private static void RunExperiment(string trainingSetFile, string seedFile, string configFile, string resultsFile, bool validateSeed = false)
        private static void RunExperiment(string configFile, bool validateSeed = false)
        {
            Console.WriteLine("Removing previous champions...");
            string[] oldChampionFiles = Directory.GetFiles(@"../../../experiments/champions/", "*.xml");
            foreach (string oldChampion in oldChampionFiles)
            {
                File.Delete(oldChampion);
            }

            Console.Write("Building Markov model...");

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

            xmlConfig.Load(configFile);
            XmlElement xmlConfigElement = xmlConfig.DocumentElement;

            // Set Training File
            string trainingSetFile = XmlUtils.GetValueAsString(xmlConfigElement, "TrainingFile");

            // Create seedFile
            string seedFile = XmlUtils.GetValueAsString(xmlConfigElement, "SeedFile");

            // Create results file.
            string resultsFile = XmlUtils.GetValueAsString(xmlConfigElement, "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, 8);

            // 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);

            // Set the passwords to be used by the fitness evaluator.
            // These are the passwords our models will try to guess.
            // PasswordsWithAccounts is the file used for validation. Its account values won't be changed.
            PasswordCrackingEvaluator.Passwords = _experiment.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];

            // Validates the seed model by running it for a large number of guesses
            if (validateSeed)
            {
                Console.WriteLine("Validating seed model...");
                var seedModel = _experiment.CreateGenomeDecoder().Decode(seed);
                ValidateModel(seedModel, _experiment.Passwords, VALIDATION_GUESSES, _experiment.Hashed);
            }

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

            // 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.UpdateEvent += new EventHandler(logEvolutionProgress);
            //_ea.UpdateScheme = new UpdateScheme(1);//.UpdateMode.

            // 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);
            }

            if (VALIDATE_ALL_STAR)
            {
                // Validate the champions of each generation.
                List <MarkovChain> championModels = new List <MarkovChain>();
                string[]           championFiles  = Directory.GetFiles(@"../../../experiments/champions/", "*.xml");
                foreach (string championFile in championFiles)
                {
                    var currentChamp = _experiment.LoadPopulation(XmlReader.Create(championFile))[0];
                    var champModel   = _experiment.CreateGenomeDecoder().Decode(currentChamp);
                    championModels.Add(champModel);
                }
                ValidateForest(championModels, _experiment.Passwords, VALIDATION_GUESSES / championFiles.Length, _experiment.Hashed);

                // Validate a population made up of copies of the final champion.

                /*    List<MarkovChain> championCopyPop = new List<MarkovChain>();
                 *
                 *  Console.WriteLine();
                 *  Console.WriteLine("Validating the final champion population");
                 *  for (int i = 0; i < MAX_GENERATIONS; i++)
                 *  {
                 *      var decoder = _experiment.CreateGenomeDecoder();
                 *      var champ = decoder.Decode(_ea.CurrentChampGenome);
                 *      championCopyPop.Add(champ);
                 *  }
                 *  ValidateAllstarTeam(championCopyPop, _experiment.Passwords, VALIDATION_GUESSES, _experiment.Hashed);
                 */
            }
            else
            {
                // Validate the resulting model.
                var decoder = _experiment.CreateGenomeDecoder();
                var champ   = decoder.Decode(_ea.CurrentChampGenome);
                ValidateModel(champ, _experiment.Passwords, VALIDATION_GUESSES, _experiment.Hashed);
            }
        }