static void Main(string[] args) { ///////////////////////////////////////////////////////////////////////////////////////// // Below are some examples of possible experiments and functions you may wish to call. // ///////////////////////////////////////////////////////////////////////////////////////// // Morph an english word dictionary into a password database where 10% of passwords have a number // ToyProblemUtil.MorphEnglish(ENGLISH_WORDS, MORPHED_ENGLISH_WORDS); // Morph an english word dictionary into a password database where all passwords are at least 8 characters // and contain at least one number // ToyProblemUtil.MorphEnglish(ENGLISH_WORDS, FORCED_MORPHED_ENGLISH_WORDS, requireDigit: true, minLength: 8); // Train on the no-rule morphed english words db and evolve against the morphed english db with // the digit and length creation rules enforced. // RunExperiment(MORPHED_ENGLISH_WORDS, MORPHED_SEED_FILE, MORPHED_CONFIG_FILE, MORPHED_RESULTS_FILE, false); //Train on the phppb dataset and evolve against the rockyou dataset //RunExperiment(PHPBB_DATASET, PHPBB_SEED_FILE, PHPBB_CONFIG_FILE, PHPBB_RESULTS_FILE, false); //Train on the toyDistribution dataset and evolve against the toyDistribution dataset //RunExperiment(TOY_DISTRIBUTION_CONFIG_FILE, false); // Print some summary statistics about the distribution of passwords in the two morphed english dictionaries. // PasswordUtil.PrintStats(@"../../../passwords/morphed_english.txt"); // no creation rules // PasswordUtil.PrintStats(@"../../../passwords/forced_morphed_english.txt"); // digit and length rules // Run a really big analysis comparing the first-order Markov model to an 8-layered one. // PrepareMarkovModelRuns(); // Parallel.For(0, _datasetFilenames.Length, i => RunAllMarkovModelPairs(i)); // Check if a database of hashed passwords contains some common passwords (check for creation rules) // MD5HashChecker md5 = new MD5HashChecker(@"../../../passwords/stratfor_hashed.txt"); // md5.PrintCounts(); // Load the training set passwords from file var passwords = PasswordUtil.LoadPasswords(@"/Users/Wesley/Projects/password-evolution/passwords/morphed_english.txt", 8); // Create a Markov model from the passwords. This model will be used // as our seed for the evolution. int outputs = MarkovFilterCreator.GenerateFirstOrderMarkovFilter( @"/Users/Wesley/Projects/password-evolution/models/supervised/morphed_english.xml", passwords); Console.WriteLine("Outputs: {0}", outputs); }
// 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); } } } }
/// <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); }
/// <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); } }