Changes in trunk/src/samhain_setpwd.c [1:22]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/samhain_setpwd.c
r1 r22 1 1 #include "config_xor.h" 2 3 #ifdef HAVE_BROKEN_INCLUDES4 #define _ANSI_C_SOURCE5 #define _POSIX_SOURCE6 #endif7 2 8 3 #include <stdio.h> … … 13 8 #include <unistd.h> 14 9 #include <sys/types.h> 10 #include <sys/wait.h> 15 11 #include <sys/stat.h> 16 12 #include <fcntl.h> 13 #include <errno.h> 14 #include <sys/time.h> 17 15 #include <time.h> 18 16 17 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD) 18 #include <sched.h> 19 #endif 20 21 #if defined(HAVE_INT_32) 22 typedef unsigned int UINT32; 23 #elif defined(HAVE_LONG_32) 24 typedef unsigned long UINT32; 25 #elif defined(HAVE_SHORT_32) 26 typedef unsigned short UINT32; 27 #endif 28 29 #define TAUS_MAX 4294967295UL 30 31 static UINT32 taus_state[3]; 32 33 static UINT32 taus_get () 34 { 35 36 #define TAUSWORTHE(s,a,b,c,d) ((s &c) <<d) ^ (((s <<a) ^s) >>b) 37 taus_state[0] = TAUSWORTHE (taus_state[0], 13, 19, 4294967294UL, 12); 38 taus_state[1] = TAUSWORTHE (taus_state[1], 2, 25, 4294967288UL, 4); 39 taus_state[2] = TAUSWORTHE (taus_state[2], 3, 11, 4294967280UL, 17); 40 return (taus_state[0] ^ taus_state[1] ^ taus_state[2]); 41 } 42 43 static void taus_seed () 44 { 45 unsigned char buf[12]; 46 unsigned char buf2[12]; 47 unsigned char buf3[12]; 48 ssize_t count; 49 size_t nbytes = sizeof(buf); 50 size_t where = 0; 51 52 struct timeval t1, t2; 53 UINT32 delta, k[3]; 54 int i, j; 55 56 int fd = open ("/dev/urandom", O_RDONLY); 57 58 if (fd == -1) 59 { 60 gettimeofday(&t1, NULL); 61 delta = t1.tv_usec; 62 memcpy(&buf[0], &delta, 4); 63 gettimeofday(&t1, NULL); 64 delta = t1.tv_usec; 65 memcpy(&buf[4], &delta, 4); 66 gettimeofday(&t1, NULL); 67 delta = t1.tv_usec; 68 memcpy(&buf[8], &delta, 4); 69 goto second; 70 } 71 72 while (nbytes) { 73 count = read(fd, &buf[where], nbytes); 74 if (count == -1 && errno == EINTR) 75 continue; 76 where += count; 77 nbytes -= count; 78 } while (count == -1 && errno == EINTR); 79 80 close(fd); 81 82 second: 83 for (i = 0; i < 12; ++i) 84 { 85 gettimeofday(&t1, NULL); 86 if (0 == fork()) 87 _exit(EXIT_SUCCESS); 88 wait(NULL); 89 gettimeofday(&t2, NULL); 90 delta = t2.tv_usec - t1.tv_usec; 91 buf2[i] = (unsigned char) delta; 92 } 93 94 for (i = 0; i < 12; ++i) 95 { 96 gettimeofday(&t1, NULL); 97 for (j = 0; j < 32768; ++j) 98 { 99 if (0 == kill (j,0)) 100 k[i % 3] ^= j; 101 } 102 gettimeofday(&t2, NULL); 103 delta = t2.tv_usec - t1.tv_usec; 104 buf3[i] ^= (unsigned char) delta; 105 } 106 107 memcpy(&taus_state[0], &buf3[0], 4); 108 memcpy(&taus_state[1], &buf3[4], 4); 109 memcpy(&taus_state[2], &buf3[8], 4); 110 111 taus_state[0] ^= k[0]; 112 taus_state[1] ^= k[1]; 113 taus_state[2] ^= k[2]; 114 115 memcpy(&k[0], &buf2[0], 4); 116 memcpy(&k[1], &buf2[4], 4); 117 memcpy(&k[2], &buf2[8], 4); 118 119 taus_state[0] ^= k[0]; 120 taus_state[1] ^= k[1]; 121 taus_state[2] ^= k[2]; 122 123 memcpy(&k[0], &buf[0], 4); 124 memcpy(&k[1], &buf[4], 4); 125 memcpy(&k[2], &buf[8], 4); 126 127 taus_state[0] ^= k[0]; 128 taus_state[1] ^= k[1]; 129 taus_state[2] ^= k[2]; 130 131 taus_state[0] |= (UINT32) 0x03; 132 taus_state[1] |= (UINT32) 0x09; 133 taus_state[2] |= (UINT32) 0x17; 134 } 19 135 20 136 #ifdef SH_STEALTH … … 112 228 113 229 char * newn; 230 size_t nlen; 114 231 int oldf; 115 232 int newf; … … 196 313 (void) umask (0); 197 314 198 srand(time(NULL) ^ getpid());315 taus_seed(); 199 316 200 317 bytecount = 0; … … 206 323 oldf = open(argv[1], O_RDONLY); 207 324 208 newn = (char *) malloc (strlen(argv[1])+strlen(argv[2])+2); 209 strcpy(newn, argv[1]); 210 strcat(newn, "."); 211 strcat(newn, argv[2]); 325 nlen = strlen(argv[1])+strlen(argv[2])+2; 326 newn = (char *) malloc (nlen); 327 strncpy(newn, argv[1], nlen); newn[nlen-1] = '\0'; 328 strncat(newn, ".", nlen); newn[nlen-1] = '\0'; 329 strncat(newn, argv[2], nlen); newn[nlen-1] = '\0'; 212 330 newf = open(newn, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU); 213 331 … … 265 383 (unsigned char) *found_it); 266 384 267 ccd = (unsigned char) (256.0 * rand()/(RAND_MAX+1.0));385 ccd = (unsigned char) (256.0 * (taus_get()/(TAUS_MAX+1.0))); 268 386 sprintf(&newpwd[i*2], _("%02x"), 269 387 (unsigned char) ccd); … … 340 458 (unsigned char) *found_it); 341 459 342 ccd = (unsigned char) (256.0 * rand()/(RAND_MAX+1.0));460 ccd = (unsigned char) (256.0 * taus_get()/(TAUS_MAX+1.0)); 343 461 sprintf(&newpwd[i*2], _("%02x"), 344 462 (unsigned char) ccd);
Note:
See TracChangeset
for help on using the changeset viewer.