Am I extracting binary JPEG data from this mysqldump correctly?
I have a very old .sql backup on the vbulletin site that I worked on 8 years ago. I am trying to see the file attachments that are stored in the DB. The script below extracts them all and validates as a hex dumped JPEG and validates the SOI (start of image) and EOI (end of image) bytes (FFD8 and FFD9 respectively) according to the JPEG wiki page .
But when I try to open them with evince I get this message "Error interpreting JPEG image file (JPEG data stream does not contain image)"
What can be done here?
Some background information:
- sqldump is about 8 years old.
- vbulletin 2.x is software that stored information
- Most likely php 4 was used
- most likely mysql 4.0, maybe even 3.x
- the data type of the column in which these attachments are stored in mediatext format
My Python 3.1 script:
#!/usr/bin/env python3.1
import re
trim_l = re.compile(b"""^INSERT INTO attachment VALUES\('\d+', '\d+', '\d+', '(.+)""")
trim_r = re.compile(b"""(.+)', '\d+', '\d+'\);$""")
extractor = re.compile(b"""^(.*(?:\.jpe?g|\.gif|\.bmp))', '(.+)$""")
with open('attachments.sql', 'rb') as fh:
for line in fh:
data = trim_l.findall(line)[0]
data = trim_r.findall(data)[0]
data = extractor.findall(data)
if data:
name, data = data[0]
try:
filename = 'files/%s' % str(name, 'UTF-8')
ah = open(filename, 'wb')
ah.write(data)
except UnicodeDecodeError:
continue
finally:
ah.close()
fh.close()
Update The JPEG wiki says that the FF bytes are section markers and the next byte indicates the section type. I see some that are not listed in the wiki page (in particular, I see a lot of 5C bytes, so FF5C). But the list has "common labels", so I'm trying to find a more complete list. Any guidance here would be appreciated as well.
a source to share
Update your question with an example SQL statement including multiple lines / bytes of a JPEG string value. Possibly the data is base64 encoded or even straight hex values. We will help you further.
It is also easier to see the content type of a file by issuing:
file yourfile.jpg
a source to share