From 86ad1f3220a99a1f84e2695386b34656d25b36a4 Mon Sep 17 00:00:00 2001 From: Parker Date: Tue, 27 Sep 2022 17:31:57 -0500 Subject: [PATCH] Complete --- .env.example | 8 ++++++ .gitignore | 1 + LICENSE | 24 +++++++++++++++++ README.md | 16 ++++++++++++ main.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 123 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 main.py diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..238a44d --- /dev/null +++ b/.env.example @@ -0,0 +1,8 @@ +BITWARDEN_CLIENT_ID= +BITWARDEN_CLIENT_SECRET= +BITWARDEN_MASTER_PASSWORD= +SKIP_ACCOUNTS= +TWILIO_ACCOUNT_SID= +TWILIO_AUTH_TOKEN= +TWILIO_FROM_NUMBER= +MY_PHONE_NUMBER= \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..66f5979 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/README.md b/README.md new file mode 100644 index 0000000..362cd10 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Bitwarden Password Checker (UNIX Systems ONLY) + +## Uses the Bitwarden-cli tool - available [here](https://bitwarden.com/help/cli/) -, the Twilio API, and the HaveIBeenPwned API in order to check the passwords in your vault against the SHA-1 hashes of compromised passwords. If a compromised password is found, it alerts you with a text message. + +

+ +## In order to setup everything, you must enter all of the necessary information into a .env file. Please note, for the `SKIP_ACCOUNTS` field, account names should be separated by a comma. If you would not like to skip any accounts, simply do not fill in this field, however, do not delete the entry. +## Also, make sure to install all of the necessary dependencies (pexpect, twilio, python-dotenv, requests) + +

+ +## Also note, your passwords are NEVER exposed to the public HaveIBeenPwned API. Your passwords are taken in plain text from your Bitwarden vault and turned to SHA-1 hashes. Then, ONLY the first 5 characters of the hash are sent to the API, which returns all hashes with the matching characters. This list is then checked against the full hashes stored in a python list. + +

+ +### Side note on the dependency of UNIX systems. This is because of the need for the pexpect module, which is used to login to your Bitwarden account with your API credentials. The functions which are used are only available for use on UNIX based systems. \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..fb5b654 --- /dev/null +++ b/main.py @@ -0,0 +1,74 @@ +from subprocess import Popen, PIPE +from dotenv import load_dotenv +from twilio.rest import Client +import pexpect +import hashlib +import requests +import json +import os + +load_dotenv() + +bitwarden_client_id = os.getenv('BITWARDEN_CLIENT_ID') +bitwarden_client_secret = os.getenv('BITWARDEN_CLIENT_SECRET') +bitwarden_master_password = os.getenv('BITWARDEN_MASTER_PASSWORD') +skip_accounts = os.getenv('SKIP_ACCOUNTS').split(',') +twilio_account_sid = os.getenv('TWILIO_ACCOUNT_SID') +twilio_auth_token = os.getenv('TWILIO_AUTH_TOKEN') +twilio_from_number = os.getenv('TWILIO_FROM_NUMBER') +my_phone_number = os.getenv('MY_PHONE_NUMBER') + +for acc in skip_accounts: + location = skip_accounts.index(acc) + skip_accounts[location] = acc.upper() + +try: + child = pexpect.spawn('bw login --apikey') + child.expect('client_id.*') + child.sendline(bitwarden_client_id) + child.expect('client_secret.*') + child.sendline(bitwarden_client_secret) + child.readlines() +except pexpect.exceptions.EOF: + pass + +proc = Popen(['bw','unlock'], stdin=PIPE, encoding='utf-8') +proc.communicate(input=bitwarden_master_password) + +proc = Popen(['bw','list','items'], stdin=PIPE, stdout=PIPE, encoding='utf-8') +proc.communicate(input=bitwarden_master_password) +stdout, stderr = proc.communicate() + +data = json.loads(stdout.encode('utf-8')) + +compromised_passwords = [] + +for i in data: + try: + name = i['name'] + password = i['login']['password'] + hashed_pass = hashlib.sha1(password.encode('utf-8')).hexdigest() + check_api = requests.get('https://api.pwnedpasswords.com/range/' + hashed_pass[:5]) + + if hashed_pass[5:].upper() in check_api.text and name.upper() not in skip_accounts: + compromised_passwords.append({name: password}) + + except KeyError: + pass + +if compromised_passwords: + num = len(compromised_passwords) + message = f"{len(compromised_passwords)} of your passwords {'has' if num==1 else 'have'} been compromised. Please change the passwords for the following accounts.\n\n" + + for entry in compromised_passwords: + for key, value in entry.items(): + message += f"Account: {key}\nPassword: {value}\n\n" + + client = Client(twilio_account_sid, twilio_auth_token) + + message = client.messages \ + .create( + body=message, + from_=twilio_from_number, + to=my_phone_number + ) \ No newline at end of file