News for hackers. Stuff that matters.
Re:JPEG image of Shakespeare which is also a zip file contai... by catpolice on Tuesday October 30, 2018 @09:40PM (#18342719)
Anybody have any insight into what kind of trickery goes into this? I know a decent amount about both of the compression algorithms, but I've forgotten the structure of the file formats so I can't tell if this required any really funny business.
Re:JPEG image of Shakespeare which is also a zip file contai... by danielvf on Tuesday October 30, 2018 @10:29PM (#18342909)
I've written my own zip and unzipping code.

Basically the "top level" zip file section lives at the end of the zip file and contains pointers back into the file for the actual data. This is so that you can keep adding to a zip file without having to rewrite anything you have done previously - just append a few more files and then your updated directory at the end.

JPEG segments contain two special opening bytes, maybe a length, and then the actual data. So it would be trivial to have the last jpeg section in a jpeg file end with zip directory bytes. This directory could point back to data in other sections of the jpeg.

..my guess, anyway.

Re:JPEG image of Shakespeare which is also a zip file contai... by plaguuuuuu on Tuesday October 30, 2018 @11:02PM (#18343033)
I can't exactly remember what JPG and ZIP look like, but yeah pretty much, I've done it before.

What's surprising is that it works on Twitter. I tried the same thing with image hosts and the data got scrubbed. I guess they don't re-encode small images but I dunno.

Re:JPEG image of Shakespeare which is also a zip file contai... by Laforet on Wednesday October 31, 2018 @02:01AM (#18343644)
In other words twitter's backend did not sanitize image uploads properly.
Re:JPEG image of Shakespeare which is also a zip file contai... by DavidBuchanan on Tuesday October 30, 2018 @09:53PM (#18342769)
It isn't massively complex, but it did require some custom encoding/decoding logic. I plan to write a blog post on this some time soon...
Re:JPEG image of Shakespeare which is also a zip file contai... by comboy on Wednesday October 31, 2018 @04:06AM (#18344014)

    cat file.jpg file.zip > magic.jpg
that's all there is to it (tested under linux, file compressed and decompressed with zip command)
Re:JPEG image of Shakespeare which is also a zip file contai... by Retr0spectrum on Wednesday October 31, 2018 @06:01AM (#18344416)
That's not all there is to it. Did you try uploading the resulting file to twitter?
Re:JPEG image of Shakespeare which is also a zip file contai... by comboy on Wednesday October 31, 2018 @07:14AM (#18344729)
I did not. This is just a response to the parent comment not to the post itself.

(Although if they do something with this file and you still are able to do this trick that would seem even more .. interesting on their side than leaving images below certain size untouched)

Re:JPEG image of Shakespeare which is also a zip file contai... by tyingq on Wednesday October 31, 2018 @12:00PM (#18346826)
The zip offset fields would be wrong, so your unzip must have some error correction. Use "zip -A zipfile" on Linux to fix them. As mentioned though, image upload sites would likely scrub out the extra data.
Re:JPEG image of Shakespeare which is also a zip file contai... by ThrustVectoring on Tuesday October 30, 2018 @11:37PM (#18343151)
The only trickery involved is making sure it survives twitter's compression and image cleaning algorithms. Other than that, programs that read images will ignore garbage data after what should be there for a jpg or whatever, and programs that open archives will ignore anything that comes before the archive header, so it's literally as straightforward as appending a .rar file at the end of a .jpg file.
Re:JPEG image of Shakespeare which is also a zip file contai... by charonn0 on Wednesday October 31, 2018 @01:38PM (#18347714)
The JPG format ignores data that comes after the EOF marker, and the zip format ignores data that comes before the first file entry.