set RDP_PORT=3390
reg add "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlTerminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
reg add "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlTerminal ServerWinStationsRDP-Tcp" /v PortNumber /t REG_DWORD /d %RDP_PORT% /f
netsh advfirewall firewall delete rule name="p%RDP_PORT%" >nul 2>&1
netsh advfirewall firewall add rule name="p%RDP_PORT%" dir=in protocol=TCP localport=%RDP_PORT% action=allow
for /f "tokens=2" %%a in ('tasklist /SVC ^| findstr "TermService"') do SET MYPID=%%a
taskkill /F /PID %MYPID%
net stop TermService /y
net start TermService
netstat -anp tcp| findstr "%RDP_PORT%"
1. 设置自定义RDP端口
set RDP_PORT=3390
-
定义环境变量 RDP_PORT
,将远程桌面协议(RDP)的默认端口从3389
改为3390
,方便后续更改。
2. 启用远程桌面连接
reg add "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlTerminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
-
修改注册表项 fDenyTSConnections
的值为0
,启用远程桌面连接功能(默认值为1
表示禁用)。
3. 修改RDP监听端口
reg add "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlTerminal ServerWinStationsRDP-Tcp" /v PortNumber /t REG_DWORD /d %RDP_PORT% /f
-
将RDP服务的默认端口 3389
修改为自定义的3390
,通过修改注册表中的PortNumber
值实现。
4. 配置防火墙规则
netsh advfirewall firewall delete rule name="p%RDP_PORT%" >nul 2>&1
netsh advfirewall firewall add rule name="p%RDP_PORT%" dir=in protocol=TCP localport=%RDP_PORT% action=allow
-
删除旧规则(如果存在名为 p3390
的规则,避免重复)。 -
添加新入站规则,允许TCP流量通过自定义端口 3390
,确保外部可访问新的RDP端口。
5. 重启远程桌面服务
for /f "tokens=2" %%a in ('tasklist /SVC ^| findstr "TermService"') do SET MYPID=%%a
taskkill /F /PID %MYPID%
net stop TermService /y
net start TermService
-
查找并终止TermService进程: -
使用 tasklist
和findstr
获取远程桌面服务(TermService
)的进程ID(PID)。 -
强制结束该进程( taskkill /F
)。 -
重启服务:通过 net stop
和net start
确保新的端口配置生效。
6. 验证端口监听状态
netstat -anp tcp| findstr "%RDP_PORT%"
-
使用 netstat
检查TCP协议中是否有进程正在监听3390
端口,确认配置成功。
原文始发于微信公众号(生有可恋):一键修改远程桌面端口
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论