<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Digital Exploration</title>
	<atom:link href="http://digitalexploration.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://digitalexploration.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Fri, 11 Nov 2011 05:04:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='digitalexploration.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Digital Exploration</title>
		<link>http://digitalexploration.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://digitalexploration.wordpress.com/osd.xml" title="Digital Exploration" />
	<atom:link rel='hub' href='http://digitalexploration.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Jpeg Header Definitions</title>
		<link>http://digitalexploration.wordpress.com/2009/11/17/jpeg-header-definitions/</link>
		<comments>http://digitalexploration.wordpress.com/2009/11/17/jpeg-header-definitions/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 16:51:08 +0000</pubDate>
		<dc:creator>imbeyondboredom</dc:creator>
				<category><![CDATA[Forensics]]></category>
		<category><![CDATA[Jpeg]]></category>

		<guid isPermaLink="false">http://digitalexploration.wordpress.com/?p=90</guid>
		<description><![CDATA[Intro Since I have gone over the basic structure of the jpeg file, I now need to show some of the headers needed to parse a jpeg file. I will include their byte signatures and structures as well as their names from the official JPEG standard. That way when you read other literature it wont [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=90&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Intro</h2>
<p>Since I have gone over the basic structure of the jpeg file, I now need to show some of the headers needed to parse a jpeg file. I will include their byte signatures and structures as well as their names from the official JPEG standard. That way when you read other literature it wont be as confusing.  This will probably be a boring read, but it will be useful for reference purposes later. (I&#8217;m half writing this for myself so I have it written down somewhere)</p>
<h2>Headers</h2>
<h3>Start of Image:</h3>
<ul>
<li>Abbreviation &#8211; SOI</li>
<li>Length &#8211; 2 bytes</li>
<li>Byte Value &#8211; <span style="color:#ffcc66;font-size:100%;">0xFFD8</span></li>
<li>Description &#8211; The simplest of the markers. This marker is the first two bytes of every jpeg file and has nothing following it. These two bytes imply that this file is most likely a jpeg file.</li>
<li>Example:</li>
</ul>
<table border="1">
<tbody>
<tr style="background-color:grey;color:black;">
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>A</td>
<td>B</td>
<td>C</td>
<td>D</td>
<td>E</td>
<td>F</td>
</tr>
<tr>
<td align="center">FF</td>
<td align="center">D8</td>
<td colspan="14" align="center"></td>
</tr>
</tbody>
</table>
<h3>Application Data:</h3>
<ul>
<li>Abbreviation &#8211; APP1 &#8211; APP15</li>
<li>Byte Value &#8211; <span style="color:#ffcc66;font-size:100%;">0xFFE0</span> &#8211; <span style="color:#ffcc66;font-size:100%;">0xFFEF</span></li>
<li>Description &#8211; Not to be confused with comments, this marker identifies applications information that is not defined in the official standard. The APP0 marker is special however and holds information specific to the JFIF and EXIF implementations of the jpeg. From what I can tell the jpeg standard was too vague so some guy came up with the JFIF standard and it stuck, EXIF is newer but definitely sticking, especially with digital cameras. Other than the APPo segment the data is application specific, so just skip the data unless you want to write a lot of parsers for it. Otherwise the structure of the APP0 segment (as defined by JFIF)  is below.</li>
<li>Structure -
<ol>
<li>Marker &#8211; 2 bytes
<ol>
<li>Byte Value &#8211; <span style="color:#ffcc66;font-size:100%;">0xFFE0</span> &#8211; <span style="color:#ffcc66;font-size:100%;">0xFFEF</span></li>
</ol>
</li>
<li>Length &#8211; 2 bytes
<ol>
<li>Again, this includes the length bytes</li>
</ol>
</li>
<li>identifier &#8211; 5 bytes
<ol>
<li><span style="color:#ffcc66;font-size:100%;">0x4A46494600 </span>(&#8220;JFIF&#8221; in ascii) (if this was an EXIF image then this would be &#8220;EXIF&#8221;)</li>
</ol>
</li>
<li>version &#8211; 2 bytes
<ol>
<li>the most significant byte is used for major revisions</li>
<li>the least significant byte for minor revisions</li>
</ol>
</li>
<li>units &#8211; 1 byte:
<ol>
<li>Units for the X and Y densities</li>
<li>0 =&gt; no units, X and Y specify the pixel aspect ratio</li>
<li>1 =&gt; X and Y are dots per inch</li>
<li>2 =&gt; X and Y are dots per cm</li>
</ol>
</li>
<li>Xdensity &#8211; 2 bytes
<ol>
<li>Horizontal pixel density</li>
</ol>
</li>
<li>Ydensity &#8211; 2 bytes
<ol>
<li>Vertical pixel density</li>
</ol>
</li>
<li>Xthumbnail size &#8211; 1 byte
<ol>
<li> 0 = no thumbnail</li>
</ol>
</li>
<li>Ythumbnail size &#8211; 1 byte
<ol>
<li> 0 = no thumbnail</li>
</ol>
</li>
<li>Thumbnail data &#8211; 3n bytes
<ol>
<li> packed (24-bit) RGB values for the thumbnail pixels</li>
<li> n = Xthumbnail       * Ythumbnail</li>
</ol>
</li>
</ol>
</li>
</ul>
<p>Example:</p>
<table border="1">
<tbody>
<tr style="background-color:grey;color:black;">
<td align="center">0</td>
<td align="center">1</td>
<td align="center">2</td>
<td align="center">3</td>
<td align="center">4</td>
<td align="center">5</td>
<td align="center">6</td>
<td align="center">7</td>
<td align="center">8</td>
<td align="center">9</td>
<td align="center">A</td>
<td align="center">B</td>
<td align="center">C</td>
<td align="center">D</td>
<td align="center">E</td>
<td align="center">F</td>
</tr>
<tr>
<td align="center">FF</td>
<td align="center">E0</td>
<td colspan="2" align="center">#2</td>
<td colspan="5" align="center">#3</td>
<td colspan="2" align="center">#4</td>
<td align="center">5</td>
<td colspan="2" align="center">#6</td>
<td colspan="2" align="center">#7</td>
</tr>
<tr>
<td align="center">8</td>
<td align="center">9</td>
<td colspan="14" align="center">#10</td>
</tr>
</tbody>
</table>
<p>Filled in:</p>
<table border="1">
<tbody>
<tr style="background-color:grey;color:black;">
<td align="center">0</td>
<td align="center">1</td>
<td align="center">2</td>
<td align="center">3</td>
<td align="center">4</td>
<td align="center">5</td>
<td align="center">6</td>
<td align="center">7</td>
<td align="center">8</td>
<td align="center">9</td>
<td align="center">A</td>
<td align="center">B</td>
<td align="center">C</td>
<td align="center">D</td>
<td align="center">E</td>
<td align="center">F</td>
</tr>
<tr>
<td align="center">FF</td>
<td align="center">E0</td>
<td colspan="2" align="center">00 10</td>
<td colspan="5" align="center">4A 46 49 46 00</td>
<td colspan="2" align="center">01 01</td>
<td align="center">10</td>
<td colspan="2" align="center">00 60</td>
<td colspan="2" align="center">00 60</td>
</tr>
<tr>
<td align="center">00</td>
<td align="center">00</td>
<td colspan="14" align="center"></td>
</tr>
</tbody>
</table>
<h3>Huffman Tables:</h3>
<ul>
<li>Abbreviation &#8211; DHT</li>
<li>Byte Value &#8211; <span style="color:#ffcc66;font-size:100%;">0xFFC4</span></li>
<li>Description &#8211; This segment defines the Huffman tables to be used to decompress the jpeg data. More information about it can be found <a title="Jpeg Huffman Tables" href="http://digitalexploration.wordpress.com/2009/07/29/jpeg-huffman-tables/" target="_blank">here</a>.</li>
<li>Structure -
<ol>
<li>Marker signature (2 bytes)
<ol>
<li><span style="color:#ffcc66;font-size:100%;">0xFFC4</span></li>
</ol>
</li>
<li>Length of data (2 bytes)
<ol>
<li>Remember length of data includes the 2 length bytes</li>
</ol>
<p><strong>Everything after this point is repeated (usually for 4 tables) until the length is exhausted</strong></li>
<li>Table Identifier (1 byte)
<ol>
<li>The 4 high bits determine the class: 0=DC table, 1=Ac table. The 4 low bits specify the table identifier (0,1,2, or 3).</li>
</ol>
</li>
<li>Number of values for each bit length (16 bytes)
<ol>
<li>The position of each byte represents the bit length of the Huffman table and each byte value is the number of values for that bit length. For example, if the data is <span style="color:#ffcc66;font-size:100%;">0&#215;000105 </span>then there will be 0 values with a bit length of 1, 1 value with a bit length of 2 and 5 values with a bit length of 3.</li>
</ol>
</li>
<li>Actual values  (*Sum  of values above* bytes)
<ol>
<li>Length is equal to the sum of the values in # 4. So for the example above <span style="color:#ffcc66;font-size:100%;">0&#215;000105</span> would translate to 6 bytes.</li>
</ol>
</li>
</ol>
</li>
<table border="1">
<tbody>
<tr style="background-color:grey;color:black;">
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>A</td>
<td>B</td>
<td>C</td>
<td>D</td>
<td>E</td>
<td>F</td>
</tr>
<tr>
<td align="center">FF</td>
<td align="center">C4</td>
<td colspan="2" align="center">#2</td>
<td align="center">#3</td>
<td colspan="11" align="center">#4</td>
</tr>
<tr>
<td colspan="5" align="center">#4 cont.</td>
<td colspan="11" align="center">#5 &#8230;</td>
</tr>
</tbody>
</table>
</ul>
<h3>Quantization Table:</h3>
<ul>
<li>Abbreviation &#8211; DQT (Define Quantization Table)</li>
<li>Byte Value &#8211; <span style="color:#ffcc66;font-size:100%;">0xFFDB</span></li>
<li>Description &#8211; This segment defines the Quantization tables to be used. All of the Quantization tables are defined in this one definition, there can&#8217;t me multiple DQT headers. The data almost always consists of 3 tables which are made up of 1 table information byte and 64 table elements. The byte length of the table elements are determined by the information byte.</li>
<li>Structure -
<ol>
<li>Marker signature (2 bytes)
<ol>
<li><span style="color:#ffcc66;font-size:100%;">0xFFDB</span></li>
</ol>
</li>
<li>Length of data (2 bytes)
<ol>
<li>Remember length of data includes the 2 length bytes</li>
</ol>
<p><strong>Everything after this point is repeated (usually for 3 tables) until the length is exhausted</strong></li>
<li>Table Identifier (1 byte)
<ol>
<li>The 4 high bits determine how many bytes make up an element
<ol>
<li>0 = 1 byte per element so 64 bytes per table</li>
<li> 1 = 2 bytes per element, so 128 bytes per table</li>
</ol>
</li>
<li>The 4 low bits are the number associated with the table (0-3)</li>
</ol>
</li>
<li>The 64 elements of the table (64 or 128 bytes depending on #3)
<ol>
<li>Each element is used to fill in the table in a zig-zag formation as shown below.</li>
</ol>
</li>
</ol>
</li>
</ul>
<table border="1">
<tbody>
<tr style="background-color:grey;color:black;">
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>A</td>
<td>B</td>
<td>C</td>
<td>D</td>
<td>E</td>
<td>F</td>
</tr>
<tr>
<td align="center">FF</td>
<td align="center">DB</td>
<td colspan="2" align="center">#2</td>
<td align="center">#3</td>
<td colspan="11" align="center">#4</td>
</tr>
<tr>
<td colspan="16" align="center">#4 cont.</td>
</tr>
</tbody>
</table>
<p>Table Layout</p>
<table border="1">
<tbody>
<tr>
<td>0</td>
<td>2</td>
<td>5</td>
<td>9</td>
<td>14</td>
<td>20</td>
<td>27</td>
<td>35</td>
</tr>
<tr>
<td>1</td>
<td>4</td>
<td>8</td>
<td>13</td>
<td>19</td>
<td>26</td>
<td>34</td>
<td>42</td>
</tr>
<tr>
<td>3</td>
<td>7</td>
<td>12</td>
<td>18</td>
<td>25</td>
<td>33</td>
<td>41</td>
<td>48</td>
</tr>
<tr>
<td>6</td>
<td>11</td>
<td>17</td>
<td>24</td>
<td>32</td>
<td>40</td>
<td>47</td>
<td>53</td>
</tr>
<tr>
<td>10</td>
<td>16</td>
<td>23</td>
<td>31</td>
<td>39</td>
<td>46</td>
<td>52</td>
<td>57</td>
</tr>
<tr>
<td>15</td>
<td>22</td>
<td>30</td>
<td>38</td>
<td>45</td>
<td>51</td>
<td>56</td>
<td>60</td>
</tr>
<tr>
<td>21</td>
<td>29</td>
<td>37</td>
<td>44</td>
<td>50</td>
<td>55</td>
<td>59</td>
<td>62</td>
</tr>
<tr>
<td>28</td>
<td>36</td>
<td>43</td>
<td>49</td>
<td>54</td>
<td>58</td>
<td>61</td>
<td>63</td>
</tr>
</tbody>
</table>
<h3>Restart Interval:</h3>
<ul>
<li>Abbreviation &#8211; DRI  (Define Restart Interval)</li>
<li>Byte Value &#8211; <span style="color:#ffcc66;font-size:100%;">0xFFDD</span></li>
<li>Description &#8211; This market specifies the number of MCUs (Minimum Coded Units) between restart markers. If the value is 0 then there are no reset markers. This can be used for parallel processing of jpeg images.</li>
<li>Structure -
<ol>
<li>Marker signature (2 bytes)
<ol>
<li><span style="color:#ffcc66;font-size:100%;">0xFFDD</span></li>
</ol>
</li>
<li>Length of data (2 bytes)
<ol>
<li>Always = 4 (Remember length of data includes the 2 length bytes)</li>
</ol>
</li>
<li>Number of coded units between restart markers</li>
</ol>
</li>
</ul>
<table border="1">
<tbody>
<tr style="background-color:grey;color:black;">
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>A</td>
<td>B</td>
<td>C</td>
<td>D</td>
<td>E</td>
<td>F</td>
</tr>
<tr>
<td align="center">FF</td>
<td align="center">DD</td>
<td colspan="2" align="center">#2</td>
<td colspan="4" align="center">#3</td>
</tr>
</tbody>
</table>
<h3>Frame Header:</h3>
<ul>
<li>Abbreviation &#8211; SOF  (Start of Frame)</li>
<li>Byte Value &#8211; <span style="color:#ffcc66;font-size:100%;">0xFFC0</span></li>
<li>Description &#8211; This is a marker to indicate the start of a frame. There can be only one.</li>
<li>Structure -
<ol>
<li>Marker signature (2 bytes)
<ol>
<li><span style="color:#ffcc66;font-size:100%;">0xFFC0</span></li>
</ol>
</li>
<li>Length of data (2 bytes)
<ol>
<li>Remember length of data includes the 2 length bytes</li>
</ol>
</li>
<li>Data precision (1 byte)
<ol>
<li>8 or 12 bits</li>
</ol>
</li>
<li>Image Height in pixels (2 bytes)</li>
<li>Image Width in pixels (2 bytes)</li>
<li>Number of components (1 byte)</li>
<li>Component 1-N
<ol>
<li>Component ID (1 byte)
<ol>
<li>JPEG defines it as 0-255 but JFIF restricts it to 1,2,3</li>
</ol>
</li>
<li>Horizontal Sampling (high 4 bits)
<ol>
<li>Values: 1,2,3, or 4</li>
</ol>
</li>
<li>Vertical Sampling (low 4 bits)
<ol>
<li>Values: 1,2,3, or 4</li>
</ol>
</li>
<li>Quantization table ID (1 byte)
<ol>
<li>Can be 0,1,2,3</li>
</ol>
</li>
</ol>
</li>
</ol>
</li>
</ul>
<h3>Comment:</h3>
<ul>
<li>Abbreviation -COM  (Comment)</li>
<li>Byte Value &#8211; <span style="color:#ffcc66;font-size:100%;">0xFFFE</span></li>
<li>Description &#8211; This is a marker to indicate a comment.</li>
<li>Structure -
<ol>
<li>Marker signature (2 bytes)
<ol>
<li><span style="color:#ffcc66;font-size:100%;">0xFFFE</span></li>
</ol>
</li>
<li>Length of data (2 bytes)
<ol>
<li>Remember length of data includes the 2 length bytes</li>
</ol>
</li>
<li>Comments
<ol>
<li>Any information, often application specific</li>
</ol>
</li>
</ol>
</li>
</ul>
<h3>Start of Scan:</h3>
<ul>
<li>Abbreviation -SOS  (Start of Scan)</li>
<li>Byte Value &#8211; <span style="color:#ffcc66;font-size:100%;">0xFFDA</span></li>
<li>Description &#8211; This is the last header before the compressed image data.</li>
<li>Structure -
<ol>
<li>Marker signature (2 bytes)
<ol>
<li><span style="color:#ffcc66;font-size:100%;">0xFFDA</span></li>
</ol>
</li>
<li>Length of data (2 bytes)
<ol>
<li>Remember length of data includes the 2 length bytes</li>
</ol>
</li>
<li>Component Count (1 byte)</li>
<li>Component Descriptions (2 * Component Count) (Note, the order these components appear is the order in the compressed data)
<ol>
<li>Component ID (1 byte)</li>
<li>DC Huffman Table ID (4 high bits)</li>
<li>AC Huffman Table ID (4 low bits)</li>
</ol>
</li>
<li>Spectral Selection Start (1 byte)
<ol>
<li>0-63</li>
</ol>
</li>
<li>Spectral Selection End (1 byte)
<ol>
<li>0-63</li>
</ol>
</li>
<li>&#8216;Successive Approximation&#8217; (two 4-bit fields, each in the range 0-13)</li>
</ol>
</li>
</ul>
<h3>End Of Image:</h3>
<ul>
<li>Abbreviation &#8211; EOI  (End of Image)</li>
<li>Byte Value &#8211; <span style="color:#ffcc66;font-size:100%;">0xFFD9</span></li>
<li>Description &#8211; This is a marker to indicate the end of an image. These are the last two bytes of a file.</li>
<li>Structure -
<ol>
<li>Marker signature (2 bytes)
<ol>
<li><span style="color:#ffcc66;font-size:100%;">0xFFD9</span></li>
</ol>
</li>
</ol>
</li>
</ul>
<p>That took forever to make but now that it&#8217;s over I hope to get more motivated and post something more interesting.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/digitalexploration.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/digitalexploration.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/digitalexploration.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/digitalexploration.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/digitalexploration.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/digitalexploration.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/digitalexploration.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/digitalexploration.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/digitalexploration.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/digitalexploration.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/digitalexploration.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/digitalexploration.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/digitalexploration.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/digitalexploration.wordpress.com/90/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=90&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://digitalexploration.wordpress.com/2009/11/17/jpeg-header-definitions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/584ce0d6557f45b6de3729c4493ebd65?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">imbeyondboredom</media:title>
		</media:content>
	</item>
		<item>
		<title>JPEG &#8211; Old issues, new problems</title>
		<link>http://digitalexploration.wordpress.com/2009/08/06/jpeg-old-issues-new-problems/</link>
		<comments>http://digitalexploration.wordpress.com/2009/08/06/jpeg-old-issues-new-problems/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 14:33:57 +0000</pubDate>
		<dc:creator>imbeyondboredom</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Jpeg]]></category>
		<category><![CDATA[Malicious]]></category>

		<guid isPermaLink="false">http://digitalexploration.wordpress.com/?p=106</guid>
		<description><![CDATA[This post will actually be rather short as I just wanted to post something interesting I&#8217;ve stumbled upon. According to this site, there is a new vulnerability in the Java Web Start application which comes standard with java. Java Web Start For those of you that do now know what Java Web Start applications are, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=106&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This post will actually be rather short as I just wanted to post something interesting I&#8217;ve stumbled upon. According to this <a title="Java Vulnerability" href="http://packetstormsecurity.org/0908-advisories/ZDI-09-050.txt" target="_blank">site</a>, there is a new vulnerability in the Java Web Start application which comes standard with java.</p>
<h3>Java Web Start</h3>
<p>For those of you that do now know what Java Web Start applications are, check out this <a title="Java Explanation" href="http://www.java.com/en/download/faq/5000070700.xml" target="_blank">site</a> and this <a title="Wikipedia explanation" href="http://en.wikipedia.org/wiki/Java_Web_Start" target="_blank">one</a>. For simplicities sake, Java Web Start enables java applications to be downloaded and run off of someone&#8217;s computer via a website or an application. Basically the program is downloaded, cached and run to allow for developers to easily provide java applications to the end user. These programs are usually in a sandbox so they cant access any files that aren&#8217;t specific to the application. However, if the java application is signed, the application can access anything that the user has permission on.</p>
<h3>Problem</h3>
<p>According to the Zero Day Initiative, Java Web Start suffers from a &#8220;JPEG Header Parsing Integer Overflow&#8221;. From my <a title="Jpeg Exploitation" href="http://digitalexploration.wordpress.com/2009/07/16/jpeg-exploitation/" target="_blank">post</a> earlier, we learned how the Microsoft Library was vulnerable to the manipulation of JPEG headers. From the title, and subsequent description of this zero day vulnerability, I think it is pretty obvious that Java Web Start suffers from the exact same problem.</p>
<h3>Attack Vector</h3>
<p>Really the best avenue would be to attack a signed application so you could get the best access to the computer. Fortunately, (or unfortunately depending how you look at it?) Java Web Start is still relatively new I believe (or just not that widely used) so there aren&#8217;t too many programs to attack. That being said, if you find an application that is vulnerable, then that is a good way in as most people don&#8217;t even think of things like buffer overflows in java applications. As I&#8217;ve said before, the java application can be run through a web site link, so if you click on something that launches java, maybe be a little careful.</p>
<h3>Impact</h3>
<p>Java Web Start is not widely used and it is probably not that common to have the program load too many images. That being said, the application can be launched through a web link, so it&#8217;s possible that someone could start posting links to a java application and tricking uses to click on them. If an attacker has access to your local network he can substitute legitimate pictures traveling across the net with malicious ones so there is some risk there. If you have an anti-virus then you might get lucky depending on how the application works. If the jpeg is stored in an intermediate location then the anti-virus will probably detect and delete it. If the jpeg is loaded directly to memory then you&#8217;ll be out of luck.</p>
<h3>Mitigation</h3>
<p>Java has issued a patch to solve this issue, so update java. <a title="Java Vuln Discussion" href="http://sunsolve.sun.com/search/document.do?assetkey=1-66-263428-1" target="_blank">Read More</a></p>
<h3>Lessons Learned</h3>
<p>Old vulnerabilities are still relevant today and can show up in trusted products. With my previous post one could easily craft something to break the java application and, with a little work and knowledge, one could easily form a respectable exploit. So don&#8217;t run random programs you don&#8217;t need and always keep your software up to date. Also, as a developer, always do sanity checking on lengths. These problems are all avoidable if you just practice good programming practices.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/digitalexploration.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/digitalexploration.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/digitalexploration.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/digitalexploration.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/digitalexploration.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/digitalexploration.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/digitalexploration.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/digitalexploration.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/digitalexploration.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/digitalexploration.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/digitalexploration.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/digitalexploration.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/digitalexploration.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/digitalexploration.wordpress.com/106/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=106&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://digitalexploration.wordpress.com/2009/08/06/jpeg-old-issues-new-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/584ce0d6557f45b6de3729c4493ebd65?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">imbeyondboredom</media:title>
		</media:content>
	</item>
		<item>
		<title>Jpeg Huffman Tables</title>
		<link>http://digitalexploration.wordpress.com/2009/07/29/jpeg-huffman-tables/</link>
		<comments>http://digitalexploration.wordpress.com/2009/07/29/jpeg-huffman-tables/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 19:58:21 +0000</pubDate>
		<dc:creator>imbeyondboredom</dc:creator>
				<category><![CDATA[Forensics]]></category>
		<category><![CDATA[Jpeg]]></category>

		<guid isPermaLink="false">http://digitalexploration.wordpress.com/?p=53</guid>
		<description><![CDATA[Background Most jpeg files use Huffman encoding for compression. This compression relies on Huffman trees which are stored in the jpeg file and are used to decompress the image data.  (If you are not familiar with Huffman encoding there is a good explanation on wikipedia) Therefore, we need to understand the structure of the Huffman [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=53&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Background</h2>
<p>Most jpeg files use Huffman encoding for compression. This compression relies on Huffman trees which are stored in the jpeg file and are used to decompress the image data.  (If you are not familiar with Huffman encoding there is a good explanation on <a title="Huffman Encoding" href="http://en.wikipedia.org/wiki/Huffman_coding" target="_blank">wikipedia</a>) Therefore, we need to understand the structure of the Huffman trees in the jpeg file in order to extract them and then extract the image.</p>
<h2>Huffman Table Marker Structure</h2>
<p>Ok, so to start we need to identify the marker that identifies a Huffman Table (<span style="color:#FFCC66;font-size:100%;">0xFFC4</span>). The structure of this marker is as follows:</p>
<ol>
<li>Marker signature (2 bytes)
<ol>
<li><span style="color:#ffcc66;font-size:100%;">0xFFC4</span></li>
</ol>
</li>
<li>Length of data (2 bytes)
<ol>
<li>Remember length of data includes the 2 length bytes</li>
</ol>
<p><strong>Everything after this point is repeated (usually for 4 tables) until the length is exhausted</strong></li>
<li>Table Identifier (1 byte)
<ol>
<li>The 4 high bits determine the class: 0=DC table, 1=Ac table. The 4 low bits specify the table identifier (0,1,2, or 3).</li>
</ol>
</li>
<li>Number of values for each bit length (16 bytes)
<ol>
<li>The position of each byte represents the bit length of the Huffman table and each byte value is the number of values for that bit length. For example, if the data is <span style="color:#ffcc66;font-size:100%;">0&#215;000105 </span>then there will be 0 values with a bit length of 1, 1 value with a bit length of 2 and 5 values with a bit length of 3.</li>
</ol>
</li>
<li>Actual values  (*Sum  of values above* bytes)
<ol>
<li>Length is equal to the sum of the values in # 4. So for the example above <span style="color:#ffcc66;font-size:100%;">0&#215;000105</span> would translate to 6 bytes.</li>
</ol>
</li>
</ol>
<table border="1">
<tbody>
<tr style="background-color:grey;color:black;">
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>A</td>
<td>B</td>
<td>C</td>
<td>D</td>
<td>E</td>
<td>F</td>
</tr>
<tr>
<td align="center">FF</td>
<td align="center">C4</td>
<td colspan="2" align="center">#2</td>
<td align="center">#3</td>
<td colspan="11" align="center">#4</td>
</tr>
<tr>
<td colspan="5" align="center">#4 cont.</td>
<td colspan="11" align="center">#5 &#8230;</td>
</tr>
</tbody>
</table>
<h2>Parsing the table</h2>
<p>Now, assuming you&#8217;ve read the Wikipedia entry, you know that we need to figure out how to translate the bit length to the tree aspect. While I may provide code for that later, I figured I would at least step through how to create it. I should also add that, from what I&#8217;ve read, most jpeg decoders do not create an actual tree, but instead create look-up tables to improve performance. I am going to try to explain it in tree form and then you may convert it as you see necessary.  Let us assume that the first section of our data is as follows:</p>
<table border="1">
<tbody>
<tr style="background-color:grey;color:black;">
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>A</td>
<td>B</td>
<td>C</td>
<td>D</td>
<td>E</td>
<td>F</td>
</tr>
<tr>
<td><span style="color:#99cc00;">ff</span></td>
<td><span style="color:#99cc00;">c4</span></td>
<td><span style="color:#00ccff;">01</span></td>
<td><span style="color:#00ccff;">a2</span></td>
<td><span style="color:#cc99ff;">00</span></td>
<td><span style="color:#ff99cc;">00</span></td>
<td><span style="color:#ff99cc;">01</span></td>
<td><span style="color:#ff99cc;">05</span></td>
<td><span style="color:#ff99cc;">01</span></td>
<td><span style="color:#ff99cc;">01</span></td>
<td><span style="color:#ff99cc;">01</span></td>
<td><span style="color:#ff99cc;">01</span></td>
<td><span style="color:#ff99cc;">01</span></td>
<td><span style="color:#ff99cc;">01</span></td>
<td><span style="color:#ff99cc;">00</span></td>
<td><span style="color:#ff99cc;">00</span></td>
</tr>
<tr>
<td><span style="color:#ff99cc;">00</span></td>
<td><span style="color:#ff99cc;">00</span></td>
<td><span style="color:#ff99cc;">00</span></td>
<td><span style="color:#ff99cc;">00</span></td>
<td><span style="color:#ff99cc;">00</span></td>
<td>00</td>
<td>01</td>
<td>02</td>
<td>03</td>
<td>04</td>
<td>05</td>
<td>06</td>
<td>07</td>
<td>08</td>
<td>09</td>
<td>0a</td>
</tr>
<tr>
<td>0b</td>
</tr>
</tbody>
</table>
<p>The first two bytes are the marker data so we skip those (in green). The next two bytes are the length (in blue) so we read them in and since the length is <span style="color:#FFCC66;font-size:100%;">0x01A2</span> then the actual length is 418 bytes (only one table is shown so this length does not match correctly!). After the length there is the table identifier <span style="color:#ffcc66;font-size:100%;">0&#215;00 </span>(purple) which means that this is a DC table (higher 4 bits = 0) and the identifier is 0. After that we have the values for each bit length (pink).  Now each byte in this segment represents a level in the tree. The value of each byte represents the number of endpoints on that level. So for example, the first 3 bytes are  <span style="color:#ffcc66;font-size:100%;">0&#215;000105</span>. This means that on the first level of the tree there are no endpoints, on the second level there is one endpoint, and on the third level there are five endpoints. All of the values are filled in from the left side of the tree to the right and, therefore, the tree would look something like the following (please excuse by horrible ascii drawing):</p>
<pre>           -------   Level 1
          |       |
       ----       -----   Level 2
      x    |     |     |
          ---   ---   -----   Level 3
         x   x  x  x  x   |
                         ---</pre>
<p>Now that the structure is understood, how are those values filled in? Well the sum of all of the endpoints will equal how many additional bytes we need to read in. So we add up the level bytes and get: 00+01+05+01+01+01+01+01+01+00+00+00+00+00+00+00 = 12 bytes. So we read in the next 12 bytes, fill in the empty spots as we go, and we get the following (only partial tree shown):</p>
<pre>           -------   Level 1
          |       |
       ----       -----   Level 2
      0    |     |     |
          ---   ---   -----   Level 3
         1   2  3  4  5   |
                         ---</pre>
<h2>Parsing the Data</h2>
<p>So now that the tables are filled in, how are they used? Well, the tree is traversed by a zero bit going down the left side of a decision and a 1 bit navigating the right. For example, the byte <span style="color:#ffcc66;font-size:100%;">0&#215;00</span> (shown above on the far left of the tree as just 0) is represented by the bits: 00 whereas the byte <span style="color:#ffcc66;font-size:100%;">0&#215;01</span> ( shown above as 1) would be the bits 010. So if you had a byte of compressed data like <span style="color:#ffcc66;font-size:100%;">0xA5</span> then that would translate to 1010 0101 in binary. If we use the above Huffman Tree then we will find that would translate to 101 = <span style="color:#ffcc66;font-size:100%;">0&#215;04</span>, 00 = <span style="color:#ffcc66;font-size:100%;">0&#215;00</span>, 101 = <span style="color:#ffcc66;font-size:100%;">0&#215;04</span>. At this point we have successfully decoded 3 bytes from the original one.</p>
<h2>Caveats</h2>
<p>Now I should mention a few things. First, the decoded bytes will not always rest on a byte boundary. In the bigger Huffman tables you can and will have bit strings with 16bits in length. You ask why it makes sense to represent a single byte with 16bits? Well the Huffman encoding relies on some byte values appearing much more common than others. This means that bytes that are extremely rare can be represented in 16bits and it will still equal a considerable compression since the extremely common bytes are only represented in a small number of bits. This also means that parsing data will be difficult as one cannot just read in a pre-determined number of bytes and parse them. This will also stand as a huge barrier to overcome once we look into carving jpeg pieces that have become fragmented. But I&#8217;ll deal with that when the time comes.</p>
<p>Second, the compressed data in jpeg files isn&#8217;t just raw pixel data compressed using Huffman encoding. Instead of simply the red/blue/green values for a pixel, the compressed data contains a lot of information that allows the decode to create those values. I&#8217;ll go into all of that in a later post, but with the above knowledge you will be able to successfully parse through the compressed data once you know how it&#8217;s structured.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/digitalexploration.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/digitalexploration.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/digitalexploration.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/digitalexploration.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/digitalexploration.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/digitalexploration.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/digitalexploration.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/digitalexploration.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/digitalexploration.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/digitalexploration.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/digitalexploration.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/digitalexploration.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/digitalexploration.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/digitalexploration.wordpress.com/53/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=53&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://digitalexploration.wordpress.com/2009/07/29/jpeg-huffman-tables/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/584ce0d6557f45b6de3729c4493ebd65?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">imbeyondboredom</media:title>
		</media:content>
	</item>
		<item>
		<title>Jpeg Exploitation</title>
		<link>http://digitalexploration.wordpress.com/2009/07/16/jpeg-exploitation/</link>
		<comments>http://digitalexploration.wordpress.com/2009/07/16/jpeg-exploitation/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 15:45:08 +0000</pubDate>
		<dc:creator>imbeyondboredom</dc:creator>
				<category><![CDATA[Forensics]]></category>
		<category><![CDATA[Jpeg]]></category>
		<category><![CDATA[Malicious]]></category>

		<guid isPermaLink="false">http://digitalexploration.wordpress.com/?p=39</guid>
		<description><![CDATA[Background As promised, I said I would look into why the jpeg format was exploitable for a period to the windows OS and various applications. The answer I came up with was actually pretty interesting. (If you aren&#8217;t already familiar with the jpeg format then take a look at my previous post first) Intro As [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=39&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Background</h2>
<p>As promised, I said I would look into why the jpeg format was exploitable for a period to the windows OS and various applications. The answer I came up with was actually pretty interesting. (If you aren&#8217;t already familiar with the jpeg format then take a look at my previous <a title="Jpeg Basic Format" href="http://digitalexploration.wordpress.com/2009/07/15/jpeg-basic-format/" target="_blank">post</a> first)</p>
<h2>Intro</h2>
<p>As you can probably remember (or just read), jpeg files consist of various markers before the actual image data. The interesting thing about the markers happen to deal the length of the data which is represented as 2 bytes. The important part is that these two bytes include the length bytes (for example a length of 6 is actually 2 length bytes and 4 bytes of data). This means that technically a value of 1 or 0 for the length is invalid and parsing should be halted immediately.</p>
<h2>Explanation and Impact</h2>
<p>So, the problem arose when the library to decode jpeg files would read in a length and then subtract 2 from it to know how many more bytes to read in. This would result in a -2 or -1 for the length. Well it turns out that, before any verification, the length was transformed into a 32bit integer. This would make -2 into approximately 4GB and the subsequent read would trigger a buffer overflow.</p>
<p>So how severe was the problem? Well it turns out that the vulnerable code resided in the GDI, or Graphics Device Interface, which is basically a library that Microsoft provided for many programs to use. Therefore, all of the programs relying on this library were vulnerable and Microsoft had a fairly long list of programs using it.</p>
<h2>Detection and Removal</h2>
<p>Since the exploit was proven, how would one detect this attack? Well it seems fairly straightforward once you know all of the facts. Basically one would just need to parse the markers as usual and attempt to detect a length of less than 2. An example script demonstrating this can be found at the end of this post. If you decide to test this then I should warn you that if you create a jpeg file with a manipulated marker length then it seems all of the common anti-virus seem to detect it. This will probably result in it removing your image and either way it&#8217;s reported in the AV stats&#8230; So I wouldn&#8217;t test it at work (like I did&#8230; that&#8217;s a fun one to explain).</p>
<p>The normal anti-virus removal seems a little harsh as I would be rather upset if my personal images were deleted. Instead, lets investigate if we can remove the infection. For a jpeg, obviously what we truly care about is the ability to show the picture. Since it is possible to exploit any of the headers except for the SOS (Start of Scan-&gt; <span style="color:#FFCC66;font-size:100%;">0xFFDA</span>) marker then it is possible that the compressed image data could be unmodified. So, we need to determine which markers we actually need in order to decompress an image&#8230;they are as follows:</p>
<ul>
<li>The Main Marker (Usually JFIF marker: <span style="color:#FFCC66;font-size:100%;">xFFE0</span>)</li>
<li> Quantization table marker (<span style="color:#FFCC66;font-size:100%;">0xFFDB</span>)</li>
<li>Huffman table markers (<span style="color:#FFCC66;font-size:100%;">FFC4</span>)</li>
<li>Start of frame marker (<span style="color:#FFCC66;font-size:100%;">FFC0</span>)</li>
<li>Start of Scan marker (<span style="color:#FFCC66;font-size:100%;">FFDA</span>)</li>
</ul>
<p>If all of these markers are present and non-malicious then remove the others and save the file. This means that the maliciousness is most likely in the comments section of the jpeg and that is used primarily by image editors like photoshop to store other interesting information about the file.</p>
<p>If any of these markers are NOT present or are malicious, then it becomes tricky. Basically this will probably depend on whether or not the image is a digital camera image or one processed using an image editor. The reason lies in the structure of jpeg compression. I will elaborate further on this in another post, but basically jpeg compression uses binary trees to create shorter bit strings to represent common bytes. This is called Huffman compression and if you don&#8217;t want to wait for my explanation there are plenty on the net. The important fact is that the tree used is then stored in the jpeg file. Since digital cameras do not have a lot of processing power, they often use a hard-coded huffman tree for compression. This results in smaller files but not optimal compression. In contrast, photoshop forms its tables based on the image data. This results in optimal compression, but it uses different trees for each file.</p>
<p>So what does this all mean? Well if you have two images from the same digital camera (and using the same compression settings) then in all likelihood you can use the same tables from the other image to fix the corrupted image. This would actually have a pretty good chance of success but, in all fairness to the AV companies, it would be hard to automate. If I get a chance later, I&#8217;ll try to show an example in a later post. For now, I&#8217;ll leave you with the code I wrote that verifies a jpeg file is not malicious. If you want more information about the vulnerability and its exploitation then have a look <a title="Jpeg Vulnerability PDF" href="www.infosecwriters.com/text_resources/pdf/JPEG.pdf" target="_blank">here</a> as it was an excellent writeup on all of the very technical details about the problem.</p>
<h2>Code</h2>
<p>NOTE: this is not optimized, nor should it be used in production environments. I just wrote this to give you a quick look into how jpeg files are structured and how simple it is to detect a malicious one:</p>
<p>[EDIT] The first program is in perl and I have added a second in C# below it. The C# program does not do as much checking as the perl version, but it will still detect malicious jpeg files.</p>
<pre>use warnings;
my $buffer;
my $numRead;
my %verified;
my %signatures = ( "APPS"=&gt; "^\xFF[\xE0-\xE9]", "QT"=&gt;"^\xFF\xDB", "Huff"=&gt;"^\xFF\xC4","SOF"=&gt;"^\xFF\xC0","SOS"=&gt;"^\xFF\xDA");

#quit if user did not provide a file name or the file doesn't exist
die "You must provide a filename" if(!$ARGV[0] || !(-f $ARGV[0]));

#open the file
open($IN,"&lt;".$ARGV[0]);

#use binary reading
binmode($IN);

#read in the first two bytes
$numRead = read($IN,$buffer,2);

#Die if it doesn't look like a jpeg file
die "File is not a JPEG" if!($buffer =~ /^\xFF\xD8/);

#loop forever
while(1)
{
	$numRead = read($IN,$buffer,4);

	#if there is nothing else to read
	if($numRead &lt; 4)
	{
		#exit the program
		determineSuccess();
		last;
	}

	#exit if we are at the compressed data segment
	if($buffer =~ /^\xFF\xDA/)
	{
		$verified{"SOS"} = 1;
		#exit the program
		determineSuccess();
		last;
	}

	#if the header does not start with \xFF then it's corrupt
	if(!($buffer =~ /^\xFF/))
	{
		print "Could not find marker, file appears corrupt\n";
		last;
	}

	#check to see if we're at a known header
	foreach $key (keys(%signatures))
	{
		if($buffer =~ /$signatures{$key}/)
		{
			$verified{$key} = 1;
		}
	}

	#unpack the length as an unsigned short in big-endian
	$len = unpack("n",substr($buffer,2,2));

	#check to see if the length is bad
	if($len &lt; 2)
	{
		print "Jpeg appears to have malicious content\n";
		last;
	}

	#skip the header data
	$numRead = read($IN,$buffer,$len-2);
}

sub determineSuccess
{
	#Success if all of the necessary segments were found
	if($verified{"APPS"} &amp;&amp; $verified{"QT"} &amp;&amp; $verified{"Huff"} &amp;&amp; $verified{"SOF"} &amp;&amp; $verified{"SOS"})
	{
		print "Verified! - Jpeg appears to have no malicious content\n";
	}
	else
	{
		print "Jpeg appears to be missing a segment(s)\n";
	}
}</pre>
<p>And an example in C#</p>
<pre>using System;
using System.IO;
namespace JpegVerifier
{
	///
	/// This class will verify the jpeg sent to it
	///
	class Class1
	{
		///
		/// The main entry point for the application.
		///
		[STAThread]
		static void Main(string[] args)
		{
			byte[] header;
			int length;
			BinaryReader br;

			if(args.Length &lt; 0)
			{
				return;
			}

			br = new BinaryReader(File.OpenRead(args[0]));

			try
			{
				header = br.ReadBytes(2);

				if(header[0] != '\xff' ||
					header[1] != '\xd8')
				{
					System.Console.WriteLine("File is not a JPG");
					return;
				}

				while(true)
				{
					//if the header is 0xffda then we're at the image data
					header = br.ReadBytes(2);
					if(header[0] == '\xff' &amp;&amp;
						header[1] == '\xda')
					{
						//Assume jpg is well formed
						System.Console.WriteLine("Jpeg File is Clean");
						return;
					}

					//if the header does not start with 0xff then it's not a header
					if(header[0] != '\xff')
					{
						System.Console.WriteLine("Jpeg Marker is corrupt");
						return;
					}

					//assume marker is valid for our purposes
					//read in length
					length = br.ReadUInt16();

					//convert to BigEndian
					length = ((0x00FF &amp; length) &lt;&lt; 8 | (0xFF00 &amp; length) &gt;&gt; 8 );

					if(length &lt; 2)
					{
						System.Console.WriteLine("Jpeg is MALICIOUS!");
						return;
					}

					//skip the marker data
					br.ReadBytes(length-2);
				}
			}
			catch(Exception e)
			{
				System.Console.WriteLine(e.Message);
			}
		}
	}
}</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/digitalexploration.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/digitalexploration.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/digitalexploration.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/digitalexploration.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/digitalexploration.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/digitalexploration.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/digitalexploration.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/digitalexploration.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/digitalexploration.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/digitalexploration.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/digitalexploration.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/digitalexploration.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/digitalexploration.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/digitalexploration.wordpress.com/39/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=39&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://digitalexploration.wordpress.com/2009/07/16/jpeg-exploitation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/584ce0d6557f45b6de3729c4493ebd65?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">imbeyondboredom</media:title>
		</media:content>
	</item>
		<item>
		<title>Jpeg Basic Format</title>
		<link>http://digitalexploration.wordpress.com/2009/07/15/jpeg-basic-format/</link>
		<comments>http://digitalexploration.wordpress.com/2009/07/15/jpeg-basic-format/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 15:42:46 +0000</pubDate>
		<dc:creator>imbeyondboredom</dc:creator>
				<category><![CDATA[Forensics]]></category>
		<category><![CDATA[Jpeg]]></category>

		<guid isPermaLink="false">http://digitalexploration.wordpress.com/?p=13</guid>
		<description><![CDATA[I&#8217;m back at work and as such&#8230; have time to post? Now where to begin? For reference, everything beginning with 0x means that it is in Hexadecimal format. If you need to know what that is I can suggest some reading or a quick Google search will fill you in. I have made progress on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=13&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m back at work and as such&#8230; have time to post? Now where to begin? For reference, everything beginning with <span style="color:#FFCC66;font-size:100%;">0x</span> means that it is in Hexadecimal format. If you need to know what that is I can suggest some reading or a quick Google search will fill you in.</p>
<p>I have made progress on breaking down the basic JPEG format. Through reading the ISO standard and various sites online I have found that a JPEG file normally consists of the hex values <span style="color:#FFCC66;font-size:100%;">0xFFD8</span> followed by a number of JPEG &#8220;markers&#8221; until the marker <span style="color:#FFCC66;font-size:100%;"><code>0xFFD9</code></span> which indicates an end of file.</p>
<p>Basically a JPEG Marker is the bytes <span style="color:#FFCC66;font-size:100%;">0xFF</span> followed by a marker byte which signifies a different piece of the JPEG file. In addition, each JPEG Marker (except for one) follows this pattern&#8230;</p>
<ol>
<li>Header
<ol>
<li>2 bytes beginning with <span style="color:#FFCC66;font-size:100%;">0xFF</span></li>
</ol>
</li>
<li>Length of data (including length bytes)
<ol>
<li>2 bytes</li>
</ol>
</li>
<li>Data
<ol>
<li>[Length] bytes</li>
</ol>
</li>
</ol>
<p>The view on disk would be as follows:</p>
<table border="1">
<tbody>
<tr style="background-color:grey;color:black;">
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>A</td>
<td>B</td>
<td>C</td>
<td>D</td>
<td>E</td>
<td>F</td>
</tr>
<tr>
<td colspan="2">#1</td>
<td colspan="2">#2</td>
<td colspan="12" align="center">#3 &#8230;</td>
</tr>
</tbody>
</table>
<p>The data can then follow a variety of formats depending on what the marker was. For a breakdown of the major ones visit this <a title="site" href="http://www.obrador.com/essentialjpeg/headerinfo.htm" target="_blank">site</a> but there are also a variety of proprietary ones that can often tell you a lot about the tools used to create the pictures. For example.. many pictures will actually have the photoshop version and date the picture was edited. Have you ever wondered if a photo was edited? A quick look at the picture&#8217;s internals can often tell you a lot about the picture.</p>
<p>So, what is the one that breaks the pattern? <span style="color:#FFCC66;font-size:100%;">0xFFDA</span> or the &#8220;Start of Scan&#8221; marker which is the beginning of the actual compressed image data. I will dive into much detail on this later but to start this field should contain no other markers except for possibly <span style="color:#FFCC66;font-size:100%;">0xFFD[1-7]</span> or the reset marker. (This marker will make it possible to simultaneously decompress different parts of the image) Finally, the image is finished with the bytes <span style="color:#FFCC66;font-size:100%;">0xFFD9</span>. An overall view would be as follows:</p>
<table border="1">
<tbody>
<tr style="background-color:grey;color:black;">
<th width="6.25%">0</th>
<th width="6.25%">1</th>
<th width="6.25%">2</th>
<th width="6.25%">3</th>
<th width="6.25%">4</th>
<th width="6.25%">5</th>
<th width="6.25%">6</th>
<th width="6.25%">7</th>
<th width="6.25%">8</th>
<th width="6.25%">9</th>
<th width="6.25%">A</th>
<th width="6.25%">B</th>
<th width="6.25%">C</th>
<th width="6.25%">D</th>
<th width="6.25%">E</th>
<th width="6.25%">F</th>
</tr>
<tr>
<td>0xFF</td>
<td>0xD8</td>
<td colspan="14" align="center">Various Markers</td>
</tr>
<tr>
<td colspan="16" align="center">Various Markers cont&#8230;</td>
</tr>
<tr>
<td colspan="16" align="center">Various Markers cont&#8230;</td>
</tr>
<tr>
<td>0xFF</td>
<td>0xDA</td>
<td colspan="14" align="center">Compressed Data</td>
</tr>
<tr>
<td colspan="16" align="center">Compressed Data cont&#8230;</td>
</tr>
<tr>
<td colspan="16" align="center">Compressed Data cont&#8230;</td>
</tr>
<tr>
<td colspan="14" align="center">Compressed Data cont&#8230;</td>
<td>0xFF</td>
<td>0xD9</td>
</tr>
</tbody>
</table>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/digitalexploration.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/digitalexploration.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/digitalexploration.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/digitalexploration.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/digitalexploration.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/digitalexploration.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/digitalexploration.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/digitalexploration.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/digitalexploration.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/digitalexploration.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/digitalexploration.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/digitalexploration.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/digitalexploration.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/digitalexploration.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=13&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://digitalexploration.wordpress.com/2009/07/15/jpeg-basic-format/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/584ce0d6557f45b6de3729c4493ebd65?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">imbeyondboredom</media:title>
		</media:content>
	</item>
		<item>
		<title>Jpeg Beginnings</title>
		<link>http://digitalexploration.wordpress.com/2009/07/03/jpeg-beginnings/</link>
		<comments>http://digitalexploration.wordpress.com/2009/07/03/jpeg-beginnings/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 19:39:19 +0000</pubDate>
		<dc:creator>imbeyondboredom</dc:creator>
				<category><![CDATA[Forensics]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Jpeg]]></category>

		<guid isPermaLink="false">http://digitalexploration.wordpress.com/?p=7</guid>
		<description><![CDATA[Wow that was a while with no updates, I guess that&#8217;s what happens when you start a blog, a new job and move. I&#8217;ve decided that the first stop is an intricate look into the JPEG file format. I&#8217;ve always wanted to write a JPEG parser/carver and this seems like a good time to start. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=7&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Wow that was a while with no updates, I guess that&#8217;s what happens when you start a blog, a new job and move.</p>
<p>I&#8217;ve decided that the first stop is an intricate look into the JPEG file format. I&#8217;ve always wanted to write a JPEG parser/carver and this seems like a good time to start. For those of you that are thoroughly confused as to why this would be helpful I know numerous people who have &#8220;lost&#8221; pictures on corrupt hard drives in the past and while i have researched some tools there aren&#8217;t any decent tools that I have found.</p>
<p>So what do I hope to cover in the next few days/weeks/months?</p>
<ul>
<li>Examine and understand the jpeg file format</li>
<li>Create a basic jpeg carver/analyzer capable of jpeg analysis and basic error correction (or at least  understandable error messages)</li>
<li>Look into jpeg exploitations of the past (<a href="http://www.microsoft.com/technet/security/bulletin/ms04-028.mspx">Example</a>)</li>
</ul>
<p>Also, since there are different variations of jpeg file formats, I am going to analyze formats more common with digital cameras since that is my primary interest.</p>
<p>Lastly, once we start diving into things I will try my best to provide the pictures I am working with and examples. If you can&#8217;t follow what I&#8217;m doing then what&#8217;s the point right?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/digitalexploration.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/digitalexploration.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/digitalexploration.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/digitalexploration.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/digitalexploration.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/digitalexploration.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/digitalexploration.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/digitalexploration.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/digitalexploration.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/digitalexploration.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/digitalexploration.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/digitalexploration.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/digitalexploration.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/digitalexploration.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=7&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://digitalexploration.wordpress.com/2009/07/03/jpeg-beginnings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/584ce0d6557f45b6de3729c4493ebd65?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">imbeyondboredom</media:title>
		</media:content>
	</item>
		<item>
		<title>Introductions</title>
		<link>http://digitalexploration.wordpress.com/2009/05/21/hello-world/</link>
		<comments>http://digitalexploration.wordpress.com/2009/05/21/hello-world/#comments</comments>
		<pubDate>Thu, 21 May 2009 16:10:45 +0000</pubDate>
		<dc:creator>imbeyondboredom</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Welcome to Digital Exploration<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=1&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Welcome to Digital Exploration,</p>
<p>I am going to be using this blog to document my explorations into everything digital.  In particular, I hope to post some useful information regarding digital exploitation and forensics. With the combination of the two I hope to run into some interesting examples and hopefully we can both learn something</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/digitalexploration.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/digitalexploration.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/digitalexploration.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/digitalexploration.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/digitalexploration.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/digitalexploration.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/digitalexploration.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/digitalexploration.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/digitalexploration.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/digitalexploration.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/digitalexploration.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/digitalexploration.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/digitalexploration.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/digitalexploration.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=digitalexploration.wordpress.com&amp;blog=7845337&amp;post=1&amp;subd=digitalexploration&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://digitalexploration.wordpress.com/2009/05/21/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/584ce0d6557f45b6de3729c4493ebd65?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">imbeyondboredom</media:title>
		</media:content>
	</item>
	</channel>
</rss>
