forked from neurobin/shc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
134 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
*/ | ||
|
||
static const char my_name[] = "shc"; | ||
static const char version[] = "Version 4.0.2"; | ||
static const char version[] = "Version 4.0.3"; | ||
static const char subject[] = "Generic Shell Script Compiler"; | ||
static const char cpright[] = "GNU GPL Version 3"; | ||
static const struct { const char * f, * s, * e; } | ||
|
@@ -147,6 +147,86 @@ static int BUSYBOXON_flag = 0; | |
|
||
static const char * RTC[] = { | ||
"", | ||
"#if HARDENING", | ||
"static const char * shc_x[] = {", | ||
"\"/*\",", | ||
"\" * Copyright 2019 - Intika <[email protected]>\",", | ||
"\" * Replace ******** with secret read from fd 21\",", | ||
"\" * Also change arguments location of sub commands (sh script commands)\",", | ||
"\" * gcc -Wall -fpic -shared -o shc_secret.so shc_secret.c -ldl\",", | ||
"\" */\",", | ||
"\"\",", | ||
"\"#define _GNU_SOURCE /* needed to get RTLD_NEXT defined in dlfcn.h */\",", | ||
"\"#define PLACEHOLDER \\\"********\\\"\",", | ||
"\"#include <dlfcn.h>\",", | ||
"\"#include <stdlib.h>\",", | ||
"\"#include <string.h>\",", | ||
"\"#include <unistd.h>\",", | ||
"\"#include <stdio.h>\",", | ||
"\"#include <signal.h>\",", | ||
"\"\",", | ||
"\"static char secret[128000]; //max size\",", | ||
"\"typedef int (*pfi)(int, char **, char **);\",", | ||
"\"static pfi real_main;\",", | ||
"\"\",", | ||
"\"// copy argv to new location\",", | ||
"\"char **copyargs(int argc, char** argv){\",", | ||
"\" char **newargv = malloc((argc+1)*sizeof(*argv));\",", | ||
"\" char *from,*to;\",", | ||
"\" int i,len;\",", | ||
"\"\",", | ||
"\" for(i = 0; i<argc; i++){\",", | ||
"\" from = argv[i];\",", | ||
"\" len = strlen(from)+1;\",", | ||
"\" to = malloc(len);\",", | ||
"\" memcpy(to,from,len);\",", | ||
"\" // zap old argv space\",", | ||
"\" memset(from,'\\\\0',len);\",", | ||
"\" newargv[i] = to;\",", | ||
"\" argv[i] = 0;\",", | ||
"\" }\",", | ||
"\" newargv[argc] = 0;\",", | ||
"\" return newargv;\",", | ||
"\"}\",", | ||
"\"\",", | ||
"\"static int mymain(int argc, char** argv, char** env) {\",", | ||
"\" //fprintf(stderr, \\\"Inject main argc = %d\\\\n\\\", argc);\",", | ||
"\" return real_main(argc, copyargs(argc,argv), env);\",", | ||
"\"}\",", | ||
"\"\",", | ||
"\"int __libc_start_main(int (*main) (int, char**, char**),\",", | ||
"\" int argc,\",", | ||
"\" char **argv,\",", | ||
"\" void (*init) (void),\",", | ||
"\" void (*fini)(void),\",", | ||
"\" void (*rtld_fini)(void),\",", | ||
"\" void (*stack_end)){\",", | ||
"\" static int (*real___libc_start_main)() = NULL;\",", | ||
"\" int n;\",", | ||
"\"\",", | ||
"\" if (!real___libc_start_main) {\",", | ||
"\" real___libc_start_main = dlsym(RTLD_NEXT, \\\"__libc_start_main\\\");\",", | ||
"\" if (!real___libc_start_main) abort();\",", | ||
"\" }\",", | ||
"\"\",", | ||
"\" n = read(21, secret, sizeof(secret));\",", | ||
"\" if (n > 0) {\",", | ||
"\" int i;\",", | ||
"\"\",", | ||
"\" if (secret[n - 1] == '\\\\n') secret[--n] = '\\\\0';\",", | ||
"\" for (i = 1; i < argc; i++)\",", | ||
"\" if (strcmp(argv[i], PLACEHOLDER) == 0)\",", | ||
"\" argv[i] = secret;\",", | ||
"\" }\",", | ||
"\"\",", | ||
"\" real_main = main;\",", | ||
"\"\",", | ||
"\" return real___libc_start_main(mymain, argc, argv, init, fini, rtld_fini, stack_end);\",", | ||
"\"}\",", | ||
"\"\",", | ||
"0};", | ||
"#endif /* HARDENING */", | ||
"", | ||
"/* rtc.c */", | ||
"", | ||
"#include <sys/stat.h>", | ||
|
@@ -281,26 +361,51 @@ static const char * RTC[] = { | |
"} ", | ||
"/* End Seccomp Sandboxing Init */", | ||
"", | ||
"void shc_x_file() {", | ||
" FILE *fp;", | ||
" int line = 0;", | ||
"", | ||
" if ((fp = fopen(\"/tmp/shc_x.c\", \"w\")) == NULL ) {exit(1); exit(1);}", | ||
" for (line = 0; shc_x[line]; line++) fprintf(fp, \"%s\\n\", shc_x[line]);", | ||
" fflush(fp);fclose(fp);", | ||
"}", | ||
"", | ||
"int make() {", | ||
" char * cc, * cflags, * ldflags;", | ||
" char cmd[4096];", | ||
"", | ||
" cc = getenv(\"CC\");", | ||
" if (!cc) cc = \"cc\";", | ||
"", | ||
" sprintf(cmd, \"%s %s -o %s %s\", cc, \"-Wall -fpic -shared\", \"/tmp/shc_x.so\", \"/tmp/shc_x.c -ldl\");", | ||
" if (system(cmd)) {remove(\"/tmp/shc_x.c\"); return -1;}", | ||
" remove(\"/tmp/shc_x.c\"); return 0;", | ||
"}", | ||
"", | ||
"void arc4_hardrun(void * str, int len) {", | ||
" //Decode locally", | ||
" char tmp2[len];", | ||
" char tmp3[len+1024];", | ||
" memcpy(tmp2, str, len);", | ||
"", | ||
" unsigned char tmp, * ptr = (unsigned char *)tmp2;", | ||
"", | ||
" int lentmp = len;", | ||
" int pid, status;", | ||
" pid = fork();", | ||
"", | ||
"", | ||
" shc_x_file();", | ||
" if (make()) {exit(1);}", | ||
"", | ||
" setenv(\"LD_PRELOAD\",\"/tmp/shc_x.so\",1);", | ||
"", | ||
" if(pid==0) {", | ||
"", | ||
" //Start tracing to protect from dump & trace", | ||
" if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {", | ||
" printf(\"Operation not permitted\\n\");", | ||
" kill(getpid(), SIGKILL);", | ||
" _exit(1);", | ||
" }", | ||
"", | ||
"", | ||
" //Decode Bash", | ||
" while (len > 0) {", | ||
" indx++;", | ||
|
@@ -314,25 +419,29 @@ static const char * RTC[] = { | |
" len--;", | ||
" }", | ||
"", | ||
" //Exec bash script", | ||
" //Do the magic", | ||
" sprintf(tmp3, \"%s %s\", \"'********' 21<<<\", tmp2);", | ||
"", | ||
" //Exec bash script //fork execl with 'sh -c'", | ||
" system(tmp2);", | ||
"", | ||
" //Empty script variable", | ||
" memcpy(tmp2, str, lentmp);", | ||
"", | ||
" //Clean temp", | ||
" remove(\"/tmp/shc_x.so\");", | ||
"", | ||
" //Sinal to detach ptrace", | ||
" ptrace(PTRACE_DETACH, 0, 0, 0);", | ||
" exit(0);", | ||
" }", | ||
" else {", | ||
" wait(&status);", | ||
" }", | ||
" else {wait(&status);}", | ||
"", | ||
" /* Seccomp Sandboxing - Start */", | ||
" seccomp_hardening();", | ||
"", | ||
" exit(0);", | ||
"} ", | ||
"}", | ||
"#endif /* HARDENING */", | ||
"", | ||
"/*", | ||
|