http://www.perlmonks.org?node_id=691549

I want you to write malicious election software in Perl!

Rules:

Your program should:
  1. Present a list of candidates (feel free to fill the ballot with whatever names you like) and instructions on how to enter votes.
  2. Read votes from STDIN, according to the instructions.
  3. After voting is finished (by entering a special command, or closing STDIN), display the final vote count for each candidate.
You should strive for the following virtues of malicious election software:
Covertness

The program's source code should look completely benign. In other words, it should appear to correctly count the given votes, according to the instructions printed out, and not appear to do anything else suspicious. It should not appear intentionally obfuscated. You want this code to pass a source inspection and get included in election equipment.*

Likewise, the results of the election should be plausible. If all votes go to one candidate, or the number of votes is vastly different than what was entered, people will get suspicious!

Deviousness / Ingenuity

When executed, the program should definitely not be benign, but instead rig the election! Be creative in how you tamper with the election. Some example ideas:

  • Steal occasional votes from one candidate and give them to your preferred candidate
  • Always ensure that your favorite candidate wins by a slight margin
  • Always force a tie
  • Elect yourself as a write-in candidate
  • Include a backdoor triggered by malformed input
  • Randomly assign votes to candidates, ignoring all actual submitted votes.

It should be quite a challenge to achieve both of these virtues simultaneously. In essence, this is an obfuscation challenge, but only the malicious intent should be obfuscated; the rest of the code should look unobfuscated.

Some other devious ideas:

Misc:

This was inspired by this obfuscation contest. You might be inspired by some of the submissions. Some of the top submissions were very good, and it's hard to see where the votes are getting screwed up, even if you know the code has a hole somewhere. That contest used C, but of course, I encourage clever ideas that are unique to Perl.

If you feel like including them, put hints and post-mortem analyses in <spoiler> tags.

Sample code:

Here is a sample program that counts votes correctly -- of course, yours shouldn't:
use strict; use warnings; my %ballot = qw( Obama 0 McCain 0 Barr 0 blokhead 0); print "Available candidates: @{[ keys %ballot ]}\n"; print "To cast a vote, type candidate's full name. End the election wi +th ^D\n"; while (<>) { chomp( my $vote = $_ ); if ( exists $ballot{$vote} ) { $ballot{$vote}++; } else { warn "Invalid candidate!\n"; } } print "Final results:\n"; printf "%10s : %d\n", $_, $ballot{$_} for sort { $ballot{$b} <=> $ballot{$a} } keys %ballot;
Feel free to deviate from my sample code in how you store the votes, expect the votes to be given in STDIN, display the results, etc..

*: You'll have to use your imagination and pretend that voting machine companies actually review their code.

blokhead