The Unexpected Dangers of Dynamic Javascript
Total Page:16
File Type:pdf, Size:1020Kb
The Unexpected Dangers of Dynamic JavaScript Sebastian Lekies, Ruhr-University Bochum; Ben Stock, Friedrich-Alexander-Universität Erlangen-Nürnberg; Martin Wentzel and Martin Johns, SAP SE https://www.usenix.org/conference/usenixsecurity15/technical-sessions/presentation/lekies This paper is included in the Proceedings of the 24th USENIX Security Symposium August 12–14, 2015 • Washington, D.C. ISBN 978-1-939133-11-3 Open access to the Proceedings of the 24th USENIX Security Symposium is sponsored by USENIX The Unexpected Dangers of Dynamic JavaScript Sebastian Lekies Ben Stock Martin Wentzel Ruhr-University Bochum FAU Erlangen-Nuremberg SAP SE [email protected] [email protected] [email protected] Martin Johns SAP SE [email protected] Abstract the fly. In turn, this enabled the transformation of the Web’s initial document-centric nature into the versatile Modern Web sites frequently generate JavaScript on-the- platform that we know today. fly via server-side scripting, incorporating personalized 2. Browser-driven Web front-ends: Furthermore, the user data in the process. In general, cross-domain access Web browser has proven to be a highly capable container to such sensitive resources is prevented by the Same- for server-provided user interfaces and application logic. Origin Policy. The inclusion of remote scripts via the Thanks to the flexible nature of the underlying HTML HTML script tag, however, is exempt from this policy. model and the power of client-side scripting via Java- This exemption allows an adversary to import and exe- Script, the server can push arbitrary user interfaces to the cute dynamically generated scripts while a user visits an browser that rival their counterparts of desktop applica- attacker-controlled Web site. By observing the execution tion. In addition, and unlike monolithic desktop appli- behavior and the side effects the inclusion of the dynamic cations, however, browser-based UIs enable easy incor- script causes, the attacker is able to leak private user data poration of content from multiple parties using HTML’s leading to severe consequences ranging from privacy vi- inherent hypertext capabilities. olations up to full compromise of user accounts. Based on this foundation, the recent years have shown Although this issues has been known for several years an ongoing shift from Web applications that host the ma- under the term Cross-Site Script Inclusion, it has not jority of their application logic on the server side towards been analyzed in-depth on the Web. Therefore, to sys- rich client-side applications, which use JavaScript to re- tematically investigate the issue, we conduct a study on alize a significant portion of their functionality within the its prevalence in a set of 150 top-ranked domains. We user’s browser. observe that a third of the surveyed sites utilize dynamic With the increase of the functionality implemented on JavaScript. After evaluating the effectiveness of the de- the client side, the necessity for the JavaScript code to ployed countermeasures, we show that more than 80% gain access to additional user data rises natrually. In this of the sites are susceptible to attacks via remote script paper, we explore a specific technique that is frequently inclusion. Given the results of our study, we provide a used to pull such data from the server to the client-side: secure and functionally equivalent alternative to the use Dynamic JavaScript generation. of dynamic scripts. Similar to HTML, which is often generated dynam- ically, JavaScript may also be composed on the fly 1 Introduction through server-side code. In this composition process, user-specific data is often included in the resulting script Since its beginning in the early nineties, the Web evolved code, e.g., within the value of a variable. After deliv- from a mechanism to publish and link static documents ering the script to the browser, this data is immediately into a sophisticated platform for distributed Web applica- available to the client-side logic for further processing tions. This rapid transformation was driven by two tech- and presentation. This practice is potentially dangerous nical cornerstones: as the inclusion of script files is exempt from the Same- 1. Server-side generation of code: For one, on the Origin Policy [23]. Therefore, an attacker-controlled server side, static HTML content was quickly replaced Web page is able to import such a dynamically gener- by scripting to dynamically compose the Web server’s ated script and observe the side effects of the execu- HTTP responses and the contained HTML/JavaScript on tion, since all included scripts share the global object USENIX Association 24th USENIX Security Symposium 723 of the embedding web document. Thus, if the script 2 Technical Background contains user-specific data, this data might be accessi- ble to other attacker-controlled JavaScript. Although this In this section, we cover the technical background rele- attack, dubbed Cross-Site Script Inclusion (XSSI), has vant for this work. been mentioned within the literature [31], the prevalence of flaws which allow for this attack vector has not been 2.1 The Same-Origin Policy studied on real-world Web sites. In this paper we therefore present the first, systematic The Same-Origin Policy (SOP) is the principal security analysis of this vulnerability class and provide empirical policy in Web browsers. The SOP strongly separates mu- evidence on its severeness. First, we outline the general tually distrusting Web content within the Web browser attack patterns and vectors that can be used to conduct through origin-based compartmentalization [23]. More such an attack. Furthermore, we present the results of an precisely, the SOP allows a given JavaScript access only empirical study on several high-profile domains, show- to resources that have the same origin. The origin is de- ing how these domains incorporate dynamic scripts into fined as the triple consisting of scheme, host, and port their applications. Thereby, we find evidence that many of the involved resources. Thus, for instance, a script of these scripts are not or only inadequately protected executed under the origin of attacker.org is not able against XSSI attacks. We demonstrate the sever conse- to access a user’s personal information rendered under quences of these data leaks by reporting on real-world webmail.com. exploitation scenarios ranging from de-anonymization, While JavaScript execution is subject to the SOP, the to targeted phishing attacks up to complete compromise same does not hold true for cross-domain inclusion of of a victim’s account. Web content using HTML tags. Following the initial hy- To summarize, we make the following contributions: pertext vision of the WWW HTML-tags, such as image, may reference resources from foreign origins and include We elaborate on different ways an attacker is capa- • them into the current Web document. ble of leaking sensitive data via dynamically gener- Using this mechanism, the HTML script tag can ated scripts, enabled by the object scoping and dy- point to external script resources, using the tag’s src at- namic nature of JavaScript. tribute. When the browser encounters such a tag, it issues We report on the results of an empirical study a request to the foreign domain to retrieve the referenced • on several high-ranked domains to investigate the script. Important to note in this instance is the fact that prevalence of dynamic scripts. the request also carries authentication credentials in the form of cookies which the browser might have stored for Using the data collected during our empirical study, the remote host. When the response arrives, the script • we show that many dynamic scripts are not properly code inherits the origin of the including document and is protected against XSSI attacks. To demonstrate the executed in the context of the hosting page. This mecha- severity of the outlined vulnerabilities, we present nism is used widely in the Web, for instance to consume different exploitation scenarios ranging from de- third party JavaScript services, such as traffic analysis or anonymization to complete hijacking of a victim’s advertisement reselling [24]. account. Based on the observed purposes of the dynamic 2.2 JavaScript Language Features • scripts encountered in our study, we discuss secure In the following, we cover the most important JavaScript ways of utilizing such data without the use of dy- concepts necessary for the rest of the paper. namically generated scripts. The remainder of the paper is structured as follows: In Scoping In JavaScript, a scope is “a lexical environ- Section 2, we explain the technical foundations needed ment in which a function object is executed” [6]. From a for rest of the paper. Section 3 then covers the general at- developer’s point of view, a scope is the region in which tack patterns and techniques to exploit cross-domain data an identifier is defined. While C++ or Java make use leakage vulnerabilities. In Section 4, we report on the re- of block scoping, JavaScript utilizes so-called function sults of our empirical study and analyze the underlying scoping. This means that the JavaScript engine creates purposes of dynamic scripts. Furthermore, in Section 5, a new scope for each new function it encounters. As a we provide a scheme that is functionally equivalent, but consequence, an identifier that is locally defined in such is not prone to the attacks described in this paper. Sec- a function is associated with the corresponding scope. tion 6 covers related work, Section 7 gives an outlook Only code that is defined within the same function is thus and Section 8 concludes the paper. able to access such a variable residing in the local scope, 724 24th USENIX Security Symposium USENIX Association whereas global variables are associated with the global 3 Cross-Domain Data Leakages scope.