Windows and RDP

How to secure RDP on Windows Server against brute force

Scanners quickly find an open RDP port, and bots start guessing the password, most often for the name Administrator. The six settings below protect the server from this kind of guessing and take about half an hour in total.

  • Time: 30 minutes
  • Level: Intermediate

In short

  • Change the password you were given to a long, unique one with net user Administrator *.
  • Create your own administrator with a non-obvious name and disable the built-in Administrator: every bot knows that name.
  • Turn on lockout after 10 wrong passwords and allow RDP in Windows Firewall only from your own IP addresses. The IP restriction does the most.
  • Changing port 3389 reduces noise in the log but does not protect against guessing. Keep NLA turned on and install updates.
Contents
  1. First, see who is already guessing the password
  2. Step 1. Change the administrator password
  3. Step 2. Create your own administrator and disable Administrator
  4. Step 3. Turn on lockout after wrong passwords
  5. Step 4. Allow RDP only from your own IP addresses
  6. Step 5. Changing port 3389: what it gives and how to do it
  7. Step 6. Keep NLA turned on and install updates
  8. How to check that the protection works
  9. If you cannot log in after the setup
  10. FAQ

You will need

  • RDP access to the server with administrator rights: how to connect.
  • PowerShell as administrator: find PowerShell in the Start menu, right-click it and choose Run as administrator.
  • The external IP address of your internet connection, if you are going to restrict access by address.
  • A backup way in, in case of a mistake: the Hetzner KVM console or IPMI on OVH.

First, see who is already guessing the password

Every failed login attempt goes into the Windows Security log as event 4625. The command below shows the ten names most often used in login attempts:

Get-WinEvent -FilterHashtable @{ LogName = 'Security'; Id = 4625 } -MaxEvents 500 |
  Group-Object { $_.Properties[5].Value } |
  Sort-Object Count -Descending |
  Select-Object -First 10 Count, Name

If the list shows administrator, admin, user, test and other dictionary names, these are bots: they try passwords for names that exist on almost any server. If the command replies No events were found that match the specified selection criteria, there have been no failed logins yet. Run it again a day after you finish the setup and compare.

Steps 4 and 5 change which addresses and which port the server accepts RDP on. A mistake in an address or port number will lock you out too. Test the new connection from a second window without closing the first, and find out in advance how to open the server’s screen without RDP: the Hetzner KVM console or IPMI on OVH.

Step 1. Change the administrator password

The password from the ticket is stored in the ticket history and your e-mail, so change it in the first minutes. Generate the new password in a password manager: at least 16 random characters, not used anywhere else. Then run in PowerShell:

net user Administrator *

Windows asks you to enter the password twice; nothing shows as you type. On a localized Windows Server, the account name is translated (for example, Администратор in the Russian edition). Even if you disable this user in step 2, change the password right away: until it is disabled, it remains a working way into the server.

Step 2. Create your own administrator and disable Administrator

Every bot knows the name Administrator, so it only has to guess the password. With a non-obvious name, such as srv-ivan, it has to guess the name as well.

  1. Create a user

    Replace srv-ivan with a name of your own (not admin, user or test). The command asks for the new user’s password; nothing shows as you type:

    New-LocalUser -Name "srv-ivan" -Password (Read-Host -AsSecureString "Password") -PasswordNeverExpires

    If Windows replies that the password does not meet the policy requirements, generate a longer one with digits and symbols.

  2. Add the user to the administrators

    Add-LocalGroupMember -SID "S-1-5-32-544" -Member "srv-ivan"

    S-1-5-32-544 is the fixed identifier of the Administrators group, so the command works in Windows in any language. Administrators are allowed to log in over RDP by default.

  3. Log in as the new user

    Without closing the current session, open a new connection to the server and log in with the new name. Make sure PowerShell starts as administrator.

  4. Disable the built-in Administrator

    From the new user’s session, run:

    Disable-LocalUser -Name "Administrator"

    On a localized Windows, use the translated account name instead of Administrator. You can turn the account back on with Enable-LocalUser and the same name.

If you forget the new user’s password, nobody will be able to log in over RDP, and you will have to restore access through the server console: how to reset a Windows password on Hetzner. Save the password in a password manager right away.

Access for a colleague without administrator rights

A colleague who only needs to work in programs does not need administrator rights. Create a user for them with the New-LocalUser command from step 2, and instead of the Administrators group, add them to the Remote Desktop Users group:

Add-LocalGroupMember -SID "S-1-5-32-555" -Member "srv-anna"

Without RDS licenses, two people can work over RDP at the same time: how a server for a team works.

Step 3. Turn on lockout after wrong passwords

When lockout is off, Windows lets anyone guess passwords forever. Check the current state: the net accounts command shows a Lockout threshold line (on a localized Windows, the lines are translated), and the value Never means lockout is off. Turn it on:

net accounts /lockoutthreshold:10 /lockoutduration:15 /lockoutwindow:15
net accounts

Now, after 10 wrong passwords within 15 minutes, the account is locked for 15 minutes. The second command shows the result: the Lockout threshold line must say 10.

Lockout works against you as well: if a bot is guessing the password for your name specifically, you cannot log in either until the 15 minutes pass. That is why the administrator name should be non-obvious, and better still, RDP should be closed to other addresses, as in step 4: then it never gets as far as a lockout.

You can set the same thing in the Local Security Policy window: open secpol.msc, then Account PoliciesAccount Lockout Policy.

Lockout affects the built-in Administrator only if Allow Administrator account lockout is enabled. If you disabled this user in step 2, you can leave the item as it is.

Step 4. Allow RDP only from your own IP addresses

This is the strongest of the six measures. Windows Firewall drops connections from other addresses before the password is even checked, so a bot has nothing to guess.

This works if you connect from fixed addresses: from an office, or from home with a static IP. If your internet address changes, read the block about dynamic IPs below first.

  1. Find your external IP

    On your own computer, search for “what is my ip”: Google and other search engines show the address you go online from. If you connect from several places, find the address at each one.

  2. Restrict the RDP rules to your addresses

    Replace 198.51.100.20 and 192.0.2.30 with your own addresses, separated by commas:

    Set-NetFirewallRule -Group "@FirewallAPI.dll,-28752" -RemoteAddress 198.51.100.20, 192.0.2.30

    @FirewallAPI.dll,-28752 is the system name of the built-in Remote Desktop rule group; it is the same in Windows in any language.

  3. Check that the firewall is on

    Get-NetFirewallProfile | Format-Table Name, Enabled

    All three profiles must show True in the Enabled column. If any shows False, the firewall was off and the RDP rules may not have been working. Enable both RDP rules first and only then the firewall, or it will close RDP to you as well:

    Enable-NetFirewallRule -Name "RemoteDesktop-UserMode-In-TCP", "RemoteDesktop-UserMode-In-UDP"
    Set-NetFirewallProfile -All -Enabled True
  4. Find other rules that open port 3389

    Get-NetFirewallPortFilter | Where-Object LocalPort -eq 3389 | Get-NetFirewallRule | Format-Table DisplayName, Enabled, Direction, Action

    You have already restricted the Remote Desktop group rules. If the list shows other inbound allow rules, restrict them too: Set-NetFirewallRule -DisplayName "rule name" -RemoteAddress 198.51.100.20, 192.0.2.30. Otherwise they will let everyone in.

  5. Test from an outside address

    Turn off Wi-Fi on your phone and try to connect to the server over mobile internet in Windows App. The connection must fail, while login from your computer works as before.

If you prefer a settings window, the same rule can be changed in wf.msc: Inbound RulesRemote Desktop - User Mode (TCP-In)PropertiesScope; under Remote IP address, choose These IP addresses and add the addresses with the Add button. Do the same for the UDP-In rule.

If your IP changes or you have locked yourself out

If your internet provider gives you a new address on every connection, you can allow its whole network, for example 198.51.100.0/24: this is weaker than a single address but stops most bots. A more reliable option is to get a static IP from your provider.

If you have lost access, open the server’s screen through the Hetzner KVM console or IPMI on OVH, log in and restore access from all addresses:

Set-NetFirewallRule -Group "@FirewallAPI.dll,-28752" -RemoteAddress Any

Step 5. Changing port 3389: what it gives and how to do it

Bots most often knock on the standard port 3389, so after you change the port, the log shows fewer failed logins. It does not protect against guessing: scanners find RDP on any port, just later. If you have already done step 4, changing the port adds almost nothing.

Choose a number from 1024 to 49151; the example below uses 42390. Open the new port in the firewall first, and only then change the RDP setting and reboot the server: without a firewall rule, the server will be unreachable after the reboot.

  1. Allow the new port in the firewall

    New-NetFirewallRule -DisplayName "RDP 42390 TCP" -Direction Inbound -Protocol TCP -LocalPort 42390 -Action Allow -Profile Any
    New-NetFirewallRule -DisplayName "RDP 42390 UDP" -Direction Inbound -Protocol UDP -LocalPort 42390 -Action Allow -Profile Any

    If you restricted addresses in step 4, add the same addresses to the end of both commands: -RemoteAddress 198.51.100.20, 192.0.2.30.

  2. Change the RDP port

    Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name PortNumber -Value 42390
  3. Reboot the server

    The new port starts working after a reboot. The current connection will drop.

    Restart-Computer
  4. Connect on the new port

    After a few minutes, connect with the port added to the IP after a colon: 203.0.113.10:42390. Enter the address the same way in Windows App on a Mac or phone.

If the server does not answer on the new port after the reboot, log in through the console, set port 3389 back with the same command from the second step using -Value 3389, and reboot the server again.

Step 6. Keep NLA turned on and install updates

Network Level Authentication (NLA) requires the password before the server creates a session and shows the login screen. This way, bots do not see the login screen until they know the password. NLA is on by default. Check that nobody has turned it off:

(Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp').UserAuthentication

A reply of 1 means NLA is on. If it is 0, open sysdm.cpl, go to the Remote tab and select Allow connections only from computers running Remote Desktop with Network Level Authentication (recommended).

Windows security updates come out on the second Tuesday of every month. Install them in Settings, under Windows Update, or through sconfig. After installation, the server reboots and is unreachable over RDP for a few minutes, so pick a time when that will not disrupt work.

How to check that the protection works

  • net accounts shows a Lockout threshold of 10.
  • The command Get-NetFirewallRule -Group "@FirewallAPI.dll,-28752" | Get-NetFirewallAddressFilter shows your addresses in RemoteAddress, not Any.
  • You cannot connect over mobile internet, but you can from your computer.
  • A day later, the command from the first section shows fewer failed logins, or none.

If all four checks pass, the password can no longer be guessed from an outside address. Keep the rollback command from step 4 handy: you will need it if your IP changes.

If you cannot log in after the setup

MessageWhat it meansWhat to do
“The connection was denied because the user account is not authorized for remote login”The new user is in neither the Administrators group nor the Remote Desktop Users group.Log in as another administrator and add the user to the group with the command from step 2.
As a security precaution, the user account has been locked because there were too many logon attempts or password change attempts.The lockout from step 3 kicked in.Wait 15 minutes. If the lockout keeps recurring by itself, someone is guessing the password for your name: restrict addresses as in step 4.
“Remote Desktop can’t connect to the remote computer…” or Error code: 0x204 in Windows AppThe firewall does not let your address through, or you are connecting on the wrong port.Check whether your IP has changed. If you changed the port, add it: 203.0.113.10:42390. If that does not help, log in through the console and roll back the change with the command from the block above.
“Your credentials did not work”The name or password is wrong, or you are logging in as the disabled Administrator.Log in as the new user. Write the name as .\srv-ivan so that Windows does not add your computer’s name.

If you are locked out and the console does not help, message us on Telegram or in a ticket: we will tell you how to open the server’s screen at your provider.

FAQ

Do I need a program like RDPGuard?
If RDP is open only to your addresses, such a program adds almost nothing. It is useful when you cannot restrict addresses: programs such as the paid RDPGuard or the free IPBan read 4625 events and block an IP after several failures. We have not tested them and do not set them up.
How often should I change the password?
A long, unique password that has not leaked does not need changing on a schedule. Change it if you sent it to someone in a message, typed it on someone else’s computer, or see logins you did not make.
What do I do if I locked myself out?
Open the server’s screen through the Hetzner KVM console or IPMI on OVH and roll back the change: Set-NetFirewallRule -Group "@FirewallAPI.dll,-28752" -RemoteAddress Any restores access from all addresses.

Did this guide help?

Questions?