FindElection() public static method

public static FindElection ( string host, int port, CookieCollection cookieCollection, string electionId ) : Election
host string
port int
cookieCollection System.Net.CookieCollection
electionId string
return Electro.Model.Election
示例#1
0
        public static int ProcessGet(string host, string id, string flag)
        {
            log.Info("Processing Vuln2.Get");

            var state = JsonHelper.ParseJson <Vuln2State>(Convert.FromBase64String(id));

            log.InfoFormat("Looking for Election {0}", state.ElectionId);
            var election = ElectroClient.FindElection(host, Program.PORT, state.Voter.Cookies, state.ElectionId);

            if (election == null)
            {
                throw new ServiceException(ExitCode.CORRUPT, string.Format("Can't find election '{0}'", id));
            }
            log.InfoFormat("Election {0} found", state.ElectionId);

            var gotFlag = ExtractFlag(election, state.PrivateKey, flag.Length);

            if (flag != gotFlag)
            {
                throw new ServiceException(ExitCode.CORRUPT, string.Format("Invalid flag! Got '{0}' instead of expected '{1}'", gotFlag, flag));
            }

            log.Info("Flag found! OK");
            return((int)ExitCode.OK);
        }
示例#2
0
        public static int ProcessGet(string host, string id, string flag)
        {
            log.Info("Processing Vuln1.Get");

            var state = JsonHelper.ParseJson <Vuln1State>(Convert.FromBase64String(id));

            var now            = DateTime.UtcNow;
            var elapsedSeconds = now.Subtract(state.ElectionStartDate).TotalMilliseconds;

            if (elapsedSeconds < 0)
            {
                throw new ServiceException(ExitCode.CHECKER_ERROR, string.Format("Possible time desynchronization on checksystem hosts! Election started in future: '{0}' and now is only '{1}'", state.ElectionStartDate.ToSortable(), now.ToSortable()));
            }

            var nominateEndTime = state.ElectionStartDate.AddSeconds(nominateTimeInSec);
            var voteEndTime     = state.ElectionStartDate.AddSeconds(nominateTimeInSec + voteTimeInSec);

            log.InfoFormat("Looking for Election {0}", state.ElectionId);
            var election = ElectroClient.FindElection(host, Program.PORT, state.Candidates[0].Cookies, state.ElectionId);

            if (election == null || election.Candidates == null || election.Candidates.Count < 2)
            {
                throw new ServiceException(ExitCode.CORRUPT, string.Format("Can't find election '{0}' or it has less than 2 candidates", state.ElectionId));
            }
            log.InfoFormat("Election {0} found", state.ElectionId);

            log.InfoFormat("Election startDt {0}", state.ElectionStartDate.ToSortable());
            log.InfoFormat("Nominate end Dt  {0}", nominateEndTime.ToSortable());
            log.InfoFormat("Vote end Dt      {0}", voteEndTime.ToSortable());
            log.InfoFormat("Now              {0}", now.ToSortable());

            if (now < nominateEndTime)
            {
                log.InfoFormat("Nomination is still going, got election, considering everything OK");
                return((int)ExitCode.OK);
            }
            else if (now < voteEndTime)
            {
                log.InfoFormat("Nomination finished, but voting is still going. Trying to win!");
                int notFlagNum = 0;
                for (; notFlagNum < election.Candidates.Count; notFlagNum++)
                {
                    if (election.Candidates[notFlagNum] != null && !election.Candidates[notFlagNum].IsMe)
                    {
                        break;
                    }
                }
                if (notFlagNum == election.Candidates.Count)
                {
                    throw new ServiceException(ExitCode.CORRUPT, string.Format("Can't find candidate with no flag in election '{0}'", state.ElectionId));
                }

                var random      = new Random();
                var votersCount = random.Next(votesMinCount, votesMaxCount + 1);

                var votesForWinner =
                    Enumerable.Repeat(Utils.GenVoteVector(election.Candidates.Count, notFlagNum), (votersCount / 2) + 1).ToArray();
                var restVotes = Utils.GenRandomVoteVectors(election.Candidates.Count, votersCount - votesForWinner.Length).ToArray();
                var votes     = votesForWinner.Concat(restVotes).ToArray();

                var voters = Vuln2Methods.RegisterVoters(host, votes, state.Candidates);
                Vote(host, voters, election.Id, election.PublicKey);
                return((int)ExitCode.OK);
            }
            else
            {
                log.InfoFormat("Voting has already finished. Considering everything OK");
                var realWinner = election.FindWinner();
                if (realWinner == null)
                {
                    throw new ServiceException(ExitCode.MUMBLE, string.Format("Can't find winner in election '{0}'", election.Id));
                }

                var winner = state.Candidates.FirstOrDefault(info => info.Login == realWinner.Name);
                if (winner == null)
                {
                    throw new ServiceException(ExitCode.CORRUPT, string.Format("We have no credentials for winner in election '{0}'. Possibly hacker won, so we lost a flag", election.Id));
                }

                log.InfoFormat("Reloading election, now as winner '{0}'", winner.Login);
                election = ElectroClient.FindElection(host, Program.PORT, winner.Cookies, election.Id.ToString());
                if (election == null)
                {
                    throw new ServiceException(ExitCode.CORRUPT, string.Format("Can't find election '{0}'", state.ElectionId));
                }

                if (election.Candidates.All(info => info.PrivateNotesForWinner != flag))
                {
                    throw new ServiceException(ExitCode.CORRUPT, "Can't find flag", null);
                }

                log.Info("Flag found! Ok");
                return((int)ExitCode.OK);
            }
        }