md5 collision(md5碰撞)

题目来源:南京邮电大学网络攻防训练平台 Web题 md5 collision

解题过程:

点开题目标题,呈现在眼前的是一段php代码,代码如下:

$md51 = md5('QNKCDZO');
$a = @$_GET['a'];
$md52 = @md5($a);
if(isset($a)){
    if ($a != 'QNKCDZO' && $md51 == $md52) {
        echo "nctf{*****************}";
    } else {
        echo "false!!!";
    }
}
else{
    echo "please input a";
}

这段代码什么意思呢?

首先,变量md51的值是经过md5加密的字符串QNKCDZO,字符串QNKCDZO经过md5加密之后的值为:0e830400451993494058024219903391。之后的变量a,分析源代码可知,如果a的值不为QNKCDZO,并且md5的值也为0e830400451993494058024219903391,则会输出flag。根据之前的了解,不过好像除了QNKCDZO,并没有其他字符串的md5值为0e830400451993494058024219903391。再回头看源码,注意到判断md51和md52是否相等的时候,使用$md51 == %md52,这里使用的是 == 而不是 === ,也就是说md51和md52不需要完全相等。

拓展:一个数字和一个字符串进行比较,PHP会把字符串转换成数字再进行比较。PHP转换的规则的是:若字符串以数字开头,则取开头数字作为转换结果,若无则输出0。例如:123abc转换后应该是123,而abc则为0,0==0这当然是成立的啦!所以,0 ==’abc’是成立的。当有一个对比参数是整数的时候,会把另外一个参数强制转换为整数。

这个题目也是这个原理,如果你提前知道md5碰撞的概念,同时知道了在php中md5中的0e的比较,这道题目就非常简单了。

如果md的值是以0e开头的,那么就与其他的0e开头的md5值是相等的,这里给大家列举了一写:常见的MD5碰撞:md5值为0e开头

php代码测试:

<?php
$a = md5('240610708'); // = 0e462097431906509019562988736854
$b = md5('QNKCDZO'); // = 0e830400451993494058024219903391
var_dump($a == $b);
?>

输出:true

这样就更加证明了这一点。

所以,这里我们就给a传值经过md5加密后是0e开头的字符串,以传入字符串s878926199a,为例:

end!

总结:

  • php使用==比较数字和字符串时,将字符串转换成数字后与数字进行比较。
  • php字符串转换成数字的规则是:
    • 字符串以数字开头:取前面的数字
    • 字符串不以数字开头:0
  • 如果==两侧的字符串都是以0e开头,并且后面还是数字,就会被解释成科学计数法,也就是0的多少多少次幂,也就是后面的数字无论是多少,最后结果都为0.
  • 上一点也是这道题的另一种思路。找到另一个字符串,这个字符串的md5值也以0e开头。
  • 以0e开头的串很危险!!!

 

相关推荐
sha碰撞,MD5碰撞实现,#!/usr/local/bin/perl # It was noted that Intel IA-32 C compiler generates code which # performs ~30% *faster* on P4 CPU than original *hand-coded* # SHA1 assembler implementation. To address this problem (and # prove that humans are still better than machines:-), the # original code was overhauled, which resulted in following # performance changes: # # compared with original compared with Intel cc # assembler impl. generated code # Pentium -25% +37% # PIII/AMD +8% +16% # P4 +85%(!) +45% # # As you can see Pentium came out as looser:-( Yet I reckoned that # improvement on P4 outweights the loss and incorporate this # re-tuned code to 0.9.7 and later. # ---------------------------------------------------------------- # Those who for any particular reason absolutely must score on # Pentium can replace this module with one from 0.9.6 distribution. # This "offer" shall be revoked the moment programming interface to # this module is changed, in which case this paragraph should be # removed. # ---------------------------------------------------------------- # <appro@fy.chalmers.se> $normal=0; push(@INC,"perlasm","../../perlasm"); require "x86asm.pl"; &asm_init($ARGV[0],"sha1-586.pl",$ARGV[$#ARGV] eq "386"); $A="eax"; $B="ecx"; $C="ebx"; $D="edx"; $E="edi"; $T="esi"; $tmp1="ebp"; $off=9*4; @K=(0x5a827999,0x6ed9eba1,0x8f1bbcdc,0xca62c1d6); &sha1_block_data("sha1_block_asm_data_order"); &asm_finish(); sub Nn { local($p)=@_; local(%n)=($A,$T,$B,$A,$C,$B,$D,$C,$E,$D,$T,$E); return($n{$p}); } sub Np { local($p)=@_; local(%n)=($A,$T,$B,$A,$C,$B,$D,$C,$E,$D,$T,$E); local(%n)=($A,$B,$B,$C,$C,$D,$D,$E,$E,$T,$T,$A); return($n{$p}); } sub Na { local($n)=@_; return( (($n )&0x0f), (($n+ 2)&0x0f), (($n+ 8)&0x0f), (($n+13)&0x0f), (($n+ 1)&0x0f)); } sub X_expand { local($in)=@_; &comment("First, load the words onto the stack in network byte order"); for ($i=0; $i<16; $i+=2) { &mov($A,&DWP(($i+0)*4,$in,"",0));# unless $i == 0; &mov($B,&DWP(($i+1)*4,$in,"
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页