Showing posts with label Vulnerability. Show all posts
Showing posts with label Vulnerability. Show all posts

Saturday, June 01, 2024

Common prevention techniques against injection attacks

With reference to my previous blog post. Here are few prevention techniques against injection attacks:

  1. Input Validation: Validate and sanitize all user input to ensure it meets expected formats and ranges. Avoid dynamic queries built using untrusted input.

  2. Use Parameterized Queries: Utilize parameterized queries with prepared statements or stored procedures to prevent the injection of malicious code.

  3. Escaping Input: Special characters in user input should be escaped to neutralize their harmful effects, making them harmless before use.

  4. Least Privilege Principle: Applications should operate with the least privilege necessary to limit the potential impact of a successful injection attack.

  5. Regular Software Patching: Keep all software components and frameworks up to date to patch known injection vulnerabilities.

  6. Web Application Firewalls (WAF): Implement WAF solutions to filter and block malicious input before it reaches the application.

  7. Code Reviews and Security Testing: Conduct regular code reviews, security audits, and penetration testing to identify and mitigate potential injection vulnerabilities.

  8. Secure Development Practices: Train developers in secure coding practices to minimize the introduction of injection vulnerabilities during application development.

  9. Secure Configuration: Follow best practices for server configuration and secure coding guidelines to reduce the attack surface for injection attacks.

By implementing a combination of these techniques and maintaining a proactive approach to web application security, organizations can significantly reduce the risk of falling victim to injection attacks. 

Tuesday, June 13, 2023

What is a SQL Injection Attack?

SQL injection is a type of web application security vulnerability and attack that occurs when an attacker is able to manipulate an application's SQL (Structured Query Language) statements. It takes advantage of poor input validation or improper construction of SQL queries, allowing the attacker to insert malicious SQL code into the application's database query.

SQL Injection attacks are also called SQLi. SQL stands for 'structured query language' and SQL injection is sometimes abbreviated to SQLi

Impact of SQL injection on your applications

  • Steal credentials—attackers can obtain credentials via SQLi and then impersonate users and use their privileges.
  • Access databases—attackers can gain access to the sensitive data in database servers.
  • Alter data—attackers can alter or add new data to the accessed database. 
  • Delete data—attackers can delete database records or drop entire tables. 
  • Lateral movement—attackers can access database servers with operating system privileges, and use these permissions to access other sensitive systems.
  • Types of SQL Injection Attacks

    There are several types of SQL injection:

  • Union-based SQL Injection – Union-based SQL Injection represents the most popular type of SQL injection and uses the UNION statement. The UNION statement represents the combination of two select statements to retrieve data from the database.
  • Error-Based SQL Injection – this method can only be run against MS-SQL Servers. In this attack, the malicious user causes an application to show an error. Usually, you ask the database a question and it returns an error message which also contains the data they asked for.
  • Blind SQL Injection – in this attack, no error messages are received from the database; We extract the data by submitting queries to the database. Blind SQL injections can be divided into boolean-based SQL Injection and time-based SQL Injection.
  • SQLi attacks can also be classified by the method they use to inject data:

  • SQL injection based on user input – web applications accept inputs through forms, which pass a user’s input to the database for processing. If the web application accepts these inputs without sanitizing them, an attacker can inject malicious SQL statements.
  • SQL injection based on cookies – another approach to SQL injection is modifying cookies to “poison” database queries. Web applications often load cookies and use their data as part of database operations. A malicious user, or malware deployed on a user’s device, could modify cookies, to inject SQL in an unexpected way.
  • SQL injection based on HTTP headers – server variables such HTTP headers can also be used for SQL injection. If a web application accepts inputs from HTTP headers, fake headers containing arbitrary SQL can inject code into the database.
  • Second-order SQL injection – these are possibly the most complex SQL injection attacks, because they may lie dormant for a long period of time. A second-order SQL injection attack delivers poisoned data, which might be considered benign in one context, but is malicious in another context. Even if developers sanitize all application inputs, they could still be vulnerable to this type of attack.
  • Here are few defense mechanisms to avoid these attacks 

    1. Prepared statements:  These are easy to learn and use, and eliminate problem  of SQL Injection. They force you to define SQL code, and pass each parameter to the query later, making a strong distinction between code and data

    2. Stored Procedures: Stored procedures are similar to prepared statements, only the SQL code for the stored procedure is defined and stored in the database, rather than in the user’s code. In most cases, stored procedures can be as secure as prepared statements, so you can decide which one fits better with your development processes.

    There are two cases in which stored procedures are not secure:

  • The stored procedure includes dynamic SQL generation – this is typically not done in stored procedures, but it can be done, so you must avoid it when creating stored procedures. Otherwise, ensure you validate all inputs.
  • Database owner privileges – in some database setups, the administrator grants database owner permissions to enable stored procedures to run. This means that if an attacker breaches the server, they have full rights to the database. Avoid this by creating a custom role that allows storage procedures only the level of access they need.
  • 3. Allow-list Input Validation: This is another strong measure that can defend against SQL injection. The idea of allow-list validation is that user inputs are validated against a closed list of known legal values.

    4. Escaping All User-Supplied Input: Escaping means to add an escape character that instructs the code to ignore certain control characters, evaluating them as text and not as code.