Advertisement
opexxx

pdfcarver.py

Jul 12th, 2014
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. ## Stupid, simple, PDF carver - by @bbaskin
  2.  
  3. import sys
  4.  
  5. try:
  6.   fname = sys.argv[1]
  7. except IndexError:
  8.     print "Please specify file to carve PDFs from."
  9.     quit()
  10.  
  11.  
  12. f = open(fname, 'rb')
  13. inPDF = False
  14. count = 0
  15. while f:
  16.     byte = f.read(1)
  17.     if byte == b"":
  18.         break
  19.     if byte == "%":
  20.         if inPDF:
  21.             byte2 = f.read(4)
  22.             if byte2 == "%EOF":
  23.                 PDF_end =  f.tell()
  24.                
  25.                 PDF_size = PDF_end - PDF_begin
  26.                 f.seek(PDF_begin)
  27.                 pdf = f.read(PDF_size)
  28.                 outfname = fname + "_%s.pdf" % count
  29.                 print "Out: %s\t Offset: 0x%x\t Size: %d\t End Offset: 0x%x" % (outfname, PDF_begin, PDF_size, PDF_end)
  30.                 open(outfname, 'wb').write(pdf)
  31.                 count += 1
  32.                 inPDF = False
  33.         else:
  34.             byte2 = f.read(3)
  35.             if byte2 == "PDF":
  36.                 PDF_begin =  f.tell() - 4
  37.                 inPDF = True
  38. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement