Unable to load image

Any C hackers in here 👀👀

Just curious, and I haven't googled because I already know it's not going to answer the question ....

So say hypothetically I launch an API to run in-line C code from the terminal. It'd just be a basic shell script that has a skeleton of a .c file and injects STDIN straight into the main function. The only header would be <stdio.h> Is it possible to do this securely? Or is the simply allowing arbitrary C even with these restrictions a recipe for pwnage.


So it'd be just

In the endpoint directory:

index.php
run.sh

index.php would receive the string and call shell to remove comments

:a;s@//.*@@;ta; :a;s@/\*@@;ta; :a;s@\*/@@;ta;

then it would just echo out

#include <stdio.h>
int main(void) {

**

<<user input>>

**

return 0; }

into a temporary.c , compile, and then php can capture the output into a variable and with that reply to the API call.

I'm gonna do this anyway on a throwaway server but I was wondering mostly out of curiosity whether even this can be exploited. (i.e. somehow adding more headers and executing a fork bomb or something)

19
Jump in the discussion.

No email address required.

Yes that's just arbitrary code execution. That's the worst thing you can do security wise.

When you compile C code, #include is a preprocessor directive to copy the library's code directly into the file.

Jump in the discussion.

No email address required.

so one could even load a library from inside of main? I thought I was being slick by hardcoding that :marseycry:

Jump in the discussion.

No email address required.

C std has system() that passes arbitrary commands to the shell

Jump in the discussion.

No email address required.

An #include is never necessary, you can always supply your own defines and type and function definitions, and can inject arbitrary code easily. See this stupid example:

int main(void) {
   unsigned char buf[] = { 0x48, 0x89, 0xfe, 0xbf, 0x00, 0x00, 0x00, 0x00, 0xba, 0x10, 0x00, 0x00, 0x00, 0xb8, 0x01, 0x00, 0x00, 0x00, 0x0f, 0x05, 0xc3 };

   int mprotect(void* addr, unsigned long len, int prot);
   mprotect(((void*)(((unsigned long)buf) / 4096 * 4096)), 4096, 7);

   typedef void (*fptr)(const char*);

   fptr my_clandestine_print = (fptr)buf;
   my_clandestine_print(":marseywave:");
}

(Only works on Intel and AMD systems, because the payload is AMD64)

:marseywave2:

Jump in the discussion.

No email address required.

:marseyitneverbegan:

C is so awesome :marseyadmire: 's#(void*|void *)#foid#g'

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.