How To: Server Socket Hijacking in Java

By Angsuman Chakraborty, Gaea News Network
Tuesday, June 6, 2006

Overview
Socket hijacking allows you to override a server socket opened on the same port by a different process. There are several good uses of socket hijacking like developing a port blocker application (poor man’s firewall) and some bad uses too.

Normally the operating system doesn’t allow you to open a server socket on a port which is already opened by another (or even the same) application. However there is an exception and an exception to the exception.

What and how of socket hijacking
Often a ServerSocket is opened without specifying a particular IP address to bind to. So the socket essentially binds to all available IP address of the machine. This is simple for the programmer. However it introduces a security hole. Any application can bind to a specific IP address of the same machine and on the same port. The original server socket still binds on the remaining port. In essence the port has been hijacked by the new application for a specific IP address. This is socket hijacking.

Java support for socket hijacking
Starting with JDK 1.4 Java supports the method ServerSocket.setReuseAddress(boolean). It allows you to hijack a port for a particular IP address as described above. Here is a sample code which allows you to hijack a server socket.

Code
ServerSocket ssock = new ServerSocket();
ssock.setReuseAddress(true); // The magic
ssock.bind(new InetSocketAddress(addr, i)); // addr = IP, i = port
Socket sock = ssock.accept();
// Do your thing with the accepted connection
sock.close();

Discussion
January 6, 2010: 10:39 pm

This is getting a bit more subjective, but I much prefer the Zune Marketplace. The interface is colorful, has more flair, and some cool features like ‘Mixview’ that let you quickly see related albums, songs, or other users related to what you’re listening to. Clicking on one of those will center on that item, and another set of “neighbors” will come into view, allowing you to navigate around exploring by similar artists, songs, or users. Speaking of users, the Zune “Social” is also great fun, letting you find others with shared tastes and becoming friends with them. You then can listen to a playlist created based on an amalgamation of what all your friends are listening to, which is also enjoyable. Those concerned with privacy will be relieved to know you can prevent the public from seeing your personal listening habits if you so choose.


ami ku4
December 16, 2009: 2:15 pm

will this server socket implementation block access to all other services trying to use this port?


arun
December 5, 2009: 6:02 am

how can monotor cpu load,user loging and monitor network load blance….


Jame
February 19, 2009: 8:44 am

How can I block the packets when socket is connected??


Jame
February 19, 2009: 8:03 am

when the connection was accept how to drop the packet ?

November 18, 2007: 6:54 am

Does your computer (on which you are running this program) have this IP address?

Can you ping it?


Tero Lehtinen
November 16, 2007: 4:22 pm

I tried this hijacking, but did not get it work.
Only IP address I could bind to was localhost.
All other IP’s throw java.net.BindException: Cannot assign requested address: JVM_Bind?

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :