An example of authenticating against a database [Powershell]
Posted: Mon Nov 07, 2022 11:34 pm
Wrote a script I thought would be cool to share, authenticate users against a MySql/MariaDB database, requires the MariaDB or MySql ODBC connector
config:
auth.ps1
Hashing passwords would be ideal, this is just for simplicity's sake, will be doing php next with bcrypt
config:
Code: Select all
auth-user-pass-verify 'C:/PROGRA~1/Powershell/7/pwsh.exe C:/SCRIPTS/OpenVPN/auth.ps1' via-file
Code: Select all
# Script requires ODBC Connector MySql/MariaDB
$data = Get-Content "$args"
$username = $data[0]
$password = $data[1]
#connection - pwsh.exe Get-OdbcDriver
$driver = "MariaDB ODBC 3.1 Driver"
$Server = "127.0.0.1"
$DBName = "openvpn"
$User = "openvpn"
$PW = "abcd1234"
# Connect to the database
$Connection = New-Object System.Data.ODBC.ODBCConnection
$Connection.connectionstring = "DRIVER={$driver};" +
"Server = $Server;" +
"Database = $DBName;" +
"UID = $User;" +
"PWD= $PW;" +
"Option = 3"
$Connection.Open()
$Query = "SELECT * FROM openvpn.users WHERE password = `"$password`" AND user = `"$username`""
$Command = New-Object System.Data.ODBC.ODBCCommand($Query, $Connection)
$DataTable = New-Object -TypeName System.Data.DataTable
$Reader = $Command.ExecuteReader()
$DataTable.Load($Reader)
if (@($DataTable).count -gt 0) { (Write-Host "ok"); exit 0 }
$Connection.Dispose()
Write-Host "not ok"
exit 1