How could a Suricata rule detect the XSS payloads <script>alert(document.cookie);</script> and <img src="javascript:document.write('XSS')" />?
Match an HTML tag opening in literal or URL-encoded form (< or %3C) followed by a tag name and a closing >/%3E, with a PCRE that ignores case. For example: pcre:"/((\%3C)|<)((\%2F)|\/)*[a-z0-9\%]+((\%3E)|>)/ix";
Cross-site scripting injects HTML or JavaScript that the victim's browser executes. The common element of both payloads is an HTML tag inside user input where none belongs.
alert tcp any any -> $HOME_NET any (msg:"XSS attempt"; flow:to_server,established; pcre:"/((\%3C)|<)((\%2F)|\/)*[a-z0-9\%]+((\%3E)|>)/ix"; classtype:web-application-attack; sid:1000018; rev:1;)
((\%3C)|<): an opening angle bracket, literal or encoded.((\%2F)|\/)*: an optional slash (closing tags like</script>).[a-z0-9\%]+: the tag name.((\%3E)|>): the closing bracket.
A narrower variant targets the image trick specifically: <, then img in any case or encoding, then anything up to >, which catches <img src="javascript:…">. Variants with script or javascript: are possible too.
The trade-off is the same as for SQL injection. A generic tag pattern catches many variants but also any legitimate HTML in form input, so real deployments scope it to specific parameters or pair it with content keywords such as script or javascript.
Go deeper:
Mookhey: Detection of SQL Injection and Cross-site Scripting Attacks (Black Hat USA 2004) — the origin of the tag regex and of the
<img src>variant, with the reasoning behind each.OWASP XSS Filter Evasion Cheat Sheet — the encodings and tricks attackers use to slip past exactly this kind of signature.
Cross-site scripting (Wikipedia) — reflected vs stored XSS and what an attacker gains from each.