HawkScan Test Info for SQL Injection - MsSQL

SQL Injection - MsSQL

Reference

Plugin Id: 40027 | CWE: 89

Remediation

  1. Use built-in Object Data Models (ODMs) to gather data: Instead of directly concatenating user input into SQL queries, use ODMs provided by the language framework to interact with the database. ODMs automatically handle parameterization and sanitization of user input, reducing the risk of SQL injection vulnerabilities. For example, in Node.js with the Sequelize library, you can define models and use them to query the database:

    const User = sequelize.define('user', {
      username: Sequelize.STRING,
      password: Sequelize.STRING
    });
    
    // Query using ODM
    User.findAll({
      where: {
        username: 'admin'
      }
    });
    
  2. Use parameterized queries: Instead of directly embedding user input into SQL statements, use parameterized queries to separate the SQL code from the user input. This ensures that user input is treated as data and not executable code. Here’s an example in Java using JDBC:

    String sql = "SELECT * FROM users WHERE username = ?";
    PreparedStatement statement = connection.prepareStatement(sql);
    statement.setString(1, userInput);
    ResultSet resultSet = statement.executeQuery();
    
  3. Avoid string concatenation on SQL statements: Constructing SQL statements by concatenating user input is prone to SQL injection. Instead, use prepared statements or query builders that handle parameterization and sanitization internally. For example, in Python with SQLAlchemy:

    from sqlalchemy import create_engine, text
    
    engine = create_engine('mssql+pyodbc://user:password@server/database')
    connection = engine.connect()
    
    # Query using parameterized statement
    query = text("SELECT * FROM users WHERE username = :username")
    result = connection.execute(query, username=user_input)
    

About

SQL injection vulnerabilities occur when user input is not properly sanitized or directly passed to the back-end SQL server. Attackers can exploit these vulnerabilities to inject malicious SQL commands into the application.

Risks

  1. Enumeration of column names: An attacker can use SQL injection to gather information about the database structure, including column names. This information can be used for further attacks or unauthorized access.

  2. Data retrieval from the database: By injecting SQL commands, an attacker can retrieve sensitive data from the database, such as usernames, passwords, or other confidential information.

  3. Remote code execution: In some cases, SQL injection can lead to remote code execution, allowing an attacker to execute arbitrary commands on the server hosting the database. This can result in complete compromise of the application and the underlying system.

It’s important to note that StackHawk tests for SQL injection vulnerabilities are often time-based, where the scanner tries to manipulate the SQL server’s response time by performing specific actions.