Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros temas > La Taberna
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 15-04-2012
Avatar de marcoszorrilla
marcoszorrilla marcoszorrilla is offline
Capo
 
Registrado: may 2003
Ubicación: Cantabria - España
Posts: 11.221
Poder: 10
marcoszorrilla Va por buen camino
He encontrado esto en unos foros:

Delphi - prevent against SQL injection



up vote 16 down vote favorite 1
share [g+] share [fb] share [tw]
I need to protect an application from SQL injection. Application is connecting to Oracle, using ADO, and search for the username and password to make the authentication.
From what I've read until now, the best approach is by using parameters, not assigning the entire SQL as string. Something like this:
query.SQL.Text := 'select * from table_name where name=:Name and id=:ID';
query.Prepare;
query.ParamByName( 'Name' ).AsString := name;
query.ParamByName( 'ID' ).AsInteger := id;
query.Open;
Also, I'm thinking to verify the input from user, and to delete SQL keywords like delete,insert,select,etc...Any input character different than normal ASCII letters and numbers will be deleted.
This will assure me a minimum of security level?
I do not want to use any other components than Delphi 7 standard and Jedi.

sql oracle delphi sql-injection delphi-7





feedback

3 Answers

active oldest votes


up vote 26 down vote accepted
Safe
query.SQL.Text := 'select * from table_name where name=:Name';
This code is safe because you are using parameters.
Parameters are always safe from SQL-injection.
Unsafe
var Username: string;
...
query.SQL.Text := 'select * from table_name where name='+ UserName;
Is unsafe because Username could be name; Drop table_name; Resulting in the following query being executed.
select * from table_name where name=name; Drop table_name;
Also Unsafe
var Username: string;
...
query.SQL.Text := 'select * from table_name where name='''+ UserName+'''';
Because it if username is ' or (1=1); Drop Table_name; -- It will result in the following query:
select * from table_name where name='' or (1=1); Drop Table_name; -- '
But this code is safe
var id: integer;
...
query.SQL.Text := 'select * from table_name where id='+IntToStr(id);
Because IntToStr() will only accept integers so no SQL code can be injected into the query string this way, only numbers (which is exactly what you want and thus allowed)
But I want to do stuff that can't be done with parameters
Parameters can only be used for values. They cannot replace field names or table names. So if you want to execute this query
query:= 'SELECT * FROM :dynamic_table '; {doesn't work}
query:= 'SELECT * FROM '+tableName; {works, but is unsafe}
The first query fails because you cannot use parameters for table or field names.
The second query is unsafe but is the only way this this can be done.
How to you stay safe?
You have to check the string tablename against a list of approved names.
Const
ApprovedTables: array[0..1] of string = ('table1','table2');

procedure DoQuery(tablename: string);
var
i: integer;
Approved: boolean;
query: string;
begin
Approved:= false;
for i:= lo(ApprovedTables) to hi(ApprovedTables) do begin
Approved:= Approved or (lowercase(tablename) = ApprovedTables[i]);
end; {for i}
if not Approved then exit;
query:= 'SELECT * FROM '+tablename;
...
That's the only way to do this, that I know of.
BTW Your original code has an error:
query.SQL.Text := 'select * from table_name where name=:Name where id=:ID';
Should be
query.SQL.Text := 'select * from table_name where name=:Name and id=:ID';
Because you have an error there, you cannot have two where's in one (sub)query

Espero sirva de algo.

Un Saludo.
__________________
Guía de Estilo de los Foros
Cita:
- Ça c'est la caisse. Le mouton que tu veux est dedans.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Aprendiendo a usar Trigger... verito_83mdq MySQL 11 23-02-2011 00:05:13
Aprendiendo juegos de delphi ferra99 Varios 6 29-10-2008 12:47:19
Aprendiendo calistian Varios 4 14-06-2008 21:47:48
Aprendiendo a Aprender Firebird...!!! RK2 Firebird e Interbase 5 12-05-2008 20:11:48
Aprendiendo delphi for php JULIPO PHP 6 21-09-2007 21:19:47


La franja horaria es GMT +2. Ahora son las 16:57:59.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi