Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - What are the vulnerabilities of a port-listening program?

I've recently created a fairly simple IRC client and server in Java but to make it fully functional I had to port forward. It occurred to me that there were probably some security issues with opening ports so I did some research. Everywhere, I found people saying that 'the biggest vulnerability is the program listening to the port'.

So my questions are:

  1. What exactly can be exploited in a Java program which listens to a port and writes incoming data to a string?

  2. As the developer of the software, how can I prevent these vulnerabilities?

like image 850
user1248420 Avatar asked Dec 21 '22 01:12

user1248420


2 Answers

There are numerous ways an attacker can take advantage of a known open port ranging from exploiting bugs in TCP implementation, causing denial-of-service by tricking your server into performing expensive computation (remember recent 2.2250738585072012e-308 bug?), causing buffer overflows to crash your program or to even make it execute arbitrary code.

Platform security

There have been a few vulnerabilities in TCP implementation on some operating systems in which an attacker relied on knowing an open port on a target host, e.g. SYN flood attack. These have been largely mitigated in all major OSes out there, but whoever is responsible for the security of your host should be on a constant lookout for recent security issues in the platform.

Server security

Vulnerabilities in the OS and TCP implementation aside, there are also potential issues connected with the server itself. If your server can perform security-relevant operations in response to requests it receives, an attacker can take advantage of it. These include reading and writing files, allocating large chunks of memory, sending queries to databases etc.

From developer perspective

Ensuring that your server can run with low privileges and low resources, that it validates all input received from the user and escapes all output it sends to other systems and that it does not perform any unnecessary security-relevant actions are the first steps to making it secure. If you do need to perform security-related operations, you may want to encapsulate them in a separate process and use IPC. Extensive testing of your program is very hard, but critical to its security as well.

From admin perspective

Critical points are making sure recent security updates in the OS have been applied, that your server actually does run with lowest privileges possible and that it is unable to exhaust critical system resources (e.g. CPU, RAM, open file descriptors, open TCP connections etc).

like image 177
Adam Zalcman Avatar answered Jan 11 '23 22:01

Adam Zalcman


Opening a port, reading incoming bytes and storing them in a string is safe with Java. Just be prepared to get illegal or "malicious" content. Trivial example: an application uses the received bytes for database queries and someone sends "sql injection code"...

There's more risk with programming language where buffer overflow is possible. Then one could use the connection to inject and execute machine code - if the listening application is vulnerable.

Preventing is pretty easy: validate the input and drop every illegal message. Add some tests that send illegal content and check, if those inputs are properly rejected.

like image 38
Andreas Dolk Avatar answered Jan 11 '23 22:01

Andreas Dolk