代码的执行时间挺长的,好囧!

  参考了https://13c5.wordpress.com/2015/04/20/plaidctf-2015-png-uncorrupt/的代码

  通过这个题目,也对Png文件格式更深入地理解了!

  使用这个代码的前提是将png signature里面的0x0a修改为0x0d0a

 from itertools import combinations
import binascii
import os def find_all(source,aim):
start=
while True:
start=source.find(aim,start)
if start==-:
return
yield start
start +=len(aim) def repair(source,aim,filedes,num,crc):
matchlist=list(find_all(source,'\x0a')) for subnet in combinations(matchlist,num):
subnet=sorted(subnet)
temp=''
if(num==):
temp=source[:subnet[]]+'\x0d\x0a'+source[subnet[]+:subnet[]]+'\x0d\x0a'+source[subnet[]+:subnet[]]+'\x0d\x0a'+source[subnet[]+:]
if(num==):
temp=source[:subnet[]]+'\x0d\x0a'+source[subnet[]+:subnet[]]+'\x0d\x0a'+source[subnet[]+:]
if(num==):
temp=source[:subnet[]]+'\x0d\x0a'+source[subnet[]+:]
if "%08x" % (binascii.crc32(temp)&0xFFFFFFFF)==crc:
filedes.write(temp)
filedes.write(binascii.a2b_hex(crc))
filedes.flush()
print "success"
break;
print "fail" uncfile=open("corrupt_735acee15fa4f3be8ecd0c6bcf294fd4.png","rb")
cocfile=open("correct.png","wb")
#first write
correct=uncfile.read(0x6d)
cocfile.write(correct)
cocfile.flush() correct=uncfile.read(0x4)#length
cocfile.write(correct)
cocfile.flush() uncorrect=uncfile.read(0x20000-0x1+0x4)
crc=uncfile.read(0x4)
crc=binascii.hexlify(crc)
print crc
repair(uncorrect,'\x0a',cocfile,,crc)#
#second write
correct=uncfile.read(0x4)#length
cocfile.write(correct)
cocfile.flush()
uncorrect=uncfile.read(0x20000-0x3+0x4)
crc=uncfile.read(0x4)
crc=binascii.hexlify(crc)
print crc
repair(uncorrect,'\x0a',cocfile,,crc)#
#third write
correct=uncfile.read(0x4)#length
cocfile.write(correct)
cocfile.flush()
uncorrect=uncfile.read(0x20000-0x1+0x4)
crc=uncfile.read(0x4)
crc=binascii.hexlify(crc)
print crc
repair(uncorrect,'\x0a',cocfile,,crc)#
#fourth write
correct=uncfile.read(0x4+0x4+0x20000+0x4)
cocfile.write(correct)
cocfile.flush()
#fifth write
correct=uncfile.read(0x4)#length
cocfile.write(correct)
cocfile.flush()
uncorrect=uncfile.read(0x20000-0x3+0x4)
crc=uncfile.read(0x4)
crc=binascii.hexlify(crc)
print crc
repair(uncorrect,'\x0a',cocfile,,crc)#
#6th
correct=uncfile.read(0x4)#length
cocfile.write(correct)
cocfile.flush()
uncorrect=uncfile.read(0x20000-0x1+0x4)
crc=uncfile.read(0x4)
crc=binascii.hexlify(crc)
print crc
repair(uncorrect,'\x0a',cocfile,,crc)#
#7th
correct=uncfile.read(0x4)
cocfile.write(correct)
cocfile.flush()
uncorrect=uncfile.read(0x20000-0x2+0x4)
crc=uncfile.read(0x4)
crc=binascii.hexlify(crc)
print crc
repair(uncorrect,'\x0a',cocfile,,crc)#
#8th
correct=uncfile.read(0x4+0x4+0x20000+0x4)
cocfile.write(correct)
cocfile.flush()
#9th
correct=uncfile.read(0x4)
cocfile.write(correct)
cocfile.flush()
uncorrect=uncfile.read(0x20000-0x1+0x4)
crc=uncfile.read(0x4)
crc=binascii.hexlify(crc)
print crc
repair(uncorrect,'\x0a',cocfile,,crc)#
#10th
correct=uncfile.read(0x4+0x4+0x216f)
cocfile.write(correct)
cocfile.flush() uncfile.close()
cocfile.close()

  

  结果:

参考文献:

  http://blog.csdn.net/gogor/article/details/5265710

  http://www.libpng.org/pub/png/apps/pngcheck.html

  http://www.libpng.org/pub/png/book/chapter08.html

  http://stackoverflow.com/questions/27238021/png-images-not-loaded

  https://13c5.wordpress.com/2015/04/20/plaidctf-2015-png-uncorrupt/

plaidctf2015 uncorrupt png的更多相关文章

  1. plaidctf2015 ebp

    很容易看出是格式化字符串漏洞.这里的格式化字符串漏洞不像传统的那样,格式化字符串是放在bss段中,并没放在栈上,因此利用起来有些困难. 不过,我们可以利用ebp,可以修改函数的ebp,从而能控制函数的 ...

  2. [PWN]fsb with stack frame

    0x00: 格式化字符串漏洞出现的时间很早了,偶然在前一段时间学到了一个其他的利用姿势,通过栈桢结构去利用格式化字符串漏洞. 原文链接:http://phrack.org/issues/59/7.ht ...

随机推荐

  1. A Corrupt Mayor's Performance Art(线段树区间更新+位运算,颜色段种类)

    A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100 ...

  2. ComponentName的意思

    ComponentName是用来打开其它应用程序中的Activity或服务的. 使用方法: Intent i=new Intent(); i.setComponent(new ComponentNam ...

  3. 如何把UIView转成UIImage,解决模糊失真问题

    最近工作中,遇到一个需求,需要把一个UIView对象转成UIImage对象显示.经过网络搜索,找到如下答案: ? 1 2 3 4 5 6 7 8 -(UIImage*)convertViewToIma ...

  4. tail

    tail用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理.常用查看日志文件. -f 循环读取 -q 不显示处理信息 -v 显示详细的处理信息 -c<数目> 显示的字节数 -n& ...

  5. 搭建高可用mongodb集群—— 分片

    从节点每个上面的数据都是对数据库全量拷贝,从节点压力会不会过大? 数据压力大到机器支撑不了的时候能否做到自动扩展? 在系统早期,数据量还小的时候不会引起太大的问题,但是随着数据量持续增多,后续迟早会出 ...

  6. Hello World 老调重谈

    namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine( ...

  7. canvas模糊事件处理

    不知道大家项目中有没有用到canvas时还有时候会出现模糊的情况: 具体推测可能是屏幕改变了,然而canvas的渲染对象并没有跟着一起变: 这里简单介绍个对象,window.devicePixelRa ...

  8. js判断是否安装flash

    <script type="text/javascript"> (function () { var noFlash = "你的浏览器没有安装Flash,会影 ...

  9. C#同步数据库的数据到Neo4J

    数据组件采用https://github.com/Readify/Neo4jClient 在nuget里面有 需要注意的是 以下是示例代码: using System;using System.Col ...

  10. U - stl 的 优先队列 Ⅰ

    Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...