Some deeper mysteries of the the world of hash bangs....

Without cheating (trying to run it) what will this pair of files do under Linux....

Here is a little script we want to be interpreted by the "flashbang" interpreter...
===/home/johnc/foo/hashbang===========================================
#!/home/johnc/foo/flashbang


puts ARGV[0]


======================================================================

So we expect
 ./hashbang "Hello World"
to be formally equivalent to running...

/home/johnc/foo/flashbang ./hashbang "Hello World"

Now assume....

===/home/johnc/foo/flashbang==========================================
#!/bin/sh

echo flashbanger
echo $0
echo "$@"

/usr/bin/ruby "$@"
======================================================================

Let's try...
 /home/johnc/foo/flashbang -e 'puts "Hello World"'

On my system the result is....
flashbanger
/home/johnc/foo/flashbang
puts "Hello World"
Hello World

No surprise...

Now what do you think happens if you say...?

  ./hashbang "Hello World"

Now what do you think happens if you say....?

  /home/johnc/foo/flashbang ./hashbang "Hello World"


Scroll down for the answers...
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
./hashbang "Hello World"
./hashbang: line 4: puts: command not found

Surprise Number 1 : The binfmt look up code looks up flashbang and
finds it's also a #! freaks out and just uses sh as the interpreter...

To check that lets try...
==hashcat=============================================================
#!/home/johnc/foo/flashcat


puts ARGV[0]

echo "Look, we must be running $SHELL"
======================================================================
  and ==flashcat============================================================
#!/bin/cat

echo flashbanger
echo $0
echo "$@"

/usr/bin/ruby "$@"
======================================================================

Then...
----------------------------------------------------------------------
 ./hashcat "Hello World"
./hashcat: line 4: puts: command not found
Look, we must be running /bin/bash
----------------------------------------------------------------------
and...
/home/johnc/foo/flashcat ./hashbang "Hello World"

Comes out as ...
/bin/cat /home/johnc/foo/flashcat ./hashcat "Hello World"

Which results, no surprise as...
----------------------------------------------------------------------
#!/bin/cat

echo flashbanger
echo $0
echo "$@"

/usr/bin/ruby "$@"
#!/home/johnc/foo/flashbang


puts ARGV[0]

/bin/cat: Hello World: No such file or directory
----------------------------------------------------------------------


Now for Surprise #2
----------------------------------------------------------------------
/home/johnc/foo/flashbang ./hashbang "Hello World" | head -20
flashbanger
/home/johnc/foo/flashbang
./hashbang Hello World
flashbanger
/home/johnc/foo/flashbang
./hashbang Hello World
flashbanger
/home/johnc/foo/flashbang
./hashbang Hello World
flashbanger
/home/johnc/foo/flashbang
./hashbang Hello World
flashbanger
/home/johnc/foo/flashbang
./hashbang Hello World
flashbanger
/home/johnc/foo/flashbang
./hashbang Hello World
flashbanger
/home/johnc/foo/flashbang
----------------------------------------------------------------------
WHAT THE HELL!? Why?

Well, deep within ruby/ruby.c is a chunk of code that looks for a #!
on the first line. If it contains the word "ruby" anywhere, it ignores
it. If it doesn't it says, Oh dear. This one wasn't actually for me. I
better be gnice and exec that interpreter and feed it this script...

All in all, you can tell I have a had rather exotic and frustrating
day or three.

John Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : [EMAIL PROTECTED]
New Zealand

Reply via email to