File: match.x.c

package info (click to toggle)
shc 3.7-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 208 kB
  • ctags: 104
  • sloc: ansic: 1,121; makefile: 93; sh: 28; sed: 12; csh: 2
file content (374 lines) | stat: -rw-r--r-- 10,317 bytes parent folder | download
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#if 0
	shc Version 3.7, Generic Script Compiler
	Copyright (c) 1994-2003 Francisco Rosales <frosal@fi.upm.es>

	./shc -v -r -f match 
#endif

static  long date = 0;
static  char mail[] = "Please contact your provider";
static  int  relax = 1;
typedef char pswd_t[377];
static  char pswd[] = 
	"\054\272\075\173\147\140\037\255\371\373\331\346\122\122\072\177"
	"\101\301\252\114\220\343\175\265\074\160\157\226\376\125\020\053"
	"\017\115\246\167\255\305\044\247\300\375\215\023\120\307\222\221"
	"\210\075\336\031\040\133\316\134\313\075\362\312\222\002\365\242"
	"\120\233\031\375\141\075\244\041\072\061\064\212\370\307\034\201"
	"\004\372\232\044\125\150\200\040\246\162\352\070\165\337\332\305"
	"\173\363\302\334\060\147\375\153\230\062\365\221\371\021\022\375"
	"\013\254\041\140\024\241\201\272\023\153\363\210\113\315\115\306"
	"\301\020\242\361\167\237\134\017\321\122\240\312\143\262\307\157"
	"\136\350\317\163\211\120\055\235\274\040\045\007\356\163\315\257"
	"\203\157\240\372\016\375\011\340\117\252\252\262\134\162\041\273"
	"\132\361\056\344\101\133\201\375\174\246\004\152\031\321\031\234"
	"\100\271\226\117\266\240\057\005\112\331\270\246\113\331\141\246"
	"\312\217\212\014\353\013\011\147\261\016\321\313\337\352\147\040"
	"\243\376\157\132\236\236\137\350\167\027\216\303\361\360\151\273"
	"\177\363\307\152\376\321\321\257\337\242\172\276\214\342\336\060"
	"\340\115\212\176\353\351\146\143\001\364\046\362\344\217\255\144"
	"\202\165\316\200\106\240\057\045\102\252\343\317\214\302\377\154"
	"\017\211\352\373\162\120\136\163\104\204\145\051\023\023\215\225"
	"\210\133\025\316\373\104\363\076\356\326\015\172\230\014\346\250"
	"\225\320\243\007\040\001\173\145\205\340\216\230\363\033\055\173"
	"\166\102\111\162\206\074\260\165\023\275\357\253\311\326\123\136"
	"\246\366\145\307\367\340\054\174\301\272\024\264\325\101\060\113"
	"\203\171\275\012\266\155\177\311\052\164\363\104\310\121\353\276"
	"\267\262\266\227\336\062\130";
typedef char shll_t[8];
static  char shll[] = 
	"\175\112\220\201\155\111\046\265\107\015\155\210\075\270\014\266";
typedef char inlo_t[3];
static  char inlo[] = 
	"\122\005\073\026\154\343\225\065\016";
typedef char xecc_t[15];
static  char xecc[] = 
	"\241\017\261\031\357\151\233\325\133\005\325\143\226\036\005\252"
	"\001\110";
typedef char lsto_t[1];
static  char lsto[] = 
	"\223\123\063";
#define TEXT_chk1	"DgFFH1i0"
typedef char chk1_t[9];
static  char chk1[] = 
	"\115\134\062\322\347\031\273\351\044\072\317\053\042\162\161\013"
	"\135\052\110\173\023\216\176";
typedef char opts_t[1];
static  char opts[] = 
	"\065\166\104";
typedef char text_t[337];
static  char text[] = 
	"\351\161\266\056\370\314\362\364\031\123\063\246\334\175\122\130"
	"\054\302\322\010\337\166\251\322\371\001\313\063\261\361\106\242"
	"\031\135\377\161\350\075\313\137\264\334\232\227\042\204\321\210"
	"\237\044\236\350\251\210\224\261\024\320\362\115\275\224\220\306"
	"\111\341\330\225\040\134\064\130\320\007\224\007\023\212\017\251"
	"\124\165\152\061\300\112\000\034\276\171\071\000\063\244\127\356"
	"\020\313\056\056\130\141\126\371\216\243\214\140\050\010\162\203"
	"\003\022\237\324\073\243\201\124\106\300\203\054\371\341\001\363"
	"\022\213\352\207\150\243\252\112\326\204\271\007\044\273\062\270"
	"\105\340\261\044\255\155\203\005\356\346\025\305\060\044\322\110"
	"\324\126\355\021\065\202\164\345\046\247\127\242\176\174\146\004"
	"\052\345\230\176\312\073\162\210\174\163\273\107\056\235\030\143"
	"\153\244\026\230\020\146\177\335\233\313\042\113\304\357\216\221"
	"\220\031\353\071\160\310\043\177\325\216\314\224\245\013\341\026"
	"\256\041\047\051\320\255\205\231\160\115\201\160\023\023\062\103"
	"\326\345\223\147\325\271\134\067\330\266\133\015\003\163\256\204"
	"\247\217\227\040\033\112\246\033\355\357\333\273\262\202\335\234"
	"\166\251\044\064\273\173\346\255\306\113\134\316\261\021\030\246"
	"\361\041\067\203\006\002\042\357\240\372\257\377\013\164\371\312"
	"\113\206\322\377\263\211\371\065\315\032\066\161\156\123\010\141"
	"\134\262\073\040\035\071\100\142\151\162\170\370\155\071\223\000"
	"\147\130\145\023\101\116\104\156\270\152\226\030";
#define TEXT_chk2	"RiOt9V3xfm"
typedef char chk2_t[11];
static  char chk2[] = 
	"\237\313\342\205\074\275\361\101\355\077\145\374\007\217\116\161"
	"\337\302\115\240\101\206";
typedef char hide_t[4096];

#define DEBUGEXEC	0	/* Define as 1 to debug execvp calls */
#define TRACEABLE	0	/* Define as 1 to enable ptrace the executable */

/* rtc.c */

#include <sys/stat.h>
#include <sys/types.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

/**
 * 'Alleged RC4' Source Code picked up from the news.
 * From: allen@gateway.grumman.com (John L. Allen)
 * Newsgroups: comp.lang.c
 * Subject: Shrink this C code for fame and fun
 * Date: 21 May 1996 10:49:37 -0400
 */

static unsigned char state[256], indx, jndx;

/*
 * Reset rc4 state. 
 */
void state_0(void)
{
	indx = jndx = 0;
	do {
		state[indx] = indx;
	} while (++indx);
}

/*
 * Set key. Can be used more than once. 
 */
void key(char * str, int len)
{
	unsigned char tmp, * ptr = (unsigned char *)str;
	while (len > 0) {
		do {
			tmp = state[indx];
			jndx += tmp;
			jndx += ptr[(int)indx % len];
			state[indx] = state[jndx];
			state[jndx] = tmp;
		} while (++indx);
		ptr += 256;
		len -= 256;
	}
}

/*
 * Crypt data. 
 */
void rc4(char * str, int len)
{
	unsigned char tmp, * ptr = (unsigned char *)str;
	jndx = 0;
	while (len > 0) {
		indx++;
		tmp = state[indx];
		jndx += tmp;
		state[indx] = state[jndx];
		state[jndx] = tmp;
		tmp += state[indx];
		*ptr ^= state[tmp];
		ptr++;
		len--;
	}
}

/*
 * Key with file invariants. 
 */
int key_with_file(char * file)
{
	struct stat statf[1];
	struct stat control[1];

	if (stat(file, statf) < 0)
		return -1;

	/* Turn on stable fields */
	memset(control, 0, sizeof(control));
	control->st_ino = statf->st_ino;
	control->st_dev = statf->st_dev;
	control->st_rdev = statf->st_rdev;
	control->st_uid = statf->st_uid;
	control->st_gid = statf->st_gid;
	control->st_size = statf->st_size;
	control->st_mtime = statf->st_mtime;
	control->st_ctime = statf->st_ctime;
	key((char *)control, sizeof(control));
	return 0;
}

#if DEBUGEXEC
void debugexec(char * shll, int argc, char ** argv)
{
	int i;
	fprintf(stderr, "shll=%s\n", shll ? shll : "<null>");
	fprintf(stderr, "argc=%d\n", argc);
	if (!argv) {
		fprintf(stderr, "argv=<null>\n");
	} else { 
		for (i = 0; i <= argc ; i++)
			fprintf(stderr, "argv[%d]=%.60s\n", i, argv[i] ? argv[i] : "<null>");
	}
}
#endif /* DEBUGEXEC */

void rmarg(char ** argv, char * arg)
{
	for (; argv && *argv && *argv != arg; argv++);
	for (; argv && *argv; argv++)
		*argv = argv[1];
}

int chkenv(int argc)
{
	char buff[512];
	unsigned mask, m;
	int l, a, c;
	char * string;
	extern char ** environ;

	mask  = (unsigned)chkenv;
	mask ^= (unsigned)getpid() * ~mask;
	sprintf(buff, "x%x", mask);
	string = getenv(buff);
#if DEBUGEXEC
	fprintf(stderr, "getenv(%s)=%s\n", buff, string ? string : "<null>");
#endif
	l = strlen(buff);
	if (!string) {
		/* 1st */
		sprintf(&buff[l], "=%u %d", mask, argc);
		putenv(strdup(buff));
		return 0;
	}
	c = sscanf(string, "%u %d%c", &m, &a, buff);
	if (c == 2 && m == mask) {
		/* 3rd */
		rmarg(environ, &string[-l - 1]);
		return 1 + (argc - a);
	}
	return -1;
}

#if !TRACEABLE

#define _LINUX_SOURCE_COMPAT
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

void untraceable(char * argv0)
{
	char proc[80];
	int pid, mine;

	switch(pid = vfork()) {
	case  0:
		pid = getppid();
		/* For problematic SunOS ptrace */
		sprintf(proc, "/proc/%d/as", (int)pid);
		close(0);
		mine = !open(proc, O_RDWR|O_EXCL);
		if (!mine && errno != EBUSY)
			mine = !ptrace(PTRACE_ATTACH, pid, 0, 0);
		if (mine) {
			kill(pid, SIGCONT);
		} else {
			fprintf(stderr, "%s is being traced!\n", argv0);
			kill(pid, SIGKILL);
		}
		_exit(mine);
	case -1:
		break;
	default:
		if (pid == waitpid(pid, 0, 0))
			return;
	}
	perror(argv0);
	_exit(1);
}
#endif /* !TRACEABLE */

char * xsh(int argc, char ** argv)
{
	char buff[512];
	char * scrpt;
	int ret, i, j;
	char ** varg;

	state_0();
	key(pswd, sizeof(pswd_t));
	rc4(shll, sizeof(shll_t));
	rc4(inlo, sizeof(inlo_t));
	rc4(xecc, sizeof(xecc_t));
	rc4(lsto, sizeof(lsto_t));
	rc4(chk1, sizeof(chk1_t));
	if (strcmp(TEXT_chk1, chk1))
		return "location has changed!";
	ret = chkenv(argc);
	if (ret < 0)
		return "abnormal behavior!";
	varg = (char **)calloc(argc + 10, sizeof(char *));
	if (!varg)
		return 0;
	if (ret) {
		if (!relax && key_with_file(shll))
			return shll;
		rc4(opts, sizeof(opts_t));
		rc4(text, sizeof(text_t));
		rc4(chk2, sizeof(chk2_t));
		if (strcmp(TEXT_chk2, chk2))
			return "shell has changed!";
		if (sizeof(text_t) < sizeof(hide_t)) {
			/* Prepend spaces til a sizeof(hide_t) script size. */
			scrpt = malloc(sizeof(hide_t));
			if (!scrpt)
				return 0;
			memset(scrpt, (int) ' ', sizeof(hide_t));
			memcpy(&scrpt[sizeof(hide_t) - sizeof(text_t)], text, sizeof(text_t));
		} else {
			scrpt = text;	/* Script text */
		}
	} else {			/* Reexecute */
		if (*xecc) {
			sprintf(buff, xecc, argv[0]);
			scrpt = buff;
		} else {
			scrpt = argv[0];
		}
	}
	j = 0;
	varg[j++] = argv[0];		/* My own name at execution */
	if (ret && *opts)
		varg[j++] = opts;	/* Options on 1st line of code */
	if (*inlo)
		varg[j++] = inlo;	/* Option introducing inline code */
	varg[j++] = scrpt;		/* The script itself */
	if (*lsto)
		varg[j++] = lsto;	/* Option meaning last option */
	i = (ret > 1) ? ret : 0;	/* Args numbering correction */
	while (i < argc)
		varg[j++] = argv[i++];	/* Main run-time arguments */
	varg[j] = 0;			/* NULL terminated array */
#if DEBUGEXEC
	debugexec(shll, j, varg);
#endif
	execvp(shll, varg);
	return shll;
}

int main(int argc, char ** argv)
{
#if DEBUGEXEC
	debugexec("main", argc, argv);
#endif
#if !TRACEABLE
	untraceable(argv[0]);
#endif
	if (date && (date < (long)time(NULL))) {
		fprintf(stderr, "%s has expired!\n", argv[0]);
		fprintf(stderr, "%s\n", mail);
	} else {
		argv[1] = xsh(argc, argv);
		fprintf(stderr, "%s%s%s: %s\n", argv[0],
			errno ? ": " : "",
			errno ? strerror(errno) : "",
			argv[1] ? argv[1] : "<null>"
		);
	}
	return 1;
}