Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ModSecurity 2.5

You're reading from   ModSecurity 2.5 Prevent web application hacking with this easy to use guide

Arrow left icon
Product type Paperback
Published in Nov 2009
Publisher Packt
ISBN-13 9781847194749
Length 280 pages
Edition 1st Edition
Arrow right icon
Toc

Table of Contents (17) Chapters Close

ModSecurity 2.5
Credits
About the Author
About the Reviewers
1. Preface
1. Installation and Configuration FREE CHAPTER 2. Writing Rules 3. Performance 4. Audit Logging 5. Virtual Patching 6. Blocking Common Attacks 7. Chroot Jails 8. REMO 9. Protecting a Web Application Directives and Variables Regular Expressions Index

Backreferences


Backreferences are used to capture a part of a regular expression so that it can be referred to later. The regex Hello, my name is (.+) will capture the name into a variable that can be referred to later. The reason for this is that the .+ construct is surrounded by parentheses.

The name of the variable that the matched text is captured into will differ depending on what regex flavor you are working with. In Perl, for example, regex backreferences are captured into variables named $1, $2, $3, and so on.

Captures are made left-to-right, with the text within the first parentheses captured into the variable $1, the second into $2, and so forth. Capturing can even be made within a set of parenthesis, so the regex My full name is ((\w+) \w+) would store the complete name (first and last) into $1 and the first name only into $2.

These are the same kind of parenthesis used for grouping, so grouping using standard parenthesis will also create a backreference. We will however shortly see how to achieve grouping without capturing backreferences.

Captures and ModSecurity

To use captured backreferences in a ModSecurity rule, you specify the capture action in the rule, which makes the captured backreferences available in the transaction variables TX:1 through TX:9.

The following rule uses a regex that looks for a browser name and version number in the request headers. If found, the version number is captured into the transaction variable TX:1 (which is accessed using the syntax %{TX.1}) and is subsequently logged to the error log file:

SecRule REQUEST_HEADERS:User-Agent "Firefox/(\d\.\d\.\d)" "pass,phase:2,capture,log,logdata:%{TX.1}"

Up to nine captures can be made this way. The transaction variable TX:0 is used to capture the entire regex match, so in the above example, it would contain something like Firefox/3.0.9.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image