Book Image

ModSecurity 2.5

Book Image

ModSecurity 2.5

Overview of this book

With more than 67% of web servers running Apache and web-based attacks becoming more and more prevalent, web security has become a critical area for web site managers. Most existing tools work on the TCP/IP level, failing to use the specifics of the HTTP protocol in their operation. Mod_security is a module running on Apache, which will help you overcome the security threats prevalent in the online world. A complete guide to using ModSecurity, this book will show you how to secure your web application and server, and does so by using real-world examples of attacks currently in use. It will help you learn about SQL injection, cross-site scripting attacks, cross-site request forgeries, null byte attacks, and many more so that you know how attackers operate. Using clear, step-by-step instructions this book starts by teaching you how to install and set up ModSecurity, before diving into the rule language with examples. It assumes no prior knowledge of ModSecurity, so as long as you are familiar with basic Linux administration, you can start to learn right away. Real-life case studies are used to illustrate the dangers on the Web today ñ you will for example learn how the recent worm that hit Twitter works, and how you could have used ModSecurity to stop it in its tracks. The mechanisms behind these and other attacks are described in detail, and you will learn everything you need to know to make sure your server and web application remain unscathed on the increasingly dangerous web. Have you ever wondered how attackers figure out the exact web server version running on a system? They use a technique called HTTP fingerprinting, and you will learn about this in depth and how to defend against it by flying your web server under a "false flag". The last part of the book shows you how to really lock down a web application by implementing a positive security model that only allows through requests that conform to a specific, pre-approved model, and denying anything that is even the slightest bit out of line.
Table of Contents (17 chapters)
ModSecurity 2.5
Credits
About the Author
About the Reviewers
Preface
Directives and Variables
Index

Character classes


Character classes provide a way to specify that exactly one of a group of characters should be matched against. Character classes are denoted by square brackets—[]—that contain characters or ranges of characters to match against.

As an example, the character class [abc] will match either a, b, or c exactly once, so the first match when matching against the string Brothers in arms would be the a in arms.

Character classes can contain ranges of characters, specified by using a hyphen. One example is the common character class [a-z], which denotes any character between a and z. A similar class is [a-zA-Z] which means any character between a and z, or A and Z, matching characters regardless of their case. Note how the first range is a-z, and the second range A-Z is specified immediately following it without any space or other character in-between.

Ranges work equally well for digits, and [0-9] means any digit, whereas [0-3] means only the digits 0, 1, 2, and 3.

Negated matching

You can negate a character class by specifying a caret immediately following the opening bracket. This means that the character class should match only characters not present inside the brackets. For example, the character class [^a-z] matches anything that isn't a letter from a through z.

Another thing to keep in mind is that there is no regular expression construct to negate matching of anything more than a single character. So for example it's not possible to have a regex that specifies that any other color than red should match in the string Roses are red, unless you want to resort to the regex Roses are [^r][^e][^d].*. (There is something called negative lookahead which can be handy if you really do want to assert that something is not present at a particular position in a regex, but lookaheads are beyond the scope of this book. A simple Google search will enlighten you if you really need this sort of regex.)

ModSecurity does have inverted rule matching using the exclamation mark operator, and this allows you to specify that a rule should match when a regex isn't present in the variable being matched against. The following rule, for example, will match if the string Firefox isn't in the user-agent string:

SecRule REQUEST_HEADERS:User-Agent "!Firefox" "phase,2"

Shorthand notation

There are a number of shorthand notations for common character classes, such as whitespace or digits. These consist of a backslash followed by a letter, and provide a simple way to use a character class without having to type it out in full each time. For example, the class shorthand \w means "part-of-word character" and is equivalent to [a-zA-Z0-9_], and will thus match a single letter, digit, or underscore character.

The following table lists the most common shorthand notations available.

Shorthand

Description

\d

Matches any digit.

Equivalent to [0-9].

\D

Matches any character that is not a digit.

Equivalent to [^0-9].

\w

Matches a word character.

Equivalent to [a-zA-Z0-9_].

\W

Matches anything that is not a word character.

Equivalent to [^a-zA-Z0-9_].

\s

Matches whitespace (space, tab, newline, form feed, and so on.)

\S

Matches anything that is not whitespace.

Equivalent to [^\s].

Note that the character class shorthands are case sensitive, and how the upper-case version usually means negation of the character class—for example \d means a digit whereas \D means any non-digit.