Last active
January 25, 2023 21:14
-
-
Save 0xpizza/83c645bcfe5c050f4e2c677392bfdb7b to your computer and use it in GitHub Desktop.
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
import base64 | |
import os | |
import pathlib | |
import sys | |
import qrcode | |
def gen_constr(ssid:bytes, psk:bytes, hidden:bool=False) -> bytes: | |
"""https://en.wikipedia.org/wiki/QR_code#Joining_a_Wi%E2%80%91Fi_network""" | |
for c in b'\\:;': | |
ssid = ssid.replace(bytes([c]), bytes([92, c])) # 92 = backslash | |
psk = psk.replace(bytes([c]), bytes([92, c])) | |
hidden = b'true' if hidden else b'false' | |
constr = b'WIFI:S:' + ssid + b';T:WPA;P:' + psk + b';H:' + hidden + b';;' | |
return constr | |
def cli(): | |
print('Use CTRL+Z to go back and CTRL+C to exit') | |
print('For binary inputs, start with "hex:"') | |
try: | |
# ssid | |
while True: | |
try: | |
ssid = input('SSID: ') | |
if not ssid: continue | |
except EOFError: | |
continue | |
if ssid.startswith('hex:'): | |
ssid = ssid[4:].strip() | |
try: | |
ssid = bytes.fromhex(ssid) | |
except ValueError: | |
print("That isn't hex") | |
continue | |
print('SSID entered as binary value') | |
else: | |
ssid = ssid.encode() | |
if len(ssid) > 32: | |
print(f'Your SSID is too long ({len(ssid)}) must be 32 or less.') | |
continue | |
# password | |
while True: | |
try: | |
pw = input('Password: ') | |
if not pw: continue | |
except EOFError: | |
break | |
if pw.startswith('hex:'): | |
pw = pw[4:].strip() | |
try: | |
pw = bytes.fromhex(pw) | |
except ValueError: | |
print("That isn't hex") | |
continue | |
print('Password entered as binary value') | |
else: | |
pw = pw.encode() | |
if len(pw) > 63: | |
print(f'Your password is too long ({len(pw)}) must be 63 or less.') | |
continue | |
# hidden | |
while True: | |
try: | |
hidden = input('hidden (y/[n]): ').strip().lower() | |
except EOFError: | |
break | |
if not hidden: | |
print('Hidden set to "no"') | |
hidden = 'n' | |
else: | |
hidden = hidden[0] | |
if hidden not in 'yn': | |
print('Please answer y or n') | |
continue | |
hidden = True if hidden == 'y' else False | |
# build the connection string and show QR image | |
constr = gen_constr(ssid, pw, hidden) | |
print('Your connection string:') | |
print('B64: ', base64.b64encode(constr).decode()) | |
print('UTF8: ', constr.decode('utf8', errors='replace')) | |
print('Latin1:', constr.decode('latin1', errors='replace')) | |
print('\nHere comes your QR Code!!') | |
qrcode.make(constr).save('wifi.png') | |
try: | |
r = os.system('wifi.png') | |
if r != 0: | |
raise Exception | |
except: | |
p = pathlib.Path('wifi.png').absolute() | |
print(f'Well, that didnt\'t work :( Try looking here:\n"{p}"') | |
return | |
except KeyboardInterrupt: | |
return | |
def main(): | |
cli() | |
print('\nDone.', file=sys.stdout) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment