#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Bash script to c conversion :)


    Hello, looking for your help on the following code conversion. Basically I try to make c run bash script commands. It did work for me with echo commands and launching of programs but in the following code, something is wrong with the written code. I get some comments about adding "("..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SHELLSCRIPT "\
    #/bin/bash \n\
    \"while true; do\"\n\
    \"curl -u a:b  -s https://updates.opendns.com/nic/update | grep -vE "^good"\"\n\
    \"sleep 180\"\n\
    \"done\"\n\
    "
    
    int main()
    {
    
        system(SHELLSCRIPT);
        return 0;
    }
    Your help will be very appreciated.

    Thanks.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,384
    Rep Power
    1871
    What is the point of this?

    There is no utility in just embedding the shell script as a string in a C program, if all you're going to do is call system().

    Nor is there any point at all 'for performance', since you sleep for 3 minutes at a time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    it is going to be


    it is going to be an IP updater app, that sends the current IP every 3 minutes. I have the main app to run it with

    This is the way I'm comfortable with, as I know and have written it already in bash and it runs. Just need to fix the syntax errors.

    The problem is just that the syntax is different, once it's bash in c.

    However if you do have the knowledge of how I do this in C, it will be very appreciated.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,384
    Rep Power
    1871
    So why don't you save your script as an executable bash file and be done with it?

    Here is a foretaste of what it would look like in C. The handy 1-line pipeline becomes a mess of 20+ lines of code (and this is paraphrased, without error checks).
    Code:
    #include<stdio.h>
    int main()
    {
      while ( 1 ) {
        int p[2];
        pipe(p);
        pid_t grepper = fork();
        if ( grepper == 0 ) {
          char *argv[] = { "grep", "-vE", "^good", NULL };
          close(p[1]);
          dup2(p[0],0); // make stdin come from the pipe
          execvp(argv[0],argv);
        } else if ( grepper > 0 ) {
          pid_t curler = fork();
          if ( curler == 0 ) {
            char *argv[] = { "curl", "", "", "", NULL };  // complete
            close(p[0]);
            dup2(p[1],1); // make stdout go to pipe
            execvp(argv[0],argv);
          } else {
          }
        }
        sleep(180);
      }
      return 0;
    }
    Not only is it harder to write, it's also harder to maintain, and you now have at least TWO files to worry about (the source code and the executable).

    You're not buying any security with it either, at least not from anyone with any technical competence.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0
    Originally Posted by salem
    So why don't you save your script as an executable bash file and be done with it?

    Here is a foretaste of what it would look like in C. The handy 1-line pipeline becomes a mess of 20+ lines of code (and this is paraphrased, without error checks).
    Code:
    #include<stdio.h>
    int main()
    {
      while ( 1 ) {
        int p[2];
        pipe(p);
        pid_t grepper = fork();
        if ( grepper == 0 ) {
          char *argv[] = { "grep", "-vE", "^good", NULL };
          close(p[1]);
          dup2(p[0],0); // make stdin come from the pipe
          execvp(argv[0],argv);
        } else if ( grepper > 0 ) {
          pid_t curler = fork();
          if ( curler == 0 ) {
            char *argv[] = { "curl", "", "", "", NULL };  // complete
            close(p[0]);
            dup2(p[1],1); // make stdout go to pipe
            execvp(argv[0],argv);
          } else {
          }
        }
        sleep(180);
      }
      return 0;
    }
    Not only is it harder to write, it's also harder to maintain, and you now have at least TWO files to worry about (the source code and the executable).

    You're not buying any security with it either, at least not from anyone with any technical competence.
    That is awesome! Thank you so much.
    I will try it soon, hopefully I could figure out how to run it.

    I actually forgot to mention the reason I didn't want the executable sh is, I didn't want anyone to see the file since it holds the user's credentials. So I want everyone to run it but don't want to be seen and with sh as far as I noticed, it isn't possible, when I limit the viewing..

    I'll let you know, wish I could write programs like that so quick
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,384
    Rep Power
    1871
    > I actually forgot to mention the reason I didn't want the executable sh is, I didn't want anyone to see the file since it holds the user's credentials.
    But hiding them in an executable doesn't protect you from anyone who knows what they're doing.

    All they have to do is "strings myprog", and all the string constants stored in your program are instantly revealed.
    Code:
    $ cat baz.c
    #include<stdio.h>
    char *password = "OpenSesame";
    int main()
    {
      char  buff[100];
      printf("Enter password\n");
      scanf("%s",buff);
      return 0;
    }
    $ gcc baz.c
    $ strings a.out 
    /lib64/ld-linux-x86-64.so.2
    ZEg6
    __gmon_start__
    libc.so.6
    __isoc99_scanf
    puts
    __stack_chk_fail
    __libc_start_main
    GLIBC_2.7
    GLIBC_2.4
    GLIBC_2.2.5
    fff.
    l$ L
    t$(L
    |$0H
    OpenSesame
    Enter password
    ;*3$"
    > So I want everyone to run it but don't want to be seen and with sh as far as I noticed, it isn't possible, when I limit the viewing..
    If you do something like
    chmod 711 myscript.sh
    then it will have permissions "-rwx--x--x", meaning that only you will have full access to the content of the file, but everyone else will be able to execute it.
    Last edited by salem; August 13th, 2013 at 12:20 AM.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Found something cool :)


    With SHC, I encrypted my shell scripts.
    Basically it turns them into c and make them executable.

    I think the final problem for now is how to run the whole thing through the gui with double clicking which a bit frustrating since the project is done. :)

    Managed to run the intro bash menu I made, with a desktop file, but as I choose something from it to go to a different bash script, it disappears.

    Needless to say, it works fine when running from terminal.

    Dag Google for the answer and couldn't find it, hopefully in here I will..

    Thanks :)

IMN logo majestic logo threadwatch logo seochat tools logo