-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathobfuscated_bash.c
276 lines (246 loc) · 8.98 KB
/
obfuscated_bash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* Obfuscated Bash
// Copyright (C) 2017- Davide Rao: louigi600 (at) yahoo (dot) it
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version provided that no poit of the
// AA License is violated.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <time.h>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/buffer.h>
#include "functions.h"
#include "functions.c"
#include "interpreter.h"
typedef struct option
{ char option_shortname;
enum option_types { flag = 0, parameter = 1, } option_type;
char option_helptext[256];
bool option_flag_status;
char option_param[256];
} option;
char prod_uuid[256]="/sys/devices/virtual/dmi/id/product_uuid";
char prod_serial[256]="/sys/devices/virtual/dmi/id/product_serial";
char key[33]="\0";
char iv[17]="\0";
/* getopt extension functions for using an array of option as defined above */
bool flag_status (char shortname, option *oa, int oalen)
{ int i;
for (i=0;i<oalen;i++)
if(oa[i].option_shortname==shortname && oa[i].option_flag_status) return(true);
return(false);
}
int set_flag (char shortname, option *oa, int oalen)
{ int i;
for (i=0;i<oalen;i++)
if(oa[i].option_shortname==shortname) oa[i].option_flag_status=true;
}
int set_param (char shortname, char *parameter, option *oa, int oalen)
{ int i;
for (i=0;i<oalen;i++)
if(oa[i].option_shortname==shortname)
{ strcpy(oa[i].option_param,parameter);
oa[i].option_flag_status=true;
}
}
/*return the element of optionarray containing the shortname flag/parameter */
int who_has_shortname (char shortname, option *oa, int oalen)
{int i;
for (i=0;i<oalen;i++)
if(oa[i].option_shortname==shortname && oa[i].option_flag_status) return(i);
return(-1);
}
int get_param (char *param, char shortname, option *oa, int oalen)
{ int i;
for (i=0;i<oalen;i++)
{ if(oa[i].option_shortname==shortname && oa[i].option_flag_status)
{ strcpy(param,oa[i].option_param);
return(i);
}
}
// sprintf(param,"\0");
return(-1);
}
int usage(char *name, char *message, option *oa,int oalen)
{ int i;
char str[80]="\0",options[80]="\0";
for (i=0;i<oalen;i++)
{ if(oa[i].option_type==0) sprintf(str,"[-%c] ",oa[i].option_shortname);
else sprintf(str,"[-%c <value>] ",oa[i].option_shortname);
strcat(options,str);
}
printf("Usage:\n%s %s <input filename>\n",name,options);
for (i=0;i<oalen;i++)
{ if(oa[i].option_type==0) printf("-%c \t\t: %s\n",oa[i].option_shortname,oa[i].option_helptext);
else printf("-%c <value>\t: %s\n",oa[i].option_shortname,oa[i].option_helptext);
}
if(strlen(message)>0)
{ fprintf(stderr,"%s\n",message);
exit(1);
} else exit(0);
}
/* getopt extentsion ends here */
int main(int argc, char *argv[])
{ FILE *infile;
// char string[256]="\0";
int ret;
char *str;
static const char *copyright="Obfuscated Bash\n"
"Copyright (C) 2017- Davide Rao: louigi600 (at) yahoo (dot) it\n"
"\nThis program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation; either version 2 of the License, or\n"
"(at your option) any later version provided that no poit of the\n"
"AA License is violated.\n";
/* these variables are for getopt and the extension */
int c;
opterr = 0;
option optionarray[5] = {
{'c',0,"Cleanup intermediate c files on success.",false,""},
{'h',0,"How this help message.",false,""},
{'o',1,"Output filename.",false,""},
{'r',0,"Create a reusable binary.",false,""},
{'s',0,"Create a statc binary.",false,""},
};
// char optstring[80]="\0";
char optstring[256]="\0";
int i;
/* str is already defines as pointer to char and there is a mallog allocating 256 bytes for it, we should be ok to use the one already defined */
// char str[80]="\0";
/* end variables for getopt */
char input_filename[256]="\0",output_filename[256]="\0";
char uuid[37]="\0";
char serial[17]="\0";
char hex_digits[17]="0123456789abcdef\0";
bool reusable_build=false;
str=malloc(256);
/*parsing options */
for (i=0;i<sizeof(optionarray)/sizeof(option);i++)
{ if(optionarray[i].option_type==0) sprintf(str,"%c",optionarray[i].option_shortname);
else sprintf(str,"%c:",optionarray[i].option_shortname);
strcat(optstring,str);
}
while ((c = getopt (argc, argv, optstring)) != -1)
switch (c)
{ case 'c':
case 'r':
set_flag(c, optionarray, sizeof(optionarray)/sizeof(option));
break;
case 's':
set_flag(c, optionarray, sizeof(optionarray)/sizeof(option));
break;
case 'h':
usage(argv[0],"\0",optionarray,sizeof(optionarray)/sizeof(option));
case 'o':
set_param(c,optarg,optionarray,sizeof(optionarray)/sizeof(option));
break;
case '?':
if (optopt == 'o') sprintf(str,"\nERROR: option `-%c' requires an argument.", optopt);
else if (isprint (optopt)) sprintf(str,"\nERROR: unknown option `-%c'.", optopt);
else sprintf(str,"\nERROR: nknown option character `\\x%x'.", optopt);
usage(argv[0],str,optionarray,sizeof(optionarray)/sizeof(option));
default:
abort ();
}
/* doing some sanity checks */
if(optind==argc) usage(argv[0],"\nERROR: no input file was provided.",optionarray,sizeof(optionarray)/sizeof(option));
sprintf(input_filename,"%s",argv[optind]);
if(!flag_status('o',optionarray,sizeof(optionarray)/sizeof(option))) sprintf(output_filename,"%s.x",argv[optind]);
else get_param(output_filename,'o',optionarray,sizeof(optionarray)/sizeof(option));
printf("Output filename will be: %s\n",output_filename);
/* finished parsing options */
/* making sure input file is readable and then immediately closing it */
if((infile=fopen(input_filename,"r"))==NULL)
{ printf("Error opening %s.\n",input_filename);
exit(1);
}
fclose(infile);
/* generating random uuid and serial for reusable binary */
if(flag_status('r',optionarray,sizeof(optionarray)/sizeof(option)))
{ srand ( time(NULL) );
for(i=0;i<sizeof(uuid)-1;i++)
{ switch(i)
{ case 8:
case 13:
case 18:
case 23:
c='-';
uuid[i]='-';
break;
default:
c=(unsigned char) (rand() % 16);
uuid[i]=hex_digits[c];
break;
}
}
uuid[sizeof(uuid)-1]='\0';
printf("Random uuid: %s\n",uuid);
for(i=0;i<sizeof(serial)-1;i++)
{ c=(unsigned char) (rand() % 16);
serial[i]=hex_digits[c];
}
serial[sizeof(serial)-1]='\0';
printf("Random serial: %s\n",serial);
reusable_build=true;
} else
{ c=getuuid(uuid);
printf("Machine uuid: %s of length %i\n",uuid,c);
c=getserial(serial);
printf("Machine serial: %s of length %i\n",serial,c);
}
// printf("About to make key ... ");
makekey(key,uuid);
printf("Used key: >%s<\n",key);
makeiv(iv,serial);
printf("Used IV: >%s<\n",iv);
if((ret=mk_sh_c(input_filename,key,iv,reusable_build,serial,uuid))<0)
printf("Failed: %i/n",ret);
else printf("Created %s.c\n",input_filename);
printf("Compiling %s.c ",input_filename);
if(flag_status('s',optionarray,sizeof(optionarray)/sizeof(option)))
{ printf("as static binary ");
// sprintf(str,"sleep 1 ; sync ;cc %s.c -o %s -static -lssl -lcrypto -ldl -lltdl -static-libgcc && strip %s",input_filename,output_filename,output_filename);
sprintf(str,"sleep 1 ; sync ;cc %s.c -o %s -static -lssl -lcrypto -ldl -static-libgcc && strip %s",input_filename,output_filename,output_filename);
} else sprintf(str,"sleep 1 ; sync ;cc %s.c -o %s -lssl -lcrypto && strip %s",input_filename,output_filename,output_filename);
// printf("%s\n",str);
// exit(0);
printf("... ");
if(system(str)!=0)
{ printf("failed\n");
exit(1);
} else printf("done\n");
/* if -c flag was issued cleaning up intermediate c file */
if(flag_status('c',optionarray,sizeof(optionarray)/sizeof(option)))
{ printf("Cleaning up intermediate c file: %s.c ... ",input_filename);
sprintf(str,"rm -f %s.c",input_filename);
if(system(str)!=0)
{ printf("failed\n");
exit(1);
} else printf("done\n");
}
printf("Output filename: %s\n",output_filename);
//#ifdef __linux__
// printf("You are a linux freak\n");
//#else
// printf("You are not a linux freak\n");
//#endif
return(0);
printf("%s\n",copyright);
}