LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 10-02-2014, 10:43 AM   #1
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
BASH Anti shell-shock wrapper.


For those of you like me who really don't like the idea of bash parsing environment variables to define functions, I've written a small wrapper that you may like to try. I originally wrote it to work around a problem invoking bash as sh via 'su' but it wasn't that hard to add a bit of code that will remove anything that looks like a function definition from the environment as well.

Disclaimer: I'm not much of a programmer, so provided as-is, use at your own risk and all that. And for gods sake test it thoroughly before you go replacing /bin/sh with it.

This should protect anything that calls /bin/sh such as programs that call popen() or system(), but won't do anything for things that invoke /bin/bash directly.

UPDATE2: Seems I broke the original function of my wrapper when adding the shell-shock protections, so I've removed it from this post as it doesn't give you anything over the one on post #3. The one on post #3 does work however, so if you want to use a wrapper to protect yourself then that's the one to use. Sorry for the confusion.

Last edited by GazL; 10-02-2014 at 04:21 PM.
 
Old 10-02-2014, 11:25 AM   #2
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
It's a cool idea. However, as you said /bin/bash can still be called directly, which is risk that is not fixed by this. I think the better option is to just disable the feature in bash directly.
 
Old 10-02-2014, 12:07 PM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Original Poster
Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
/bin/bash can be wrapped in a similar manner. The problem with trying to patch bash itself at present is that its a moving target with all the frantic activity going on at present. Seemed much easier to just wrap it while things settle down (especially as I was already using the wrapper to fix the su issue).

I've attached a simplified version of the bash-wrapper that can be used for /bin/bash itself, without all the stuff to do with fixing 'su'.

Last edited by GazL; 12-11-2014 at 11:43 AM.
 
Old 10-02-2014, 01:46 PM   #4
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Yeah, you might be right, it looks like they just released another patch.
 
Old 10-06-2014, 04:30 PM   #5
jrifkin
LQ Newbie
 
Registered: Oct 2014
Posts: 1

Rep: Reputation: Disabled
We independently developed a bash wrapper that is very similar. The main difference is rather than remove the environment variables, we 'clean' them. Cleaning is done by replacing any occurance of '()' with blanks, replaced characters beyond ASCII 127 with blanks, and setting the LANG and LC_ALL variables to C if they exist.

I've pasted all 81 lines of it below:

Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#define ORIGINAL_BASH "/bin/bash-broken"

/*  cbash:  Bash wrapper to defeat ShellShock exploit 

    Before calling bash, filter all strings '()' from environmental variables.  
    Expoits use these to cloak arbitrary commands to the bash evaluator

*/

int main(int argc, char *argv[], char *envp[])
{
    char *pos, **e;

    /*  If --version option sent, print cbash version and then continue  */
    e = argv;
    while (*e) {
        if (strcmp(*e,"--version")==0) {
                /*  Print version info  */
                printf("-----------------------------------------\n");
                printf("cbash, version 1.1\n");
                printf("Bash wrapper to defeat ShellShock exploit\n");
                printf("Original bash executable: '%s'\n", ORIGINAL_BASH);
                printf("-----------------------------------------\n");
                break;
        }
        *e++;
    }

    /*  Verify that original bash exists and is executable */
    if ( access( ORIGINAL_BASH, F_OK) == -1 ) {
        printf("ERROR:  Original bash file '%s' does not exist\n", ORIGINAL_BASH);
        exit(1);
    }
    if ( access( ORIGINAL_BASH, X_OK) == -1 ) {
        printf("ERROR:  Original bash file '%s' is not executable\n", ORIGINAL_BASH);
        exit(1);
    }


    /*  Clean the environment stored in *envp[]  */
    e = envp;
    while (*e) {

        /*  Overwrite () in environment variable to defeat shellshock hack  */
        pos = strstr(*e, "()");
        if (pos) {
            *pos++ = ' ';
            *pos   = ' ';
        }

        /*  Replace non-ascii characters with blanks  */
        /*  NOTE:  Older compilers (?) require (unsigned char) below  */
        pos = *e;
        while (*pos) {
                if ( (unsigned char) *pos>127) *pos = ' ';
                *pos++;
        }

        /*  Replace values of LANG and LC_ALL with "C"  */
        if (strncmp(*e,"LANG=",5)==0) {
                if (strlen(*e)>5) {
                        (*e)[5] = 'C';
                        (*e)[6] = 0;
                }
        } else if (strncmp(*e, "LC_ALL=", 7)==0) {
                if (strlen(*e)>7) {
                        (*e)[7] = 'C';
                        (*e)[8] = 0;
                }
        }
        *e++;
    }

    /*  Exec old bash with a cleaned envionment  */
    execve(ORIGINAL_BASH, argv, envp);
}

Last edited by unSpawn; 10-06-2014 at 05:31 PM. Reason: //Add vBB code tags.
 
Old 10-06-2014, 05:19 PM   #6
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Rep: Reputation: 195Reputation: 195
Not trying to be rude but it would be better if you posted it with [code] tags or on some paste site.

Like this.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Shock- Security Threat abhijeetdutta Linux - Newbie 1 09-25-2014 01:47 PM
LXer: Ubuntu 11.10 without Unity shell shock LXer Syndicated Linux News 0 12-07-2011 02:50 AM
[SOLVED] shell script wrapper for automating rsync someshpr Linux - Newbie 3 06-03-2010 01:31 AM
shell shock wogga Linux - Software 3 05-25-2004 04:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 10:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration