Create executable, automountable filesystem in a file, with password!

dd if=/dev/zero of=T bs=1024 count=10240;mkfs.ext3 -q T;E=$(echo 'read O;mount -o loop,offset=$O F /mnt;'|base64|tr -d '\n');echo "E=\$(echo $E|base64 -d);eval \$E;exit;">F;cat <(dd if=/dev/zero bs=$(echo 9191-$(stat -c%s F)|bc) count=1) <(cat T;rm T)>>F
This is just a proof of concept: A FILE WHICH CAN AUTOMOUNT ITSELF through a SIMPLY ENCODED script. It takes advantage of the OFFSET option of mount, and uses it as a password (see that 9191? just change it to something similar, around 9k). It works fine, mounts, gets modified, updated, and can be moved by just copying it. USAGE: SEE SAMPLE OUTPUT The file is composed of three parts: a) The legible script (about 242 bytes) b) A random text fill to reach the OFFSET size (equals PASSWORD minus 242) c) The actual filesystem Logically, (a)+(b) = PASSWORD, that means OFFSET, and mount uses that option. PLEASE NOTE: THIS IS NOT AN ENCRYPTED FILESYSTEM. To improve it, it can be mounted with a better encryption script and used with encfs or cryptfs. The idea was just to test the concept... with one line :) It applies the original idea of http://www.commandlinefu.com/commands/view/7382/command-for-john-cons for encrypting the file. The embedded bash script can be grown, of course, and the offset recalculation goes fine. I have my own version with bash --init-file to startup a bashrc with a well-defined environment, aliases, variables.
Sample Output
Use at your own risk.

# dd if=/dev/zero of=T bs=1024 count=10240;mkfs.ext3 -q T;E=$(echo 'read O;mount -o loop,offset=$O F /mnt;'|base64|tr -d '\n');  echo "E=\$(echo $E|base64 -d);eval \$E;exit;">F;cat <(dd if=/dev/zero bs=$(echo 9191-$(stat -c%s F)|bc) count=1) <(cat T;rm    T)>>F
10240+0 records in
10240+0 records out
10485760 bytes (10 MB) copied, 0.127043 s, 82.5 MB/s
T is not a block special device.
Proceed anyway? (y,n) y
1+0 records in
1+0 records out
9104 bytes (9.1 kB) copied, 0.000116352 s, 78.2 MB/s

(before executing next, make sure /mnt is empty and has nothing mounted, otherwise you can lose files)

# bash F
9191
(I entered 9191, is the password, can be modified in the script)

# l /mnt/
drwx------ 2 root root 12K Jan 31 02:33 lost+found/

# df -h
Filesystem        Size  Used Avail Use% Mounted on
...
/dev/loop0        9.7M  1.1M  8.1M  12% /mnt

# mount
...
/root/F on /mnt type ext3 (rw,relatime,data=ordered)

# umount /mnt 

# head -1 F
E=$(echo cmVhZCBPO21vdW50IC1vIGxvb3Asb2Zmc2V0PSRPIEYgL21udDsK|base64 -d);eval $E;exit;

# E=$(echo cmVhZCBPO21vdW50IC1vIGxvb3Asb2Zmc2V0PSRPIEYgL21udDsK|base64 -d);echo $E
(changed eval x echo)
read O;mount -o loop,offset=$O F /mnt;

What Others Think

doesnt seem to work on ubunbu, bash F just hangs
chrismccoy · 297 weeks and 4 days ago
Chrismccoy: It's probably not hanging, but asking you for the offset. If not, just follow the procedure (sample output) to confirm it generated the executable base64 encoder header correctly. I'ts really simple...
rodolfoap · 297 weeks and 4 days ago
(executable base64 encodeD header)
rodolfoap · 297 weeks and 4 days ago
head -1 F shows the exact same as yours, then the next part root@sorry:/home/chris# read O;mount -o loop,offset=$O F /mnt; mount: invalid offset '' specified
chrismccoy · 297 weeks and 3 days ago
nm got it working ;)
chrismccoy · 297 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: