Advertisement
bolkin

fake_entropy.py

Feb 13th, 2012
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os,fcntl,struct
  4.  
  5. """
  6. cat > test.c <<DEL
  7. #include <stdio.h>
  8. #include <linux/random.h>
  9. main() { printf("%u\n",RNDADDENTROPY);}
  10. DEL
  11. gcc test.c -o a.out; ./a.out
  12. """
  13. RNDADDENTROPY = 1074287107
  14. entropy_dev = '/proc/sys/kernel/random/entropy_avail'
  15.  
  16. def write_rand(block_size,bc,entropy_ratio):
  17.         fd = os.open("/dev/random", os.O_WRONLY)
  18.         fakefd = os.open("/dev/urandom", os.O_RDONLY)
  19.  
  20.         fmt = 'ii%is' % (block_size)
  21.  
  22.         rnd = os.read(fakefd,block_size)
  23.         os.close(fakefd)
  24.         this = struct.pack(fmt, entropy_ratio * len(rnd), len(rnd), rnd)
  25.  
  26.         #refeeding the same block, nooooooo
  27.         for _ in xrange(bc):
  28.                 fcntl.ioctl(fd, RNDADDENTROPY, this)
  29.  
  30.         os.close(fd)
  31.  
  32. def read(s):
  33.         f = open(s,'r')
  34.         rv = f.read()
  35.         f.close()
  36.         return rv
  37.  
  38. expect = 512
  39.  
  40. try:
  41.         before = int(read(entropy_dev))
  42.         while True:
  43.                 write_rand(512,10,1)
  44.                 after = int(read(entropy_dev))
  45.                 print after
  46.                 if after>=expect: break
  47.  
  48.         print 'Entropy was %s, now %s' % (before,after)
  49.         print 'Your crypto karma just dropped below zero, Bruce Schneier hates you.'
  50. except Exception, e:
  51.         print e
  52.         print 'You probably forgot to sudo'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement