SHARE
TWEET

wpa_passphrase with pure python

DeaD_EyE Sep 11th, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. from argparse import ArgumentParser
  3. from binascii import hexlify
  4. from hashlib import pbkdf2_hmac
  5.  
  6.  
  7. CONFIG = """
  8. ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
  9. update_config=1
  10. network={{
  11.        ssid="{ssid}"
  12.        #psk="{password}"
  13.        psk={psk}
  14.        proto=RSN
  15.        key_mgmt=WPA-PSK
  16.        pairwise=CCMP
  17.        group=CCMP
  18. }}
  19. """.lstrip()
  20.  
  21.  
  22. def gen_psk(ssid, password):
  23.     password, ssid = map(str.encode, (password, ssid))
  24.     hash = pbkdf2_hmac('sha1', password, ssid, 4096, 32)
  25.     return hexlify(hash).decode()
  26.  
  27.  
  28. def main(ssid, password):
  29.     if not sys.stdin.isatty() and password:
  30.         print('Which password should I take? From stdin or the argument?', file=sys.stderr)
  31.         return 2
  32.     if not sys.stdin.isatty() and not password:
  33.         password = sys.stdin.readline().rstrip()
  34.     try:
  35.         psk = gen_psk(ssid, password)
  36.         print(CONFIG.format(ssid=ssid, password=password, psk=psk))
  37.     except Exception as e:
  38.         print(e, file=sys.stderr)
  39.         return 1
  40.  
  41.  
  42. if __name__ == '__main__':
  43.     parser = ArgumentParser()
  44.     parser.add_argument('ssid', help='SSID of hotspot')
  45.     parser.add_argument('password', default='', nargs='?', help='Password of hotspot')
  46.     args = parser.parse_args()
  47.     retval = main(**vars(args))
  48.     sys.exit(retval)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top