--to オプションが 52 未満の場合は、iptables 文字列の一致は機能しません。

--to オプションが 52 未満の場合は、iptables 文字列の一致は機能しません。

文字列に一致するiptablesルールを入力し、--toオプションが>= 52

iptables -I FORWARD 1 -m string --string anypattern --algo bm --to 100 -j DROP

上記は正常に動作し、「anypattern」文字列を含む IP パケットをブロックします。

--toを値に変更すると< 52動作しなくなります

iptables -I FORWARD 1 -m string --string anypattern --algo bm --to 50 -j DROP

そして、IP パケットはブロックされません。

何か見落としているのでしょうか? それともこれは iptables の問題でしょうか?

例:

linux:~$ sudo iptables -I OUTPUT 1 -m string --algo bm --string 7oula --to 52 -j DROP
linux:~$ echo 7oulaaaaaaaaaaa | nc  212.227.247.109 80
^C  #<---- Blocked here ==> Good
linux:~$ sudo iptables -I OUTPUT 1 -m string --algo bm --string coula --to 51 -j DROP
linux:~$ echo coulaaaaaaaaaaa | nc  212.227.247.109 80
HTTP/1.1 400 Bad Request
Server: nginx
Date: Sun, 26 Jan 2020 15:35:55 GMT
Content-Type: text/html
Content-Length: 150
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>

答え1

この--toオプションでは、最大限文字列を開始できるオフセット。オフセットは IP パケットの開始からの相対値です。合計すると次のようになります。

  • 20IPヘッダーのバイト、
  • 20TCPヘッダーのバイト、
  • 12一部のTCPオプションのバイト

最終的に 52 バイトになります。TCP ペイロードの開始はオフセット 52 にあり、それより小さい場合は TCP/IP ヘッダーのみに一致します。

TCPオプションの長さは固定ではないことに注意してください。12という数字は実験的に得られたものです。tcpダンプただし、システムごとに送信できるオプションの数は異なります。

関連情報