# PunchThru Remote Desktop check # https://punchthru.com/check/ # # Shows whether this Windows computer's Remote Desktop is being attacked, # using what Windows has already recorded. READ-ONLY: it changes nothing, # installs nothing, and sends nothing anywhere. Everything stays on screen. # # Run in PowerShell as Administrator (the Security log needs it). # Works on Windows Server 2012 R2 and later, and Windows 10/11. function Invoke-PunchThruRdpCheck { $ErrorActionPreference = 'SilentlyContinue' function Say($text, $color) { if ($color) { Write-Host $text -ForegroundColor $color } else { Write-Host $text } } function Head($text) { Write-Host ''; Write-Host $text -ForegroundColor Cyan } function Num($n) { '{0:N0}' -f $n } function NoAddress($ip) { return (-not $ip -or $ip -eq '-') } function IsPublic($ip) { if ((NoAddress $ip) -or $ip -eq '::1' -or $ip -like '127.*') { return $false } if ($ip -like '10.*' -or $ip -like '192.168.*' -or $ip -like '169.254.*') { return $false } if ($ip -match '^172\.(1[6-9]|2[0-9]|3[01])\.') { return $false } if ($ip -match '^100\.(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])\.') { return $false } if ($ip -match '^(fe80|fc|fd)' ) { return $false } return $true } Say '' Say 'PunchThru Remote Desktop check' Cyan Say 'Read-only. Nothing is changed or sent anywhere.' DarkGray $me = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() if (-not $me.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Say '' Say 'Please run this in PowerShell as Administrator.' Yellow Say 'Right-click PowerShell, choose "Run as administrator", and try again.' return } # --- Remote Desktop settings --------------------------------------------- Head 'REMOTE DESKTOP ON THIS COMPUTER' $ts = Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server' $rdp = Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' $rdpOn = ($ts.fDenyTSConnections -eq 0) $port = $rdp.PortNumber if ($rdpOn) { Say " Remote Desktop: on, port $port" } else { Say ' Remote Desktop: off' } if ($rdp.UserAuthentication -eq 1) { Say ' Sign-in before session: yes (Network Level Authentication)' } else { Say ' Sign-in before session: NO - Network Level Authentication is off' Yellow } if ($port -and $port -ne 3389) { Say " Note: a non-standard port ($port) does not hide Remote Desktop. Scanners check every port." DarkGray } $fw = Get-NetFirewallRule -DisplayGroup 'Remote Desktop' -Enabled True -Direction Inbound -Action Allow if ($fw) { $from = ($fw | Get-NetFirewallAddressFilter | Select-Object -ExpandProperty RemoteAddress -Unique) -join ', ' if ($from -match 'Any') { Say ' Windows Firewall: lets anyone reach Remote Desktop' Yellow Say ' (A firewall in front of this computer, such as your router or hosting provider, may still block it.)' DarkGray } else { Say " Windows Firewall: Remote Desktop allowed from $from" } } # --- Account lockout policy ---------------------------------------------- $cfg = Join-Path $env:TEMP ("punchthru-check-{0}.cfg" -f [guid]::NewGuid().ToString('N')) secedit /export /cfg $cfg /areas SECURITYPOLICY /quiet | Out-Null $pol = @{} if (Test-Path $cfg) { Get-Content $cfg | ForEach-Object { if ($_ -match '^\s*(\w+)\s*=\s*(.+?)\s*$') { $pol[$Matches[1]] = $Matches[2] } } Remove-Item $cfg -Force } if ($pol.ContainsKey('LockoutBadCount')) { $bad = [int]$pol['LockoutBadCount'] if ($bad -eq 0) { Say ' Account lockout: NEVER - attackers get unlimited password guesses' Yellow } else { $dur = $pol['LockoutDuration'] Say " Account lockout: after $bad wrong passwords, for $dur minutes" } } # --- Failed sign-ins ----------------------------------------------------- Head 'FAILED SIGN-INS RECORDED BY WINDOWS' $log = Get-WinEvent -ListLog Security $oldest = Get-WinEvent -LogName Security -MaxEvents 1 -Oldest $ev = @(Get-WinEvent -FilterHashtable @{ LogName = 'Security'; Id = 4625 } -MaxEvents 500000) $rows = @($ev | ForEach-Object { [pscustomobject]@{ Time = $_.TimeCreated; User = [string]$_.Properties[5].Value; IP = [string]$_.Properties[19].Value } }) $public = @($rows | Where-Object { IsPublic $_.IP }) # Older Windows (2012 R2 and earlier) often records no address when # Network Level Authentication rejects a password, so internet attacks # show up with none. Count them as likely attacks, and say so. $unknown = @($rows | Where-Object { NoAddress $_.IP }) $inside = $rows.Count - $public.Count - $unknown.Count $attacks = @($public + $unknown) $days = 0 if ($oldest) { $days = [math]::Max(((Get-Date) - $oldest.TimeCreated).TotalDays, 0.04) } $perDay = 0 if ($days -gt 0) { $perDay = $attacks.Count / $days } if ($oldest) { $span = if ($days -ge 2) { "{0:N0} days" -f $days } else { "{0:N0} hours" -f ($days * 24) } Say (" Windows' log goes back {0} (to {1:d})." -f $span, $oldest.TimeCreated) } Say (" Failed sign-ins from internet addresses: {0}" -f (Num $public.Count)) if ($unknown.Count -gt 0) { Say (" Failed sign-ins with no address recorded: {0}" -f (Num $unknown.Count)) Say ' (Windows often leaves out the address when it blocks a password before the' DarkGray Say ' session starts. On a computer reachable from the internet, these are almost all attackers.)' DarkGray } if ($inside -gt 0) { Say (" Failed sign-ins from inside the network: {0}" -f (Num $inside)) DarkGray } if ($attacks.Count -gt 0) { Say (" That is about {0} a day." -f (Num ([math]::Round($perDay)))) Say (" Most recent attempt: {0}" -f ($attacks | Sort-Object Time | Select-Object -Last 1).Time) } if ($public.Count -gt 0) { Head 'WHERE THE ATTEMPTS CAME FROM (top 10 addresses)' $public | Group-Object IP | Sort-Object Count -Descending | Select-Object -First 10 | ForEach-Object { Say (" {0,8} {1}" -f (Num $_.Count), $_.Name) } Say (" {0} different addresses in all." -f (Num (@($public | Select-Object -ExpandProperty IP -Unique).Count))) DarkGray } if ($attacks.Count -gt 0) { Head 'USERNAMES THEY TRIED (top 15)' $local = @{} Get-WmiObject Win32_UserAccount -Filter 'LocalAccount=True' | ForEach-Object { $local[$_.Name.ToLower()] = $true } $hits = 0 $attacks | Group-Object User | Sort-Object Count -Descending | Select-Object -First 15 | ForEach-Object { $real = $local.ContainsKey($_.Name.ToLower()) if ($real) { $hits++ } $tag = if ($real) { ' <- a real account on this computer' } else { '' } $color = if ($real) { 'Yellow' } else { $null } Say (" {0,8} {1}{2}" -f (Num $_.Count), $_.Name, $tag) $color } $host8 = $env:COMPUTERNAME if ($host8.Length -gt 4) { $named = @($attacks | Where-Object { $_.User -and $host8.ToUpper().Contains($_.User.ToUpper()) -and $_.User.Length -ge 4 } | Select-Object -ExpandProperty User -Unique) if ($named) { Say (" They also tried names taken from this computer's own name ({0}): {1}" -f $host8, ($named -join ', ')) Yellow Say ' Remote Desktop tells anyone who connects what the computer is called.' DarkGray } } } $lock = @(Get-WinEvent -FilterHashtable @{ LogName = 'Security'; Id = 4740 } -MaxEvents 100000) if ($lock.Count -gt 0) { Say '' Say (" Accounts locked out in this period: {0}" -f (Num $lock.Count)) Yellow Say ' Each lockout can stop a real person from working until it clears.' DarkGray } # --- Verdict ------------------------------------------------------------- Head 'WHAT THIS MEANS' $stopped = $false if ($attacks.Count -eq 0) { if ($rdpOn) { Say ' No failed sign-ins from the internet are in the log. Either Remote Desktop' Say ' is not reachable from the internet, or the log does not go back far enough.' } else { Say ' No failed sign-ins from the internet are in the log.' } } else { # A busy attack that suddenly goes quiet means something now blocks it. $last = ($attacks | Sort-Object Time | Select-Object -Last 1).Time $quietHours = ((Get-Date) - $last).TotalHours $stopped = ($perDay -ge 24 -and $quietHours -ge 6) -or ($perDay -ge 5 -and $quietHours -ge 48) } if ($attacks.Count -gt 0 -and $stopped) { Say (" THE ATTACKS STOPPED: about {0} guesses a day until {1}, none since." -f (Num ([math]::Round($perDay))), $last) Green Say ' Something now blocks Remote Desktop from the internet - most likely a firewall' Say ' change or Remote Desktop being closed. Keep it that way: the attackers have not gone,' Say ' they just cannot reach this computer.' } elseif ($attacks.Count -gt 0) { if ($perDay -ge 100) { Say ' HEAVY ATTACK: this computer is being hammered with password guesses.' Red } elseif ($perDay -ge 5) { Say ' UNDER ATTACK: strangers are guessing passwords every day.' Red } else { Say ' BEING PROBED: strangers on the internet are trying to sign in.' Yellow } Say ' Every attempt is someone who can reach this computer from anywhere in the world.' Say ' One reused or weak password is all it takes; stolen passwords are the most common' Say ' way ransomware gets in.' } if ($log.FileSize -ge ($log.MaximumSizeInBytes * 0.9)) { Say '' Say (" The Security log is full ({0:N0} MB), so older attempts were already erased." -f ($log.MaximumSizeInBytes / 1MB)) DarkGray Say ' The real numbers are higher than shown.' DarkGray } Say '' Say ' PunchThru closes Remote Desktop to the internet completely, so none of these' Cyan Say ' attempts can reach the computer, while your people still connect from anywhere.' Cyan Say ' https://punchthru.com' Cyan Say '' } Invoke-PunchThruRdpCheck