Port Forwarding in Windows

转自:http://woshub.com/port-forwarding-in-windows/

Since Windows XP there is a built-in ability in Microsoft Windows to set up network ports forwarding. Due to it, any incoming TCP connection (IPv4 or IPv6) to local port can be redirected to another local port or even to port on the remote computer. And it is not necessary for system to have a service that listens to this port.

In Linux, port redirection is configured quite simply using iptables. On Windows Server systems, the Routing and Remote Access Service (RRAS) is used to organize port forwarding. However, there is an easier way to configure the port forwarding, which works well in any version of Windows.

Port forwarding in Windows can be configured using Portproxy mode of the command Netsh. The syntax of this command is as follows:
netsh interface portproxy add v4tov4 listenaddress=localaddress listenport=localport connectaddress=destaddress connectport=destport
where

  1. listenaddress – is a local IP address waiting for a connection.
  2. listenport – local listening TCP port (the connection is waited on it).
  3. connectaddress – is a local or remote IP address (or DNS name) to which the incoming connection will be redirected.
  4. connectport – is a TCP port to which the connection from listenport is forwarded to.

Let’s imagine that our task is to make the RDP service to respond on a non-standard port, for example 3340 (the port can be changed in the settings of the service, but we will use RDP to make it easier to demonstrate forwarding). To do this, you need to redirect incoming traffic from TCP port 3340 to another local port – 3389 (standard rdp port).

Start the command prompt as an administrator and perform the following command:

netsh interface portproxy add v4tov4 listenport=3340 listenaddress=10.1.1.110 connectport=3389 connectaddress=10.1.1.110

Where 10.10.1.110 – the current IP address of this computer.

Using netstat make sure that port 3340 is listened now:

netstat -ano | findstr :3340

Note. If this command does not return anything and port forwarding via the netsh interface portproxy does not work, make sure that you have the iphlpsvc (IP Helper) service running on your system.

IPv6 support must be enabled on the network interface for which the port forwarding rule is created.

These are the prerequisites for correct port-forwarding. Without the IP Helper service and without IPv6 support enabled, the port redirection won’t work.

You can find out what process is listening to this port use its PID (in our example, the PID is 636):

tasklist | findstr 636

Let’s try to connect to this computer from a remote system using any RDP client. Port 3340 should be specified as the RDP port.It is specified after the colon following the RDP server address, for example, 10.10.1.110:3340:

The connection should be established successfully.

Important. Make sure that your firewall (Windows Firewall or a third-party one that are often included into an antivirus software) allows incoming connections to the new port. If necessary, you can add a new Windows Firewall rule using this command:

netsh advfirewall firewall add rule name=”forwarded_RDPport_3340” protocol=TCP dir=in localip=10.1.1.110  localport=3340 action=allow

When creating an incoming firewall rule for port 3340 via Windows Firewall graphical interface, no program needs to be associated with it. This port is only listened by the network driver.

You can create any number of Windows port forwarding rules. All netsh interface portproxy rules are persistent and are stored in the system after a Windows restart.

Display the list of forwarding rules in the system:

netsh interface portproxy show all

In our case there is only one forwarding rule from port 3340 to 3389:

Listen on ipv4:             Connect to ipv4:
Address         Port        Address         Port
--------------- ----------  --------------- ----------
10.1.1.110     3340        10.1.1.110     3389

Tip. Also, portproxy settings can be obtained as follows:
netsh interface portproxy dump
#========================
# Port Proxy configuration
#========================
pushd interface portproxy
reset
add v4tov4 listenport=3340 connectaddress=10.1.1.110 connectport=3389
popd
# End of Port Proxy configuration

To remove a specific port forwarding rule:

netsh interface portproxy delete v4tov4 listenport=3340 listenaddress=10.1.1.110

To clear all current port forwarding rules:

netsh interface portproxy reset

Important. This forwarding scheme works only for TCP ports. You won’t be able to forward UDP ports this way. Also you can’t use 127.0.0.1 as connect address.

If you want to forward an incoming TCP connection to another computer, the command can look like this:

netsh interface portproxy add v4tov4 listenport=3389 listenaddress=0.0.0.0 connectport=3389 connectaddress=192.168.100.101

This rule will redirect all incoming RDP requests (to port 3389) from this computer to a remote computer with an IP address 192.168.1.101.

Another portproxy feature is an opportunity to make it look like any remote network service is operating locally.

For example, forward the connection from the local port 5555 to the remote address 157.166.226.25 (CNN website):

netsh interface portproxy add v4tov4 listenport=5555 connectport=80 connectaddress= 157.166.226.25 protocol=tcp

Now if you go to http://localhost:5555/ in your browser, CNN Start page will open. So despite the browser addresses the local computer, it opens a remote page.

Port forwarding can also be used to forward a port from an external address of a network card to a virtual machine port running on the same computer.

Also, there were cases when in Windows Server 2012 R2 the port forwarding rules worked only until the system was rebooted, and after restart they were reset. In this case, you need to check whether there is a periodic disconnection on the network interface, and whether the IP address changes when the OS boots (it is better to use a static IP). As a workaround, I had to add a script to the Windows scheduler with the netsh interface portproxy rules that run on the system startup.

In Windows Server 2003 / XP, you must additionally set the IPEnableRouter parameter to 1 in the registry key HKLM\SYSTEM\ControlSet001\Services\Tcpip\Parameters.

原文地址:https://www.cnblogs.com/xzlive/p/9700855.html

时间: 2024-11-10 08:35:26

Port Forwarding in Windows的相关文章

How to do SSH Tunneling (Port Forwarding)

In this post we will see how ssh works?, what is SSH tunneling? what is important of ssh tunnels and how to setup the ssh tunnel.When SSH server is installed in machine then by default its allowed ssh tunneling. SSH Tunneling (Port Forwarding) Tunnel

port forwarding in yosemite, a way to replace ipfw

login as root, or do the following in sudo. create a file /etc/pf.anchors/com.pow, add following two lines with new line breaker at the end: rdr pass on lo0 inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080 rdr pass on lo0 inet proto tcp

路由器port触发与转发---Port Forwarding & Port Triggering

What is Port Triggering? If you have not read my explanation of port forwarding do so now. You can find it here. Port triggering is pretty simple once you know what port forwarding is. Port Triggering is port forwarding with an on/off switch for the

路由器端口触发与转发---Port Forwarding & Port Triggering

What is Port Triggering? If you have not read my explanation of port forwarding do so now. You can find it here. Port triggering is pretty simple once you know what port forwarding is. Port Triggering is port forwarding with an on/off switch for the

NAT&Port Forwarding&Port Triggering

NAT Nat,网络地址转换协议.主要功能是实现局域网内的本地主机与外网通信. 在连接外网时,内部Ip地址需要转换为网关(一般为路由器Ip地址)(端口号也需要相应的转换) 如:QQ程序 在一个局域网中,路由器(网关)WAN侧ip为200.0.0.123 局域网三台计算机:计算机A 192.168.0.10,计算机B 192.168.0.20,计算机C 192.168.0.30 分别登陆一个qq程序,在各自机子上,IP和Port分别为 计算机A 192.168.0.10:4000,计算机B 192

使用Port Forwarding连接k8s集群的pod(redis、mysql等)

一. 创建Redis的deployment和service 1. 创建Redis deployment redis-master-deployment.yaml  apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 kind: Deployment metadata: name: redis-master labels: app: redis spec: selector: matchLabels: app: redi

[Monkeyrunner]waitForConnection()报错:Adb rejected adb port forwarding command: cannot bind socket

整理自: Stackoverflow(方法二亲测有效~~) Condition: a. adb device 有设备信息 b. Commands : Error: ...(error msg省略中)... Solution: 方法1:adb-killer +PC reboot 方法2:1. netstat -ano,找到12345 port的PID 2. 打开任务管理器,杀掉对应PID进程,Done~

从Android访问PC端的port (reverse port forwarding)

大致就是 Device side: install SSH Server, add ssh server on 22222 port and user q start ssh server PC side: adb forward tcp:22222 tcp:22222    //这是为了能够从PC访问android里的ssh server的22222端口. ssh -f -N -R 3000:localhost:3000  q@localhost -p 22222 Then you can u

iptables 通讯端口转接(Port Forwarding)

是一种特殊的DNAT操作,其作用是让一部电脑(通常是防火牆)担任其它电脑的代理伺服器(proxy).防火牆接收外界网络接传给它自己的包,然后改写包的目的地位址或目的端口,使其像是要送到內部网路其它电脑的樣子,然后才修改好的包送往新目的地.此外,来自內部网路的相关回复包,也会被防火牆改写成像是从防火牆自己发出的樣子,然后才送到外界电脑. 「通讯端口转接」通常用於提供內部网路的公开服务(像是web server或email server)给外界存取,而不需要一个以上的public IP.对於外界而言