Skip to main content

OWASP Top 10 :Understanding Injection

 

What is injection?

In injection, an attacker can send malicious data to a program. This data is then processed by the program in an unintended way, often leading to unauthorized access or damage. 



Common Types of Injection Attacks:

1. SQL Injection: Manipulating a website's database queries by entering malicious SQL commands, which can lead to unauthorized access or data changes.


Example:(SQL)

SQL query : SELECT * FROM users WHERE username = 'user' AND password = 'pass';

Malicious Input: ' OR '1'='1

Resulting Query: SELECT * FROM users WHERE username = '' OR '1'='1' AND password = ' ';

Prevention:

  • Use prepared statements (parameterized queries).

  • Validate and sanitize user inputs.

  • Use ORM frameworks.

  • Least privilege for database accounts.


2. Command Injection: Injecting harmful commands into a system command or script, allowing attackers to execute arbitrary commands on the server.



Example:(bash)

basic command: ping -c 4 192.168.0.1

Malicious Input: 192.168.0.1; rm -rf /

Resulting Query: ping -c 4 192.168.0.1; rm -rf /

Prevention:

  • Validate and sanitize user inputs.

  • Use safe APIs that avoid the use of shell commands.

  • Apply the principle of least privilege.


3. LDAP Injection: Exploiting flaws in LDAP (directory service) queries to manipulate or access data stored in a directory.


Example:(LDAP)

basic command: (&(uid=user)(userPassword=pass))

Malicious Input: *)(uid=*))(|(uid=*

Resulting Query: (&((uid=*)(uid=*))(userPassword=pass))

Prevention:

  • Use parameterized queries for LDAP.

  • Validate and sanitize inputs.


4.XML Injection: Injecting malicious XML data into an XML document, which can alter or access data in unexpected ways.

Example:(XML)

basic command:

                             <user>

                                    <name>user</name>

                                    <password>pass</password>

                            </user>

Malicious Input: 

                            <user>

                                    <name>user</name>

                                    <password>pass</password>

                                    <role>admin</role>

                           </user>

Resulting Query: 

                               <user>

                                      <name>user</name>

                                       <password>pass</password>

                                       <role>admin</role>

                               </user>

Prevention:

  • Validate and sanitize XML inputs.

  • Use XML parsers with secure settings.


5.XPath Injection: Manipulating XPath queries used to navigate XML data, allowing attackers to access or modify data they shouldn't.


Example:(XPath)

basic command: //users/user[username='user' and password='pass']

Malicious Input: user' or '1'='1

Resulting Query: //users/user[username='user' or '1'='1' and password='pass']

Prevention:

  • Use parameterized XPath queries.

  • Validate and sanitize inputs.

6.NoSQL Injection: Injecting malicious queries into NoSQL databases (which don’t use SQL) to manipulate or access data improperly.


Example:(javascript)

basic command: db.users.find({ username: user, password: pass });

Malicious Input: { "$ne": null }

Resulting Query: db.users.find({ username: { "$ne": null }, password: {} });

Prevention:

  • Use parameterized queries.

  • Validate and sanitize inputs.

  • Apply the least privilege for database accounts.

  

7.HTML Injection: Inserting malicious HTML code into a web page, which can alter how the page looks or behaves, potentially leading to attacks like Cross-Site Scripting (XSS).


Example:(HTML)

basic command: 

                         <div>

                                   <p>Comment: user_comment</p>

                         </div>

Malicious Input: <script>alert('Hacked!');</script>

Resulting Query:

                            <div>

                                     <p>Comment: <script>alert('Hacked!');</script></p>

                            </div>

Prevention:

  • Validate and sanitize user inputs.

  • Encode HTML entities.


8. Email Header Injection: Adding malicious content to email headers, which can manipulate email behavior, send spam, or spoof emails.


Example:(plaintext)

basic command: To: victim@example.com

                           Subject: user_subject

Malicious Input: user_subject

                            CC: attacker@example.com

Resulting Query: To: victim@example.com

                            Subject: user_subject

                            CC: attacker@example.com

Prevention:

  • Validate and sanitize email inputs.

  • Use secure libraries for email handling.


9. CRLF Injection: Inserting Carriage Return (CR) and Line Feed (LF) characters into input, which can manipulate HTTP headers and cause unintended behavior.

Example:(HTTP)

basic command: Set-Cookie: session=user_session

Malicious Input: user_session\r\nSet-Cookie: admin=true

Resulting Query: Set-Cookie: session=user_session

                             Set-Cookie: admin=true

Prevention:

  • Validate and sanitize inputs.

  • Use secure functions for setting headers.


10. Path Traversal: Exploiting vulnerabilities to navigate to restricted files or directories on a server, potentially accessing sensitive data or files.


Example:(python)

basic command: open('/var/www/html/' + filename)

Malicious Input: ../../etc/passwd

Resulting Query: open('/var/www/html/../../etc/passwd')

Prevention:

  • Validate and sanitize file paths.

  • Use secure file-handling functions.

  • Apply the least privilege for file access.


Conclusion: injection attacks happen when attackers insert harmful code into an application to trick it into doing something it shouldn't. This can lead to unauthorized access, data leaks, or even control of the system. To prevent these attacks, always validate and sanitize user inputs, use secure coding practices, and implement protective measures like firewalls and automated testing.

Comments

Popular posts from this blog

OWASP Top 10 : Understanding Broken Access Control

What is broken access control? Broken access control is a security issue where users can access data or perform actions that they shouldn't be allowed to. This happens when the system fails to properly enforce rules about what users can and cannot do. Types of access controls :  1 . Vertical privilege escalation:  Vertical privilege escalation happens when a normal user gains access to functionalities reserved for higher-privileged users. Example:  A normal user can change the policies of the company.  2. Horizontal privilege escalation:  Horizontal privilege escalation allows a user to switch their access to another user's account, essentially impersonating them. Example:  A normal user can switch their account to admin.  3 . Insecure direct object reference ( IDOR):  IDOR occurs when an application exposes a reference to an internal implementation object, such as a file, directory, or database key. Example:  Suppose...

OWASP Top 10 :Understanding Software and Data Integrity Failures

   What are Software and Data Integrity Failures? Software and Data Integrity Failures refer to issues that compromise the accuracy, consistency, and trustworthiness of software and data. These failures can result from various factors, including malicious attacks, software bugs, or misconfigurations. Key Types of Software and Data Integrity Failures: 1. Input Validation Failures : Occur when the software does not properly validate input data. Examples : Buffer overflows, SQL injection, and cross-site scripting (XSS). 2. Authentication and Authorization Failures : Occur when there are weaknesses in verifying user identities or controlling user permissions. Examples : Broken authentication mechanisms, and improper access controls. 3. Cryptographic Failures : Involve weaknesses or misconfigurations in cryptographic mechanisms. Examples : Use of weak encryption algorithms, and improper key management. 4. Configuration and Deployment Failures : Arise from incorrect software or...

Intruder - Sniper Attack

Purpose:  The Sniper attack type is designed to test one input position at a time, allowing you to see how a single variable affects the outcome of a request. This is particularly useful for brute-force attacks on parameters like usernames, passwords, session tokens, etc. How It Works: Single Position Testing:  In a Sniper attack, you identify a single position in your request where the payloads will be inserted one by one. Each request only varies by this one position. Payload Iteration:  Burp Suite will go through the list of payloads you provide and substitute them at the designated position, sending a new HTTP request for each payload. Steps: 1.Capture the Request: Use Burp Suite’s Proxy tab to intercept the login request. For example, the intercepted HTTP POST request might look like this :                                       ...