Autor Beitrag
matze
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Sa 04.03.06 12:23 
Hallo !

Ich suche in Delphi eine Funktion, die der mysql_real_escape_string Funktion von PHP entspricht um mich gegen SQL Injection zu schützen.
Wo finde ich soetwas ?

PS: Ich arbiete mit den ZEOS Kompos, falls das was hilft.

Danke,
Matze

_________________
In the beginning was the word.
And the word was content-type: text/plain.
digi_c
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1905

W98, XP
D7 PE, Lazarus, WinAVR
BeitragVerfasst: Sa 04.03.06 16:14 
Ich habe in nem ASP Forum gelesen das man dafür die Eingaben als Parameter an Stored Procedures übergeben soll.

Wirst du ja sicherlich schon gelesen haben:
www.heise.de/security/artikel/43175/0
entwickler.com/itr/n...22580,nodeid,82.html
Suche in Wikipedia SQL INJECTION da verstehe ich aber nicht wieso das dann bei .NET nichtmehr hinhaut :(
matze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: So 05.03.06 10:32 
ja die artikel sind ja schön und nett, aber ich weiß nun immer noch nicht, wie ich das in Delphi machen kann !

_________________
In the beginning was the word.
And the word was content-type: text/plain.
digi_c
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1905

W98, XP
D7 PE, Lazarus, WinAVR
BeitragVerfasst: Di 07.03.06 12:03 
Ich habe das mal bei mir ausprobiert, weil ich befürchtet hatte, das man die Filter manipulieren könnte. Ich konnte das nicht manipulieren indem ich "; SELECT..." miteingegeben hab. Ich render auch nur lokale Datenbestände mit MatchesMask(...)
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 07.03.06 19:58 
Du setzst in deinen String-Variablen für die Queries einfach ein Backslash vor folgende Zeichen:
'
"
#0
#10
#13
#26
\

Das heisst, du ersetzst z.B. ' durch \' in deinem String.

Ohne Garantie auf Korrektheit, bitte zuerst selbst testen:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
function AddSlashes(const S: String): String;
const
 EscapeChars = [#0,#26,#10,#13,'\','''','"'];
var
 I, Size, L: Integer;
 R, W: PChar;
begin
  R := PChar(S);
  if R=nil then
   exit;
  L := Length(S);
  Size := 0;
  for I := L downto 1 do
  begin
   if R^ in EscapeChars then
    inc(Size, 2else
    inc(Size);
   inc(R);
  end;
  SetLength(Result, Size);
  R := PChar(S);
  W := PChar(Result);
  for I := L downto 1 do
  begin
   if R^ in EscapeChars then
   begin
    W^ := '\';
    inc(W);
   end;
   W^ := R^;
   inc(R);
   inc(W);
  end;
end;
matze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Mi 08.03.06 10:37 
Wow danke für deine Funktion !

Ich habe mich in der Zeit auch mal schlau gemacht.
Die Zeichen die du maskierst sind laut dem MySQL Manual nicht alle, aber die kann man ja einfach nachrüsten.
dev.mysql.com/doc/re...n/string-syntax.html

Ich werde die Funktion demnächst mal testen.

Vielen Dank nochmal.

_________________
In the beginning was the word.
And the word was content-type: text/plain.
digi_c
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1905

W98, XP
D7 PE, Lazarus, WinAVR
BeitragVerfasst: Mi 08.03.06 14:24 
Ich versteh leider nicht so wirklich, welche Auswirkungen die Slashes haben sollen :( oder reden wir von zwei verschiedenen Sachen?
Postman
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

Windows 7
Delphi 2010
BeitragVerfasst: Di 31.07.07 16:52 
Um das Thema nochmal aufzugreifen:

Hab die Funktion von delfiphan mal bei mir eingebaut und bekomme jetzt nen MySQL Fehler gemeldet wenn ich sie in Kombination mit " benutze. Hat sich da irgendwas geändert? Benutze MySQL 5.0.32

ausblenden SQL-Anweisung
1:
'SELECT * FROM `test` WHERE User LIKE "%'+User+'%" AND Adresse LIKE "%'+Adresse+'%" ORDER BY User'					
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 31.07.07 17:16 
Wenn du die DLLs von MySql verwendest und einen vollständigen Delphi Header hast, dann sollte die Funktion mysql_real_escape_string (oder ähnlich) existieren. Ich empfehle dir, diese offizielle Funktion zum Escapen zu verwenden.
Postman
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

Windows 7
Delphi 2010
BeitragVerfasst: Di 31.07.07 17:31 
Hm ich verwende MyDac um auf die Datenbank zuzugreifen, keine Ahnung ob man da eine solche Funktion aufrufen kann oder ob sie eh schon in der Komponente integriert ist.
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Do 02.08.07 19:18 
Die saubere Variante wäre es aber, mit Parametern zu arbeitern.

ausblenden SQL-Anweisung
1:
'SELECT * FROM `test` WHERE User LIKE :User AND Adresse LIKE :Adresse ORDER BY User'					

Danach Parameter :User und :Adresse setzen. Wie bzw. ob das mit MyDac geht, weiss ich nicht.