Delphi考慮sql注入 QuotedStr

以前只在BS架構的項目中考慮了Sql注入問題,卻不多考慮到用了多年的Delphi項目也應該考慮Sql注入的問題,今天作了個實驗,成功完成注入,把表裏數據所有刪除,之後再作Delphi項目還真的考慮這個問題。sql

整體講,大致知道有兩種方式能夠避免Delphi中的Sql注入:一、用QuotedStr替代'''進行字符串拼接;二、採用傳參數的方式與數據庫交互,這種方式哪天再仔細體驗一下。數據庫

 

如下爲一個小測試,一個簡單的插入語句,若是Edit1內容爲
abc') delete from tb1 insert into tb1(Id, Name) values(123, 'xxxx
則運行後,tb1表中以前數據將所有清除,只剩下insert into tb1(Id, Name) values(123, 'xxxx
添加的一條服務器

 

[delphi]  view plain copy
 
  1. procedure TForm1.Button1Click(Sender: TObject);  
  2. var  
  3.   sqlStr: String;  
  4. begin  
  5.   sqlStr := 'insert into tb1(Id, Name) values(1, ''' + edit1.Text + ''')';  
  6.   self.ADOQuery1.SQL.Text := sqlStr;  
  7.   self.ADOQuery1.ExecSQL;  
  8.   ShowMessage('成功完成Sql注入');  
  9. end;  

 

 

改爲sqlStr := 'insert into tb1(Id, Name) values(1, ' + QuotedStr(edit1.Text) + ')';架構

則可避免。測試

 

QuotedStr參考以下語句: spa

adoquery1.sql.text:=
'select 字符型編號 from YourTable where 字符型編號='abc' and 整型編號=123';
等價於
adoquery1.sql.text:=
'select '+AFieldName+' from '+ATableName+' where '+AFieldName
+'='''+AStr+''' and 整型編號='+AnIntStr;
也等價於
adoquery1.sql.text:=
'select '+AFieldName+' from '+ATableName+' where '+AFieldName
+'='+QuotedStr(AStr)+' and 整型編號='+Inttostr(AnInt);
傳到數據庫服務器爲:
select 字符型編號 from YourTable where 字符型編號='abc' and 整型編號=123.net

相關文章
相關標籤/搜索