十五年测试老手,长期负责WEB\APP 项目测试,目前主要负责团队管理工作。

[转载]防止黑客发起 SQL注入的方法

上一篇 / 下一篇  2008-11-22 14:00:27 / 个人分类:SQL注入

51Testing软件测试网U\`0e T

  网站要做足安全, 特别是对防SQL注入, 因为大多数程序员都会很粗心大意而导致网站被黑。51Testing软件测试网Ph'U!fj9y0o

51Testing软件测试网_U'K_.[K D5WW

  php脚本:

_2LwT_ @6J8O^0

9S~]B5u0  基本上php本身就带有类似功能的函数了, 比如mysql_real_escape_string, addslashes等.

Y0sZ?_9S h4Z,~&Q0

pc v&J)b)VS]7W0  大多数虚拟主机商比如耐思尼克(都会开启了magic_quotes_gpc这个选项, 那提交数据时, 会自动执行了addslashes这个函数, 这样就可以杜绝大多数的注入了

o pj0p lpO0

Jrs7{:G}![?0  另外因为mysql是不分数字还是文本,都可以用''来括住, 所以建议在写sql的时候, 参数都用''来括起来

d,ct IS0@D051Testing软件测试网MQ^'s E1O2v

  使用方法如下:51Testing软件测试网Ju'e&y,|G5uHI

W0v;z hxsf0  $query = mysql_real_escape_string("SELECT * FROM products WHERE name='$productname'");

XU ^m(TKC051Testing软件测试网`T B$y@

  asp脚本:

o.|!Nzr,j#u#_c"A0

)aS&Oy fh:^Y8J0  FUNCTION CHECKSTR(ISTR)51Testing软件测试网NI6B(i!}Gi#@

"LXB$B I0  DIM ISTR_FORM,SQL_KILL,SQL_KILL_1,SQL_KILL_2,ISTR_KILL

@$e]D$C051Testing软件测试网)AMt0f7`g5Q1R8M

  IF ISTR="" THEN EXIT FUNCTION

3r/G)a])z;eY9S7y8PL051Testing软件测试网8^rI3{:}3n*V

  ISTR=LCase(ISTR)

p8A:YK`1c0B_6H#t0

gV#Uh)Y"BcpbH0  ISTR_FORM=ISTR51Testing软件测试网Z HX)[%U*B

51Testing软件测试网]\^0s,MyI]9`

  SQL_KILL="' and exec insert51Testing软件测试网)U Ubo_q4HBv B

51Testing软件测试网.m)AvF]tn

   select delete update count * % chr mid master truncate char declare set ; from ="

0_-GA;^&f7D|\051Testing软件测试网*j;@pW,N

  SQL_KILL_1=SPLIT(SQL_KILL," ")51Testing软件测试网;\HdH0C*B,g0r

51Testing软件测试网[!tb/H @8s

  FOR EACH SQL_KILL_2 IN SQL_KILL_1

U t-U3}['Y1E+zufq}051Testing软件测试网3iP#Yf2{

  ISTR=REPLACE(ISTR,SQL_KILL_2,"")51Testing软件测试网&TWB6{7de

#xI3mpr_ y l0  NEXT51Testing软件测试网-F2{z5WB b2d

51Testing软件测试网*_JL,eg

  CHECKSTR=ISTR

'y"ZU7? [0

i1G`c2Q;GCO6P(U0  ISTR_KILL=REPLACE(ISTR_FORM,ISTR,"")

2\ ieJ#Xb9L0

1b.k0a-E5L+?t@0  IF ISTR<>ISTR_FORM THEN

x)v^NI$B+vB0

b$C3r6i}\0  RESPONSE.WRITE ""

uy L4~'Za+PPR'M0

i3YYDo0  RESPONSE.END

Eql8J,`M0

l f}1P0Q k R!D0  END IF51Testing软件测试网*B YLv6m?

51Testing软件测试网e3BBy6h.l5}

  END FUNCTION51Testing软件测试网"l~-s*\O1[&s,^&C

51Testing软件测试网 N q X LT_["l2Ae

  C#脚本:51Testing软件测试网x kleHm^$Z

m G/T-x XZ0  bool CheckParams(params object[] args)
;o[+U"u2DWn9G n0
$Vv"].g8anJD0  {
-bv5_(pm"tO0
5YO?n8dn:u(c4Ar0  string[] Lawlesses={"=","'"};51Testing软件测试网E"EU$d:kk K$h
51Testing软件测试网b(Bz5a7wn!v&I-\&L
  if(Lawlesses==null  Lawlesses.Length<=0)return true;51Testing软件测试网2\&H:M(O&w(X|(r
51Testing软件测试网0juc8_-da|C'y!k
  //构造正则表达式,例:Lawlesses是=号和'号,则正则表达式为 .*[=}'].* (正则表达式相关内容请见MSDN)
AD k R1VL2k b0
3s"]%M+gQx s"]g0  //另外,由于我是想做通用而且容易修改的
C8d&oJA,R Am.j7DX0
&uP1Si0~$O K0  函数,所以多了一步由字符数组到正则表达式,实际使用中,直接写正则表达式亦可;51Testing软件测试网K%xHg5g$RI.T-D

2oW~5P uHC0q Y d0  string str_Regex=".*[";51Testing软件测试网Hh)rYt/Re2j]8o
51Testing软件测试网]{Jh7HuJ
  for(int i=0;i< Lawlesses.Length-1;i++)
Sr;LU ]C0c0
1]pK8V3L0  str_Regex+=Lawlesses+" ";51Testing软件测试网rO"M(w)M

*XC8KUP8]4xv0  str_Regex+=Lawlesses[Lawlesses.Length-1]+"].*";
&v yeVA*q/e9dX7q0
6db2g:C!wUk_[0  //51Testing软件测试网.EJl"M#l$f

6c#N-qiSd0  foreach(object arg in args)
[XSK.]6Uv)F0
O7xeo"V*G2~gW9Y0  {51Testing软件测试网x4J6MM8z

N#kU8GuMO${kT5q0  if(arg is string)//如果是字符串,直接检查
9i@*Q ?U wh(O e7Q051Testing软件测试网LPZSK2V D
  {
Xe9K$I4a$}(x0
&g"P HN9i0  if(Regex.Matches(arg.ToString(),str_Regex).Count>0)51Testing软件测试网;F-B'Mq j#v7u
51Testing软件测试网Atd0RPS
  return false;
7qD&U0} lz A @0
$y7V&Yfg-[w,K_$P0  }51Testing软件测试网0]n)E fHx W/W V;@

$Ik8e,}"PhU~G0  else if(arg is ICollection)//如果是一个集合,则检查集合内元素是否字符串,是字符串,就进行检查51Testing软件测试网}1{e6Q*oo7`

Hi_K r T^0  {
1x)GU6[X!]051Testing软件测试网WvC.H{0`Ix I
  foreach(object obj in (ICollection)arg)
cp:}"z/s8~x051Testing软件测试网Wq3R(EiS4| kz
  {51Testing软件测试网8k5g!_ ft0D-?6_+m]

r5j2fkds EE0  if(obj is string)51Testing软件测试网$] f!t c F&IA\w
51Testing软件测试网DyA3d$~/E;Mr$S7?
  {
i8hKQm*x[:d0
L\A,r,WcW)t0  if(Regex.Matches51Testing软件测试网D|$BDARY,~
51Testing软件测试网1?N4U#go;P.M C
  (obj.ToString(),str_Regex).Count>0)
B(}a0r!R(Q"Qm%kn051Testing软件测试网9K)vwe7n\
  return false;51Testing软件测试网6M4W.O e!p$I

wJ+`c)Rz0  }51Testing软件测试网1?!zpn,H1p
51Testing软件测试网1q&w5},GD H^
  }
$|YK)q0gi}:V1|0
^C}:D q'O!{0  }
0YgV dI/Z6]9|051Testing软件测试网^AF&OAO$F _Y
  }
5t!E L%B'|R s051Testing软件测试网E'} } oK J%x Tt
  return true;51Testing软件测试网(|8W:Yh9^y6~#Yh
51Testing软件测试网 tE,N%G$`+Emq
  }

$v n%} }"N:q1Eb_ e4]0

TAG: SQL注入 攻击

 

评分:0

我来说两句

Open Toolbar