Python Authentication – Ahazu.com

Python Authentication

A couple of ways to store and recall credentials and authenticate. As well as using said credentials to authenticate with websites and SQL servers.

Fetch from Microsoft Credentials Manager

import wincred 
#(can be found here: https://gist.github.com/mrh1997/717b14f5783b49ca14310419fa7f03f6)

# Fetch a tuple with username and password 
username, password = wincred.GetGenericCredential('CredentialName')

Fetch from Keepass

from pykeepass import PyKeePass

kp = PyKeePass(r'C:\path\to\Database.kdbx', password=password, keyfile=r'C:\path\to\Database.key')

entry = kp.find_entries(title='PasswordEntryTitle', first=True)
username = entry.username
password = entry.password

Authenticate with Integrated Windows

import requeusts
from requests_negotiate_sspi import HttpNegotiateAuth

r = requests.get('http://www.target.com', auth=HttpNegotiateAuth())

POSTing to a website that requires authentication

import requests

payload = {
    'username : username,
    'password' : password
}

r.requests.post("http://www.target.com", data=payload)

Authenticating with SQL servers using Windows Authentication

import pyodbc

cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
    r"Server=HOSTNAME\INSTANCE_NAME;"
    "Database=DB_NAME;"
    "Trusted_Connection=yes;")

Autenticating with SQL servers using a username and password

import pyodbc

cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
    r"Server=HOSTNAME\INSTANCE_NAME;"
    "Database=DB_NAME;"
    "UID="+username+";"
    "PWD="+password+";")

Leave a comment

Your email address will not be published. Required fields are marked *