public void UpdateSimulatorState(Simulator simulator, SimIpHistory ipHistory)
        {
            IsRepeatFailure = !IsPasswordValid && (
                (SimAccount == null)
                    ? simulator._recentIncorrectPasswords.AddMember(UserNameOrAccountId + "\n" + Password)
                    : simulator._userAccountController.AddIncorrectPhaseTwoHash(SimAccount, Password, TimeOfAttemptUtc)
                );

            int passwordsHeightOnBinomialLadder = (IsPasswordValid || IsRepeatFailure)
                ? simulator._binomialLadderFilter.GetHeight(Password)
                : simulator._binomialLadderFilter.Step(Password);

            IsFrequentlyGuessedPassword = passwordsHeightOnBinomialLadder >=
                                          simulator._experimentalConfiguration.BlockingOptions.BinomialLadderFrequencyThreshdold_T;

            DeviceCookieHadPriorSuccessfulLoginForThisAccount = SimAccount != null &&
                                                                simulator._userAccountController.HasClientWithThisHashedCookieSuccessfullyLoggedInBefore(SimAccount, CookieProvidedByBrowser);

            if (SimAccount != null && IsPasswordValid)
            {
                ipHistory.AdjustBlockingScoreForPastTyposTreatedAsFullFailures(simulator, SimAccount, TimeOfAttemptUtc,
                                                                               Password);
                simulator._userAccountController.RecordHashOfDeviceCookieUsedDuringSuccessfulLoginBackground(
                    SimAccount, CookieProvidedByBrowser, TimeOfAttemptUtc);
                SimAccount.ConsecutiveIncorrectAttempts.SetValue(0, this.TimeOfAttemptUtc);
            }
            else if (SimAccount != null && !IsRepeatFailure)
            {
                SimAccount.ConsecutiveIncorrectAttempts.AddInPlace(
                    simulator._experimentalConfiguration.BlockingOptions.BlockScoreHalfLife, 1d,
                    this.TimeOfAttemptUtc);
                if (SimAccount.ConsecutiveIncorrectAttempts.GetValue(
                        simulator._experimentalConfiguration.BlockingOptions.BlockScoreHalfLife)
                    >
                    SimAccount.MaxConsecutiveIncorrectAttempts.GetValue(
                        simulator._experimentalConfiguration.BlockingOptions.BlockScoreHalfLife))
                {
                    SimAccount.MaxConsecutiveIncorrectAttempts.SetValue(SimAccount.ConsecutiveIncorrectAttempts);
                }
            }

            if (!IsPasswordValid && !IsRepeatFailure && SimAccount != null)
            {
                ipHistory.RecentPotentialTypos.Add(new SimLoginAttemptSummaryForTypoAnalysis()
                {
                    WhenUtc             = TimeOfAttemptUtc,
                    Password            = Password,
                    UsernameOrAccountId = UserNameOrAccountId,
                    WasPasswordFrequent = IsFrequentlyGuessedPassword
                });
            }


            DecayingDouble decayingOneFromThisInstant = new DecayingDouble(1, TimeOfAttemptUtc);
            TimeSpan       halfLife = simulator._experimentalConfiguration.BlockingOptions.BlockScoreHalfLife;

            if (IsPasswordValid)
            {
                ipHistory.SuccessfulLogins.AddInPlace(halfLife, decayingOneFromThisInstant);
            }
            else if (SimAccount == null)
            {
                if (IsRepeatFailure)
                {
                    if (IsFrequentlyGuessedPassword)
                    {
                        ipHistory.RepeatAccountFailuresFrequentPassword.AddInPlace(halfLife, decayingOneFromThisInstant);
                    }
                    else
                    {
                        ipHistory.RepeatAccountFailuresInfrequentPassword.AddInPlace(halfLife, decayingOneFromThisInstant);
                    }
                }
                else
                {
                    if (IsFrequentlyGuessedPassword)
                    {
                        ipHistory.AccountFailuresFrequentPassword.AddInPlace(halfLife, decayingOneFromThisInstant);
                    }
                    else
                    {
                        ipHistory.AccountFailuresInfrequentPassword.AddInPlace(halfLife, decayingOneFromThisInstant);
                    }
                }
            }
            else
            {
                if (IsRepeatFailure)
                {
                    if (IsFrequentlyGuessedPassword)
                    {
                        ipHistory.RepeatPasswordFailuresNoTypoFrequentPassword.AddInPlace(halfLife, decayingOneFromThisInstant);
                    }
                    else
                    {
                        ipHistory.RepeatPasswordFailuresNoTypoInfrequentPassword.AddInPlace(halfLife, decayingOneFromThisInstant);
                    }
                }
                else
                {
                    if (IsFrequentlyGuessedPassword)
                    {
                        ipHistory.PasswordFailuresNoTypoFrequentPassword.AddInPlace(halfLife, decayingOneFromThisInstant);
                    }
                    else
                    {
                        ipHistory.PasswordFailuresNoTypoInfrequentPassword.AddInPlace(halfLife, decayingOneFromThisInstant);
                    }
                }
            }
        }
Beispiel #2
0
        public async Task RunAsync()
        {
            _logger.WriteStatus("In RunInBackground");

            //_logger.WriteStatus("Priming password-tracking with known common passwords");
            _simPasswords.PrimeWithKnownPasswordsAsync(_binomialLadderFilter, 40);
            //_logger.WriteStatus("Finished priming password-tracking with known common passwords");

            //_logger.WriteStatus("Creating IP Pool");
            _ipPool = new IpPool(_experimentalConfiguration);
            //_logger.WriteStatus("Generating simualted account records");
            _simAccounts = new SimulatedAccounts(_ipPool, _simPasswords, _logger);
            _simAccounts.Generate(_experimentalConfiguration);

            //_logger.WriteStatus("Creating login-attempt generator");
            _attemptGenerator = new SimulatedLoginAttemptGenerator(_experimentalConfiguration, _simAccounts, _ipPool,
                                                                   _simPasswords);
            _logger.WriteStatus("Finiished creating login-attempt generator");

            FricSimulator fri = new FricSimulator();

            _logger.WriteStatus("   ");
            _logger.WriteStatus("   ");
            _logger.WriteStatus("Click Enter To First Testing Step");
            _logger.WriteStatus("   ");
            _logger.WriteStatus("   ");
            Console.Read();

            await fri.RunAsync(_logger, _ipPool);

            _logger.WriteStatus("   ");
            _logger.WriteStatus("   ");
            _logger.WriteStatus("Click Enter To Second Testing Step");
            _logger.WriteStatus("   ");
            _logger.WriteStatus("   ");
            Console.Read();
            Console.Read();

            _logger.WriteStatus("Running Password File to check");
            _logger.WriteStatus("   ");

            foreach (
                ConcurrentStreamWriter writer in
                new[]
                { _Attempts })
            {
                lock (writer)
                {
                    writer.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",
                                                   "UserID",
                                                   "IP",
                                                   "IsFrequentlyGuessedPw",
                                                   "IsPasswordCorrect",
                                                   "IsFromAttackAttacker",
                                                   "IsAGuess"
                                                   ));
                }
            }

            TimeSpan testTimeSpan       = _experimentalConfiguration.TestTimeSpan;
            double   ticksBetweenLogins = ((double)testTimeSpan.Ticks) /
                                          (double)_experimentalConfiguration.TotalLoginAttemptsToIssue;

            _experimentalConfiguration.TotalLoginAttemptsToIssue = 30000;
            int interlockedCount = 0;

            Parallel.For(0L, (long)_experimentalConfiguration.TotalLoginAttemptsToIssue, (count, pls) =>
            {
                interlockedCount = Interlocked.Add(ref interlockedCount, 1);
                if (interlockedCount % 10000 == 0)
                {
                    _logger.WriteStatus("Login Attempt {0:N0}", interlockedCount);
                }
                DateTime eventTimeUtc = StartTimeUtc.AddTicks((long)(ticksBetweenLogins * interlockedCount));
                SimulatedLoginAttempt simAttempt;
                if (StrongRandomNumberGenerator.GetFraction() <
                    _experimentalConfiguration.FractionOfLoginAttemptsFromAttacker)
                {
                    switch (_experimentalConfiguration.AttackersStrategy)
                    {
                    case ExperimentalConfiguration.AttackStrategy.UseUntilLikelyPopular:
                        simAttempt =
                            _attemptGenerator.MaliciousLoginAttemptBreadthFirstAvoidMakingPopular(eventTimeUtc);
                        break;

                    case ExperimentalConfiguration.AttackStrategy.Weighted:
                        simAttempt = _attemptGenerator.MaliciousLoginAttemptWeighted(eventTimeUtc);
                        break;

                    case ExperimentalConfiguration.AttackStrategy.BreadthFirst:
                    default:
                        simAttempt = _attemptGenerator.MaliciousLoginAttemptBreadthFirst(eventTimeUtc);
                        break;
                    }
                }
                else
                {
                    simAttempt = _attemptGenerator.BenignLoginAttempt(eventTimeUtc);
                }


                SimIpHistory ipHistory = _ipHistoryCache.GetOrAdd(simAttempt.AddressOfClientInitiatingRequest,
                                                                  (ip) => new SimIpHistory(
                                                                      _experimentalConfiguration.BlockingOptions
                                                                      .NumberOfFailuresToTrackForGoingBackInTimeToIdentifyTypos));
                double[] scores = ipHistory.GetAllScores(_experimentalConfiguration.BlockingOptions.BlockScoreHalfLife,
                                                         simAttempt.TimeOfAttemptUtc);

                simAttempt.UpdateSimulatorState(this, ipHistory);

                double decayingInvalidPasswordAttempts = 0d;
                if (simAttempt.IsPasswordValid)
                {
                    DecayingDouble incorrectPasswordAttempts;
                    if (_incorrectPasswordCounts.TryGetValue(simAttempt.Password, out incorrectPasswordAttempts))
                    {
                        decayingInvalidPasswordAttempts = incorrectPasswordAttempts.GetValue(_experimentalConfiguration.BlockingOptions.BlockScoreHalfLife, simAttempt.TimeOfAttemptUtc);
                    }
                }
                else
                {
                    decayingInvalidPasswordAttempts = 1d;
                    DecayingDouble incorrectPasswordAttempts;
                    if (_incorrectPasswordCounts.TryGetValue(simAttempt.Password, out incorrectPasswordAttempts))
                    {
                        decayingInvalidPasswordAttempts += incorrectPasswordAttempts.GetValue(_experimentalConfiguration.BlockingOptions.BlockScoreHalfLife, simAttempt.TimeOfAttemptUtc);
                    }
                    _incorrectPasswordCounts[simAttempt.Password] = new DecayingDouble(decayingInvalidPasswordAttempts, simAttempt.TimeOfAttemptUtc);
                }

                var ipInfo          = _ipPool.GetIpAddressDebugInfo(simAttempt.AddressOfClientInitiatingRequest);
                string outputString = string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",
                                                    simAttempt.SimAccount?.UsernameOrAccountId ?? "<null>",
                                                    simAttempt.AddressOfClientInitiatingRequest,
                                                    simAttempt.IsFrequentlyGuessedPassword ? "Frequent" : "Infrequent",
                                                    simAttempt.IsPasswordValid ? "Correct" : "Incorrect",
                                                    simAttempt.IsFromAttacker ? "FromAttacker" : "FromUser",
                                                    simAttempt.IsGuess ? "IsGuess" : "NotGuess",
                                                    simAttempt.IsFromAttacker
                       ? (ipInfo.UsedByBenignUsers ? "IsInBenignPool" : "NotUsedByBenign")
                       : (ipInfo.UsedByAttackers ? "IsInAttackersIpPool" : "NotUsedByAttacker"),
                                                    ipInfo.IsPartOfProxy ? "ProxyIP" : "NotAProxy",
                                                    string.IsNullOrEmpty(simAttempt.MistakeType) ? "-" : simAttempt.MistakeType,
                                                    decayingInvalidPasswordAttempts,
                                                    simAttempt.SimAccount?.MaxConsecutiveIncorrectAttempts.GetValue(_experimentalConfiguration.BlockingOptions.BlockScoreHalfLife, simAttempt.TimeOfAttemptUtc) ?? 0d,
                                                    string.Join("\t", scores.Select(s => s.ToString(CultureInfo.InvariantCulture)).ToArray())
                                                    );


                _Attempts.WriteLine(outputString);
                _logger.WriteStatus(outputString);
                Thread.Sleep(1300);
            });
            foreach (
                ConcurrentStreamWriter writer in
                new[]
                { _Attempts })
            {
                writer.Close();
            }
        }