/
passwd_checker
executable file
·174 lines (145 loc) · 4.34 KB
/
passwd_checker
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
# Try running it like this from bash:
# ./passwd.py -j200 -pbo "$(date +%s_%c)" 123.456.789.{1..255} 2>/dev/null
# BUGS/TODO:
# - specifying -p without -o causes stdout display issues due to line length
# - should check for shells not /bin/false or /sbin/nologin
# - implement two pools, one for network requests and another for processing?
# - sort output by IP or username; or output in CSV-compatible format
from argparse import ArgumentParser
from multiprocessing import cpu_count, RLock
from multiprocessing.dummy import Pool
from os import getpid, kill
from re import compile
from signal import SIGKILL
from subprocess import check_output, CalledProcessError
from sys import stdout
from time import sleep, time
PASSWD_FILE = "/etc/passwd"
PASSWD_DELIM = ":"
# Varies depending on what services are installed by default.
GOOD_USERS = [
"_apt",
"avahi",
"avahi-autoipd",
"backup",
"bin",
"colord",
"daemon",
"dnsmasq",
"games",
"gnats",
"hplip",
"irc",
"kernoops",
"lightdm",
"list",
"lp",
"mail",
"man",
"messagebus",
"mongodb",
"news",
"nobody",
"postfix",
"proxy",
"pulse",
"root",
"rtkit",
"saned",
"service",
"speech-dispatcher",
"sshd",
"statd",
"sync",
"sys",
"syslog",
"systemd-bus-proxy",
"systemd-network",
"systemd-resolve",
"systemd-timesync",
"usbmux",
"uucp",
"uuidd",
"whoopsie",
"www-data"
]
# This might potentially discard possible suspects!
GOOD_REGEXES = [
r"guest-.{6}"
]
complete = 0
cLock = RLock()
outfile = stdout
start_time = time()
def exit_hook():
print("Finished {} items in {:.3f}s".format(complete, time() - start_time))
outfile.close()
def compile_regexes(glob_list):
for i in range(0, len(glob_list)):
glob_list[i] = compile(glob_list[i])
def add_complete(num=1):
global complete
with cLock:
complete += num
def get_bad_users(file_contents):
ret = []
for line in file_contents.splitlines():
username = line.split(PASSWD_DELIM, 1)[0]
if username not in GOOD_USERS and \
not all(r.match(username) for r in GOOD_REGEXES):
ret.append(username)
return ret
def main(ip):
passwd_contents = ""
try:
passwd_contents = check_output(
cmd_head + [ip, "cat", PASSWD_FILE]).decode()
except CalledProcessError:
add_complete()
return
for bu in get_bad_users(passwd_contents):
print("[{}] {}".format(ip, bu), file=outfile)
add_complete()
if __name__ == '__main__':
p = ArgumentParser(description="Find unexpected users on remote machines.")
p.add_argument('--batch', '-b', action='store_true',
help="prevent prompting for password")
p.add_argument('--identity', '-i', action='store', metavar="identity_file",
help="specify identity file")
p.add_argument('--jobs', '-j', action='store', metavar="jobs",
default=cpu_count(), help="number of parallel jobs")
p.add_argument('--login', '-l', action='store', metavar='login_name',
help="pass login name to OpenSSH client")
p.add_argument('--outfile', '-o', action='store', metavar="output_file",
help="specify output file")
p.add_argument('--progress', '-p', action='store_true',
help="indicate progress")
p.add_argument('ip', nargs='+', action='append',
help="IP of machine to test")
args = p.parse_args()
numitems = len(args.ip[0])
cmd_head = ["ssh"]
if args.batch:
cmd_head += ["-oBatchMode=yes"]
if args.login:
cmd_head += ["-l", args.login]
if args.identity:
cmd_head += ["-i", args.identity]
if args.outfile:
outfile = open(args.outfile, "w")
compile_regexes(GOOD_REGEXES)
with Pool(int(args.jobs)) as p:
p.map_async(main, args.ip[0])
try:
while complete < numitems:
if args.progress:
print("Done {} of {} in {:.3f}s".format(
complete, numitems, time() - start_time), end="\r")
sleep(1)
p.close()
exit_hook()
except KeyboardInterrupt:
p.terminate()
exit_hook()
kill(getpid(), SIGKILL)