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

Navigating the Seas of Cyber Threats: Understanding Phishing Attacks

Network Segmentation: Enhancing Security and Performance