Embedding ZIP Files Inside JPG Files Using ColdFusion

Posted January 16, 2009 at 4:42 PM

Tags: ColdFusion

A while back, I saw a interesting trick where someone used the MS DOS command, "copy," to hide MP3 files inside of images that they were uploading to Flickr. I thought that idea might be something fun to play around with in ColdFusion. After reading the documentation for the MS DOS copy command, it turns out that the trick they were using was simply appending one binary data to another. In ColdFusion and Java, that's pretty easy to do... but, could that possibly be all that it takes?

To test this from end to end, I am gonna do the following:

  1. ZIP several files together.
  2. Embed that ZIP file inside of a JPG image file.
  3. Read image file.
  4. Output image file to screen with new format.
  5. Read ZIP entries out of image and output query.
  6. Extract one of the ZIP entries and output.

I figure this way, we can see if the ZIP file can, in fact, be embedded in an image. And, if so, can we still use the image file successfully as both an image and a ZIP archive.

Here's the test:

 Launch code in new window » Download code as text file »

  • <!---
  • Store the name of the image file into which we want
  • to embed the zipped data.
  • --->
  • <cfset strImageFile = "girl.jpg" />
  •  
  • <!---
  • This is the name of our new graphic with the embedded
  • zip file.
  • --->
  • <cfset strNewImageFile = "special_girl.jpg" />
  •  
  •  
  • <!---
  • Create the array of files we want to zip up and embed
  • with in the image file.
  • --->
  • <cfset arrFileNames = [
  • "DataFile_1.txt",
  • "DataFile_2.txt",
  • "DataFile_3.txt",
  • "Song.mp3"
  • ] />
  •  
  •  
  • <!---
  • Create our ZIP file based using all of the files above.
  • This is the zip file that will be embedded in the image.
  • --->
  • <cfzip
  • action="zip"
  • file="#ExpandPath( './data.zip' )#"
  • overwrite="true">
  •  
  • <!--- Loop over files to add zip entries. --->
  • <cfloop
  • index="strFileName"
  • array="#arrFileNames#">
  •  
  • <cfzipparam
  • source="#ExpandPath( './#strFileName#' )#"
  • />
  •  
  • </cfloop>
  •  
  • </cfzip>
  •  
  •  
  • <!---
  • Create a binary output stream. This will be used to
  • copy the zip file to the image file.
  • --->
  • <cfset objOutputStream = CreateObject(
  • "java",
  • "java.io.ByteArrayOutputStream"
  • ).Init()
  • />
  •  
  •  
  • <!---
  • Loop over files that we want to join. The first is the
  • image and the second is our zip data file.
  • --->
  • <cfloop
  • index="strFileName"
  • list="#strImageFile#|data.zip"
  • delimiters="|">
  •  
  • <!--- Read in the file binary data (byte array). --->
  • <cffile
  • action="readbinary"
  • file="#ExpandPath( './#strFileName#' )#"
  • variable="binFileData"
  • />
  •  
  • <!--- Append the file to the binary output buffer. --->
  • <cfset objOutputStream.Write(
  • binFileData,
  • JavaCast( "int", 0 ),
  • JavaCast( "int", ArrayLen( binFileData ) )
  • ) />
  •  
  • </cfloop>
  •  
  •  
  • <!--- Write the byte array stream to disk. --->
  • <cffile
  • action="write"
  • file="#ExpandPath( strNewImageFile )#"
  • output="#objOutputStream.ToByteArray()#"
  • />
  •  
  •  
  • <!---
  • Now, read the image file into ColdFusion and write it
  • to the browser. This will test to see if it can be read
  • and manipulated properly as an image.
  • --->
  • <p>
  • <cfimage
  • action="writetobrowser"
  • source="#strNewImageFile#"
  • format="png"
  • />
  • </p>
  •  
  •  
  • <!---
  • Now, read the contents of the "zip" data out of the new
  • image file. This will test to see if the image file can
  • be read and properly manipulated as a ZIP file.
  • --->
  • <cfzip
  • action="list"
  • file="#ExpandPath( strNewImageFile )#"
  • name="qEntry"
  • />
  •  
  • <!--- Trim the query for output. --->
  • <cfquery name="qEntry" dbtype="query">
  • SELECT
  • name,
  • size,
  • type
  • FROM
  • qEntry
  • </cfquery>
  •  
  • <!--- Output the ColdFusion query. --->
  • <cfdump
  • var="#qEntry#"
  • label="#strNewImageFile# w/ Data Zip"
  • />
  •  
  •  
  • <!--- Read the text data for a given entry. --->
  • <cfzip
  • action="unzip"
  • file="#ExpandPath( strNewImageFile )#"
  • destination="#ExpandPath( './output' )#"
  • entrypath="DataFile_1.txt"
  • />
  •  
  • <!--- Include the text file. --->
  • <p>
  • <cfinclude template="./output/DataFile_1.txt" />
  • </p>

As you can see, I am following the exact outline as above. And, when we run the above code, we get the following screen output:

 
 
 
 
 
 
Embedding ZIP Data Files Inside Of JPG Images. 
 
 
 

Holy cow! I can't believe that actually works - and, that it is done so easily. ColdFusion successfully uses the new merged file as a JPG (converting it to a PNG for output) using CFImage; and, ColdFusion's CFZIP tag is able to read all the data entries and extract a given one to disk.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jan 16, 2009 at 4:55 PM // reply »
56 Comments

It's either really cold or...


Jan 16, 2009 at 4:56 PM // reply »
6,516 Comments

... or this ColdFusion post is just really really exciting :)


Jan 17, 2009 at 5:40 PM // reply »
15 Comments

Very clever. :-) Can this be put to a real world use? For example, when I try to email zip files or other non-Office files to certain organisations/clients it's rejected by their filewall, so sending an innocent looking file could be slipped through. (Wouldn't that be like a tojan, something scanners are supposed to detect?)

Even if it got through how would the person at the other end unmerge the files from the email attachment keeping in mind that even if an "unmerge" web app was available on the Internet the resulting downloads would be blocked too. Hmm. AIR can't run CFML on a desktop yet can it?


Jan 17, 2009 at 8:55 PM // reply »
102 Comments

Well, I guess if the local computer was running CF, then it could call localhost, but it can't run a self-contained instance of CF.


Jan 18, 2009 at 4:21 PM // reply »
6,516 Comments

@Gary,

You can actually just rename the file and it works. So, if you saved the special_girl.jpg and renamed it to be "special_girl.zip" and double-clicked it - it would open up as a ZIP file. No need to do anything programmatically.


Jan 19, 2009 at 10:07 PM // reply »
15 Comments

"The compressed folder is invalid or corrupt". :-( WinRar doesn't like it either.


Jan 19, 2009 at 10:20 PM // reply »
6,516 Comments

@Gary,

Did you rename it as .ZIP or a .RAR file? I'll put together a demo video tomorrow, just to demonstrate it, to make sure we're all on the same page.


Jan 20, 2009 at 5:51 AM // reply »
15 Comments

Renamed to .zip. A demo video? Cool, I like them. :-)


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »