• ## Blogroll

• Inferno - International Man Of Mystery. And a very good friend.
• Merlijn - One of my best friends in Holland

• # Recover media files from HTC HSMBackup

Posted by bb on Thursday 9th of October 2014 at 2:42 pm;.
Filed Under IT Tipz & Trix

Due to my M7 going in for repair, I have temporarily got a Samsung as a loan phone. With an upcoming weekend away I wanted to grab my music and stick it on the Samsung so I could listen to it in the car on the long drive.

However, as most things in bb_land, this is not just a case of using the HTC Sync Manager to Restore -> New Phone where it extracts the files into their native format and copies them over. In fact you can’t do it at all.

A quick bit of bumbling around and I find the files stores in AppData/Local/HTC MediaHub/HSM Backup/ in folders helpfully named things like BR_534833355459393033373038 and 35F67E512C69CF39ECF43E90A40F2BDE5A510F2C with useful filenames such as 0C52E903FFEC6CDFBB37A3113D99476A0BB46891. Awesome.

Some quick peeking with a Hex Editor reveals these to be the files I need, just with no extension and a garbled filename. Only wanting to grab the MP3s and not any videos etc I banged up this quick script to be run from the HSM Backup folder

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21   $file) { //read first 3 bytes of file echo "Attempting File :$file \n"; //Show file we are attempting if (substr($file,-1) <> ".") { //Eliminate whining when attempting . and .. in the dir structure$newhandle = fopen($file, "r"); echo "Opened File :$file \n"; //Show we have a handle on the file $contents = fread($newhandle, 3); echo "Read File : $file \n"; //Show we have read from the file //is it ID3 if ($contents == "ID3") { //Yes -> copy to place and rename to MP3 $newfile=$file.".MP3"; copy ($file,$newfile); } fclose($newhandle); } } ?> Running the script copies any files with ID3 as the first 3 bytes of the file to a new file with the same name but an MP3 extension (thus preserving the original for when the M7 comes back from repair to be resynced. After that a simple Windows Search for *.MP3 showed me all my files that I just cut and pasted into the Music folder on the Samsung. Script is easy to change for different formats (i.e if you are looking for a video just change the number of bytes read and the compare constant) Hope it helps someone recover their music if they’ve swapped phones and this is the only backup they have. (3 votes, average: 3.67 out of 5) Loading... # Comments 27 Responses to “Recover media files from HTC HSMBackup” 1. rukfash on December 21st, 2014 2:13 pm Any idea how to get the contacts out of said database files? They seem to be encrypted xml contact files like 123.xml cheers, rg 2. Jim on January 8th, 2015 1:23 am Could this be modified to get all the photos I have stored in the backup? Thanks! Jim 3. Jim on January 8th, 2015 1:33 am Sorry for the double post here but, 1) Must have skipped the part about “Script is easy to change for different format” which answered my last question. For photos or images, what would I have to search for instead of ID3? Better question, what do all images and photos have as the first x number of bytes? 2) How exactly do I run that script thingy? I tried saving it as a .bat file and running it but I don’t think it worked… If you could explain it to me like I’m five years old, that would be great 🙂 4. bb on January 8th, 2015 9:40 am Hi Jim, I would guess that you should look for Exif in the header (bytes 6 – 10) Code would need to be changed to something like (I haven’t tested this so please bear with me) : < ?php$di = new RecursiveDirectoryIterator('path\to\HSMBackup'); foreach (new RecursiveIteratorIterator($di) as$filename => $file) { //read first 10 bytes of file$newhandle = fopen($file, “r”);$contents = fread($newhandle, 10); //is it Exif if (substr($contents,6) == “Exif”) {
//Yes -> copy to place and rename to jpg
$newfile=$file.”.jpg”;
copy ($file,$newfile);
}
fclose($newhandle); } ?> As you can see it’s a php file (you can tell because it starts 5. bb on January 8th, 2015 9:47 am Hi rukfash, Sorry – I really haven’t looked into that. If I do, I’ll let you know. bb 6. Jim on January 8th, 2015 9:39 pm Hello bb, First thanks for getting back so soon! I tried the modified script out but it doesn’t seem to be doing anything. I’ll tell you what I’m doing if it’s something I’m doing wrong. Ok so first I opened up notepad++ and pasted the script in and saved it as a php file. Then I put that file in the folder “HSMBackup” where there are some folders from previous backups with long and weird names. Then I linked the execution of the php file with php.exe (so that way I just had to double click the php file). It runs super fast and the command prompt quickly flashes on screen for a split second then goes away. I search the HSMBackup directory for *.jpg but nothing shows up. In fact I also tried the same method (original script) for mp3 files but that didn’t want to work for me either. Is there something I’m doing wrong? Maybe I’m not running it correctly? I think I used the right program to run it (I downloaded it from php.net). Thanks again for all the time and consideration! Jim 7. bb on January 9th, 2015 8:50 am Morning Jim, Sorry for the delay in getting back to you – timezones I guess 🙂 Looks like you are doing everything right – you have set the$di variable correctly at the beginning of the script?

I would suggest running the script from a command line, that way you have a chance to see any error messages that appear before the window closes.

You can paste the whole script you are using here and any errors and we can have a look at what is wrong.

Please bear in mind that its the weekend here tomorrow (and undoubtedly where you are too!) so I may not get around to looking at it until Monday. Sorry for any inconvenience.

Cheers
bb

8. bb on January 9th, 2015 8:54 am

Also Jim, you might look at adding some debugging steps in the script.

After the //Is it MP3/Exif you could try adding

echo “Filename $file has$contents as first 10 bytes \n”;

This should output each file it is reading and what the first 10 bytes are. That way you can tell if you are correctly parsing the directory and looking at each file and also checking to see if my assumption was correct about Exif being the correct string to be looking for.

HTH

9. dzendzula on January 30th, 2015 6:54 pm

Hi, I found your post and it can help me.

I have a HSMFolder with a backup which was done on a different PC. How can I recover contacts from it? In folders there are XML files with a content like

1f8b 0800 0000 0000 040b 7d92 516f 9b30

10. Jim on February 7th, 2015 7:41 pm

Hey

Sorry for not getting back for the longest time (I’m a full time college student).

When I run it, warnings are repeated for lines 6, 7, and 14.

Line 6 with fopen says “failed to open stream: Bad file descriptor.” And when I make it echo the contents, it shows that nothing is in the contents.

I don’t really know what’s going on. It’s okay if you don’t
have the time to help. Not getting back the photo’s won’t kill me.

Have a good one!
Jim

11. bb on February 10th, 2015 11:10 pm

Hi Jim,

No worries – I understand that there is real life too!

Unfortunately I am away for a week on a course and just logged in to let you know I will look at this when I get back. Hope that is OK.

PS. Can you just let me know what your line 3 looks like (please make sure you don’t post any personal information)?

12. Jim on February 13th, 2015 2:30 am

my line 3 is exactly this (no need to worry about personal stuff in the path):

$di = new RecursiveDirectoryIterator(‘E:\phone backup\HSMBackup’); 13. bb on February 19th, 2015 11:19 am Hi Jim, That should work. Can you open the files in anything else i.e. Notepad++ or just plain old notepad.exe? If it helps you can zip that directory up and host it somewhere, let me know the address and I can download it and test it here? I’ve updated the script a little with some error handling (it used to moan when trying to access certain parts of the directory structure) Maybe that will help a little? 14. bb on February 19th, 2015 11:28 am I think I have it Jim 🙂 I think the comment section mangled the php a little. Can you try replacing all of the quotation marks in the script? I know it seems odd but I think the comments have given us the nice angled quotation marks instead of the plain old vanilla vertical ones. In a plain text editor, just manually delete each quotation mark and replace it with an ordinary one. Worth a shot anyway 🙂 15. Byron on August 28th, 2015 1:28 pm Any chance of showing me how to generate the XML for all data for a back up? I got factory reset yesterday and I’m screwed. Some candid wedding photos, all my business contacts. 16. bb on August 28th, 2015 1:32 pm Hi Byron, If you have all of the data for a backup, can you not just restore the data using the HTC software? Sorry if I have misunderstood! bb 17. SA11S on October 27th, 2015 11:27 am *xml files in backup folder are compressed with zip algoritm 🙂 18. zxero on November 8th, 2015 4:39 am Hello men i got the same warnings that Jim when i run the scripst for images (Jpg) “When I run it, warnings are repeated for lines 6, 7, and 14.” Can you help me check the script for this works properly Thank U Sorry my bad English 19. Prasad on November 17th, 2015 9:25 am How can I extract the contacts alone from the HSMBackup? at present I dont have the HTC phone. Any help on this is appreciated. 20. milan on November 23rd, 2015 11:41 pm SA11S on October 27th, Thank you!! 21. Jason on November 7th, 2016 12:30 pm Hi, Ive been looking all over for this post and bless who ever came up with some sort of solution for this. I have attempted the 2 codes provided and they both worked (dont know how well they did ). I have successfully extracted JPEG & MP3 files. Here is my problem : determining the byte location of each tag type for example, how did one know that exif files are from byte 6 – to ? how will I extract videos and messages ? Any help will be appreciated . Thank you 22. bb on November 7th, 2016 12:58 pm Hi Jason, What you are looking for is part of the file format specification. Imagine you are looking for MP4 files. If you look at the header specification (example found here : http://www.file-recovery.com/mp4-signature-format.htm) then you can use this information to determine a byte pattern to look for. I would suggest searching for ‘ftypmp42’ from byte 04-11 (as I just took a quick video on the HTC and that would appear to be the format that it uses as default). Don’t forget to update the line$newfile=$file.”.MP3″; to$newfile=\$file.”.MP4″;

Hope that helps – and glad my solution is working for you.

bb

23. Jason on November 7th, 2016 1:35 pm

Thank you bb for teaching me something valuable..

I have adjusted my code to search for ‘ftypmp42’:

and the code yields no result.
I manually tested a file within the HSMbackup which i was sure was a video file and opened it with vlc player and it worked. So i really cannot figure out whats wrong here, the code seemed to have worked for both mp3 and jpeg but does not extract and append the extension to the file.

Thank you again

24. Jason on November 7th, 2016 1:52 pm

I fixed the code by reading from 4th to 12th byte location. The rest of code is fine

25. Jason on November 7th, 2016 1:53 pm

Next is attempt contacts,notes and messages

26. bb on November 7th, 2016 1:59 pm

Glad you got it working Jason.

If you are working with a format that isn’t well documented (for example notes, messages etc) – create a couple of sample files (i.e. a couple of sample notes) and then look at them in a hex editor (something like https://mh-nexus.de/en/hxd/) and see if you can spot any common patterns that you can then search for.

Hope it helps and good luck!

bb

27. Sanjay on November 30th, 2016 11:19 am

Jason,
Can you please help figure out a way to extract contacts? I checked the opened the files in hex editor but didn’t find any useful information.

Thanks!