APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Control and protect shell scripts

© Tony Lawrence, aplawrence.com

Shell scripts are simple to create, but if a user has permission to execute the script, they also have permission to read it. There are ways to prevent that:

shc - shell script compiler

Shc is available from Francisco Javier Rosales Garcia and compiles with a simple "make".

It's function really isn't a compiler: instead, it encrypts the shell script, and puts a C wrapper around it that, when run, decrypts the script and calls the requested shell for execution. Therefore, you must have a shebang line (#!/bin/sh or whatever). That also means it isn't limited to shell scripts: you can "compile" a Perl script just as easily.

You can optionally specify an expiration date with a message to display if the program is used after that date, and it also allows passing other arguments to the shell. In its simplest form "shc -f script" will produce "script.x" and "script.x.c"; the "script.x" is your executable and the ".c" is the source code.

In the October 2005 Linux Journal, Nalneesh Guar reports that he was able to break the encryption very easily. The man page seems to agree:

 You can use it if you wish to distribute your scripts
 but don't want them to be easily readable by other people.

Note 'easily'. As the old saying goes, locks keep out honest people. In a typical office, this might be all you need to prevent casual observation or modification of scripts, and could be useful for that.

Password protect

If all you want to do is password protect a script, there is a simple way to that:

The script you want to protect is /usr/bin/yourscript

Create a user "runme" and set your script tobe exaeutable only by that user:

# useradd -m runme
# passwd runme 
Enter new UNIX password:
Retype new UNIX password:

# chown runme /usr/bin/yourscript
# chmod 700 /usr/bin/yourscript

Create the script "/usr/bin/doit" to contain:

su - runme /usr/bin/yourscript

Make "doit" executable

# chmod 755 /usr/bin/doit

An ordinary user can't run or read "yourscript", but when they run "doit", they'll be asked for "runme"'s password. If they enter it correctly. "yourscript" will run.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Control and Protect shell scripts

1 comment

More Articles by

Find me on Google+

Tony Lawrence

Click here to add your comments
- no registration needed!

Tue May 5 06:09:14 2009: 6321   DYFeng

It's helpful.thx

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments

If you want a picture to show with your comment, go get a Gravatar
Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Jump to Comments

Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

This post tagged: