An example of authenticating against a database [Powershell]

Use this forum to share your network setup and what's been working for you.

Moderators: TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech

Post Reply
kimboslice
OpenVpn Newbie
Posts: 3
Joined: Tue Nov 01, 2022 3:57 pm

An example of authenticating against a database [Powershell]

Post by kimboslice » 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:

Code: Select all

auth-user-pass-verify 'C:/PROGRA~1/Powershell/7/pwsh.exe C:/SCRIPTS/OpenVPN/auth.ps1' via-file
auth.ps1

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
Hashing passwords would be ideal, this is just for simplicity's sake, will be doing php next with bcrypt

Post Reply