You are on page 1of 4

Portable UDP port forwarding in user space

Vancea Florin*, Vancea Codruţa**


*
Department of Computers,
University of Oradea, Faculty of Electrical Engineering and Information Technology,
Universităţii st. 1, 410087 Oradea, Romania, E-Mail:fvancea@uoradea.ro
**
Department EMUEE,
University of Oradea, Faculty of Electrical Engineering and Information Technology,
Universităţii st. 1, 410087 Oradea, Romania, E-Mail:cvancea@uoradea.ro

Abstract – Port forwarding is frequently used to telnet access from a client on network A behind a
selectively expose services available on remote firewall to a network-enabled simple device placed on
machines to clients running on the local machine or network B, behind another firewall.
on machines connected to the local network. Services The above-described telnet requirement can be
running over TCP are easy to forward using SSH, at solved rather easily with TCP port-forwarding over SSH
least as long as the details of the transported protocol and some network address translation or with an IP
are transparent. Tools are available for other classes of tunnel established between network A and network B.
tunneling, including UDP tunneling. However, these One possible setup is presented in Figure 1. The client
tools are not very portable and may be limited to establishes a SSH session with the remote border router
root/admin usage or may simply not be available. We and configures port forwarding over this session. Due to
are presenting a Java client/server pair that may be port forwarding, SSH will listen to a local port and
successfully used in almost any environment to forward all connections attempts to the remote target.
forward a UDP dialog over an existing forwarded TCP We assume that border router B supports SSH with port
connection. The tool is tested for traffic like SNMP forwarding. If not, the diagram should be extended with
with multiple clients and multiple targets. a “relay” host on B side, supporting SSH with port
forwarding and the border router B should do reverse
Keywords: TCP, UDP, port forwarding, address translation to expose the SSH service for the
inner host.
As long as the transport involved is TCP or an IP
I. INTRODUCTION tunnel can be established between the two networks the
setup is fairly common. However, some particular
There are many scenarios where traffic should be situations require UDP traffic to flow from network A to
carried at the transport level between two hosts on network B and due to other restrictions no IP tunnel can
different networks even if normally no traffic can (or be established.
should) traverse the internetwork from the first host to Our target example is to allow SNMP traffic between

Network A Border router A Border router B Network B

SSH connection
NAT with forwarding

inter-net

Figure 1. TCP forwarded


Forwarded TCPby SSH
Local TCP
connection connection

the second one. A reasonable example would be remote a client (or several clients) on network A to a target

75
Network A Network B
192.168.100.0/24 Border router A Border router B 10.0.10.0/24

inter-net

SNMP dialog over UDP

Figure 2. Target scenario


SNMP host (or several ones) on network B. All SNMP However, the tunnel and filter solution is acceptable
traffic is initiated on network A (i.e. there is no need for if we have full control over both border routers and the
SNMP-trap). Furthermore, no other type of traffic required connection is a long-term one (i.e. the effort
should freely flow from network A to network B or in required to set it up is justified).
the reverse direction and one limitation to consider is Another possible solution is to use ready-made tools
that due to administrative constraints an IP tunnel available in the Unix world (and not only there). There
between the two networks is to be avoided. The target is an simple but powerful utility called netcat. Half of its
requirement is presented in Figure 2. main function is to accept a TCP stream or a stream of
UDP datagrams and cleanly pipe the payload data into
II. POSSIBLE SOLUTIONS its standard output. The other half is the reverse process:
payload data coming from standard input is piped into a
First, let us consider that IP tunneling is still a TCP connection or pushed as UDP datagrams. We can
possible choice. In this case we can establish an IP build a pipe of netcat instances using shell scripting
tunnel between network A and network B using the capabilities and use the SSH TCP port forwarding
corresponding border routers. If confidentiality or capabilities to cross the inter-net. The principle is
integrity of transiting data is a concern, the tunnel can be presented in Figure 4.
protected using VPN techniques. The tunnel solution The first netcat instance listens on a UDP socket for
will effectively make possible any IP traffic between datagrams from the actual client. All data is piped
network A and network B, so to observe the limited through netcat standard output to the next netcat
traffic requirement we will also have to install filtering instance standard input. The second netcat instance
rules in the border routers. The solution is depicted in pipes the payload data into a TCP connection
Figure 3. established with the local port where SSH is listening.
The tunnel solution has some disadvantages. First of Since SSH forwards the port to the remote SSH server,
all, it requires additional configuration of the border the TCP connection ends on network B where the third
routers. This can be only an additional administration netcat instance is listening. The third netcat instance
complication or it can be a major difficulty if the border pipes further the data into its standard output. Finally,
routers are not under our control. the last netcat instance takes the data stream and pushes
The tunnel also offers wider transport abilities than it into UDP datagrams towards the actual UDP server
the minimum we need. We have to take steps to limit the listening for data. Of course, the whole chain has to be
traffic only to the SNMP over UDP we are strictly replicated for the reverse direction.
interested in. All the additional filter rules may be The complex pipe can also be built without netcat, or
complicated to set up right and maintain over time.
UDP listening port
TCP forwarded port
OS pipe
Network A Network B
Filtering rules Network A
netcat netcat SSH
IP tunnel client

inter-net
SSH netcat netcat UDP
server server

TCP Network B
OS pipe
listening port

Figure 3. IP tunnel and filtering Figure 4. netcat chain

76
only partially with netcat, by using another feature
available in some operation systems. Certain Linux The proper way to relay UDP datagrams over a TCP
distributions have special virtual /dev/udp and /dev/tcp connection is to handle explicitly the message
devices that allow plain shell scripts to access UDP and boundaries at source, transmit them in-band over the
TCP functionality. Of course, setting up such “pipe TCP connection and rebuild the UDP datagrams at the
dreams” can become quite complicated, especially for other end in strict conformity with the original
multiple targets or multiple local clients, because each messages. This is a rather complex task that requires a
chain supports only one dialog. specialized pair of programs to handle properly all the
The main advantage of such complex pipes is that details. As long as the participating networks are known
they can be built whenever needed, with much less in advance and stable, almost any language able to
overhead than the IP tunnel solution. Furthermore, they handle TCP and UDP will do. However, when the
may not require enhanced privileges on the endpoint connection has to be established ad-hoc with a variety of
hosts. If you can SSH to a remote host and do port target networks it may be difficult to work with binaries
forwarding, you may be also able to build a UDP pipe. or to recompile source. The remote machine may run an
However, the netcat pipe suffers from a fundamental unusual or unfamiliar OS, for which there are no
problem. The pipe does not preserve the datagram prebuilt relay binaries or which has no preinstalled
alignment. If the protocol that uses UDP as transport compiler.
relies on UDP to preserve message boundary then it may Luckily, more and more systems have nowadays a
not work over the pipe. The reason is that netcat does JVM installed because many applications are at least
not introduce any kind of message delimiter and TCP partly written in Java or allow Java-based extensions.
does not guarantee anything beyond the sequentiality of Therefore Java is the language of choice for the tool we
the data stream. For the chain to work, a sum of special want to build.
conditions should be attained: To achieve the goal set in the introductory part we
- messages must be reasonably small will need a local converting half and a remote
- messages must come widely spaced in time converting half. The two halves are connected through
- the quality of the IP chain below TCP TCP and this connection can be comfortably forwarded
connection should be quite reliable over SSH. The local half listens for UDP, wraps the
If the conditions are not perfectly met, two or more messages with boundary delimiters and other
original messages may aggregate in the same TCP information, then pushes the resulting data units over the
segment and surface at the other end of the pipe as a TCP connection. On other side, the remote half unwraps
single (probably ill-formed) message. If a single large the data units and acts as a proxy, relaying the messages
message is segmented by TCP during transit, it will unchanged to the actual target. In fact, the pair acts like
produce two or more smaller messages, again without a tunnel for UDP over TCP with a proxy at the remote
meaning for the protocol above UDP. end. The solution is shown in Figure 5.
The pair creates effectively a new layer in ISO-OSI
III. JAVA-BASED SOLUTION acception and each half is the implementation for this

Network A Network B

SSH connection
with port forwarding Agent forwards
UDP traffic

NAT NAT

inter-net

Forwarded TCP
connection
Agent accepts
UDP traffic
Local Remote
agent agent

Figure 5. UDP tunnel layout

77
layer. Local UDP ports are SAP’s of this layer, paired would be a consulting representative which needs to
with remote targets. UDP datagrams are SDU’s temporarily monitor or configure network devices
presented to the new layer. Those SDU are wrapped in placed at the customer site using SNMP. No intervention
custom protocol PDU and multiplexed over the TCP to the network infrastructure is required, which
connection offered by SSH. The layer communicates in minimizes the chances for misconfiguration leading to
connected mode. Each local SAP – remote target forms security hazards. The tunnel can be set up whenever is
a virtual connection that has to be established in needed and can also be shutdown quickly. The
advance. requirements are:
In order to allow for coherent reverse traffic, the - a remote host on network B accessible by SSH
remote half has to offer SAP’s, too. Those SAP’s are with port forwarding
actually the UDP ports on the remote host, used to - a JVM on the remote host
originate the UDP datagrams to the targets. The remote No other special privileges are required on any of the
SAP’s are in turn paired with local client originating relay hosts involved. To establish the tunnel, the jar
addresses, creating a parallel set of virtual connections containing the remote half has to be copied to the remote
for the reverse traffic. side, then it has to be started with appropriate
With this setup, an arbitrary number of local clients configuration. All further operation and configuration
can send UDP datagrams to a UDP socket where the can be done from the local end of the tunnel. Once the
local agent listens. Each client will receive UDP replies tunnel is started it can be transparently used without
properly coming from the same socket, exactly as if the intervention to the tunnel itself.
remote target is listening on local relay host. On the A careful reader would already have noticed that all
remote side, the agent can initiate UDP client-server these advantages for legitimate use are also great news
sessions with multiple targets, using a different for unauthorized use. Any host accepting UDP packets
originating UDP socket for each local client represented. on the remote network is instantly accessible to a
Its operation is very close to a proxy. Each remote target malicious user on the local side. Furthermore, the
sees one or several distinct sessions, all originating from process can be arranged to pass almost un-noticed.
the same host (the relay). On the other hand, the main issue here is not the tool
The setup required for this tunnel is minimal. On the itself, but the SSH ability to forward ports. Even if a
remote half the only configuration required is the TCP user is granted access to a host on a limited account and
port where the SSH forwarding should terminate. On the the host appears to be the only one accessible by reverse
local side, each forward connection should be NAT, the port forwarding feature spoils this fragile
configured, in a manner similar to SSH port forwarding. security. The security-conscious administrator that must
Each tunnel requires a triple allow SSH to unprivileged accounts on a particular host
local_port:target_IP:target_port which is specified in should disable the port forwarding feature.
the command line that starts the local half. The two
halves perform upon start a setup phase that associates IV. CONCLUSIONS
identifiers to the channels to reduce traffic PDU size.
After that, each PDU will contain only the forward We have described a method to build an additional
tunnel identifier, which is a small integer. layer over TCP that can tunnel and proxy UDP traffic.
To implement the reverse pairing, the local half The particular implementation we have developed
maintains a client table updated on the fly and marks turned to be portable, easy to use and almost transparent.
PDU’s from different clients with a different reverse The tool can be useful, but in the wrong hands it may be
tunnel identifier. Thus, each PDU contains minimal a security hazard, not by itself but by exploiting weak
overhead: two tunnel identifiers and a message length. configuration setups.
We have successfully implemented a pair of agents
and tested it against real SNMP traffic. Common SNMP REFERENCES
traffic is not dependent on the IP of the originator and
the proxying principle is not impacting the operation of [1] T. Ylonen, C. Lonvick, ″The Secure Shell (SSH) Protocol
the SNMP requests. The multiple client / multiple target Architecture ″, RFC4251.
ability is quite comfortable. The only drawback is that [2] T. Ylonen, C. Lonvick, “The Secure Shell (SSH)
the whole setup has a slightly higher latency, otherwise Authentication Protocol″, RFC4252.
all traffic operates as if the targets are present on the [3] T. Ylonen, C. Lonvick, “The Secure Shell (SSH)
local network. Transport Layer Protocol″, RFC4253.
[4] T. Ylonen, C. Lonvick, “The Secure Shell (SSH)
Connection Protocol″, RFC4254.
IV. SECURITY NOTES
[5] A. S. Tannenbaum, ″Computer networks, Fourth Edition″,
Pearson 2002, ISBN-13: 9780130661029.
As in many other cases, the tool we have developed [6] Sun Microsystems, “Java Custom Networking”, available
is a double-edged sword. On one hand, a legitimate user online at http://java.sun.com/docs/books/tutorial/
operating on network A may quickly access SNMP networking/index.html.
targets on remote networks where no direct IP is
possible or desirable. An example of such use case

78

You might also like