SQL-injection-in-store procedure

Your Stored Procedures Are at Risk: The Hidden Danger of Query Concatenation

In the world of secure coding, one of the most overlooked vulnerabilities lies within the stored procedures of databases. While they are intended to encapsulate and protect logic, improper use — especially involving query concatenation — can leave serious security holes.

Recently, while auditing a web application, I uncovered a dangerous vulnerability hidden inside a stored procedure. This real-world example reveals why query concatenation must be treated as a red flag.


🔍 Initial Discovery: A Suspicious Search Field

During the security testing of a web application’s search functionality, I entered a simple single quote character (') in the search field — a classic test to check for input validation issues.

To my surprise, the application threw a SQL error. This suggested that the input wasn’t being properly sanitized or parameterized — a critical warning sign of a potential SQL injection vulnerability.

🧩 Vulnerability Origin: The Stored Procedure

Upon reviewing the backend, I located the stored procedure used to handle search queries. Here’s the risky part of the logic:

set @CondStr= ‘ and (com_name_eng like ”%’ + @SearchText + ‘%” or cast(com_company_code as varchar) like ”%’ + @SearchText + ‘%”)’

This code directly concatenates the user input (@SearchText) into the SQL string. No parameterization, no escaping — a perfect recipe for exploitation.

🧪 Proof of Concept (PoC)

Using the vulnerable search field, I submitted the following payload:

Name’) and 1=2 union select 1,concat(‘Database Version: ‘,@@version),concat(‘Database Username: ‘,user,char(10),’Database Name: ‘,db_name()),4 — –

📋 What This Payload Does:

  • Closes the existing query condition (Name'))
  • Forces a false condition (1=2) so only the union query is executed
  • Extracts:
    • Database Version (@@version)
    • Current DB User (user)
    • Database Name (db_name())

This successful attack demonstrated how dangerous it is to concatenate user input directly into dynamic SQL within stored procedures.

💥 Why This Is Dangerous

Stored procedures are often trusted because they encapsulate logic on the database side. However, when used with dynamic SQL and unsafe input handling, they become just as vulnerable as poorly written inline queries.

Here’s what went wrong:

  • String concatenation of input data
  • Lack of input validation
  • No use of parameterized queries

These issues allow attackers to craft malicious input that alters the SQL logic, resulting in data leaks, unauthorized access, or even full database compromise.

✅ How to Fix This

To prevent vulnerabilities like this:

  1. Always use parameterized queries: sqlCopyEditEXEC sp_executesql @SQLQuery, N'@SearchText nvarchar(50)', @SearchText
  2. Validate input server-side:
    Use allowlists and strict data formats (e.g., alphanumeric for search).
  3. Avoid unnecessary dynamic SQL:
    Static queries are safer and easier to maintain.
  4. Implement web application firewalls (WAFs):
    Add a security layer that detects and blocks injection attempts.
  5. Regularly audit stored procedures:
    Especially those involving user input.

📌 Conclusion

This real-world example demonstrates how stored procedures — when misused — can become a severe entry point for SQL injection attacks. Query concatenation, though sometimes convenient, is a practice that should be strictly avoided in modern secure coding standards.

💡 As a developer, engineer, or security professional, your best defense is secure by design — avoiding shortcuts, applying principle of least privilege, and always questioning how user input is handled at every layer.

Leave a Reply

Your email address will not be published. Required fields are marked *