http://qs1969.pair.com?node_id=164891

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Where could I find a perl/cgi script in which I can encript my program. For example I have a myscript.cgi script and I want it unreadable by others, but that script will also run or work fine even if I encript it or unreadable bu others.

Please adviced me where could I fine such script or anyone could give me that script. Thanks

Edit kudra, 2002-05-07 $title =~ s/cripting scipt/crypting script/

  • Comment on Encrypting script (was Encripting scipt)

Replies are listed 'Best First'.
Re: Encripting scipt
by Zaxo (Archbishop) on May 08, 2002 at 03:30 UTC

    I think Acme::Bleach is ideal for your purposes.

    After Compline,
    Zaxo

Re: Encrypting script
by ariels (Curate) on May 08, 2002 at 07:28 UTC

    Amplifying on the previous answers: you can't.

    However, if you can restrict access to the environment in which your scripts run, you can use some external key to decrypt them. Of course, if an attacker can lay hands on that key, you still lose. Think of it as exactly the same as using Acme::Bleach, only keeping decryption knowledge for everything localised at a single place.

    Put the password "<kbd>foobar</kbd>" (without the quotes) in your environment variable <samp>PASSWD</samp> (I told you it was laughably insecure!), and execute this code:

    #!/usr/local/bin/perl use warnings; use strict; use Crypt::CBC; use Crypt::DES; # give compile-time error if uninstalled use Digest::MD5 qw/md5/; my $cr = new Crypt::CBC ({ key => $ENV{PASSWD}, cipher => 'DES', }); $cr->start('decrypting'); eval join '', (map {$cr->crypt(unpack 'u',$_)} <DATA>), $cr->finish; __END__ H4F%N9&]M258,7+K!Y.>&R#R(9V'IHIQ/V38VIC%,U54_2EA%]]#L```` (C4$VS`4H"\T` 8`^Z3::)M9OZC2NJ6!C!."#\1T+4:?`DE M98',<EH%[_U>_#TF-U;OHK<?^0Q'/$EKW,MRB;.Z(A@*7Q4?MCQ=;^!QF/B0 CR/)<\*6UC+0R3[P*L>Y+YF\C6WGV7E["2:=,PKE^;*[?M`@` (TCPFK5[11M8`
    Uses Digest::MD5, Crypt::DES and Crypt::CBC. DES encryption really isn't considered particularly secure nowadays, but compared to the security you're getting from the rest of this writeup it's an impregnable fortress.

    Here's a program to "encrypt". Again, put your password in envariable <samp>PASSWD</samp>.

    #!/usr/local/bin/perl use warnings; use strict; use Crypt::CBC; use Crypt::DES; # give compile-time error if uninstalled use Digest::MD5 qw/md5/; die "$0: Gimme a password in environment variable PASSWD\n" unless exists $ENV{PASSWD}; my $cr = new Crypt::CBC ({ key => $ENV{PASSWD}, cipher => 'DES', }); $cr->start('encrypting'); my $hdr = <<'END_HEADER'; #!/usr/local/bin/perl use warnings; use strict; use Crypt::CBC; use Crypt::DES; # give compile-time error if uninstalled use Digest::MD5 qw/md5/; my $cr = new Crypt::CBC ({ key => $ENV{PASSWD}, cipher => 'DES', }); $cr->start('decrypting'); eval join '', (map {$cr->crypt(unpack 'u',$_)} <DATA>), $cr->finis +h; __END__ END_HEADER $hdr =~ s/^ {4}//mg; print $hdr; while (<>) { print pack 'u', $cr->crypt($_); } print pack 'u', $cr->finish;

    Adding encryption for modules is "left as an exercise for the interested reader" (i.e. I'm too indolent to do something so useless).

    Finally, I cannot stress this enough: you get very little security from this sort of thing. If you need security, take a good hard look at what it is you're trying to do. Any encryption (including the superstars like AES, RSA, and any other TLA) is no stronger than the protection of its key. And if you want to run your code, you must provide access to that key.

      Or, the module Filter::CBC appears to be a more solid way of doing exactly this. I've not used it, so I cannot comment on its security...

Re: Encripting scipt
by Molt (Chaplain) on May 08, 2002 at 09:42 UTC

    I'm wondering why your 'myscript.cgi' is readable by others. If it's the fact that people can simply type some URL into their browser and download the source then there's a significant security hole on the server and you should really get it fixed by contacting the server administrator, or getting another server.

    If you're worried about other people logged into the server being able to see it then set the permissions so only the user it runs as can read it. This works far better when you're running on a machine where each script executes as the user who has it in their cgi-bin, or even better a special account for them to run CGIs as.

    If this isn't the case on your server then I'd recommend you get another, more secure, server before playing about with encryption as it's a far better way to guarantee safety.

    Now for the bit with disclaimers- I'm really not recommending this cause of action, I'm only putting it here in case you're determined to follow this course. Giving you enough rope to shoot yourself in the foot, or somesuch.

    If you're just trying to make the script difficult to read for customers etc. to stop them messing with your carefully-crafted code then look at the Obfuscation section of this very site.. many people write 'Obfuscation engines' to hide the meaning of code. This will help to hide the details, but expect a significant performance hit whilst it's decrypted, and make sure you test the encrypted version exceptionally well since it's quite possible it's broken it.

Re: Encripting scipt
by IndyZ (Friar) on May 08, 2002 at 04:56 UTC
    The short answer: You can't.

    Longer answer: You can, but it won't hold up. For your program to continue working, the source still has to be there in some form or another. This different form of your code might (emphasis on "might") be harder for a determined person to read, but if they have enough time they can recover the source or at worst an obfuscated version of the source.

    --
    IndyZ

      For example Acme::Bleach has been suggested above by Zaxo but this is trivial to decrypt (although conceptually brilliant). see also unbleach.pl which was not written by TheDamian

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Encripting scipt
by educated_foo (Vicar) on May 08, 2002 at 06:19 UTC
    You might also try Language::Befunge, though the "encryption" process might be rather time-consuming.

    /s

Re: Encrypting script (was Encripting scipt)
by Kage (Scribe) on Oct 21, 2002 at 10:14 UTC
    Actually, contrary to what everyone says, there is one way to encrypt the entire script, as in making it, from the #!/usr/bin/perl to the very end. It will not hold up well in extensive scripts, or large scripts, but it does work nonetheless.

    I'm not sure if they still offer it, but WorldWideScripts.com makes a program called PerlEncoder. You gotta buy it if you wanna use it unlimited.

    Though, the sad thing is that in scripts, it's nearly impossible to encrypt every single park of your code, unless you use some sort of strong randomizing method, then eval() it.. Though I don't know if eval would work under alot of code..

    My strongest recommendation is just encrypt those things you don't want viewed, or editable, such as your copyright link that's displayed..
    “A script is what you give the actors. A program is what you give the audience.” ~ Larry Wall
Re: Encrypting script (was Encripting scipt)
by Beatnik (Parson) on Dec 22, 2002 at 00:47 UTC
    Well, uhm I more or less wrote Filter::CBC for that purpose... or was it to hide my lack of comments? Actually, Paul Marquess already included a simple encryption source filter with Filter::Util::Call.

    To clear things up, YES it can be done... in fact, YES, it has been done. You don't HAVE to provide the key since a few lines in a BEGIN block can query the user for the passphrase, instantiate a CGI session or whatever you want to do. I did a talk on source filters in general (at YAPC::Eu 2.00.1) which, among other things, explained how Filter::CBC works.

    Greetz
    Beatnik
    ...Perl is like sex: if you're doing it wrong, there's no fun to it.