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

  参考了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. 格而知之7:我所理解的Runtime(2)

    消息发送(Messaging) 8.以上便是runtime相关的一些数据结构,接下来我们回看一开始的疑问: objc_msgSend()函数在执行的过程中是如何找到对应的类,找到对应的方法实现的呢? ...

  2. 格而知之4:寻找EXC_BAD_ACCESS

    EXC_BAD_ACCESS算是一个比较常见的错误,大部分情况下,它出现在某个对象还未初始化或已被释放后,还去试图访问这个对象的时候,即是在出现悬挂指针的时候(当然也有非悬挂指针导致的EXC_BAD_ ...

  3. 解决ios上微信无法捕获返回键按钮事件的问题

    1 //匿名函数 $(function(){ getHistory(); var flag=false; setTimeout(function(){ flag=true },1000) window ...

  4. Java 的zip压缩和解压缩

    Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...

  5. JNI_最简单的Java调用C/C++代码

    JNI_最简单的Java调用C/C++代码 JNI.是Java Native Interface的简称,中文是"Java本地调用".通过这种技术能够做到下面两点: Java程序中的 ...

  6. Android 之 Gallery

    1    在 xml 布局中添加 Gallery activity_main.xml <LinearLayout xmlns:android="http://schemas.andro ...

  7. 【二分查找+优化O(n)】【续UVA1121】Subsequence

    之前的二分答案做法 http://blog.csdn.net/zy691357966/article/details/40212215 二分查找做法: 我们首先试试只枚举终点.对于终点j,我们的目标是 ...

  8. 完美实现同时分享图片和文字(Intent.ACTION_SEND)

    private void share(String content, Uri uri){ Intent shareIntent = new Intent(Intent.ACTION_SEND); if ...

  9. HDU 2604 - Queuing

    长度为 n 有男有女的队伍里没有 fmf 和 fff 的序列有多少 判断最后一个人无法得出结论 于是判断最后两人的递推式: fm(n) =   mm(n-1) //最后两人为fm的长度为n的队伍 只能 ...

  10. linux学习笔记之线程同步机制

    一.基础知识. 1:线程同步机制:互斥量,读写锁,条件变量,自旋锁,屏障. 1,互斥量:每个进程访问被互斥量保护的资源时,都需要先对互斥量进行判断. 1)互斥量重要属性:进程共享属性,健壮属性,类型属 ...