Back

Topic

[KB1096]Sql Variables – Avoid SQL Injections

Tags: SQL

4 years ago
By HOTE
Options
Print
Applies to:

PcVue 15 onwards


Summary:

Embedding SQL scripting in PcVue is extremely powerful, but this feature can be exploited to perform a malicious act, erase data, read sensitive data or even corrupt a system. But fortunately PcVue is equipped with tools to prevent these risks.

This article is here to help you configuring your SQL queries safely within PcVue.


Details:

It is possible to modify queries dynamically using parameters. This is interesting in many situations but it can be dangerous too. Parameters are configured by default to be safely used but we will see that sometime we can improve security and why we should do it.

Let us imagine we have this basic table in a database to retrieve personal information of different users:

HomeSecurity table


Sample 1

We can prepare a query in PcVue in order to know who is at home or not:

SELECT [NickName] FROM [HomeSecurity] WHERE [IsAtHome] = ?Param01

Normal usage will be to execute this query with ?Param01 at 0 to know who is at home or not:

Query1 normal usage

But a malicious user could retrieve sensible information like the fact that Alice and David are away and their alarms are not activated:

Query1 malicious usage

Another example of injection is to call a system variable to get SGBD version to exploit a vulnerability of the system.

5

How to avoid these situations?

Configure ?Param01 parameter in query configuration to limit the use of numerical value and limit the string length.

4

It will then not be possible anymore to use AND or OR magic keywords. It is also a good habit to limit the number of characters (max string lengh) to avoid a buffer overflow.


Sample 2

We can also prepare the following query to retrieve user information if login and password match

SELECT [HomeAddress], [IsAtHome], [HomeAlarmStatus]FROM [HomeSecurity] WHERE [Name] = ‘?Param01’ AND [Password] = ‘?Param02’

?Param01 should only contain login

?Param02 should only contain password

But if ?Param02 “Escape string” property is unticked, it will be easy for a malicious user to know all adresses interesting to visit or not, without knowing a login nor a password:

Query2 malicious usage

Here below, another example of injection to corrupt the system, here it will delete the [HomSecurity] table:

Query2 malicious usage drop table

How to avoid these situations?

Let the escape string option selected to escape malicious characters (by default activated), and define adequat Max string length.

7


Summary of the different options available in the parameters configuration

4

  • Max String Length

Truncate the parameter value substitution string.

Ex: if value is 4 and parameter resolution is “My name”

Result is “My n”

  • Is Numeric

This option allows to verify if text value represents a numerical value or not

  • Escape String

This option allows to escape the following characters:

  • \r
  • \n
  • \
  • 0x1a
  • 0x00

Created on: 12 Jan 2021 Last update: 04 Sep 2024