From 6fdde0f5deb55b8c4cfba40fc3291308ca913707 Mon Sep 17 00:00:00 2001 From: chenzhch Date: Fri, 28 Jun 2024 21:52:12 +0800 Subject: [PATCH] Add command parameter --- README.md | 8 ++- shellc.c | 126 +++++++++++++++------------------- "\350\257\264\346\230\216.md" | 9 ++- 3 files changed, 70 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index fe82535..13afb97 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ At present, the program has practical applications in ```AIX```, ```UNIX```, ``` ### User manual - Generate C code command - ```shellc command inputfile [-t] [-s] [-f fix-format] [-e fix-file] [-b 8|16|32|64]``` + ```shellc command inputfile [-t] [-s] [-f fix-format] [-e fix-file] [-p parameter] [-b 8|16|32|64]``` command: Execute script commands, such ```sh```,```perl```,```python```,```node```,```ruby```,```Rscript```,```php```, etc. @@ -37,6 +37,8 @@ At present, the program has practical applications in ```AIX```, ```UNIX```, ``` -e option: Fix parameter 0 value by custom external file. + -p option: Command parameter, such as ```busybox shell``` using by ```shellc busybox example.sh -p sh```. + -b option: Operating system bits setting. - Code pattern differences @@ -82,6 +84,10 @@ macOS 13|lldb|NO ### History +- v1.3 2024-06-28 + + Add command parameter + - v1.2 2024-06-22 Add operating system bits setting diff --git a/shellc.c b/shellc.c index 9f1b0a5..a84b7d9 100644 --- a/shellc.c +++ b/shellc.c @@ -4,7 +4,7 @@ * Function: Convert script into C code * Author: ChenZhongChao * Date: 2023-12-25 - * Version: 1.2 + * Version: 1.3 * Github: https://github.com/chenzhch/shellc.git */ @@ -246,7 +246,11 @@ static const char *fourth[] = { " }", " free(str);", " memset(dev, 0, sizeof(dev));", - " sprintf(dev, \"/dev/fd/%d\", file[0]);", + " sprintf(dev, \"/proc/self/fd/%d\", file[0]);", + " if (access(dev, R_OK)) {", + " sprintf(dev, \"/dev/fd/%d\", file[0]);", + " }", + " args = (char **) malloc((argc + 8) * sizeof(char *));", 0 }; @@ -262,13 +266,18 @@ static const char *fourth_safe[] = { " name = malloc((size_t) length);", " if (which(command, name)) {", " return(1);", - " }", - " pipe = popen(name, \"w\");", + " }", + 0 +}; + +static const char *fourth_end[] = { + " free(name);", + " pipe = popen(str, \"w\");", " if(pipe == NULL) {", - " fprintf(stderr, \"Error: Command %s not found\\n\", command);", + " perror(\"Failed to popen\");", " return(1);", " }", - " free(name);", + " free(str);", "", 0 }; @@ -298,15 +307,7 @@ static const char *sh_start[] = { 0 }; -static const char *fifth[] = { - " args = (char **) malloc((argc + 8) * sizeof(char *));", - " j = 0;", - " if (!strcmp(command, \"node\")) {", - " args[j++] = strdup(command);", - " args[j++] = strdup(\"--preserve-symlinks-main\");", - " } else {", - " args[j++] = strdup(argv[0]);", - " }", +static const char *fifth[] = { " args[j++] = strdup(dev);", " for (i = 1; i < argc; i++) {", " args[j++] = strdup(argv[i]);", @@ -537,56 +538,13 @@ void function(int order, int x, int y, char *str) } } -/*Which command*/ -int which(const char *command) -{ - char *name; - int i, j, len; - char *path = getenv("PATH"); - - if (command[0] == '/' || (command[0] == '.')) { - if (access(command, X_OK) == 0) { - return(0); - } - fprintf(stderr, "Error: Command %s not found\n", command); - return(1); - } - - if (!path) { - fprintf(stderr, "PATH environment variable not set\n"); - return(1); - } - len = strlen(path) + strlen(command) + 8; - name = (char *) malloc(len); - memset(name, 0, (size_t) len); - j = 0; - len = strlen(path); - for (i = 0; i <= len; i++) { - if(i == len || path[i] == ':') { - strcat(name, "/"); - strcat(name, command); - if (access(name, X_OK) == 0) { - free(name); - return(0); - } - memset(name, 0, (size_t) len); - j = 0; - } else { - name[j++] = path[i]; - } - } - free(name); - fprintf(stderr, "Error: Command %s not found\n", command); - return(1); -} - int main(int argc, char **argv) { FILE *in, *out, *fix_file, *self_file; int code_length, obscure_length, length, pos; int fix_pos = -1; char *code_text, *obscure_text, *text; - char *bitmap, *inname = NULL, *outname, *command = NULL; + char *bitmap, *inname = NULL, *outname, *command = NULL, *parameter = NULL; char *fix_format = NULL, *file_name = NULL, *bit = NULL; char str[1024]; long result, offset1, offset2; @@ -598,13 +556,14 @@ int main(int argc, char **argv) char algorithm[32][17]; int i, j, k, loop, mode; int trace_flag = 0, fix_flag = 0, input_flag = 0, command_flag = 0, file_flag = 0; - int self_flag = 0, safe_flag = 0, bit_flag = 0; + int self_flag = 0, safe_flag = 0, bit_flag = 0, para_flag = 0; struct utsname sysinfo; struct stat status; char *self_name = "/proc/self/status"; - char *option = "f:e:b:tsh"; + char *option = "f:e:b:p:tsh"; char **args = (char **) malloc((argc + 1) * sizeof(char *)); - char *usage = "command inputfile [-t] [-s] [-f fix-format] [-e fix-file] [-b 8|16|32|64]"; + char *token; + char *usage = "command inputfile [-t] [-s] [-f fix-format] [-e fix-file] [-p parameter] [-b 8|16|32|64]"; j = 0; args[j++] = strdup(argv[0]); @@ -636,13 +595,14 @@ int main(int argc, char **argv) switch (opt) { case 'h': printf("%s: Convert script into C code\n", argv[0]); - printf("Usage: %s %s", argv[0], usage); + printf("Usage: %s %s\n", argv[0], usage); printf("Option: \n"); printf(" -t Make traceable binary\n"); printf(" -s Using safe mode\n"); printf(" -f Fix arguments format\n"); printf(" -e Fix arguments 0 by external file\n"); printf(" -b Operating system bits\n"); + printf(" -p Command parameter\n"); printf(" -h Display help and return\n"); return(0); case 't': @@ -663,6 +623,10 @@ int main(int argc, char **argv) bit_flag++; bit = strdup(optarg); break; + case 'p': + para_flag++; + parameter = strdup(optarg); + break; case '?': return(1); default: @@ -682,17 +646,14 @@ int main(int argc, char **argv) } if (input_flag != 1 || command_flag != 1 || fix_flag > 1 || trace_flag > 1 - || safe_flag > 1 || file_flag > 1 || bit_flag>1 + || safe_flag > 1 || file_flag > 1 || bit_flag > 1 || para_flag > 1 || (bit_flag && strcmp(bit, "8") && strcmp(bit, "16") && strcmp(bit, "32") && strcmp(bit,"64"))) { fprintf(stderr, "Usage: %s %s\n", argv[0], usage); return(1); } /*Running environment check*/ - if (which(command)) { - return(1); - } - + if (uname(&sysinfo)) { perror("Failed to uname"); return(1); @@ -728,7 +689,7 @@ int main(int argc, char **argv) fprintf(stderr, "Error: fix file %s is not exists\n", fix_format); return(1); } - + /*Self file format*/ if (access(self_name, R_OK) == 0) { self_file = fopen(self_name, "r"); @@ -983,11 +944,23 @@ int main(int argc, char **argv) i = 0; if (safe_flag) { while (fourth_safe[i]) fprintf(out, "%s\n", fourth_safe[i++]); + if (para_flag) { + fprintf(out, " length = strlen(name) + strlen(\"%s\") + 2;\n", parameter); + fprintf(out, " str = malloc(length);\n"); + fprintf(out, " memset(str, 0, length);\n"); + fprintf(out, " strcat(str, name);\n"); + fprintf(out, " strcat(str, \" \");\n"); + fprintf(out, " strcat(str, \"%s\");\n", parameter); + } else { + fprintf(out, " str = strdup(name);\n"); + } + i = 0; + while (fourth_end[i]) fprintf(out, "%s\n", fourth_end[i++]); i = 0; if (!fix_flag || !arg_code[fix_pos][0]) { while(sh_start[i]) fprintf(out, "%s\n", sh_start[i++]); } - } else { + } else { while (fourth[i]) fprintf(out, "%s\n", fourth[i++]); } @@ -1110,7 +1083,18 @@ int main(int argc, char **argv) } else { if (fix_format != NULL && !strcmp(fix_format, "PHP")) { fprintf(out, " write(file[1], \"//\", 2);\n"); - } + } + fprintf(out, "%s\n", " j = 0;"); + if (para_flag) { + fprintf(out, "%s\n", " args[j++] = strdup(command);"); + fprintf(out, " args[j++] = strdup(\"%s\");\n", parameter); + } else if (!strcmp(command, "node")) { + fprintf(out, "%s\n", " args[j++] = strdup(command);"); + fprintf(out, "%s\n", " args[j++] = strdup(\"--preserve-symlinks-main\");"); + } else { + fprintf(out, "%s\n", " args[j++] = strdup(argv[0]);"); + } + i = 0 ; while (fifth[i]) fprintf(out, "%s\n", fifth[i++]); } diff --git "a/\350\257\264\346\230\216.md" "b/\350\257\264\346\230\216.md" index e7f2a4d..823fe29 100644 --- "a/\350\257\264\346\230\216.md" +++ "b/\350\257\264\346\230\216.md" @@ -24,7 +24,7 @@ shellc除了解决```shc```存在的问题外,还增加了代码混淆、随 ### 使用手册 - 生成C代码命令 - ```shellc command inputfile [-t] [-s] [-f fix-format] [-e fix-file] [-b 8|16|32|64]``` + ```shellc command inputfile [-t] [-s] [-f fix-format] [-e fix-file] [-p parameter] [-b 8|16|32|64]``` command: 执行脚本的命令,如 ```sh```、```perl```、```python```、```node```、```ruby```、```Rscript```、```php``` 等。 @@ -38,7 +38,10 @@ shellc除了解决```shc```存在的问题外,还增加了代码混淆、随 -e 选项: 使用外部文件修复参数0值 + -p 选项: 命令参数,如```busybox shell```使用```shellc busybox example.sh -p sh``` + -b 选项: 操作系统位数设置 + - 代码模式区别 @@ -83,6 +86,10 @@ macOS 13|lldb|不支持 ### 修改记录 +- v1.3 2024-06-28 + + 增加命令参数支持 + - v1.2 2024-06-22 增加操作系统位数设置功能