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...

Intruder - Battering Ram Attack

Purpose:  The Battering Ram attack type is used when you need to insert the same payload into multiple positions within the same request. This can be useful in scenarios where multiple fields might share the same value or where you want to test how the application handles identical inputs across different parameters. How It Works: Multiple Position Synchronization:  In a Battering Ram attack, the same payload is simultaneously inserted into all the designated positions within the request. Synchronized Input Testing:  This type of attack is beneficial for testing inputs that are expected to be the same or related across different fields. 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 :                                       ...

Intruder - Pitchfork Attack

Purpose:  The Pitchfork attack type allows you to test multiple parameters with different payloads simultaneously. It’s ideal for scenarios where you want to test how different combinations of inputs interact with each other. How It Works: Parallel Payload Insertion:  In a Pitchfork attack, Burp Suite inserts different payloads from multiple lists into multiple positions. Each position gets its unique payload. Combinatorial Testing:  This method is effective when you suspect that specific combinations of inputs might trigger unique responses or vulnerabilities. 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 :                                        POST /example?p1=p1val&p2=p2val HTTP/1.0      ...