「配枪朱丽叶。」

RootのCTF学习笔记。

关于PHP中md5比较绕过方式的总结

<?php
session_start();
error_reporting(0);
if (@$_POST['username'] and @$_POST['password'])
{    
	$username = $_POST['username'];
    $password = $_POST['password'];
	if ($username == $password) { 
        echo "Your input can't be the same";
    } 
    else if ((md5($username) == md5($password))){
        echo "Good"; 
	}else {
        echo "<pre> Invalid password</pre>";
    }
}
?>

240610708 和 QNKCDZO md5值类型相似,但并不相同,在”==”相等操作符的运算下,结果返回了true,也可以利用数组绕过。

<?php
session_start();
error_reporting(0);
if (@$_POST['username'] and @$_POST['password'])
{    
	$username = $_POST['username'];
    $password = $_POST['password'];
	if ($username == $password) { 
        echo "Your input can't be the same";
    } 
    else if ((md5($username) === md5($password))){
        echo "Good"; 
	}else {
        echo "<pre> Invalid password</pre>";
    }
}
?>

相信大家都知道==与===的区别,全等于会检查类型和值。
md5对一个数组进行加密将返回NULL;而NULL===NULL返回true,所以可绕过判断。

<?php
session_start();
error_reporting(0);
if (@$_POST['username'] and @$_POST['password'])
{    
	$username = (string)$_POST['username'];
    $password = (string)$_POST['password'];
	if ($username == $password) { 
        echo "Your input can't be the same";
    } 
    else if ((md5($username) === md5($password))){
        echo "Good"; 
	}else {
        echo "<pre> Invalid password</pre>";
    }
}
?>

一看是全等,上来想到数组绕过解析为NULL,然而并不行。。因为值进行了string强制类型转换。

这里使用一个md5快速碰撞工具fastcoll,它可以生成两个MD5值相同的不同文件。

fastcoll_v1.0.0.5.exe -o a b

然后POST它们即可。

curl.exe 网址 --data-urlencode username@a --data-urlencode password@b


但是这个工具有一个缺陷,只能生成两个md5相同的文件。。
参考这篇文章,生成四个md5值相同的文件的方法如下:

【先生成两个MD5值相同的文件】
fastcoll_v1.0.0.5.exe -o test0 test1

【然后根据test1再生成两个MD5值相同的文件,此时test00,test01的MD5值相同】
fastcoll_v1.0.0.5.exe -p test1 -o test00 test01

【将test00的最后128位写入文件a,(-c 128 表示最后128位,tail读文件是从后往前读的,这128位正是test1和test00MD5不同的原因),同理处理一下test01】
tail -c 128 test00 > a
tail -c 128 test01 > b

【执行type命令将test0和a的内容写进test10中,将test0和b的内容写入test11】
type test0 a > test10
type test1 b > test11

于是就生成了test00,test01,test10,test11四个MD5值相同的文件。
tail.exe的下载链接