<?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/"
	>

<channel>
	<title>wally's blog &#187; Linux</title>
	<atom:link href="http://matt.matzi.org.uk/tag/linux/feed/" rel="self" type="application/rss+xml" />
	<link>http://matt.matzi.org.uk</link>
	<description>Delving deep into the mind of me</description>
	<lastBuildDate>Thu, 19 Aug 2010 10:48:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0-beta1</generator>
		<item>
		<title>Reconstructing heavily damaged hard drives</title>
		<link>http://matt.matzi.org.uk/2008/07/03/reconstructing-heavily-damaged-hard-drives/</link>
		<comments>http://matt.matzi.org.uk/2008/07/03/reconstructing-heavily-damaged-hard-drives/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 11:22:09 +0000</pubDate>
		<dc:creator>wally</dc:creator>
				<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Sys Admin]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[forensics]]></category>
		<category><![CDATA[helix]]></category>
		<category><![CDATA[recovery]]></category>

		<guid isPermaLink="false">http://matt.matzi.org.uk/?p=46</guid>
		<description><![CDATA[Recover data even when an NTFS (or other) won&#8217;t mount due to partial hard drive failure. This was born when someone brought me a near dead hard drive (a serious number of read errors, so bad that nothing could mount or fix the filesystem), asking if I could recover any data. Now obviously (as almost [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Recover data even when an NTFS (or other) won&#8217;t mount due to partial hard drive failure.</strong></p>
<p>This was born when someone brought me a near dead hard drive (a serious number of read errors, so bad that nothing could mount or fix the filesystem), asking if I could recover any data.</p>
<p>Now obviously (as almost any geek would know), the answer of course is a very likely yes. There are many ways of recovering data. One such way (which I performed) is using <a href="http://linux.die.net/man/1/foremost">Foremost</a> to find files based on their headers and structures. While this technique works really quite well, it does miss a lot of files, fragment others up, leave bits out and generally not retrieve any metadata (such as filenames etc).</p>
<p>This makes Matt mad.  No filenames == days of renaming files.</p>
<p>So I booted up <a href="http://www.e-fense.com/helix/">Helix</a>, created a quick image of the drive to a 500GB external drive, and tried running <a href="http://www.sleuthkit.org/">Autopsy </a>(the GUI of Sleuthkit).  This is where things got interesting.</p>
<p>I say interesting, because Sleuthkit <em>couldn&#8217;t</em> read the filesystem.  But it <em>could</em> retrieve the inodes, and the metadata along with them.  And it could accordingly retrieve the data content of (some) files.</p>
<p>Observing this, I realized there was a high probability that I could somehow <em>use</em> Sleuthkit&#8217;s command line tools to retrieve the files which were not on bad clusters and recover the filenames from the inode. As it turns out, this wasn&#8217;t such a bad idea!</p>
<p>There are 3 tools which proved useful:</p>
<ul>
<li>ils</li>
<li>ffind</li>
<li>icat</li>
</ul>
<p><em>ils</em> &#8220;lists inode information&#8221; from the image, <em>ffind</em> &#8220;finds the name of the file or directory using the given inode&#8221; and <em>icat</em> &#8220;outputs the content of the file based on it&#8217;s inode number&#8221;. Using these three tools and a bit of bash, we can grab a list of inodes, get the filename from the metadata, create the directory structure beneath it, extract the file content, move on to the next.</p>
<p>So for this task I knocked up the following (really ugly, potentially unsafe) script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="bash"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #000000; font-weight: bold;">for</span> inode <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>inodes<span style="color: #7a0874; font-weight: bold;">&#41;</span> ; <span style="color: #000000; font-weight: bold;">do</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">/</span>KNOPPIX<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>sleuthkit<span style="color: #000000;">-2.09</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ffind <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>hda1 <span style="color: #007800;">$inode</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> -eq <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;INODE: $inode&quot;</span>
	<span style="color: #007800;">INODEDIR=</span><span style="color: #000000; font-weight: bold;">`/</span>KNOPPIX<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>sleuthkit<span style="color: #000000;">-2.09</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ffind <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>hda1 <span style="color: #007800;">$inode</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
	<span style="color: #007800;">REALDIR=</span><span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>out<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;$INODEDIR&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
	<span style="color: #007800;">FILENAME=</span><span style="color: #ff0000;">&quot;/mnt/out$INODEDIR&quot;</span>
	<span style="color: #c20cb9; font-weight: bold;">mkdir</span> -p <span style="color: #ff0000;">&quot;$REALDIR&quot;</span>
&nbsp;
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;FILENAME: $FILENAME&quot;</span>
	<span style="color: #000000; font-weight: bold;">/</span>KNOPPIX<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>sleuthkit<span style="color: #000000;">-2.09</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>icat <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>hda1 <span style="color: #007800;">$inode</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #ff0000;">&quot;$FILENAME&quot;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">du</span> <span style="color: #ff0000;">&quot;$FILENAME&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $1}'</span><span style="color: #000000; font-weight: bold;">`</span> == <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
	<span style="color: #000000; font-weight: bold;">then</span>
		<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #ff0000;">&quot;$FILENAME&quot;</span>
		<span style="color: #c20cb9; font-weight: bold;">mkdir</span> -p <span style="color: #ff0000;">&quot;$FILENAME&quot;</span>
	<span style="color: #000000; font-weight: bold;">fi</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p>Really, I do warn you, <strong>take serious care running this</strong>!</p>
<p>It needs a lot of work, but enough is there for it to function.  It reads a file of inode numbers (one per line) and uses <em>ffind</em> to get the filename.  We extract the path, attempt to create it, output the file content and (this is important), take a <em><strong>wild guess</strong></em> at if the inode was a directory. Please note this is wildly inaccurate and needs serious rethinking! Currently we look at the file size, and assume directories alone use 1 byte.</p>
<p>We can populate a file with inode numbers like so:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">ils -a <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>hda1 <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F <span style="color: #ff0000;">'|'</span> <span style="color: #ff0000;">'{print $1}'</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>inodes</pre></div></div>

<p>(Users of Helix will need to use the full pathname to <em>ils</em> as in the above script).</p>
<p>At some point (no garuntees when) I&#8217;ll tidy up the script and make it more bullet proof. In the meantime, I hope this saves some data!</p>
<p><strong>Remember</strong>: No matter how much data you have, it&#8217;s always better to have 2 hard drives of half the size, mirrored than it is to have one large expensive drive. They <strong><em>will</em></strong> die unexpectedly! When you next buy a bigger hard drive, consider this: 1x500GB drive will loose you 500GB of data. 2x250GB will 99.9% probability loose you nothing. So if you&#8217;re on a tight budget, buy twice smaller. If you&#8217;ve a lot of money, buy twice big.</p>
<p>Oh, and <em><strong>always</strong></em> make regular backups.  Cheap USB drives are good for this!</p>
]]></content:encoded>
			<wfw:commentRss>http://matt.matzi.org.uk/2008/07/03/reconstructing-heavily-damaged-hard-drives/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
