-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpwdsearch.py
executable file
·101 lines (80 loc) · 2.82 KB
/
pwdsearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Nikita A. Medvedev <[email protected]>.
import os
import sys
if sys.version_info < (3, 0):
sys.stdout.write("Sorry, this tool requires Python 3.x\n")
sys.exit(1)
import re
import codecs
# Set accurace for Levenshtein distance. Less more accurate.
accuracy = 2
search_terms = []
# Help message:
if len(sys.argv) >= 2:
first_arg = sys.argv[1]
if '-h' == first_arg or '--help' == first_arg:
help_message = \
'''Tool searching for different default passwords. It uses Levenshtein distance for fuzzy search.
Usage:
pwdsearch.py --- print all passwords in csv and GoGrepYourself.
pwdsearch.py tp-link --- search for all passwords related to TP-Link devices (or similar word).
Hint: Use csvlook from csvkit to beautify csv in console.
1) Install csvkit:
sudo apt install csvkit
or
sudo pip install csvkit
2) And then:
pwdsearch.py tp-link | csvlook
'''
print(help_message)
exit()
else:
for a in sys.argv[1:]:
print('Searching for: ' + a, file=sys.stderr)
# Calculates the Levenshtein distance between a and b.
def distance(a, b):
n, m = len(a), len(b)
if n > m:
# Make sure n <= m, to use O(min(n,m)) space
a, b = b, a
n, m = m, n
if m-n > 3:
return(100)
current_column = range(n+1) # Keep current and previous column, not entire matrix
for i in range(1, m+1):
previous_column, current_column = current_column, [i]+[0]*n
for j in range(1,n+1):
add, delete, change = previous_column[j]+1, current_column[j-1]+1, previous_column[j-1]
if a[j-1] != b[i-1]:
change += 1
current_column[j] = min(add, delete, change)
return current_column[n]
# Get curr dir name:
dirname, filename = os.path.split(os.path.abspath(__file__))
# Main script:
res = []
with codecs.open(dirname + '/default-passwords.csv', "r",encoding='utf-8', errors='ignore') as f:
header = f.readline().strip()
body = f.read().encode("ascii", errors="ignore").decode().split('\r\n')
if len(sys.argv) <= 1:
print(header)
for p in body: print(p)
exit()
find_term = sys.argv[1]
find_term = find_term.lower()
for line in body:
for word in re.split(r';|,|\s|\*|"|\'|`|!|@|\$|%|\^|/|\.|\\\\|=|_| ', line):
dist = distance(word.lower().replace('-', ''), find_term.replace('-', ''))
if dist <= accuracy:
res.append(str(dist) + '\t' + line)
break
# Linux "head" tool bug fix. There was some problem while interrupting output (BrokenPipeError).
try:
res = sorted(res)
print(header)
for l in res:
print(l.split('\t')[1])
except (BrokenPipeError, IOError):
pass