Cross site scripting SQL injection
Cross site scripting SQL injection
Cross-Site Scripting (XSS) and SQL Injection are two of the most common types of
vulnerabilities in web applications. Here's a detailed explanation of each:
1. Cross-Site Scripting (XSS)
Definition:
XSS is a security vulnerability that allows an attacker to inject malicious scripts into web
pages viewed by other users. These scripts are typically written in JavaScript but can include
other types of code.
Types of XSS:
1. Stored XSS:
o The malicious script is permanently stored on the target server (e.g., in a
database, message board, or comment field).
o When a user accesses the stored content, the script executes in their browser.
o Example: An attacker posts a comment like <script>alert('XSS');</script> on a
forum, and every user who views the comment triggers the alert.
2. Reflected XSS:
o The malicious script is embedded in a link, and when the link is clicked, the
script is reflected off the web server and executed in the user's browser.
o Example: A phishing email with a URL like
http://example.com/search?q=<script>alert('XSS');</script>.
3. DOM-Based XSS:
o The vulnerability exists in the client-side script rather than the server.
o Example: If a web page's JavaScript reads data from the URL and directly
executes it without proper validation.
Impact:
• Stealing cookies, session tokens, or sensitive information.
• Defacing websites.
• Performing actions on behalf of users (e.g., sending malicious requests).
Prevention:
• Input Validation: Ensure input data is sanitized and encoded.
• Output Encoding: Encode data before rendering it in the browser (e.g., escape
HTML characters).
• Use CSP (Content Security Policy): Restrict the sources of scripts that the browser
can execute.
• Use Security Libraries: Libraries like OWASP Java Encoder can help protect against
XSS.
2. SQL Injection
Definition:
SQL Injection occurs when an attacker can inject malicious SQL code into a query to
manipulate or access a database in unintended ways.
How it Works:
• An attacker exploits input fields to inject SQL code into a query.
• Example:
SQL
SELECT * FROM users WHERE username = 'admin' AND password = '';
If an attacker enters admin' OR '1'='1 as the username:
SQL
SELECT * FROM users WHERE username = 'admin' OR '1'='1' AND password = '';
This query always evaluates as true, potentially granting unauthorized access.
Types of SQL Injection:
1. Classic SQL Injection:
o Directly injects malicious SQL queries.
o Example: Inputting 1 OR 1=1 to bypass login checks.
2. Blind SQL Injection:
o No direct feedback from the database. The attacker infers information through
responses (e.g., page loads or error messages).
o Techniques:
▪ Boolean-based: Checking true/false conditions.
▪ Time-based: Using SQL commands that delay response.
3. Error-Based SQL Injection:
o Exploits error messages to extract information about the database.
4. Union-Based SQL Injection:
o Combines results of two SELECT queries to retrieve hidden data.
Impact:
• Unauthorized access to sensitive data.
• Modifying or deleting database records.
• Gaining administrative control over systems.
Prevention:
• Parameterized Queries (Prepared Statements):
o Use placeholders instead of concatenating user input in queries.
o Example (in Python):
PYTHON
cursor.execute("SELECT * FROM users WHERE username = %s AND password = %s",
(username, password))
• Stored Procedures: Use database-stored procedures instead of direct queries.
• Input Validation: Validate and sanitize all inputs.
• Least Privilege: Restrict database user permissions to limit damage in case of an
attack.
• Web Application Firewall (WAF): Protect against known SQL injection patterns.
• Error Messages: Avoid revealing detailed error messages to users.
Key Differences:
Both vulnerabilities can have severe consequences, and developers must adopt secure coding
practices and regular security testing to protect web applications.