-
Notifications
You must be signed in to change notification settings - Fork 0
/
bruteban.py
73 lines (64 loc) · 2.63 KB
/
bruteban.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
import socket
import os
import subprocess
import re
from jail.jails import Jails
matchess = {'SSHD': 'sshd.service'}
class BruteBan:
def __init__(self):
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._path = os.getcwd() + '/'
#Получает все активные инструкции
self._jails = Jails(self._path).get_options()
#После добавления измненения команды journcalctl изменить этот метод
def journalctl_option(self,instruction):
return instruction['log_path'] == 'journalctl'
def search_for_matches(self,name):
if name in matchess:
return matchess.get(name)
def get_logs(self,name):
sname = self.search_for_matches(name)
print(self._jails)
print(sname)
if self.journalctl_option(self._jails[f'{name}']):
result = subprocess.run(['journalctl', '-f', '-u', f'{sname}'], capture_output=True, text=True)
return result.stdout
def log_tracking(self):
for i in self._jails:
process = self.get_logs(i)
pattern = re.compile(
rf'{self._jails[i]["filter"][0][1]}'
)
unique_ips = set()
try:
while True:
output = process.stdout.readline()
if output == b'' and process.poll() is not None:
break
if output:
logs = output.decode('utf-8')
matches = pattern.findall(logs)
# Обработка совпадений
for match in matches:
if match[0]: # Если это неуспешная попытка входа
user = match[0]
ip_address = match[1]
port = match[2]
if ip_address not in unique_ips:
unique_ips.add(ip_address)
subprocess.run(['iptables', '-A', 'INPUT', '-s', ip_address, '-j', 'DROP'],
capture_output=True)
print(match[1])
except KeyboardInterrupt:
process.terminate()
def run():
a = BruteBan()
print("nvim is working")
while True:
a.log_tracking()
run()
# a = BruteBan()
# print(a._get_filterparams())
# print(a.get_jailparams())
# print(a.config_coordination())
# a.log_option()