0

I have this part of code:

   #include <stdio.h>
   #include <stdlib.h>

   int main(void)
   {
       FILE * fp;
       char * line = NULL;
       size_t len = 0;
       ssize_t read;

       fp = fopen("./file_commands", "r");
       if (fp == NULL)
           exit(EXIT_FAILURE);

       while ((read = getline(&line, &len, fp)) != -1) {
        //   printf("%s", line);
       system(line);
       }

       fclose(fp);
       if (line)
           free(line);
       exit(EXIT_SUCCESS);
   }

In file_commands i want to put and be able to run a bash script like the following: \x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80 what is the best way to do this?

1
  • hm. i want to put and be able to run a bash script like the following: \x31\xc0\x50\x68\x6e\x2f\x73\x68\x68 the hexdump isn't bash script. Commented Dec 24, 2015 at 8:21

1 Answer 1

4

There's no reason to go line by line like that. There are actually several things you're doing wrong that are causing this code to fail, but the most important is that going line by line through the bash file does not conserve resources.

In fact it wastes resources. Every time you make a call to system your OS creates a new process for the execution of the command. You would actually save system resources if you just did this

int result = system("./file_commands");

instead of trying to execute every line of that file in a separate call to system.

Sign up to request clarification or add additional context in comments.

4 Comments

That is not the point.. What i want is to run the above hexdump (as jm666 mentioned) from file as it would have been run if it was inside stack
@J.S ok I think I understand. That is tricky...maybe impossible. It also seems silly. Did you compile a bunch of programs and paste the output to lines all in the same file?
I am just trying to make the same hexdump which i would put into the stack to execute normally, to make it run from console like if i am using it system (line)
@J.S that is simply not how system works. In fact, that is really not how C works. For one thing, system always creates a new process, which is sandboxed from your old process, i.e. it will never run in the current stack. There really isn't a way to do what you're trying to do. I suggest you find another route of attack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.