Is 'within' affected by 'distance'?

According to the manual, ‘within’ is not affected by ‘distance’.
The position of ‘within’ is constant regardless of the value of ‘distance’.
https://suricata.readthedocs.io/en/suricata-6.0.1/rules/payload-keywords.html#within

However, if you actually test it, it will be affected.
It is detected when distance is 2 and within is 4, but is not detected when distance is 0 and within is 4.

for example,
ETOpen rule #2000332

alert tcp any any → any 4660:4799 (msg:“ET P2P ed2k request part”; flow: to_server,established; content:“|e3|”; offset: 1; content:“|00 00 00 47|”; distance: 2; within: 4; classtype:policy-violation; sid:2000332; rev:11; metadata:created_at 2010_07_30, updated_at 2010_07_30;)

Which one is right? manual or test result?

Can you share the pcap for this test as well?

Hi, we did run into the same question and found this old thread.

According to the docs, within ‘is relative to the preceding match‘ [1]. There is no word about whether distance is included or not.
Therefore we assume that the last graphic [2] maybe is not fully correct, because the pink within-pointer points between f-g because of within: 4 depending on the end of the previous content. But it should instead point between g-h, because it does include the distance keyword! So in the graphic, even a distance:1; within:3; should be a match.

We did a short test with the rules and the PCAP attached (Suricata v8.0.3, Debian unstable). It contains a UNIX ping having some payload “ABCDEFGHI…”.

Signature 5 uses content:"ABC"; content:"EFG"; distance:1; within:3; which should not fire, according to the docs, because within: 3 only would include “DEF”, but not “EFG“. But this rule does fire in our test! So our assumption seems to be correct.

This leads me to the following question: What is the intended behaviour of the within keyword? Should it start searching beginning at the distance from the previous content match or should it start directly after the previous match?

Assuming the current Suricata code/behaviour is correct, we should probably correct the graphic and move the pink within-pointer between g-h. If you agree, I can try to do some merge request fixing that.

Thanks for your help :slight_smile:
Andreas

rules.rules (999 Bytes)

ping.pcapng (780 Bytes)

[1] 8.7. Payload Keywords — Suricata 9.0.0-dev documentation

[2] https://docs.suricata.io/en/latest/_images/within_distance2.png

I agree with you. The docs for distance probably do a better job at explaining the use case for distance + within: 8.7. Payload Keywords — Suricata 9.0.0-dev documentation

Hi Shivani, thanks for your reply.

Did a short PR to fix the graphic: doc: fix within-distance pointer graphics in payload keywords doc by e-cite · Pull Request #14744 · OISF/suricata · GitHub

Please tell me if any changes required on that.
Thanks for the review :slight_smile:

Regards,
Andreas