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

Installation and use of assetfinder

What is assetfinder ? Assetfinder is a subdomain discovery tool used in Kali Linux, focusing on finding related domains and subdomains for a target. It’s especially valued for its simplicity and efficiency in discovering assets associated with a domain, including subdomains and related domains. Assetfinder is particularly useful during the reconnaissance phase of penetration testing. Uses of Assetfinder in Kali Linux: Subdomain and Domain Discovery:  Assetfinder searches the web to find subdomains and related domains for a target domain. It aggregates data from various sources to provide a comprehensive list of assets. Combining Multiple Data Sources:  The tool integrates with numerous online services and databases, combining their results to give a broader view of the domain’s subdomains. This includes sources like crt.sh , certspotter , hackertarget , and others. Efficient and Quick Enumeration:  Assetfinder is known for its speed and efficiency. It quickly en...

OWASP Top 10 :Understanding Insecure Design

    What is insecure design? Insecure design means creating a system or application without thinking enough about security. These occur when security is not considered or prioritized during the design process, resulting in weaknesses that attackers can exploit. Types of Insecure Design: 1. Lack of Security Requirements:  Not including security requirements during the design phase. Example : Failing to specify that passwords should be stored using strong hashing algorithms. 2. Improper Data Validation:  Not verifying input data properly, Example : Not checking user inputs, allowing SQL injection attacks. 3. Weak Authentication and Authorization:  Inadequate mechanisms to verify user identity and access permissions. Example : Allowing access to sensitive data without proper user verification. 4. Lack of Logging and Monitoring : Not tracking system activities or security events. Example : Not logging failed login attempts, making it hard to detect brute-force attac...