Append Script to an Image

2

2

I have a JPG image. I want to add/append a small piece of script(It can be of any language like PHP,Javascript,etc), but the image extension should not be changed. It must be jpg itself even after appending the script and I want to view the image in normal image viewers. Is it possible to achieve this?

Mad Angle

Posted 2014-08-13T05:57:42.377

Reputation: 129

3A little background or the goal you are trying to reach with this construction would be appreciated. – Pavel – 2014-08-13T11:13:55.753

@PavelPetrman its for some kind of tracking purpose:) – Mad Angle – 2014-08-27T03:47:05.157

Answers

6

There's something you can do with the Windows copy facility and a RAR archive, which might suit what you're looking for. Get a JPG and a RAR and run the following command in the Windows command prompt:

copy /b image.jpg + archive.rar finalimage.jpg

This will produce a JPG image that can be opened using the WinRAR archiver to reveal files inside. It works because the RAR format is such that any program designed to handle the archives will scan through the headers of any binary file and only engage once it finds the "Rar!" header. As a result, you can place a "Rar!" header after another file (in this case a JPG, but it also works for MP3 files) without damaging the data of either file.

Under a unix operating system this can be achieved with the cat command as shown.

cat image.jpg archive.rar > finalimage.jpg

In regards to your initial question, I sincerely hope your intention was not to produce a JPG that would contain a script that would be executed by users upon viewing the image, or as some sort of 21st-century user-hostile copy-protection, as time has demonstrated again and again that such methods not only cause considerable backlash but often serve only to irritate the people who are giving you legitimate custom (with pirated versions of your material often having the offending protection either neutered or stripped out entirely).

seagull

Posted 2014-08-13T05:57:42.377

Reputation: 4 278

+1 for the telling us how RAR works. Equivalent linux command is cat image.jpg archive.rar > finalimage.jpg – Kenneth L – 2014-08-13T08:54:10.183

That simple, huh? It's a neat command either way, and chronically underused - I hope it's of some use to you. – seagull – 2014-08-13T08:55:55.547

I have gone through this. http://www.online-tech-tips.com/computer-tips/hide-file-in-picture/

– Mad Angle – 2014-08-13T09:14:43.577

@seagull, technically, concatenating files is the intended usage of cat, hence the name. The usage like cat file.txt | grep "hello" is rather crude and not true Unix style – Sebastian Godelet – 2014-08-13T09:50:55.673

I'll have to disagree with your statement that the RAR format is such that all archive managers are capable of handling binary files with the RAR signature anywhere inside those files. That's simply not true. WinRAR and a few other archive managers are able to find and open the embedded archives only because they are programmed to do so. It has nothing to do with the RAR format. – Vinayak – 2014-08-13T13:30:55.643

Consider this example: instead of appending a RAR archive, use the same command to append a PDF document. Now rename finalimage.jpg to whatever.pdf and open it with the Reader app on Windows 8. It'll show you the document you appended to the JPEG. However, if you opened the same file using Adobe Acrobat, it'll say it's an invalid/corrupt PDF file – Vinayak – 2014-08-13T13:35:45.470

@Vinayak that was my understanding of the RAR format; the practice only works (as far as I am aware) if the Rar! header is placed after the JFIF data has ended (it wouldn't work if it were in the middle of another file, for instance). My interpretation that such a feature composed part of the RAR standard was based on the capability of WinRAR, the official program coded by the codec's developers, for such a feature; if this is an oversight, I defer to you. – seagull – 2014-08-13T13:52:20.163

I was curious, so I tested that theory right now. You are right that the archive is detected and parsed only if the RAR file is appended after the JFIF data but that only holds true for WinRAR. 7-Zip was still able to extract the archive even when the RAR file was placed somewhere in between the JFIF data. – Vinayak – 2014-08-13T14:02:41.523

Curious and curiouser. I guess not all archivers are created equal. – seagull – 2014-08-13T14:03:29.540

I'm not saying it's an oversight, it's just a misunderstanding of the RAR format is all. And this feature is certainly not exclusive to WinRAR as 7-Zip can do it and so can Microsoft Reader (with PDFs) and I believe a lot of other software programs do this too. – Vinayak – 2014-08-13T14:10:34.220

3

While you can hide something within a JPEG file, I'm afraid that adding a script that runs is not possible.

JPEG is a standard set out by Joint Photographic Experts Group, and these standards are now part of ISO standards. However in the standards there is no standard about "codes". That means most programs, if not all, would not understand the "code" you embedded in the JPEG file as it is no standard.

If you are trying to exploit security issues of certain JPEG reader, that's an other story... probably you will need a lot more research.

Kenneth L

Posted 2014-08-13T05:57:42.377

Reputation: 12 537