Fail2banのjam.confファイルの%(...)s文字列は何ですか?どのように動作しますか?

Fail2banのjam.confファイルの%(...)s文字列は何ですか?どのように動作しますか?

設定中、fail2ban上部の変数はjail.conf次のようになります。

mytime=300
.
.
.
[ssh]
bantime=%(mytime)s

または、次のように、より複雑な形式で使用できます。

action_ = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"].

質問

  • これらはどのように機能し、それらに何が起こりますか?
  • 正確に何が起こりましたか%(...string...)s

ベストアンサー1

含まれているルールを調べると、fail2banこれらの変数を使用して作業をよりきれいでパラメータ化できることがわかります。たとえば、包含では、jail.confこれを使用してさまざまな刑務所を定義するときに使用できる一般的な運用規則を開発します。

はい

上部にはいくつかの基本変数があります。

# Destination email address used solely for the interpolations in
# jail.{conf,local,d/*} configuration files.
destemail = root@localhost

# Sender email address used solely for some actions
sender = root@localhost

# Default protocol
protocol = tcp

# Ports to be banned
# Usually should be overridden in a particular jail
port = 0:65535

その後、これらの変数を他の変数と組み合わせて使用​​して、いくつかの基本操作を構成します。

# Default banning action (e.g. iptables, iptables-new,
# iptables-multiport, shorewall, etc) It is used to define
# action_* variables. Can be overridden globally or per
# section within jail.local file
banaction = iptables-multiport

# The simplest action to take: ban only
action_ = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]

# ban & send an e-mail with whois report to the destemail.
action_mw = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
            %(mta)s-whois[name=%(__name__)s, dest="%(destemail)s", protocol="%(protocol)s", chain="%(chain)s"]

action_ここでは、他の変数( `%(protocol)sなど%(banaction)s)を使って作成されるという一般的なタスクを構成しています%(port)s

man jail.confマニュアルページから:

Pythonの「文字列補間」メカニズムを使用すると、他の定義が許可され、%(name)sは後で他の定義で使用できます。例えば。

         baduseragents = IE|wget
         failregex = useragent=%(baduseragents)s

したがって、彼らは%(...)sPython言語の一部です。検索すると、最終的にPython言語仕様で次のタイトルのこのページを見つけることができます。5.6.2。文字列フォーマット操作。このページには次の例があります。

>>> print '%(language)s has %(number)03d quote types.' % \
...       {"language": "Python", "number": 2}
Python has 002 quote types.

Pythonでは%(...string...)s文字列形式化または補間演算子として知られています。最後s%(...string...)、渡すことができるすべてのPythonオブジェクトが文字列に変換されることを指定するフラグがあります。私が引用したリンクには、許可されているすべてのフラグを含むテーブルがあります。

  SS#1

%指定子を開始する場所と拡張したい(...string...)Python変数がある場所を指定します。

おすすめ記事