Created
March 10, 2020 19:21
-
-
Save fuji246/a82718fb189ad7cb3cfbb6b296def546 to your computer and use it in GitHub Desktop.
wpa_passphrase replacement that supports unicode and space in SSID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# coding=utf8 | |
# modified from https://github.com/SwordYork/NetworkTool/blob/master/WPA-PSK/TKIP/psk.py | |
# alternatively, you can refer https://github.com/lomorage/pi-gen/blob/lomorage/stage2/02-net-tweaks/files/wifi_switch.sh#L12 | |
# and use wpa_passphrase, and then replace ssid with hex binary of the SSID | |
import hmac | |
import hashlib | |
import struct | |
import argparse | |
from operator import xor | |
from itertools import izip, starmap | |
################################# | |
##### https://github.com/mitsuhiko/python-pbkdf2/blob/master/pbkdf2.py | |
################################# | |
def pbkdf2_hex(data, salt, iterations=1000, keylen=24, hashfunc=None): | |
"""Like :func:`pbkdf2_bin` but returns a hex encoded string.""" | |
return pbkdf2_bin(data, salt, iterations, keylen, hashfunc).encode('hex') | |
def pbkdf2_bin(data, salt, iterations=1000, keylen=24, hashfunc=None): | |
"""Returns a binary digest for the PBKDF2 hash algorithm of `data` | |
with the given `salt`. It iterates `iterations` time and produces a | |
key of `keylen` bytes. By default SHA-1 is used as hash function, | |
a different hashlib `hashfunc` can be provided. | |
""" | |
hashfunc = hashfunc or hashlib.sha1 | |
mac = hmac.new(data, None, hashfunc) | |
def _pseudorandom(x, mac=mac): | |
h = mac.copy() | |
h.update(x) | |
return map(ord, h.digest()) | |
buf = [] | |
fmt = '>{0}sI'.format(len(salt)) | |
for block in xrange(1, -(-keylen // mac.digest_size) + 1): | |
rv = u = _pseudorandom(struct.pack(fmt, salt, block)) | |
for i in xrange(iterations - 1): | |
u = _pseudorandom(''.join(map(chr, u))) | |
rv = starmap(xor, izip(rv, u)) | |
buf.extend(rv) | |
return ''.join(map(chr, buf))[:keylen] | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("ssid") | |
parser.add_argument("passphrase") | |
args = parser.parse_args() | |
ssid_bytes = "".join("{:02x}".format(ord(c)) for c in args.ssid) | |
psk = pbkdf2_hex(args.passphrase, args.ssid,4096,256)[:64] | |
output = ''' | |
network = { | |
ssid=%s | |
psk=%s | |
} | |
''' % (ssid_bytes, psk) | |
print(output) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment