Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Once while talking with my friend I was trying to joke that we might stand a better chance of completing our project if we just run loads of random programs and expect one of them to solve our problem. To demonstrate that, I wrote this "proof of concept":

while true; do

dd if=/dev/urandom of=pliczek count=1
chmod +x pliczek
./pliczek

done

To my horror, when I ran this loop and called ls, I noticed a lot of files with random-looking filenames in my current directory (tested on Fedora Linux on 64-bit x86). Now I can't stop wondering - what could actually have happened?

share|improve this question
2  
Everything a program with 512 bytes could do could have happened. Including creating files, changing your root password or spitting out a tar archive containing your project source code including documentation. –  Marco Jan 12 at 21:34
    
It's unlikely that a valid ELF header happened at least once. Also, one particular action kept happening a lot of the times - creating a file with the random name in the current directory. Thus the question. –  d33tah Jan 12 at 21:36
6  
An ELF header is not requred. Simple ASCII text is sufficient and will be interpreted by the shell. So ":>abc123" or just ">abc123" followed by e.g. U+0004 creates random file names, depending on your shell. –  Marco Jan 12 at 21:43
    
Yeep. So 1/256 of those programs wrote a random file. There's a 1:4294967296 chance of rm *, too. O_o –  goldilocks Jan 12 at 21:58
    
@Marco: definitely makes sense. Could you submit your comment as an answer so I could vote it up and accept it? –  d33tah Jan 12 at 22:12

1 Answer 1

up vote 6 down vote accepted

You are writing 512 bytes into a file and execute it. So the outcome could be anything a program with 512 bytes could possibly do. What that is depends on your machine. But 512 bytes are plenty of instructions, so basically everything could have happened like changing the root password, creating random files or generating a tar archive containing the source code for your project.

An ELF header is not required. Simple ASCII text is sufficient and will be interpreted by the currently running shell (because of the missing shebang line).

A greater than sign (>) redirects the output to a file. Therefore this particular byte is already sufficient to create files. Example:

# this will create a file named abc123 in almost every shell
:>abc123

# another variant
>abc123^D

This demonstrates that there are several ways to create files using a small amount of bytes which makes it more likely to happen.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.