11.07.2015 Views

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 2: <strong>Threats</strong> <strong>and</strong> Countermeasures 27If the <strong>Web</strong> application takes the query string, fails to properly validate it, <strong>and</strong> thenreturns it to the browser, the script code executes in the browser. The precedingexample displays a harmless pop-up message. With the appropriate script, theattacker can easily extract the user’s authentication cookie, post it to his site, <strong>and</strong>subsequently make a request to the target <strong>Web</strong> site as the authenticated user.Countermeasures to prevent XSS include:● Perform thorough input validation. Your applications must ensure that input fromquery strings, form fields, <strong>and</strong> cookies are valid for the application. Consider alluser input as possibly malicious, <strong>and</strong> filter or sanitize for the context of thedownstream code. Validate all input for known valid values <strong>and</strong> then reject allother input. Use regular expressions to validate input data received via HTMLform fields, cookies, <strong>and</strong> query strings.● Use HTMLEncode <strong>and</strong> URLEncode functions to encode any output that includesuser input. This converts executable script into harmless HTML.SQL InjectionA SQL injection attack exploits vulnerabilities in input validation to run arbitrarycomm<strong>and</strong>s in the database. It can occur when your application uses input toconstruct dynamic SQL statements to access the database. It can also occur if yourcode uses stored procedures that are passed strings that contain unfiltered user input.Using the SQL injection attack, the attacker can execute arbitrary comm<strong>and</strong>s in thedatabase. The issue is magnified if the application uses an over-privileged account toconnect to the database. In this instance it is possible to use the database server to runoperating system comm<strong>and</strong>s <strong>and</strong> potentially compromise other servers, in additionto being able to retrieve, manipulate, <strong>and</strong> destroy data.Example of SQL InjectionYour application may be susceptible to SQL injection attacks when you incorporateunvalidated user input into database queries. Particularly susceptible is code thatconstructs dynamic SQL statements with unfiltered user input. Consider thefollowing code:SqlDataAdapter myComm<strong>and</strong> = new SqlDataAdapter("SELECT * FROM UsersWHERE UserName ='" + txtuid.Text + "'", conn);Attackers can inject SQL by terminating the intended SQL statement with the singlequote character followed by a semicolon character to begin a new comm<strong>and</strong>, <strong>and</strong>then executing the comm<strong>and</strong> of their choice. Consider the following character stringentered into the txtuid field.'; DROP TABLE Customers -

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!