Skip to content

Commit

Permalink
Escape smart quotes in JSON output. Error handling in submit_online.
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-unwin committed Apr 3, 2024
1 parent e98033c commit 745d2a4
Showing 1 changed file with 130 additions and 101 deletions.
231 changes: 130 additions & 101 deletions other/audit_windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ echo "Not ready for use"
exit

param (
[int]$debugging = 1
[int]$debugging = 1,
[string]$url = "http://localhost/open-audit/index.php/input/devices",
[string]$create_file = "n",
[string]$submit_online = "y"
)

$debug = $debugging

$timer = [Diagnostics.Stopwatch]::StartNew()
Expand Down Expand Up @@ -61,12 +65,12 @@ if ($result.sys.os_name -like "*2000*") { $result.sys.os_family = "Windows 2000"
if ($result.sys.os_name -like "* XP*") { $result.sys.os_family = "Windows XP" }
if ($result.sys.os_name -like "*2003*") { $result.sys.os_family = "Windows 2003" }
if ($result.sys.os_name -like "*Vista*") { $result.sys.os_family = "Windows Vista" }
if ($result.sys.os_name -like "*2008*") { $result.sys.os_family = "Windows 2008" }
if ($result.sys.os_name -like "*Windows 7*") { $result.sys.os_family = "Windows 7" }
if ($result.sys.os_name -like "*Windows 8*") { $result.sys.os_family = "Windows 8" }
if ($result.sys.os_name -like "*2012*") { $result.sys.os_family = "Windows 2012" }
if ($result.sys.os_name -like "*Windows 10*") { $result.sys.os_family = "Windows 10" }
if ($result.sys.os_name -like "*Windows 11*") { $result.sys.os_family = "Windows 11" }
if ($result.sys.os_name -like "*2008*") { $result.sys.os_family = "Windows 2008" }
if ($result.sys.os_name -like "*2012*") { $result.sys.os_family = "Windows 2012" }
if ($result.sys.os_name -like "*2016*") { $result.sys.os_family = "Windows 2016" }
if ($result.sys.os_name -like "*2019*") { $result.sys.os_family = "Windows 2019" }
if ($result.sys.os_name -like "*2022*") { $result.sys.os_family = "Windows 2022" }
Expand Down Expand Up @@ -1747,103 +1751,6 @@ if ($debug -gt 0) {
}


# $result.server = @()
# Clear-Variable -name item
# $item = @{}
# $item.version = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\CurrentVersion\CSDVersion -ErrorAction Ignore

# if ($item.version -eq "" -or $item.version -eq $null) {
# $item.version = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\CurrentVersion\CurrentVersion -ErrorAction Ignore
# }

# if ($item.version -eq "" -or $item.version -eq $null) {
# $item.version = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSQLSERVER\CurrentVersion\CurrentVersion" -ErrorAction Ignore
# }

# if ($item.version -eq "" -or $item.version -eq $null) {
# $item.version = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\SQLEXPRESS\MSSQLSERVER\CurrentVersion\CurrentVersion" -ErrorAction Ignore
# }

# if ($item.version -ne "" -and $item.version -ne $null) {
# $item.edition = ""

# # SQL 2014
# $item.edition = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.MSSQLSERVER\Setup\Edition" -ErrorAction Ignore

# # SQL 2008 R2
# if ($item.edition -eq "" -or $item.edition -eq $null) {
# $item.edition = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\Setup\Edition" -ErrorAction Ignore
# }

# # SQL 2008
# if ($item.edition -eq "" -or $item.edition -eq $null) {
# $item.edition = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.MSSQLSERVER\Setup\Edition" -ErrorAction Ignore
# }

# # SQL 2005
# if ($item.edition -eq "" -or $item.edition -eq $null) {
# $item.edition = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\Setup","Edition" -ErrorAction Ignore
# }

# # SQL 2000
# if ($item.edition -eq "" -or $item.edition -eq $null) {
# $item.edition = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Setup\Edition" -ErrorAction Ignore
# }

# # SQL 2000
# if ($item.edition -eq "" -or $item.edition -eq $null) {
# $item.edition = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\MSSQLServer\Setup\Edition" -ErrorAction Ignore
# }

# if ($item.edition -like "*express*") {
# $item.edition = "Express Edition"
# }

# $item.instances = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL\*"

# $loginMode = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\LoginMode"
# if ($loginMode -eq $null -or $loginMode -eq "") {
# $instance = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL\MSSQLSERVER"
# $loginMode = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instance\MSSQLServer\LoginMode"
# }

# if ($loginMode -eq $null -or $loginMode -eq "") {
# $loginMode = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instances[0]\MSSQLServer\LoginMode"
# }

# if ($loginMode -eq $null -or $loginMode -eq "") {
# $loginMode = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSQLServer\LoginMode"
# }

# switch ($loginMode) {
# # If we hit this, because we don't have SQL credentials, we don't enumerate databases
# "0" { $item.login_type = "Allow SQL Server Authentication only" }

# "1" { $item.login_type = "Allow Windows Authentication only" }

# "2" { $item.login_type = "Allow Windows Authentication or SQL Server Authentication" }

# # If we hit this, we don't enumerate databases
# "9" { $item.login_type = "Security type unknown" }

# # If we hit this, we cannot log in to the DB Server, therefore, we don't enumerate databases
# default { $item.login_type = "Unknown" }
# }

# $item.port = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\SuperSocketNetLib\Tcp\TcpPort"
# if ($item.port -eq "" -or $item.port -eq $null) {
# $item.port = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSQLServer\SuperSocketNetLib\Tcp\IPAll\TcpPort"
# }
# if ($item.port -eq "" -or $item.port -eq $null -and $item.edition -like "*express*") {
# $item.port = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\IPAll\TcpPort"
# }
# if ($item.port -eq "" -or $item.port -eq $null -and $item.edition -like "*express*") {
# $item.port = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instances[0]\MSSQLServer\SuperSocketNetLib\Tcp\IPAll\TcpPort"
# }

# $result.server += $item
# }




Expand Down Expand Up @@ -1939,7 +1846,25 @@ if ($debug -gt 0) {
}


$result | ConvertTo-Json | Out-File "output.json"
$result = $result | ConvertTo-Json
$result = $result -replace '[\u2019\u2018]', "'"
$result = $result -replace '[\u201C\u201D]', '\"'

if ($submit_online -eq "y") {
try {
$Response = Invoke-WebRequest -UseBasicParsing "$url" -Method POST -Body "data=$result"
$StatusCode = $Response.StatusCode
} catch {
$StatusCode = $_.Exception.Response.StatusCode.value__
}
if ($debug -gt 0) {
"Submission Status: $StatusCode"
}
}

if ($create_file -eq "y") {
$result | Out-File "output.json"
}

$timer.Stop()
$totalSecs = [math]::Round($timer.Elapsed.TotalSeconds,0)
Expand All @@ -1950,3 +1875,107 @@ if ($debug -gt 0) {








# $result.server = @()
# Clear-Variable -name item
# $item = @{}
# $item.version = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\CurrentVersion\CSDVersion -ErrorAction Ignore

# if ($item.version -eq "" -or $item.version -eq $null) {
# $item.version = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\CurrentVersion\CurrentVersion -ErrorAction Ignore
# }

# if ($item.version -eq "" -or $item.version -eq $null) {
# $item.version = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSQLSERVER\CurrentVersion\CurrentVersion" -ErrorAction Ignore
# }

# if ($item.version -eq "" -or $item.version -eq $null) {
# $item.version = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\SQLEXPRESS\MSSQLSERVER\CurrentVersion\CurrentVersion" -ErrorAction Ignore
# }

# if ($item.version -ne "" -and $item.version -ne $null) {
# $item.edition = ""

# # SQL 2014
# $item.edition = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.MSSQLSERVER\Setup\Edition" -ErrorAction Ignore

# # SQL 2008 R2
# if ($item.edition -eq "" -or $item.edition -eq $null) {
# $item.edition = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\Setup\Edition" -ErrorAction Ignore
# }

# # SQL 2008
# if ($item.edition -eq "" -or $item.edition -eq $null) {
# $item.edition = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.MSSQLSERVER\Setup\Edition" -ErrorAction Ignore
# }

# # SQL 2005
# if ($item.edition -eq "" -or $item.edition -eq $null) {
# $item.edition = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\Setup","Edition" -ErrorAction Ignore
# }

# # SQL 2000
# if ($item.edition -eq "" -or $item.edition -eq $null) {
# $item.edition = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Setup\Edition" -ErrorAction Ignore
# }

# # SQL 2000
# if ($item.edition -eq "" -or $item.edition -eq $null) {
# $item.edition = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\MSSQLServer\Setup\Edition" -ErrorAction Ignore
# }

# if ($item.edition -like "*express*") {
# $item.edition = "Express Edition"
# }

# $item.instances = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL\*"

# $loginMode = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\LoginMode"
# if ($loginMode -eq $null -or $loginMode -eq "") {
# $instance = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL\MSSQLSERVER"
# $loginMode = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instance\MSSQLServer\LoginMode"
# }

# if ($loginMode -eq $null -or $loginMode -eq "") {
# $loginMode = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instances[0]\MSSQLServer\LoginMode"
# }

# if ($loginMode -eq $null -or $loginMode -eq "") {
# $loginMode = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSQLServer\LoginMode"
# }

# switch ($loginMode) {
# # If we hit this, because we don't have SQL credentials, we don't enumerate databases
# "0" { $item.login_type = "Allow SQL Server Authentication only" }

# "1" { $item.login_type = "Allow Windows Authentication only" }

# "2" { $item.login_type = "Allow Windows Authentication or SQL Server Authentication" }

# # If we hit this, we don't enumerate databases
# "9" { $item.login_type = "Security type unknown" }

# # If we hit this, we cannot log in to the DB Server, therefore, we don't enumerate databases
# default { $item.login_type = "Unknown" }
# }

# $item.port = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\SuperSocketNetLib\Tcp\TcpPort"
# if ($item.port -eq "" -or $item.port -eq $null) {
# $item.port = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSQLServer\SuperSocketNetLib\Tcp\IPAll\TcpPort"
# }
# if ($item.port -eq "" -or $item.port -eq $null -and $item.edition -like "*express*") {
# $item.port = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\IPAll\TcpPort"
# }
# if ($item.port -eq "" -or $item.port -eq $null -and $item.edition -like "*express*") {
# $item.port = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instances[0]\MSSQLServer\SuperSocketNetLib\Tcp\IPAll\TcpPort"
# }

# $result.server += $item
# }



0 comments on commit 745d2a4

Please sign in to comment.