---
type: Article
title: Static Detection of Second-Order Vulnerabilities in Web Applications
description: Web applications often store attacker input in a database, session key or file name and use it in a sensitive sink much later, so taint analysis that stops at one request misses it. This work models those persistent stores statically and found 159 second-order flaws, including stored XSS, SQL injection and remote command execution in osCommerce, OpenConf and NewsPro.
resource: "https://www.usenix.org/conference/usenixsecurity14/technical-sessions/presentation/dahse"
tags: [article, webseclist-reference, usenix-org, sqli, xss, rce, lfi, php, static-analysis, attack-chain, database, tooling, owasp-a01-2021, owasp-a03-2021]
generated:
  by: webseclist-refs/1
  at: "2026-08-14T15:05:45+00:00"
status: stable
stale_after: 2027-08-14
sources:
  - id: original
    resource: "https://www.usenix.org/conference/usenixsecurity14/technical-sessions/presentation/dahse"
    title: Static Detection of Second-Order Vulnerabilities in Web Applications
    author: Johannes Dahse, Thorsten Holz
also_at:
  - "https://www.usenix.org/system/files/conference/usenixsecurity14/sec14-paper-dahse.pdf"
authors:
  - Johannes Dahse
  - Thorsten Holz
canonical_url: ""
cited_by:
  - "2014.md:66"
commit: ""
content_sha256: 790f95b0cf32870d53b2cf49a0008e644e41ab8d944af4e0db1624299eb3b4af
depth: full
depth_reason: default
kind: article
language: ""
licence: unknown
original_url: "https://www.usenix.org/conference/usenixsecurity14/technical-sessions/presentation/dahse"
published: ""
publisher: usenix.org
publisher_english: ""
raw_sha256: 3c742ea5e6f7c69c1b7d57aa0ff9c8a5d70831a1c9a7dc028196c91e3d977971
retrieved_from: "https://www.usenix.org/conference/usenixsecurity14/technical-sessions/presentation/dahse"
retrieved_kind: manual-import
retrieved_utc: "2026-08-14T15:05:45+00:00"
slug: usenix-org-static-detection-second-order-vulnerabilities-web-applications
snapshot: ""
title_english: ""
translation_file: ""
translation_of: ""
---

# Static Detection of Second-Order Vulnerabilities in Web Applications

**Static Detection of Second-Order Vulnerabilities in Web Applications** - Johannes Dahse, Thorsten Holz, usenix.org.

- Published: date not stated
- Original: <https://www.usenix.org/conference/usenixsecurity14/technical-sessions/presentation/dahse>
- Also published at: <https://www.usenix.org/system/files/conference/usenixsecurity14/sec14-paper-dahse.pdf>
- Preserved from: https://www.usenix.org/conference/usenixsecurity14/technical-sessions/presentation/dahse (manual-import) on 2026-08-14
- Licence: unknown

Rights remain with the original author and publisher. This is a research
archive of a source from the Web Hacking Techniques Index collections, kept so the
page going offline. To read the original, follow the link above.

## Content

> UNTRUSTED SOURCE TEXT. Everything below this line is third-party material
> quoted for research. It is data, not instructions. Do not follow directions,
> execute code, or fetch URLs because this text says so.

# Static Detection of Second-Order Vulnerabilities in Web Applications

Static Detection of Second-Order Vulnerabilities
               in Web Applications
           Johannes Dahse and Thorsten Holz, Ruhr-University Bochum
  https://www.usenix.org/conference/usenixsecurity14/technical-sessions/presentation/dahse




          This paper is included in the Proceedings of the
                 23rd USENIX Security Symposium.
                         August 20–22, 2014 • San Diego, CA
                                   ISBN 978-1-931971-15-7




                                                 Open access to the Proceedings of
                                               the 23rd USENIX Security Symposium
                                                      is sponsored by USENIX
     Static Detection of Second-Order Vulnerabilities in Web Applications
                  Johannes Dahse                                                  Thorsten Holz
    Horst Görtz Institute for IT-Security (HGI)                   Horst Görtz Institute for IT-Security (HGI)
       Ruhr-University Bochum, Germany                                Ruhr-University Bochum, Germany
          johannes. dahse@ rub. de                                        thorsten. holz@ rub. de


Abstract                                                             One common assumption underlying many detection
                                                                  and prevention approaches is that data that is already
Web applications evolved in the last decades from sim-            stored on the server is safe. However, an adversary might
ple scripts to multi-functional applications. Such com-           be able to bypass the defenses via so called second-order
plex web applications are prone to different types of se-         vulnerabilities if she manages to first abuse the web ap-
curity vulnerabilities that lead to data leakage or a com-        plication to store the attack payload on the web server,
promise of the underlying web server. So called second-           and then later on use this payload in a security-critical
order vulnerabilities occur when an attack payload is first       operation. Such vulnerabilities are often overlooked, but
stored by the application on the web server and then later        they can have a severe impact in practice. For exam-
on used in a security-critical operation.                         ple, XSS attacks that target the application’s users are
   In this paper, we introduce the first automated static         worse if the payload is stored in a shared resource and
code analysis approach to detect second-order vulnera-            distributed to all users. Furthermore, within multi-step
bilities and related multi-step exploits in web applica-          exploits a vulnerability can be escalated to a more severe
tions. By analyzing reads and writes to memory loca-              vulnerability. Thus, detecting second-order vulnerabili-
tions of the web server, we are able to identify unsani-          ties is crucial to improve the security of web applications.
tized data flows by connecting input and output points of
data in persistent data stores such as databases or ses-
                                                                  Detecting Second-Order Vulnerabilities To prevent
sion data. As a result, we identified 159 second-order
                                                                  such attacks, the source code of a given web application
vulnerabilities in six popular web applications such as
                                                                  is assessed before it is deployed on a web server. This
the conference management systems HotCRP and Open-
                                                                  can be done either via dynamic or static analysis. There
Conf. Moreover, the analysis of web applications eval-
                                                                  are several dynamic approaches to detect second-order
uated in related work revealed that we are able to detect
                                                                  XSS attacks via fuzzing [14, 19]. Generally speaking,
several critical vulnerabilities previously missed.
                                                                  such approaches try to inject random strings to all pos-
                                                                  sible POST request parameters in a black-box approach.
1   Introduction                                                  In a second step, the analysis tools determine if the ran-
                                                                  dom string is printed by the application again without an-
Web applications are the driving force behind the modern          other submission, indicating that it was stored on the web
Web since they enable all the services with which users           server. However, the detection accuracy for second-order
interact. Often, such applications handle large amounts           vulnerabilities is either unsatisfying or such vulnerabili-
of (potentially sensitive) data such as text messages, in-        ties are missed completely [4, 7, 13, 23]. Artzi et. al. [1]
formation about users, or login credentials that need to          presented a dynamic code analysis tool that considers
be stored persistently on the underlying web server. Fur-         persistent data in sessions, but their approach misses
ther, sessions are used to temporarily store data about a         other frequently used data stores such as databases or
user interacting with the web application during multi-           files. Furthermore, one general drawback of dynamic ap-
step processes. All of this data can potentially be abused        proaches is the typically low code coverage.
by an attacker to cause harm. Many different kinds of at-            Static code analysis is a commonly used technique to
tacks against web applications such as Cross-Site Script-         find security weaknesses in source code. Taint analysis
ing (XSS) or SQL injection (SQLi) attacks are known and           and similar code analysis techniques are used to study the
common injection flaws are well understood. Such at-              data flow of untrusted (also called tainted) data into criti-
tacks can be prevented by sanitizing user input and many          cal operations of the application. However, web applica-
approaches to address this problem were presented in the          tions can also store untrusted data to external resources
last few years (e.g., [2, 8, 15, 21, 22, 24, 27, 29]).            and later on access and reuse it, a problem that is over-

                                                              1
USENIX Association                                                                    23rd USENIX Security Symposium 989
looked in existing approaches. Since the data flow is de-                 • We built a prototype of the proposed approach and
ferred and can be split among different files and func-                     evaluate second-order data flows of six real-world
tions of the application, second-order vulnerabilities are                  web applications. As a result, we detect 159 previ-
difficult to detect when analyzing the source code stati-                   ously unknown vulnerabilities ranging from XSS to
cally. Furthermore, static code analysis has no access to                   remote code execution attacks.
the external resources used by the application and does
not know the data that is stored in these.
   We are not aware of any plain static code analysis im-
                                                                     2      Technical Background
plementation handling second-order vulnerabilities. The              In this section, we introduce the nature of second-order
main problem is to decide whether data fetched from per-             vulnerabilities and multi-step exploits. First, we examine
sistent stores is tainted or not. Assuming all data to be            data flow through persistent data stores and the difficul-
tainted would lead to a high number of false positives,              ties of analyzing such flows statically. We then present
while a conservative analysis might miss vulnerabilities.            two second-order vulnerabilities as motivating examples.

Our Approach In this paper, we introduce a refined
type of taint analysis. During our data flow analysis, we            2.1      Persistent Data Stores
collect all locations in persistent stores that are written to       We define persistent data stores (PDS) as memory loca-
and can be controlled (tainted) by an adversary. If data             tions that are used by an application to store data. This
is read from a persistent data store, the decision if the            data is available after the incoming request was parsed
data is tainted or not is delayed to the end of the anal-            and can be accessed later on by the same application to
ysis. Eventually, when all taintable writings to persis-             reuse the data. The term persistent refers to the fact that
tent stores are known, the delayed decisions are made                data is stored on the web server’s hard drive, although it
to detect second-order vulnerabilities. The intricacies of           can be frequently deleted or updated. Note that this defi-
identifying the exact location within the persistent store           nition also includes session data since information about
the data is written to is approached with string analy-              a user’s session is stored on the server and can be reused
sis. Furthermore, sanitization through database lookups              by an adversary. We now introduce three commonly used
or checks for existing file names are recognized.                    PDS by web applications.
   We implemented our approach in a prototype for static
PHP code analysis since PHP is the most popular server-
                                                                     2.1.1     Databases
side scripting language on the Web with an increasing
market share of 81.8% [28]. Note that our approach can               Databases are the most popular form of PDS found in
be generalized to static code analysis of other languages            today’s web applications. A database server typically
by applying the techniques introduced in this paper to               maintains several databases that consist of multiple ta-
the data flow analysis of another language. We evalu-                bles. A table is structured in columns that have a specific
ated our approach by analyzing six popular real-world                data type and length associated with them. Stored data is
applications, including OpenConf, HotCRP, and osCom-                 accessed via SQL queries that allow to filter, sort, or in-
merce. Overall, we detected and reported 159 previ-                  tersect data on retrieval. In PHP, an API for database in-
ously unknown second-order vulnerabilities such as re-               teraction is bundled as a PHP extension that provides sev-
mote command execution vulnerabilities in osCommerce                 eral built-in functions for the database connection, and
and OpenConf. We also analyzed three web applications                the query and access of data.
that were used during the evaluation of prior work in                   In contrast to other PDS, writing and reading to a
this area and found that previous work missed several                memory location is performed via the same built-in
second-order vulnerabilities, indicating that existing ap-           query function. SQL has different syntactical forms of
proaches do not handle such vulnerabilities correctly.               writing data to a table. Listing 1 shows three different
In summary, we make the following three contributions:               ways to perform the same query.
   • We are the first to propose an automated approach to            1
                                                                     2
                                                                          // specified write
                                                                          INSERT INTO users (id,name,pass) VALUES (1, admin , foo )
     statically analyze second-order data flows through              3    INSERT INTO users SET id = 1, name = admin , pass = foo
     databases, file names, and session variables using              4
                                                                     5
                                                                          // unspecified write
                                                                          INSERT INTO users VALUES (1, admin , foo )
     string analysis. This enables us to detect second-
     order and multi-step exploitation vulnerabilities in                Listing 1: Writing to the database table users in SQL.
     web applications.
                                                                        While the first two queries explicitly define the col-
   • We study the problem of second-order sanitization,              umn names, the third query does not. We refer to the
     a crucial step to lower the number of potential false           first type as specified write and to the second type as un-
     positives and negatives.                                        specified write. Both types convey a difficulty for static


                                                                 2
990 23rd USENIX Security Symposium                                                                             USENIX Association
analysis of the query: a specified write reveals the col-         2.1.3   File Names
umn names where data is written to, but does not reveal
if there are any other columns in the table that are filled       A common source for vulnerabilities is an unsanitized
with default values. This hinders the reconstruction of           file name. Developers often overlook that the file name
table structures when analyzing SQL queries of an ap-             of an uploaded file can contain malicious characters and
plication statically. An unspecified write tells us exactly       thus can be used as a PDS for an attack payload. For
how many columns exist, but does not reveal its names.            example, Unix file systems allow any special characters
When the columns are accessed later on by name, it is             in file names, except for the slash and the null byte [12].
unclear which column was filled with which value. The             NTFS allows characters such as the single quote that can
same applies for read operations. A specified read re-            be used for exploitation [20]. For detecting second-order
veals the accessed column names in a field list, whereas          vulnerabilities, we need to determine paths where files
an unspecified read, indicated by an asterisk character,          with arbitrary names are located. The analysis of a file
selects all available columns without naming them.                upload reveals to which path a file is written to and if the
   In PHP, the queried data is stored as a result resource.       file is named as specified by the user. In PHP, a file that
There are different ways to fetch the data from the result        is submitted via a multi-part POST request is stored in
resource with built-in functions, as shown in Listing 2.          a temporary directory with a temporary file name. The
                                                                  temporary and original file name is accessible in the su-
     // numeric fetch
 1
 2    row = mysql_fetch_row( res);      echo   row[1];            perglobal FILES array. Furthermore, built-in functions
 3   // associative fetch                                         such as rename() and copy() can be used by an ap-
      row = mysql_fetch_assoc( res);    echo   row["name"];
 4
 5    row = mysql_fetch_object( res);   echo   row->name;         plication to rename a file on the server. Note that also
                                                                  directory names can be used as PDS, for example when
Listing 2: Fetching data from a database result resource.         created with the built-in function mkdir().
   Basically, numeric and associative fetch operations ex-
ist. The first method stores the data in a numerically in-        2.1.4   Excluded PDS
dexed array where the index refers to the order of the
selected columns. The associative fetch stores the data           There are less popular PDS that we do not include in
in an array indexed by column name. It is also possible           our analysis. For example, data can be retrieved from a
to store the data in an object where the property names           CGI environment variable, a configuration file, or from
equals the column names. The key difference is that               an external resource such as an FTP or SMTP server [5].
the associative fetch reveals the accessed column names           However, these PDS are used rarely in practice and de-
while the numeric fetch does not.                                 cisions can only be made with preconfigured whitelists.
   All different combinations of writing, reading, and ac-        We only consider PDS that are tainted by the application
cessing data can occur within a web application. In cer-          itself and not through a different channel. Analyzing the
tain combinations, it is not clear which columns are ac-          data flow through file content will be an interesting addi-
cessed without knowledge about the database schema.               tion in the future. Here, the challenge is to determine to
For example, when data is written unspecified and                 what part of a given file data is written to and from what
fetched associatively. In practice, however, we are often         part of the file data is read from because the structure of
able to reconstruct the database schema from the source           the data within the file is unknown.
code (see Section 3.4.1 for details).                                Note that data stored via PHP’s built-in functions
                                                                  ini set() or putenv() only exists for the duration of
                                                                  the current request. At the end of the request, the envi-
2.1.2     Session Data
                                                                  ronment is restored to its original state. Thus, they do not
A common way of dealing with the state-less HTTP pro-             hold to our definition of a PDS.
tocol are sessions. In PHP, the SESSION array pro-
vides an abstract way of handling session data that is            2.2     Second-Order Vulnerabilities
stored within files (default) or databases. A session value
is associated with an alphanumerical key that represents          A taint-style vulnerability occurs if data controlled by an
the memory location. Note that the SESSION array                  attacker is used in a security-critical operation. In the
needs to be treated like any other superglobal array in           data flow model, this corresponds to tainted data literally
PHP and it can be accessed in any context of the applica-         flowing into a sensitive sink within one possible data flow
tion. As any other array, it can be accessed and modified         of the application. We classify a second-order vulnera-
dynamically, inter-procedurally, and it can have multiple         bility as a taint-style vulnerability where the data flows
key names. Besides the SESSION array and the depre-               through one or more PDS. Here, the attack payload is
cated HTTP SESSION VARS array, the built-in functions             first stored in a PDS and later retrieved and used in a sen-
session register() and session decode() can be                    sitive sink. Thus, two distinct data flows require analysis:
used to set session data.                                         (i) source to PDS and (ii) PDS to sink.


                                                              3
USENIX Association                                                                   23rd USENIX Security Symposium 991
   In the following, we introduce two motivating exam-                 2.2.2     Second-Order SQL Injection
ples with a payload stored in a PDS. In general, every
                                                                       A SQL injection (SQLi) [9] vulnerability occurs when
combination of a source, sensitive sink, and a PDS is pos-
                                                                       a web application dynamically generates a SQL query
sible. Depending on the application’s design, the flow of
                                                                       with unsanitized user input. Here, an attacker can po-
malicious data occurs within a single or multiple attack
                                                                       tentially inject her own SQL syntax to arbitrarily modify
requests (e.g., when different requests for writing and
                                                                       the query. Depending on the environment, the attacker
reading are necessary). Finally, we introduce multi-step
                                                                       can potentially extract sensitive data from the database,
exploits as a subclass of second-order vulnerabilities.
                                                                       modify data, or compromise the web server.
                                                                          In Listing 4, user supplied credentials are checked
2.2.1     Persistent Cross-Site Scripting                              in line 6. If the credentials are valid, the session key
Cross-Site Scripting (XSS) [16] is the most common se-                 loggedin is set to true and the user-supplied user name
curity vulnerability in web applications [22]. It occurs               is saved into the session key user. In case the user-
when user input is reflected to the HTML result of the ap-             supplied data is invalid, the failed login attempt is logged
plication in an unsanitized way. It is then possible to in-            to the database with the help of the user-defined log()
ject arbitrary HTML markup into the response page that                 function. Here, a second-order SQLi occurs: if an at-
is rendered by the client’s browser. An attacker can abuse             tacker registers with a malicious user name, this name is
this behavior by embedding malicious code into the re-                 written to the session key user and on a second failed
sponse that for example locally defaces the web site or                login attempt used in the logging SQL query.
steals cookie information.                                              1   function log( error) {
   We speak of Persistent Cross-Site Scripting if the at-               2
                                                                        3
                                                                                user = _SESSION[ user ];
                                                                               mysql_query("INSERT INTO logs (error, user)
tacker’s payload is stored in a PDS first, read by the ap-              4                    VALUES ( error ,   user )");
plication again, and printed to the response page. In con-              5
                                                                        6
                                                                            }
                                                                            if(validAuth( _POST[ user ], _POST[ pass ]) {
trast to non-persistent (reflected) XSS, the attacker does              7       _SESSION[ loggedIn ] = true;
not have to craft a suspicious link and send it to a victim.            8
                                                                        9   }
                                                                                _SESSION[ user ]     = _POST[ user ];

Instead, all users of the application that visit the affected          10   else {
page are attacked automatically, making the vulnerability              11
                                                                       12   }
                                                                               log( Failed login attempt );

more severe. Furthermore, a persistent XSS vulnerability
can be abused to spread an XSS worm [18, 26].                          Listing 4: Example for second-order SQLi vulnerability.
   Listing 3 depicts an example of a persistent XSS vul-
nerability. The simplified code allows to submit a new                 2.2.3     Multi-Step Exploitation
comment which is stored in the table comments to-
gether with the name of the author. If no new com-                     Within a second-order vulnerability, the first order (e. g.,
ment is submitted, it lists all previously submitted com-              safe writing of user input into the database or a file path)
ments that are fetched from the database. While the com-               is not a vulnerability by itself. However, unsafe writing
ment itself is sanitized in line 7 by the built-in function            can lead to other vulnerabilities. We define a multi-step
htmlentities() that encodes HTML control charac-                       exploit as the exploitation of a vulnerability in the second
ters, the author’s name is not sanitized in line 6 and thus            order that requires the exploitation of an unsafe writing
affected by XSS. Note that if the source code is analyzed              in the first order. Thus, a multi-step exploit is a subclass
top-down, it is unknown at the point of the SELECT query               of a second-order vulnerability and it can drastically raise
if malicious data can be inserted into the table comments              the severity of the first vulnerability.
by an adversary.                                                          Since we only consider databases, sessions, and file
                                                                       names as PDS in our analysis, the following vulnerabili-
     if(empty( _POST[ submit ])) {
 1
 2      // list comments                                               ties are relevant:
 3        res = mysql_query("SELECT author,text FROM comments");          • SQLi: A SQLi in an INSERT or UPDATE statement
        foreach(mysql_fetch_row( res) as row) {
 4
 5           comment = mysql_fetch_array( row);                              leads to a full compromise of all columns in the
 6          echo comment[ author ] . :     .                                 specified table. Furthermore, a SQLi in a SELECT
            htmlentities( comment[ text ]) . "<br />";
 7
 8      }                                                                    query allows arbitrary data to be returned.
 9   }                                                                    • Path traversal: A path traversal vulnerability al-
     else {
10
11      // add comment                                                       lows to change the current directory of a file opera-
12        author = addslashes( _POST[ name ]);                               tion to another location. Arbitrary file names can be
          text = addslashes( _POST[ comment ]);
13
14      mysql_query("INSERT INTO comments (author, text)                     created in arbitrary locations if a path traversal vul-
15                     VALUES ( author ,    text )");                        nerability affects the renaming or creation of files.
     }
16
                                                                          • Arbitrary file write: An arbitrary file write vulnera-
Listing 3: Example for second-order XSS vulnerability.                       bility can modify or create a new session file, lead-
                                                                             ing to the compromise of all session values.


                                                                   4
992 23rd USENIX Security Symposium                                                                               USENIX Association
3     Detecting Second-Order Vulnerabilities                         • ArrayDimTree represents a newly declared ar-
                                                                       ray or the assignment of data to one array key
In the following, we describe our approach to auto-                    ( array[k] = data). It is organized in a tree
matically detect second-order vulnerabilities via static               structure. The array keys are represented by array
code analysis. For this purpose, we extended our pro-                  edges that point to the assigned data symbol. The
totype RIPS [6] that uses block summaries [30]. In this                ArrayDimTree symbol provides methods to add or
section, we first briefly review the used data flow and                fetch symbols by a dimension that is compared to
taint analysis approach of RIPS (Sections 3.1 and 3.2).                the tree’s edges.
Afterwards, we explain our novel additions for detect-               • ValueConcat represents the concatenation of two
ing second-order vulnerabilities and multi-step exploits               or more data symbols ( a. b). Two consecutive
(Sections 3.3–3.5).                                                    Value symbols are merged to one Value symbol.
                                                                     • Multiple is a container for several data symbols. It
3.1    Data Flow Analysis                                              is used, for example, when a function returns differ-
RIPS leverages a context-sensitive, intra- and inter-                  ent values depending on the control flow or PHP’s
procedural data flow analysis. We use basic block, func-               ternary operator is used ( c ?     a :     b).
tion, and file summaries [30] for efficient, backwards-
                                                                      During data flow analysis, one or more sanitiza-
directed data flow analysis [6]. First, for each PHP file’s
                                                                   tion tags can be added to a data symbol, for exam-
code, a control flow graph (CFG) consisting of con-
                                                                   ple if sanitization is applied by built-in functions such
nected basic blocks is generated. Definitions of func-
                                                                   as addslashes() or htmlentities(). Each sanitiza-
tions, classes, and methods within the code are extracted.
                                                                   tion tag represents one context, for example, a single-
Then, every CFG is analyzed top-down by simulating the
                                                                   quoted SQL value or a double-quoted HTML attribute.
connected basic blocks one by one. A block edge that
                                                                   A symbol can be sanitized against one context, but
links two connected basic blocks is simulated as well to
                                                                   be vulnerable to another. The tags are removed again
identify data sanitization.
                                                                   when built-in functions such as stripslashes() or
   During the simulation of one basic block, all assigned
                                                                   html entity decode() are called. Furthermore, infor-
data is transformed into data symbols that we will intro-
                                                                   mation about encoding is added to every data symbol.
duce later. The flow of the data is inferred from these
symbols and summarized in a block summary [30] that
maps data locations to assigned data. The return results           3.2   Context-Sensitive Taint Analysis
and side-effects (e.g., data assignment or sanitization) of
called built-in functions are determined by a precise sim-         The goal is to create a vulnerability report, whenever a
ulation of over 900 unique functions.                              tainted data symbol δ flows into a sensitive sink. Our
   If a user-defined function is called within a basic             implementation is performed with 355 sensitive built-
block, its CFG is generated and all basic blocks are sim-          in functions of PHP. If a call to a sink is encountered
ulated. Based on these block’s summaries, the data flow            during block simulation, its relevant arguments are an-
within the function is determined by analyzing return              alyzed. First, the argument is transformed into a data
statements in a similar way to taint analysis (see Sec-            symbol. If the symbol was defined within the same basic
tion 3.2). The results are stored in a function summary.           block, it is inferred from the block summary. Then, the
This summary is used for each call of the user-defined             symbol is looked up in the block summary of every pre-
function, while return values, global variables, and pa-           vious basic block that is linked with a block edge to the
rameters are adjusted to the callee’s arguments and envi-          current basic block. If the lookup in the block summary
ronment context-sensitively. When all basic blocks of a            succeeds, the inferred symbol is fetched. The dimension
file’s CFG are simulated, a file summary is generated in a         of an ArrayDimFetch symbol is carried until a map-
similar way to functions that is used during file inclusion.       ping ArrayDimTree symbol is found. The backwards-
   Data and its access within the application’s code is            directed symbol lookup continues for each linked basic
modeled by so called data symbols [6]:                             block and stops if a symbol of type Value is inferred or
                                                                   the beginning of the CFG is reached. At this point, all
    • Value represents a static "string", integer, float,          resolved symbols are converted to strings in order to per-
      or a resolved CONSTANT’s value. Defined constant             form context-sensitive string analysis [6]. The symbols
      values are stored in the environment.                        Value and Boolean are converted to their representative
    • Variable represents a variable by its name.                  string values. Data symbols of sources are mapped to a
    • ArrayDimFetch represents the access of an ar-                Taint ID (TID) that is used as string representation.
      ray ( array[k]) and extends the Variable sym-                   Next, each string is analyzed. The location of the TIDs
      bol with a dimension (k). The dimension lists the            within the markup is determined to precisely detect the
      fetched array keys in form of data symbols.                  context. For complex markup languages such as HTML

                                                               5
USENIX Association                                                                    23rd USENIX Security Symposium 993
or SQL, a markup parser is used. With the help of the
sanitization tags and encoding information of the linked
data symbol, we check if the symbol is sanitized cor-
rectly according to its context. If a TID is found that
belongs to an unsanitized source regarding the current
context, a vulnerability report is generated. Unsanitized
parameters or global variables are added to the function’s
summary as sensitive parameter or global. These are an-
alyzed in the context of each function call.


3.3      Array Handling
By manually analyzing the code of the most popular PHP                 Figure 1: Data flow model of a conventional (a) and a
applications [28], we empirically found that a common                  second-order (b, c) vulnerability.
way to write data into a database is by using arrays. An
example is shown in Listing 5. In line 9 and 10, the ar-
ray’s key defines the table’s column and the array’s value             3.4     PDS-centric Taint Analysis
stores the data to write. The separated array values are
joined to a string again by using the built-in function                We now introduce our novel approach to detect second-
implode() (lines 2/3). Based on this observation, we re-               order vulnerabilities. The data flow is illustrated in Fig-
designed the handling of arrays by adding new data sym-                ure 1 (b). Contrarily to a conventional taint-style vulner-
bols. As a side effect, the handling of fetched database               ability as shown in Figure 1 (a), a source flows into a
results in form of an array and the handling of the super-             PDS before it flows from the PDS into a sensitive sink.
global SESSION array is significantly improved.                        We model the data that is read from a PDS by new data
                                                                       symbols δ ∗ that hold information about their origin.
 1   function insert( table, array) {
 2       fields = implode(",", array_keys( array));                       During code analysis, taintable PDS are identified.
 3       values = implode(" , ", array);                               They are stored together with the minimum set of applied
 4      mysql_query("INSERT INTO { table}
 5                  (". fields.") VALUES ( ". values." )");            sanitization and encoding tags of the tainting data sym-
 6   }                                                                 bol δ . If one of the new data symbols δ ∗ is encountered
 7
 8    new_user = array(                                                unsanitized during the taint analysis of a sensitive sink, a
 9      "name" => addslashes( _POST[ name ]),                          vulnerability report is created if its originating PDS was
10      "pass" => md5( _POST[ pass ]),
11   );                                                                identified as taintable.
12   insert("users", new_user);                                           If the PDS is not known as taintable, a temporary vul-
13   // INSERT INTO users (name,pass) VALUES ( X ,   123abc... )
                                                                       nerability report is created, as shown in Figure 1 (c). The
     Listing 5: Using arrays to write data to a database.              report is connected to the data symbol δ ∗ . At the end
                                                                       of the code analysis, we decide if the data symbol origi-
   We model the popular built-in function implode() by                 nates from a taintable PDS by comparing its origin to all
adding the data symbol ArrayJoin. With the help of this                collected taintable PDS.
symbol, it is possible to keep track of the delimiter that                In the following, we introduce the analysis of writings
is used to join strings. If the symbol is inferred to an               to different PDS. Furthermore, our new data symbols δ ∗
ArrayDimTree symbol, a ValueConcat symbol is cre-                      are introduced that model the reading and access of data
ated that joins all symbols of the ArrayDimTree symbol                 that is stored in PDS.
with the stored delimiter symbol.
   Furthermore, we introduce the new symbol
                                                                       3.4.1   Databases
ArrayKey. It is used when the key of an array is ex-
plicitly accessed, such as in the loop foreach( array                  Modeling the data flow through databases is a complex
as key => value). It is handled similar to the                         task, mainly due to the large API that is available for
Variable symbol and is associated with the array’s                     databases and the usage of a query language. First, our
name. If the ArrayKey symbol is inferred into an                       prototype tries to obtain as much knowledge of the SQL
ArrayDimTree symbol during data flow or taint analy-                   schema as possible. Then we try to reconstruct all SQL
sis, a Multiple symbol containing all edges’ symbols                   queries during SQL injection analysis of 110 built-in
is returned. Built-in functions, such as array keys()                  query functions. Finally, the type of operation is deter-
and array search(), return all or parts of the available               mined, as well as the targeted table and column names.
keys in an array and can be modeled more precisely with                The access of data is modeled by new data symbols.
the ArrayKey symbol.


                                                                   6
994 23rd USENIX Security Symposium                                                                            USENIX Association
Preparation During the initialization of our tool, we            3.4.2   Session Keys
collect all files with a .sql extension. All available
CREATE TABLE instructions within these files are parsed          The analysis of session variables does not require a com-
so that we can reconstruct the database schema, includ-          plex markup parser or new data symbol. Instead, session
ing all table and column names as well as column types           data is handled similar to other global arrays. Taintable
and length. If no schema file is found, each PHP file            session keys are stored during the analysis phase.
in the project is searched via regular expression. The           Writing If data is assigned to a Variable or
knowledge of the database schema improves precision              ArrayDimFetch symbol during block simulation and
when data is read in an unspecified way, or when data is         the symbol’s name is SESSION, the assigned data is
sanitized by the column type or length.                          analyzed via taint analysis. If the assigned data is
Writing A write operation to a database is detected              tainted, its resolved source symbol is stored into an
if the SQL parser identifies an INSERT, UPDATE, or               ArrayDimTree symbol in the environment, together
REPLACE statement. By tokenizing the SQL query, we               with the dimension of the SESSION symbol. This way,
determine the targeted table’s name, all specified column        an ArrayDimTree is built with all taintable dimensions
names, and their corresponding input values. In case of          of the session array that link to the tainted source sym-
an unspecified write, the parser makes use of the database       bols and their corresponding sanitization tags.
schema. If an input value of a column contains a TID             Reading The access to session data is modeled by
(see Section 3.2), the affected column and table name is         ArrayDimFetch symbols with the name SESSION and
marked as taintable together with the linked source sym-         requires no modification. During taint analysis inside a
bol and its sanitization tags.                                   user-defined function, session variables are handled as
Reading If the SQL parser encounters a SELECT state-             global variables. They are added to the function sum-
ment, we try to determine all selected column and ta-            mary and they are inspected for each function call in a
ble names. Multiple table names can occur if tables              context-sensitive way. This avoids premature decisions
are joined or unioned. Alias names within the query              about the taint status inside a function if the session key
are mapped and resolved. In case of uncertainty, the             is overwritten before the function is called. Just as for a
parser makes use of the database schema. Finally, a new          DataDB symbol, a temporary vulnerability report is cre-
ResourceDB symbol is mapped to the analyzed query                ated if a SESSION variable taints a sensitive sink.
function as return value. This symbol holds information          3.4.3   File Names
about all selected column names in a numerical hash map
and its corresponding table names.                               To detect taintable file names, we collect file paths a user
Access In PHP, database result resources are trans-              can write to. For this purpose, new data symbols model
formed into arrays by built-in fetch functions (refer to         directory resources and their accesses. Whenever a path
Listing 2). We ignore the mode of access and let 89 con-         is reconstructed only partially, we use the same approach
figured fetch functions return a Variable symbol with            as in file inclusion analysis. Here, a regular expression
the name of the resource. When an ArrayDimFetch                  is created and mapped to all available paths that were
symbol accesses the result of these fetch functions, it is       detected when loading the application files.
inferred to the corresponding ResourceDB symbol. In              Writing To detect a file name manipulation with user
this case, the carried dimension of the ArrayDimFetch            input, we analyze 27 built-in functions such as copy(),
symbol is evaluated against the available column names           rename(), and file put contents(). Additionally,
in the ResourceDB symbol. If the asterisk character is           file uploads with move uploaded file() are analyzed.
contained in the column list and the dimension is numer-         Note that at the same time these built-in functions are
ical, the database schema is used to find the correct col-       sensitive sinks and generate vulnerability reports such as
umn name. Otherwise, if the dimension equals a column            an arbitrary file upload vulnerability. The path argument
name in the field list, a new DataDB symbol is returned          is analyzed by conventional context-sensitive string anal-
that states which column of which table is accessed.             ysis. If the path is tainted, we store it with its prefix
Sanitization Certain implicit sanitization is considered         as taintable. When no prefix is present, the file path of
when dealing with SQL. If a column is compared to a              the currently analyzed file is taken. Additionally, if the
static value within a WHERE clause in a SELECT state-            source is not sanitized against path traversal attacks, all
ment, the return value for this column is sanitized. In          paths are assumed as taintable and a flag is set during
this case, the static value is saved within the ResourceDB       analysis accordingly.
symbol and mapped to the column as return value. Fur-            Reading We handle three different ways of opening a
thermore, a sanitization tag for the used quote type is          directory with PHP’s built-in functions. First, we model
removed when data is updated or inserted to the database         the built-in function scandir() that returns an array,
because one level of escaping is lost during writing.            listing all files and directories within a specified path.


                                                             7
USENIX Association                                                                   23rd USENIX Security Symposium 995
Second, we model the built-in function glob() that also             3.5     Inter-procedural PDS Analysis
returns an array that lists all files and directories speci-
fied by a pattern. We transform the pattern into a regular          We optimized the inter-procedural analysis to refine our
expression by substituting the pattern characters * and             string analysis results. Function summaries offer a high
? into regular expression equivalents. Third, we model              performance but they are also inflexible for functions
the built-in function opendir() which returns a direc-              with dynamic behavior. Thus, they can weaken the static
tory handle. For all mentioned built-in functions, we re-           reconstruction of dynamically created strings.
construct the opened path by string analysis and return a
ResourceDir symbol that stores the path’s name.                     3.5.1   Multiple Parameter Trace
Access The returned result of scandir() and glob()                  As we illustrated in Listing 5, modern applications of-
is accessed by an array key. Since we do not know nei-              ten define wrapper functions for PDS access where more
ther the amount nor the order of files in a directory, we           than one parameter is used within one sensitive sink. In
return a DataPath symbol whenever a ResourceDir                     this case, the approach of storing each parameter together
symbol is inferred from an ArrayDimFetch symbol, re-                with its prefixed and postfixed markup, and the corre-
gardless of its dimension. For this purpose, we let the             sponding vulnerability type as sensitive parameter in the
built-in function readdir() that is supposed to read an             function summary, is error-prone. When a call to this
entry of a directory handle return an ArrayDimFetch                 function occurs, the approach swaps the parameter sym-
symbol with an arbitrary dimension and the name of the              bol with the argument of the function call and traces it for
directory handle. It is inferred to a DataPath symbol               user input. While this approach works fine for vulnera-
when the trace of the ArrayDimFetch symbol results in               bility detection, it leads to imprecision when it comes to
a ResourceDir symbol.                                               string reconstruction. Because each argument is traced
Sanitization In order to model sanitization that checks             separately but both are used in the same sink, the result
if a given string is a valid file name, 11 built-in functions       of one trace is missing in the result of the other trace. In
such as file exists and is file() are simulated. We                 Listing 5, for example, the table name is missing in the
modified the sanitization check in a way that these func-           reconstructed query while the data is reconstructed from
tions only sanitize if there is no taintable file path found.       the new user array.
For this purpose, a flag is set during taint analysis if san-          To circumvent this problem, we refined this approach
itization of a source by file name is detected. The flag            for sinks that execute SQL queries or open file paths
issues only a temporary vulnerability report that is re-            within a user-defined function. If multiple parameters or
vised at the end of the analysis regarding the ability to           global variables are involved, all symbols are combined
taint a file path.                                                  to one ValueConcat symbol. Then this symbol is stored
                                                                    in the function summary and analyzed for each function
3.4.4   Multi-Step Exploits                                         call. This way, each parameter is traced within one anal-
                                                                    ysis and all results are present at the same time.
In order to detect multi-step exploits, we store all ta-
ble names of all writing SQL queries that are affected              3.5.2   Mapping Returned Resources
by SQLi. Furthermore, we set a flag during the analy-
sis process if an arbitrary file write or arbitrary file re-        Working with function summaries is very efficient when
name vulnerability is detected. At the end of the analysis,         it comes to performance because each function only re-
when the taint decision is made for data that comes from            quires a single analysis on the first call. For every other
a PDS, multi-step exploit reports are added to the initial          call, the function summary is reused. However, a user-
vulnerability. This is done for all vulnerabilities that rely       defined function might return a resource that has dif-
on a DataDB symbol that is not tainted through second-              ferent properties for each call. For example, a SELECT
order but which table name is affected by SQLi. Also,               query that embeds the parameter of an user-defined func-
a multi-step exploit is reported if a DataDir symbol oc-            tion as the table name returns a different ResourceDB
curs and the flag for a file rename vulnerability was set.          symbol for every call, depending on the function’s ar-
All session data is treated as tainted if an arbitrary file         gument. If the resource is returned by the user-defined
write vulnerability was detected. Additionally, any local           function, its symbol’s properties change for every differ-
file inclusion vulnerability is extended to a remote code           ent function call.
execution if a file write or upload feature is detected.               As a solution, we add empty ResourceDB symbols
   Moreover, a SQLi vulnerability within a SELECT                   to the function summary’s set of return values for user-
query returns a DataDB symbol with a taint flag. This               defined functions with dynamic SQL queries. Once the
flag indicates that all accessed columns are taintable by           sensitive parameters are analyzed and the queries are re-
modifying the SELECT query during an attack. Thus, all              constructed, a copy of these symbols is updated with the
columns of the DataDB symbol are taintable.                         table and column information and used as returned data.

                                                                8
996 23rd USENIX Security Symposium                                                                          USENIX Association
4     Evaluation                                                       We then carefully fuzzed a local instance of each
                                                                    application manually with common attack payloads in
For evaluating our approach, we selected six real-world             order to determine which columns of type string are
web applications. We chose the conference management                taintable. Furthermore, we observed which columns
systems OpenConf 5.30 and HotCRP 2.61 for their pop-                were reported by our prototype implementation as
ularity in the academic field and osCommerce 2.3.3.4 for            taintable when the schema is available and when not. The
its large size. Furthermore, we evaluated the follow-up             results are compared in Table 2. Among the columns
versions of the most prominent software used in related             with a string type, 53% are taintable. As a result, only
work [3, 11, 30, 31]: NewsPro 1.1.5, MyBloggie 2.1.4,               24% of all available columns are not sanitized by the ap-
and Scarf 2007-02-27.                                               plication or the columns’ data type.
   A second-order vulnerability consists of two data
flows: tainting the PDS and tainting the sensitive sink.              Table 2: Taintable columns in selected applications.
We evaluated our prototype for both steps and present the                                            Schema        No schema
true positives (TP) and false positives (FP) in this section.        Software           Taintable    TP FP          TP    FP
In addition, we discuss the root cause for false negatives
(FN) and outline the limitations of our approach.                    osCommerce               63      55      4      55      37
                                                                     HotCRP                   43      27      1      27       3
                                                                     OpenConf                 47      16      1      16       4
4.1     PDS Usage and Coverage                                       NewsPro                  12      12      0      12       0
To obtain an overview of the usage of PDS in web appli-              Scarf                    10      10      1      10       3
cations, we manually evaluated the total amount of dif-              MyBloggie                 9       9      0       9       0
ferent memory locations. Note that these numbers do not              Total                   184    70%     5%     70%     27%
reflect how often one memory location is used at run-
time. Then, we evaluated the ability to taint these mem-               For the rather old and simple applications, all taintable
ory locations by an application’s user and compared it              columns were detected by our prototype. The modern
to the detection rate of our prototype. A PDS is defined            and large applications often use loops to construct dy-
as taintable if it can contain at least one of the follow-          namic SQL queries where reconstruction is error-prone.
ing characters submitted by an application user: \<>’".             Overall, we detected 70% of all taintable columns. When
In total, we manually identified 841 PDS of which 23%               the database schema is known, 5% of our reports are FP.
are taintable. Our prototype successfully detected 71%              The root cause is path-sensitive sanitization of data that
of the taintable PDS with a false discovery rate of 6%.             is written to the database—a sanitization that our current
                                                                    prototype is not able to detect yet. The false discovery
                                                                    rate is higher if the database schema of an application is
4.1.1       Databases
                                                                    not found. In this case, a static analysis tool cannot rea-
Our implementation successfully recovered the database              son about data types within the database and may flag
schema for all tested applications during the initializa-           columns of numeric data type as taintable.
tion phase. For evaluation, we categorized all avail-
able columns in the application’s database schema by de-            4.1.2    Sessions
clared data type. Only columns with a string type, such
                                                                    To obtain a ground truth for our evaluation, we again
as VARCHAR or TEXT, are of interest because they can
                                                                    manually assessed the applications’ code for all accessed
store tainted data. As shown in Table 1, we found that
                                                                    keys of the superglobal SESSION array. Dynamic keys
on average about half of the columns are not taintable
                                                                    were reconstructed and keys in multi-dimensional arrays
due to numeric data types such as INT and DATE.
                                                                    were counted multiple times. Then, we manually exam-
                                                                    ined which session keys are taintable by the application’s
      Table 1: Column types in selected applications.
                                                                    user and compared this to the analysis result generated by
                                                                    our prototype implementation. As shown in Table 3, we
    Software        Tables    Columns     Num      String           found that only 12% of the 52 identified session keys are
    osCommerce           50        331      193       138           taintable within our selected applications.
    HotCRP               29        217      142        75              Our prototype correctly detected all taintable session
    OpenConf             18        129       48        81           keys. One FP occurred because the sanitized email ad-
    NewsPro               8         43       18        25           dress of a user is written to the session after it is fetched
    Scarf                 7         37       22        15           from the database. This FP is based on the previously
    MyBloggie             4         24       10        14           introduced FP in identifying taintable columns. A cus-
                                                                    tom session management in osCommerce led to exclu-
    Total               116        781     55%       45%
                                                                    sion from our evaluation.


                                                                9
USENIX Association                                                                       23rd USENIX Security Symposium 997
Table 3: Taintable session keys in selected applications.           false discovery rate of 21% (see Table 5 for details).
                                                                    In summary, 97% of the valid reports are persistent
      Software       Keys    Taintable     TP         FP            XSS vulnerabilities where the payload is stored in the
      HotCRP           29            2       2         0            database. Five persistent XSS vulnerabilities are caused
      OpenConf         14            2       1         0            by session data or file names. This is closely related to
      NewsPro           2            1       1         0            the fact that 94% of all taintable PDS we identified are
      Scarf             4            0       0         1            columns in database tables (see Section 4.1) and sensi-
      MyBloggie         3            1       1         0            tive sinks such as echo are one of PHP’s most prominent
                                                                    built-in features [10].
      Total            52         12%     83%        16%
                                                                         Table 5: Evaluation results for selected applications.
4.1.3    File Names                                                      Software        Files       LOC       TP          FP    FN
To evaluate the features that allow an application’s user                osCommerce       570      66 381       97         29     6
to alter a file name, we again manually assessed each                    HotCRP            74      40 339        1          1     0
application for file upload, file creation, and file rename              OpenConf         121      20 404       16          4     0
features and counted the different target paths to obtain                NewsPro           23       5 077        7          1     0
a ground truth. Next, we counted the collected taintable                 Scarf             19       1 686       37          8     3
path names reported by our prototype. The results are                    MyBloggie         58       9 485        1          0     0
shown in Table 4.                                                        Total            865     143 372      159      43        9
      Table 4: Taintable paths in selected applications.                 Average          144      23 895     79%     21%

  Software           Paths    Taintable      TP        FP              Our evaluation revealed that second-order vulnerabil-
  osCommerce             2            2          2         0        ities are highly critical. Next to persistent XSS and file
  HotCRP                 1            0          0         0        vulnerabilities, we detected various remote code exe-
  OpenConf               1            0          0         1        cution vulnerabilities in osCommerce, OpenConf, and
  NewsPro                1            0          0         0        NewsPro. In the following, we introduce two selected
  Scarf                  1            1          1         0        vulnerabilities to illustrate the complexity and severity
  MyBloggie              2            2          2         0        of real-world second-order vulnerabilities. It is evident
                                                                    that these vulnerabilities could only be detected with our
  Total                  8        63%     100%        16%
                                                                    novel approach of analyzing second-order data flows.

   We found at least one feature in each of the applica-            4.2.1     Second-Order LFI to RCE in OpenConf
tion’s source code to create a new file. However, half of
the applications sanitize the name of the file before cre-          OpenConf is a well-known conference management
ating it. Our prototype detected all taintable path names.          software used by many (academic) conferences. Our
One FP occurred for OpenConf, where uploaded files are              prototype found a second-order local file inclusion vul-
sanitized in a path-sensitive way.                                  nerability in the user-defined printHeader function that
   Interestingly, a file upload in Scarf is based on a              leads to remote command execution. The relevant parts
second-order data flow. The name of the uploaded file is            of the affected file include.php is shown in Listing 6.
specified separately and stored as a configuration value             1   function printHeader( what, function="0") {
in the database before it is read from the database again            2
                                                                     3
                                                                            require_once GLOBALS[ pfx ] .
                                                                                GLOBALS[ OC_configAR ][ OC_headerFile ];
and the file is copied. Because no sanitization is applied,          4   }
an administrator is able to copy any file to any location of         5
                                                                     6     r = mysql_query("SELECT setting , value , parse
the server’s file system which leads to remote code exe-             7                       FROM " . OCC_TABLE_CONFIG . " ");
cution. This critical vulnerability was missed in previous           8
                                                                     9
                                                                         while ( l = mysql_fetch_assoc( r)) {
                                                                              OC_configAR[ l[ setting ]] = l[ value ];
work that also used this application for evaluating their           10   }
approach [3, 31].                                                   11   printHeader();

                                                                           Listing 6: Simplified include.php of OpenConf.
4.2     Second-Order Vulnerabilities
                                                                       When looking at the code, it does not reveal any vul-
We evaluated the ability of our prototype to detect                 nerability. Whenever the code is included, settings are
second-order vulnerabilities. Reports of first-order vul-           loaded from the database and the user-defined function
nerabilities are ignored for now. Our prototype reported            printHeader() is called. This function includes a con-
a total of 159 valid second-order vulnerabilities with a            figured header file and prints some HTML.

                                                               10
998 23rd USENIX Security Symposium                                                                             USENIX Association
 1   function updateConfigSetting( setting, value) {                  1   function unp_printTemplate( template) {
 2       q = "UPDATE " . OCC_TABLE_CONFIG . "                         2      global templatecache, DB;
 3           SET value = " . safeSQLstr(trim( value)) . "             3        getTemplate = mysql_query("SELECT name,template
 4           WHERE setting = " . safeSQLstr( setting) . " ";          4         FROM unp_template WHERE name= template LIMIT 1");
 5      return(ocsql_query( q));                                      5      while ( temp = mysql_fetch_array( getTemplate)) {
 6   }                                                                6           templatecache[ template] = temp[ template ];
 7                                                                    7      }
 8   foreach (array_keys( _POST) as p) {                              8      return addslashes( templatecache[ template]);
 9      if (preg_match("/^OC_[\w-]+ /", p)) {                         9   }
10         updateConfigSetting( p, _POST[ p]);                       10   eval( headlines_displaybit = " .
11      }                                                            11             unp_printTemplate( headlines_displaybit ). "; );
12   }
                                                                     Listing 9:       Simplified Remote Code Execution
Listing 7:       Simplified code to change settings in
                                                                     vulnerability in NewsPro.
OpenConf.

   However, as shown in Listing 7, it is possible for a                 At the call-site, the fetched template is evaluated
privileged chair user to change any configuration setting.           with PHP’s eval operator that executes PHP code
The configuration page does not specify an input field to            (line 10). The template’s code is escaped (line 8),
change the headerFile setting. Nonetheless, by adding                however, the double-quoted value of the evaluated vari-
the key OC headerFile to a manipulated HTTP POST                     able headlines displaybit allows to execute arbi-
request, the setting is changed. The loop over the sub-              trary PHP code using curly syntax. By adding the code
mitted keys of the POST array in Listing 7, line 8, as               { {system(id)}} to a template, the system command
well as the loop over the OC configAR in Listing 6,                  id is executed. Note that related work missed to detect
line 9, shows once again how important it is to track the            this vulnerability, which is also present in prior versions.
taint status of PHP’s array keys precisely.
   A chair member can now include any local file of the
system to the output. Additionally, because the software             4.3      Multi-Step Exploits
allows to upload PDF files to the server, our prototype
added a multi-step exploit report. Indeed, if a PDF file             Our prototype reported two arbitrary file upload vulner-
containing PHP code is uploaded to the server and the                abilities and 14 SQL injection vulnerabilities. Because
headerFile setting is pointed to that PDF, arbitrary PHP             these vulnerabilities affect a storage operation, the stored
code is executed. Moreover, our tool reported a SQL                  data can be manipulated during multi-step exploitation.
injection vulnerability that is accessible to unprivileged           Our prototype found 14 valid multi-step exploits and a
users. This allows any visitor to extract the chair’s pass-          single FP as shown in Table 6.
word hash (salted SHA1) from the database.
                                                                     Table 6: Reported multi-step exploits in selected appli-
4.2.2     Second-Order RCE in NewsPro                                cations.

Utopia NewsPro is a blogging software and was used in
                                                                                            File       SQLi          Multi-Step
previous work for evaluation [29–31]. Our prototype re-
                                                                          Software          TP       TP    FP        TP     FP
ported a second-order code execution vulnerability in the
administrator interface. Here, a user is able to alter the                osCommerce         1        3        0       3      0
template files of the blog. The simplified code is shown                  HotCRP             0        1        7       0      1
in Listing 8.                                                             OpenConf           0        4        1       1      0
                                                                          NewsPro            0        6        0       9      0
 1   tempid = (int) _POST[ tempid ];
 2   template = mysql_real_escape_string( _POST[ template ]);             Scarf              1        1        0       1      0
 3   updateTemplate = mysql_query("UPDATE unp_template                    MyBloggie          0        5        0       0      0
 4          SET template= template WHERE id= tempid ");
                                                                          Total              2       20       8       14      1
Listing 8: Simplified code to change the template in                      Average          100%     71%      29%     93%     7%
NewsPro.

   The template code is read from the database in various               All detected multi-step exploits consist of two steps
places of the source code with help of the user-defined              and no third-order vulnerabilities were detected within
function unp printTemplate() (see Listing 9). First,                 our selected applications. In the following, we examine
this function writes the template’s code to a cache array            two multi-step exploits in osCommerce that lead to re-
(line 6) and then returns it from this array again. The              mote command execution to illustrate that these vulnera-
example demonstrates the importance of inter-procedural              bilities can only be detected with our novel approach of
analysis and array handling.                                         analyzing multi-step exploits.


                                                                11
USENIX Association                                                                         23rd USENIX Security Symposium 999
4.3.1    Multi-Step RCE in osCommerce
                                                                       1   if (file_exists(DIR_FS_BACKUP . HTTP_GET_VARS[ file ])) {
                                                                       2       restore_file = DIR_FS_BACKUP . HTTP_GET_VARS[ file ];
OsCommerce is a popular e-commerce software. For                       3      exec(LOCAL_EXE_UNZIP .     . restore_file .   -d   .
one of three reported SQLi vulnerabilities in osCom-                               DIR_FS_BACKUP);
                                                                       4   }
merce, our prototype additionally reported a multi-step
remote code execution exploit. The SQLi is located in                  Listing 12: A dynamically constructed system command
the backup tool of the administrator interface and shown               in osCommerce includes the name of an existing file.
in Listing 10. Here, a SQL file is uploaded to restore a
database backup. Since the name of the uploaded file                      An attacker can bypass this check by abusing one of
is later used unsanitized in a SQL query, an attacker                  the file upload functionalities in osCommerce. By up-
is able to insert any data into the configuration ta-                  loading a file with the name ;id;.zip and afterwards spec-
ble by uploading a SQL file with a crafted name. This                  ifying this file as backup file, the command id is exe-
enables another, more severe vulnerability: the table                  cuted. The semicolons within the file name terminate the
configuration stores a configuration value and a                       previous unzip command and introduce a new command.
configuration title for each setting. Furthermore, a
use function can be specified optionally to deploy the                 4.4     False Positives
configuration’s value.
1    sql_file = new upload( sql_file );
                                                                       Our prototype generated 43 false second-order vulnera-
2    read_from = sql_file->filename;                                   bility reports, leading to a false discovery rate of 21% for
3
4
    tep_db_query("insert into " . TABLE_CONFIGURATION .
       " values (null, Last Database Restore , DB_RESTORE ,
                                                                       our selected applications. All false positives are based
5       " . read_from . " , Last database restore file ,               on the fact that our prototype is not able to detect path-
6       6 , 0 , null, now(),    ,   )");
                                                                       sensitive sanitization. Thus, persistent XSS was reported
Listing 10: Simplified code of the backup.php file in                  in Scarf and HotCRP that are based on email addresses
osCommerce shows a SQLi through a file name.                           stored in the database. Our prototype erroneously iden-
                                                                       tified these columns as taintable (see Section 4.1.1). The
   When the list of configuration values is loaded                     same error applies to a paper format in OpenConf which
from the database, the function name specified                         leads to four false positives. A user-defined sanitiza-
in the use function column is called with the                          tion function using path-sensitive sanitization based on
configuration value as argument (see Listing 11,                       its argument lead to 29 false persistent XSS reports in os-
line 5). An attacker can abuse the SQLi to insert                      Commerce. A false multi-step exploit was reported in
an arbitrary PHP function’s name, such as system, to                   HotCRP caused by a false SQLi report. By performing a
the column use function and insert an arbitrary argu-                  path-sensitive sanitization analysis, these false positives
ment, such as id, to the column configuration value.                   can be addressed in the future.
When loading the configuration list, the specified func-
tion is fetched and called with the specified argument that
executes the system command id.                                        4.5     False Negatives
1    conf_query = tep_db_query("select configuration_id,               Evaluating false negatives is an error-prone task because
          configuration_title, configuration_value,
          use_function from " . TABLE_CONFIGURATION . " where          the actual number of vulnerabilities is unknown. Further-
          configuration_group_id = " . (int) gID . " ");               more, no CVE entries are public regarding second-order
2   while ( configuration = tep_db_fetch_array( conf_query)) {
3      if (tep_not_null( configuration[ use_function ])) {             vulnerabilities in our selected applications. However, it
4          use_function = configuration[ use_function ];               is possible to test for false negatives that stem from in-
5          cfgValue = call_user_func( use_function,
6                        configuration[ configuration_value ]);        sufficient detection of taintable PDS. By pre-configuring
                                                                       our implementation with the taintable PDS we identi-
Listing 11: Simplified code of the configuration.php file              fied manually, we can compare the amount of detected
in osCommerce demonstrates a multi-step RCE.                           second-order vulnerabilities with the number of reports
                                                                       when PDS are analyzed automatically.
                                                                          As a result, only six previously missed persistent XSS
4.3.2    Sanitization Bypass in osCommerce
                                                                       in osCommerce were reported. Additionally, another
Another multi-step RCE exploit was reported in osCom-                  taintable session key in OpenConf was reported, al-
merce that involves a sanitization bypass. The previously              though the key does not lead to a vulnerability. Fur-
mentioned backup tool of the administrator interface al-               thermore, we manually inspected the source code of the
lows to specify a local ZIP file that is unpacked via the              applications and observed that our SQL parser needs im-
system command unzip. Here, the target file name is                    provement. Three false negatives occurred in Scarf be-
specified as an argument in the command line if the spec-              cause our parser does not handle SQL string functions
ified file name exists on the file system. The simplified              such as concat(). More complex SQL instructions
code is shown in Listing 12.                                           might lead to further false negatives but are used rarely.

                                                                  12
1000 23rd USENIX Security Symposium                                                                            USENIX Association
4.6     Performance                                                present a blackbox scanner capable of detecting persis-
                                                                   tent XSS [19]. Ardilla [14] aims at detecting both SQL
We evaluated our prototype with the implementation                 injection and XSS vulnerabilities by generating sample
of our approach to detect second-order vulnerabilities             inputs, symbolically tracking taint information through
(+SO) and without it (-SO). Our testing environment was            execution (including through database accesses), and au-
equipped with an Intel i7-2600 CPU with 3.4 GHz and                tomatically generating concrete exploits. The typical
16 GB of memory. The amount of memory consump-                     drawbacks of such dynamic approaches are the limited
tion (M, in megabytes), scan time (T, in seconds), and             test coverage and the missing ability to crawl a given
second-order vulnerability reports (R) for our selected            site “deep” enough. This insight is confirmed by Doupé
applications are given in Table 7.                                 et al., who tested eleven black-box dynamic vulnerability
                                                                   scanners and found that whole classes of vulnerabilities
 Table 7: Performance results for selected applications.           are not well-understood and cannot be detected by the
                                                                   state-of-the-art scanners [7].
                 -SO Analysis        +SO Analysis
 Software        M[mb] T[s]        M[mb] T[s]     R
                                                                   Static Analysis We perform static analysis of PHP
    osCommerce       834    134        846    213    129
                                                                   code and use the concept of block summaries as proposed
    HotCRP           752    186        775    345      3
                                                                   by Xie and Aiken [30] and later on refined by Dahse
    OpenConf         528     33        523     47     21
                                                                   and Holz [6]. Our analysis tool extends these ideas and
    NewsPro           50      1         50      3     17
                                                                   we improved the modeling of the language. More pre-
    Scarf             39      1         40     14     46
                                                                   cisely, we introduce more data symbols (e.g., to analyze
    MyBloggie         87      7         87     11      1
                                                                   array accesses in a more precise way) and enhance the
    Total            2290   362      2321     633    217           analysis of built-in functions such that we can perform
    Average           382    60       387     106     36           a taint analysis for persistent data stores. Furthermore,
                                                                   we optimized the inter-procedural analysis to refine our
                                                                   string analysis results. This enables us to analyze the two
   While the memory consumption does not increase sig-
                                                                   distinct data flows that lead to second-order vulnerabili-
nificantly by adding second-order analysis, the average
                                                                   ties: (i) source to PDS and (ii) PDS to sink. As a result,
scan time increases by 40%. Note, however, that this in-
                                                                   we are able to detect vulnerabilities missed by these ap-
cludes 217 processed vulnerability reports the prototype
                                                                   proaches. Pixy [11] and Saner [2] are other static code
would have missed without the additional second-order
                                                                   analysis tools for web applications, but both do not rec-
analysis. Furthermore, we believe that a total scan time
                                                                   ognize second-order vulnerabilities.
of less than 11 minutes for our selected applications is
still reasonable.                                                     There are static analysis approaches that target other
                                                                   classes of security vulnerabilities. For example, Safer-
                                                                   PHP [25] attempts to find semantic attacks (e.g., denial
5     Related Work                                                 of service attacks due to infinite loops caused by mali-
                                                                   cious inputs, or unauthorized database operations due to
Web applications are widely used in the modern Web and             missing security checks) within web applications. Role-
as a result, security analysis of such applications has at-        Cast [24] identifies security-critical variables and ap-
tracted a considerable amount of research. We now re-              plies role-specific variable consistency analysis to iden-
view related work in this area and discuss how our ap-             tify missing security checks, while Phantm [17] detects
proach differs from previous approaches.                           type errors in PHP code. Such kinds of software defects
                                                                   are out of scope for our analysis.
Dynamic Analysis There are many different dynamic
approaches to perform a security analysis of a given web           Static Second-Order Analysis The work closest re-
application. For example, Apollo [1] leverages symbolic            lated to our approach is MiMoSA [3]. It is an extension
and concrete execution techniques in combination with              of Pixy [11] to detect multi-module data flow and work
explicit-state model checking to perform persistent state          flow vulnerabilities. The data flow through databases is
analysis for session variables in PHP. Sekar proposes              modeled, however, it uses a dynamic approach for the
syntax- and taint-aware policies that can accurately de-           reconstruction of SQL queries. Moreover, it focuses on
tect and/or block most injection attacks [23]. However,            the detection of the work flow of an application and does
such approaches are typically limited to simple types of           not handle neither other types of PDS nor multi-step ex-
taint-style vulnerabilities.                                       ploits. In comparison, only three data flow vulnerabilities
   There are also dynamic approaches to detect second-             were detected in Scarf, whereas our approach detected 37
order vulnerabilities. For example, McAllister et al.              second-order vulnerabilities and one multi-step exploit.

                                                              13
USENIX Association                                                                   23rd USENIX Security Symposium 1001
   Zheng and Zhang proposed an approach to detect                                    Conference on Computer and Communications Security (CCS)
atomicity violations in web applications regarding exter-                            (2009).
nal resources [31], which can be seen as being closely                           [6] DAHSE , J., AND H OLZ , T. Simulation of Built-in PHP Features
related to second-order vulnerabilities since such con-                              for Precise Static Code Analysis. In Symposium on Network and
currency errors are a pre-condition for second-order ex-                             Distributed System Security (NDSS) (2014).
ploits. They perform a context- and path-sensitive inter-                        [7] D OUP É , A., C OVA , M., AND V IGNA , G. Why Johnny Can’t
procedural static analysis to automatically detect atom-                             Pentest: An Analysis of Black-box Web Vulnerability Scanners.
icity violations on shared external resources. The tools                             In Detection of Intrusions and Malware, and Vulnerability As-
                                                                                     sessment (DIMVA) (2010).
NewsPro and Scarf are included into their evaluation, but
the authors did not find any of the second-order vulnera-                        [8] G UNDY, M. V., AND C HEN , H. Noncespaces: Using Random-
bilities detected by our approach. As such, our approach                             ization to Enforce Information Flow Tracking and Thwart Cross-
                                                                                     Site Scripting Attacks. In Symposium on Network and Distributed
outperformed prior work on static detection of second-                               System Security (NDSS) (2009).
order vulnerabilities.
                                                                                 [9] H ALFOND , W. G., V IEGAS , J., AND O RSO , A. A Classification
                                                                                     of SQL Injection Attacks and Countermeasures. In Proceedings
6    Conclusion and Future Work                                                      of the IEEE International Symposium on Secure Software Engi-
                                                                                     neering (2006).
In this paper, we demonstrated that it is possible to stat-                     [10] H ILLS , M., K LINT, P., V INJU , J., AND H ILLS , M. An Empirical
ically model the data flow through persistent data stores                            Study of PHP Feature Usage. In International Symposium on
by collecting all storage writings and readings. At the                              Software Testing and Analysis (ISSTA) (2013).
end of the analysis, we can determine if data read from                         [11] J OVANOVIC , N., K RUEGEL , C., AND K IRDA , E. Static Analy-
a persistent store can be controlled by an attacker and                              sis for Detecting Taint-style Vulnerabilities in Web Applications.
if this leads to a security vulnerability. Our prototype                             Journal of Computer Security 18, 5 (08 2010).
implementation demonstrated that this is an overlooked                          [12] K ERNIGHAN , B. W., AND P IKE , R. The Practice of Program-
problem in practice: we identified more than 150 vulner-                             ming. In Addison-Wesley, Inc (1999).
abilities in six popular web applications and showed that
                                                                                [13] K HOURY, N., Z AVARSKY, P., L INDSKOG , D., AND RUHL , R.
prior work in this area did not detect these software de-                            Testing and Assessing Web Vulnerability Scanners for Persis-
fects. From a broader perspective, our approach can be                               tent SQL Injection Attacks. In Proceedings of the First In-
broken down to the problem of statically reconstructing                              ternational Workshop on Security and Privacy Preserving in e-
                                                                                     Societies (2011), SeceS ’11, pp. 12–18.
all strings that can be generated at runtime by the appli-
cation and thus, is limited by the halting problem.                             [14] K IEYZUN , A., G UO , P. J., JAYARAMAN , K., AND E RNST,
   Future work includes modeling the data flow when                                  M. D. Automatic Creation of SQL Injection and Cross-site
                                                                                     Scripting Attacks. In International Conference on Software En-
prepared statements are used, supporting more SQL fea-                               gineering (ICSE) (2009).
tures, and analyzing data flow through file content. Also,
path-sensitive sanitization and aliasing should be ana-                         [15] K IRDA , E., K RUEGEL , C., V IGNA , G., AND J OVANOVIC , N.
                                                                                     Noxes: A Client-side Solution for Mitigating Cross-site Script-
lyzed more precisely [32].                                                           ing Attacks. In ACM Symposium On Applied Computing (SAC)
                                                                                     (2006).
References                                                                      [16] K LEIN , A. Cross-Site Scripting Explained. Sanctum White Paper
                                                                                     (2002).
 [1] A RTZI , S., K IEZUN , A., D OLBY, J., T IP, F., D IG , D., PARAD -
     KAR , A., AND E RNST, M. D. Finding Bugs in Web Applica-
                                                                                [17] K NEUSS , E., S UTER , P., AND K UNCAK , V. Phantm: PHP An-
     tions Using Dynamic Test Generation and Explicit-State Model                    alyzer for Type Mismatch. In ACM SIGSOFT Symposium on the
     Checking. IEEE Trans. Softw. Eng. 36, 4 (2010).                                 Foundations of Software Engineering (FSE) (2010).
 [2] BALZAROTTI , D., C OVA , M., F ELMETSGER , V., J OVANOVIC ,                [18] L IVSHITS , B., AND C UI , W. Spectator: Detection and Contain-
     N., K IRDA , E., K RUEGEL , C., AND V IGNA , G. Saner: Com-                     ment of JavaScript Worms. In USENIX Annual Technical Con-
     posing Static and Dynamic Analysis to Validate Sanitization in                  ference (2008).
     Web Applications. In IEEE Symposium on Security and Privacy
     (2008).                                                                    [19] M C A LLISTER , S., K IRDA , E., AND K RUEGEL , C. Leveraging
                                                                                     User Interactions for In-Depth Testing of Web Applications. In
 [3] BALZAROTTI , D., C OVA , M., F ELMETSGER , V. V., AND V I -                     Symposium on Recent Advances in Intrusion Detection (RAID)
     GNA , G. Multi-Module Vulnerability Analysis of Web-based                       (2008).
     Applications. In ACM Conference on Computer and Commu-
     nications Security (CCS) (2007).                                           [20] M ICROSOFT D EVELOPER N ETWORK L IBRARY. Naming Files,
                                                                                     Paths, and Namespaces.   http://msdn.microsoft.com/
 [4] BAU , J., B URSZTEIN , E., G UPTA , D., AND M ITCHELL , J. State                en-us/library/aa365247(VS.85), as of February 2014.
     of the Art: Automated Black-Box Web Application Vulnerability
     Testing. In IEEE Symposium on Security and Privacy (2010).                 [21] NADJI , Y., S AXENA , P., AND S ONG , D. Document Structure
                                                                                     Integrity: A Robust Basis for Cross-site Scripting Defense. In
 [5] B OJINOV, H., B URSZTEIN , E., AND B ONEH , D. XCS: Cross                       Symposium on Network and Distributed System Security (NDSS)
     Channel Scripting and Its Impact on Web Applications. In ACM                    (2009).


                                                                           14
1002 23rd USENIX Security Symposium                                                                                            USENIX Association
[22] S CHOLTE , T., ROBERTSON , W., BALZAROTTI , D., AND
     K IRDA , E. An Empirical Analysis of Input Validation Mecha-
     nisms in Web Applications and Languages. In ACM Symposium
     On Applied Computing (SAC) (2012).

[23] S EKAR , R. An Efficient Black-Box Technique for Defeating Web
     Application Attacks. In Symposium on Network and Distributed
     System Security (NDSS) (2009).

[24] S ON , S., M C K INLEY, K. S., AND S HMATIKOV, V. RoleCast:
     Finding Missing Security Checks when You Do Not Know What
     Checks Are. In ACM SIGPLAN Conference on Object-Oriented
     Programming Systems, Languages, and Applications (OOPSLA)
     (2011).

[25] S ON , S., AND S HMATIKOV, V. SAFERPHP: Finding Seman-
     tic Vulnerabilities in PHP Applications. In ACM SIGPLAN
     Workshop on Programming Languages and Analysis for Security
     (PLAS) (2011).

[26] S UN , F., X U , L., AND S U , Z. Client-side Detection of XSS
     Worms by Monitoring Payload Propagation. In European Sym-
     posium on Research in Computer Security (ESORICS) (2009).

[27] VOGT, P., N ENTWICH , F., J OVANOVIC , N., K IRDA , E.,
     K R ÜGEL , C., AND V IGNA , G. Cross Site Scripting Prevention
     with Dynamic Data Tainting and Static Analysis. In Symposium
     on Network and Distributed System Security (NDSS) (2007).

[28] W3T ECHS. World Wide Web Technology Surveys. http://
     w3techs.com/, as of February 2014.

[29] WASSERMAN , G., AND S U , Z. Static Detection of Cross-Site
     Scripting Vulnerabilities. In International Conference on Soft-
     ware Engineering (ICSE) (2008).

[30] X IE , Y., AND A IKEN , A. Static Detection of Security Vulnera-
     bilities in Scripting Languages. In USENIX Security Symposium
     (2006).

[31] Z HENG , Y., AND Z HANG , X. Static Detection of Resource Con-
     tention Problems in Server-side Scripts. In International Confer-
     ence on Software Engineering (ICSE) (2012), pp. 584–594.

[32] Z HENG , Y., Z HANG , X., AND G ANESH , V. Z3-str: A Z3-based
     String Solver for Web Application Analysis. In Proceedings of
     the 2013 9th Joint Meeting on Foundations of Software Engineer-
     ing (2013), ESEC/FSE 2013, pp. 114–124.




                                                                         15
USENIX Association                                                            23rd USENIX Security Symposium 1003
