Advertisement
bmtd

OTP chatbot

Nov 7th, 2016
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. from flag import *
  2. import random, signal, sys
  3.  
  4. def handler(signum, frame):
  5.     print 'Timeout'
  6.     sys.exit(-1)
  7.  
  8. # Set the signal handler and a 60-second alarm
  9. signal.signal(signal.SIGALRM, handler)
  10. signal.alarm(60)
  11.  
  12. class Unbuffered(object):
  13.    def __init__(self, stream):
  14.        self.stream = stream
  15.    def write(self, data):
  16.        self.stream.write(data)
  17.        self.stream.flush()
  18.    def __getattr__(self, attr):
  19.        return getattr(self.stream, attr)
  20.  
  21. sys.stdout = Unbuffered(sys.stdout)
  22. ##############################################
  23.  
  24. greet = ["'sup bro", "hey", "*nods*", "hey you get my snap?"]
  25.  
  26. def egcd(a, b):
  27.     if a == 0:
  28.         return (b, 0, 1)
  29.     else:
  30.         g, y, x = egcd(b % a, a)
  31.         return (g, x - (b // a) * y, y)
  32.  
  33. def invmod(a, m):
  34.     g, x, y = egcd(a, m)
  35.     if g != 1:
  36.         raise Exception('modular inverse does not exist')
  37.     else:
  38.         return x % m
  39.  
  40. def b2n(b):
  41.     return int(b.encode('hex'),16)
  42.  
  43. def randbyte(nbytes):
  44.     import os
  45.     return os.urandom(nbytes)
  46.  
  47. N = pp * qq
  48.  
  49. def sign(x, key): # CRT compute msg ^ d  mod N, faster, saving my energy.
  50.     d = 0x10001
  51.     mp = pow(x, d, pp)
  52.     mq = pow(x, d, qq)
  53.     rp = invmod(pp, qq)
  54.     rq = invmod(qq, pp)
  55.     return (mp * qq * rq + mq * pp * rp) % key
  56.  
  57. def encrypt(x, key): # Encrypt is fast... no need to move my butt on CRT.
  58.     d = 0x10001
  59.     return pow(x, d, key)
  60.  
  61. encrypt_key = b2n(randbyte(100))
  62. # Sending this to you via your token device....
  63.  
  64. sign_key = N ^ encrypt_key
  65. # Originally I use N, but now I use OTP, why not!!!
  66.  
  67. banner = """
  68. Welcome to OTP chatbot 2.0-alpha
  69. You can chat with me, but unless you have the token, you can't read it.
  70. To verify this is really me, you use my public key with the token, this is my newest features!!
  71. Here is my public key: %d
  72. You can try to verify this message first to ensure correctness of your key.
  73. """ % N
  74.  
  75. print banner
  76. print 'Signature code:', sign(b2n(banner), sign_key)
  77.  
  78. while True:
  79.     msg = raw_input("> ")
  80.     if 'FLAG?' in msg:
  81.         res = FLAG
  82.     elif 'hello' in msg or 'hi' in msg or 'good' in msg or 'chao' in msg:
  83.         res = greet[random.randrange(0,len(greet))]
  84.     else:
  85.         res = 'What?!? ' + randbyte(10)
  86.    
  87.     enc = encrypt(b2n(res), encrypt_key)
  88.     print enc
  89.     print 'Signature code:', sign(enc, sign_key)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement