-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSimpleTransformation.xslt
executable file
·137 lines (122 loc) · 9.1 KB
/
SimpleTransformation.xslt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/Profile">
# $Id: SimpleTransformation.xslt 6 2016-08-22 12:38 [email protected] $
#
# This rule set has been compiled using xslt transformation includes rules for:
# Parameters, Headers, Session Cookies and Cookies checked for each registered resource
#
# Rules compatible with modsecurity 2.5 to 2.9+
#
# License: Apache License Version 2.0
# Based on $Id: SimpleTransformation.xslt 55 2007-10-03 22:50:56Z [email protected] $
#
<xsl:apply-templates><xsl:with-param name="path" select="''"/></xsl:apply-templates>
<LocationMatch "^.*$">
#SecRule &TX:METHOD_CHECKED "!@gt 0" "id:9931733,auditlog,phase:2,log,msg:'Invalid URL requested! %{REQUEST_METHOD} - %{REQUEST_FILENAME} with ARGS:%{ARGS_NAMES}',redirect:UnknownURLRequested.html"
SecRule &TX:METHOD_CHECKED "!@gt 0" "id:9931733,auditlog,phase:2,log,msg:'Invalid URL requested! %{REQUEST_METHOD} - %{REQUEST_FILENAME} with ARGS:%{ARGS_NAMES}',pass"
</LocationMatch>
</xsl:template>
<xsl:template match="Meta-Inf"># Meta-Information
# Author : <xsl:value-of select="./@Author" />
# Version: <xsl:value-of select="./@Version" />
SecDefaultAction phase:2,t:none,pass,log
#debuggen
</xsl:template>
<xsl:template match="SessionCookie">
#
# Session-Handling
#
<xsl:choose>
<xsl:when test="./@ratio > 0 and ./@score > 0">
<xsl:if test="count(@required) > 0"> SecRule &REQUEST_COOKIES:<xsl:value-of select="./@name"/> "@eq 0" "id:9931733,phase:1,setvar:tx.score=+<xsl:value-of select="./@ratio" />,pass,msg:'Missing required session cookie <xsl:value-of select="./@name"/>'"</xsl:if>
SecRule REQUEST_COOKIES:<xsl:value-of select="./@name" /> "!@rx <xsl:value-of select="./@regexp"/>" "<xsl:if test="./@id = ''">id:9931733,</xsl:if><xsl:if test="./@id != ''">id:<xsl:value-of select="./@id"/>,</xsl:if>phase:1,t:none,t:urlDecode,pass,setvar:tx.score=+<xsl:value-of select="@score"/>"
</xsl:when>
<xsl:otherwise>
<xsl:if test="count(@required) > 0"> SecRule &REQUEST_COOKIES:<xsl:value-of select="./@name"/> "@eq 0" "id:9931733,phase:1,setvar:tx.score=+10,pass,msg:'Missing required session cookie <xsl:value-of select="./@name"/>'"</xsl:if>
SecRule REQUEST_COOKIES:<xsl:value-of select="./@name" /> "!@rx <xsl:value-of select="./@regexp"/>" "<xsl:if test="./@id = ''">id:9931733,</xsl:if><xsl:if test="./@id != ''">id:<xsl:value-of select="./@id"/>,</xsl:if>phase:1,t:none,t:urlDecode,pass,setvar:tx.score=+10"
</xsl:otherwise>
</xsl:choose>
SecRule REQUEST_COOKIES:<xsl:value-of select="./@name" /> "@rx <xsl:value-of select="./@regexp"/>" "id:9931733,phase:1,t:none,pass,setsid:%{REQUEST_COOKIES:<xsl:value-of select="@name"/>}"
</xsl:template>
<xsl:template match="Resource">
<xsl:if test="count(./*) - count(./Resource) > 0">
<xsl:variable name="path"><xsl:for-each select="ancestor-or-self::Resource"><xsl:if test="./@name != ''"><xsl:value-of select="concat('',./@name)" /></xsl:if></xsl:for-each></xsl:variable>
<LocationMatch "^<xsl:value-of select="$path"/>$">
#
# mypath: <xsl:value-of select="$path"/>
# resource-specific rules
#
<xsl:apply-templates select="Method" />
#
# the control-section
#
SecRule &TX:METHOD_CHECKED "@eq 0" "id:9931733,setvar:tx.score=+1,log,auditlog,msg:'Fatal:_Invalid_request-method_for_URL_<xsl:value-of select="$path" />'"
SecRule TX:SCORE "@gt 0" "id:9931733,log,auditlog,msg:'transaction-score is %{TX.SCORE}'"
SecRule SESSION:SCORE "@gt 2" "id:9931733,log,auditlog,msg:'Session score is %{SESSION.SCORE}!'"
</LocationMatch>
</xsl:if>
<xsl:apply-templates select="Resource" />
</xsl:template>
<!-- -->
<!-- The following template creates a parameter-check -->
<!-- -->
<xsl:template match="Parameter">
<xsl:choose>
<xsl:when test="./@ratio > 0 and ./@score > 0">
<xsl:if test="count(@required) > 0"> SecRule &ARGS:<xsl:value-of select="./@name"/> "@eq 0" "id:9931733,phase:2,setvar:tx.score=+<xsl:value-of select="./@ratio" />,pass,msg:'Missing required parameter <xsl:value-of select="./@name"/>'"</xsl:if>
SecRule ARGS:<xsl:value-of select="./@name" /> "!@rx <xsl:value-of select="./@regexp"/>" "<xsl:if test="./@id = ''">id:9931733,</xsl:if><xsl:if test="./@id != ''">id:<xsl:value-of select="./@id"/>,</xsl:if>phase:2,t:none,t:urlDecode,pass,setvar:tx.score=+<xsl:value-of select="@score"/>"
</xsl:when>
<xsl:otherwise>
<xsl:if test="count(@required) > 0"> SecRule &ARGS:<xsl:value-of select="./@name"/> "@eq 0" "id:9931733,phase:2,setvar:tx.score=+10,pass,msg:'Missing required parameter <xsl:value-of select="./@name"/>'"</xsl:if>
SecRule ARGS:<xsl:value-of select="./@name" /> "!@rx <xsl:value-of select="./@regexp"/>" "<xsl:if test="./@id = ''">id:9931733,</xsl:if><xsl:if test="./@id != ''">id:<xsl:value-of select="./@id"/>,</xsl:if>phase:2,t:none,t:urlDecode,pass,setvar:tx.score=+1"
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates />
</xsl:template>
<!-- -->
<!-- The following template creates a parameterlist-check -->
<!-- -->
<xsl:template match="ParameterList">
<xsl:if test="count(@parameter_list) > 0"> SecRule ARGS_NAMES "@rx ^(.*)$" "chain,<xsl:if test="./@id = ''">id:9931733,</xsl:if><xsl:if test="./@id != ''">id:<xsl:value-of select="./@id"/>,</xsl:if>phase:2,capture,t:urlDecode,t:lowercase,pass,setvar:tx.parameter_name='/%{tx.1}/',msg:'Found new parameter %{tx.parameter_name} in %{REQUEST_METHOD} - %{REQUEST_FILENAME}'"
SecRule TX:parameter_name "!@rx (<xsl:value-of select="./@parameter_list"/>)" "t:none,setvar:tx.score=+10,setvar:tx.new_parameter=1"
</xsl:if>
<xsl:apply-templates />
</xsl:template>
<!-- -->
<!-- The following template creates a method-check -->
<!-- -->
<xsl:template match="Method">
SecRule REQUEST_METHOD "!@rx ^<xsl:value-of select="./@value"/>$" "id:9931733,phase:2,t:none,log,auditlog,skip:<xsl:value-of select="count(child::*) + 1 + count(child::Parameter)"/>"
SecAction "id:9931733,setvar:tx.method_checked=1,pass,nolog,noauditlog"
<xsl:apply-templates select="Parameter|ParameterList|CreateToken|CheckToken|SessionCookie|Cookie|Header"/>
</xsl:template>
<xsl:template match="Cookie">
<xsl:choose>
<xsl:when test="./@ratio > 0 and ./@score > 0">
<xsl:if test="count(@required) > 0"> SecRule &REQUEST_COOKIES:<xsl:value-of select="./@name"/> "@eq 0" "id:9931733,phase:2,setvar:tx.score=+<xsl:value-of select="./@ratio" />,pass,msg:'Missing required cookie <xsl:value-of select="./@name"/>'"</xsl:if>
SecRule REQUEST_COOKIES:<xsl:value-of select="./@name" /> "!@rx <xsl:value-of select="./@regexp"/>" "<xsl:if test="./@id = ''">id:9931733,</xsl:if><xsl:if test="./@id != ''">id:<xsl:value-of select="./@id"/>,</xsl:if>phase:2,t:none,t:urlDecode,pass,setvar:tx.score=+<xsl:value-of select="@score"/>"
</xsl:when>
<xsl:otherwise>
<xsl:if test="count(@required) > 0"> SecRule &REQUEST_COOKIES:<xsl:value-of select="./@name"/> "@eq 0" "id:9931733,phase:2,setvar:tx.score=+10,pass,msg:'Missing required cookie <xsl:value-of select="./@name"/>'"</xsl:if>
SecRule REQUEST_COOKIES:<xsl:value-of select="./@name" /> "!@rx <xsl:value-of select="./@regexp"/>" "<xsl:if test="./@id = ''">id:9931733,</xsl:if><xsl:if test="./@id != ''">id:<xsl:value-of select="./@id"/>,</xsl:if>phase:2,t:none,t:urlDecode,pass,setvar:tx.score=+3"
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="Header">
<xsl:choose>
<xsl:when test="./@ratio > 0 and ./@score > 0">
<xsl:if test="count(@required) > 0"> SecRule &REQUEST_HEADERS:<xsl:value-of select="./@name"/> "@eq 0" "id:9931733,phase:2,setvar:tx.score=+<xsl:value-of select="./@ratio" />,pass,msg:'Missing required header <xsl:value-of select="./@name"/>'"</xsl:if>
SecRule REQUEST_HEADERS:<xsl:value-of select="./@name" /> "!@rx <xsl:value-of select="./@regexp"/>" "<xsl:if test="./@id = ''">id:9931733,</xsl:if><xsl:if test="./@id != ''">id:<xsl:value-of select="./@id"/>,</xsl:if>phase:2,t:none,t:urlDecode,pass,setvar:tx.score=+<xsl:value-of select="@score"/>"
</xsl:when>
<xsl:otherwise>
<xsl:if test="count(@required) > 0"> SecRule &REQUEST_HEADERS:<xsl:value-of select="./@name"/> "@eq 0" "id:9931733,phase:2,setvar:tx.score=+10,pass,msg:'Missing required header <xsl:value-of select="./@name"/>'"</xsl:if>
SecRule REQUEST_HEADERS:<xsl:value-of select="./@name" /> "!@rx <xsl:value-of select="./@regexp"/>" "<xsl:if test="./@id = ''">id:9931733,</xsl:if><xsl:if test="./@id != ''">id:<xsl:value-of select="./@id"/>,</xsl:if>phase:2,t:none,t:urlDecode,pass,setvar:tx.score=+1"
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
</xsl:stylesheet>