existz / shc-3.8.9

SHC - Bash script compiler
GNU General Public License v2.0
62 stars 29 forks source link

shc-3.8.9 compiled script can't be used in Bourne shell scripts #3

Open suntong opened 10 years ago

suntong commented 10 years ago

To duplicate, compile match.x first:

 make
  make test

Verify it works:

$ ./match.x bash
/usr/bin/bashbug
/bin/bash
/bin/rbash
...

Now put that into shell scripts:

$ echo ./match.x bash > /tmp/test.sh

$ sh -x /tmp/test.sh
+ ./match.x bash
/bin/sh: 0: Can't open bash

Ref: http://sfxpt.wordpress.com/2014/02/23/shc-3-8-9-is-not-usable/

[Update]:

Seems that shc-3.8.9 is hard wired to bash -- everything bash works whereas everything Bourne shell (sh) won't work:

$ cat /tmp/test-sh.sh
#!/bin/sh
./match.x bash

$ /tmp/test-sh.sh
^C

I.e., scripts with Bourne shell (sh) Shebang will hung until break.

However, everything bash works. I.e. both bash -x /tmp/test.sh and the following script work:

$ cat /tmp/test-bash.sh
#!/bin/bash
./match.x bash

Thanks to Jahidul Hamid (neurobin) finding that out.

neurobin commented 9 years ago

Check out my new version 3.9.0 at https://github.com/neurobin/shc I have added [-o filename] option to output to a specific file, and the install have proper man page now.

suntong commented 9 years ago

Jahidul,

Thanks

neurobin commented 9 years ago

It runs good in my case:

   shc -f match
   echo ./match.x bash > /tmp/test.sh
   sudo sh -x /tmp/test.sh

Output:

   + ./match.x bash
   /usr/bin/bashbug
   /usr/bin/checkbashisms
   /usr/bin/dh_bash-completion
   /bin/bash
   /bin/rbash
   [3399] PAUSED... Hit return!
suntong commented 9 years ago

"Now put that into shell scripts"...

neurobin commented 9 years ago

script new:

 #!/bin/sh
 shc -f match
 echo ./match.x bash > /tmp/test.sh
 sudo sh -x /tmp/test.sh

running ./new gives the same output as before

suntong commented 9 years ago

Removing that sudo when invoking /tmp/test.sh? -- Running under sudo is different from running from with shell scripts.

neurobin commented 9 years ago

You will need to make the executable like this at first for that:

 shc -rT -f match
neurobin commented 9 years ago

Any way, I have altered the -T option so that it will be traceable by default. It's now -U which means Untraceable. If not specified it will take traceable=true, and create a binary which can be executed by users other than root.

N.B: -r option is for a release version which will work on other systems as well

suntong commented 9 years ago

Currently that's a problem I am facing. Even C or C++ code compiled with gcc isn't giving me the required permission to run it with normal user.

Look in the closed issue in this project for the (systematic) solution, that doesn't need you to alter the make method.

neurobin commented 9 years ago

That systematic solution, basically changes #if !TRACEABE to #if !defined(TRACEABLE). But a common practice of initializing variables could effectively protect it from misbehaving, and basically that's what I have done. And thus #if !TRACEABLE works fine as it should be. There was no need to use #if !defined for me. And along the way, I altered the logic, so that -T option becomes a default. And if anyone doesn't want it, they can put -U option instead. In that case it will no longer be executed without root.

neurobin commented 9 years ago

And I have released it in launchpad. You can install it by:

  sudo add-apt-repository -y ppa:neurobin/ppa
  sudo apt-get update
  sudo apt-get install shc

All the best...

suntong commented 9 years ago

And I have released it in launchpad.

Can It be used in shell scripts?

neurobin commented 9 years ago

Can It be used in shell script? This seems kinda ambiguous to me, if you could give me an example, I could help you better. Anyway, if you mean using shc in script, then of course, you can. Here's an script named test:

 #!/bin/bash
 shc -f match
 ./match.x

match is another script in the same folder as test. it will compile match and run it.

N.B: As shc is installed, it is global command, you can access it from anywhere. It resides in /usr/bin

suntong commented 9 years ago

Thanks, Please post your output of "sh -x /tmp/test.sh" running as a normal user, after having done echo ./match.x bash > /tmp/test.sh

Thanks again.

neurobin commented 9 years ago

It shows error:

+ ./match.x bash
/bin/sh: 0: Can't open bash

If you run without the sh command (simply /tmp/test.sh), it runs as expected. It seems it is having a hard time interpreting the paths.

suntong commented 9 years ago

If you run without the sh command (simply /tmp/test.sh), it runs as expected.

Oh, good to know. Thanks!!!

neurobin commented 9 years ago

bash /tmp/test.sh works too...

suntong commented 9 years ago

Yep, noticed that as well. Seems that shc-3.8.9 is hard wired against bash. Updating my OP...

neurobin commented 9 years ago

/usr/bin/sh is actually a link to /usr/bin/bash, so practically sh invokes bash by default...