|
With the help of the Script Handler you can execute a Python script. This enables you to expand the scope of functions of Stingray Application Firewall to your specific requirements.
Within your scripts you can use all basic Python operators plus the following modules and functions:
Module
|
Functions available
|
time
(standard Python module)
|
All functions from the standard Python time module.
|
re
(standard Python module)
|
All functions from the standard Python re module.
|
hashlib
(standard Python module)
|
All functions from the standard Python hashlib module.
Can be used, for example, to create cryptographically secure hash values for adding additional security features to your web application, such as signed cookies.
|
etree
(provided by lxml library)
|
All functions from the etree module of lxml.
From a script, the module is available as lxml_etree.
Example: xml = lxml_etree.parse(StringIO(xml_body))
|
StringIO
(standard Python module)
|
Only accepts file descriptors.
Needed for feeding the lxml library with data.
|
http
(special interface to interact with Stingray Application Firewall)
|
All functions listed in the sections below this table.
|
Inportant: In detection mode, only those functions that are accessible during requests do work. If in detection mode you use functions that are only available during responses, these functions won’t work. In some rare cases, if a web application makes use of Stingray Application Firewall functions, such as URL rewriting, this might result in the fact that your web application doesn’t work any more.
Overview: The following functions of the special http module are accessible during requests:
add_request_header(key, value)
add_response_header(key, value)
allow_request()
del_request_header(key)
del_response_header(key)
filter_response_full()
filter_response_header()
generate_blacklist_event(ip_range, timeframe)
get_client_ip()
get_request_args()
get_request_body()
get_request_cookie(name)
get_request_cookies()
get_request_header(key)
get_request_method()
get_request_uri()
get_storage()
|
|
|
is_request()
is_response()
log(string1, string2, string3, ..., string n)
make_random_cookie()
redirect(url)
send_response(content_type, body)
set_request_args((key1,value1),(key2,value2),...)
set_request_cookie(key, value)
set_request_header(key, value)
set_request_uri(uri, arguments)
set_response_cookie(key, value)
set_response_header(key, value)
set_returncode(code)
terminate_session()
|
Overview: The following functions of the special http module are accessible during responses:
Functions accessible during requests in detail
add_request_header(key, value)
|
Purpose:
|
Adds a new header with the given data (key: value).
|
Input:
|
key, value as String
|
Output:
|
–
|
add_response_header(key, value)
|
Purpose:
|
Adds a header to the response with the given values.
You can also do that by enabling response filtering and then calling the function add_response_header during a response, but in this case the whole response is filtered.
|
Input:
|
key, value as String
|
Output:
|
–
|
allow_request()
|
Purpose:
|
Aborts the current request processing and accepts the request. This can be used to bypass all handlers that are invoked after the Script Handler.
The handlers that Stingray Application Firewall executes after the Script Handler are the handlers that are listed below the Script Handler on the Handlers tab. The sequence is determined automatically, so you can’t change it.
|
Input:
|
–
|
Output:
|
–
|
del_request_header(key)
|
Purpose:
|
Deletes the header that has the given key.
|
Input:
|
key as String
|
Output:
|
–
|
del_response_header(key)
|
Purpose:
|
Deletes a header from the response with the given values.
You can also do that by enabling response filtering and then calling the function del_response_header during a response, but in this case the whole response is filtered.
|
Input:
|
key as String
|
Output:
|
–
|
filter_response_full()
|
Purpose:
|
Tells Stingray Application Firewall to filter the full response, including headers and body.
Be careful with potentially big response bodies. This can result in significant impact on performance.
|
Input:
|
–
|
Output:
|
–
|
filter_response_header()
|
Purpose:
|
Tells Stingray Application Firewall to filter also the response headers belonging to the current request.
There’s no access to the response body during the response cycle. If you want to filter also the body, use the function filter_response_full.
|
Input:
|
–
|
Output:
|
–
|
generate_blacklist_event(ip_range, timeframe)
|
Purpose:
|
Adds the specified IP address or the specified range of IP addresses to the global IP blacklist (see Global IP Blacklisting).
|
Input:
|
String,
Integer value
|
Output:
|
–
|
get_client_ip()
|
Purpose:
|
Returns the IP address of the user.
|
Input:
|
–
|
Output:
|
String
|
get_request_args()
|
Purpose:
|
Returns the request arguments as a list of tuples:
[(key1,value1),(key2,value2),...]
|
Input:
|
–
|
Output:
|
list of tuples [(key1,value1),(key2,value2),...]
|
get_request_body()
|
Purpose:
|
Returns the full request body as string (empty string if there’s no body).
|
Input:
|
–
|
Output:
|
String
|
get_request_cookie(name)
|
Purpose:
|
Returns a string with the value of the cookie.
|
Input:
|
cookie name as String
|
Output:
|
cookie value as String
|
get_request_cookies()
|
Purpose:
|
Returns a dict with all cookies: { cookie-name1 : cookie-value1, cookie-name2 : cookie-value2 }
|
Input:
|
–
|
Output:
|
dict {cookie-name1 : cookie-value1, cookie-name2 : cookie-value2 }
|
get_request_header(key)
|
Purpose:
|
Returns the value of the request header stated by the parameter key.
If this header doesn’t exist, the function returns None.
|
Input:
|
key as String
|
Output:
|
String or None
|
get_request_method()
|
Purpose:
|
Returns the HTTP method for the current request (GET, POST...) as string.
|
Input:
|
–
|
Output:
|
String
|
get_request_uri()
|
Purpose:
|
Returns the requested URI.
|
Input:
|
–
|
Output:
|
String
|
get_storage()
|
Purpose:
|
Returns a dict that can be used as data storage throughout a session.
The Session Handler needs to be enabled for this to work.
|
Input:
|
–
|
Output:
|
dictionary
|
is_request()
|
Purpose:
|
Returns True if the current script is executed during a request. Else the result is False.
The statement not is_response() returns the same result.
|
Input:
|
–
|
Output:
|
Boolean
|
is_response()
|
Purpose:
|
Returns True if the current script is executed during a response. Else the result is False.
The statement not is_request() returns the same result.
|
Input:
|
–
|
Output:
|
Boolean
|
log(string1, string2, string3, ..., string n)
|
Purpose:
|
Concatenates the given list of strings (at least one) and writes the result to the Stingray Application Firewall Log Files.
|
Input:
|
list of String
|
Output:
|
–
|
make_random_cookie()
|
Purpose:
|
Generates a random string, which can be used as the name of a cookie.
|
Input:
|
–
|
Output:
|
String
|
redirect(url)
|
Purpose:
|
Redirects to the given URL.
|
Input:
|
URL as String
|
Output:
|
–
|
send_response(content_type, body)
|
Purpose:
|
Aborts the current request and sends a response with the given content_type (e.g. “text/html”) and body.
|
Input:
|
content_type, body as String
|
Output:
|
–
|
set_request_args((key1,value1),(key2,value2),...)
|
Purpose:
|
Sets the arguments in a POST request.
|
Input:
|
list of tuples [(key1,value1),(key2,value2),...]
|
Output:
|
–
|
set_request_cookie(key, value)
|
Purpose:
|
Sets the cookie (from browser) that has the name key to the given value.
If you want to set a cookie on the browser side, use the function set_response_cookie instead.
|
Input:
|
key, value as String
|
Output:
|
–
|
set_request_header(key, value)
|
Purpose:
|
Replaces the value of the header that has the given key with a given string.
|
Input:
|
key, value as String
|
Output:
|
–
|
set_request_uri(uri, arguments)
|
Purpose:
|
Replaces the request URI with the given one. Current URI arguments are also replaced with the given ones.
The parameter arguments is a list of tuples.
If, for example, you want to set the URI for the current request to:
/index.html?a=b&c=d
you would call:
set_request_uri('/index.html',[(a,b),(c,d)])
|
Input:
|
URI as String, arguments as list of tuples
|
Output:
|
–
|
set_response_cookie(key, value)
|
Purpose:
|
Sets the response cookie (to browser) that has the name key to the given value.
|
Input:
|
key, value as String
|
Output:
|
–
|
set_response_header(key, value)
|
Purpose:
|
Unlike the function set_request_header, sets a header for the response
You can also do that by enabling response filtering and then calling the function set_response_header during a response, but in this case the whole response is filtered.
|
Input:
|
key, value as String
|
Output:
|
–
|
set_returncode(code)
|
Purpose:
|
Aborts the current request or response and returns the given return code.
(For a list of possible codes, see HTTP Error Codes.)
|
Input:
|
Integer
|
Output:
|
–
|
terminate_session()
|
Purpose:
|
Ends the secure session that has been established between Stingray Application Firewall and the web application (see Session Handler, Cookie Jar Handler).
A typical scenario is the implementation of a log out function via Stingray Application Firewall for a web application that doesn’t provide a manual log out option.
Example:
| 1. | Create the path /logout. |
| 2. | To this path, add the Script Handler and call the function http.terminate_session(). |
| 3. | In your web application, provide the following link to log out: <a href="/logout">Logout</a>. |
|
Input:
|
–
|
Output:
|
–
|
Functions accessible during responses in detail
add_response_header(key, value)
|
Purpose:
|
Adds a response header that has the name key to the given value.
(See also add_response_header function during requests.)
|
Input:
|
key, value as String
|
Output:
|
–
|
del_response_header(key)
|
Purpose:
|
Removes the response header that has the name key.
(See also del_response_header function during requests.)
|
Input:
|
key as String
|
Output:
|
–
|
get_request_args()
|
Purpose:
|
Returns the request arguments as a list of tuples:
[(key1,value1),(key2,value2),...]
|
Input:
|
–
|
Output:
|
list of tuples [(key1,value1),(key2,value2),...]
|
get_request_uri()
|
Purpose:
|
Returns the requested URI.
|
Input:
|
–
|
Output:
|
String
|
get_response_body()
|
Purpose:
|
Returns the full body of the current response.
This only works if a response body does actually exist and if response filtering was triggered for the full response, not only for the headers (see function filter_response_full during requests).
|
Input:
|
–
|
Output:
|
body as String
|
get_response_cookies()
|
Purpose:
|
Returns a dict with all cookies: { cookie-key1 : cookie-val1, cookie-key2 : cookie-val2 }
|
Input:
|
–
|
Output:
|
dict { cookie-key1 : cookie-val1, cookie-key2 : cookie-val2 }
|
get_response_header(key)
|
Purpose:
|
Returns the value of the response header stated by the parameter key.
If this header doesn’t exist, the function returns None.
|
Input:
|
key as String
|
Output:
|
value as String
|
get_returncode()
|
Purpose:
|
Returns the return code for the current response.
|
Input:
|
–
|
Output:
|
return code as String
Other than you might expect, the output is not an Integer value. Thus, a correct statement would be, for example:
if http.get_returncode() == "200":
Mind the quotation marks.
|
get_storage()
|
Purpose:
|
Returns a dict that can be used as data storage throughout a session.
The Session Handler needs to be enabled for this to work.
|
Input:
|
–
|
Output:
|
dictionary
|
is_request()
|
Purpose:
|
Returns True if the current script is executed during a request. Else the result is False.
The statement not is_response() returns the same result.
|
Input:
|
–
|
Output:
|
Boolean
|
is_response()
|
Purpose:
|
Returns True if the current script is executed during a response. Else the result is False.
The statement not is_request() returns the same result.
|
Input:
|
–
|
Output:
|
Boolean
|
log(string1, string2, string3, ..., string n)
|
Purpose:
|
Concatenates the given list of strings (at least one) and writes the result to the Stingray Application Firewall Log Files.
|
Input:
|
list of String
|
Output:
|
–
|
make_random_cookie()
|
Purpose:
|
Generates a random string, which can be used as the name of a cookie.
|
Input:
|
–
|
Output:
|
String
|
redirect(url)
|
Purpose:
|
Redirects to the given URL.
|
Input:
|
URL as String
|
Output:
|
–
|
set_response_body(body)
|
Purpose:
|
Sets the response body to the given string.
|
Input:
|
body as String
|
Output:
|
–
|
set_response_cookie(key, value)
|
Purpose:
|
Sets the response cookie (to browser) that has the given key to value.
(See also set_cookie_response function during requests.)
|
Input:
|
key, value as String
|
Output:
|
–
|
set_response_header(key, value)
|
Purpose:
|
Sets the response header that has the name key to the given value.
(See also set_response_header function during requests.)
|
Input:
|
key, value as String
|
Output:
|
–
|
set_returncode(code)
|
Purpose:
|
Aborts the current request or response and returns the given return code.
(For a list of possible codes, see HTTP Error Codes.)
|
Input:
|
Integer
|
Output:
|
–
|
terminate_session()
|
Purpose:
|
Ends the secure session that has been established between Stingray Application Firewall and the web application (see Session Handler, Cookie Jar Handler).
A typical scenario is the implementation of a log out function via Stingray Application Firewall for a web application that doesn’t provide a manual log out option.
Example:
| 1. | Create the path /logout. |
| 2. | To this path, add the Script Handler and call the function http.terminate_session(). |
| 3. | In your web application, provide the following link to log out: <a href="/logout">Logout</a>. |
|
Input:
|
–
|
Output:
|
–
|
Expand /Collapse All Subsections
The following simple example script adds some own content (e.g. a company logo) to the end of each page:
if http.is_request():
http.filter_response_full()
else:
ct = http.get_response_header("content-type")
if ct and ct.split(';')[0].lower() == "text/html":
body = http.get_response_body()
if len(body) > 0:
try:
idx = body.upper().index("</BODY>")
newbody = []
newbody.append(body[:idx])
newbody.append("<BR/><IMG SRC='logo.gif'/></BODY>")
newbody.append(body[idx+len("</BODY>"):])
http.set_response_body("".join(newbody))
except ValueError:
http.log("no closing html body tag found")
|
|
The following script takes the request URI (e.g. /image/mandelbrot1.gif) and translates it to /image_handler.php with the subpath argument set to the rest of the URI (e.g. /mandelbrot1.gif):
if http.is_request():
uri = http.get_request_uri()
path = [ s for s in uri.split('/') if len(s) > 0 ]
if len(path) > 0:
uri = "/%s_handler.php" % path[0]
subpath = '/'
if len(path) > 1:
subpath += '/'.join(path[1:])
http.set_request_uri(uri,[("subpath",subpath)])
|
|
The following script retrieves a cookie given in a request. If the cookie isn’t valid in the session, the script redirects to a given URL. If no cookie has been given at all, the script generates and sets a random response cookie.
if http.is_request():
s = http.get_storage()
c = http.get_request_cookie('MYVALIDATOR')
if 'MYVALIDATOR' in s:
if s['MYVALIDATOR'] != c:
http.log('found session with invalid validator')
http.redirect('/')
elif c:
s['MYVALIDATOR'] = c
else:
s['MYVALIDATOR'] = http.make_random_cookie()
http.set_response_cookie('MYVALIDATOR', s['MYVALIDATOR'])
|
|
If a user calls the URL /honeypot, the following script adds the IP address of the request to the gobal IP blacklist for a duration of 5 minutes (300 seconds).
if http.is_request() and http.get_url() == "/honeypot":
ip = http.get_client_ip()
http.generate_blacklist_event(ip, 300)
|
The following is a simple example of how to verify some incoming XML requests against a given XML Schema:
xml_schema_string = '''
...
'''
if http.is_request() and http.get_request_method() == 'POST'
and http.get_request_header('content-type').lower() == 'text/xml':
xml_body = str(http.get_request_body()).strip()
xml = lxml_etree.parse(StringIO(xml_body))
xml_schema = lxml_etree.XMLSchema(lxml_etree.parse
(StringIO(xml_schema_string.strip())))
if not xml_schema.validate(xml):
http.log("xml request does not match schema")
http.set_returncode(403)
|
The following example validates the XML against a defined XML DTD. If the XML is valid, the script writes the value true to the Stingray Application Firewall Log Files.
xml_string = '''<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>''';
dtd_string = '''<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>'''
xml = lxml_etree.XML(xml_string)
dtd_io = StringIO(dtd_string)
dtd = lxml_etree.DTD(dtd_io)
http.log(str(dtd.validate(xml)))
|
The following example validates the XML against a given XML Schema file. If the XML is valid, the script writes the value true to the Stingray Application Firewall Log Files.
xml_string = '''<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>''';
schema_string = '''<xsd:schema xmlns:xsd="http://www.w3.org/
2001/XMLSchema">
<xsd:element name="note" type="noteType"/>
<xsd:complexType name="noteType">
<xsd:sequence>
<xsd:element name="to" type="xsd:string" />
<xsd:element name="from" type="xsd:string" />
<xsd:element name="heading" type="xsd:string" />
<xsd:element name="body" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>'''
xml_io = StringIO(xml_string)
schema_io = StringIO(schema_string)
schema_doc = lxml_etree.parse(schema_io)
schema = lxml_etree.XMLSchema(schema_doc)
xml_doc = lxml_etree.parse(xml_io)
http.log(str(schema.validate(xml_doc)))
|
|
|
|