byte order 即指 network / host, 多称 big-endian 和 little endian, 此处多谢赖同学的指点,big-endian 就是network byte order(网络序).
话说在抗日过程中,某一流程需要读取二进制文件,此文件由header+data组成, header 中含有MD5的checksum, 但是发现checksum 永远是失败的..., 研习后发现,MD5的校验是将header 从 little-endian 转变成 big-endian, 然后再加上原本的data 做最终的MD5值...
用python来验证这个二进制文件很容易, struct module 可以轻松实现. 所谓header 也就是对应C当中的某一个struct, 用python 匹配过来,然后转换,在输出。
for unsigned short:
>>> inp='\x04\x00'
>>> outp = struct.pack('!H', struct.unpack('=H', inp)[0])
>>> outp = struct.pack('!H', struct.unpack('=H', inp)[0])
for unsigned long:
>>> inp='\x04\x00\x04\x00'
>>> outp = struct.pack('!L', struct.unpack('=L', inp)[0])
执行动作就是, 首先将内存C代码结构按照正确的byte-order 来unpack,匹配之后,在将它pack 成需要的byte-order.
'=' : little endian
'!' : big endian
几行代码就可以计算出正确的checksum 了,无需要在编译一个private image...
python很强大......后果很严重.....
0 comments
Post a Comment