Importing passwords from csv via command line

Hello! I’m attempting to build a password generator script that can automatically update my stored Brave passwords upon execution. I haven’t finished completely searching my options yet, I thought I would ask the community if they have any ideas. I just discovered you can export your passwords to csv, and import them from csv via an optional feature. I am wondering if there’s a command line feature that can import the csv I can implement into my script.

This is my current unfinished script:

#!usr/bin/python3

import random
import sys
import os
from datetime import datetime
import hashlib
import csv

path = str(os.path.expanduser('~') + '/Sync/Credentials.txt')
logpath = str(os.path.expanduser('~') + '/pwmanager.log')
csvpath = str(os.path.expanduser('~') + '/Brave Passwords.csv')
csvpath2 = str(os.path.expanduser('~') + '/Brave Passwords2.csv')


if len(sys.argv) < 2:
    Service = input("What is this password for?\nEnter Service: ")
    Account_Name = input("What is your Account name?\nEnter Account name: ")
    Update = input("Update your browser csv file?\nEnter yes or no: ")
else:
    Service = sys.argv[1]
    Account_Name = sys.argv[2]
    Update = sys.argv[3]


Header = 'Service : Account Name : Password\n'
Chars = 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*_+-'
string = ''

while len(string) <= 35:
	string += Chars[random.randint(0,len(Chars)-1)]

entry = str(Service + ':' + Account_Name + ':' + string)
hashed_entry = hashlib.sha512(entry.encode('utf-8)'))

try:
    with open(path,'r') as file:
        lines = file.readlines()
except:
    print('[+]Credentials file doesn\'t exist')
    with open(path,'w') as file:
        file.write(Header)
    with open(path,'r') as file:
        lines = file.readlines()

## Checking to see if Service exists already: If so, rewrite the entry
with open(path,'w+') as file:
    for line in lines:

        if Service.lower() in line.lower():
            if Account_Name.lower() == line.split(':')[1].lower():
                sys.stdout = open(logpath,'a')
                deleted_hash = hashlib.sha512(line.encode('utf-8)'))
                print('\n' + str(datetime.now()) + ' Deleting: ',line)
                continue
            else:
                file.write(line)
        elif line == '\n':
            continue
        else:
            file.write(line)
            
with open(path,'a') as file:
    file.write('\n')
    file.write(entry)

sys.stdout = open(logpath,'a')
print('\n' + str(datetime.now()) + ' Inserting: ',hashed_entry.hexdigest())

if Service == 'Microsoft':
    Service = 'live'

if Update == 'no' or Update == 'No':
    None
    
elif Update == 'yes' or Update == 'Yes':
    with open(csvpath, 'r') as f:
        csv_lines = csv.reader(f, delimiter=',')
        with open(csvpath2,'w',newline='') as f:
            writer = csv.writer(f)
            for line in csv_lines:
                if Service.lower() in line[1]:
                    if Account_Name.lower() == line[2].lower():
                        line[3] = string
                        new_line = line
                        continue
                    elif Account_Name.lower() != line[2].lower():
                        new_line = [line[0],line[1],Account_Name.lower(),string]
                        writer.writerow(line)
                else:
                    writer.writerow(line)
            writer.writerow(new_line)

    with open(csvpath2, 'r') as f:
        csv_lines = csv.reader(f, delimiter=',')
        with open(csvpath,'w',newline='') as f:
            writer = csv.writer(f)
            for line in csv_lines:
                writer.writerow(line)
                
    os.remove(csvpath2)
                    
                        
    

Unfortunately, I don’t think there’s a quick answer. I looked and wasn’t able to find anything which would actually read the CSV via command line. I only found things to enable the feature - which still require the user to use the interface and do the import. For example:

However, there is something you can try. The logins are all stored in a sqlite database. You can open the profile directory (ex: on Windows, this is %USERPROFILE%\AppData\Local\BraveSoftware\Brave-Browser\User Data) and then go to the profile in question and open the Login Data file.

There should be decent sqlite bindings for Python; if not, you can always open a pipe and interact with the sqlite executable. When you retrieve the results, the passwords will be encrypted using Chromium’s OS crypt.

You can see what operations take place (different for each platform; this is the Windows one) here:
https://source.chromium.org/chromium/chromium/src/+/main:components/os_crypt/os_crypt_win.cc?q=CryptEncrypt&ss=chromium

2 Likes