Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple DDoS Mitigation [<20 lines]
#1
This is a simple *nix DDoS mitigation script I wrote for my own server. It uses some AWK magic, with netstat, to show connections per IP on the server. If an IP has more connections then the set limit, a NullRoute will be added for the offending IP. It will then wait the specified time and repeat. This has proved to be effective with simple DDoS attacks.

CONLIMIT = Maximum connections from a single IP
SLEEP = Time in seconds to wait before repeating the cycle

Code:
#!/usr/bin/env python
import os, time
CONLIMIT = 20
SLEEP = 12
Round = 0
Banned = 0
while True:
Round += 1
for Line in os.popen("netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n", "r").read().split("\n"):
  List = Line.split(" ")
  try:
   if int(List[-2]) > CONLIMIT:
    os.system( "route add %s gw 127.0.0.1 lo" % ( List[ -1 ] ) )
    print "Banning %s...." % ( List[ -1 ] )
    Banned += 1
  except Exception:
    pass
print "Round: %s Bans: %s" % ( str(Round), str(Banned) )
time.sleep(SLEEP)
[Image: nv70ad.png]
Terrorcore, unleash, extermination
Hyper real, cold blood, determination
fudge them, I like this sensation
Incredible, I from the annihilation
Reply
#2
wow awesome script ^__^
[Image: sig.php]
Reply
#3
How does it add it I don't understand python, does it use iptables or what? I found a script that uses iptables and looks just like this one, same netstat command. (netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n)


http://deflate.medialayer.com/
Reply
#4
Great script. This seemed to work well on your forums Smile
Reply
#5
Great script, code looks neat Smile
Reply
#6
(10-05-2009, 02:50 PM)MyNameIs940 Wrote: How does it add it I don't understand python, does it use iptables or what? I found a script that uses iptables and looks just like this one, same netstat command. (netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n)


http://deflate.medialayer.com/

yeah thats how it counts, using AWK
[Image: nv70ad.png]
Terrorcore, unleash, extermination
Hyper real, cold blood, determination
fudge them, I like this sensation
Incredible, I from the annihilation
Reply
#7
(10-05-2009, 03:54 PM)Fallen Wrote: yeah thats how it counts, using AWK

Ah ok, but this still wastes your banwidth which sucks Sad but atleast people can still have access to your site.
Reply
#8
(10-05-2009, 04:00 PM)MyNameIs940 Wrote: Ah ok, but this still wastes your banwidth which sucks Sad but atleast people can still have access to your site.

Better than having low level skids DDoSing your site with a basic ddos. At least it still allows access for the honest user.


Great script Fallen, and it's short, neat and easy to understand too. Big Grin
[Image: wa3bbk.jpg]
Help us help you...
Reply
#9
Awesome script dude.
Thnx.
Giveaway - A Rare Autoblog Guide. PM me for it, by clicking here.

Hey guys. It's Dr.Viper from HF. Howdy.
o HTML and PHP based website design.
o SEO/SEM services.
o Logo & GFX Design services.
And above all, everyone know what i'm good at HF... So,
o Security Consultancy.
Reply
#10
You always make good programs. Tongue
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)