Skip to content

Instantly share code, notes, and snippets.

@victor141516
Created April 25, 2018 15:44
Show Gist options
  • Save victor141516/7f73b637f77df4157a8ca968544f3550 to your computer and use it in GitHub Desktop.
Save victor141516/7f73b637f77df4157a8ca968544f3550 to your computer and use it in GitHub Desktop.
Change macOS password using a one-time-password. You can use any OTP app like GAuthenticator or Authy. It does send both old and new password to Telegram on every password change.
import os
import pyotp
import requests
import subprocess
import time
CHANNEL_NAME = ''
BOT_TOKEN = ''
CURRENT_PASSWORD = None # Write here your current password if it's the first time using this
def replace_password(old_password, new_password):
exit_code = subprocess.call(['dscl', '.', 'passwd', '/Users/{}'.format(os.getlogin()), old_password, new_password])
if exit_code is 0:
return True
if exit_code is 10:
return False
try:
secret = open('secret').read().split('\n')[1]
except IOError as e:
if e[0] is not 2:
raise e
secret = pyotp.random_base32()
open('secret', 'w').write(secret)
print('Your secret is {}'.format(secret))
totp = pyotp.TOTP(secret)
password_file = open('password', 'w')
new_password = totp.now()
if CURRENT_PASSWORD is not None:
if replace_password(CURRENT_PASSWORD, new_password):
print('Your new password is {}'.format(new_password))
while True:
if not totp.verify(new_password):
old_password = new_password
password_file.write(old_password + '\n')
new_password = totp.now()
password_file.write(new_password)
password_file.seek(0)
print('Replacing password for {}'.format(new_password))
requests.get('https://api.telegram.org/{}/sendMessage?chat_id=@{}&text=OLD {}'.format(BOT_TOKEN, CHANNEL_NAME, old_password))
requests.get('https://api.telegram.org/{}/sendMessage?chat_id=@{}&text=NEW {}'.format(BOT_TOKEN, CHANNEL_NAME, new_password))
if replace_password(old_password, new_password):
print('!!Replaced password for {}'.format(new_password))
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment