UrbanPro
true

Learn SQL Programming from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Coding Instructions To Prevent SQL-Injection

Animesh Anand
15/06/2017 0 0

SQL-Injection is a very sensitive and very common technology to hack an application. I am describing a few techniques to make the SQL-Injection difficult.

The main point of injection is a dynamic query. Whenever a dynamic query is executed on the database there is a chance of SQL-Injection. If we follow some rule then we can minimize the hack attack.

Microsoft suggests using parameterized queries to avoid the SQL-Injection hack. For detailed information kindly visit: How To: Protect From SQL Injection in ASP.NET. We can create a parameterized query in both applications and databases. I am describing how to create and execute a parameterized query.

Generally we use this terminology to create and execute a query.

Code

In the preceding query if vulnerable data is passed using a query string “name” then the SQL-Injection can happen. Suppose a hacker passes “sumit’ or 1=1--” as the value of the query string “name” then the actual query that will be executed on the database will be:

code

This query will return all the records. In the same manner any query can be executed using this page or parameter. Like when a hacker passes “sumit’; drop table TestInjection;--” the actual query that will be executed will be:

code

To prevent these queries from execution we can create the same queries in this way.

corrected code

If queries are created in the preceding form then the @name parameter is treated as a literal value and not as executable code. In other words if any injection values are passed using a parameter then whatever is passed in the parameter will be entirely used as a single value, not as two separate queries. And as a result no result will be returned from the database.

Sometimes we need to create and execute dynamic queries in a procedure also. Normally we create dynamic queries in this way.

corrected code

This way of creating and executing a query is again vulnerable. For testing just create the procedure as shown above and execute in the following manner.

We can create parameterized queries in a database too. See the following code:

I would like to describe the method of creating and executing a query. I will describe only the preceding query. For detailed information please refer to the following online documentation: sp_executesql (Transact-SQL) and Using sp_executesql.

For creating a dynamic query in SQL server first declare a nvarchar variable and assign a query to this variable. In the query text write SQL parameters instead of the actual parameter passed to the procedure from the application as I used @namenew.

Now again declare a new nvarchar variable for parameter definition. This variable will describe the parameter details that are used in the actual query. As I declared @p.

Now use the sp_executesql procedure to execute this query. This procedure will take three arguments to execute the query. First the query to be executed, second the parameter defining the variable and third the value for the parameters used in the query.

If any query is written and executed in this way then it is safe from SQL-Injection.

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Write A Query To Get Nth Highest Salary
WITH CTE AS ( SELECT EmpID, EmpName, EmpSalary, RN = ROW_NUMBER() OVER (ORDER BY EmpSalary DESC) FROM dbo.Salary ) SELECT EmpID, EmpName, EmpSalary FROM CTE WHERE RN = @NthRowUse...

SQL Tips (4 to 6)
SQL tips 4:Avoid INDEX, unless you need to retrieve information quickly. Index will slower insert and update data query.The another way is using sub querySelect MAX(salary)FROM employeeWHERE salary IN(Select...

DBMS (Database Management System) Vs RDBMS ( Relational Database Management System)
DBMS RDBMS Stored data in a file Stored data in a table As it is stored in a file, there is no relationship concept Data in one table may be relationship with data in another table...

"foreach" loop in C#
foreach is a looping statement : repeats a group of statements for each element in an array or an object collection. (or) used to iterate through the collection/ an array to get the required information. Advantages: Easy...

WebSphere
WebSphere is a set of Java-based tools from IBM that allows customers to create and manage sophisticated business Web sites. The central WebSphere tool is theWebSphere Application Server (WAS), an application...
X

Looking for SQL Programming Classes?

The best tutors for SQL Programming Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn SQL Programming with the Best Tutors

The best Tutors for SQL Programming Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more