Chapter 2
Chapter 2
2
To become an expert on web security, you
need a firm grasp of the internet’s under-
lying web technologies and protocols. This
chapter examines the Internet Protocol Suite,
which dictates how computers exchange data over the
web. You’ll also learn about stateful connections and
encryption, which are key elements of the modern web. I’ll highlight where
security holes tend to appear along the way.
NOTE TCP remains the most common protocol because of its delivery guarantees, but
nowadays, several other protocols are also used over the internet. The User Datagram
Protocol (UDP), for instance, is a newer protocol that deliberately allows packets to
be dropped so that data can be streamed at a constant rate. UDP is commonly used for
streaming live video, since consumers prefer a few dropped frames over having their
feed delayed when the network gets congested.
6 Chapter 2
domain name server, for instance, has the address 8.8.8.8. Because IPv4
addresses are getting used up at a rate that isn’t sustainable, the internet is
shifting to IP version 6 (IPv6) addresses to allow for more connected devices,
represented as eight groups of four hexadecimal digits separated by colons
(for example: 2001:0db8:0000:0042:0000:8a2e:0370:7334).
Application layer DNS FTP HTTP IMAP POP SMTP SSH XMPP
Figure 2-1: The various layers that make up the internet protocol suite
HTTP Requests
An HTTP request sent by a browser consists of the following elements:
Method Also known as a verb, this describes the action that the user
agent wants the server to perform.
Universal resource locator (URL) This describes the resource being
manipulated or fetched.
Headers These supply metadata such as the type of content the user
agent is expecting or whether it accepts compressed responses.
Body This optional component contains any extra data that needs to
be sent to the server.
Listing 2-1 shows an HTTP request.
GETu http://example.com/v
w User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
x Accept: text/html,application/xhtml+xml,application/xml; */*
8 Chapter 2
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
The method u and the URL v appear on the first line. These are fol-
lowed by HTTP headers on separate lines. The User-Agent header w tells the
website the type of browser that is making the request. The Accept header x
tells the website the type of content the browser is expecting.
GET requests are the most common type of request on the internet—
they request a particular resource on the web server, identified by a specific
URL. The response to a GET request will contain a resource: perhaps a
web page, an image, or even the results of a search request. The example
request in Listing 2-1 represents an attempt to load the home page of
example.com, and would be generated when a user types example.com in the
browser’s navigation bar.
If the browser needs to send information to the server, rather than just
fetch data, it typically uses a POST request. When you fill out a form on a web
page and submit it, the browser sends a POST request. Because POST requests
contain information sent to the server, the browser sends that information
in a request body, after the HTTP headers.
In Chapter 8, you’ll see why it’s important to use POST rather than GET
requests when sending data to your server. Websites that erroneously use
GET requests for doing anything other than retrieving resources are vul-
nerable to cross-site request forgery attacks.
When writing a website, you may also encounter PUT, PATCH, and DELETE
requests. These are used to upload, edit, or delete resources on the server,
respectively, and are typically triggered by JavaScript embedded in a web
page. Table 2-1 documents a handful of other methods that are worth
knowing about.
HTTP Responses
HTTP responses sent back by a web server begin with a protocol descrip-
tion, a three-digit status code, and, typically, a status message that indicates
whether the request can be fulfilled. The response also contains headers
providing metadata that instructs the browser how to treat the content.
Finally, most responses contain a body that itself contains the requested
resource. Listing 2-2 shows the contents of a simple HTTP response.
y <!doctype html>
<html>
<head>
<title>Example Domain</title>
z <style type="text/css">
body {
background-color: #f0f0f2;
font-family: "Open Sans", "Helvetica Neue", Helvetica, sans-serif;
}
div {
width: 600px;
padding: 50px;
background-color: #fff;
border-radius: 1em;
}
</style>
</head>
{ <body>
<div>
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples.</p>
<p>
<a href="http://www.iana.org/domains/example">More information...</a>
</p>
</div>
</body>
</html>
Listing 2-2: An HTTP response from example.com, the world’s least interesting website
The response begins with the protocol description u, the status code v,
and the status message w. Status codes formatted as 2xx indicate that the
request was understood, accepted, and responded to. Codes formatted as
10 Chapter 2
3xx redirect the client to a different URL. Codes formatted as 4xx indicate
a client error: the browser generated an apparently invalid request. (The
most common error of this type is HTTP 404 Not Found). Codes formatted as
5xx indicate a server error: the request was valid, but the server was unable
to fulfill the request.
Next are the HTTP headers x. Almost all HTTP responses include a
Content-Type header that indicates the kind of data being returned. Responses
to GET requests also often contain a Cache-Control header to indicate that the
client should cache large resources (for example, images) locally.
If the HTTP response is successful, the body contains the resource the
client was trying to access as well as HyperText Markup Language (HTML) y
describing the structure of the requested web page. In this case, the response
contains styling information z as well as the page content itself {. Other
types of responses may return JavaScript code, Cascading Style Sheets
(CSS) used for styling HTML, or binary data in the body.
Stateful Connections
Web servers typically deal with many user agents at once, but HTTP does
nothing to distinguish which requests are coming from which user agent.
This wasn’t an important consideration in the early days of the internet,
because web pages were largely read-only. Modern websites, however, often
allow users to log in and will track their activity as they visit and interact
with different pages. To allow for this, HTTP conversations need to be
made stateful. A connection or conversation between a client and a server
is stateful when they perform a “handshake” and continue to send packets
back and forth until one of the communicating parties decides to terminate
the connection.
When a web server wants to keep track of which user it’s responding to
with each request, and thus achieve a stateful HTTP conversation, it needs
to establish a mechanism to track the user agent as it makes the subsequent
requests. The entire conversation between a particular user agent and a
web server is called an HTTP session. The most common way of tracking ses-
sions is for the server to send back a Set-Cookie header in the initial HTTP
response. This asks the user agent receiving the response to store a cookie,
a small snippet of text data pertaining to that particular web domain. The
user agent then returns the same data in the Cookie header of any subse-
quent HTTP request to the web server. If implemented correctly, the con-
tents of the cookie being passed back and forth uniquely identify the user
agent and hence establish the HTTP session.
Session information contained in cookies is a juicy target for hackers.
If an attacker steals another user’s cookie, they can pretend to be that user
on the website. Similarly, if an attacker successfully persuades a website to
accept a forged cookie, they can impersonate any user they please. We’ll
look at various methods of stealing and forging cookies later in the book.
Summary
In this chapter, you learned about the plumbing of the internet. TCP
enables reliable communication between internet-connected computers
that each have an IP address. The Domain Name System provides human-
readable aliases for IP addresses. HTTP builds on top of TCP to send
HTTP requests from user agents (such as web browsers) to web servers,
which in turn reply with HTTP responses. Each request is sent to a specific
URL, and you learned about various types of HTTP methods. Web servers
respond with status codes, and send back cookies to initiate stateful con-
nections. Finally, encryption (in the form of HTTPS) can be used to secure
communication between a user agent and a web server.
In the next chapter, you’ll take a look at what happens when a web
browser receives an HTTP response—how a web page is rendered, and how
user actions can generate more HTTP requests.
12 Chapter 2