summaryrefslogtreecommitdiffstats
path: root/src/exepack.c
blob: 219abb4087e35b05d3fa13d23f4583a5f05f4b40 (plain)
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
#include "config.h"


#include <stdlib.h>
#include <stdio.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>

extern char **environ;


#include "minilzo.h"

/* integer data type that is _exactly_ 32 bit
 */
#if defined(HAVE_INT_32)
#define UINT32 unsigned int
#elif defined(HAVE_LONG_32)
#define UINT32 unsigned long
#elif defined(HAVE_SHORT_32)
#define UINT32 unsigned short
#endif


#include "exepack.data"


static UINT32 cstate[3], astate[3];
 
/* interval [0, 4294967296]
 */       
static UINT32 taus_get_long (UINT32 * state)
{
#define TAUSWORTHE(s,a,b,c,d) ((s &c) <<d) ^ (((s <<a) ^s) >>b)

  state[0] = TAUSWORTHE (state[0], 13, 19, 4294967294UL, 12);
  state[1] = TAUSWORTHE (state[1],  2, 25, 4294967288UL,  4);
  state[2] = TAUSWORTHE (state[2],  3, 11, 4294967280UL, 17);

  return (state[0] ^ state[1] ^ state[2]);
}

void taus_set_from_state (UINT32 * state, UINT32 * state0)
{
  state[0] = state0[0]  | (UINT32) 0x03;
  state[1] = state0[1]  | (UINT32) 0x09;
  state[2] = state0[2]  | (UINT32) 0x17;
  
  /* 'warm up'
   */
  taus_get_long (state);
  taus_get_long (state);
  taus_get_long (state);
  taus_get_long (state);
  taus_get_long (state);
  taus_get_long (state);

  return;
}

void set2 (char * pos, char c1, char c2)
{
  pos[0] = c1;
  pos[1] = c2;
  return;
}
  
void set4 (char * pos, char c1, char c2, char c3, char c4)
{
  pos[0] = c1;
  pos[1] = c2;
  pos[2] = c3;
  pos[3] = c4;
  return;
}

int main(int argc, char *argv[]) 
{
  int file;
  long result;

  unsigned long i    = argc; /* dummy use of argc to fix compiler warning */
  unsigned long len  = 0;

  struct stat sbuf;
  struct stat fbuf;

  /* For compression.
   */
  lzo_byte *    inbuf;
  lzo_byte *    outbuf;
  int           r;
  lzo_uint      in_len;
  lzo_uint      out_len;


  char *        p;

  char          fname[128];
#if defined (__linux__)
  char          pname[128];
#endif

  UINT32        pid;

  /* no SUID
   */
  if (getuid() != geteuid())
    {
      setuid(getuid());
    }

  /* reset umask 
   */
  umask(0);


  astate[0] = programkey_0[0];
  astate[1] = programkey_0[1];
  astate[2] = programkey_0[2];

  taus_set_from_state (cstate, astate);

  out_len = (unsigned long) programlen_compressed_0;
  len     = (unsigned long) programlen_0;
  in_len  = len;

  outbuf  = program_0;

  /* Decode.
   */
  for (i = 0; i < out_len; ++i)
    {
      outbuf[i] ^= (taus_get_long (cstate) & 0xff);
    }


  inbuf  = (lzo_byte *) malloc (sizeof(lzo_byte) * len);


  /*
   * Step 1: initialize the LZO library
   */
  if (lzo_init() != LZO_E_OK)
    {
      return 1;
    }

  /*
   * Step 2: decompress again, now going from `out' to `in'
   */
  r = lzo1x_decompress_safe (outbuf, out_len, inbuf, &in_len, NULL);

  if (r == LZO_E_OK && in_len == len)
    {
      /*
      printf("decompressed %lu bytes back into %lu bytes\n",
	     (long) out_len, (long) in_len);
      */
      ;
    }
  else
    {
      /*
      printf("internal error - decompression failed: %d\n", 
	     r);
      */
      return 2;
    }

  /*
   * Step 3: choose a filename
   */

 nameIt:

  p  = fname;

  /* --- use /tmp if the sticky bit is set ---
   */
#if defined(S_ISVTX)

  set4 (p, '/', 't', 'm', 'p');
  p += 4;
  *p = '\0';

  if ( 0 != stat(fname, &sbuf))
    {
      if ( (sbuf.st_mode & S_ISVTX) != S_ISVTX)
	{
	  p  = fname;
	  set4 (p, '/', 'u', 's', 'r');
	  p += 4;
	  set4 (p, '/', 'b', 'i', 'n');
	  p += 4;
	}
    }

#else

  set4 (p, '/', 'u', 's', 'r');
  p += 4;
  set4 (p, '/', 'b', 'i', 'n');
  p += 4;

#endif

  set4 (p, '/', 't', 'm', 'p');

  p += 4;

  cstate[0] ^= (UINT32) getpid ();
  cstate[1] ^= (UINT32) time (NULL);
  cstate[0] |= (UINT32) 0x03;
  cstate[1] |= (UINT32) 0x09;

  pid = (UINT32) (taus_get_long (cstate) ^ taus_get_long (cstate));

  for (i = 0; i < 4; ++i)
    {
      *p = 'a' + (pid % 26);
      pid /= 26;
      ++p;
    }

  pid = (UINT32) (taus_get_long (cstate) ^ taus_get_long (cstate));

  for (i = 0; i < 4; ++i)
    {
      *p = 'a' + (pid % 26);
      pid /= 26;
      ++p;
    }

  pid = (UINT32) (taus_get_long (cstate) ^ taus_get_long (cstate));

  for (i = 0; i < 3; ++i)
    {
      *p = 'a' + (pid % 26);
      pid /= 26;
      ++p;
    }
  *p = '\0';

  if ( (-1) != stat(fname, &sbuf) || errno != ENOENT)
    {
      /* because cstate[2] is not initialized, the next name will
       * be different
       */
      goto nameIt;
    } 
      
  if ((file = open (fname, O_CREAT|O_EXCL|O_WRONLY, 0700)) < 0)
    {
      return (4);
    } 

  result = (long) write(file, inbuf, in_len);
  if (result < 0 || in_len != (lzo_uint) result)
    {
      return (5);
    }

#if defined(__linux__)

  if ( 0 != fstat(file, &sbuf))
    {
      return (6);
    } 
  
  /* Must reopen for read only.
   */
  close(file);
  file = open (fname, O_RDONLY, 0);

  if ( 0 != fstat(file, &fbuf))
    {
      return (7);
    } 

  /* check mode, inode, owner, and device, to make sure it is the same file
   */
  if (sbuf.st_mode != fbuf.st_mode || 
      sbuf.st_ino  != fbuf.st_ino  || 
      sbuf.st_uid  != fbuf.st_uid  ||
      sbuf.st_gid  != fbuf.st_gid  ||
      sbuf.st_dev  != fbuf.st_dev )
    {
      close  ( file );
      return ( 8 );
    }
  
  p = pname;
  set4(p, '/', 'p', 'r', 'o');
  p += 4;

  set2(p, 'c', '/');
  p += 2;

  set4(p, 's', 'e', 'l', 'f');
  p += 4;

  set4(p, '/', 'f', 'd', '/');
  p += 4;

  sprintf(p, "%d", file);


  if (0 == access(pname, R_OK|X_OK))
    {
      unlink (fname);
      fcntl  (file, F_SETFD, FD_CLOEXEC);
      execve (pname, argv, environ); 
      return (9);
    }
#endif

  /* /proc not working, or not linux
   */
  close (file);

  if ( (i = fork()) != 0) 
    {
      wait   (NULL);
      execve (fname, argv, environ);
      unlink (fname);
      return (10);
    } 
  else if (i == 0)
    {
      if (0 == fork())
	{
	  sleep  (3);
	  unlink (fname);
	}
      return (0);
    }

  /* only reached in case of error 
   */
  unlink (fname);
  return (-1);
}