Web Security, part II

Note: These lecture notes were slightly modified from the ones posted on the 6.858 course website from 2014.

Last lecture, we looked at a core security mechanism for the web: the same-origin policy.

In this lecture, we'll continue to look at how we can build secure web applications.

"Shell shock"-like exploits

The recent "Shell Shock" bug is a good example of how difficult it is to design web services that compose multiple technologies.

A web client can include extra headers in its HTTP requests, and determine which query parameters are in a request. Example:

GET /query.cgi?searchTerm=cats HTTP 1.1
Host: www.example.com
Custom-header: Custom-value

CGI servers map the various components of the HTTP request to Unix environment variables.

Vulnerability: Bash has a parsing bug in the way that it handles the setting of environment variables! If a string begins with a certain set of malformed bytes, bash will continue to parse the rest of the string and execute any commands that it finds! For example, if you set an environment variable to a value like this...

() { :;};  /bin/id

...will confuse the bash parser, and cause it to execute the /bin/id command (which displays the UID and GID information for the current user).

Live demo:

Step 1: Run the CGI server.
  ./victimwebserver.py 8082

Step 2: Run the exploit script.
  ./shellshockclient.py localhost:8082 index.html

More information: http://seclists.org/oss-sec/2014/q3/650

Cross-site scripting (XSS) attacks

Shell Shock is a particular instance of security bugs which arise from improper content sanitzation. Another type of content sanitzation failure occurs during cross-site scripting attacks (XSS).

Example: Suppose that a CGI script embeds a query string parameter in the HTML that it generates.

Demo:

Step 1: Run the CGI server.
   ./cgiServer.py

Step 2: In browser, load these URLs:
   http://127.0.0.1:8282/cgi-bin/uploadRecv.py?msg=hello
   http://127.0.0.1:8282/cgi-bin/uploadRecv.py?msg=<b>hello</b>
   http://127.0.0.1:8282/cgi-bin/uploadRecv.py?msg=<script>alert("XSS");</script>
           //The XSS attack doesn't work for this one . . .
           //we'll see why later in the lecture.

   http://127.0.0.1:8282/cgi-bin/uploadRecv.py?msg=<IMG """><SCRIPT>alert("XSS")</SCRIPT>">

           //This works! [At least on Chrome 37.0.2062.124.]
           //Even though the browser caught the
           //straightforward XSS injection, it
           //incorrectly parsed our intentionally
           //malformed HTML.
           // [For more examples of XSS exploits via
           //  malformed code, go here:
           //      https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet
           // ]

Why is cross-site scripting so prevalent?

XSS defenses

SQL injection attacks

You can also run into problems if untrusted entities can supply filenames.

Django

Session management: cookies

Dos and Don'ts of Client Authentication on the Web

Zoobar, Django, and many web frameworks put a random session ID in the cookie.

Stateless cookies

Alternatives to cookies for session management

HTTP protocol ambiguities

The web stack has some protocol ambiguities that can lead to security holes.

Covert channel attacks

Web applications are also vulnerable to covert channel attacks.

A web page also needs to use postMessage() securely.

There are many other aspects to building a secure web application.