Thread: Changing data inside an image file (.img) without mounting.

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    30

    Changing data inside an image file (.img) without mounting.

    I'm interested in knowing if it's possible to edit a single text file from within an image file. Here's my idea...

    If I make an image of my drive with 'dd'. Then I edit an existing file (the one I want to edit in the .img file), save it, and make another image file with dd. Then by comparing the bytes from each file, find out exactly where I would need to edit the .img file.

    That way if someone wants a copy, I can just run a program that injects, lets say, a serial number linked to that user hidden in the .img file.

    I know I would have to edit the template to include spaces where the serial number will go, so that way I'm just changing existing bits, and not making the .img file bigger.

    Is this possible, or do I actually have to look at programming a way to mount the .img file first before it gets edited every time?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If I make an image of my drive with 'dd'. Then I edit an existing file (the one I want to edit in the .img file), save it, and make another image file with dd. Then by comparing the bytes from each file, find out exactly where I would need to edit the .img file.
    You'll find that mounting most file systems (unless it's something simple like DOS-FAT) write a lot of structural information to the volume, in addition to the file itself.
    Further, depending on your editing program, the new copy of the file could be in a different location on the volume (for safety).

    > Is this possible, or do I actually have to look at programming a way to mount the .img file first before it gets edited every time?
    You can just mount it using loopback
    guide:mount_loopback [Open Source (tm) Guides & Tutorials]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    30
    Quote Originally Posted by Salem View Post
    Further, depending on your editing program, the new copy of the file could be in a different location on the volume (for safety).
    I see this as being a problem. What about if I add a unique entry in place of the desired serial number, make the image file and search the .img file by bit shifting until a matching pattern is found?

    I would like to save mounting the .img file as a final resort if possible.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you're only changing the content of individual sectors or clusters, then you should be OK.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    30
    Quote Originally Posted by Salem View Post
    If you're only changing the content of individual sectors or clusters, then you should be OK.
    Thank you very much for your input. I'm just starting to program in the C language, and wanted to do this project. I was afraid I was setting out to try and do something that couldn't be done (which can stifle the learning process). Once you said it was possible, I knew I could have the mind set to complete it no matter what.

    Surprisingly, this was a very easy thing to do. But thank you for your input!

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Unless the filesystem does any checksuming, like ZFS and BTRFS.

    Basically if we think of the image file as the disk (underlying storage for the filesystem), what you are doing is corrupting it. Some filesystems (not very common right now, but will become more common as time goes on) try to detect and correct corruptions in underlying storage, and editing the image file won't work with those file systems.

    Mounting the filesystem and editing the file through the filesystem is much safer.
    Last edited by cyberfish; 02-10-2014 at 10:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help changing the values inside an array.
    By zbred in forum C Programming
    Replies: 3
    Last Post: 04-10-2011, 11:06 PM
  2. Replies: 2
    Last Post: 04-13-2010, 03:14 PM
  3. Changing header data in binary file
    By neruocomp in forum C Programming
    Replies: 8
    Last Post: 11-14-2008, 07:30 PM
  4. Changing pointer inside function?
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2008, 05:10 AM
  5. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 10:52 PM