Append Script to an Image

6,463

Solution 1

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).

Solution 2

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.

Share:
6,463

Related videos on Youtube

Mad Angle
Author by

Mad Angle

Programmer Who Doesn't Love to Code. Weird Right?? Full Stack Specialist Open Source Geek Blogger Footballer Movie Lover And last but not the least A HARDCORE RAJNIKANTH FAN reach me @ [email protected]

Updated on September 18, 2022

Comments

  • Mad Angle
    Mad Angle over 1 year

    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?

    • Pavel
      Pavel almost 10 years
      A little background or the goal you are trying to reach with this construction would be appreciated.
    • Mad Angle
      Mad Angle over 9 years
      @PavelPetrman its for some kind of tracking purpose:)
  • Kenneth L
    Kenneth L almost 10 years
    +1 for the telling us how RAR works. Equivalent linux command is cat image.jpg archive.rar > finalimage.jpg
  • seagull
    seagull almost 10 years
    That simple, huh? It's a neat command either way, and chronically underused - I hope it's of some use to you.
  • Mad Angle
    Mad Angle almost 10 years
  • Sebastian Godelet
    Sebastian Godelet almost 10 years
    @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
  • Vinayak
    Vinayak almost 10 years
    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
    Vinayak almost 10 years
    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
  • seagull
    seagull almost 10 years
    @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.
  • Vinayak
    Vinayak almost 10 years
    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.
  • seagull
    seagull almost 10 years
    Curious and curiouser. I guess not all archivers are created equal.
  • Vinayak
    Vinayak almost 10 years
    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.