13 Replies - 1765 Views - Last Post: 28 July 2011 - 02:31 AM Rate Topic: -----

#1 Mommy   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Bottom level protection of java server from DoS attacks?

Posted 26 July 2011 - 05:41 PM

Hi guys.

I've written some a very basic server for a thing I'm trying out.

Basically the server is multi-threaded and currently accepts all connections. I don't want it to do this however, as someone could easily connect a lot of times over a little period of time, depleting my server resources, eventually crashing the server, or making it slow for other users.

I could probably implement some sort of solution that checks up the connecting IP against all connected IP addresses and denying to process the request if one connection already existed.

This still poses a problem however. As long as I actually handle any connections on a code base like that, it is taking up resources, albeit not as many as it would, if the connection would be processed, but still resources.

I'm essentially looking for something that can provide bottom level protection from this problem.

I guess I'm asking for an advanced firewall, but haven't been able to find anything appropriate for this set-up, that runs on Windows.

Also, the code part is very simple:

try {
            ServerSocket serverSock = new ServerSocket(80);
            while (true) {
                Socket sock = serverSock.accept();
                new Thread(new listenThread(sock)).start();
            }
        } catch (IOException ex) {
            methods.log("Couldn't create server. Port in use.");
            ex.printStackTrace();
        }


Hope you guys can help me :)

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Bottom level protection of java server from DoS attacks?

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Bottom level protection of java server from DoS attacks?

Posted 26 July 2011 - 05:49 PM

create a HashMap<IPAddress, Time>
When a connection request is made check if the IPAddress of the client is in the HashMap, if it is compare the actual time with the one stored in the HashMap
If the delta time is less than your number, refuse the connection
else
accept it and store the IPAdress and the actual time in the HashMap
Was This Post Helpful? 1
  • +
  • -

#3 Mommy   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Re: Bottom level protection of java server from DoS attacks?

Posted 26 July 2011 - 05:56 PM

Figured I'd do something like this. Should be pretty efficient since it's a HashMap too..

Had just hoped there would be some soluions like iptables for windows or something similar.

Thanks :)
Was This Post Helpful? 0
  • +
  • -

#4 Mommy   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Re: Bottom level protection of java server from DoS attacks?

Posted 26 July 2011 - 06:20 PM

How would something like this work ? (having efficiency in mind) :

package inforeader;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

public class InfoReader {

    public static mainFrame gui;
    public static HashMap h = new HashMap(32);
    public static String IP;
    public static final long timeout = 1000;
    public static String oldTime;


    public static void main(String[] args) {

        gui = new mainFrame();

        try {
            ServerSocket serverSock = new ServerSocket(80);
            while (true) {
                Socket sock = serverSock.accept();
                IP = "" + sock.getInetAddress();
                if (canContinue(IP)) {
                    new Thread(new listenThread(sock)).start();
                } else {
                    sock.close();
                }
            }
        } catch (IOException ex) {
            methods.log("Couldn't create server. Port in use");
            ex.printStackTrace();
        }
    }

    private static boolean canContinue(String IP) {
        if ((oldTime = "" + h.get(IP)) == null) {
            h.put(IP, new Long(System.currentTimeMillis()));
            return true;
        } else if ((System.currentTimeMillis() - Long.parseLong(oldTime)) >= timeout) {
            h.put(IP, new Long(System.currentTimeMillis()));
            return true;
        } else {
            return false;
        }
    }
}



This post has been edited by Mommy: 26 July 2011 - 06:33 PM

Was This Post Helpful? 0
  • +
  • -

#5 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1576
  • View blog
  • Posts: 3,548
  • Joined: 05-April 11

Re: Bottom level protection of java server from DoS attacks?

Posted 26 July 2011 - 06:50 PM

Why are you using the oldTime String in this if statement?
if ((oldTime = "" + h.get(IP)) == null) {

Also in line 42 oldTime will always be "". You have just assigned it to "" in the if statement above.
You will need to do some work with your timeout logic I think

EDIT: Sorry I read the assignment wrong.. my bad
Still the if statement is not right since oldTime + h.get(IP) never will be equal null.

This post has been edited by CasiOo: 26 July 2011 - 06:57 PM

Was This Post Helpful? 1
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Bottom level protection of java server from DoS attacks?

Posted 26 July 2011 - 06:51 PM

At least, since generic, make your HashMap<String, Long>

Long lastTime = h.get(IP);
long now = System.currentTimeMillis();
if(lastTime == null) {
   h.add(IP, now);
   return true;
}
if(now - lastTime < delay) {
   h.add(IP, now);
   return true;
}
return false;


Was This Post Helpful? 1
  • +
  • -

#7 Mommy   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Re: Bottom level protection of java server from DoS attacks?

Posted 27 July 2011 - 06:55 AM

View PostCasiOo, on 26 July 2011 - 07:50 PM, said:

Why are you using the oldTime String in this if statement?
if ((oldTime = "" + h.get(IP)) == null) {

Also in line 42 oldTime will always be "". You have just assigned it to "" in the if statement above.
You will need to do some work with your timeout logic I think

EDIT: Sorry I read the assignment wrong.. my bad
Still the if statement is not right since oldTime + h.get(IP) never will be equal null.


Yeah the if statement should check if i was simply "".. Wrote it last night, perhaps a bi to late in the evening.

Thanks for pointing it out.


View Postpbl, on 26 July 2011 - 07:51 PM, said:

At least, since generic, make your HashMap<String, Long>

Long lastTime = h.get(IP);
long now = System.currentTimeMillis();
if(lastTime == null) {
   h.add(IP, now);
   return true;
}
if(now - lastTime < delay) {
   h.add(IP, now);
   return true;
}
return false;



Gonna use pretty much line by line what you wrote there :) thanks.
Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3541
  • View blog
  • Posts: 16,143
  • Joined: 20-September 08

Re: Bottom level protection of java server from DoS attacks?

Posted 27 July 2011 - 07:54 AM

Quote

This still poses a problem however. As long as I actually handle any connections on a code base like that, it is taking up resources, albeit not as many as it would, if the connection would be processed, but still resources.


That's right and that's why it's already too late to do it at the application level. It needs to be done at a much lower network layer and that's not a Java issue.
Was This Post Helpful? 1
  • +
  • -

#9 Mommy   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Re: Bottom level protection of java server from DoS attacks?

Posted 27 July 2011 - 03:19 PM

View Postg00se, on 27 July 2011 - 08:54 AM, said:

Quote

This still poses a problem however. As long as I actually handle any connections on a code base like that, it is taking up resources, albeit not as many as it would, if the connection would be processed, but still resources.


That's right and that's why it's already too late to do it at the application level. It needs to be done at a much lower network layer and that's not a Java issue.


I realize that this is not necessarily a java-specific topic.

Do you have any ideas or suggestions regarding more deeply integrated protection?
Was This Post Helpful? 0
  • +
  • -

#10 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3541
  • View blog
  • Posts: 16,143
  • Joined: 20-September 08

Re: Bottom level protection of java server from DoS attacks?

Posted 27 July 2011 - 03:22 PM

Not really for Windows i'm afraid
Was This Post Helpful? 0
  • +
  • -

#11 Mommy   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Re: Bottom level protection of java server from DoS attacks?

Posted 27 July 2011 - 03:51 PM

View Postg00se, on 27 July 2011 - 04:22 PM, said:

Not really for Windows i'm afraid


Well the server is gonna run on ubuntu :P. Any luck there?
Was This Post Helpful? 0
  • +
  • -

#12 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Bottom level protection of java server from DoS attacks?

Posted 27 July 2011 - 04:34 PM

View PostMommy, on 27 July 2011 - 06:19 PM, said:

Do you have any ideas or suggestions regarding more deeply integrated protection?

Like what ? Does really call protection the fact that you reject multiple connections from the same client.
Was This Post Helpful? 0
  • +
  • -

#13 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3541
  • View blog
  • Posts: 16,143
  • Joined: 20-September 08

Re: Bottom level protection of java server from DoS attacks?

Posted 28 July 2011 - 01:42 AM

Quote

Well the server is gonna run on ubuntu . Any luck there?



Absolutely. Look into iptables
Was This Post Helpful? 0
  • +
  • -

#14 Mommy   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Re: Bottom level protection of java server from DoS attacks?

Posted 28 July 2011 - 02:31 AM

Yeah I think I'm gonna go with iptables in my final configuration and then just disregard it while developing. thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1