Changes in trunk/src/samhain_setpwd.c [22:1]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/samhain_setpwd.c
r22 r1 1 1 #include "config_xor.h" 2 3 #ifdef HAVE_BROKEN_INCLUDES 4 #define _ANSI_C_SOURCE 5 #define _POSIX_SOURCE 6 #endif 2 7 3 8 #include <stdio.h> … … 8 13 #include <unistd.h> 9 14 #include <sys/types.h> 10 #include <sys/wait.h>11 15 #include <sys/stat.h> 12 16 #include <fcntl.h> 13 #include <errno.h>14 #include <sys/time.h>15 17 #include <time.h> 16 18 17 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD)18 #include <sched.h>19 #endif20 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 #endif28 29 #define TAUS_MAX 4294967295UL30 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 }135 19 136 20 #ifdef SH_STEALTH … … 228 112 229 113 char * newn; 230 size_t nlen;231 114 int oldf; 232 115 int newf; … … 313 196 (void) umask (0); 314 197 315 taus_seed();198 srand(time(NULL) ^ getpid()); 316 199 317 200 bytecount = 0; … … 323 206 oldf = open(argv[1], O_RDONLY); 324 207 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'; 208 newn = (char *) malloc (strlen(argv[1])+strlen(argv[2])+2); 209 strcpy(newn, argv[1]); 210 strcat(newn, "."); 211 strcat(newn, argv[2]); 330 212 newf = open(newn, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU); 331 213 … … 383 265 (unsigned char) *found_it); 384 266 385 ccd = (unsigned char) (256.0 * (taus_get()/(TAUS_MAX+1.0)));267 ccd = (unsigned char) (256.0 * rand()/(RAND_MAX+1.0)); 386 268 sprintf(&newpwd[i*2], _("%02x"), 387 269 (unsigned char) ccd); … … 458 340 (unsigned char) *found_it); 459 341 460 ccd = (unsigned char) (256.0 * taus_get()/(TAUS_MAX+1.0));342 ccd = (unsigned char) (256.0 * rand()/(RAND_MAX+1.0)); 461 343 sprintf(&newpwd[i*2], _("%02x"), 462 344 (unsigned char) ccd);
Note:
See TracChangeset
for help on using the changeset viewer.