Last active
March 5, 2017 21:03
-
-
Save 0xa/3f719ece2e518841f4a6caf75fab1019 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
ROBCO Industries (TM) Password Recovery Tool | |
---- | |
Next-generation hacking assistant for your RobCo Pip-Boy 3000. | |
> Words: seven peace broke small steal agree deity armed store empty codes brink | |
> | |
> try: peace (6 matches) | |
> score? [likeness n or Ctrl-C] 1 | |
> OK, updating constraints: {'peace': 1} | |
> 1 constraints, 6 words left | |
> | |
> try: agree (3 matches) | |
> score? [likeness n or Ctrl-C] 1 | |
> OK, updating constraints: {'peace': 1, 'agree': 1} | |
> 2 constraints, 3 words left | |
> | |
> try: seven (1 matches) | |
> score? [likeness n or Ctrl-C] 0 | |
> OK, updating constraints: {'peace': 1, 'agree': 1, 'seven': 0} | |
> 3 constraints, 1 words left | |
> | |
> Password: broke | |
""" | |
import re | |
def sim(a, b): | |
assert len(a) == len(b) | |
return sum(1 for (ac, bc) in zip(a, b) if ac == bc) | |
def filter_words(words, constraints): | |
return [w for w in words if all(sim(w, c[0]) == c[1] for c in constraints.items())] | |
def find_middle(words): | |
results = [] | |
for w in words: | |
sims = [(x, sim(w, x)) for x in words if x != w] | |
sims = [s for s in sims if s[1] > 0] | |
results.append((w, len(sims), sims)) | |
closest = min(results, key=lambda x:abs(x[1]-len(words)/2)) | |
return closest | |
def main(): | |
print() | |
print("Welcome to ROBCO Industries (TM) Password Recovery Tool") | |
print() | |
print("Words: ", end="") | |
words = input().lower() | |
words = re.sub('[^a-z]', ' ', words) | |
words = re.split(' +', words) | |
print() | |
constraints = {} | |
while True: | |
subset = filter_words(words, constraints) | |
if len(subset) == 0: | |
print("No words left, impossible constraints.") | |
break | |
if len(subset) == 1: | |
print("Password: %s" % subset[0]) | |
break | |
word, n_matches, matches = find_middle(subset) | |
print("try: %s (%d matches)" % (word, n_matches)) | |
print("score? [likeness n or Ctrl-C] ", end="") | |
likeness = int(input()) | |
constraints[word] = likeness | |
subset = filter_words(words, constraints) | |
print("OK, updating constraints: %r" % constraints) | |
print("%d constraints, %d words left" % (len(constraints), len(subset))) | |
print() | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment