简体   繁体   English

为 fail2ban 调整 Mysql-auth.conf 过滤器给了我 Python 异常

[英]Adjusting Mysql-auth.conf filter for fail2ban gives me Python Exceptions

the log line TO JAIL in /etc/mysql/error.log /etc/mysql/error.log 中的 TO JAIL 日志行

2022-06-23T16:19:10.452205Z 233 [Note] [MY-010926] [Server] Access denied for user 'webadmin'@'93.223.131.127' (using password: YES)

the regex正则表达式

(?:(?:\d{6}|\d{4}-\d{2}-\d{2})[ T]\s?\d{1,2}:\d{2}:\d{2}).?(?:\d+[A-Z]) ?(?:\d+ ) ?\[\w+\] (?:\[[^\]]+\] )*Access denied for user '[^']+'@'0.0.0.0' (to database '[^']*'|\(using password: (YES|NO)\))*\s*$

adjusted in www.regex101.com and confirmed working.www.regex101.com中调整并确认工作。

match output匹配输出

0-132   2022-06-23T16:19:10.452205Z 233 [Note] [MY-010926] [Server] Access denied for user 'webadmin'@'93.22...
111-132 (using password: YES)
128-131 YES

tried in filter.d/mysqld-auth.conf在 filter.d/mysqld-auth.conf 中尝试过

#before = common.conf
failregex = ^%(?:(?:\d{6}|\d{4}-\d{2}-\d{2})[ T]\s?\d{1,2}:\d{2}:\d{2}).?(?:\d+[A-Z]) ?(?:\d+ ) ?\[\w+\] (?:\[[^\]]+\] )*Access denied for user '[^'...etc...

testing with fail2ban-regex gives me用 fail2ban-regex 测试给了我

Running tests
=============

Use   failregex filter file : mysqld-auth, basedir: /etc/fail2ban
Traceback (most recent call last):
  File "/usr/local/bin/fail2ban-regex", line 4, in <module>
    __import__('pkg_resources').run_script('fail2ban==0.9.4', 'fail2ban-regex')
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 667, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1463, in run_script
    exec(code, namespace, namespace)
  File "/usr/local/lib/python3.8/dist-packages/fail2ban-0.9.4-py3.8.egg/EGG-INFO/scripts/fail2ban-regex", line 34, in <module>
    exec_command_line()
  File "/usr/local/lib/python3.8/dist-packages/fail2ban-0.9.4-py3.8.egg/fail2ban/client/fail2banregex.py", line 596, in exec_command_line
    if not fail2banRegex.start(opts, args):
  File "/usr/local/lib/python3.8/dist-packages/fail2ban-0.9.4-py3.8.egg/fail2ban/client/fail2banregex.py", line 496, in start
    if not self.readRegex(cmd_regex, 'fail'):
  File "/usr/local/lib/python3.8/dist-packages/fail2ban-0.9.4-py3.8.egg/fail2ban/client/fail2banregex.py", line 288, in readRegex
    reader.getOptions(None)
  File "/usr/local/lib/python3.8/dist-packages/fail2ban-0.9.4-py3.8.egg/fail2ban/client/configreader.py", line 283, in getOptions
    self._opts = ConfigReader.getOptions(
  File "/usr/local/lib/python3.8/dist-packages/fail2ban-0.9.4-py3.8.egg/fail2ban/client/configreader.py", line 137, in getOptions
    return self._cfg.getOptions(*args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/fail2ban-0.9.4-py3.8.egg/fail2ban/client/configreader.py", line 220, in getOptions
    v = self.get(sec, option[1])
  File "/usr/lib/python3.8/configparser.py", line 799, in get
    return self._interpolation.before_get(self, section, option, value,
  File "/usr/lib/python3.8/configparser.py", line 395, in before_get
    self._interpolate_some(parser, option, L, value, section, defaults, 1)
  File "/usr/local/lib/python3.8/dist-packages/fail2ban-0.9.4-py3.8.egg/fail2ban/client/configparserinc.py", line 58, in _interpolate_some
    return super(BasicInterpolationWithName, self)._interpolate_some(
  File "/usr/lib/python3.8/configparser.py", line 427, in _interpolate_some
    raise InterpolationSyntaxError(option, section,
configparser.InterpolationSyntaxError: bad interpolation variable reference "%(?:(?:\\d{6}|\\d{4}-\\d{2}-\\d{2})[ T]\\s?\\d{1,2}:\\d{2}:\\d{2}).?(?:\\d+[A-Z]) ?(?:\\d+ ) ?\\[\\w+\\] (?:\\[[^\\]]+\\] )*Access denied for user '[^']+'@'<HOST>' (to database '[^']*'|\\(using password: (YES|NO)\\))*\\s*$"

tried also in /filter/mysqld-auth.conf也在 /filter/mysqld-auth.conf 中尝试过

before = common.conf
failregex = ^%(__prefix_line)s(?:etc...

leading to Running tests =============导致运行测试=============

Use   failregex filter file : mysqld-auth, basedir: /etc/fail2ban
Use         log file : /var/log/mysql/sample.log
Use         encoding : UTF-8


Results
=======

Failregex: 0 total
|-  #) [# of hits] regular expression
|   1) [0] ^<lt_<logtype>/__prefix_line>(?:(?:\d{6}|\d{4}-\d{2}-\d{2})[ T]\s?\d{1,2}:\d{2}:\d{2}).?(?:\d+[A-Z]) ?(?:\d+ ) ?\[\w+\] (?:\[[^\]]+\] )*Access denied for user '[^']+'@'<HOST>' (to database '[^']*'|\(using password: (YES|NO)\))*\s*$
`-

Ignoreregex: 0 total

Date template hits:
|- [# of hits] date format
|  [1] Year-Month-Day[T ]24hour:Minute:Second(?:\.Microseconds)?(?:Zone offset)?
|  [0] (?:DAY )?MON Day 24hour:Minute:Second(?:\.Microseconds)?(?: Year)?
|  [0] (?:DAY )?MON Day Year 24hour:Minute:Second(?:\.Microseconds)?
|  [0] Year(?P<_sep>[-/.])Month(?P=_sep)Day 24hour:Minute:Second(?:,Microseconds)?
|  [0] Day(?P<_sep>[-/])Month(?P=_sep)(?:Year|Year2) 24hour:Minute:Second
|  [0] Day(?P<_sep>[-/])MON(?P=_sep)Year[ :]?24hour:Minute:Second(?:\.Microseconds)?(?: Zone offset)?
|  [0] Month/Day/Year:24hour:Minute:Second
|  [0] Month-Day-Year 24hour:Minute:Second\.Microseconds
|  [0] TAI64N
|  [0] Epoch
|  [0] ^24hour:Minute:Second
|  [0] ^<Month/Day/Year2@24hour:Minute:Second>
|  [0] ^Year2MonthDay  ?24hour:Minute:Second
|  [0] MON Day, Year 12hour:Minute:Second AMPM
|  [0] ^MON-Day-Year2 24hour:Minute:Second
`-

Lines: 1 lines, 0 ignored, 0 matched, 1 missed
[processed in 0.00 sec]

what is the right way of writing this filter with fail2ban?用fail2ban编写这个过滤器的正确方法是什么? Is #before important? #before 重要吗? how does ^%(__prefix_line)s impact? ^%(__prefix_line)s 有何影响? Are my tries hoax and lead python to except or is my python not properly installed?我的尝试是恶作剧并导致 python 出现异常,还是我的 python 安装不正确?

Using Ubuntu 20.04 THANKS!!!使用 Ubuntu 20.04 谢谢!!!

There's a number of problems with your attempt:您的尝试存在许多问题:

  1. Your first regex begins with %( string interpolation notation but it doesn't use any variable and it doesn't end like it should, so you get Python error bad interpolation variable reference . Python's correct string interpolation operator is %(...)s .您的第一个正则表达式以%(字符串插值表示法开头,但它不使用任何变量,也没有按应有的方式结束,因此您得到 Python 错误bad interpolation variable reference 。Python 的正确字符串插值运算符是%(...)s

  2. You need to replace the 0.0.0.0 part with either <ADDR> or <HOST> tag (first one matches IP addresses, latter one IP + host names).您需要将0.0.0.0部分替换为<ADDR><HOST>标记(第一个匹配 IP 地址,后一个匹配 IP + 主机名)。 As it is now, it can only match 0.0.0.0 (and it won't even work with fail2ban which will throw you ERROR: No failure-id group ).就像现在一样,它只能匹配 0.0.0.0 (它甚至不能与 fail2ban 一起使用,这会抛出ERROR: No failure-id group )。

  3. Remove the part that tries to match the date pattern from the failregex.从 failregex 中删除尝试匹配日期模式的部分。 Fail2ban first cuts out the date pattern from each input log line and then it applies the failregex. Fail2ban 首先从每个输入日志行中删除日期模式,然后应用失败正则表达式。

So, this will match what you want:因此,这将符合您的要求:

^\s*(?:\d+ ) ?\[\w+\] (?:\[[^\]]+\] )*Access denied for user '[^']+'@'<HOST>' (to database '[^']*'|\(using password: (YES|NO)\))*\s*$

%(__prefix_line)s is used to match common line prefixes and you can see its regex in /etc/fail2ban/filter.d/common.conf . %(__prefix_line)s用于匹配常见的行前缀,您可以在/etc/fail2ban/filter.d/common.conf中看到它的正则表达式。 It's optional, in your specific use case you can omit this.它是可选的,在您的特定用例中,您可以省略它。 Otherwise you could use:否则你可以使用:

^%(__prefix_line)s(?:\d+ ) ?\[\w+\] (?:\[[^\]]+\] )*Access denied for user '[^']+'@'<HOST>' (to database '[^']*'|\(using password: (YES|NO)\))*\s*$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM