Last active
December 23, 2015 05:59
-
-
Save dmiedema/6590918 to your computer and use it in GitHub Desktop.
Searches auth.log for failed logins and adds them to hosts.deny. Probably a terrible way to do it, and it's potentially a massive failure. But i'm sick of fail2ban not banning
This file contains 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 python | |
authlog=open('/var/log/auth.log','r') | |
lines = [] | |
for line in authlog: | |
lines.append(line) | |
authlog.close() | |
failedAttempts = [] | |
for line in lines: | |
try: | |
x = line.split('Failed password for')[1] | |
except IndexError: | |
1 + 1 | |
else: | |
failedAttempts.append(x) | |
ips = [] | |
for failedAttempt in failedAttempts: | |
attempt = failedAttempt.split(' ')[1] | |
if attempt == 'invalid': | |
ips.append(failedAttempt.split(' ')[5]) | |
elif attempt == 'root': | |
ips.append(failedAttempt.split(' ')[3]) | |
else: | |
2 + 4 | |
ips = sorted(set(ips)) | |
# hostsDeny line 20 is start of IPs | |
hostsDeny=open('/etc/hosts.deny', 'r') | |
tempdenys = [] | |
for line in hostsDeny: | |
tempdenys.append(line) | |
# close hostsdeny | |
hostsDeny.close() | |
denys = [] | |
newIPs = [] | |
for x in range(20, len(tempdenys)): | |
denys.append(tempdenys[x]) | |
hostsDenyWrite = file('/etc/hosts.deny', 'a') | |
for ip in ips: | |
ipCheck = ip + '\n' | |
if ipCheck not in denys: | |
print ip | |
hostsDenyWrite.write(ip + '\n') | |
hostsDenyWrite.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment