This commit is contained in:
aixiao 2023-07-07 11:58:01 +08:00
parent abf85e1cbd
commit d420e58926
5 changed files with 762 additions and 738 deletions

View File

@ -7,6 +7,7 @@ OBJ := sha
all: aes.o sha.o all: aes.o sha.o
$(CC) $(CFLAGS) -o $(OBJ) $^ $(LDFLAGS) $(CC) $(CFLAGS) -o $(OBJ) $^ $(LDFLAGS)
$(STRIP) $(OBJ)
.c.o: .c.o:
$(CC) $(CFLAGS) -c $< $(CC) $(CFLAGS) -c $<

View File

@ -20,11 +20,13 @@
SHA SHA
Shell Strict AES 128 bit encryption tool Shell Strict AES 128 bit encryption tool
AUTHOR: AIXIAO@AIXIAO.ME Author: AIXIAO@AIXIAO.ME
Usage: Usage:
-k : key sha [-kfh?]
-f : Script file -k : Key
-f : Script File
-h -? : Print Help
静态链接: 静态链接:

1
aes.c
View File

@ -2,7 +2,6 @@
#include "aes.h" #include "aes.h"
#define Nb 4 #define Nb 4
#if defined(AES256) && (AES256 == 1) #if defined(AES256) && (AES256 == 1)

5
aes.h
View File

@ -1,6 +1,11 @@
#ifndef _AES_H_ #ifndef _AES_H_
#define _AES_H_ #define _AES_H_
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>

189
sha.c
View File

@ -16,11 +16,9 @@
#define BUFFER_SIZE 270 #define BUFFER_SIZE 270
char *buff; char *buff;
long file_size; long file_size;
static void hex2str(uint8_t * input, uint16_t input_len, char *output) static void hex2str(uint8_t * input, uint16_t input_len, char *output)
{ {
char *hexEncode = "0123456789ABCDEF"; char *hexEncode = "0123456789ABCDEF";
@ -44,7 +42,7 @@ int open_file(char *filename)
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
file_size = ftell(file); file_size = ftell(file);
buff = (char *)malloc(file_size + 1); buff = (char *)malloc(file_size + 1);
if (buff == NULL) if (!buff)
perror("out of memory."); perror("out of memory.");
rewind(file); rewind(file);
if (fread(buff, file_size, 1, file) < 1) { if (fread(buff, file_size, 1, file) < 1) {
@ -549,53 +547,50 @@ char *source_c[] = {
"", "",
"#endif // #if defined(CTR) && (CTR == 1)", "#endif // #if defined(CTR) && (CTR == 1)",
"", "",
"static int oneHexChar2Hex(char hex)",
"{", "static int oneHexChar2Hex(char hex) {",
" int outHex = 0;", " int outHex = 0;",
" if (isdigit(hex)) {", " if (isdigit(hex)) {",
" outHex = hex - '0';", " outHex = hex - '0';",
" } else if (isupper(hex)) {", " } else if (isupper(hex)) {",
" outHex = hex - 'A' + 10;", " outHex = hex - 'A' + 10;",
" } else {", " } else if (islower(hex)) {",
" outHex = hex - 'a' + 10;", " outHex = hex - 'a' + 10;",
" }", " }",
" return outHex;", " return outHex;",
"}", "}",
"", "",
"static int HexString2Hex(char *inHexString, char *outHex, int count)", "static int HexString2Hex(const char *inHexString, uint8_t *outHex, int count) {",
"{", " if (inHexString == NULL || outHex == NULL || count <= 0) {",
" int ret = -1;",
" int len = 0;",
" int i;",
" char ch1, ch2;",
" if (NULL == inHexString)",
" return -1;", " return -1;",
" len = count;",
" if (len < 1)",
" return -1;",
" len &= ~1;",
" for (i = 0; i < len; i += 2) {",
" ch1 = inHexString[i];",
" ch2 = inHexString[i + 1];",
" outHex[i / 2 + 1] = 0;",
" if (isxdigit(ch1) && isxdigit(ch2)) {",
" ch1 = oneHexChar2Hex(ch1);",
" ch2 = oneHexChar2Hex(ch2);",
" outHex[i / 2] = (ch1 << 4) | ch2;",
" } else {",
" goto EXIT;",
" }", " }",
" }", "" " int len = strlen(inHexString);",
" return 0;", " bool hasIncompleteByte = (len % 2 != 0);",
"EXIT:", "",
" return ret;", " if (hasIncompleteByte) {",
" len--;",
" }",
"",
" if (count < len / 2) {",
" return -1;",
" }",
"",
" for (int i = 0; i < len; i += 2) {",
" char ch1 = inHexString[i];",
" char ch2 = inHexString[i + 1];",
"",
" if (!isxdigit(ch1) || !isxdigit(ch2)) {",
" return -1;",
" }",
"" " int hex1 = oneHexChar2Hex(ch1);",
" int hex2 = oneHexChar2Hex(ch2);",
"",
" outHex[i / 2] = (hex1 << 4) | hex2;",
" }",
"",
" return len / 2;",
"}", "}",
"", "",
"static int is_Resolver(char *shll_text, char *shbin)", "static int is_Resolver(char *shll_text, char *shbin)",
"{", "{",
" char *p, *p1;", " char *p, *p1;",
@ -663,13 +658,12 @@ char *source_c[] = {
" char *argvs[BUFFER_SIZE];", " char *argvs[BUFFER_SIZE];",
" int l=1;", " int l=1;",
" int i=4;", " int i=4;",
" //static uint8_t key[16] = \"aixiao.me\";", "",
" struct AES_ctx ctx;", " struct AES_ctx ctx;",
" uint8_t *Hex_string = (uint8_t *) malloc(encrypted_text_len*2);", " uint8_t *Hex_string = (uint8_t *) malloc(encrypted_text_len*2);",
" char *shbin = NULL;", " char *shbin = NULL;",
"", "",
" reverse_string((char *)Encrypted_data, encrypted_text_len*2);", " reverse_string((char *)Encrypted_data, encrypted_text_len*2);",
" memset(Hex_string, 0, encrypted_text_len*2);", " memset(Hex_string, 0, encrypted_text_len*2);",
"", "",
@ -737,7 +731,7 @@ void reverse_string(char *str)
length = strlen(str); //获取字符串长度 length = strlen(str); //获取字符串长度
p1 = str; //p1指向字符串首地址 p1 = str; //p1指向字符串首地址
p2 = str + length - 1; //p2指向字符串尾地址 p2 = str + length - 1; //p2指向字符串尾地址
if (str == NULL) { if (!str) {
printf("空指针错误!"); printf("空指针错误!");
return; return;
} }
@ -756,21 +750,53 @@ void usage(void)
{ {
printf(" SHA\n"); printf(" SHA\n");
printf(" Shell Strict AES 128 bit encryption tool\n"); printf(" Shell Strict AES 128 bit encryption tool\n");
printf("AUTHOR: AIXIAO@AIXIAO.ME\n"); printf("Author: AIXIAO@AIXIAO.ME\n");
printf("\n"); printf("\n");
printf("Usage:\n"); printf("Usage:\n");
printf(" -k : key\n"); printf(" sha [-kfh?]\n");
printf(" -f : Script file\n"); printf(" -k : Key\n");
printf(" -f : Script File\n");
printf(" -h -? : Print Help\n");
printf("\n"); printf("\n");
exit(0); exit(0);
} }
char pool[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', '!', '@', '#', '$', '%', '^', '&', '*',
};
int rand_key(char *key)
{
int PASSWD_LEN = 16;
struct timeval tpstart;
char password[17];
int i = 0;
memset(password, 0, 17);
gettimeofday(&tpstart, NULL);
srand(tpstart.tv_usec);
while (i != PASSWD_LEN) {
password[i++] = pool[rand() % sizeof(pool)];
}
strcpy(key, password);
return 0;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
static uint8_t key[16] = "aixiao.me"; static uint8_t key[17] = "";
int key_l = 0;
char sh_file[1024]; char sh_file[1024];
int indx = 0;
int opt; int opt;
char optstrs[] = ":k:f:h?"; char optstrs[] = ":k:f:h?";
@ -783,94 +809,83 @@ int main(int argc, char *argv[])
strcpy((char *)sh_file, optarg); strcpy((char *)sh_file, optarg);
break; break;
case ':': case ':':
//printf("\nMissing argument after: -%c\n", optopt);
usage(); usage();
case 'h': case 'h':
case '?': case '?':
//printf("\nInvalid argument: %c\n", optopt);
usage(); usage();
default: default:
usage(); usage();
} }
} }
key_l = strlen((char *)key); if (strlen((char *)key) == 0) {
//printf("%d\n", key_l); rand_key((char *)key);
} else {
if ((strlen((char *)key) < 8) || (strlen((char *)key) > 15)) {
if (key_l < 8 || key_l > 15) {
printf("The key must be 8-15 digits!\n"); printf("The key must be 8-15 digits!\n");
exit(1); exit(1);
} }
}
if (argv[1] == NULL) {
usage();
}
open_file(sh_file); open_file(sh_file);
struct AES_ctx ctx; struct AES_ctx ctx;
//static uint8_t key[16] = "aixiao.me";
uint8_t text_content[file_size * 2]; uint8_t text_content[file_size * 2];
uint8_t text_content_out[file_size * 2]; uint8_t text_content_out[file_size * 2];
uint16_t text_content_length = file_size; uint16_t text_content_length = file_size;
unsigned char encrypted_text[file_size * 3]; unsigned char encrypted_text[file_size * 2];
char encrypted_text_len[BUFFER_SIZE]; char encrypted_text_len[BUFFER_SIZE];
uint8_t *hexString = (uint8_t *) malloc(file_size * 3); uint8_t *hexString = (uint8_t *) malloc(file_size * 2);
FILE *sfile = NULL; FILE *sfile = NULL;
char sourcefile[BUFFER_SIZE]; char sourcefile[BUFFER_SIZE];
char binfile[BUFFER_SIZE]; char binfile[BUFFER_SIZE];
char buildcmd[BUFFER_SIZE * 10]; char buildcmd[BUFFER_SIZE * 10];
char encrypted_key[1024];
int indx = 0;
memset(text_content, 0, file_size * 2); memset(text_content, 0, file_size * 2);
memset(text_content_out, 0, file_size * 2); memset(text_content_out, 0, file_size * 2);
memset(encrypted_text, 0, file_size * 3); memset(encrypted_text, 0, file_size * 2);
memset(encrypted_text_len, 0, BUFFER_SIZE); memset(encrypted_text_len, 0, BUFFER_SIZE);
memset(hexString, 0, file_size * 3); memset(hexString, 0, file_size * 2);
memset(sourcefile, 0, BUFFER_SIZE); memset(sourcefile, 0, BUFFER_SIZE);
memset(binfile, 0, BUFFER_SIZE); memset(binfile, 0, BUFFER_SIZE);
memset(buildcmd, 0, BUFFER_SIZE * 10); memset(buildcmd, 0, BUFFER_SIZE * 10);
memset(encrypted_key, 0, 1024);
memcpy(text_content, buff, strlen(buff)); memcpy(text_content, buff, strlen(buff));
AES_init_ctx(&ctx, key); AES_init_ctx(&ctx, key);
AES_ECB_encrypt(&ctx, text_content); AES_ECB_encrypt(&ctx, text_content);
//转16进制字符串 //转16进制字符串
hex2str(text_content, file_size, (char *)hexString); hex2str(text_content, file_size, (char *)hexString);
//printf("%s\n", hexString);
reverse_string((char *)hexString); reverse_string((char *)hexString);
//printf("%s\n", hexString);
//拼接 //拼接
strcat((char *)encrypted_text, "char Encrypted_data[]=\""); strcat((char *)encrypted_text, "char Encrypted_data[]=\"");
strcat((char *)encrypted_text, (const char *)hexString); strcat((char *)encrypted_text, (const char *)hexString);
strcat((char *)encrypted_text, "\";"); strcat((char *)encrypted_text, "\";");
char encrypted_key[1024]; strcpy(encrypted_key, "const char key[17] = \"");
memset(encrypted_key, 0, 1024);
strcpy(encrypted_key, "const char key[16] = \"");
strcat((char *)encrypted_key, (char *)key); strcat((char *)encrypted_key, (char *)key);
strcat((char *)encrypted_key, "\";"); strcat((char *)encrypted_key, "\";");
// 长度 // 长度
sprintf(encrypted_text_len, "int encrypted_text_len=%d;\n", text_content_length); sprintf(encrypted_text_len, "int encrypted_text_len=%d;\n", text_content_length);
strcpy(sourcefile, sh_file); strcpy(sourcefile, sh_file);
strcat(sourcefile, ".c"); strcat(sourcefile, ".c");
// 写入文件 // 写入文件
sfile = fopen(sourcefile, "w"); sfile = fopen(sourcefile, "w");
if (sfile == NULL) if (sfile == NULL) {
{
perror("fopen"); perror("fopen");
return 1; return 1;
} }
@ -883,8 +898,6 @@ int main(int argc, char *argv[])
fprintf(sfile, "%s\n", source_c[indx]); fprintf(sfile, "%s\n", source_c[indx]);
fclose(sfile); fclose(sfile);
strcpy(binfile, sourcefile); strcpy(binfile, sourcefile);
strcat(binfile, ".x"); strcat(binfile, ".x");
sleep(1); sleep(1);
@ -894,7 +907,6 @@ int main(int argc, char *argv[])
else else
sprintf(buildcmd, "gcc %s -o %s", sourcefile, binfile); sprintf(buildcmd, "gcc %s -o %s", sourcefile, binfile);
strcat(buildcmd, " && strip "); strcat(buildcmd, " && strip ");
strcat(buildcmd, binfile); strcat(buildcmd, binfile);
// 编译 // 编译
@ -902,29 +914,34 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
//remove(sourcefile); //remove(sourcefile);
free(buff); free(buff);
// 压缩 // 压缩
if (0 == system("which upx 1> /dev/null")) { if (system("which upx 1> /dev/null") == 0) {
FILE *fp = NULL; char *upxcommand = malloc(BUFFER_SIZE);
char upxcommand[BUFFER_SIZE]; if (upxcommand == NULL) {
return -1; // 内存分配失败,处理错误
}
memset(upxcommand, 0, BUFFER_SIZE); memset(upxcommand, 0, BUFFER_SIZE);
strcpy(upxcommand, "upx -9 "); strcpy(upxcommand, "upx -9 ");
strcat(upxcommand, binfile); strcat(upxcommand, binfile);
sleep(1);
fp = popen(upxcommand, "r"); FILE *fp = popen(upxcommand, "r");
if (NULL == fp) if (fp == NULL) {
{ free(upxcommand); // 释放分配的内存
return -1; return -1; // 处理打开管道错误
} }
pclose(fp); int status = pclose(fp);
if (status == -1 || WEXITSTATUS(status) != 0) {
free(upxcommand); // 释放分配的内存
return -1; // 处理执行upx命令错误
} }
free(upxcommand); // 释放分配的内存
}
return 0; return 0;
} }