Guest User

Untitled

a guest
May 25th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import sys
  4. from math import factorial
  5.  
  6. if len(sys.argv) == 1 or '-h' in sys.argv or '--help' in sys.argv:
  7.     print('''
  8. Usage:
  9.  {self} <fingerprint-length> <number-of-attacked-bits> <bits-checked>
  10.  
  11. This tool calculates the likelihood, given the current settings,
  12. for an attacker to get away with a forged fingerprint.
  13.  
  14. For example, assuming an attacker can brute force 72 bits of your 128-bit
  15. fingerprint and you check a random block of 32 bits:
  16.    {self} 128 72 32
  17. '''.lstrip().format(self=sys.argv[0]))
  18.     exit(1)
  19.  
  20. def choose(x, y):
  21.     # combinatorial 'x choose y' algorithm
  22.     return factorial(x) / (factorial(x - y) * factorial(y))
  23.  
  24. # fingerprint length in bits,
  25. # attacker bits (how many bits can an attacker forge),
  26. # shown bits (how many bits are randomly checked)
  27. fpbits, attbits, shownbits = map(int, sys.argv[1:])
  28.  
  29. if attbits >= fpbits:
  30.     print('If the attacker can brute force the whole fingerprint, there is no point'
  31.         + ' verifying fingerprints.')
  32.     exit(0)
  33.  
  34. if shownbits > attbits:
  35.     print(('Attacker can only attack {} bits and you check {} bits. An attacker can '
  36.         + 'never mount an attack.').format(attbits, shownbits))
  37.  
  38. c = shownbits
  39. n = fpbits
  40. m = attbits
  41. s = 2  # alphabet is binary
  42.  
  43. result = 0
  44. for d in range(999):
  45.     if d <= m and d <= c and 0 <= c-d <= n-m:
  46.         print('d={} is acceptable'.format(d))
  47.     else:
  48.         continue
  49.     p1 = choose(m, d)
  50.     p2 = choose(n-m, c-d)
  51.     p3 = (1 / pow(s, c-d))
  52.     result += p1 * p2 * p3
  53.  
  54. result = 1/choose(n, c) * result
  55.  
  56. print('Prob(successful attack) =', result * 100, '%')
  57.  
  58. result = 1/result
  59. if result > 1e7:
  60.     result = str(round(result/1e6)) + 'M'
  61. elif result > 1e4:
  62.     result = str(round(result/1e3)) + 'k'
  63. elif result > 10:
  64.     result = round(result)
  65.  
  66. print('Or 1 in', result)
Advertisement
Add Comment
Please, Sign In to add comment