Ad
How Can I Execute Terminal Commands From A C Program?
I am creating a reverse shell in C (just for practice) and I don't know how to execute terminal commands from my C program. How should I go about doing this? (I am on a POSIX system) (I would like to get the output)
Ad
Answer
how to execute terminal commands from my C program ... (I would like to get the output)
Look at popen
Example
#include <stdio.h>
int main(int argc, char ** argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <command>\n", *argv);
return -1;
}
FILE * fp = popen(argv[1], "r");
if (fp != 0) {
char s[64];
while (fgets(s, sizeof(s), fp) != NULL)
fputs(s, stdout);
pclose(fp);
}
return 0;
}
Compilation and executions:
[email protected]:/tmp$ gcc -Wall o.c
[email protected]:/tmp$ ./a.out date
samedi 11 avril 2020, 17:58:45 (UTC+0200)
[email protected]:/tmp$ a.out "date | wc"
1 6 42
[email protected]:/tmp$ ./a.out "cat o.c"
#include <stdio.h>
int main(int argc, char ** argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <command>\n", *argv);
return -1;
}
FILE * fp = popen(argv[1], "r");
if (fp != 0) {
char s[64];
while (fgets(s, sizeof(s), fp) != NULL)
fputs(s, stdout);
pclose(fp);
}
return 0;
}
[email protected]:/tmp$
Ad
source: stackoverflow.com
Related Questions
- → OctoberCMS Backend Loging Hash Error
- → "failed to open stream" error when executing "migrate:make"
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Create plugin that makes objects from model in back-end
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → How to install console for plugin development in October CMS
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → How to update data attribute on Ajax complete
- → October CMS - Conditionally Load a Different Page
Ad