當 --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轉儲,但每個系統可以發送不同數量的選項。

相關內容